You are here

public function HighlightTest::testPostprocessSearchResultsWithPreviousHighlighting in Search API 8

Tests field highlighting when previous highlighting is present.

File

tests/src/Unit/Processor/HighlightTest.php, line 385

Class

HighlightTest
Tests the "Highlight" processor.

Namespace

Drupal\Tests\search_api\Unit\Processor

Code

public function testPostprocessSearchResultsWithPreviousHighlighting() {
  $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' => 'AND',
    'foo',
  ]));

  /** @var \Drupal\search_api\Query\QueryInterface $query */
  $field = $this
    ->createTestField('body', 'entity:node/body');
  $this->index
    ->expects($this
    ->atLeastOnce())
    ->method('getFields')
    ->will($this
    ->returnValue([
    'body' => $field,
  ]));
  $this->processor
    ->setIndex($this->index);
  $body_values = [
    'Some foo value',
  ];
  $fields = [
    'entity:node/body' => [
      'type' => 'text',
      'values' => $body_values,
    ],
  ];
  $items = $this
    ->createItems($this->index, 1, $fields);
  $results = new ResultSet($query);
  $results
    ->setResultItems($items);
  $results
    ->setResultCount(1);
  $highlighted_fields["body_2"][0] = 'Old highlighting text';
  $highlighted_fields["body_2"][1] = 'More highlighting text';
  $items[$this->itemIds[0]]
    ->setExtraData('highlighted_fields', $highlighted_fields);
  $this->processor
    ->postprocessSearchResults($results);
  $fields = $items[$this->itemIds[0]]
    ->getExtraData('highlighted_fields');
  $this
    ->assertEquals('Some <strong>foo</strong> value', $fields['body'][0], 'Highlighting correctly applied to body field.');
  $this
    ->assertEquals('Old highlighting text', $fields["body_2"][0], 'Old highlighting data is preserved.');
  $this
    ->assertEquals('More highlighting text', $fields["body_2"][1], 'Old highlighting data is preserved.');
}