You are here

public function LinkitAutocompleteTest::testAutocompletionTranslations in Linkit 8.5

Tests autocompletion with translated entities.

File

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

Class

LinkitAutocompleteTest
Tests the linkit autocomplete functionality.

Namespace

Drupal\Tests\linkit\Kernel

Code

public function testAutocompletionTranslations() {

  /** @var \Drupal\linkit\MatcherInterface $plugin */
  $plugin = $this->matcherManager
    ->createInstance('entity:entity_test_mul');
  $this->linkitProfile
    ->addMatcher($plugin
    ->getConfiguration());
  $this->linkitProfile
    ->save();
  $this
    ->setupLanguages();
  $entity = EntityTestMul::create([
    'name' => 'Barbar',
  ]);

  // Copy the array and shift the default language.
  $translations = $this->langcodes;
  array_shift($translations);
  foreach ($translations as $langcode) {
    $entity
      ->addTranslation($langcode, [
      'name' => 'Barbar ' . $langcode,
    ]);
  }
  $entity
    ->save();
  foreach ($this->langcodes as $langcode) {
    $this
      ->config('system.site')
      ->set('default_langcode', $langcode)
      ->save();
    $data = $this
      ->getAutocompleteResult('bar');
    $this
      ->assertTrue(count($data) == 1, 'Autocomplete returned the expected amount of suggestions.');
    $this
      ->assertSame($entity
      ->getTranslation($langcode)
      ->label(), $data[0]['label'], 'Autocomplete returned the "no results."');
  }
}