public function HighlightTest::testFieldExtraction in Search API 8
Tests that field extraction in the processor works correctly.
File
- tests/
src/ Unit/ Processor/ HighlightTest.php, line 934
Class
- HighlightTest
- Tests the "Highlight" 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 foo');
$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 foo');
$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,
]);
$this->index
->method('getFields')
->willReturn([
'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([
[
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 foo',
'value5 foo',
]);
}
}
});
$this->index
->method('getProcessorsByStage')
->willReturnMap([
[
ProcessorInterface::STAGE_ADD_PROPERTIES,
[],
[
'aggregated_field' => $this->processor,
'processor1' => $processor_mock,
],
],
]);
$this->processor
->setIndex($this->index);
/** @var \Drupal\search_api\Datasource\DatasourceInterface|\PHPUnit\Framework\MockObject\MockObject $datasource */
$datasource = $this
->createMock(DatasourceInterface::class);
$datasource
->method('getPluginId')
->willReturn('entity:test1');
$item = \Drupal::getContainer()
->get('search_api.fields_helper')
->createItem($this->index, 'id', $datasource);
$item
->setOriginalObject($object);
$field = $this
->createTestField('field4', 'baz')
->addValue('wrong_value1 foo');
$item
->setField('field4', $field);
$field = $this
->createTestField('field5', 'entity:test1/foobar')
->addValue('value1 foo')
->addValue('value2 foo');
$item
->setField('field5', $field);
$this->processor
->setConfiguration([
'excerpt' => FALSE,
]);
/** @var \Drupal\search_api\Query\QueryInterface|\PHPUnit\Framework\MockObject\MockObject $query */
$query = $this
->createMock(QueryInterface::class);
$query
->method('getOriginalKeys')
->willReturn('foo');
$query
->expects($this
->once())
->method('getProcessingLevel')
->willReturn(QueryInterface::PROCESSING_FULL);
$results = new ResultSet($query);
$results
->setResultCount(1)
->setResultItems([
$item,
]);
$this->processor
->postprocessSearchResults($results);
$expected = [
'field1' => [
'value3 <strong>foo</strong>',
],
'field3' => [
'value4 <strong>foo</strong>',
'value5 <strong>foo</strong>',
],
'field5' => [
'value1 <strong>foo</strong>',
'value2 <strong>foo</strong>',
],
];
$fields = $item
->getExtraData('highlighted_fields');
$this
->assertEquals($expected, $fields);
}