You are here

public function LinkitFilterEntityTest::testFilterEntityTranslations in Linkit 8.5

Tests the linkit filter for entities with translations.

File

tests/src/Kernel/LinkitFilterEntityTest.php, line 84

Class

LinkitFilterEntityTest
Tests the Linkit filter.

Namespace

Drupal\Tests\linkit\Kernel

Code

public function testFilterEntityTranslations() {

  // Create an entity and add translations to that.

  /** @var \Drupal\entity_test\Entity\EntityTestMul $entity */
  $entity = EntityTestMul::create([
    'name' => $this
      ->randomMachineName(),
  ]);
  $entity
    ->addTranslation('sv', [
    'name' => $this
      ->randomMachineName(),
    'langcode' => 'sv',
  ]);
  $entity
    ->addTranslation('da', [
    'name' => $this
      ->randomMachineName(),
    'langcode' => 'da',
  ]);
  $entity
    ->addTranslation('fi', [
    'name' => $this
      ->randomMachineName(),
    'langcode' => 'fi',
  ]);
  $entity
    ->save();
  $url = $entity
    ->toUrl()
    ->toString();

  // Add url aliases.
  $this
    ->createPathAlias($url, '/' . $this
    ->randomMachineName(), 'en');
  $this
    ->createPathAlias($url, '/' . $this
    ->randomMachineName(), 'sv');
  $this
    ->createPathAlias($url, '/' . $this
    ->randomMachineName(), 'da');
  $this
    ->createPathAlias($url, '/' . $this
    ->randomMachineName(), 'fi');

  // Disable the automatic title attribute.
  $this->filter
    ->setConfiguration([
    'settings' => [
      'title' => 0,
    ],
  ]);

  /** @var \Drupal\Core\Language\Language $language */
  foreach ($entity
    ->getTranslationLanguages() as $language) {
    $this
      ->assertLinkitFilter($entity
      ->getTranslation($language
      ->getId()), $language
      ->getId());
  }

  // Enable the automatic title attribute.
  $this->filter
    ->setConfiguration([
    'settings' => [
      'title' => 1,
    ],
  ]);

  /** @var \Drupal\Core\Language\Language $language */
  foreach ($entity
    ->getTranslationLanguages() as $language) {
    $this
      ->assertLinkitFilterWithTitle($entity
      ->getTranslation($language
      ->getId()), $language
      ->getId());
  }
}