View source
<?php
namespace Drupal\Tests\menu_ui\Functional;
use Drupal\language\Entity\ConfigurableLanguage;
use Drupal\language\Entity\ContentLanguageSettings;
use Drupal\Tests\BrowserTestBase;
use Drupal\Tests\menu_ui\Traits\MenuUiTrait;
class MenuUiLanguageTest extends BrowserTestBase {
use MenuUiTrait;
protected static $modules = [
'language',
'menu_link_content',
'menu_ui',
];
protected $defaultTheme = 'stark';
protected function setUp() {
parent::setUp();
$this
->drupalLogin($this
->drupalCreateUser([
'access administration pages',
'administer menu',
]));
foreach ([
'aa',
'bb',
'cc',
'cs',
] as $language_code) {
ConfigurableLanguage::create([
'id' => $language_code,
'label' => $this
->randomMachineName(),
])
->save();
}
}
public function testMenuLanguage() {
$menu_name = mb_strtolower($this
->randomMachineName(16));
$label = $this
->randomString();
$edit = [
'id' => $menu_name,
'description' => '',
'label' => $label,
'langcode' => 'aa',
];
$this
->drupalPostForm('admin/structure/menu/add', $edit, t('Save'));
ContentLanguageSettings::loadByEntityTypeBundle('menu_link_content', 'menu_link_content')
->setDefaultLangcode('bb')
->setLanguageAlterable(TRUE)
->save();
$this
->assertOptionSelected('edit-langcode', $edit['langcode'], 'The menu language was correctly selected.');
$link_path = '/';
$link_title = $this
->randomString();
$edit = [
'title[0][value]' => $link_title,
'link[0][uri]' => $link_path,
];
$this
->drupalPostForm("admin/structure/menu/manage/{$menu_name}/add", $edit, t('Save'));
$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());
ContentLanguageSettings::loadByEntityTypeBundle('menu_link_content', 'menu_link_content')
->setDefaultLangcode('cc')
->setLanguageAlterable(TRUE)
->save();
$link_title = $this
->randomString();
$edit = [
'title[0][value]' => $link_title,
'link[0][uri]' => $link_path,
];
$this
->drupalPostForm("admin/structure/menu/manage/{$menu_name}/add", $edit, t('Save'));
$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());
$edit = [
'langcode[0][value]' => 'bb',
];
$this
->drupalPostForm('admin/structure/menu/item/' . $menu_link
->id() . '/edit', $edit, t('Save'));
$this
->assertMenuLink([
'menu_name' => $menu_name,
'route_name' => '<front>',
'langcode' => 'bb',
], $menu_link
->getPluginId());
$this
->drupalGet('admin/structure/menu/item/' . $menu_link
->id() . '/edit');
$this
->assertOptionSelected('edit-langcode-0-value', 'bb', 'The menu link language was correctly selected.');
ContentLanguageSettings::loadByEntityTypeBundle('menu_link_content', 'menu_link_content')
->setDefaultLangcode('cc')
->setLanguageAlterable(FALSE)
->save();
$this
->drupalGet("admin/structure/menu/manage/{$menu_name}/add");
$this
->assertNoField('edit-langcode-0-value', 'The language selector field was hidden the page');
}
}