Chris@0: drupalLogin($this->drupalCreateUser(['access administration pages', 'administer menu'])); Chris@0: Chris@0: // Add some custom languages. Chris@0: foreach (['aa', 'bb', 'cc', 'cs'] as $language_code) { Chris@0: ConfigurableLanguage::create([ Chris@0: 'id' => $language_code, Chris@0: 'label' => $this->randomMachineName(), Chris@0: ])->save(); Chris@0: } Chris@0: } Chris@0: Chris@0: /** Chris@0: * Tests menu language settings and the defaults for menu link items. Chris@0: */ Chris@0: public function testMenuLanguage() { Chris@0: // Create a test menu to test the various language-related settings. Chris@0: // Machine name has to be lowercase. Chris@0: $menu_name = Unicode::strtolower($this->randomMachineName(16)); Chris@0: $label = $this->randomString(); Chris@0: $edit = [ Chris@0: 'id' => $menu_name, Chris@0: 'description' => '', Chris@0: 'label' => $label, Chris@0: 'langcode' => 'aa', Chris@0: ]; Chris@0: $this->drupalPostForm('admin/structure/menu/add', $edit, t('Save')); Chris@0: ContentLanguageSettings::loadByEntityTypeBundle('menu_link_content', 'menu_link_content') Chris@0: ->setDefaultLangcode('bb') Chris@0: ->setLanguageAlterable(TRUE) Chris@0: ->save(); Chris@0: Chris@0: // Check menu language. Chris@0: $this->assertOptionSelected('edit-langcode', $edit['langcode'], 'The menu language was correctly selected.'); Chris@0: Chris@0: // Test menu link language. Chris@0: $link_path = '/'; Chris@0: Chris@0: // Add a menu link. Chris@0: $link_title = $this->randomString(); Chris@0: $edit = [ Chris@0: 'title[0][value]' => $link_title, Chris@0: 'link[0][uri]' => $link_path, Chris@0: ]; Chris@0: $this->drupalPostForm("admin/structure/menu/manage/$menu_name/add", $edit, t('Save')); Chris@0: // Check the link was added with the correct menu link default language. Chris@0: $menu_links = entity_load_multiple_by_properties('menu_link_content', ['title' => $link_title]); Chris@0: $menu_link = reset($menu_links); Chris@0: $this->assertMenuLink($menu_link->getPluginId(), [ Chris@0: 'menu_name' => $menu_name, Chris@0: 'route_name' => '', Chris@0: 'langcode' => 'bb', Chris@0: ]); Chris@0: Chris@0: // Edit menu link default, changing it to cc. Chris@0: ContentLanguageSettings::loadByEntityTypeBundle('menu_link_content', 'menu_link_content') Chris@0: ->setDefaultLangcode('cc') Chris@0: ->setLanguageAlterable(TRUE) Chris@0: ->save(); Chris@0: Chris@0: // Add a menu link. Chris@0: $link_title = $this->randomString(); Chris@0: $edit = [ Chris@0: 'title[0][value]' => $link_title, Chris@0: 'link[0][uri]' => $link_path, Chris@0: ]; Chris@0: $this->drupalPostForm("admin/structure/menu/manage/$menu_name/add", $edit, t('Save')); Chris@0: // Check the link was added with the correct new menu link default language. Chris@0: $menu_links = entity_load_multiple_by_properties('menu_link_content', ['title' => $link_title]); Chris@0: $menu_link = reset($menu_links); Chris@0: $this->assertMenuLink($menu_link->getPluginId(), [ Chris@0: 'menu_name' => $menu_name, Chris@0: 'route_name' => '', Chris@0: 'langcode' => 'cc', Chris@0: ]); Chris@0: Chris@0: // Now change the language of the new link to 'bb'. Chris@0: $edit = [ Chris@0: 'langcode[0][value]' => 'bb', Chris@0: ]; Chris@0: $this->drupalPostForm('admin/structure/menu/item/' . $menu_link->id() . '/edit', $edit, t('Save')); Chris@0: $this->assertMenuLink($menu_link->getPluginId(), [ Chris@0: 'menu_name' => $menu_name, Chris@0: 'route_name' => '', Chris@0: 'langcode' => 'bb', Chris@0: ]); Chris@0: Chris@0: // Saving menu link items ends up on the edit menu page. To check the menu Chris@0: // link has the correct language default on edit, go to the menu link edit Chris@0: // page first. Chris@0: $this->drupalGet('admin/structure/menu/item/' . $menu_link->id() . '/edit'); Chris@0: // Check that the language selector has the correct default value. Chris@0: $this->assertOptionSelected('edit-langcode-0-value', 'bb', 'The menu link language was correctly selected.'); Chris@0: Chris@0: // Edit menu to hide the language select on menu link item add. Chris@0: ContentLanguageSettings::loadByEntityTypeBundle('menu_link_content', 'menu_link_content') Chris@0: ->setDefaultLangcode('cc') Chris@0: ->setLanguageAlterable(FALSE) Chris@0: ->save(); Chris@0: Chris@0: // Check that the language selector is not available on menu link add page. Chris@0: $this->drupalGet("admin/structure/menu/manage/$menu_name/add"); Chris@0: $this->assertNoField('edit-langcode-0-value', 'The language selector field was hidden the page'); Chris@0: } Chris@0: Chris@0: }