View source
<?php
namespace Drupal\Tests\menu_ui\Functional;
use Drupal\language\Entity\ConfigurableLanguage;
use Drupal\menu_link_content\Entity\MenuLinkContent;
use Drupal\node\Entity\Node;
use Drupal\Tests\BrowserTestBase;
class MenuUiNodeTest extends BrowserTestBase {
protected $editor;
protected static $modules = [
'menu_ui',
'test_page_test',
'node',
'block',
'locale',
'language',
'content_translation',
];
protected $defaultTheme = 'stark';
protected function setUp() : void {
parent::setUp();
$this
->drupalPlaceBlock('system_menu_block:main');
$this
->drupalPlaceBlock('page_title_block');
$this
->drupalCreateContentType([
'type' => 'page',
'name' => 'Basic page',
]);
$this->editor = $this
->drupalCreateUser([
'access administration pages',
'administer content types',
'administer menu',
'create page content',
'edit any page content',
'delete any page content',
'create content translations',
'update content translations',
'delete content translations',
'translate any entity',
]);
$this
->drupalLogin($this->editor);
}
public function testMenuNodeFormWidget() {
$this
->drupalGet('admin/structure/types/manage/page');
$this
->assertSession()
->responseHeaderContains('X-Drupal-Cache-Contexts', 'user.roles:authenticated');
$title_max_length = \Drupal::service('entity_field.manager')
->getBaseFieldDefinitions('menu_link_content')['title']
->getSetting('max_length');
$this
->drupalGet('node/add/page');
$this
->assertSession()
->responseMatches('/<input .* id="edit-menu-title" .* maxlength="' . $title_max_length . '" .* \\/>/');
$description_max_length = \Drupal::service('entity_field.manager')
->getBaseFieldDefinitions('menu_link_content')['description']
->getSetting('max_length');
$this
->drupalGet('node/add/page');
$this
->assertSession()
->responseMatches('/<input .* id="edit-menu-description" .* maxlength="' . $description_max_length . '" .* \\/>/');
$edit = [
'menu_options[main]' => FALSE,
];
$this
->drupalGet('admin/structure/types/manage/page');
$this
->submitForm($edit, 'Save content type');
$this
->drupalGet('node/add/page');
$this
->assertSession()
->pageTextContains('Create Basic page');
$this
->assertSession()
->pageTextNotContains('Menu settings');
$node_title = $this
->randomMachineName();
$edit = [
'title[0][value]' => $node_title,
'body[0][value]' => $this
->randomString(),
];
$this
->submitForm($edit, 'Save');
$node = $this
->drupalGetNodeByTitle($node_title);
$this
->assertEquals($edit['title[0][value]'], $node
->getTitle());
$edit = [
'menu_options[tools]' => 1,
'menu_parent' => 'main:',
];
$this
->drupalGet('admin/structure/types/manage/page');
$this
->submitForm($edit, 'Save content type');
$this
->assertSession()
->pageTextContains('The selected menu link is not under one of the selected menus.');
$this
->assertSession()
->pageTextNotContains("The content type Basic page has been updated.");
$edit = [
'menu_options[main]' => 1,
'menu_options[tools]' => 1,
'menu_parent' => 'main:',
];
$this
->drupalGet('admin/structure/types/manage/page');
$this
->submitForm($edit, 'Save content type');
$this
->assertSession()
->pageTextContains("The content type Basic page has been updated.");
$edit = [
'title[0][value]' => $node_title,
'menu[enabled]' => 1,
'menu[title]' => 'Test preview',
];
$this
->drupalGet('node/add/page');
$this
->submitForm($edit, 'Preview');
$node_title = $this
->randomMachineName();
$edit = [
'title[0][value]' => $node_title,
'body[0][value]' => $this
->randomString(),
];
$this
->drupalGet('node/add/page');
$this
->submitForm($edit, 'Save');
$node = $this
->drupalGetNodeByTitle($node_title);
$this
->drupalGet('test-page');
$this
->assertSession()
->linkNotExists($node_title);
$edit = [
'menu[enabled]' => 1,
];
$this
->drupalGet('node/' . $node
->id() . '/edit');
$this
->submitForm($edit, 'Save');
$this
->drupalGet('test-page');
$this
->assertSession()
->linkNotExists($node_title);
$admin_user = $this
->drupalCreateUser([
'access administration pages',
'administer content types',
'administer nodes',
'administer menu',
'create page content',
'edit any page content',
]);
$this
->drupalLogin($admin_user);
$edit = [
'menu[enabled]' => 1,
'menu[title]' => $node_title,
'status[value]' => FALSE,
];
$this
->drupalGet('node/' . $node
->id() . '/edit');
$this
->submitForm($edit, 'Save');
$this
->drupalGet('test-page');
$this
->assertSession()
->linkNotExists($node_title, 'Found no menu link with the node unpublished');
$edit['status[value]'] = TRUE;
$this
->drupalGet('node/' . $node
->id() . '/edit');
$this
->submitForm($edit, 'Save');
$this
->drupalGet('test-page');
$this
->assertSession()
->linkExists($node_title, 0, 'Found a menu link with the node published');
$this
->drupalLogin($this->editor);
$edit = [
'menu[enabled]' => 1,
'menu[title]' => $node_title,
'menu[weight]' => 17,
];
$this
->drupalGet('node/' . $node
->id() . '/edit');
$this
->submitForm($edit, 'Save');
$this
->drupalGet('test-page');
$this
->assertSession()
->linkExists($node_title);
$this
->drupalGet('node/' . $node
->id() . '/edit');
$this
->assertSession()
->fieldValueEquals('edit-menu-weight', 17);
$this
->assertSession()
->responseMatches('/<input .* id="edit-menu-title" .* maxlength="' . $title_max_length . '" .* \\/>/');
$this
->assertSession()
->responseMatches('/<input .* id="edit-menu-description" .* maxlength="' . $description_max_length . '" .* \\/>/');
$link_id = menu_ui_get_menu_link_defaults($node)['entity_id'];
$link = MenuLinkContent::load($link_id);
$link
->set('enabled', FALSE);
$link
->save();
$this
->drupalGet($node
->toUrl('edit-form'));
$this
->submitForm($edit, 'Save');
$link = MenuLinkContent::load($link_id);
$this
->assertFalse($link
->isEnabled(), 'Saving a node with a disabled menu link keeps the menu link disabled.');
$edit = [
'menu[enabled]' => FALSE,
];
$this
->drupalGet('node/' . $node
->id() . '/edit');
$this
->submitForm($edit, 'Save');
$this
->drupalGet('test-page');
$this
->assertSession()
->linkNotExists($node_title);
$item = MenuLinkContent::create([
'link' => [
[
'uri' => 'entity:node/' . $node
->id(),
],
],
'title' => $this
->randomMachineName(16),
'menu_name' => 'admin',
]);
$item
->save();
$this
->drupalGet('node/' . $node
->id() . '/edit');
$this
->assertSession()
->pageTextContains('Provide a menu link');
$this
->drupalGet('node/' . $node
->id() . '/edit');
$this
->submitForm($edit, 'Save');
$link = MenuLinkContent::load($item
->id());
$this
->assertInstanceOf(MenuLinkContent::class, $link);
$item->menu_name->value = 'tools';
$item
->save();
$child_node = $this
->drupalCreateNode([
'type' => 'article',
]);
$child_item = MenuLinkContent::create([
'link' => [
[
'uri' => 'entity:node/' . $child_node
->id(),
],
],
'title' => $this
->randomMachineName(16),
'parent' => $item
->getPluginId(),
'menu_name' => $item
->getMenuName(),
]);
$child_item
->save();
$this
->drupalGet('node/' . $node
->id() . '/edit');
$this
->assertSession()
->optionNotExists('edit-menu-menu-parent', 'tools:' . $item
->getPluginId());
$this
->assertSession()
->optionNotExists('edit-menu-menu-parent', 'tools:' . $child_item
->getPluginId());
$this
->assertSession()
->optionNotExists('edit-menu-menu-parent', 'admin:');
}
public function testMultilingualMenuNodeFormWidget() {
$langcodes = [
'de',
];
foreach ($langcodes as $langcode) {
ConfigurableLanguage::createFromLangcode($langcode)
->save();
}
array_unshift($langcodes, \Drupal::languageManager()
->getDefaultLanguage()
->getId());
$config = \Drupal::service('config.factory')
->getEditable('language.negotiation');
$config
->set('url.source', 'path_prefix');
$config
->set('url.prefixes.' . $langcodes[0], $langcodes[0]);
$config
->save();
$this
->rebuildContainer();
$languages = [];
foreach ($langcodes as $langcode) {
$languages[$langcode] = ConfigurableLanguage::load($langcode);
}
$this
->drupalLogout();
$this
->drupalLogin($this->rootUser);
$edit = [
'entity_types[node]' => TRUE,
'entity_types[menu_link_content]' => TRUE,
'settings[node][page][settings][language][language_alterable]' => TRUE,
'settings[node][page][translatable]' => TRUE,
'settings[node][page][fields][title]' => TRUE,
'settings[menu_link_content][menu_link_content][translatable]' => TRUE,
];
$this
->drupalGet('admin/config/regional/content-language');
$this
->submitForm($edit, 'Save configuration');
$this
->drupalLogout();
$this
->drupalLogin($this->editor);
$node_title = $this
->randomMachineName(8);
$node = Node::create([
'type' => 'page',
'title' => $node_title,
'body' => $this
->randomMachineName(16),
'uid' => $this->editor
->id(),
'status' => 1,
'langcode' => $langcodes[0],
]);
$node
->save();
$translated_node_title = $this
->randomMachineName(8);
$node
->addTranslation($langcodes[1], [
'title' => $translated_node_title,
'body' => $this
->randomMachineName(16),
'status' => 1,
]);
$node
->save();
$edit = [
'menu[enabled]' => 1,
'menu[title]' => $node_title,
'menu[weight]' => 17,
];
$options = [
'language' => $languages[$langcodes[0]],
];
$url = $node
->toUrl('edit-form', $options);
$this
->drupalGet($url);
$this
->submitForm($edit, 'Save (this translation)');
$edit = [
'menu[enabled]' => 1,
'menu[title]' => $translated_node_title,
'menu[weight]' => 17,
];
$options = [
'language' => $languages[$langcodes[1]],
];
$url = $node
->toUrl('edit-form', $options);
$this
->drupalGet($url);
$this
->submitForm($edit, 'Save (this translation)');
$this
->drupalGet('node/' . $node
->id(), [
'language' => $languages[$langcodes[0]],
]);
$this
->assertSession()
->linkExists($node_title);
$this
->drupalGet('node/' . $node
->id(), [
'language' => $languages[$langcodes[1]],
]);
$this
->assertSession()
->linkExists($translated_node_title);
$options = [
'language' => $languages[$langcodes[0]],
];
$url = $node
->toUrl('edit-form', $options);
$this
->drupalGet($url);
$this
->assertSession()
->fieldValueEquals('edit-menu-title', $node_title);
$this
->submitForm([], 'Save (this translation)');
$options = [
'language' => $languages[$langcodes[1]],
];
$url = $node
->toUrl('edit-form', $options);
$this
->drupalGet($url);
$this
->assertSession()
->fieldValueEquals('edit-menu-title', $translated_node_title);
}
}