You are here

protected function SearchApiSolrTest::checkHighlight in Search API Solr 8.3

Same name and namespace in other branches
  1. 4.x tests/src/Kernel/SearchApiSolrTest.php \Drupal\Tests\search_api_solr\Kernel\SearchApiSolrTest::checkHighlight()

Tests highlight options.

1 call to SearchApiSolrTest::checkHighlight()
SearchApiSolrTest::checkBackendSpecificFeatures in tests/src/Kernel/SearchApiSolrTest.php
Checks backend specific features.

File

tests/src/Kernel/SearchApiSolrTest.php, line 684

Class

SearchApiSolrTest
Tests index and search capabilities using the Solr search backend.

Namespace

Drupal\Tests\search_api_solr\Kernel

Code

protected function checkHighlight() {
  $this->travisLogger
    ->debug('SearchApiSolrTest::checkHighlight()');
  $server = $this
    ->getIndex()
    ->getServerInstance();
  $config = $server
    ->getBackendConfig();
  $this
    ->indexItems($this->indexId);
  $query = $this
    ->buildSearch('foobar');
  $results = $query
    ->execute();
  $this
    ->assertEquals(1, $results
    ->getResultCount(), 'Search for »foobar« returned correct number of results.');

  /** @var \Drupal\search_api\Item\ItemInterface $result */
  foreach ($results as $result) {
    $this
      ->assertEmpty($result
      ->getExtraData('highlighted_fields', []));
    $this
      ->assertEmpty($result
      ->getExtraData('highlighted_keys', []));
  }
  $config['highlight_data'] = TRUE;
  $server
    ->setBackendConfig($config);
  $server
    ->save();
  $query = $this
    ->buildSearch('foobar');
  $results = $query
    ->execute();
  $this
    ->assertEquals(1, $results
    ->getResultCount(), 'Search for »foobar« returned correct number of results.');

  /** @var \Drupal\search_api\Item\ItemInterface $result */
  foreach ($results as $result) {
    $this
      ->assertStringContainsString('<strong>foobar</strong>', (string) $result
      ->getExtraData('highlighted_fields', [
      'body' => [
        '',
      ],
    ])['body'][0]);
    $this
      ->assertEquals([
      'foobar',
    ], $result
      ->getExtraData('highlighted_keys', []));
    $this
      ->assertEquals('… bar … test <strong>foobar</strong> Case …', $result
      ->getExcerpt());
  }

  // Test highlghting with stemming.
  $query = $this
    ->buildSearch('foobars');
  $results = $query
    ->execute();
  $this
    ->assertEquals(1, $results
    ->getResultCount(), 'Search for »foobar« returned correct number of results.');

  /** @var \Drupal\search_api\Item\ItemInterface $result */
  foreach ($results as $result) {
    $this
      ->assertStringContainsString('<strong>foobar</strong>', (string) $result
      ->getExtraData('highlighted_fields', [
      'body' => [
        '',
      ],
    ])['body'][0]);
    $this
      ->assertEquals([
      'foobar',
    ], $result
      ->getExtraData('highlighted_keys', []));
    $this
      ->assertEquals('… bar … test <strong>foobar</strong> Case …', $result
      ->getExcerpt());
  }
}