You are here

public function SuggestionFactoryTest::testCreateFromSuggestedKeys in Search API Autocomplete 8

Tests creating a suggestion from the suggested keys.

@covers ::createFromSuggestedKeys

File

tests/src/Unit/SuggestionFactoryTest.php, line 23

Class

SuggestionFactoryTest
Tests the functionality of the suggestion factory class.

Namespace

Drupal\Tests\search_api_autocomplete\Unit

Code

public function testCreateFromSuggestedKeys() {
  $factory = new SuggestionFactory('foo');
  $suggestion = $factory
    ->createFromSuggestedKeys('bar');
  $this
    ->assertEquals('bar', $suggestion
    ->getSuggestedKeys());
  $this
    ->assertNull($suggestion
    ->getUserInput());
  $this
    ->assertEquals('bar', $suggestion
    ->getLabel());
  $this
    ->assertNull($suggestion
    ->getResultsCount());
  $suggestion = $factory
    ->createFromSuggestedKeys('fooo');
  $this
    ->assertEquals('fooo', $suggestion
    ->getSuggestedKeys());
  $this
    ->assertEquals('foo', $suggestion
    ->getUserInput());
  $this
    ->assertNull($suggestion
    ->getLabel());
  $this
    ->assertEquals('o', $suggestion
    ->getSuggestionSuffix());
  $this
    ->assertNull($suggestion
    ->getResultsCount());
  $suggestion = $factory
    ->createFromSuggestedKeys('foooo', 5);
  $this
    ->assertEquals('foooo', $suggestion
    ->getSuggestedKeys());
  $this
    ->assertEquals('foo', $suggestion
    ->getUserInput());
  $this
    ->assertNull($suggestion
    ->getLabel());
  $this
    ->assertEquals('oo', $suggestion
    ->getSuggestionSuffix());
  $this
    ->assertEquals(5, $suggestion
    ->getResultsCount());

  // Test case-insensitivity.
  $suggestion = $factory
    ->createFromSuggestedKeys('Foooo', 5);
  $this
    ->assertEquals('Foooo', $suggestion
    ->getSuggestedKeys());
  $this
    ->assertEquals('Foo', $suggestion
    ->getUserInput());
  $this
    ->assertNull($suggestion
    ->getLabel());
  $this
    ->assertEquals('oo', $suggestion
    ->getSuggestionSuffix());
  $this
    ->assertEquals(5, $suggestion
    ->getResultsCount());

  // Test case-insensitivity with non-ASCII characters.
  Unicode::check();
  $factory = new SuggestionFactory('öd');
  $suggestion = $factory
    ->createFromSuggestedKeys('Ödön', 5);
  $this
    ->assertEquals('Ödön', $suggestion
    ->getSuggestedKeys());
  $this
    ->assertEquals('Öd', $suggestion
    ->getUserInput());
  $this
    ->assertNull($suggestion
    ->getLabel());
  $this
    ->assertEquals('ön', $suggestion
    ->getSuggestionSuffix());
  $this
    ->assertEquals(5, $suggestion
    ->getResultsCount());
}