You are here

public function SearchApiSolrTest::testAutocomplete in Search API Solr 8.2

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

Tests the autocomplete support.

1 method overrides SearchApiSolrTest::testAutocomplete()
SearchApiSolrMultilingualTest::testAutocomplete in tests/src/Kernel/SearchApiSolrMultilingualTest.php
Tests the autocomplete support.

File

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

Class

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

Namespace

Drupal\Tests\search_api_solr\Kernel

Code

public function testAutocomplete() {
  $this
    ->addTestEntity(1, [
    'name' => 'Test Article 1',
    'body' => 'The test article number 1 about cats, dogs and trees.',
    'type' => 'article',
  ]);

  // Add another node with body length equal to the limit.
  $this
    ->addTestEntity(2, [
    'name' => 'Test Article 1',
    'body' => 'The test article number 2 about a tree.',
    'type' => 'article',
  ]);
  $this
    ->indexItems($this->indexId);

  /** @var \Drupal\search_api_solr\Plugin\search_api\backend\SearchApiSolrBackend $backend */
  $backend = Server::load($this->serverId)
    ->getBackend();
  $autocompleteSearch = new Search([], 'search_api_autocomplete_search');
  $query = $this
    ->buildSearch([
    'artic',
  ], [], [
    'body_unstemmed',
  ], FALSE);
  $suggestions = $backend
    ->getAutocompleteSuggestions($query, $autocompleteSearch, 'artic', 'artic');
  $this
    ->assertEquals(1, count($suggestions));
  $this
    ->assertEquals('le', $suggestions[0]
    ->getSuggestionSuffix());
  $this
    ->assertEquals(2, $suggestions[0]
    ->getResultsCount());
  $query = $this
    ->buildSearch([
    'artic',
  ], [], [
    'body',
  ], FALSE);
  $suggestions = $backend
    ->getTermsSuggestions($query, $autocompleteSearch, 'artic', 'artic');
  $this
    ->assertEquals(1, count($suggestions));

  // This time we test the stemmed token.
  $this
    ->assertEquals('l', $suggestions[0]
    ->getSuggestionSuffix());
  $this
    ->assertEquals(2, $suggestions[0]
    ->getResultsCount());
  $query = $this
    ->buildSearch([
    'articel',
  ], [], [
    'body',
  ], FALSE);
  $suggestions = $backend
    ->getSpellcheckSuggestions($query, $autocompleteSearch, 'articel', 'articel');
  $this
    ->assertEquals(1, count($suggestions));
  $this
    ->assertEquals('article', $suggestions[0]
    ->getSuggestedKeys());
  $this
    ->assertEquals(0, $suggestions[0]
    ->getResultsCount());
  $query = $this
    ->buildSearch([
    'article tre',
  ], [], [
    'body_unstemmed',
  ], FALSE);
  $suggestions = $backend
    ->getAutocompleteSuggestions($query, $autocompleteSearch, 'tre', 'article tre');
  $this
    ->assertEquals('article tree', $suggestions[0]
    ->getSuggestedKeys());
  $this
    ->assertEquals(1, $suggestions[0]
    ->getResultsCount());

  // Having set preserveOriginal in WordDelimiter let punction remain.
  $this
    ->assertEquals('article tree.', $suggestions[1]
    ->getSuggestedKeys());
  $this
    ->assertEquals(1, $suggestions[1]
    ->getResultsCount());
  $this
    ->assertEquals('article trees', $suggestions[2]
    ->getSuggestedKeys());
  $this
    ->assertEquals(1, $suggestions[2]
    ->getResultsCount());
  $this
    ->assertEquals('article trees.', $suggestions[3]
    ->getSuggestedKeys());
  $this
    ->assertEquals(1, $suggestions[3]
    ->getResultsCount());

  // @todo spellcheck tests

  #$query = $this->buildSearch(['articel doks'], [], ['body'], FALSE);

  #$suggestions = $backend->getSpellcheckSuggestions($query, $autocompleteSearch, 'doks', 'articel doks');

  #$this->assertEquals(1, count($suggestions));

  #$this->assertEquals('article dogs', $suggestions[0]->getSuggestedKeys());

  #$query = $this->buildSearch(['articel tre'], [], ['body'], FALSE);

  #$suggestions = $backend->getAutocompleteSuggestions($query, $autocompleteSearch, 'tre', 'articel tre');

  #$this->assertEquals(5, count($suggestions));

  #$this->assertEquals('e', $suggestions[0]->getSuggestionSuffix());

  #$this->assertEquals(1, $suggestions[0]->getResultsCount());

  #$this->assertEquals('es', $suggestions[1]->getSuggestionSuffix());
  $query = $this
    ->buildSearch([
    'artic',
  ], [], [
    'body',
  ], FALSE);
  $suggestions = $backend
    ->getSuggesterSuggestions($query, $autocompleteSearch, 'artic', 'artic');
  $this
    ->assertEquals(2, count($suggestions));
  $this
    ->assertEquals('artic', $suggestions[0]
    ->getUserInput());
  $this
    ->assertEquals('The test <b>', $suggestions[0]
    ->getSuggestionPrefix());
  $this
    ->assertEquals('</b>le number 1 about cats, dogs and trees.', $suggestions[0]
    ->getSuggestionSuffix());
  $this
    ->assertEquals('The test <b>artic</b>le number 1 about cats, dogs and trees.', $suggestions[0]
    ->getSuggestedKeys());
  $this
    ->assertEquals('artic', $suggestions[1]
    ->getUserInput());
  $this
    ->assertEquals('The test <b>', $suggestions[1]
    ->getSuggestionPrefix());
  $this
    ->assertEquals('</b>le number 2 about a tree.', $suggestions[1]
    ->getSuggestionSuffix());
  $this
    ->assertEquals('The test <b>artic</b>le number 2 about a tree.', $suggestions[1]
    ->getSuggestedKeys());

  // @todo more suggester tests
}