You are here

public function HighlightTest::testPostprocessSearchResultsWithChangedExcerptLength in Search API 8

Tests whether highlighting works with a changed excerpt length.

File

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

Class

HighlightTest
Tests the "Highlight" processor.

Namespace

Drupal\Tests\search_api\Unit\Processor

Code

public function testPostprocessSearchResultsWithChangedExcerptLength() {
  $this->processor
    ->setConfiguration([
    'excerpt_length' => 64,
  ]);
  $query = $this
    ->createMock(QueryInterface::class);
  $query
    ->expects($this
    ->once())
    ->method('getProcessingLevel')
    ->willReturn(QueryInterface::PROCESSING_FULL);
  $query
    ->expects($this
    ->atLeastOnce())
    ->method('getOriginalKeys')
    ->will($this
    ->returnValue('congue'));

  /** @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 = [
    $this
      ->getFieldBody(),
  ];
  $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);
  $this->processor
    ->postprocessSearchResults($results);
  $output = $results
    ->getResultItems();
  $excerpt = $output[$this->itemIds[0]]
    ->getExcerpt();
  $correct_output = '… dapibus, lorem nunc <strong>congue</strong> velit, et dictum augue …';
  $this
    ->assertEquals($correct_output, $excerpt, 'Excerpt has correct reduced length.');
}