You are here

public function HighlightTest::testPostprocessSearchResultsWithTwoFields in Search API 8

Tests field highlighting and excerpts for two fields.

File

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

Class

HighlightTest
Tests the "Highlight" processor.

Namespace

Drupal\Tests\search_api\Unit\Processor

Code

public function testPostprocessSearchResultsWithTwoFields() {
  $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 */
  $body_field = $this
    ->createTestField('body', 'entity:node/body');
  $title_field = $this
    ->createTestField('title', 'title');
  $this->index
    ->expects($this
    ->atLeastOnce())
    ->method('getFields')
    ->will($this
    ->returnValue([
    'body' => $body_field,
    'title' => $title_field,
  ]));
  $this->processor
    ->setIndex($this->index);
  $body_values = [
    'Some foo value',
    'foo bar',
  ];
  $title_values = [
    'Title foo',
  ];
  $fields = [
    'entity:node/body' => [
      'type' => 'text',
      'values' => $body_values,
    ],
    'title' => [
      'type' => 'text',
      'values' => $title_values,
    ],
  ];
  $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('Some <strong>foo</strong> value', $fields['body'][0], 'Highlighting is correctly applied to first body field value.');
  $this
    ->assertEquals('<strong>foo</strong> bar', $fields['body'][1], 'Highlighting is correctly applied to second body field value.');
  $this
    ->assertEquals('Title <strong>foo</strong>', $fields['title'][0], 'Highlighting is correctly applied to title field.');
  $excerpt = $items[$this->itemIds[0]]
    ->getExcerpt();
  $this
    ->assertStringContainsString('Some <strong>foo</strong> value', $excerpt);
  $this
    ->assertStringContainsString('<strong>foo</strong> bar', $excerpt);
  $this
    ->assertStringContainsString('Title <strong>foo</strong>', $excerpt);
  $this
    ->assertEquals(4, substr_count($excerpt, '…'));
}