3 namespace Drupal\menu_ui\Tests;
5 use Drupal\block\Entity\Block;
6 use Drupal\Component\Serialization\Json;
7 use Drupal\Component\Utility\Unicode;
8 use Drupal\Core\Menu\MenuLinkInterface;
10 use Drupal\menu_link_content\Entity\MenuLinkContent;
11 use Drupal\system\Entity\Menu;
12 use Drupal\node\Entity\Node;
13 use Drupal\node\NodeInterface;
16 * Add a custom menu, add menu links to the custom menu and Tools menu, check
17 * their data, and delete them using the UI.
21 class MenuTest extends MenuWebTestBase {
28 public static $modules = ['node', 'block', 'contextual', 'help', 'path', 'test_page_test'];
31 * A user with administration rights.
33 * @var \Drupal\user\UserInterface
38 * An authenticated user.
40 * @var \Drupal\user\UserInterface
42 protected $authenticatedUser;
45 * Array of placed menu blocks keyed by block ID.
49 protected $blockPlacements;
54 * @var \Drupal\system\Entity\Menu
59 * An array of test menu links.
61 * @var \Drupal\menu_link_content\MenuLinkContentInterface[]
65 protected function setUp() {
68 $this->drupalPlaceBlock('page_title_block');
70 $this->drupalCreateContentType(['type' => 'article', 'name' => 'Article']);
73 $this->adminUser = $this->drupalCreateUser(['access administration pages', 'administer blocks', 'administer menu', 'create article content']);
74 $this->authenticatedUser = $this->drupalCreateUser([]);
78 * Tests menu functionality using the admin and user interfaces.
80 public function testMenu() {
82 $this->drupalLogin($this->adminUser);
85 $this->menu = $this->addCustomMenu();
87 $this->doTestMenuBlock();
88 $this->addInvalidMenuLink();
89 $this->addCustomMenuCRUD();
91 // Verify that the menu links rebuild is idempotent and leaves the same
92 // number of links in the table.
93 /** @var \Drupal\Core\Menu\MenuLinkManagerInterface $menu_link_manager */
94 $menu_link_manager = \Drupal::service('plugin.manager.menu.link');
95 $before_count = $menu_link_manager->countMenuLinks(NULL);
96 $menu_link_manager->rebuild();
97 $after_count = $menu_link_manager->countMenuLinks(NULL);
98 $this->assertIdentical($before_count, $after_count, 'MenuLinkManager::rebuild() does not add more links');
99 // Do standard user tests.
101 $this->drupalLogin($this->authenticatedUser);
102 $this->verifyAccess(403);
104 foreach ($this->items as $item) {
105 // Menu link URIs are stored as 'internal:/node/$nid'.
106 $node = Node::load(str_replace('internal:/node/', '', $item->link->uri));
107 $this->verifyMenuLink($item, $node);
110 // Log in the administrator.
111 $this->drupalLogin($this->adminUser);
113 // Verify delete link exists and reset link does not exist.
114 $this->drupalGet('admin/structure/menu/manage/' . $this->menu->id());
115 $this->assertLinkByHref(Url::fromRoute('entity.menu_link_content.delete_form', ['menu_link_content' => $this->items[0]->id()])->toString());
116 $this->assertNoLinkByHref(Url::fromRoute('menu_ui.link_reset', ['menu_link_plugin' => $this->items[0]->getPluginId()])->toString());
117 // Check delete and reset access.
118 $this->drupalGet('admin/structure/menu/item/' . $this->items[0]->id() . '/delete');
119 $this->assertResponse(200);
120 $this->drupalGet('admin/structure/menu/link/' . $this->items[0]->getPluginId() . '/reset');
121 $this->assertResponse(403);
123 // Delete menu links.
124 foreach ($this->items as $item) {
125 $this->deleteMenuLink($item);
128 // Delete custom menu.
129 $this->deleteCustomMenu();
131 // Modify and reset a standard menu link.
132 $instance = $this->getStandardMenuLink();
133 $old_weight = $instance->getWeight();
134 // Edit the static menu link.
136 $edit['weight'] = 10;
137 $id = $instance->getPluginId();
138 $this->drupalPostForm("admin/structure/menu/link/$id/edit", $edit, t('Save'));
139 $this->assertResponse(200);
140 $this->assertText('The menu link has been saved.');
141 $menu_link_manager->resetDefinitions();
143 $instance = $menu_link_manager->createInstance($instance->getPluginId());
144 $this->assertEqual($edit['weight'], $instance->getWeight(), 'Saving an existing link updates the weight.');
145 $this->resetMenuLink($instance, $old_weight);
149 * Adds a custom menu using CRUD functions.
151 public function addCustomMenuCRUD() {
152 // Add a new custom menu.
153 $menu_name = substr(hash('sha256', $this->randomMachineName(16)), 0, MENU_MAX_MENU_NAME_LENGTH_UI);
154 $label = $this->randomMachineName(16);
156 $menu = Menu::create([
159 'description' => 'Description text',
163 // Assert the new menu.
164 $this->drupalGet('admin/structure/menu/manage/' . $menu_name);
165 $this->assertRaw($label, 'Custom menu was added.');
168 $new_label = $this->randomMachineName(16);
169 $menu->set('label', $new_label);
171 $this->drupalGet('admin/structure/menu/manage/' . $menu_name);
172 $this->assertRaw($new_label, 'Custom menu was edited.');
176 * Creates a custom menu.
178 * @return \Drupal\system\Entity\Menu
179 * The custom menu that has been created.
181 public function addCustomMenu() {
182 // Try adding a menu using a menu_name that is too long.
183 $this->drupalGet('admin/structure/menu/add');
184 $menu_name = substr(hash('sha256', $this->randomMachineName(16)), 0, MENU_MAX_MENU_NAME_LENGTH_UI + 1);
185 $label = $this->randomMachineName(16);
191 $this->drupalPostForm('admin/structure/menu/add', $edit, t('Save'));
193 // Verify that using a menu_name that is too long results in a validation
195 $this->assertRaw(t('@name cannot be longer than %max characters but is currently %length characters long.', [
196 '@name' => t('Menu name'),
197 '%max' => MENU_MAX_MENU_NAME_LENGTH_UI,
198 '%length' => Unicode::strlen($menu_name),
201 // Change the menu_name so it no longer exceeds the maximum length.
202 $menu_name = substr(hash('sha256', $this->randomMachineName(16)), 0, MENU_MAX_MENU_NAME_LENGTH_UI);
203 $edit['id'] = $menu_name;
204 $this->drupalPostForm('admin/structure/menu/add', $edit, t('Save'));
206 // Verify that no validation error is given for menu_name length.
207 $this->assertNoRaw(t('@name cannot be longer than %max characters but is currently %length characters long.', [
208 '@name' => t('Menu name'),
209 '%max' => MENU_MAX_MENU_NAME_LENGTH_UI,
210 '%length' => Unicode::strlen($menu_name),
212 // Verify that the confirmation message is displayed.
213 $this->assertRaw(t('Menu %label has been added.', ['%label' => $label]));
214 $this->drupalGet('admin/structure/menu');
215 $this->assertText($label, 'Menu created');
217 // Confirm that the custom menu block is available.
218 $this->drupalGet('admin/structure/block/list/' . $this->config('system.theme')->get('default'));
219 $this->clickLinkPartialName('Place block');
220 $this->assertText($label);
223 $block = $this->drupalPlaceBlock('system_menu_block:' . $menu_name);
224 $this->blockPlacements[$menu_name] = $block->id();
225 return Menu::load($menu_name);
229 * Deletes the locally stored custom menu.
231 * This deletes the custom menu that is stored in $this->menu and performs
232 * tests on the menu delete user interface.
234 public function deleteCustomMenu() {
235 $menu_name = $this->menu->id();
236 $label = $this->menu->label();
238 // Delete custom menu.
239 $this->drupalPostForm("admin/structure/menu/manage/$menu_name/delete", [], t('Delete'));
240 $this->assertResponse(200);
241 $this->assertRaw(t('The menu %title has been deleted.', ['%title' => $label]), 'Custom menu was deleted');
242 $this->assertNull(Menu::load($menu_name), 'Custom menu was deleted');
243 // Test if all menu links associated with the menu were removed from
245 $result = entity_load_multiple_by_properties('menu_link_content', ['menu_name' => $menu_name]);
246 $this->assertFalse($result, 'All menu links associated with the custom menu were deleted.');
248 // Make sure there's no delete button on system menus.
249 $this->drupalGet('admin/structure/menu/manage/main');
250 $this->assertNoRaw('edit-delete', 'The delete button was not found');
252 // Try to delete the main menu.
253 $this->drupalGet('admin/structure/menu/manage/main/delete');
254 $this->assertText(t('You are not authorized to access this page.'));
258 * Tests menu functionality.
260 public function doMenuTests() {
261 $menu_name = $this->menu->id();
263 // Test the 'Add link' local action.
264 $this->drupalGet(Url::fromRoute('entity.menu.edit_form', ['menu' => $menu_name]));
266 $this->clickLink(t('Add link'));
267 $link_title = $this->randomString();
268 $this->drupalPostForm(NULL, ['link[0][uri]' => '/', 'title[0][value]' => $link_title], t('Save'));
269 $this->assertUrl(Url::fromRoute('entity.menu.edit_form', ['menu' => $menu_name]));
270 // Test the 'Edit' operation.
271 $this->clickLink(t('Edit'));
272 $this->assertFieldByName('title[0][value]', $link_title);
273 $link_title = $this->randomString();
274 $this->drupalPostForm(NULL, ['title[0][value]' => $link_title], t('Save'));
275 $this->assertUrl(Url::fromRoute('entity.menu.edit_form', ['menu' => $menu_name]));
276 // Test the 'Delete' operation.
277 $this->clickLink(t('Delete'));
278 $this->assertRaw(t('Are you sure you want to delete the custom menu link %item?', ['%item' => $link_title]));
279 $this->drupalPostForm(NULL, [], t('Delete'));
280 $this->assertUrl(Url::fromRoute('entity.menu.edit_form', ['menu' => $menu_name]));
282 // Add nodes to use as links for menu links.
283 $node1 = $this->drupalCreateNode(['type' => 'article']);
284 $node2 = $this->drupalCreateNode(['type' => 'article']);
285 $node3 = $this->drupalCreateNode(['type' => 'article']);
286 $node4 = $this->drupalCreateNode(['type' => 'article']);
287 // Create a node with an alias.
288 $node5 = $this->drupalCreateNode([
295 // Verify add link button.
296 $this->drupalGet('admin/structure/menu');
297 $this->assertLinkByHref('admin/structure/menu/manage/' . $menu_name . '/add', 0, "The add menu link button URL is correct");
299 // Verify form defaults.
300 $this->doMenuLinkFormDefaultsTest();
303 $item1 = $this->addMenuLink('', '/node/' . $node1->id(), $menu_name, TRUE);
304 $item2 = $this->addMenuLink($item1->getPluginId(), '/node/' . $node2->id(), $menu_name, FALSE);
305 $item3 = $this->addMenuLink($item2->getPluginId(), '/node/' . $node3->id(), $menu_name);
313 $this->assertMenuLink($item1->getPluginId(), [
314 'children' => [$item2->getPluginId(), $item3->getPluginId()],
315 'parents' => [$item1->getPluginId()],
316 // We assert the language code here to make sure that the language
317 // selection element degrades gracefully without the Language module.
320 $this->assertMenuLink($item2->getPluginId(), [
321 'children' => [$item3->getPluginId()],
322 'parents' => [$item2->getPluginId(), $item1->getPluginId()],
326 $this->assertMenuLink($item3->getPluginId(), [
328 'parents' => [$item3->getPluginId(), $item2->getPluginId(), $item1->getPluginId()],
333 // Verify menu links.
334 $this->verifyMenuLink($item1, $node1);
335 $this->verifyMenuLink($item2, $node2, $item1, $node1);
336 $this->verifyMenuLink($item3, $node3, $item2, $node2);
338 // Add more menu links.
339 $item4 = $this->addMenuLink('', '/node/' . $node4->id(), $menu_name);
340 $item5 = $this->addMenuLink($item4->getPluginId(), '/node/' . $node5->id(), $menu_name);
341 // Create a menu link pointing to an alias.
342 $item6 = $this->addMenuLink($item4->getPluginId(), '/node5', $menu_name, TRUE, '0');
353 $this->assertMenuLink($item4->getPluginId(), [
354 'children' => [$item5->getPluginId(), $item6->getPluginId()],
355 'parents' => [$item4->getPluginId()],
359 $this->assertMenuLink($item5->getPluginId(), [
361 'parents' => [$item5->getPluginId(), $item4->getPluginId()],
364 $this->assertMenuLink($item6->getPluginId(), [
366 'parents' => [$item6->getPluginId(), $item4->getPluginId()],
367 'route_name' => 'entity.node.canonical',
368 'route_parameters' => ['node' => $node5->id()],
374 // Modify menu links.
375 $this->modifyMenuLink($item1);
376 $this->modifyMenuLink($item2);
378 // Toggle menu links.
379 $this->toggleMenuLink($item1);
380 $this->toggleMenuLink($item2);
382 // Move link and verify that descendants are updated.
383 $this->moveMenuLink($item2, $item5->getPluginId(), $menu_name);
393 $this->assertMenuLink($item1->getPluginId(), [
395 'parents' => [$item1->getPluginId()],
399 $this->assertMenuLink($item4->getPluginId(), [
400 'children' => [$item5->getPluginId(), $item6->getPluginId(), $item2->getPluginId(), $item3->getPluginId()],
401 'parents' => [$item4->getPluginId()],
406 $this->assertMenuLink($item5->getPluginId(), [
407 'children' => [$item2->getPluginId(), $item3->getPluginId()],
408 'parents' => [$item5->getPluginId(), $item4->getPluginId()],
412 $this->assertMenuLink($item2->getPluginId(), [
413 'children' => [$item3->getPluginId()],
414 'parents' => [$item2->getPluginId(), $item5->getPluginId(), $item4->getPluginId()],
418 $this->assertMenuLink($item3->getPluginId(), [
420 'parents' => [$item3->getPluginId(), $item2->getPluginId(), $item5->getPluginId(), $item4->getPluginId()],
425 // Add 102 menu links with increasing weights, then make sure the last-added
426 // item's weight doesn't get changed because of the old hardcoded delta=50.
428 for ($i = -50; $i <= 51; $i++) {
429 $items[$i] = $this->addMenuLink('', '/node/' . $node1->id(), $menu_name, TRUE, strval($i));
431 $this->assertMenuLink($items[51]->getPluginId(), ['weight' => '51']);
433 // Disable a link and then re-enable the link via the overview form.
434 $this->disableMenuLink($item1);
436 $edit['links[menu_plugin_id:' . $item1->getPluginId() . '][enabled]'] = TRUE;
437 $this->drupalPostForm('admin/structure/menu/manage/' . $item1->getMenuName(), $edit, t('Save'));
439 // Mark item2, item4 and item5 as expanded.
440 // This is done in order to show them on the frontpage.
441 $item2->expanded->value = 1;
443 $item4->expanded->value = 1;
445 $item5->expanded->value = 1;
448 // Verify in the database.
449 $this->assertMenuLink($item1->getPluginId(), ['enabled' => 1]);
451 // Add an external link.
452 $item7 = $this->addMenuLink('', 'https://www.drupal.org', $menu_name);
453 $this->assertMenuLink($item7->getPluginId(), ['url' => 'https://www.drupal.org']);
455 // Add <front> menu item.
456 $item8 = $this->addMenuLink('', '/', $menu_name);
457 $this->assertMenuLink($item8->getPluginId(), ['route_name' => '<front>']);
458 $this->drupalGet('');
459 $this->assertResponse(200);
460 // Make sure we get routed correctly.
461 $this->clickLink($item8->getTitle());
462 $this->assertResponse(200);
464 // Check invalid menu link parents.
465 $this->checkInvalidParentMenuLinks();
467 // Save menu links for later tests.
468 $this->items[] = $item1;
469 $this->items[] = $item2;
473 * Ensures that the proper default values are set when adding a menu link
475 protected function doMenuLinkFormDefaultsTest() {
476 $this->drupalGet("admin/structure/menu/manage/tools/add");
477 $this->assertResponse(200);
479 $this->assertFieldByName('title[0][value]', '');
480 $this->assertFieldByName('link[0][uri]', '');
482 $this->assertNoFieldChecked('edit-expanded-value');
483 $this->assertFieldChecked('edit-enabled-value');
485 $this->assertFieldByName('description[0][value]', '');
486 $this->assertFieldByName('weight[0][value]', 0);
490 * Adds and removes a menu link with a query string and fragment.
492 public function testMenuQueryAndFragment() {
493 $this->drupalLogin($this->adminUser);
495 // Make a path with query and fragment on.
496 $path = '/test-page?arg1=value1&arg2=value2';
497 $item = $this->addMenuLink('', $path);
499 $this->drupalGet('admin/structure/menu/item/' . $item->id() . '/edit');
500 $this->assertFieldByName('link[0][uri]', $path, 'Path is found with both query and fragment.');
502 // Now change the path to something without query and fragment.
503 $path = '/test-page';
504 $this->drupalPostForm('admin/structure/menu/item/' . $item->id() . '/edit', ['link[0][uri]' => $path], t('Save'));
505 $this->drupalGet('admin/structure/menu/item/' . $item->id() . '/edit');
506 $this->assertFieldByName('link[0][uri]', $path, 'Path no longer has query or fragment.');
508 // Use <front>#fragment and ensure that saving it does not lose its content.
509 $path = '<front>?arg1=value#fragment';
510 $item = $this->addMenuLink('', $path);
512 $this->drupalGet('admin/structure/menu/item/' . $item->id() . '/edit');
513 $this->assertFieldByName('link[0][uri]', $path, 'Path is found with both query and fragment.');
515 $this->drupalPostForm('admin/structure/menu/item/' . $item->id() . '/edit', [], t('Save'));
517 $this->drupalGet('admin/structure/menu/item/' . $item->id() . '/edit');
518 $this->assertFieldByName('link[0][uri]', $path, 'Path is found with both query and fragment.');
522 * Tests renaming the built-in menu.
524 public function testSystemMenuRename() {
525 $this->drupalLogin($this->adminUser);
527 'label' => $this->randomMachineName(16),
529 $this->drupalPostForm('admin/structure/menu/manage/main', $edit, t('Save'));
531 // Make sure menu shows up with new name in block addition.
532 $default_theme = $this->config('system.theme')->get('default');
533 $this->drupalget('admin/structure/block/list/' . $default_theme);
534 $this->clickLinkPartialName('Place block');
535 $this->assertText($edit['label']);
539 * Tests that menu items pointing to unpublished nodes are editable.
541 public function testUnpublishedNodeMenuItem() {
542 $this->drupalLogin($this->drupalCreateUser(['access administration pages', 'administer blocks', 'administer menu', 'create article content', 'bypass node access']));
543 // Create an unpublished node.
544 $node = $this->drupalCreateNode([
546 'status' => NodeInterface::NOT_PUBLISHED,
549 $item = $this->addMenuLink('', '/node/' . $node->id());
550 $this->modifyMenuLink($item);
552 // Test that a user with 'administer menu' but without 'bypass node access'
553 // cannot see the menu item.
554 $this->drupalLogout();
555 $this->drupalLogin($this->adminUser);
556 $this->drupalGet('admin/structure/menu/manage/' . $item->getMenuName());
557 $this->assertNoText($item->getTitle(), "Menu link pointing to unpublished node is only visible to users with 'bypass node access' permission");
558 // The cache contexts associated with the (in)accessible menu links are
559 // bubbled. See DefaultMenuLinkTreeManipulators::menuLinkCheckAccess().
560 $this->assertCacheContext('user.permissions');
564 * Tests the contextual links on a menu block.
566 public function testBlockContextualLinks() {
567 $this->drupalLogin($this->drupalCreateUser(['administer menu', 'access contextual links', 'administer blocks']));
568 $custom_menu = $this->addCustomMenu();
569 $this->addMenuLink('', '/', $custom_menu->id());
570 $block = $this->drupalPlaceBlock('system_menu_block:' . $custom_menu->id(), ['label' => 'Custom menu', 'provider' => 'system']);
571 $this->drupalGet('test-page');
573 $id = 'block:block=' . $block->id() . ':langcode=en|menu:menu=' . $custom_menu->id() . ':langcode=en';
574 // @see \Drupal\contextual\Tests\ContextualDynamicContextTest:assertContextualLinkPlaceHolder()
575 $this->assertRaw('<div data-contextual-id="' . $id . '"></div>', format_string('Contextual link placeholder with id @id exists.', ['@id' => $id]));
577 // Get server-rendered contextual links.
578 // @see \Drupal\contextual\Tests\ContextualDynamicContextTest:renderContextualLinks()
579 $post = ['ids[0]' => $id];
580 $response = $this->drupalPost('contextual/render', 'application/json', $post, ['query' => ['destination' => 'test-page']]);
581 $this->assertResponse(200);
582 $json = Json::decode($response);
583 $this->assertIdentical($json[$id], '<ul class="contextual-links"><li class="block-configure"><a href="' . base_path() . 'admin/structure/block/manage/' . $block->id() . '">Configure block</a></li><li class="entitymenuedit-form"><a href="' . base_path() . 'admin/structure/menu/manage/' . $custom_menu->id() . '">Edit menu</a></li></ul>');
587 * Adds a menu link using the UI.
589 * @param string $parent
590 * Optional parent menu link id.
591 * @param string $path
592 * The path to enter on the form. Defaults to the front page.
593 * @param string $menu_name
594 * Menu name. Defaults to 'tools'.
595 * @param bool $expanded
596 * Whether or not this menu link is expanded. Setting this to TRUE should
597 * test whether it works when we do the authenticatedUser tests. Defaults
599 * @param string $weight
600 * Menu weight. Defaults to 0.
602 * @return \Drupal\menu_link_content\Entity\MenuLinkContent
603 * A menu link entity.
605 public function addMenuLink($parent = '', $path = '/', $menu_name = 'tools', $expanded = FALSE, $weight = '0') {
606 // View add menu link page.
607 $this->drupalGet("admin/structure/menu/manage/$menu_name/add");
608 $this->assertResponse(200);
610 $title = '!link_' . $this->randomMachineName(16);
612 'link[0][uri]' => $path,
613 'title[0][value]' => $title,
614 'description[0][value]' => '',
615 'enabled[value]' => 1,
616 'expanded[value]' => $expanded,
617 'menu_parent' => $menu_name . ':' . $parent,
618 'weight[0][value]' => $weight,
622 $this->drupalPostForm(NULL, $edit, t('Save'));
623 $this->assertResponse(200);
624 $this->assertText('The menu link has been saved.');
626 $menu_links = entity_load_multiple_by_properties('menu_link_content', ['title' => $title]);
628 $menu_link = reset($menu_links);
629 $this->assertTrue($menu_link, 'Menu link was found in database.');
630 $this->assertMenuLink($menu_link->getPluginId(), ['menu_name' => $menu_name, 'children' => [], 'parent' => $parent]);
636 * Attempts to add menu link with invalid path or no access permission.
638 public function addInvalidMenuLink() {
639 foreach (['access' => '/admin/people/permissions'] as $type => $link_path) {
641 'link[0][uri]' => $link_path,
642 'title[0][value]' => 'title',
644 $this->drupalPostForm("admin/structure/menu/manage/{$this->menu->id()}/add", $edit, t('Save'));
645 $this->assertRaw(t("The path '@link_path' is inaccessible.", ['@link_path' => $link_path]), 'Menu link was not created');
650 * Tests that parent options are limited by depth when adding menu links.
652 public function checkInvalidParentMenuLinks() {
656 // Get the max depth of the tree.
657 $menu_link_tree = \Drupal::service('menu.link_tree');
658 $max_depth = $menu_link_tree->maxDepth();
660 // Create a maximum number of menu links, each a child of the previous.
661 for ($i = 0; $i <= $max_depth - 1; $i++) {
662 $parent = $last_link ? 'tools:' . $last_link->getPluginId() : 'tools:';
663 $title = 'title' . $i;
665 'link[0][uri]' => '/',
666 'title[0][value]' => $title,
667 'menu_parent' => $parent,
668 'description[0][value]' => '',
669 'enabled[value]' => 1,
670 'expanded[value]' => FALSE,
671 'weight[0][value]' => '0',
673 $this->drupalPostForm("admin/structure/menu/manage/{$this->menu->id()}/add", $edit, t('Save'));
674 $menu_links = entity_load_multiple_by_properties('menu_link_content', ['title' => $title]);
675 $last_link = reset($menu_links);
676 $created_links[] = 'tools:' . $last_link->getPluginId();
679 // The last link cannot be a parent in the new menu link form.
680 $this->drupalGet('admin/structure/menu/manage/admin/add');
681 $value = 'tools:' . $last_link->getPluginId();
682 $this->assertNoOption('edit-menu-parent', $value, 'The invalid option is not there.');
684 // All but the last link can be parents in the new menu link form.
685 array_pop($created_links);
686 foreach ($created_links as $key => $link) {
687 $this->assertOption('edit-menu-parent', $link, 'The valid option number ' . ($key + 1) . ' is there.');
692 * Verifies a menu link using the UI.
694 * @param \Drupal\menu_link_content\Entity\MenuLinkContent $item
696 * @param object $item_node
697 * Menu link content node.
698 * @param \Drupal\menu_link_content\Entity\MenuLinkContent $parent
700 * @param object $parent_node
701 * Parent menu link content node.
703 public function verifyMenuLink(MenuLinkContent $item, $item_node, MenuLinkContent $parent = NULL, $parent_node = NULL) {
705 $this->drupalGet('');
706 $this->assertResponse(200);
708 // Verify parent menu link.
709 if (isset($parent)) {
711 $title = $parent->getTitle();
712 $this->assertLink($title, 0, 'Parent menu link was displayed');
714 // Verify menu link link.
715 $this->clickLink($title);
716 $title = $parent_node->label();
717 $this->assertTitle(t("@title | Drupal", ['@title' => $title]), 'Parent menu link link target was correct');
721 $title = $item->getTitle();
722 $this->assertLink($title, 0, 'Menu link was displayed');
724 // Verify menu link link.
725 $this->clickLink($title);
726 $title = $item_node->label();
727 $this->assertTitle(t("@title | Drupal", ['@title' => $title]), 'Menu link link target was correct');
731 * Changes the parent of a menu link using the UI.
733 * @param \Drupal\menu_link_content\MenuLinkContentInterface $item
734 * The menu link item to move.
736 * The id of the new parent.
737 * @param string $menu_name
738 * The menu the menu link will be moved to.
740 public function moveMenuLink(MenuLinkContent $item, $parent, $menu_name) {
744 'menu_parent' => $menu_name . ':' . $parent,
746 $this->drupalPostForm("admin/structure/menu/item/$mlid/edit", $edit, t('Save'));
747 $this->assertResponse(200);
751 * Modifies a menu link using the UI.
753 * @param \Drupal\menu_link_content\Entity\MenuLinkContent $item
756 public function modifyMenuLink(MenuLinkContent $item) {
757 $item->title->value = $this->randomMachineName(16);
760 $title = $item->getTitle();
764 $edit['title[0][value]'] = $title;
765 $this->drupalPostForm("admin/structure/menu/item/$mlid/edit", $edit, t('Save'));
766 $this->assertResponse(200);
767 $this->assertText('The menu link has been saved.');
769 $this->drupalGet('admin/structure/menu/manage/' . $item->getMenuName());
770 $this->assertText($title, 'Menu link was edited');
774 * Resets a standard menu link using the UI.
776 * @param \Drupal\Core\Menu\MenuLinkInterface $menu_link
778 * @param int $old_weight
779 * Original title for menu link.
781 public function resetMenuLink(MenuLinkInterface $menu_link, $old_weight) {
783 $this->drupalPostForm("admin/structure/menu/link/{$menu_link->getPluginId()}/reset", [], t('Reset'));
784 $this->assertResponse(200);
785 $this->assertRaw(t('The menu link was reset to its default settings.'), 'Menu link was reset');
788 $instance = \Drupal::service('plugin.manager.menu.link')->createInstance($menu_link->getPluginId());
789 $this->assertEqual($old_weight, $instance->getWeight(), 'Resets to the old weight.');
793 * Deletes a menu link using the UI.
795 * @param \Drupal\menu_link_content\Entity\MenuLinkContent $item
798 public function deleteMenuLink(MenuLinkContent $item) {
800 $title = $item->getTitle();
803 $this->drupalPostForm("admin/structure/menu/item/$mlid/delete", [], t('Delete'));
804 $this->assertResponse(200);
805 $this->assertRaw(t('The menu link %title has been deleted.', ['%title' => $title]), 'Menu link was deleted');
808 $this->drupalGet('');
809 $this->assertNoText($title, 'Menu link was deleted');
813 * Alternately disables and enables a menu link.
815 * @param \Drupal\menu_link_content\Entity\MenuLinkContent $item
818 public function toggleMenuLink(MenuLinkContent $item) {
819 $this->disableMenuLink($item);
821 // Verify menu link is absent.
822 $this->drupalGet('');
823 $this->assertNoText($item->getTitle(), 'Menu link was not displayed');
824 $this->enableMenuLink($item);
826 // Verify menu link is displayed.
827 $this->drupalGet('');
828 $this->assertText($item->getTitle(), 'Menu link was displayed');
832 * Disables a menu link.
834 * @param \Drupal\menu_link_content\Entity\MenuLinkContent $item
837 public function disableMenuLink(MenuLinkContent $item) {
839 $edit['enabled[value]'] = FALSE;
840 $this->drupalPostForm("admin/structure/menu/item/$mlid/edit", $edit, t('Save'));
842 // Unlike most other modules, there is no confirmation message displayed.
843 // Verify in the database.
844 $this->assertMenuLink($item->getPluginId(), ['enabled' => 0]);
848 * Enables a menu link.
850 * @param \Drupal\menu_link_content\Entity\MenuLinkContent $item
853 public function enableMenuLink(MenuLinkContent $item) {
855 $edit['enabled[value]'] = TRUE;
856 $this->drupalPostForm("admin/structure/menu/item/$mlid/edit", $edit, t('Save'));
858 // Verify in the database.
859 $this->assertMenuLink($item->getPluginId(), ['enabled' => 1]);
863 * Tests if administrative users other than user 1 can access the menu parents
866 public function testMenuParentsJsAccess() {
867 $admin = $this->drupalCreateUser(['administer menu']);
868 $this->drupalLogin($admin);
869 // Just check access to the callback overall, the POST data is irrelevant.
870 $this->drupalGetAjax('admin/structure/menu/parents');
871 $this->assertResponse(200);
873 // Do standard user tests.
875 $this->drupalLogin($this->authenticatedUser);
876 $this->drupalGetAjax('admin/structure/menu/parents');
877 $this->assertResponse(403);
881 * Returns standard menu link.
883 * @return \Drupal\Core\Menu\MenuLinkInterface
884 * A menu link plugin.
886 private function getStandardMenuLink() {
887 // Retrieve menu link id of the Log out menu link, which will always be on
889 /** @var \Drupal\Core\Menu\MenuLinkManagerInterface $menu_link_manager */
890 $menu_link_manager = \Drupal::service('plugin.manager.menu.link');
891 $instance = $menu_link_manager->getInstance(['id' => 'user.logout']);
893 $this->assertTrue((bool) $instance, 'Standard menu link was loaded');
898 * Verifies the logged in user has the desired access to various menu pages.
900 * @param int $response
901 * (optional) The expected HTTP response code. Defaults to 200.
903 private function verifyAccess($response = 200) {
904 // View menu help page.
905 $this->drupalGet('admin/help/menu');
906 $this->assertResponse($response);
907 if ($response == 200) {
908 $this->assertText(t('Menu'), 'Menu help was displayed');
911 // View menu build overview page.
912 $this->drupalGet('admin/structure/menu');
913 $this->assertResponse($response);
914 if ($response == 200) {
915 $this->assertText(t('Menus'), 'Menu build overview page was displayed');
918 // View tools menu customization page.
919 $this->drupalGet('admin/structure/menu/manage/' . $this->menu->id());
920 $this->assertResponse($response);
921 if ($response == 200) {
922 $this->assertText(t('Tools'), 'Tools menu page was displayed');
925 // View menu edit page for a static link.
926 $item = $this->getStandardMenuLink();
927 $this->drupalGet('admin/structure/menu/link/' . $item->getPluginId() . '/edit');
928 $this->assertResponse($response);
929 if ($response == 200) {
930 $this->assertText(t('Edit menu item'), 'Menu edit page was displayed');
933 // View add menu page.
934 $this->drupalGet('admin/structure/menu/add');
935 $this->assertResponse($response);
936 if ($response == 200) {
937 $this->assertText(t('Menus'), 'Add menu page was displayed');
942 * Tests menu block settings.
944 protected function doTestMenuBlock() {
945 $menu_id = $this->menu->id();
946 $block_id = $this->blockPlacements[$menu_id];
947 $this->drupalGet('admin/structure/block/manage/' . $block_id);
948 $this->drupalPostForm(NULL, [
949 'settings[depth]' => 3,
950 'settings[level]' => 2,
952 $block = Block::load($block_id);
953 $settings = $block->getPlugin()->getConfiguration();
954 $this->assertEqual($settings['depth'], 3);
955 $this->assertEqual($settings['level'], 2);
957 $block->getPlugin()->setConfigurationValue('depth', 0);
958 $block->getPlugin()->setConfigurationValue('level', 1);