function MenuUiNodeTest::testMultilingualMenuNodeFormWidget

Same name in other branches
  1. 9 core/modules/menu_ui/tests/src/Functional/MenuUiNodeTest.php \Drupal\Tests\menu_ui\Functional\MenuUiNodeTest::testMultilingualMenuNodeFormWidget()
  2. 8.9.x core/modules/menu_ui/tests/src/Functional/MenuUiNodeTest.php \Drupal\Tests\menu_ui\Functional\MenuUiNodeTest::testMultilingualMenuNodeFormWidget()
  3. 10 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 287

Class

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

Namespace

Drupal\Tests\menu_ui\Functional

Code

public function testMultilingualMenuNodeFormWidget() : void {
    // Setup languages.
    $langcodes = [
        'de',
    ];
    foreach ($langcodes as $langcode) {
        static::createLanguageFromLangcode($langcode);
    }
    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();
    $languages = [];
    foreach ($langcodes as $langcode) {
        $languages[$langcode] = ConfigurableLanguage::load($langcode);
    }
    // Enable translation for pages and menu link content..
    $this->enableContentTranslation('node', 'page');
    $this->enableContentTranslation('menu_link_content', 'menu_link_content');
    $this->rebuildContainer();
    // 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);
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.