View source
<?php
namespace Drupal\Tests\menu_ui\Functional;
use Drupal\block\Entity\Block;
use Drupal\Core\EventSubscriber\MainContentViewSubscriber;
use Drupal\Core\Menu\MenuLinkInterface;
use Drupal\Core\Url;
use Drupal\menu_link_content\Entity\MenuLinkContent;
use Drupal\system\Entity\Menu;
use Drupal\node\Entity\Node;
use Drupal\node\NodeInterface;
use Drupal\system\MenuStorage;
use Drupal\Tests\BrowserTestBase;
use Drupal\Tests\menu_ui\Traits\MenuUiTrait;
class MenuUiTest extends BrowserTestBase {
use MenuUiTrait;
protected static $modules = [
'block',
'contextual',
'help',
'menu_link_content',
'menu_ui',
'node',
'path',
'test_page_test',
];
protected $defaultTheme = 'stark';
protected $adminUser;
protected $authenticatedUser;
protected $blockPlacements;
protected $menu;
protected $items;
protected function setUp() {
parent::setUp();
$this
->drupalPlaceBlock('page_title_block');
$this
->drupalCreateContentType([
'type' => 'article',
'name' => 'Article',
]);
$this->adminUser = $this
->drupalCreateUser([
'access administration pages',
'administer blocks',
'administer menu',
'create article content',
]);
$this->authenticatedUser = $this
->drupalCreateUser([]);
}
public function testMenu() {
$this
->drupalLogin($this->adminUser);
$this->items = [];
$this->menu = $this
->addCustomMenu();
$this
->doMenuTests();
$this
->doTestMenuBlock();
$this
->addInvalidMenuLink();
$this
->addCustomMenuCRUD();
$menu_link_manager = \Drupal::service('plugin.manager.menu.link');
$before_count = $menu_link_manager
->countMenuLinks(NULL);
$menu_link_manager
->rebuild();
$after_count = $menu_link_manager
->countMenuLinks(NULL);
$this
->assertIdentical($before_count, $after_count, 'MenuLinkManager::rebuild() does not add more links');
$this
->drupalLogin($this->authenticatedUser);
$this
->verifyAccess(403);
foreach ($this->items as $item) {
$node = Node::load(str_replace('internal:/node/', '', $item->link->uri));
$this
->verifyMenuLink($item, $node);
}
$this
->drupalLogin($this->adminUser);
$this
->drupalGet('admin/structure/menu/manage/' . $this->menu
->id());
$this
->assertLinkByHref(Url::fromRoute('entity.menu_link_content.delete_form', [
'menu_link_content' => $this->items[0]
->id(),
])
->toString());
$this
->assertNoLinkByHref(Url::fromRoute('menu_ui.link_reset', [
'menu_link_plugin' => $this->items[0]
->getPluginId(),
])
->toString());
$this
->drupalGet('admin/structure/menu/item/' . $this->items[0]
->id() . '/delete');
$this
->assertSession()
->statusCodeEquals(200);
$this
->drupalGet('admin/structure/menu/link/' . $this->items[0]
->getPluginId() . '/reset');
$this
->assertSession()
->statusCodeEquals(403);
foreach ($this->items as $item) {
$this
->deleteMenuLink($item);
}
$this
->deleteCustomMenu();
$instance = $this
->getStandardMenuLink();
$old_weight = $instance
->getWeight();
$edit = [];
$edit['weight'] = 10;
$id = $instance
->getPluginId();
$this
->drupalPostForm("admin/structure/menu/link/{$id}/edit", $edit, t('Save'));
$this
->assertSession()
->statusCodeEquals(200);
$this
->assertText('The menu link has been saved.');
$menu_link_manager
->resetDefinitions();
$instance = $menu_link_manager
->createInstance($instance
->getPluginId());
$this
->assertEqual($edit['weight'], $instance
->getWeight(), 'Saving an existing link updates the weight.');
$this
->resetMenuLink($instance, $old_weight);
}
public function addCustomMenuCRUD() {
$menu_name = strtolower($this
->randomMachineName(MenuStorage::MAX_ID_LENGTH));
$label = $this
->randomMachineName(16);
$menu = Menu::create([
'id' => $menu_name,
'label' => $label,
'description' => 'Description text',
]);
$menu
->save();
$this
->drupalGet('admin/structure/menu/manage/' . $menu_name);
$this
->assertRaw($label, 'Custom menu was added.');
$new_label = $this
->randomMachineName(16);
$menu
->set('label', $new_label);
$menu
->save();
$this
->drupalGet('admin/structure/menu/manage/' . $menu_name);
$this
->assertRaw($new_label, 'Custom menu was edited.');
}
public function addCustomMenu() {
$this
->drupalGet('admin/structure/menu/add');
$menu_name = strtolower($this
->randomMachineName(MenuStorage::MAX_ID_LENGTH + 1));
$label = $this
->randomMachineName(16);
$edit = [
'id' => $menu_name,
'description' => '',
'label' => $label,
];
$this
->drupalPostForm('admin/structure/menu/add', $edit, t('Save'));
$this
->assertRaw(t('@name cannot be longer than %max characters but is currently %length characters long.', [
'@name' => t('Menu name'),
'%max' => MenuStorage::MAX_ID_LENGTH,
'%length' => mb_strlen($menu_name),
]));
$menu_name = strtolower($this
->randomMachineName(MenuStorage::MAX_ID_LENGTH));
$edit['id'] = $menu_name;
$this
->drupalPostForm('admin/structure/menu/add', $edit, t('Save'));
$this
->assertNoRaw(t('@name cannot be longer than %max characters but is currently %length characters long.', [
'@name' => t('Menu name'),
'%max' => MenuStorage::MAX_ID_LENGTH,
'%length' => mb_strlen($menu_name),
]));
$this
->assertRaw(t('Menu %label has been added.', [
'%label' => $label,
]));
$this
->drupalGet('admin/structure/menu');
$this
->assertText($label, 'Menu created');
$this
->drupalGet('admin/structure/block/list/' . $this
->config('system.theme')
->get('default'));
$this
->clickLink('Place block');
$this
->assertText($label);
$block = $this
->drupalPlaceBlock('system_menu_block:' . $menu_name);
$this->blockPlacements[$menu_name] = $block
->id();
return Menu::load($menu_name);
}
public function deleteCustomMenu() {
$menu_name = $this->menu
->id();
$label = $this->menu
->label();
$this
->drupalPostForm("admin/structure/menu/manage/{$menu_name}/delete", [], t('Delete'));
$this
->assertSession()
->statusCodeEquals(200);
$this
->assertRaw(t('The menu %title has been deleted.', [
'%title' => $label,
]), 'Custom menu was deleted');
$this
->assertNull(Menu::load($menu_name), 'Custom menu was deleted');
$result = \Drupal::entityTypeManager()
->getStorage('menu_link_content')
->loadByProperties([
'menu_name' => $menu_name,
]);
$this
->assertEmpty($result, 'All menu links associated with the custom menu were deleted.');
$this
->drupalGet('admin/structure/menu/manage/main');
$this
->assertNoRaw('edit-delete', 'The delete button was not found');
$this
->drupalGet('admin/structure/menu/manage/main/delete');
$this
->assertText(t('You are not authorized to access this page.'));
}
public function doMenuTests() {
$menu_name = $this->menu
->id();
$this
->drupalGet(Url::fromRoute('entity.menu.edit_form', [
'menu' => $menu_name,
]));
$this
->clickLink(t('Add link'));
$link_title = $this
->randomString();
$this
->drupalPostForm(NULL, [
'link[0][uri]' => '/',
'title[0][value]' => $link_title,
], t('Save'));
$this
->assertUrl(Url::fromRoute('entity.menu.edit_form', [
'menu' => $menu_name,
]));
$this
->clickLink(t('Edit'));
$this
->assertFieldByName('title[0][value]', $link_title);
$link_title = $this
->randomString();
$this
->drupalPostForm(NULL, [
'title[0][value]' => $link_title,
], t('Save'));
$this
->assertUrl(Url::fromRoute('entity.menu.edit_form', [
'menu' => $menu_name,
]));
$this
->clickLink(t('Delete'));
$this
->assertRaw(t('Are you sure you want to delete the custom menu link %item?', [
'%item' => $link_title,
]));
$this
->drupalPostForm(NULL, [], t('Delete'));
$this
->assertUrl(Url::fromRoute('entity.menu.edit_form', [
'menu' => $menu_name,
]));
$node1 = $this
->drupalCreateNode([
'type' => 'article',
]);
$node2 = $this
->drupalCreateNode([
'type' => 'article',
]);
$node3 = $this
->drupalCreateNode([
'type' => 'article',
]);
$node4 = $this
->drupalCreateNode([
'type' => 'article',
]);
$node5 = $this
->drupalCreateNode([
'type' => 'article',
'path' => [
'alias' => '/node5',
],
]);
$this
->drupalGet('admin/structure/menu');
$this
->assertLinkByHref('admin/structure/menu/manage/' . $menu_name . '/add', 0, "The add menu link button URL is correct");
$this
->doMenuLinkFormDefaultsTest();
$item1 = $this
->addMenuLink('', '/node/' . $node1
->id(), $menu_name, TRUE);
$item2 = $this
->addMenuLink($item1
->getPluginId(), '/node/' . $node2
->id(), $menu_name, FALSE);
$item3 = $this
->addMenuLink($item2
->getPluginId(), '/node/' . $node3
->id(), $menu_name);
$this
->assertMenuLink([
'children' => [
$item2
->getPluginId(),
$item3
->getPluginId(),
],
'parents' => [
$item1
->getPluginId(),
],
'langcode' => 'en',
], $item1
->getPluginId());
$this
->assertMenuLink([
'children' => [
$item3
->getPluginId(),
],
'parents' => [
$item2
->getPluginId(),
$item1
->getPluginId(),
],
'langcode' => 'en',
], $item2
->getPluginId());
$this
->assertMenuLink([
'children' => [],
'parents' => [
$item3
->getPluginId(),
$item2
->getPluginId(),
$item1
->getPluginId(),
],
'langcode' => 'en',
], $item3
->getPluginId());
$this
->verifyMenuLink($item1, $node1);
$this
->verifyMenuLink($item2, $node2, $item1, $node1);
$this
->verifyMenuLink($item3, $node3, $item2, $node2);
$item4 = $this
->addMenuLink('', '/node/' . $node4
->id(), $menu_name);
$item5 = $this
->addMenuLink($item4
->getPluginId(), '/node/' . $node5
->id(), $menu_name);
$item6 = $this
->addMenuLink($item4
->getPluginId(), '/node5', $menu_name, TRUE, '0');
$this
->assertMenuLink([
'children' => [
$item5
->getPluginId(),
$item6
->getPluginId(),
],
'parents' => [
$item4
->getPluginId(),
],
'langcode' => 'en',
], $item4
->getPluginId());
$this
->assertMenuLink([
'children' => [],
'parents' => [
$item5
->getPluginId(),
$item4
->getPluginId(),
],
'langcode' => 'en',
], $item5
->getPluginId());
$this
->assertMenuLink([
'children' => [],
'parents' => [
$item6
->getPluginId(),
$item4
->getPluginId(),
],
'route_name' => 'entity.node.canonical',
'route_parameters' => [
'node' => $node5
->id(),
],
'url' => '',
'langcode' => 'en',
], $item6
->getPluginId());
$this
->modifyMenuLink($item1);
$this
->modifyMenuLink($item2);
$this
->toggleMenuLink($item1);
$this
->toggleMenuLink($item2);
$this
->moveMenuLink($item2, $item5
->getPluginId(), $menu_name);
$this
->assertMenuLink([
'children' => [],
'parents' => [
$item1
->getPluginId(),
],
'langcode' => 'en',
], $item1
->getPluginId());
$this
->assertMenuLink([
'children' => [
$item5
->getPluginId(),
$item6
->getPluginId(),
$item2
->getPluginId(),
$item3
->getPluginId(),
],
'parents' => [
$item4
->getPluginId(),
],
'langcode' => 'en',
], $item4
->getPluginId());
$this
->assertMenuLink([
'children' => [
$item2
->getPluginId(),
$item3
->getPluginId(),
],
'parents' => [
$item5
->getPluginId(),
$item4
->getPluginId(),
],
'langcode' => 'en',
], $item5
->getPluginId());
$this
->assertMenuLink([
'children' => [
$item3
->getPluginId(),
],
'parents' => [
$item2
->getPluginId(),
$item5
->getPluginId(),
$item4
->getPluginId(),
],
'langcode' => 'en',
], $item2
->getPluginId());
$this
->assertMenuLink([
'children' => [],
'parents' => [
$item3
->getPluginId(),
$item2
->getPluginId(),
$item5
->getPluginId(),
$item4
->getPluginId(),
],
'langcode' => 'en',
], $item3
->getPluginId());
$items = [];
for ($i = -50; $i <= 51; $i++) {
$items[$i] = $this
->addMenuLink('', '/node/' . $node1
->id(), $menu_name, TRUE, strval($i));
}
$this
->assertMenuLink([
'weight' => '51',
], $items[51]
->getPluginId());
$this
->disableMenuLink($item1);
$edit = [];
$edit['links[menu_plugin_id:' . $item1
->getPluginId() . '][enabled]'] = TRUE;
$this
->drupalPostForm('admin/structure/menu/manage/' . $item1
->getMenuName(), $edit, t('Save'));
$item2->expanded->value = 1;
$item2
->save();
$item4->expanded->value = 1;
$item4
->save();
$item5->expanded->value = 1;
$item5
->save();
$this
->assertMenuLink([
'enabled' => 1,
], $item1
->getPluginId());
$item7 = $this
->addMenuLink('', 'https://www.drupal.org', $menu_name);
$this
->assertMenuLink([
'url' => 'https://www.drupal.org',
], $item7
->getPluginId());
$item8 = $this
->addMenuLink('', '/', $menu_name);
$this
->assertMenuLink([
'route_name' => '<front>',
], $item8
->getPluginId());
$this
->drupalGet('');
$this
->assertSession()
->statusCodeEquals(200);
$this
->clickLink($item8
->getTitle());
$this
->assertSession()
->statusCodeEquals(200);
$this
->checkInvalidParentMenuLinks();
$this->items[] = $item1;
$this->items[] = $item2;
}
protected function doMenuLinkFormDefaultsTest() {
$this
->drupalGet("admin/structure/menu/manage/tools/add");
$this
->assertSession()
->statusCodeEquals(200);
$this
->assertFieldByName('title[0][value]', '');
$this
->assertFieldByName('link[0][uri]', '');
$this
->assertNoFieldChecked('edit-expanded-value');
$this
->assertFieldChecked('edit-enabled-value');
$this
->assertFieldByName('description[0][value]', '');
$this
->assertFieldByName('weight[0][value]', 0);
}
public function testMenuQueryAndFragment() {
$this
->drupalLogin($this->adminUser);
$path = '/test-page?arg1=value1&arg2=value2';
$item = $this
->addMenuLink('', $path);
$this
->drupalGet('admin/structure/menu/item/' . $item
->id() . '/edit');
$this
->assertFieldByName('link[0][uri]', $path, 'Path is found with both query and fragment.');
$path = '/test-page';
$this
->drupalPostForm('admin/structure/menu/item/' . $item
->id() . '/edit', [
'link[0][uri]' => $path,
], t('Save'));
$this
->drupalGet('admin/structure/menu/item/' . $item
->id() . '/edit');
$this
->assertFieldByName('link[0][uri]', $path, 'Path no longer has query or fragment.');
$path = '<front>?arg1=value#fragment';
$item = $this
->addMenuLink('', $path);
$this
->drupalGet('admin/structure/menu/item/' . $item
->id() . '/edit');
$this
->assertFieldByName('link[0][uri]', $path, 'Path is found with both query and fragment.');
$this
->drupalPostForm('admin/structure/menu/item/' . $item
->id() . '/edit', [], t('Save'));
$this
->drupalGet('admin/structure/menu/item/' . $item
->id() . '/edit');
$this
->assertFieldByName('link[0][uri]', $path, 'Path is found with both query and fragment.');
}
public function testSystemMenuRename() {
$this
->drupalLogin($this->adminUser);
$edit = [
'label' => $this
->randomMachineName(16),
];
$this
->drupalPostForm('admin/structure/menu/manage/main', $edit, t('Save'));
$default_theme = $this
->config('system.theme')
->get('default');
$this
->drupalget('admin/structure/block/list/' . $default_theme);
$this
->clickLink('Place block');
$this
->assertText($edit['label']);
}
public function testUnpublishedNodeMenuItem() {
$this
->drupalLogin($this
->drupalCreateUser([
'access administration pages',
'administer blocks',
'administer menu',
'create article content',
'bypass node access',
]));
$node = $this
->drupalCreateNode([
'type' => 'article',
'status' => NodeInterface::NOT_PUBLISHED,
]);
$item = $this
->addMenuLink('', '/node/' . $node
->id());
$this
->modifyMenuLink($item);
$this
->drupalLogout();
$this
->drupalLogin($this->adminUser);
$this
->drupalGet('admin/structure/menu/manage/' . $item
->getMenuName());
$this
->assertNoText($item
->getTitle(), "Menu link pointing to unpublished node is only visible to users with 'bypass node access' permission");
$this
->assertSession()
->responseHeaderContains('X-Drupal-Cache-Contexts', 'user.permissions');
}
public function addMenuLink($parent = '', $path = '/', $menu_name = 'tools', $expanded = FALSE, $weight = '0') {
$this
->drupalGet("admin/structure/menu/manage/{$menu_name}/add");
$this
->assertSession()
->statusCodeEquals(200);
$title = '!link_' . $this
->randomMachineName(16);
$edit = [
'link[0][uri]' => $path,
'title[0][value]' => $title,
'description[0][value]' => '',
'enabled[value]' => 1,
'expanded[value]' => $expanded,
'menu_parent' => $menu_name . ':' . $parent,
'weight[0][value]' => $weight,
];
$this
->drupalPostForm(NULL, $edit, t('Save'));
$this
->assertSession()
->statusCodeEquals(200);
$this
->assertText('The menu link has been saved.');
$menu_links = \Drupal::entityTypeManager()
->getStorage('menu_link_content')
->loadByProperties([
'title' => $title,
]);
$menu_link = reset($menu_links);
$this
->assertInstanceOf(MenuLinkContent::class, $menu_link);
$this
->assertMenuLink([
'menu_name' => $menu_name,
'children' => [],
'parent' => $parent,
], $menu_link
->getPluginId());
return $menu_link;
}
public function addInvalidMenuLink() {
foreach ([
'access' => '/admin/people/permissions',
] as $type => $link_path) {
$edit = [
'link[0][uri]' => $link_path,
'title[0][value]' => 'title',
];
$this
->drupalPostForm("admin/structure/menu/manage/{$this->menu->id()}/add", $edit, t('Save'));
$this
->assertRaw(t("The path '@link_path' is inaccessible.", [
'@link_path' => $link_path,
]), 'Menu link was not created');
}
}
public function checkInvalidParentMenuLinks() {
$last_link = NULL;
$created_links = [];
$menu_link_tree = \Drupal::service('menu.link_tree');
$max_depth = $menu_link_tree
->maxDepth();
for ($i = 0; $i <= $max_depth - 1; $i++) {
$parent = $last_link ? 'tools:' . $last_link
->getPluginId() : 'tools:';
$title = 'title' . $i;
$edit = [
'link[0][uri]' => '/',
'title[0][value]' => $title,
'menu_parent' => $parent,
'description[0][value]' => '',
'enabled[value]' => 1,
'expanded[value]' => FALSE,
'weight[0][value]' => '0',
];
$this
->drupalPostForm("admin/structure/menu/manage/{$this->menu->id()}/add", $edit, t('Save'));
$menu_links = \Drupal::entityTypeManager()
->getStorage('menu_link_content')
->loadByProperties([
'title' => $title,
]);
$last_link = reset($menu_links);
$created_links[] = 'tools:' . $last_link
->getPluginId();
}
$this
->drupalGet('admin/structure/menu/manage/admin/add');
$value = 'tools:' . $last_link
->getPluginId();
$this
->assertNoOption('edit-menu-parent', $value, 'The invalid option is not there.');
array_pop($created_links);
foreach ($created_links as $key => $link) {
$this
->assertOption('edit-menu-parent', $link, 'The valid option number ' . ($key + 1) . ' is there.');
}
}
public function verifyMenuLink(MenuLinkContent $item, $item_node, MenuLinkContent $parent = NULL, $parent_node = NULL) {
$this
->drupalGet('');
$this
->assertSession()
->statusCodeEquals(200);
if (isset($parent)) {
$title = $parent
->getTitle();
$this
->assertSession()
->linkExists($title, 0, 'Parent menu link was displayed');
$this
->clickLink($title);
$title = $parent_node
->label();
$this
->assertTitle("{$title} | Drupal");
}
$title = $item
->getTitle();
$this
->assertSession()
->linkExists($title, 0, 'Menu link was displayed');
$this
->clickLink($title);
$title = $item_node
->label();
$this
->assertTitle("{$title} | Drupal");
}
public function moveMenuLink(MenuLinkContent $item, $parent, $menu_name) {
$mlid = $item
->id();
$edit = [
'menu_parent' => $menu_name . ':' . $parent,
];
$this
->drupalPostForm("admin/structure/menu/item/{$mlid}/edit", $edit, t('Save'));
$this
->assertSession()
->statusCodeEquals(200);
}
public function modifyMenuLink(MenuLinkContent $item) {
$item->title->value = $this
->randomMachineName(16);
$mlid = $item
->id();
$title = $item
->getTitle();
$edit = [];
$edit['title[0][value]'] = $title;
$this
->drupalPostForm("admin/structure/menu/item/{$mlid}/edit", $edit, t('Save'));
$this
->assertSession()
->statusCodeEquals(200);
$this
->assertText('The menu link has been saved.');
$this
->drupalGet('admin/structure/menu/manage/' . $item
->getMenuName());
$this
->assertText($title, 'Menu link was edited');
}
public function resetMenuLink(MenuLinkInterface $menu_link, $old_weight) {
$this
->drupalPostForm("admin/structure/menu/link/{$menu_link->getPluginId()}/reset", [], t('Reset'));
$this
->assertSession()
->statusCodeEquals(200);
$this
->assertRaw(t('The menu link was reset to its default settings.'), 'Menu link was reset');
$instance = \Drupal::service('plugin.manager.menu.link')
->createInstance($menu_link
->getPluginId());
$this
->assertEqual($old_weight, $instance
->getWeight(), 'Resets to the old weight.');
}
public function deleteMenuLink(MenuLinkContent $item) {
$mlid = $item
->id();
$title = $item
->getTitle();
$this
->drupalPostForm("admin/structure/menu/item/{$mlid}/delete", [], t('Delete'));
$this
->assertSession()
->statusCodeEquals(200);
$this
->assertRaw(t('The menu link %title has been deleted.', [
'%title' => $title,
]), 'Menu link was deleted');
$this
->drupalGet('');
$this
->assertNoText($title, 'Menu link was deleted');
}
public function toggleMenuLink(MenuLinkContent $item) {
$this
->disableMenuLink($item);
$this
->drupalGet('');
$this
->assertNoText($item
->getTitle(), 'Menu link was not displayed');
$this
->enableMenuLink($item);
$this
->drupalGet('');
$this
->assertText($item
->getTitle(), 'Menu link was displayed');
}
public function disableMenuLink(MenuLinkContent $item) {
$mlid = $item
->id();
$edit['enabled[value]'] = FALSE;
$this
->drupalPostForm("admin/structure/menu/item/{$mlid}/edit", $edit, t('Save'));
$this
->assertMenuLink([
'enabled' => 0,
], $item
->getPluginId());
}
public function enableMenuLink(MenuLinkContent $item) {
$mlid = $item
->id();
$edit['enabled[value]'] = TRUE;
$this
->drupalPostForm("admin/structure/menu/item/{$mlid}/edit", $edit, t('Save'));
$this
->assertMenuLink([
'enabled' => 1,
], $item
->getPluginId());
}
public function testMenuParentsJsAccess() {
$this
->drupalLogin($this
->drupalCreateUser([
'administer menu',
]));
$this
->drupalGet('admin/structure/menu/parents', [
'query' => [
MainContentViewSubscriber::WRAPPER_FORMAT => 'drupal_ajax',
],
], [
'X-Requested-With: XMLHttpRequest',
]);
$this
->assertSession()
->statusCodeEquals(200);
$this
->drupalLogin($this
->drupalCreateUser());
$this
->drupalGet('admin/structure/menu/parents', [
'query' => [
MainContentViewSubscriber::WRAPPER_FORMAT => 'drupal_ajax',
],
], [
'X-Requested-With: XMLHttpRequest',
]);
$this
->assertSession()
->statusCodeEquals(403);
}
public function testExpandAllItems() {
$this
->drupalLogin($this->adminUser);
$menu = $this
->addCustomMenu();
$node = $this
->drupalCreateNode([
'type' => 'article',
]);
$parent = $this
->addMenuLink('', $node
->toUrl()
->toString(), $menu
->id(), FALSE);
$child_1 = $this
->addMenuLink($parent
->getPluginId(), $node
->toUrl()
->toString(), $menu
->id(), FALSE);
$child_2 = $this
->addMenuLink($parent
->getPluginId(), $node
->toUrl()
->toString(), $menu
->id(), FALSE);
$this
->drupalGet('<front>');
$this
->assertSession()
->linkExists($parent
->getTitle());
$this
->assertSession()
->linkNotExists($child_1
->getTitle());
$this
->assertSession()
->linkNotExists($child_2
->getTitle());
$block_id = $this->blockPlacements[$menu
->id()];
$this
->drupalGet('admin/structure/block/manage/' . $block_id);
$this
->assertSession()
->checkboxNotChecked('settings[expand_all_items]');
$this
->drupalPostForm(NULL, [
'settings[depth]' => 2,
'settings[level]' => 1,
'settings[expand_all_items]' => 1,
], t('Save block'));
$this
->drupalGet('admin/structure/block/manage/' . $block_id);
$this
->assertSession()
->checkboxChecked('settings[expand_all_items]');
$this
->drupalGet('<front>');
$this
->assertSession()
->linkExists($parent
->getTitle());
$this
->assertSession()
->linkExists($child_1
->getTitle());
$this
->assertSession()
->linkExists($child_2
->getTitle());
}
private function getStandardMenuLink() {
$menu_link_manager = \Drupal::service('plugin.manager.menu.link');
$instance = $menu_link_manager
->getInstance([
'id' => 'user.logout',
]);
$this
->assertTrue((bool) $instance, 'Standard menu link was loaded');
return $instance;
}
private function verifyAccess($response = 200) {
$this
->drupalGet('admin/help/menu');
$this
->assertSession()
->statusCodeEquals($response);
if ($response == 200) {
$this
->assertText(t('Menu'), 'Menu help was displayed');
}
$this
->drupalGet('admin/structure/menu');
$this
->assertSession()
->statusCodeEquals($response);
if ($response == 200) {
$this
->assertText(t('Menus'), 'Menu build overview page was displayed');
}
$this
->drupalGet('admin/structure/menu/manage/' . $this->menu
->id());
$this
->assertSession()
->statusCodeEquals($response);
if ($response == 200) {
$this
->assertText(t('Tools'), 'Tools menu page was displayed');
}
$item = $this
->getStandardMenuLink();
$this
->drupalGet('admin/structure/menu/link/' . $item
->getPluginId() . '/edit');
$this
->assertSession()
->statusCodeEquals($response);
if ($response == 200) {
$this
->assertText(t('Edit menu item'), 'Menu edit page was displayed');
}
$this
->drupalGet('admin/structure/menu/add');
$this
->assertSession()
->statusCodeEquals($response);
if ($response == 200) {
$this
->assertText(t('Menus'), 'Add menu page was displayed');
}
}
protected function doTestMenuBlock() {
$menu_id = $this->menu
->id();
$block_id = $this->blockPlacements[$menu_id];
$this
->drupalGet('admin/structure/block/manage/' . $block_id);
$this
->drupalPostForm(NULL, [
'settings[depth]' => 3,
'settings[level]' => 2,
], t('Save block'));
$block = Block::load($block_id);
$settings = $block
->getPlugin()
->getConfiguration();
$this
->assertEqual($settings['depth'], 3);
$this
->assertEqual($settings['level'], 2);
$block
->getPlugin()
->setConfigurationValue('depth', 0);
$block
->getPlugin()
->setConfigurationValue('level', 1);
$block
->save();
}
public function testMenuUiWithPendingRevisions() {
$this
->drupalLogin($this->adminUser);
$assert_session = $this
->assertSession();
$menu_1 = $this
->addCustomMenu();
$root_1 = $this
->addMenuLink('', '/', $menu_1
->id());
$this
->addMenuLink($root_1
->getPluginId(), '/', $menu_1
->id());
$menu_2 = $this
->addCustomMenu();
$root_2 = $this
->addMenuLink('', '/', $menu_2
->id());
$child_2 = $this
->addMenuLink($root_2
->getPluginId(), '/', $menu_2
->id());
$this
->drupalGet('admin/structure/menu/manage/' . $menu_2
->id());
$assert_session
->pageTextNotContains($menu_2
->label() . ' contains 1 menu link with pending revisions. Manipulation of a menu tree having links with pending revisions is not supported, but you can re-enable manipulation by getting each menu link to a published state.');
$this
->drupalGet('admin/structure/menu/manage/' . $menu_1
->id());
$assert_session
->pageTextNotContains($menu_1
->label() . ' contains 1 menu link with pending revisions. Manipulation of a menu tree having links with pending revisions is not supported, but you can re-enable manipulation by getting each menu link to a published state.');
\Drupal::entityTypeManager()
->getStorage('menu_link_content')
->createRevision($child_2, FALSE)
->save();
$this
->drupalGet('admin/structure/menu/manage/' . $menu_2
->id());
$assert_session
->pageTextContains($menu_2
->label() . ' contains 1 menu link with pending revisions. Manipulation of a menu tree having links with pending revisions is not supported, but you can re-enable manipulation by getting each menu link to a published state.');
$this
->assertNotEmpty($this
->cssSelect('input[name="links[menu_plugin_id:' . $root_2
->getPluginId() . '][enabled]"]'), 'The publishing status of a default revision can be changed.');
$this
->assertEmpty($this
->cssSelect('input[name="links[menu_plugin_id:' . $child_2
->getPluginId() . '][enabled]"]'), 'The publishing status of a pending revision can not be changed.');
$this
->drupalGet('admin/structure/menu/manage/' . $menu_1
->id());
$assert_session
->pageTextNotContains($menu_1
->label() . ' contains 1 menu link with pending revisions. Manipulation of a menu tree having links with pending revisions is not supported, but you can re-enable manipulation by getting each menu link to a published state.');
$this
->drupalPostForm('admin/structure/menu/manage/' . $menu_2
->id(), [], 'Save');
$this
->assertSession()
->elementNotExists('xpath', '//div[contains(@class, "messages--error")]');
}
}