You are here

public function MenuLinkFieldKernelTest::testTranslationWithMultipleMenuItems in Menu Link (Field) 8

Tests the capability to provide multiple menu links and translate them.

File

tests/src/Kernel/MenuLinkFieldKernelTest.php, line 218

Class

MenuLinkFieldKernelTest
Tests the menu link field.

Namespace

Drupal\Tests\menu_link\Kernel

Code

public function testTranslationWithMultipleMenuItems() : void {
  $this
    ->enableMultilingual();
  $entity_test_mul = EntityTestMul::create([
    'type' => 'entity_test_mul',
    'name' => 'test',
    'field_menu_link2' => [
      'menu_name' => 'test_menu',
      'title' => 'test title EN',
      'description' => 'test description EN',
    ],
  ]);
  $entity_test_mul
    ->save();
  $entity_test_mul_es = $entity_test_mul
    ->addTranslation('es');
  $entity_test_mul_es
    ->set('field_menu_link2', [
    'menu_name' => 'test_menu_es',
    'title' => 'test title ES',
    'description' => 'test description ES',
  ]);
  $entity_test_mul_es
    ->save();

  /** @var \Drupal\Core\Menu\MenuLinkTree $menu_tree */
  $menu_tree = \Drupal::service('menu.link_tree');
  $parameters = new MenuTreeParameters();
  $result = $menu_tree
    ->load('test_menu', $parameters);
  self::assertCount(1, $result);
  $menu_link = reset($result);
  self::assertEquals(1, $menu_link->depth);
  self::assertFalse($menu_link->hasChildren);
  self::assertEquals('test title EN', $menu_link->link
    ->getTitle());
  self::assertEquals('', $menu_link->link
    ->getParent());
  self::assertEquals('test description EN', $menu_link->link
    ->getDescription());
  self::assertEquals('test_menu', $menu_link->link
    ->getMenuName());
  self::assertTrue($menu_link->link
    ->getUrlObject()
    ->isRouted());
  self::assertEquals('entity.entity_test_mul.canonical', $menu_link->link
    ->getUrlObject()
    ->getRouteName());
  self::assertEquals([
    'entity_test_mul' => $entity_test_mul
      ->id(),
  ], $menu_link->link
    ->getUrlObject()
    ->getRouteParameters());
  self::assertEquals('test description EN', $menu_link->link
    ->getUrlObject()
    ->getOptions()['attributes']['title']);
  $parameters = new MenuTreeParameters();
  $result = $menu_tree
    ->load('test_menu_es', $parameters);
  self::assertCount(1, $result);
  $menu_link_es = reset($result);
  self::assertEquals('test title ES', $menu_link_es->link
    ->getTitle());
  self::assertEquals('', $menu_link_es->link
    ->getParent());
  self::assertEquals('test description ES', $menu_link_es->link
    ->getDescription());
  self::assertEquals('test_menu_es', $menu_link_es->link
    ->getMenuName());
  self::assertTrue($menu_link_es->link
    ->getUrlObject()
    ->isRouted());
  self::assertEquals('entity.entity_test_mul.canonical', $menu_link_es->link
    ->getUrlObject()
    ->getRouteName());
  self::assertEquals([
    'entity_test_mul' => $entity_test_mul
      ->id(),
  ], $menu_link_es->link
    ->getUrlObject()
    ->getRouteParameters());
  self::assertEquals('test description ES', $menu_link_es->link
    ->getUrlObject()
    ->getOptions()['attributes']['title']);
  self::assertEquals('es', $menu_link_es->link
    ->getUrlObject()
    ->getOptions()['language']
    ->getId());
}