You are here

public function AutocompleteTest::testAutocompletion in Search API 8

Tests whether autocomplete suggestions are correctly created.

@covers ::getAutocompleteSuggestions

File

modules/search_api_db/tests/src/Kernel/AutocompleteTest.php, line 91

Class

AutocompleteTest
Tests autocomplete functionality of the Database backend.

Namespace

Drupal\Tests\search_api_db\Kernel

Code

public function testAutocompletion() {

  /** @var \Drupal\search_api_autocomplete\SearchInterface $autocomplete */
  $autocomplete = Search::load('search_api_db_test_autocomplete');
  $index = $autocomplete
    ->getIndex();

  /** @var \Drupal\search_api_db\Plugin\search_api\backend\Database $backend */
  $backend = $index
    ->getServerInstance()
    ->getBackend();
  $this
    ->assertInstanceOf(Database::class, $backend);
  $query = $index
    ->query()
    ->range(0, 10);
  $suggestions = $backend
    ->getAutocompleteSuggestions($query, $autocomplete, 'fo', 'fo');
  $expected = [
    'foo' => 4,
    'foobar' => 1,
    'foobaz' => 1,
    'foobuz' => 1,
  ];
  $this
    ->assertSuggestionsEqual($expected, $suggestions);
  $query = $index
    ->query()
    ->keys('foo')
    ->range(0, 10);
  $suggestions = $backend
    ->getAutocompleteSuggestions($query, $autocomplete, 'fo', 'foo fo');
  $expected = [
    'foo foobaz' => 1,
    'foo foobuz' => 1,
  ];
  $this
    ->assertSuggestionsEqual($expected, $suggestions);
}