You are here

public function HighlightTest::testExcerptExcludeFields in Search API 8

Tests excerpts with some fields excluded.

File

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

Class

HighlightTest
Tests the "Highlight" processor.

Namespace

Drupal\Tests\search_api\Unit\Processor

Code

public function testExcerptExcludeFields() {
  $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);
  $this->processor
    ->setConfiguration([
    'exclude_fields' => [
      'title',
    ],
  ]);
  $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 = '… Some <strong>foo</strong> value … <strong>foo</strong> bar …';
  $this
    ->assertEquals($excerpt, $items[$this->itemIds[0]]
    ->getExcerpt(), 'Correct excerpt created ignoring title field.');
}