You are here

public function HighlightTest::testPostprocessSearchResultsHighlightPartial in Search API 8

Tests highlighting of partial matches.

@dataProvider postprocessSearchResultsHighlightPartialDataProvider

Parameters

string $text: The text that should be highlighted.

string $keywords: The search keywords.

string $highlighted: The expected highlighted text.

File

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

Class

HighlightTest
Tests the "Highlight" processor.

Namespace

Drupal\Tests\search_api\Unit\Processor

Code

public function testPostprocessSearchResultsHighlightPartial($text, $keywords, $highlighted) {
  $this->processor
    ->setConfiguration([
    'highlight_partial' => TRUE,
  ]);
  $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',
    $keywords,
  ]));

  /** @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);
  $fields = [
    'entity:node/body' => [
      'type' => 'text',
      'values' => [
        $text,
      ],
    ],
  ];
  $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($highlighted, $fields['body'][0], 'Highlighting is correctly applied to a partial match.');
  $excerpt = $items[$this->itemIds[0]]
    ->getExcerpt();
  $this
    ->assertEquals("… {$highlighted} …", $excerpt, 'Highlighting is correctly applied to a partial match.');
}