You are here

function TokenMenuTest::testMultilingualMenu in Token 8

Tests that the module doesn't affect integrity of the menu, when translating them and that menu links tokens are correct.

File

tests/src/Functional/TokenMenuTest.php, line 292

Class

TokenMenuTest
Tests menu tokens.

Namespace

Drupal\Tests\token\Functional

Code

function testMultilingualMenu() {

  // Place the menu block.
  $this
    ->drupalPlaceBlock('system_menu_block:main');

  // Add a second language.
  $language = ConfigurableLanguage::create([
    'id' => 'de',
    'label' => 'German',
  ]);
  $language
    ->save();

  // Create the article content type.
  $node_type = NodeType::create([
    'type' => 'article',
  ]);
  $node_type
    ->save();
  $permissions = [
    'access administration pages',
    'administer content translation',
    'administer content types',
    'administer languages',
    'create content translations',
    'create article content',
    'edit any article content',
    'translate any entity',
    'administer menu',
  ];
  $this
    ->drupalLogin($this
    ->drupalCreateUser($permissions));

  // Enable translation for articles and menu links.
  $this
    ->drupalGet('admin/config/regional/content-language');
  $edit = [
    'entity_types[node]' => TRUE,
    'entity_types[menu_link_content]' => TRUE,
    'settings[node][article][translatable]' => TRUE,
    'settings[node][article][fields][title]' => TRUE,
    'settings[menu_link_content][menu_link_content][translatable]' => TRUE,
  ];
  $this
    ->drupalPostForm(NULL, $edit, 'Save configuration');
  $this
    ->assertText('Settings successfully updated.');

  // Create an english node with an english menu.
  $this
    ->drupalGet('/node/add/article');
  $edit = [
    'title[0][value]' => 'English test node with menu',
    'menu[enabled]' => TRUE,
    'menu[title]' => 'English menu title',
  ];
  $this
    ->drupalPostForm('/node/add/article', $edit, 'Save');
  $this
    ->assertText('English test node with menu has been created.');

  // Add a german translation.
  $this
    ->drupalGet('node/1/translations');
  $this
    ->clickLink('Add');
  $edit = [
    'title[0][value]' => 'German test node with menu',
    'menu[enabled]' => TRUE,
    'menu[title]' => 'German menu title',
  ];
  $this
    ->drupalPostForm(NULL, $edit, 'Save (this translation)');
  $this
    ->assertText('German test node with menu has been updated.');

  // Verify that the menu links are correct.
  $this
    ->drupalGet('node/1');
  $this
    ->assertSession()
    ->linkExists('English menu title');
  $this
    ->drupalGet('de/node/1');
  $this
    ->assertSession()
    ->linkExists('German menu title');

  // Verify that tokens are correct.
  $node = Node::load(1);
  $this
    ->assertTokens('node', [
    'node' => $node,
  ], [
    'menu-link' => 'English menu title',
  ]);
  $this
    ->assertTokens('node', [
    'node' => $node,
  ], [
    'menu-link' => 'German menu title',
    'menu-link:title' => 'German menu title',
  ], [
    'langcode' => 'de',
  ]);

  // Get the menu link and create a child menu link to assert parent and root
  // tokens.
  $url = $node
    ->toUrl();

  /** @var \Drupal\Core\Menu\MenuLinkManagerInterface $menu_link_manager */
  $menu_link_manager = \Drupal::service('plugin.manager.menu.link');
  $links = $menu_link_manager
    ->loadLinksByRoute($url
    ->getRouteName(), $url
    ->getRouteParameters());
  $link = reset($links);
  $base_options = [
    'provider' => 'menu_test',
    'menu_name' => 'menu_test',
  ];
  $child_1 = $base_options + [
    'title' => 'child_1 title EN',
    'link' => [
      'uri' => 'internal:/menu-test/hierarchy/parent/child_1',
    ],
    'parent' => $link
      ->getPluginId(),
    'langcode' => 'en',
  ];
  $child_1 = MenuLinkContent::create($child_1);
  $child_1
    ->save();

  // Add the german translation.
  $child_1
    ->addTranslation('de', [
    'title' => 'child_1 title DE',
  ] + $child_1
    ->toArray());
  $child_1
    ->save();
  $this
    ->assertTokens('menu-link', [
    'menu-link' => $child_1,
  ], [
    'title' => 'child_1 title EN',
    'parents' => 'English menu title',
    'root' => 'English menu title',
  ]);
  $this
    ->assertTokens('menu-link', [
    'menu-link' => $child_1,
  ], [
    'title' => 'child_1 title DE',
    'parents' => 'German menu title',
    'root' => 'German menu title',
  ], [
    'langcode' => 'de',
  ]);
}