You are here

public function MenuUiNodeTest::testMultilingualMenuNodeFormWidget in Drupal 9

Same name and namespace in other branches
  1. 8 core/modules/menu_ui/tests/src/Functional/MenuUiNodeTest.php \Drupal\Tests\menu_ui\Functional\MenuUiNodeTest::testMultilingualMenuNodeFormWidget()

Testing correct loading and saving of menu links via node form widget in a multilingual environment.

File

core/modules/menu_ui/tests/src/Functional/MenuUiNodeTest.php, line 276

Class

MenuUiNodeTest
Add, edit, and delete a node with menu link.

Namespace

Drupal\Tests\menu_ui\Functional

Code

public function testMultilingualMenuNodeFormWidget() {

  // Setup languages.
  $langcodes = [
    'de',
  ];
  foreach ($langcodes as $langcode) {
    ConfigurableLanguage::createFromLangcode($langcode)
      ->save();
  }
  array_unshift($langcodes, \Drupal::languageManager()
    ->getDefaultLanguage()
    ->getId());
  $config = \Drupal::service('config.factory')
    ->getEditable('language.negotiation');

  // Ensure path prefix is used to determine the language.
  $config
    ->set('url.source', 'path_prefix');

  // Ensure that there's a path prefix set for english as well.
  $config
    ->set('url.prefixes.' . $langcodes[0], $langcodes[0]);
  $config
    ->save();
  $this
    ->rebuildContainer();
  $languages = [];
  foreach ($langcodes as $langcode) {
    $languages[$langcode] = ConfigurableLanguage::load($langcode);
  }

  // Use a UI form submission to make the node type and menu link content entity translatable.
  $this
    ->drupalLogout();
  $this
    ->drupalLogin($this->rootUser);
  $edit = [
    'entity_types[node]' => TRUE,
    'entity_types[menu_link_content]' => TRUE,
    'settings[node][page][settings][language][language_alterable]' => TRUE,
    'settings[node][page][translatable]' => TRUE,
    'settings[node][page][fields][title]' => TRUE,
    'settings[menu_link_content][menu_link_content][translatable]' => TRUE,
  ];
  $this
    ->drupalGet('admin/config/regional/content-language');
  $this
    ->submitForm($edit, 'Save configuration');

  // Log out and back in as normal user.
  $this
    ->drupalLogout();
  $this
    ->drupalLogin($this->editor);

  // Create a node.
  $node_title = $this
    ->randomMachineName(8);
  $node = Node::create([
    'type' => 'page',
    'title' => $node_title,
    'body' => $this
      ->randomMachineName(16),
    'uid' => $this->editor
      ->id(),
    'status' => 1,
    'langcode' => $langcodes[0],
  ]);
  $node
    ->save();

  // Create translation.
  $translated_node_title = $this
    ->randomMachineName(8);
  $node
    ->addTranslation($langcodes[1], [
    'title' => $translated_node_title,
    'body' => $this
      ->randomMachineName(16),
    'status' => 1,
  ]);
  $node
    ->save();

  // Edit the node and create a menu link.
  $edit = [
    'menu[enabled]' => 1,
    'menu[title]' => $node_title,
    'menu[weight]' => 17,
  ];
  $options = [
    'language' => $languages[$langcodes[0]],
  ];
  $url = $node
    ->toUrl('edit-form', $options);
  $this
    ->drupalGet($url);
  $this
    ->submitForm($edit, 'Save (this translation)');

  // Edit the node in a different language and translate the menu link.
  $edit = [
    'menu[enabled]' => 1,
    'menu[title]' => $translated_node_title,
    'menu[weight]' => 17,
  ];
  $options = [
    'language' => $languages[$langcodes[1]],
  ];
  $url = $node
    ->toUrl('edit-form', $options);
  $this
    ->drupalGet($url);
  $this
    ->submitForm($edit, 'Save (this translation)');

  // Assert that the original link exists in the frontend.
  $this
    ->drupalGet('node/' . $node
    ->id(), [
    'language' => $languages[$langcodes[0]],
  ]);
  $this
    ->assertSession()
    ->linkExists($node_title);

  // Assert that the translated link exists in the frontend.
  $this
    ->drupalGet('node/' . $node
    ->id(), [
    'language' => $languages[$langcodes[1]],
  ]);
  $this
    ->assertSession()
    ->linkExists($translated_node_title);

  // Revisit the edit page in original language, check the loaded menu item title and save.
  $options = [
    'language' => $languages[$langcodes[0]],
  ];
  $url = $node
    ->toUrl('edit-form', $options);
  $this
    ->drupalGet($url);
  $this
    ->assertSession()
    ->fieldValueEquals('edit-menu-title', $node_title);
  $this
    ->submitForm([], 'Save (this translation)');

  // Revisit the edit page of the translation and check the loaded menu item title.
  $options = [
    'language' => $languages[$langcodes[1]],
  ];
  $url = $node
    ->toUrl('edit-form', $options);
  $this
    ->drupalGet($url);
  $this
    ->assertSession()
    ->fieldValueEquals('edit-menu-title', $translated_node_title);
}