You are here

public function HighlightTest::testPostprocessSearchResultsExcerptStripTags in Search API 8

Tests whether excerpt creation correctly handles HTML tags.

File

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

Class

HighlightTest
Tests the "Highlight" processor.

Namespace

Drupal\Tests\search_api\Unit\Processor

Code

public function testPostprocessSearchResultsExcerptStripTags() {
  $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_value = <<<'END'
Sentence with foo keyword.
<script>
var foo = 1;
</script>
<style>
a.foo {
  font-weight: bold;
}
</style>
And another foo!
END;
  $fields = [
    'entity:node/body' => [
      'type' => 'text',
      'values' => [
        $body_value,
      ],
    ],
  ];
  $items = $this
    ->createItems($this->index, 1, $fields);
  $results = new ResultSet($query);
  $results
    ->setResultItems($items);
  $results
    ->setResultCount(1);
  $this->processor
    ->postprocessSearchResults($results);
  $output = $results
    ->getResultItems();
  $excerpt = $output[$this->itemIds[0]]
    ->getExcerpt();
  $correct_output = '… Sentence with <strong>foo</strong> keyword. And another <strong>foo</strong>! …';
  $this
    ->assertEquals($correct_output, $excerpt, 'Excerpt was added.');
}