/** * Tests whether the properties are correctly altered. * * @see \Drupal\search_api\Plugin\search_api\processor\AggregatedField::alterPropertyDefinitions() */ public function testAlterPropertyDefinitions() { $fields = array('entity:test1/foo', 'entity:test1/foo:bar', 'entity:test2/foobaz:bla'); $index_fields = array(); foreach ($fields as $field_id) { $field_object = Utility::createField($this->index, $field_id); list($prefix, $label) = str_replace(':', ' » ', Utility::splitCombinedId($field_id)); $field_object->setLabelPrefix($prefix . ' » '); $field_object->setLabel($label); $index_fields[$field_id] = $field_object; } $this->index->expects($this->any())->method('getFields')->willReturn($index_fields); $this->index->method('getPropertyDefinitions')->willReturn(array()); $dummy_datasource = $this->getMock('Drupal\\search_api\\Datasource\\DatasourceInterface'); $dummy_datasource->method('label')->willReturn('datasource'); $this->index->method('getDatasources')->willReturn(array('entity:test1' => $dummy_datasource, 'entity:test2' => $dummy_datasource)); $configuration['fields'] = array('search_api_aggregation_1' => array('label' => 'Field 1', 'type' => 'union', 'fields' => array('entity:test1/foo', 'entity:test1/foo:bar', 'entity:test2/foobaz:bla')), 'search_api_aggregation_2' => array('label' => 'Field 2', 'type' => 'max', 'fields' => array('entity:test1/foo:bar'))); $this->processor->setConfiguration($configuration); /** @var \Drupal\Core\StringTranslation\TranslationInterface $translation */ $translation = $this->getStringTranslationStub(); $this->processor->setStringTranslation($translation); // Check for modified properties when no datasource is given. $properties = array(); $this->processor->alterPropertyDefinitions($properties, NULL); $property_added = array_key_exists('search_api_aggregation_1', $properties); $this->assertTrue($property_added, 'The "search_api_aggregation_1" property was added to the properties.'); if ($property_added) { $this->assertInstanceOf('Drupal\\Core\\TypedData\\DataDefinitionInterface', $properties['search_api_aggregation_1'], 'The "search_api_aggregation_1" property contains a valid data definition.'); if ($properties['search_api_aggregation_1'] instanceof DataDefinitionInterface) { $this->assertEquals('string', $properties['search_api_aggregation_1']->getDataType(), 'Correct data type set in the data definition.'); $this->assertEquals('Field 1', $properties['search_api_aggregation_1']->getLabel(), 'Correct label set in the data definition.'); $fields_string = 'datasource » foo, datasource » foo:bar, datasource » foobaz:bla'; $description = $translation->translate('A @type aggregation of the following fields: @fields.', array('@type' => $translation->translate('Union'), '@fields' => $fields_string)); $this->assertEquals($description, $properties['search_api_aggregation_1']->getDescription(), 'Correct description set in the data definition.'); } } $property_added = array_key_exists('search_api_aggregation_2', $properties); $this->assertTrue($property_added, 'The "search_api_aggregation_2" property was added to the properties.'); if ($property_added) { $this->assertInstanceOf('Drupal\\Core\\TypedData\\DataDefinitionInterface', $properties['search_api_aggregation_2'], 'The "search_api_aggregation_2" property contains a valid data definition.'); if ($properties['search_api_aggregation_2'] instanceof DataDefinitionInterface) { $this->assertEquals('integer', $properties['search_api_aggregation_2']->getDataType(), 'Correct data type set in the data definition.'); $this->assertEquals('Field 2', $properties['search_api_aggregation_2']->getLabel(), 'Correct label set in the data definition.'); $description = $translation->translate('A @type aggregation of the following fields: @fields.', array('@type' => $translation->translate('Maximum'), '@fields' => 'datasource » foo:bar')); $this->assertEquals($description, $properties['search_api_aggregation_2']->getDescription(), 'Correct description set in the data definition.'); } } // Test whether the properties of specific datasources stay untouched. $properties = array(); /** @var \Drupal\search_api\Datasource\DatasourceInterface $datasource */ $datasource = $this->getMock('Drupal\\search_api\\Datasource\\DatasourceInterface'); $this->processor->alterPropertyDefinitions($properties, $datasource); $this->assertEmpty($properties, 'Datasource-specific properties did not get changed.'); }