function MenuUiLanguageTest::testMenuLanguage

Same name in other branches
  1. 9 core/modules/menu_ui/tests/src/Functional/MenuUiLanguageTest.php \Drupal\Tests\menu_ui\Functional\MenuUiLanguageTest::testMenuLanguage()
  2. 8.9.x core/modules/menu_ui/tests/src/Functional/MenuUiLanguageTest.php \Drupal\Tests\menu_ui\Functional\MenuUiLanguageTest::testMenuLanguage()
  3. 11.x core/modules/menu_ui/tests/src/Functional/MenuUiLanguageTest.php \Drupal\Tests\menu_ui\Functional\MenuUiLanguageTest::testMenuLanguage()

Tests menu language settings and the defaults for menu link items.

File

core/modules/menu_ui/tests/src/Functional/MenuUiLanguageTest.php, line 61

Class

MenuUiLanguageTest
Tests for menu_ui language settings.

Namespace

Drupal\Tests\menu_ui\Functional

Code

public function testMenuLanguage() : void {
    // Create a test menu to test the various language-related settings.
    // Machine name has to be lowercase.
    $menu_name = $this->randomMachineName(16);
    $label = $this->randomString();
    $edit = [
        'id' => $menu_name,
        'description' => '',
        'label' => $label,
        'langcode' => 'aa',
    ];
    $this->drupalGet('admin/structure/menu/add');
    $this->submitForm($edit, 'Save');
    ContentLanguageSettings::loadByEntityTypeBundle('menu_link_content', 'menu_link_content')->setDefaultLangcode('bb')
        ->setLanguageAlterable(TRUE)
        ->save();
    // Check menu language.
    $this->assertTrue($this->assertSession()
        ->optionExists('edit-langcode', $edit['langcode'])
        ->isSelected());
    // Test menu link language.
    $link_path = '/';
    // Add a menu link.
    $link_title = $this->randomString();
    $edit = [
        'title[0][value]' => $link_title,
        'link[0][uri]' => $link_path,
    ];
    $this->drupalGet("admin/structure/menu/manage/{$menu_name}/add");
    $this->submitForm($edit, 'Save');
    // Check the link was added with the correct menu link default language.
    $menu_links = \Drupal::entityTypeManager()->getStorage('menu_link_content')
        ->loadByProperties([
        'title' => $link_title,
    ]);
    $menu_link = reset($menu_links);
    $this->assertMenuLink([
        'menu_name' => $menu_name,
        'route_name' => '<front>',
        'langcode' => 'bb',
    ], $menu_link->getPluginId());
    // Edit menu link default, changing it to cc.
    ContentLanguageSettings::loadByEntityTypeBundle('menu_link_content', 'menu_link_content')->setDefaultLangcode('cc')
        ->setLanguageAlterable(TRUE)
        ->save();
    // Add a menu link.
    $link_title = $this->randomString();
    $edit = [
        'title[0][value]' => $link_title,
        'link[0][uri]' => $link_path,
    ];
    $this->drupalGet("admin/structure/menu/manage/{$menu_name}/add");
    $this->submitForm($edit, 'Save');
    // Check the link was added with the correct new menu link default language.
    $menu_links = \Drupal::entityTypeManager()->getStorage('menu_link_content')
        ->loadByProperties([
        'title' => $link_title,
    ]);
    $menu_link = reset($menu_links);
    $this->assertMenuLink([
        'menu_name' => $menu_name,
        'route_name' => '<front>',
        'langcode' => 'cc',
    ], $menu_link->getPluginId());
    // Now change the language of the new link to 'bb'.
    $edit = [
        'langcode[0][value]' => 'bb',
    ];
    $this->drupalGet('admin/structure/menu/item/' . $menu_link->id() . '/edit');
    $this->submitForm($edit, 'Save');
    $this->assertMenuLink([
        'menu_name' => $menu_name,
        'route_name' => '<front>',
        'langcode' => 'bb',
    ], $menu_link->getPluginId());
    // Saving menu link items ends up on the edit menu page. To check the menu
    // link has the correct language default on edit, go to the menu link edit
    // page first.
    $this->drupalGet('admin/structure/menu/item/' . $menu_link->id() . '/edit');
    // Check that the language selector has the correct default value.
    $this->assertTrue($this->assertSession()
        ->optionExists('edit-langcode-0-value', 'bb')
        ->isSelected());
    // Edit menu to hide the language select on menu link item add.
    ContentLanguageSettings::loadByEntityTypeBundle('menu_link_content', 'menu_link_content')->setDefaultLangcode('cc')
        ->setLanguageAlterable(FALSE)
        ->save();
    // Check that the language selector is not available on menu link add page.
    $this->drupalGet("admin/structure/menu/manage/{$menu_name}/add");
    $this->assertSession()
        ->fieldNotExists('edit-langcode-0-value');
}

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