public function HighlightTest::testPostprocessSearchResultsWithTwoItems in Search API 8
Tests field highlighting and excerpts with two items.
File
- tests/
src/ Unit/ Processor/ HighlightTest.php, line 822
Class
- HighlightTest
- Tests the "Highlight" processor.
Namespace
Drupal\Tests\search_api\Unit\ProcessorCode
public function testPostprocessSearchResultsWithTwoItems() {
$query = $this
->createMock(QueryInterface::class);
$query
->expects($this
->once())
->method('getProcessingLevel')
->willReturn(QueryInterface::PROCESSING_FULL);
$query
->expects($this
->atLeastOnce())
->method('getOriginalKeys')
->will($this
->returnValue([
'#conjunction' => 'OR',
'foo',
]));
/** @var \Drupal\search_api\Query\QueryInterface $query */
$body_field = $this
->createTestField('body', 'entity:node/body');
$this->index
->expects($this
->atLeastOnce())
->method('getFields')
->will($this
->returnValue([
'body' => $body_field,
]));
$this->processor
->setIndex($this->index);
$body_values = [
'Some foo value',
'foo bar',
];
$fields = [
'entity:node/body' => [
'type' => 'text',
'values' => $body_values,
],
];
$items = $this
->createItems($this->index, 2, $fields);
$items[$this->itemIds[1]]
->getField('body')
->setValues([
'The second item also contains foo in its body.',
]);
$results = new ResultSet($query);
$results
->setResultItems($items);
$results
->setResultCount(1);
$this->processor
->postprocessSearchResults($results);
$fields = $items[$this->itemIds[0]]
->getExtraData('highlighted_fields');
$this
->assertEquals('Some <strong>foo</strong> value', $fields['body'][0], 'Highlighting is correctly applied to first body field value.');
$this
->assertEquals('<strong>foo</strong> bar', $fields['body'][1], 'Highlighting is correctly applied to second body field value.');
$fields = $items[$this->itemIds[1]]
->getExtraData('highlighted_fields');
$this
->assertEquals('The second item also contains <strong>foo</strong> in its body.', $fields['body'][0], 'Highlighting is correctly applied to second item.');
$excerpt1 = '… Some <strong>foo</strong> value … <strong>foo</strong> bar …';
$excerpt2 = '… The second item also contains <strong>foo</strong> in its body. …';
$this
->assertEquals($excerpt1, $items[$this->itemIds[0]]
->getExcerpt(), 'Correct excerpt created from two text fields.');
$this
->assertEquals($excerpt2, $items[$this->itemIds[1]]
->getExcerpt(), 'Correct excerpt created for second item.');
}