You are here

public function SearchApiSolrTest::testHighlightAndExcerpt in Search API Solr 8

Tests highlight and excerpt options.

File

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

Class

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

Namespace

Drupal\Tests\search_api_solr\Kernel

Code

public function testHighlightAndExcerpt() {

  // Only run the tests if we have a Solr core available.
  if ($this->solrAvailable) {
    $config = $this
      ->getIndex()
      ->getServerInstance()
      ->getBackendConfig();
    $this
      ->insertExampleContent();
    $this
      ->indexItems($this->indexId);
    $config['retrieve_data'] = TRUE;
    $config['highlight_data'] = TRUE;
    $config['excerpt'] = FALSE;
    $query = $this
      ->buildSearch('foobar');
    $query
      ->getIndex()
      ->getServerInstance()
      ->setBackendConfig($config);
    $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
        ->getField('body')
        ->getValues()[0]);
      $this
        ->assertNull($result
        ->getExcerpt());
    }
    $config['highlight_data'] = FALSE;
    $config['excerpt'] = TRUE;
    $query = $this
      ->buildSearch('foobar');
    $query
      ->getIndex()
      ->getServerInstance()
      ->setBackendConfig($config);
    $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
        ->assertStringNotContainsString('<strong>foobar</strong>', (string) $result
        ->getField('body')
        ->getValues()[0]);
      $this
        ->assertStringContainsString('<strong>foobar</strong>', $result
        ->getExcerpt());
    }
    $config['highlight_data'] = TRUE;
    $config['excerpt'] = TRUE;
    $query = $this
      ->buildSearch('foobar');
    $query
      ->getIndex()
      ->getServerInstance()
      ->setBackendConfig($config);
    $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
        ->getField('body')
        ->getValues()[0]);
      $this
        ->assertStringContainsString('<strong>foobar</strong>', $result
        ->getExcerpt());
    }
  }
  else {
    $this
      ->assertTrue(TRUE, 'Error: The Solr instance could not be found. Please enable a multi-core one on http://localhost:8983/solr/drupal');
  }
}