public function AggregatedFieldsTest::testFieldExtraction in Search API 8
Tests that field extraction in the processor works correctly.
File
- tests/
src/ Unit/ Processor/ AggregatedFieldsTest.php, line 323
Class
- AggregatedFieldsTest
- Tests the "Aggregated fields" processor.
Namespace
Drupal\Tests\search_api\Unit\ProcessorCode
public function testFieldExtraction() {
/** @var \Drupal\Tests\search_api\Unit\TestComplexDataInterface|\PHPUnit\Framework\MockObject\MockObject $object */
$object = $this
->createMock(TestComplexDataInterface::class);
$bar_foo_property = $this
->createMock(TypedDataInterface::class);
$bar_foo_property
->method('getValue')
->willReturn('value3');
$bar_foo_property
->method('getDataDefinition')
->willReturn(new DataDefinition());
$bar_property = $this
->createMock(TestComplexDataInterface::class);
$bar_property
->method('get')
->willReturnMap([
[
'foo',
$bar_foo_property,
],
]);
$bar_property
->method('getProperties')
->willReturn([
'foo' => TRUE,
]);
$foobar_property = $this
->createMock(TypedDataInterface::class);
$foobar_property
->method('getValue')
->willReturn('wrong_value2');
$foobar_property
->method('getDataDefinition')
->willReturn(new DataDefinition());
$object
->method('get')
->willReturnMap([
[
'bar',
$bar_property,
],
[
'foobar',
$foobar_property,
],
]);
$object
->method('getProperties')
->willReturn([
'bar' => TRUE,
'foobar' => TRUE,
]);
/** @var \Drupal\search_api\IndexInterface|\PHPUnit\Framework\MockObject\MockObject $index */
$index = $this
->createMock(IndexInterface::class);
$fields_helper = \Drupal::getContainer()
->get('search_api.fields_helper');
$field = $fields_helper
->createField($index, 'aggregated_field', [
'property_path' => 'aggregated_field',
'configuration' => [
'type' => 'union',
'fields' => [
'aggregated_field',
'foo',
'entity:test1/bar:foo',
'entity:test1/baz',
'entity:test2/foobar',
],
],
]);
$index
->method('getFields')
->willReturn([
'aggregated_field' => $field,
]);
$index
->method('getPropertyDefinitions')
->willReturnMap([
[
NULL,
[
'foo' => new ProcessorProperty([
'processor_id' => 'processor1',
]),
],
],
[
'entity:test1',
[
'bar' => new DataDefinition(),
'foobar' => new DataDefinition(),
],
],
]);
$processor_mock = $this
->createMock(ProcessorInterface::class);
$processor_mock
->method('addFieldValues')
->willReturnCallback(function (ItemInterface $item) {
foreach ($item
->getFields(FALSE) as $field) {
if ($field
->getCombinedPropertyPath() == 'foo') {
$field
->setValues([
'value4',
'value5',
]);
}
}
});
$index
->method('getProcessorsByStage')
->willReturnMap([
[
ProcessorInterface::STAGE_ADD_PROPERTIES,
[],
[
'aggregated_field' => $this->processor,
'processor1' => $processor_mock,
],
],
]);
$this->processor
->setIndex($index);
/** @var \Drupal\search_api\Datasource\DatasourceInterface|\PHPUnit\Framework\MockObject\MockObject $datasource */
$datasource = $this
->createMock(DatasourceInterface::class);
$datasource
->method('getPluginId')
->willReturn('entity:test1');
$item = $fields_helper
->createItem($index, 'id', $datasource);
$item
->setOriginalObject($object);
$item
->setField('aggregated_field', clone $field);
$item
->setField('test1', $fields_helper
->createField($index, 'test1', [
'property_path' => 'baz',
'values' => [
'wrong_value1',
],
]));
$item
->setField('test2', $fields_helper
->createField($index, 'test2', [
'datasource_id' => 'entity:test1',
'property_path' => 'baz',
'values' => [
'value1',
'value2',
],
]));
$item
->setFieldsExtracted(TRUE);
$this->processor
->addFieldValues($item);
$expected = [
'value1',
'value2',
'value3',
'value4',
'value5',
];
$actual = $item
->getField('aggregated_field')
->getValues();
sort($actual);
$this
->assertEquals($expected, $actual);
}