diff --git a/src/Item/FieldsHelper.php b/src/Item/FieldsHelper.php index 02effdc..e4966fb 100644 --- a/src/Item/FieldsHelper.php +++ b/src/Item/FieldsHelper.php @@ -96,13 +96,13 @@ public function __construct(EntityFieldManagerInterface $entityFieldManager, Ent /** * {@inheritdoc} */ - public function isTextType($type, array $text_types = array('text')) { - if (in_array($type, $text_types)) { + public function isTextType($type, array $textTypes = array('text')) { + if (in_array($type, $textTypes)) { return TRUE; } - $data_type = $this->dataTypeManager->createInstance($type); - if ($data_type && !$data_type->isDefault()) { - return in_array($data_type->getFallbackType(), $text_types); + $dataType = $this->dataTypeManager->createInstance($type); + if ($dataType && !$dataType->isDefault()) { + return in_array($dataType->getFallbackType(), $textTypes); } return FALSE; } @@ -114,8 +114,8 @@ public function getFieldTypeMapping() { // Check the cache first. if (!isset($this->fieldTypeMapping)) { // It's easier to write and understand this array in the form of - // $search_api_field_type => array($data_types) and flip it below. - $default_mapping = array( + // $searchApiFieldType => array($dataTypes) and flip it below. + $defaultMapping = array( 'text' => array( 'field_item:string_long.string', 'field_item:text_long.string', @@ -152,9 +152,9 @@ public function getFieldTypeMapping() { ), ); - foreach ($default_mapping as $search_api_type => $data_types) { - foreach ($data_types as $data_type) { - $mapping[$data_type] = $search_api_type; + foreach ($defaultMapping as $searchApiType => $dataTypes) { + foreach ($dataTypes as $dataType) { + $mapping[$dataType] = $searchApiType; } } @@ -173,8 +173,8 @@ public function getFieldTypeMapping() { */ public function getDataTypeFallbackMapping(IndexInterface $index) { // Check the cache first. - $index_id = $index->id(); - if (empty($this->dataTypeFallbackMapping[$index_id])) { + $indexId = $index->id(); + if (empty($this->dataTypeFallbackMapping[$indexId])) { $server = NULL; try { $server = $index->getServerInstance(); @@ -183,19 +183,19 @@ public function getDataTypeFallbackMapping(IndexInterface $index) { // If the server isn't available, just ignore it here and return all // custom types. } - $this->dataTypeFallbackMapping[$index_id] = array(); - $data_types = $this->dataTypeManager->getInstances(); - foreach ($data_types as $type_id => $data_type) { + $this->dataTypeFallbackMapping[$indexId] = array(); + $dataTypes = $this->dataTypeManager->getInstances(); + foreach ($dataTypes as $typeId => $dataType) { // We know for sure that we do not need to fall back for the default // data types as they are always present and are required to be // supported by all backends. - if (!$data_type->isDefault() && (!$server || !$server->supportsDataType($type_id))) { - $this->dataTypeFallbackMapping[$index_id][$type_id] = $data_type->getFallbackType(); + if (!$dataType->isDefault() && (!$server || !$server->supportsDataType($typeId))) { + $this->dataTypeFallbackMapping[$indexId][$typeId] = $dataType->getFallbackType(); } } } - return $this->dataTypeFallbackMapping[$index_id]; + return $this->dataTypeFallbackMapping[$indexId]; } /** @@ -204,20 +204,20 @@ public function getDataTypeFallbackMapping(IndexInterface $index) { public function extractFields(ComplexDataInterface $item, array $fields) { // Figure out which fields are directly on the item and which need to be // extracted from nested items. - $direct_fields = array(); - $nested_fields = array(); + $directFields = array(); + $nestedFields = array(); foreach (array_keys($fields) as $key) { if (strpos($key, ':') !== FALSE) { list($direct, $nested) = explode(':', $key, 2); - $nested_fields[$direct][$nested] = $fields[$key]; + $nestedFields[$direct][$nested] = $fields[$key]; } else { - $direct_fields[] = $key; + $directFields[] = $key; } } // Extract the direct fields. $properties = $item->getProperties(TRUE); - foreach ($direct_fields as $key) { + foreach ($directFields as $key) { if (empty($properties[$key])) { continue; } @@ -227,24 +227,24 @@ public function extractFields(ComplexDataInterface $item, array $fields) { } } // Recurse for all nested fields. - foreach ($nested_fields as $direct => $fields_nested) { + foreach ($nestedFields as $direct => $fieldsNested) { if (empty($properties[$direct])) { continue; } - $item_nested = $item->get($direct); - if ($item_nested instanceof DataReferenceInterface) { - $item_nested = $item_nested->getTarget(); + $itemNested = $item->get($direct); + if ($itemNested instanceof DataReferenceInterface) { + $itemNested = $itemNested->getTarget(); } - if ($item_nested instanceof EntityInterface) { - $item_nested = $item_nested->getTypedData(); + if ($itemNested instanceof EntityInterface) { + $itemNested = $itemNested->getTypedData(); } - if ($item_nested instanceof ComplexDataInterface && !$item_nested->isEmpty()) { - $this->extractFields($item_nested, $fields_nested); + if ($itemNested instanceof ComplexDataInterface && !$itemNested->isEmpty()) { + $this->extractFields($itemNested, $fieldsNested); } - elseif ($item_nested instanceof ListInterface && !$item_nested->isEmpty()) { - foreach ($item_nested as $list_item) { - if ($list_item instanceof ComplexDataInterface && !$list_item->isEmpty()) { - $this->extractFields($list_item, $fields_nested); + elseif ($itemNested instanceof ListInterface && !$itemNested->isEmpty()) { + foreach ($itemNested as $listItem) { + if ($listItem instanceof ComplexDataInterface && !$listItem->isEmpty()) { + $this->extractFields($listItem, $fieldsNested); } } } @@ -293,30 +293,30 @@ public function extractFieldValues(TypedDataInterface $data) { * {@inheritdoc} */ public function getNestedProperties(ComplexDataDefinitionInterface $property) { - $nested_properties = $property->getPropertyDefinitions(); + $nestedProperties = $property->getPropertyDefinitions(); if ($property instanceof EntityDataDefinitionInterface) { $bundles = $this->entityBundleInfo ->getBundleInfo($property->getEntityTypeId()); - foreach ($bundles as $bundle => $bundle_label) { - $bundle_properties = $this->entityFieldManager + foreach ($bundles as $bundle => $bundleLabel) { + $bundleProperties = $this->entityFieldManager ->getFieldDefinitions($property->getEntityTypeId(), $bundle); - $nested_properties += $bundle_properties; + $nestedProperties += $bundleProperties; } } - return $nested_properties; + return $nestedProperties; } /** * {@inheritdoc} */ - public function retrieveNestedProperty(array $properties, $property_path) { - list($key, $nested_path) = Utility::splitPropertyPath($property_path, FALSE); + public function retrieveNestedProperty(array $properties, $propertyPath) { + list($key, $nestedPath) = Utility::splitPropertyPath($propertyPath, FALSE); if (!isset($properties[$key])) { return NULL; } $property = $this->getInnerProperty($properties[$key]); - if (!isset($nested_path)) { + if (!isset($nestedPath)) { return $property; } @@ -324,7 +324,7 @@ public function retrieveNestedProperty(array $properties, $property_path) { return NULL; } - return $this->retrieveNestedProperty($this->getNestedProperties($property), $nested_path); + return $this->retrieveNestedProperty($this->getNestedProperties($property), $nestedPath); } /** @@ -343,8 +343,8 @@ public function getInnerProperty(DataDefinitionInterface $property) { /** * {@inheritdoc} */ - public function isFieldIdReserved($field_id) { - return substr($field_id, 0, 11) == 'search_api_'; + public function isFieldIdReserved($fieldId) { + return substr($fieldId, 0, 11) == 'search_api_'; } /** @@ -357,25 +357,25 @@ public function createItem(IndexInterface $index, $id, DatasourceInterface $data /** * {@inheritdoc} */ - public function createItemFromObject(IndexInterface $index, ComplexDataInterface $original_object, $id = NULL, DatasourceInterface $datasource = NULL) { + public function createItemFromObject(IndexInterface $index, ComplexDataInterface $originalObject, $id = NULL, DatasourceInterface $datasource = NULL) { if (!isset($id)) { if (!isset($datasource)) { throw new \InvalidArgumentException('Need either an item ID or the datasource to create a search item from an object.'); } - $id = Utility::createCombinedId($datasource->getPluginId(), $datasource->getItemId($original_object)); + $id = Utility::createCombinedId($datasource->getPluginId(), $datasource->getItemId($originalObject)); } $item = $this->createItem($index, $id, $datasource); - $item->setOriginalObject($original_object); + $item->setOriginalObject($originalObject); return $item; } /** * {@inheritdoc} */ - public function createField(IndexInterface $index, $field_identifier, $field_info = array()) { - $field = new Field($index, $field_identifier); + public function createField(IndexInterface $index, $fieldIdentifier, $fieldInfo = array()) { + $field = new Field($index, $fieldIdentifier); - foreach ($field_info as $key => $value) { + foreach ($fieldInfo as $key => $value) { $method = 'set' . Container::camelize($key); if (method_exists($field, $method)) { $field->$method($value); @@ -388,56 +388,56 @@ public function createField(IndexInterface $index, $field_identifier, $field_inf /** * {@inheritdoc} */ - public function createFieldFromProperty(IndexInterface $index, DataDefinitionInterface $property, $datasource_id, $property_path, $field_id = NULL, $type = NULL) { - if (!isset($field_id)) { - $field_id = $this->getNewFieldId($index, $property_path); + public function createFieldFromProperty(IndexInterface $index, DataDefinitionInterface $property, $datasourceId, $propertyPath, $fieldId = NULL, $type = NULL) { + if (!isset($fieldId)) { + $fieldId = $this->getNewFieldId($index, $propertyPath); } if (!isset($type)) { - $type_mapping = $this->getFieldTypeMapping(); - $property_type = $property->getDataType(); - if (isset($type_mapping[$property_type])) { - $type = $type_mapping[$property_type]; + $typeMapping = $this->getFieldTypeMapping(); + $propertyType = $property->getDataType(); + if (isset($typeMapping[$propertyType])) { + $type = $typeMapping[$propertyType]; } else { - $property_name = $property->getLabel(); - throw new SearchApiException("No default data type mapping could be found for property '$property_name' ($property_path) of type '$property_type'."); + $propertyName = $property->getLabel(); + throw new SearchApiException("No default data type mapping could be found for property '$propertyName' ($propertyPath) of type '$propertyType'."); } } - $field_info = array( + $fieldInfo = array( 'label' => $property->getLabel(), - 'datasource_id' => $datasource_id, - 'property_path' => $property_path, + 'datasource_id' => $datasourceId, + 'property_path' => $propertyPath, 'type' => $type, ); if ($property instanceof ConfigurablePropertyInterface) { - $field_info['configuration'] = $property->defaultConfiguration(); + $fieldInfo['configuration'] = $property->defaultConfiguration(); } - return $this->createField($index, $field_id, $field_info); + return $this->createField($index, $fieldId, $fieldInfo); } /** * {@inheritdoc} */ - public function getNewFieldId(IndexInterface $index, $property_path) { - list(, $suggested_id) = Utility::splitPropertyPath($property_path); + public function getNewFieldId(IndexInterface $index, $propertyPath) { + list(, $suggestedId) = Utility::splitPropertyPath($propertyPath); // Avoid clashes with reserved IDs by removing the reserved "search_api_" // from our suggested ID. - $suggested_id = str_replace('search_api_', '', $suggested_id); + $suggestedId = str_replace('search_api_', '', $suggestedId); - $field_id = $suggested_id; + $fieldId = $suggestedId; $i = 0; - while ($index->getField($field_id)) { - $field_id = $suggested_id . '_' . ++$i; + while ($index->getField($fieldId)) { + $fieldId = $suggestedId . '_' . ++$i; } - while ($this->isFieldIdReserved($field_id)) { - $field_id = '_' . $field_id; + while ($this->isFieldIdReserved($fieldId)) { + $fieldId = '_' . $fieldId; } - return $field_id; + return $fieldId; } } diff --git a/src/Item/FieldsHelperInterface.php b/src/Item/FieldsHelperInterface.php index 84450f9..316335d 100644 --- a/src/Item/FieldsHelperInterface.php +++ b/src/Item/FieldsHelperInterface.php @@ -19,13 +19,13 @@ * * @param string $type * The type to check. - * @param string[] $text_types + * @param string[] $textTypes * (optional) An array of types to be considered as text. * * @return bool * TRUE if $type is one of the specified types, FALSE otherwise. */ - public function isTextType($type, array $text_types = array('text')); + public function isTextType($type, array $textTypes = array('text')); /** * Retrieves the mapping for known data types to Search API's internal types. @@ -105,13 +105,13 @@ public function getNestedProperties(ComplexDataDefinitionInterface $property); * * @param \Drupal\Core\TypedData\DataDefinitionInterface[] $properties * The base properties, keyed by property name. - * @param string $property_path + * @param string $propertyPath * The property path of the property to retrieve. * * @return \Drupal\Core\TypedData\DataDefinitionInterface|null * The requested property, or NULL if it couldn't be found. */ - public function retrieveNestedProperty(array $properties, $property_path); + public function retrieveNestedProperty(array $properties, $propertyPath); /** * Retrieves the inner property definition of a compound property definition. @@ -133,13 +133,13 @@ public function getInnerProperty(DataDefinitionInterface $property); * We define all field IDs starting with "search_api_" as reserved, to be safe * for future additions (and from clashing with backend-defined fields). * - * @param string $field_id + * @param string $fieldId * The field ID. * * @return bool * TRUE if the field ID is reserved, FALSE if it can be used normally. */ - public function isFieldIdReserved($field_id); + public function isFieldIdReserved($fieldId); /** * Creates a search item object. @@ -162,7 +162,7 @@ public function createItem(IndexInterface $index, $id, DatasourceInterface $data * * @param \Drupal\search_api\IndexInterface $index * The item's search index. - * @param \Drupal\Core\TypedData\ComplexDataInterface $original_object + * @param \Drupal\Core\TypedData\ComplexDataInterface $originalObject * The original object to wrap. * @param string $id * (optional) The item's (combined) ID. If not set, it will be determined @@ -178,22 +178,22 @@ public function createItem(IndexInterface $index, $id, DatasourceInterface $data * @throws \InvalidArgumentException * Thrown if both $datasource and $id are NULL. */ - public function createItemFromObject(IndexInterface $index, ComplexDataInterface $original_object, $id = NULL, DatasourceInterface $datasource = NULL); + public function createItemFromObject(IndexInterface $index, ComplexDataInterface $originalObject, $id = NULL, DatasourceInterface $datasource = NULL); /** * Creates a new field object wrapping a field of the given index. * * @param \Drupal\search_api\IndexInterface $index * The index to which this field should be attached. - * @param string $field_identifier + * @param string $fieldIdentifier * The field identifier. - * @param array $field_info + * @param array $fieldInfo * (optional) An array with further configuration for the field. * * @return \Drupal\search_api\Item\FieldInterface * A new field object. */ - public function createField(IndexInterface $index, $field_identifier, $field_info = array()); + public function createField(IndexInterface $index, $fieldIdentifier, $fieldInfo = array()); /** * Creates a new field on an index based on a property. @@ -204,13 +204,13 @@ public function createField(IndexInterface $index, $field_identifier, $field_inf * The search index. * @param \Drupal\Core\TypedData\DataDefinitionInterface $property * The data definition of the property. - * @param string|null $datasource_id + * @param string|null $datasourceId * The ID of the index's datasource this property belongs to, or NULL if it * is a datasource-independent property. - * @param string $property_path + * @param string $propertyPath * The property's property path within the property structure of the * datasource. - * @param string|null $field_id + * @param string|null $fieldId * (optional) The identifier to use for the field. If not set, a new unique * field identifier on the index will be chosen automatically. * @param string|null $type @@ -223,20 +223,20 @@ public function createField(IndexInterface $index, $field_identifier, $field_inf * @throws \Drupal\search_api\SearchApiException * Thrown if no type was given and no default could be determined. */ - public function createFieldFromProperty(IndexInterface $index, DataDefinitionInterface $property, $datasource_id, $property_path, $field_id = NULL, $type = NULL); + public function createFieldFromProperty(IndexInterface $index, DataDefinitionInterface $property, $datasourceId, $propertyPath, $fieldId = NULL, $type = NULL); /** * Finds a new unique field identifier on the given index. * * @param \Drupal\search_api\IndexInterface $index * The search index. - * @param string $property_path + * @param string $propertyPath * The property path on which the field identifier should be based. Only the * last component of the property path will be considered. * * @return string * A new unique field identifier on the given index. */ - public function getNewFieldId(IndexInterface $index, $property_path); + public function getNewFieldId(IndexInterface $index, $propertyPath); } diff --git a/src/Processor/FieldsProcessorPluginBase.php b/src/Processor/FieldsProcessorPluginBase.php index 1f31c1e..cd7c261 100644 --- a/src/Processor/FieldsProcessorPluginBase.php +++ b/src/Processor/FieldsProcessorPluginBase.php @@ -7,12 +7,13 @@ use Drupal\Core\Plugin\PluginFormInterface; use Drupal\Core\Render\Element; use Drupal\search_api\Item\FieldInterface; +use Drupal\search_api\Item\FieldsHelperInterface; use Drupal\search_api\Plugin\PluginFormTrait; use Drupal\search_api\Plugin\search_api\data_type\value\TextValueInterface; use Drupal\search_api\Query\ConditionGroupInterface; use Drupal\search_api\Query\ConditionInterface; use Drupal\search_api\Query\QueryInterface; -use Drupal\search_api\Utility; +use Symfony\Component\DependencyInjection\ContainerInterface; /** * Provides a base class for processors that work on individual fields. @@ -38,12 +39,54 @@ use PluginFormTrait; + /** + * The field helper. + * + * @var \Drupal\search_api\Item\FieldsHelperInterface|null + */ + protected $fieldHelper; + // @todo Add defaultConfiguration() implementation and find a cleaner solution // for all the isset($this->configuration['fields']) checks. /** * {@inheritdoc} */ + public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) { + /** @var static $processor */ + $processor = parent::create($container, $configuration, $plugin_id, $plugin_definition); + + $processor->setFieldHelper($container->get('search_api.fields_helper')); + + return $processor; + } + + /** + * Retrieves the field helper. + * + * @return \Drupal\search_api\Item\FieldsHelperInterface + * The field helper. + */ + public function getFieldHelper() { + return $this->fieldHelper ?: \Drupal::service('search_api.fields_helper'); + } + + /** + * Sets the field helper. + * + * @param \Drupal\search_api\Item\FieldsHelperInterface $field_helper + * The new field helper. + * + * @return $this + */ + public function setFieldHelper(FieldsHelperInterface $field_helper) { + $this->fieldHelper = $field_helper; + return $this; + } + + /** + * {@inheritdoc} + */ public function buildConfigurationForm(array $form, FormStateInterface $form_state) { $fields = $this->index->getFields(); $field_options = array(); @@ -287,7 +330,7 @@ protected function testField($name, FieldInterface $field) { * TRUE if fields of that type should be processed, FALSE otherwise. */ protected function testType($type) { - return Utility::isTextType($type, array('text', 'string')); + return $this->getFieldHelper()->isTextType($type, array('text', 'string')); } /** diff --git a/tests/src/Unit/Plugin/Processor/AddURLTest.php b/tests/src/Unit/Plugin/Processor/AddURLTest.php index f8a37d5..5db77ec 100644 --- a/tests/src/Unit/Plugin/Processor/AddURLTest.php +++ b/tests/src/Unit/Plugin/Processor/AddURLTest.php @@ -38,7 +38,7 @@ class AddURLTest extends UnitTestCase { protected function setUp() { parent::setUp(); - $this->setUpDataTypePlugin(); + $this->setUpMockContainer(); // Create a mock for the URL to be returned. $url = $this->getMockBuilder('Drupal\Core\Url') diff --git a/tests/src/Unit/Plugin/Processor/AggregatedFieldsTest.php b/tests/src/Unit/Plugin/Processor/AggregatedFieldsTest.php index df9918d..74c1463 100644 --- a/tests/src/Unit/Plugin/Processor/AggregatedFieldsTest.php +++ b/tests/src/Unit/Plugin/Processor/AggregatedFieldsTest.php @@ -83,7 +83,7 @@ protected function setUp() { ), 'search_api_index'); $this->processor = new AggregatedFields(array('index' => $this->index), 'aggregated_field', array()); $this->index->addProcessor($this->processor); - $this->setUpDataTypePlugin(); + $this->setUpMockContainer(); } /** @@ -259,6 +259,10 @@ public function testFieldExtraction() { ->willReturnMap(array( array('foo', $bar_foo_property), )); + $bar_property->method('getProperties') + ->willReturn(array( + 'foo' => TRUE, + )); $foobar_property = $this->getMock('Drupal\Core\TypedData\TypedDataInterface'); $foobar_property->method('getValue') ->willReturn('wrong_value2'); @@ -269,6 +273,11 @@ public function testFieldExtraction() { array('bar', $bar_property), array('foobar', $foobar_property), )); + $object->method('getProperties') + ->willReturn(array( + 'bar' => TRUE, + 'foobar' => TRUE, + )); /** @var \Drupal\search_api\IndexInterface|\PHPUnit_Framework_MockObject_MockObject $index */ $index = $this->getMock('Drupal\search_api\IndexInterface'); diff --git a/tests/src/Unit/Plugin/Processor/FieldsProcessorPluginBaseTest.php b/tests/src/Unit/Plugin/Processor/FieldsProcessorPluginBaseTest.php index e1c3284..d27c99f 100644 --- a/tests/src/Unit/Plugin/Processor/FieldsProcessorPluginBaseTest.php +++ b/tests/src/Unit/Plugin/Processor/FieldsProcessorPluginBaseTest.php @@ -39,7 +39,7 @@ class FieldsProcessorPluginBaseTest extends UnitTestCase { public function setUp() { parent::setUp(); - $this->setUpDataTypePlugin(); + $this->setUpMockContainer(); $this->index = $this->getMock('Drupal\search_api\IndexInterface'); $this->index->expects($this->any()) ->method('status') diff --git a/tests/src/Unit/Plugin/Processor/HighlightTest.php b/tests/src/Unit/Plugin/Processor/HighlightTest.php index 6c43393..6de97c3 100644 --- a/tests/src/Unit/Plugin/Processor/HighlightTest.php +++ b/tests/src/Unit/Plugin/Processor/HighlightTest.php @@ -2,7 +2,6 @@ namespace Drupal\Tests\search_api\Unit\Plugin\Processor; -use Drupal\Core\DependencyInjection\ContainerBuilder; use Drupal\Core\TypedData\DataDefinition; use Drupal\search_api\Item\Field; use Drupal\search_api\Item\ItemInterface; @@ -45,6 +44,8 @@ class HighlightTest extends UnitTestCase { protected function setUp() { parent::setUp(); + $this->setUpMockContainer(); + $this->processor = new Highlight(array(), 'highlight', array()); $this->index = $this->getMock('Drupal\search_api\IndexInterface'); @@ -154,7 +155,7 @@ public function testPostprocessSearchResultsWithResults() { ->will($this->returnValue('foo')); /** @var \Drupal\search_api\Query\QueryInterface $query */ - $field = $this->createField('body', 'entity:node/body'); + $field = $this->createTestField('body', 'entity:node/body'); $this->index->expects($this->atLeastOnce()) ->method('getFields') @@ -200,7 +201,7 @@ public function testPostprocessSearchResultsWithChangedPrefixSuffix() { ->will($this->returnValue(array('#conjunction' => 'AND', 'foo'))); /** @var \Drupal\search_api\Query\QueryInterface $query */ - $field = $this->createField('body', 'entity:node/body'); + $field = $this->createTestField('body', 'entity:node/body'); $this->index->expects($this->atLeastOnce()) ->method('getFields') @@ -243,7 +244,7 @@ public function testPostprocessSearchResultsWithoutHighlight() { ->will($this->returnValue(array('#conjunction' => 'AND', 'foo'))); /** @var \Drupal\search_api\Query\QueryInterface $query */ - $field = $this->createField('body', 'entity:node/body'); + $field = $this->createTestField('body', 'entity:node/body'); $this->index->expects($this->atLeastOnce()) ->method('getFields') @@ -284,7 +285,7 @@ public function testPostprocessSearchResultsWithPreviousHighlighting() { ->will($this->returnValue(array('#conjunction' => 'AND', 'foo'))); /** @var \Drupal\search_api\Query\QueryInterface $query */ - $field = $this->createField('body', 'entity:node/body'); + $field = $this->createTestField('body', 'entity:node/body'); $this->index->expects($this->atLeastOnce()) ->method('getFields') @@ -330,7 +331,7 @@ public function testPostprocessSearchResultsExcerpt() { ->will($this->returnValue(array('#conjunction' => 'AND', 'congue'))); /** @var \Drupal\search_api\Query\QueryInterface $query */ - $field = $this->createField('body', 'entity:node/body'); + $field = $this->createTestField('body', 'entity:node/body'); $this->index->expects($this->atLeastOnce()) ->method('getFields') @@ -373,7 +374,7 @@ public function testPostprocessSearchResultsExerptMatchNearEnd() { ->will($this->returnValue(array('#conjunction' => 'AND', 'diam'))); /** @var \Drupal\search_api\Query\QueryInterface $query */ - $field = $this->createField('body', 'entity:node/body'); + $field = $this->createTestField('body', 'entity:node/body'); $this->index->expects($this->atLeastOnce()) ->method('getFields') @@ -418,7 +419,7 @@ public function testPostprocessSearchResultsWithChangedExcerptLength() { ->will($this->returnValue('congue')); /** @var \Drupal\search_api\Query\QueryInterface $query */ - $field = $this->createField('body', 'entity:node/body'); + $field = $this->createTestField('body', 'entity:node/body'); $this->index->expects($this->atLeastOnce()) ->method('getFields') @@ -463,7 +464,7 @@ public function testPostprocessSearchResultsWithoutExcerpt() { ->will($this->returnValue(array('#conjunction' => 'AND', 'congue'))); /** @var \Drupal\search_api\Query\QueryInterface $query */ - $field = $this->createField('body', 'entity:node/body'); + $field = $this->createTestField('body', 'entity:node/body'); $this->index->expects($this->atLeastOnce()) ->method('getFields') @@ -520,7 +521,7 @@ public function testPostprocessSearchResultsWithComplexKeys() { ->will($this->returnValue($keys)); /** @var \Drupal\search_api\Query\QueryInterface $query */ - $body_field = $this->createField('body', 'entity:node/body'); + $body_field = $this->createTestField('body', 'entity:node/body'); $this->index->expects($this->atLeastOnce()) ->method('getFields') @@ -565,8 +566,8 @@ public function testPostprocessSearchResultsWithTwoFields() { ->will($this->returnValue(array('#conjunction' => 'AND', 'foo'))); /** @var \Drupal\search_api\Query\QueryInterface $query */ - $body_field = $this->createField('body', 'entity:node/body'); - $title_field = $this->createField('title', 'title'); + $body_field = $this->createTestField('body', 'entity:node/body'); + $title_field = $this->createTestField('title', 'title'); $this->index->expects($this->atLeastOnce()) ->method('getFields') @@ -623,7 +624,7 @@ public function testPostprocessSearchResultsWithTwoItems() { ->will($this->returnValue(array('#conjunction' => 'OR', 'foo'))); /** @var \Drupal\search_api\Query\QueryInterface $query */ - $body_field = $this->createField('body', 'entity:node/body'); + $body_field = $this->createTestField('body', 'entity:node/body'); $this->index->expects($this->atLeastOnce()) ->method('getFields') @@ -674,8 +675,8 @@ public function testExcerptExcludeFields() { ->will($this->returnValue(array('#conjunction' => 'AND', 'foo'))); /** @var \Drupal\search_api\Query\QueryInterface $query */ - $body_field = $this->createField('body', 'entity:node/body'); - $title_field = $this->createField('title', 'title'); + $body_field = $this->createTestField('body', 'entity:node/body'); + $title_field = $this->createTestField('title', 'title'); $this->index->expects($this->atLeastOnce()) ->method('getFields') @@ -736,6 +737,10 @@ public function testFieldExtraction() { ->willReturnMap(array( array('foo', $bar_foo_property), )); + $bar_property->method('getProperties') + ->willReturn(array( + 'foo' => TRUE, + )); $foobar_property = $this->getMock('Drupal\Core\TypedData\TypedDataInterface'); $foobar_property->method('getValue') ->willReturn('wrong_value2 foo'); @@ -746,14 +751,19 @@ public function testFieldExtraction() { array('bar', $bar_property), array('foobar', $foobar_property), )); + $object->method('getProperties') + ->willReturn(array( + 'bar' => TRUE, + 'foobar' => TRUE, + )); $this->index->method('getFields') ->willReturn(array( - 'field1' => $this->createField('field1', 'entity:test1/bar:foo'), - 'field2' => $this->createField('field2', 'entity:test2/foobar'), - 'field3' => $this->createField('field3', 'foo'), - 'field4' => $this->createField('field4', 'baz', FALSE), - 'field5' => $this->createField('field5', 'entity:test1/foobar'), + 'field1' => $this->createTestField('field1', 'entity:test1/bar:foo'), + 'field2' => $this->createTestField('field2', 'entity:test2/foobar'), + 'field3' => $this->createTestField('field3', 'foo'), + 'field4' => $this->createTestField('field4', 'baz', FALSE), + 'field5' => $this->createTestField('field5', 'entity:test1/foobar'), )); $this->index->method('getPropertyDefinitions') ->willReturnMap(array( @@ -794,13 +804,6 @@ public function testFieldExtraction() { )); $this->processor->setIndex($this->index); - $container = new ContainerBuilder(); - $data_type_manager = $this->getMockBuilder('Drupal\search_api\DataType\DataTypePluginManager') - ->disableOriginalConstructor() - ->getMock(); - $container->set('plugin.manager.search_api.data_type', $data_type_manager); - \Drupal::setContainer($container); - /** @var \Drupal\search_api\Datasource\DatasourceInterface|\PHPUnit_Framework_MockObject_MockObject $datasource */ $datasource = $this->getMock('Drupal\search_api\Datasource\DatasourceInterface'); $datasource->method('getPluginId') @@ -808,10 +811,10 @@ public function testFieldExtraction() { $item = Utility::createItem($this->index, 'id', $datasource); $item->setOriginalObject($object); - $field = $this->createField('field4', 'baz') + $field = $this->createTestField('field4', 'baz') ->addValue('wrong_value1 foo'); $item->setField('field4', $field); - $field = $this->createField('field5', 'entity:test1/foobar') + $field = $this->createTestField('field5', 'entity:test1/foobar') ->addValue('value1 foo') ->addValue('value2 foo'); $item->setField('field5', $field); @@ -860,7 +863,7 @@ public function testFieldExtraction() { * @return \Drupal\search_api\Item\FieldInterface * A field object. */ - protected function createField($id, $combined_property_path, $text = TRUE) { + protected function createTestField($id, $combined_property_path, $text = TRUE) { $field = new Field($this->index, $id); list ($datasource_id, $property_path) = Utility::splitCombinedId($combined_property_path); $field->setDatasourceId($datasource_id); diff --git a/tests/src/Unit/Plugin/Processor/HtmlFilterTest.php b/tests/src/Unit/Plugin/Processor/HtmlFilterTest.php index d32a629..6dbdbf3 100644 --- a/tests/src/Unit/Plugin/Processor/HtmlFilterTest.php +++ b/tests/src/Unit/Plugin/Processor/HtmlFilterTest.php @@ -2,7 +2,6 @@ namespace Drupal\Tests\search_api\Unit\Plugin\Processor; -use Drupal\Core\DependencyInjection\ContainerBuilder; use Drupal\search_api\Plugin\search_api\processor\HtmlFilter; use Drupal\search_api\Utility; use Drupal\Tests\UnitTestCase; @@ -17,12 +16,16 @@ class HtmlFilterTest extends UnitTestCase { use ProcessorTestTrait; + use TestItemsTrait; /** * Creates a new processor object for use in the tests. */ public function setUp() { parent::setUp(); + + $this->setUpMockContainer(); + $this->processor = new HtmlFilter(array(), 'html_filter', array()); } @@ -229,13 +232,6 @@ public function tagConfigurationDataProvider() { * @dataProvider stringProcessingDataProvider */ public function testStringProcessing(array $config) { - $data_type_manager = $this->getMockBuilder('Drupal\search_api\DataType\DataTypePluginManager') - ->disableOriginalConstructor() - ->getMock(); - $container = new ContainerBuilder(); - $container->set('plugin.manager.search_api.data_type', $data_type_manager); - \Drupal::setContainer($container); - $this->processor->setConfiguration($config); $passed_value = '