View source
<?php
namespace Drupal\Tests\menu_multilingual\Functional;
use Drupal\language\Entity\ConfigurableLanguage;
use Drupal\Tests\BrowserTestBase;
class MenuMultilingualTest extends BrowserTestBase {
public $menuBlockConfigPath = 'admin/structure/block/manage/stark_main_menu';
protected $defaultTheme = 'stark';
protected $strictConfigSchema = FALSE;
protected static $modules = [
'block',
'content_translation',
'config_translation',
'language',
'menu_link_content',
'menu_multilingual',
'menu_ui',
'node',
'views_ui',
];
protected function setUp() : void {
parent::setUp();
$this
->drupalLogin($this->rootUser);
$this
->drupalPlaceBlock('system_menu_block:main', [
'id' => 'stark_main_menu',
]);
$this
->drupalCreateContentType([
'type' => 'article',
]);
ConfigurableLanguage::createFromLangcode('fr')
->save();
$this
->drupalGet('admin/config/regional/language/detection/url');
$this
->submitForm([
'prefix[en]' => 'en',
'prefix[fr]' => 'fr',
], 'Save configuration');
$this
->drupalGet('admin/config/regional/language/detection');
$this
->submitForm([
'language_interface[enabled][language-url]' => 1,
], 'Save settings');
$this
->drupalGet('admin/config/regional/content-language');
$this
->submitForm([
"entity_types[node]" => 1,
"settings[node][article][translatable]" => 1,
"settings[node][article][settings][language][language_alterable]" => 1,
"entity_types[menu_link_content]" => 1,
"settings[menu_link_content][menu_link_content][translatable]" => 1,
"settings[menu_link_content][menu_link_content][settings][language][language_alterable]" => 1,
], 'Save configuration');
\Drupal::entityTypeManager()
->clearCachedDefinitions();
$edit = [
'title[0][value]' => 'Node 1 translated English',
'langcode[0][value]' => 'en',
];
$this
->drupalGet('node/add/article');
$this
->submitForm($edit, 'Save');
$edit = [
'title[0][value]' => 'Node 1 translated French',
];
$this
->drupalGet('node/1/translations/add/en/fr');
$this
->submitForm($edit, 'Save');
$edit = [
'title[0][value]' => 'Node 2 untranslated English',
'langcode[0][value]' => 'en',
];
$this
->drupalGet('node/add/article');
$this
->submitForm($edit, 'Save');
$edit = [
'title[0][value]' => 'Node 3 Language Not Applicable',
'langcode[0][value]' => 'zxx',
];
$this
->drupalGet('node/add/article');
$this
->submitForm($edit, 'Save');
$edit = [
'title[0][value]' => 'Node 4 Language Not Specified',
'langcode[0][value]' => 'und',
];
$this
->drupalGet('node/add/article');
$this
->submitForm($edit, 'Save');
}
public function testMenuMultilingualFormDisplay() {
$this
->drupalGet($this->menuBlockConfigPath);
$this
->assertSession()
->pageTextContains("Configure block");
$this
->assertSession()
->pageTextContains("Hide menu items without translated label");
$this
->assertSession()
->pageTextContains("Hide menu items without translated content");
}
public function testCustomMenuItemTranslated() {
$this
->createCustomMenuItemTranslated('Node 1 translated', 1);
$this
->createCustomMenuItemTranslated('Node 2 untranslated', 2);
$this
->createCustomMenuItemTranslated('Node 3 Language Not Applicable', 3);
$this
->createCustomMenuItemTranslated('Node 4 Language Not Specified', 4);
$session = $this
->assertSession();
$this
->drupalGet('node');
$session
->pageTextContains('Node 1 translated, English menu item');
$session
->pageTextContains('Node 2 untranslated, English menu item');
$session
->pageTextContains('Node 3 Language Not Applicable, English menu item');
$session
->pageTextContains('Node 4 Language Not Specified, English menu item');
$this
->drupalGet('fr/node');
$session
->pageTextContains('Node 1 translated, French menu item');
$session
->pageTextContains('Node 2 untranslated, French menu item');
$session
->pageTextContains('Node 3 Language Not Applicable, French menu item');
$session
->pageTextContains('Node 4 Language Not Specified, French menu item');
$edit = [
'settings[only_translated_labels]' => 1,
'settings[only_translated_content]' => 0,
];
$this
->drupalGet($this->menuBlockConfigPath);
$this
->submitForm($edit, 'Save block');
$this
->drupalGet('node');
$session
->pageTextContains('Node 1 translated, English menu item');
$session
->pageTextContains('Node 2 untranslated, English menu item');
$session
->pageTextContains('Node 3 Language Not Applicable, English menu item');
$session
->pageTextContains('Node 4 Language Not Specified, English menu item');
$this
->drupalGet('fr/node');
$session
->pageTextContains('Node 1 translated, French menu item');
$session
->pageTextContains('Node 2 untranslated, French menu item');
$session
->pageTextContains('Node 3 Language Not Applicable, French menu item');
$session
->pageTextContains('Node 4 Language Not Specified, French menu item');
$edit = [
'settings[only_translated_labels]' => 1,
'settings[only_translated_content]' => 1,
];
$this
->drupalGet($this->menuBlockConfigPath);
$this
->submitForm($edit, 'Save block');
$this
->drupalGet('node');
$session
->pageTextContains('Node 1 translated, English menu item');
$session
->pageTextContains('Node 2 untranslated, English menu item');
$session
->pageTextContains('Node 3 Language Not Applicable, English menu item');
$session
->pageTextNotContains('Node 4 Language Not Specified, English menu item');
$this
->drupalGet('fr/node');
$session
->pageTextContains('Node 1 translated, French menu item');
$session
->pageTextNotContains('Node 2 untranslated, French menu item');
$session
->pageTextContains('Node 3 Language Not Applicable, French menu item');
$session
->pageTextNotContains('Node 4 Language Not Specified, French menu item');
$edit = [
'settings[only_translated_labels]' => 0,
'settings[only_translated_content]' => 1,
];
$this
->drupalGet($this->menuBlockConfigPath);
$this
->submitForm($edit, 'Save block');
$this
->drupalGet('node');
$session
->pageTextContains('Node 1 translated, English menu item');
$session
->pageTextContains('Node 2 untranslated, English menu item');
$session
->pageTextContains('Node 3 Language Not Applicable, English menu item');
$session
->pageTextNotContains('Node 4 Language Not Specified, English menu item');
$this
->drupalGet('fr/node');
$session
->pageTextContains('Node 1 translated, French menu item');
$session
->pageTextNotContains('Node 2 untranslated, French menu item');
$session
->pageTextContains('Node 3 Language Not Applicable, French menu item');
$session
->pageTextNotContains('Node 4 Language Not Specified, French menu item');
}
public function testCustomMenuItemUntranslated() {
$this
->createCustomMenuItemUntranslated('Node 1 translated', 1);
$this
->createCustomMenuItemUntranslated('Node 2 untranslated', 2);
$this
->createCustomMenuItemUntranslated('Node 3 Language Not Applicable', 3);
$this
->createCustomMenuItemUntranslated('Node 4 Language Not Specified', 4);
$session = $this
->assertSession();
$this
->drupalGet('node');
$session
->pageTextContains('Node 1 translated, English menu item');
$session
->pageTextContains('Node 2 untranslated, English menu item');
$session
->pageTextContains('Node 3 Language Not Applicable, English menu item');
$session
->pageTextContains('Node 4 Language Not Specified, English menu item');
$this
->drupalGet('fr/node');
$session
->pageTextContains('Node 1 translated, English menu item');
$session
->pageTextContains('Node 2 untranslated, English menu item');
$session
->pageTextContains('Node 3 Language Not Applicable, English menu item');
$session
->pageTextContains('Node 4 Language Not Specified, English menu item');
$edit = [
'settings[only_translated_labels]' => 1,
'settings[only_translated_content]' => 0,
];
$this
->drupalGet($this->menuBlockConfigPath);
$this
->submitForm($edit, 'Save block');
$this
->drupalGet('node');
$session
->pageTextContains('Node 1 translated, English menu item');
$session
->pageTextContains('Node 2 untranslated, English menu item');
$session
->pageTextContains('Node 3 Language Not Applicable, English menu item');
$session
->pageTextContains('Node 4 Language Not Specified, English menu item');
$this
->drupalGet('fr/node');
$session
->pageTextNotContains('Node 1 translated, English menu item');
$session
->pageTextNotContains('Node 2 untranslated, English menu item');
$session
->pageTextNotContains('Node 3 Language Not Applicable, English menu item');
$session
->pageTextNotContains('Node 4 Language Not Specified, English menu item');
$edit = [
'settings[only_translated_labels]' => 1,
'settings[only_translated_content]' => 1,
];
$this
->drupalGet($this->menuBlockConfigPath);
$this
->submitForm($edit, 'Save block');
$this
->drupalGet('node');
$session
->pageTextContains('Node 1 translated, English menu item');
$session
->pageTextContains('Node 2 untranslated, English menu item');
$session
->pageTextContains('Node 3 Language Not Applicable, English menu item');
$session
->pageTextNotContains('Node 4 Language Not Specified, English menu item');
$this
->drupalGet('fr/node');
$session
->pageTextNotContains('Node 1 translated, English menu item');
$session
->pageTextNotContains('Node 2 untranslated, English menu item');
$session
->pageTextNotContains('Node 3 Language Not Applicable, English menu item');
$session
->pageTextNotContains('Node 4 Language Not Specified, English menu item');
$edit = [
'settings[only_translated_labels]' => 0,
'settings[only_translated_content]' => 1,
];
$this
->drupalGet($this->menuBlockConfigPath);
$this
->submitForm($edit, 'Save block');
$this
->drupalGet('node');
$session
->pageTextContains('Node 1 translated, English menu item');
$session
->pageTextContains('Node 2 untranslated, English menu item');
$session
->pageTextContains('Node 3 Language Not Applicable, English menu item');
$session
->pageTextNotContains('Node 4 Language Not Specified, English menu item');
$this
->drupalGet('fr/node');
$session
->pageTextContains('Node 1 translated, English menu item');
$session
->pageTextNotContains('Node 2 untranslated, English menu item');
$session
->pageTextContains('Node 3 Language Not Applicable, English menu item');
$session
->pageTextNotContains('Node 4 Language Not Specified, English menu item');
}
public function testCustomMenuItemLanguageNotApplicable() {
$this
->createCustomMenuItemLanguageNotApplicable('Node 1 translated', 1);
$this
->createCustomMenuItemLanguageNotApplicable('Node 2 untranslated', 2);
$this
->createCustomMenuItemLanguageNotApplicable('Node 3 Language Not Applicable', 3);
$this
->createCustomMenuItemLanguageNotApplicable('Node 4 Language Not Specified', 4);
$session = $this
->assertSession();
$this
->drupalGet('node');
$session
->pageTextContains('Node 1 translated, Language Not Applicable menu item');
$session
->pageTextContains('Node 2 untranslated, Language Not Applicable menu item');
$session
->pageTextContains('Node 3 Language Not Applicable, Language Not Applicable menu item');
$session
->pageTextContains('Node 4 Language Not Specified, Language Not Applicable menu item');
$this
->drupalGet('fr/node');
$session
->pageTextContains('Node 1 translated, Language Not Applicable menu item');
$session
->pageTextContains('Node 2 untranslated, Language Not Applicable menu item');
$session
->pageTextContains('Node 3 Language Not Applicable, Language Not Applicable menu item');
$session
->pageTextContains('Node 4 Language Not Specified, Language Not Applicable menu item');
$edit = [
'settings[only_translated_labels]' => 1,
'settings[only_translated_content]' => 0,
];
$this
->drupalGet($this->menuBlockConfigPath);
$this
->submitForm($edit, 'Save block');
$this
->drupalGet('node');
$session
->pageTextContains('Node 1 translated, Language Not Applicable menu item');
$session
->pageTextContains('Node 2 untranslated, Language Not Applicable menu item');
$session
->pageTextContains('Node 3 Language Not Applicable, Language Not Applicable menu item');
$session
->pageTextContains('Node 4 Language Not Specified, Language Not Applicable menu item');
$this
->drupalGet('fr/node');
$session
->pageTextContains('Node 1 translated, Language Not Applicable menu item');
$session
->pageTextContains('Node 2 untranslated, Language Not Applicable menu item');
$session
->pageTextContains('Node 3 Language Not Applicable, Language Not Applicable menu item');
$session
->pageTextContains('Node 4 Language Not Specified, Language Not Applicable menu item');
$edit = [
'settings[only_translated_labels]' => 1,
'settings[only_translated_content]' => 1,
];
$this
->drupalGet($this->menuBlockConfigPath);
$this
->submitForm($edit, 'Save block');
$this
->drupalGet('node');
$session
->pageTextContains('Node 1 translated, Language Not Applicable menu item');
$session
->pageTextContains('Node 2 untranslated, Language Not Applicable menu item');
$session
->pageTextContains('Node 3 Language Not Applicable, Language Not Applicable menu item');
$session
->pageTextNotContains('Node 4 Language Not Specified, Language Not Applicable menu item');
$this
->drupalGet('fr/node');
$session
->pageTextContains('Node 1 translated, Language Not Applicable menu item');
$session
->pageTextNotContains('Node 2 untranslated, Language Not Applicable menu item');
$session
->pageTextContains('Node 3 Language Not Applicable, Language Not Applicable menu item');
$session
->pageTextNotContains('Node 4 Language Not Specified, Language Not Applicable menu item');
$edit = [
'settings[only_translated_labels]' => 0,
'settings[only_translated_content]' => 1,
];
$this
->drupalGet($this->menuBlockConfigPath);
$this
->submitForm($edit, 'Save block');
$this
->drupalGet('node');
$session
->pageTextContains('Node 1 translated, Language Not Applicable menu item');
$session
->pageTextContains('Node 2 untranslated, Language Not Applicable menu item');
$session
->pageTextContains('Node 3 Language Not Applicable, Language Not Applicable menu item');
$session
->pageTextNotContains('Node 4 Language Not Specified, Language Not Applicable menu item');
$this
->drupalGet('fr/node');
$session
->pageTextContains('Node 1 translated, Language Not Applicable menu item');
$session
->pageTextNotContains('Node 2 untranslated, Language Not Applicable menu item');
$session
->pageTextContains('Node 3 Language Not Applicable, Language Not Applicable menu item');
$session
->pageTextNotContains('Node 4 Language Not Specified, Language Not Applicable menu item');
}
public function testCustomMenuItemLanguageNotSpecified() {
$this
->createCustomMenuItemLanguageNotSpecified('Node 1 translated', 1);
$this
->createCustomMenuItemLanguageNotSpecified('Node 2 untranslated', 2);
$this
->createCustomMenuItemLanguageNotSpecified('Node 3 Language Not Applicable', 3);
$this
->createCustomMenuItemLanguageNotSpecified('Node 4 Language Not Specified', 4);
$session = $this
->assertSession();
$this
->drupalGet('node');
$session
->pageTextContains('Node 1 translated, Language Not Specified menu item');
$session
->pageTextContains('Node 2 untranslated, Language Not Specified menu item');
$session
->pageTextContains('Node 3 Language Not Applicable, Language Not Specified menu item');
$session
->pageTextContains('Node 4 Language Not Specified, Language Not Specified menu item');
$this
->drupalGet('fr/node');
$session
->pageTextContains('Node 1 translated, Language Not Specified menu item');
$session
->pageTextContains('Node 2 untranslated, Language Not Specified menu item');
$session
->pageTextContains('Node 3 Language Not Applicable, Language Not Specified menu item');
$session
->pageTextContains('Node 4 Language Not Specified, Language Not Specified menu item');
$edit = [
'settings[only_translated_labels]' => 1,
'settings[only_translated_content]' => 0,
];
$this
->drupalGet($this->menuBlockConfigPath);
$this
->submitForm($edit, 'Save block');
$this
->drupalGet('node');
$session
->pageTextNotContains('Node 1 translated, Language Not Specified menu item');
$session
->pageTextNotContains('Node 2 untranslated, Language Not Specified menu item');
$session
->pageTextNotContains('Node 3 Language Not Applicable, Language Not Specified menu item');
$session
->pageTextNotContains('Node 4 Language Not Specified, Language Not Specified menu item');
$this
->drupalGet('fr/node');
$session
->pageTextNotContains('Node 1 translated, Language Not Specified menu item');
$session
->pageTextNotContains('Node 2 untranslated, Language Not Specified menu item');
$session
->pageTextNotContains('Node 3 Language Not Applicable, Language Not Specified menu item');
$session
->pageTextNotContains('Node 4 Language Not Specified, Language Not Specified menu item');
$edit = [
'settings[only_translated_labels]' => 1,
'settings[only_translated_content]' => 1,
];
$this
->drupalGet($this->menuBlockConfigPath);
$this
->submitForm($edit, 'Save block');
$this
->drupalGet('node');
$session
->pageTextNotContains('Node 1 translated, Language Not Specified menu item');
$session
->pageTextNotContains('Node 2 untranslated, Language Not Specified menu item');
$session
->pageTextNotContains('Node 3 Language Not Applicable, Language Not Specified menu item');
$session
->pageTextNotContains('Node 4 Language Not Specified, Language Not Specified menu item');
$this
->drupalGet('fr/node');
$session
->pageTextNotContains('Node 1 translated, Language Not Specified menu item');
$session
->pageTextNotContains('Node 2 untranslated, Language Not Specified menu item');
$session
->pageTextNotContains('Node 3 Language Not Applicable, Language Not Specified menu item');
$session
->pageTextNotContains('Node 4 Language Not Specified, Language Not Specified menu item');
$edit = [
'settings[only_translated_labels]' => 0,
'settings[only_translated_content]' => 1,
];
$this
->drupalGet($this->menuBlockConfigPath);
$this
->submitForm($edit, 'Save block');
$this
->drupalGet('node');
$session
->pageTextContains('Node 1 translated, Language Not Specified menu item');
$session
->pageTextContains('Node 2 untranslated, Language Not Specified menu item');
$session
->pageTextContains('Node 3 Language Not Applicable, Language Not Specified menu item');
$session
->pageTextNotContains('Node 4 Language Not Specified, Language Not Specified menu item');
$this
->drupalGet('fr/node');
$session
->pageTextContains('Node 1 translated, Language Not Specified menu item');
$session
->pageTextNotContains('Node 2 untranslated, Language Not Specified menu item');
$session
->pageTextContains('Node 3 Language Not Applicable, Language Not Specified menu item');
$session
->pageTextNotContains('Node 4 Language Not Specified, Language Not Specified menu item');
}
public function testViewsMenuItemTranslated() {
$this
->createViewsMenuItemTranslated();
$session = $this
->assertSession();
$this
->drupalGet('node');
$session
->pageTextContains('Views English menu item');
$this
->drupalGet('fr/node');
$session
->pageTextContains('Views French menu item');
$edit = [
'settings[only_translated_labels]' => 1,
'settings[only_translated_content]' => 0,
];
$this
->drupalGet($this->menuBlockConfigPath);
$this
->submitForm($edit, 'Save block');
$this
->drupalGet('node');
$session
->pageTextContains('Views English menu item');
$this
->drupalGet('fr/node');
$session
->pageTextContains('Views French menu item');
$edit = [
'settings[only_translated_labels]' => 1,
'settings[only_translated_content]' => 1,
];
$this
->drupalGet($this->menuBlockConfigPath);
$this
->submitForm($edit, 'Save block');
$this
->drupalGet('node');
$session
->pageTextContains('Views English menu item');
$this
->drupalGet('fr/node');
$session
->pageTextContains('Views French menu item');
$edit = [
'settings[only_translated_labels]' => 0,
'settings[only_translated_content]' => 1,
];
$this
->drupalGet($this->menuBlockConfigPath);
$this
->submitForm($edit, 'Save block');
$this
->drupalGet('node');
$session
->pageTextContains('Views English menu item');
$this
->drupalGet('fr/node');
$session
->pageTextContains('Views French menu item');
}
public function testViewsMenuItemUntranslated() {
$this
->createViewsMenuItemUntranslated();
$session = $this
->assertSession();
$this
->drupalGet('node');
$session
->pageTextContains('Views English menu item');
$this
->drupalGet('fr/node');
$session
->pageTextContains('Views English menu item');
$edit = [
'settings[only_translated_labels]' => 1,
'settings[only_translated_content]' => 0,
];
$this
->drupalGet($this->menuBlockConfigPath);
$this
->submitForm($edit, 'Save block');
$this
->drupalGet('node');
$session
->pageTextContains('Views English menu item');
$this
->drupalGet('fr/node');
$session
->pageTextNotContains('Views English menu item');
$edit = [
'settings[only_translated_labels]' => 1,
'settings[only_translated_content]' => 1,
];
$this
->drupalGet($this->menuBlockConfigPath);
$this
->submitForm($edit, 'Save block');
$this
->drupalGet('node');
$session
->pageTextContains('Views English menu item');
$this
->drupalGet('fr/node');
$session
->pageTextNotContains('Views English menu item');
$edit = [
'settings[only_translated_labels]' => 0,
'settings[only_translated_content]' => 1,
];
$this
->drupalGet($this->menuBlockConfigPath);
$this
->submitForm($edit, 'Save block');
$this
->drupalGet('node');
$session
->pageTextContains('Views English menu item');
$this
->drupalGet('fr/node');
$session
->pageTextContains('Views English menu item');
}
public function createCustomMenuItemTranslated(string $title, int $nid) {
$edit = [
'title[0][value]' => $title . ', English menu item',
'link[0][uri]' => '/node/' . $nid,
'langcode[0][value]' => 'en',
];
$this
->drupalGet('admin/structure/menu/manage/main/add');
$this
->submitForm($edit, 'Save');
$edit = [
'title[0][value]' => $title . ', French menu item',
'link[0][uri]' => '/node/' . $nid,
];
$this
->drupalGet('admin/structure/menu/item/' . $nid . '/edit/translations/add/en/fr');
$this
->submitForm($edit, 'Save');
}
public function createCustomMenuItemUntranslated(string $title, int $nid) {
$edit = [
'title[0][value]' => $title . ', English menu item',
'link[0][uri]' => '/node/' . $nid,
'langcode[0][value]' => 'en',
];
$this
->drupalGet('admin/structure/menu/manage/main/add');
$this
->submitForm($edit, 'Save');
}
public function createCustomMenuItemLanguageNotApplicable(string $title, int $nid) {
$edit = [
'title[0][value]' => $title . ', Language Not Applicable menu item',
'link[0][uri]' => '/node/' . $nid,
'langcode[0][value]' => 'zxx',
];
$this
->drupalGet('admin/structure/menu/manage/main/add');
$this
->submitForm($edit, 'Save');
}
public function createCustomMenuItemLanguageNotSpecified(string $title, int $nid) {
$edit = [
'title[0][value]' => $title . ', Language Not Specified menu item',
'link[0][uri]' => '/node/' . $nid,
'langcode[0][value]' => 'und',
];
$this
->drupalGet('admin/structure/menu/manage/main/add');
$this
->submitForm($edit, 'Save');
}
public function createViewsMenuItemTranslated() {
$edit = [
'menu[title]' => 'Views English menu item',
'menu[type]' => 'normal',
'menu[parent]' => 'main:',
];
$this
->drupalGet('admin/structure/views/nojs/display/content/page_1/menu');
$this
->submitForm($edit, 'Apply');
$this
->drupalGet('admin/structure/views/view/content/edit/page_1');
$this
->submitForm([], 'Save');
$edit = [
'translation[config_names][views.view.content][display][page_1][display_options][menu][title]' => 'Views French menu item',
];
$this
->drupalGet('admin/structure/views/view/content/translate/fr/add');
$this
->submitForm($edit, 'Save translation');
}
public function createViewsMenuItemUntranslated() {
$edit = [
'menu[title]' => 'Views English menu item',
'menu[type]' => 'normal',
'menu[parent]' => 'main:',
];
$this
->drupalGet('admin/structure/views/nojs/display/content/page_1/menu');
$this
->submitForm($edit, 'Apply');
$this
->drupalGet('admin/structure/views/view/content/edit/page_1');
$this
->submitForm([], 'Save');
}
}