3 namespace Drupal\menu_ui\Tests;
5 use Drupal\Component\Utility\Unicode;
6 use Drupal\language\Entity\ConfigurableLanguage;
7 use Drupal\language\Entity\ContentLanguageSettings;
10 * Tests for menu_ui language settings.
12 * Create menu and menu links in non-English language, and edit language
17 class MenuLanguageTest extends MenuWebTestBase {
24 public static $modules = ['language'];
26 protected function setUp() {
29 $this->drupalLogin($this->drupalCreateUser(['access administration pages', 'administer menu']));
31 // Add some custom languages.
32 foreach (['aa', 'bb', 'cc', 'cs'] as $language_code) {
33 ConfigurableLanguage::create([
34 'id' => $language_code,
35 'label' => $this->randomMachineName(),
41 * Tests menu language settings and the defaults for menu link items.
43 public function testMenuLanguage() {
44 // Create a test menu to test the various language-related settings.
45 // Machine name has to be lowercase.
46 $menu_name = Unicode::strtolower($this->randomMachineName(16));
47 $label = $this->randomString();
54 $this->drupalPostForm('admin/structure/menu/add', $edit, t('Save'));
55 ContentLanguageSettings::loadByEntityTypeBundle('menu_link_content', 'menu_link_content')
56 ->setDefaultLangcode('bb')
57 ->setLanguageAlterable(TRUE)
60 // Check menu language.
61 $this->assertOptionSelected('edit-langcode', $edit['langcode'], 'The menu language was correctly selected.');
63 // Test menu link language.
67 $link_title = $this->randomString();
69 'title[0][value]' => $link_title,
70 'link[0][uri]' => $link_path,
72 $this->drupalPostForm("admin/structure/menu/manage/$menu_name/add", $edit, t('Save'));
73 // Check the link was added with the correct menu link default language.
74 $menu_links = entity_load_multiple_by_properties('menu_link_content', ['title' => $link_title]);
75 $menu_link = reset($menu_links);
76 $this->assertMenuLink($menu_link->getPluginId(), [
77 'menu_name' => $menu_name,
78 'route_name' => '<front>',
82 // Edit menu link default, changing it to cc.
83 ContentLanguageSettings::loadByEntityTypeBundle('menu_link_content', 'menu_link_content')
84 ->setDefaultLangcode('cc')
85 ->setLanguageAlterable(TRUE)
89 $link_title = $this->randomString();
91 'title[0][value]' => $link_title,
92 'link[0][uri]' => $link_path,
94 $this->drupalPostForm("admin/structure/menu/manage/$menu_name/add", $edit, t('Save'));
95 // Check the link was added with the correct new menu link default language.
96 $menu_links = entity_load_multiple_by_properties('menu_link_content', ['title' => $link_title]);
97 $menu_link = reset($menu_links);
98 $this->assertMenuLink($menu_link->getPluginId(), [
99 'menu_name' => $menu_name,
100 'route_name' => '<front>',
104 // Now change the language of the new link to 'bb'.
106 'langcode[0][value]' => 'bb',
108 $this->drupalPostForm('admin/structure/menu/item/' . $menu_link->id() . '/edit', $edit, t('Save'));
109 $this->assertMenuLink($menu_link->getPluginId(), [
110 'menu_name' => $menu_name,
111 'route_name' => '<front>',
115 // Saving menu link items ends up on the edit menu page. To check the menu
116 // link has the correct language default on edit, go to the menu link edit
118 $this->drupalGet('admin/structure/menu/item/' . $menu_link->id() . '/edit');
119 // Check that the language selector has the correct default value.
120 $this->assertOptionSelected('edit-langcode-0-value', 'bb', 'The menu link language was correctly selected.');
122 // Edit menu to hide the language select on menu link item add.
123 ContentLanguageSettings::loadByEntityTypeBundle('menu_link_content', 'menu_link_content')
124 ->setDefaultLangcode('cc')
125 ->setLanguageAlterable(FALSE)
128 // Check that the language selector is not available on menu link add page.
129 $this->drupalGet("admin/structure/menu/manage/$menu_name/add");
130 $this->assertNoField('edit-langcode-0-value', 'The language selector field was hidden the page');