You are here

public function HighlightTest::testPostprocessSearchResultsWithComplexKeys in Search API 8

Tests whether highlighting works on a longer text.

File

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

Class

HighlightTest
Tests the "Highlight" processor.

Namespace

Drupal\Tests\search_api\Unit\Processor

Code

public function testPostprocessSearchResultsWithComplexKeys() {
  $keys = [
    '#conjunction' => 'AND',
    [
      '#conjunction' => 'OR',
      'foo',
      'bar',
    ],
    'baz',
    [
      '#conjunction' => 'OR',
      '#negation' => TRUE,
      'text',
      'will',
    ],
  ];
  $query = $this
    ->createMock(QueryInterface::class);
  $query
    ->expects($this
    ->once())
    ->method('getProcessingLevel')
    ->willReturn(QueryInterface::PROCESSING_FULL);
  $query
    ->expects($this
    ->atLeastOnce())
    ->method('getOriginalKeys')
    ->will($this
    ->returnValue($keys));

  /** @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);
  $fields = [
    'entity:node/body' => [
      'type' => 'text',
      'values' => [
        'This foo text bar will get baz riddled with <strong> tags.',
      ],
    ],
  ];
  $items = $this
    ->createItems($this->index, 1, $fields);
  $results = new ResultSet($query);
  $results
    ->setResultItems($items);
  $results
    ->setResultCount(1);
  $this->processor
    ->postprocessSearchResults($results);
  $fields = $items[$this->itemIds[0]]
    ->getExtraData('highlighted_fields');
  $this
    ->assertEquals('This <strong>foo</strong> text <strong>bar</strong> will get <strong>baz</strong> riddled with &lt;strong&gt; tags.', $fields['body'][0], 'Highlighting is correctly applied when keys are complex.');
  $correct_output = '… This <strong>foo</strong> text <strong>bar</strong> will get <strong>baz</strong> riddled with &lt;strong&gt; tags. …';
  $excerpt = $items[$this->itemIds[0]]
    ->getExcerpt();
  $this
    ->assertEquals($correct_output, $excerpt, 'Excerpt was added.');
}