You are here

public function LinkitAutocompleteTest::testAutocompletion in Linkit 8.5

Tests autocompletion in general.

File

tests/src/Kernel/LinkitAutocompleteTest.php, line 120

Class

LinkitAutocompleteTest
Tests the linkit autocomplete functionality.

Namespace

Drupal\Tests\linkit\Kernel

Code

public function testAutocompletion() {

  /** @var \Drupal\linkit\MatcherInterface $plugin */
  $plugin = $this->matcherManager
    ->createInstance('entity:entity_test');
  $this->linkitProfile
    ->addMatcher($plugin
    ->getConfiguration());
  $this->linkitProfile
    ->save();
  $entity_1 = EntityTest::create([
    'name' => 'Barbar',
  ]);
  $entity_1
    ->save();
  $entity_2 = EntityTest::create([
    'name' => 'Foobar',
  ]);
  $entity_2
    ->save();
  $entity_3 = EntityTest::create([
    'name' => 'Basbar',
  ]);
  $entity_3
    ->save();

  // Search for something that doesn't exists.
  $data = $this
    ->getAutocompleteResult('no_suggestions');
  $this
    ->assertTrue(count($data) == 1, 'Autocomplete returned the expected amount of suggestions.');
  $this
    ->assertSame(Html::escape('no_suggestions'), $data[0]['label'], 'Autocomplete returned the "no result" suggestion.');

  // Search for something that exists one time.
  $data = $this
    ->getAutocompleteResult('bas');
  $this
    ->assertTrue(count($data) == 1, 'Autocomplete returned the expected amount of suggestions.');
  $this
    ->assertSame(Html::escape($entity_3
    ->label()), $data[0]['label'], 'Autocomplete returned the matching entity');

  // Search for something that exists three times.
  $data = $this
    ->getAutocompleteResult('bar');
  $this
    ->assertTrue(count($data) == 3, 'Autocomplete returned the expected amount of suggestions.');
  $this
    ->assertSame(Html::escape($entity_1
    ->label()), $data[0]['label'], 'Autocomplete returned the first matching entity.');
  $this
    ->assertSame(Html::escape($entity_3
    ->label()), $data[1]['label'], 'Autocomplete returned the second matching entity.');
  $this
    ->assertSame(Html::escape($entity_2
    ->label()), $data[2]['label'], 'Autocomplete returned the third matching entity.');

  // Search for something with an empty string.
  $data = $this
    ->getAutocompleteResult('');
  $this
    ->assertEmpty(count($data), 'Autocomplete did not return any suggestions.');
}