Chris@0: drupalPlaceBlock('page_title_block'); Chris@0: Chris@0: $this->drupalCreateContentType(['type' => 'article', 'name' => 'Article']); Chris@0: Chris@0: // Create users. Chris@0: $this->adminUser = $this->drupalCreateUser(['access administration pages', 'administer blocks', 'administer menu', 'create article content']); Chris@0: $this->authenticatedUser = $this->drupalCreateUser([]); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Tests menu functionality using the admin and user interfaces. Chris@0: */ Chris@0: public function testMenu() { Chris@0: // Log in the user. Chris@0: $this->drupalLogin($this->adminUser); Chris@0: $this->items = []; Chris@0: Chris@0: $this->menu = $this->addCustomMenu(); Chris@0: $this->doMenuTests(); Chris@0: $this->doTestMenuBlock(); Chris@0: $this->addInvalidMenuLink(); Chris@0: $this->addCustomMenuCRUD(); Chris@0: Chris@0: // Verify that the menu links rebuild is idempotent and leaves the same Chris@0: // number of links in the table. Chris@0: /** @var \Drupal\Core\Menu\MenuLinkManagerInterface $menu_link_manager */ Chris@0: $menu_link_manager = \Drupal::service('plugin.manager.menu.link'); Chris@0: $before_count = $menu_link_manager->countMenuLinks(NULL); Chris@0: $menu_link_manager->rebuild(); Chris@0: $after_count = $menu_link_manager->countMenuLinks(NULL); Chris@0: $this->assertIdentical($before_count, $after_count, 'MenuLinkManager::rebuild() does not add more links'); Chris@0: // Do standard user tests. Chris@0: // Log in the user. Chris@0: $this->drupalLogin($this->authenticatedUser); Chris@0: $this->verifyAccess(403); Chris@0: Chris@0: foreach ($this->items as $item) { Chris@0: // Menu link URIs are stored as 'internal:/node/$nid'. Chris@0: $node = Node::load(str_replace('internal:/node/', '', $item->link->uri)); Chris@0: $this->verifyMenuLink($item, $node); Chris@0: } Chris@0: Chris@0: // Log in the administrator. Chris@0: $this->drupalLogin($this->adminUser); Chris@0: Chris@0: // Verify delete link exists and reset link does not exist. Chris@0: $this->drupalGet('admin/structure/menu/manage/' . $this->menu->id()); Chris@0: $this->assertLinkByHref(Url::fromRoute('entity.menu_link_content.delete_form', ['menu_link_content' => $this->items[0]->id()])->toString()); Chris@0: $this->assertNoLinkByHref(Url::fromRoute('menu_ui.link_reset', ['menu_link_plugin' => $this->items[0]->getPluginId()])->toString()); Chris@0: // Check delete and reset access. Chris@0: $this->drupalGet('admin/structure/menu/item/' . $this->items[0]->id() . '/delete'); Chris@0: $this->assertResponse(200); Chris@0: $this->drupalGet('admin/structure/menu/link/' . $this->items[0]->getPluginId() . '/reset'); Chris@0: $this->assertResponse(403); Chris@0: Chris@0: // Delete menu links. Chris@0: foreach ($this->items as $item) { Chris@0: $this->deleteMenuLink($item); Chris@0: } Chris@0: Chris@0: // Delete custom menu. Chris@0: $this->deleteCustomMenu(); Chris@0: Chris@0: // Modify and reset a standard menu link. Chris@0: $instance = $this->getStandardMenuLink(); Chris@0: $old_weight = $instance->getWeight(); Chris@0: // Edit the static menu link. Chris@0: $edit = []; Chris@0: $edit['weight'] = 10; Chris@0: $id = $instance->getPluginId(); Chris@0: $this->drupalPostForm("admin/structure/menu/link/$id/edit", $edit, t('Save')); Chris@0: $this->assertResponse(200); Chris@0: $this->assertText('The menu link has been saved.'); Chris@0: $menu_link_manager->resetDefinitions(); Chris@0: Chris@0: $instance = $menu_link_manager->createInstance($instance->getPluginId()); Chris@0: $this->assertEqual($edit['weight'], $instance->getWeight(), 'Saving an existing link updates the weight.'); Chris@0: $this->resetMenuLink($instance, $old_weight); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Adds a custom menu using CRUD functions. Chris@0: */ Chris@0: public function addCustomMenuCRUD() { Chris@0: // Add a new custom menu. Chris@0: $menu_name = substr(hash('sha256', $this->randomMachineName(16)), 0, MENU_MAX_MENU_NAME_LENGTH_UI); Chris@0: $label = $this->randomMachineName(16); Chris@0: Chris@0: $menu = Menu::create([ Chris@0: 'id' => $menu_name, Chris@0: 'label' => $label, Chris@0: 'description' => 'Description text', Chris@0: ]); Chris@0: $menu->save(); Chris@0: Chris@0: // Assert the new menu. Chris@0: $this->drupalGet('admin/structure/menu/manage/' . $menu_name); Chris@0: $this->assertRaw($label, 'Custom menu was added.'); Chris@0: Chris@0: // Edit the menu. Chris@0: $new_label = $this->randomMachineName(16); Chris@0: $menu->set('label', $new_label); Chris@0: $menu->save(); Chris@0: $this->drupalGet('admin/structure/menu/manage/' . $menu_name); Chris@0: $this->assertRaw($new_label, 'Custom menu was edited.'); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Creates a custom menu. Chris@0: * Chris@0: * @return \Drupal\system\Entity\Menu Chris@0: * The custom menu that has been created. Chris@0: */ Chris@0: public function addCustomMenu() { Chris@0: // Try adding a menu using a menu_name that is too long. Chris@0: $this->drupalGet('admin/structure/menu/add'); Chris@0: $menu_name = substr(hash('sha256', $this->randomMachineName(16)), 0, MENU_MAX_MENU_NAME_LENGTH_UI + 1); Chris@0: $label = $this->randomMachineName(16); Chris@0: $edit = [ Chris@0: 'id' => $menu_name, Chris@0: 'description' => '', Chris@0: 'label' => $label, Chris@0: ]; Chris@0: $this->drupalPostForm('admin/structure/menu/add', $edit, t('Save')); Chris@0: Chris@0: // Verify that using a menu_name that is too long results in a validation Chris@0: // message. Chris@0: $this->assertRaw(t('@name cannot be longer than %max characters but is currently %length characters long.', [ Chris@0: '@name' => t('Menu name'), Chris@0: '%max' => MENU_MAX_MENU_NAME_LENGTH_UI, Chris@0: '%length' => Unicode::strlen($menu_name), Chris@0: ])); Chris@0: Chris@0: // Change the menu_name so it no longer exceeds the maximum length. Chris@0: $menu_name = substr(hash('sha256', $this->randomMachineName(16)), 0, MENU_MAX_MENU_NAME_LENGTH_UI); Chris@0: $edit['id'] = $menu_name; Chris@0: $this->drupalPostForm('admin/structure/menu/add', $edit, t('Save')); Chris@0: Chris@0: // Verify that no validation error is given for menu_name length. Chris@0: $this->assertNoRaw(t('@name cannot be longer than %max characters but is currently %length characters long.', [ Chris@0: '@name' => t('Menu name'), Chris@0: '%max' => MENU_MAX_MENU_NAME_LENGTH_UI, Chris@0: '%length' => Unicode::strlen($menu_name), Chris@0: ])); Chris@0: // Verify that the confirmation message is displayed. Chris@0: $this->assertRaw(t('Menu %label has been added.', ['%label' => $label])); Chris@0: $this->drupalGet('admin/structure/menu'); Chris@0: $this->assertText($label, 'Menu created'); Chris@0: Chris@0: // Confirm that the custom menu block is available. Chris@0: $this->drupalGet('admin/structure/block/list/' . $this->config('system.theme')->get('default')); Chris@0: $this->clickLinkPartialName('Place block'); Chris@0: $this->assertText($label); Chris@0: Chris@0: // Enable the block. Chris@0: $block = $this->drupalPlaceBlock('system_menu_block:' . $menu_name); Chris@0: $this->blockPlacements[$menu_name] = $block->id(); Chris@0: return Menu::load($menu_name); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Deletes the locally stored custom menu. Chris@0: * Chris@0: * This deletes the custom menu that is stored in $this->menu and performs Chris@0: * tests on the menu delete user interface. Chris@0: */ Chris@0: public function deleteCustomMenu() { Chris@0: $menu_name = $this->menu->id(); Chris@0: $label = $this->menu->label(); Chris@0: Chris@0: // Delete custom menu. Chris@0: $this->drupalPostForm("admin/structure/menu/manage/$menu_name/delete", [], t('Delete')); Chris@0: $this->assertResponse(200); Chris@0: $this->assertRaw(t('The menu %title has been deleted.', ['%title' => $label]), 'Custom menu was deleted'); Chris@0: $this->assertNull(Menu::load($menu_name), 'Custom menu was deleted'); Chris@0: // Test if all menu links associated with the menu were removed from Chris@0: // database. Chris@0: $result = entity_load_multiple_by_properties('menu_link_content', ['menu_name' => $menu_name]); Chris@0: $this->assertFalse($result, 'All menu links associated with the custom menu were deleted.'); Chris@0: Chris@0: // Make sure there's no delete button on system menus. Chris@0: $this->drupalGet('admin/structure/menu/manage/main'); Chris@0: $this->assertNoRaw('edit-delete', 'The delete button was not found'); Chris@0: Chris@0: // Try to delete the main menu. Chris@0: $this->drupalGet('admin/structure/menu/manage/main/delete'); Chris@0: $this->assertText(t('You are not authorized to access this page.')); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Tests menu functionality. Chris@0: */ Chris@0: public function doMenuTests() { Chris@0: $menu_name = $this->menu->id(); Chris@0: Chris@0: // Test the 'Add link' local action. Chris@0: $this->drupalGet(Url::fromRoute('entity.menu.edit_form', ['menu' => $menu_name])); Chris@0: Chris@0: $this->clickLink(t('Add link')); Chris@0: $link_title = $this->randomString(); Chris@0: $this->drupalPostForm(NULL, ['link[0][uri]' => '/', 'title[0][value]' => $link_title], t('Save')); Chris@0: $this->assertUrl(Url::fromRoute('entity.menu.edit_form', ['menu' => $menu_name])); Chris@0: // Test the 'Edit' operation. Chris@0: $this->clickLink(t('Edit')); Chris@0: $this->assertFieldByName('title[0][value]', $link_title); Chris@0: $link_title = $this->randomString(); Chris@0: $this->drupalPostForm(NULL, ['title[0][value]' => $link_title], t('Save')); Chris@0: $this->assertUrl(Url::fromRoute('entity.menu.edit_form', ['menu' => $menu_name])); Chris@0: // Test the 'Delete' operation. Chris@0: $this->clickLink(t('Delete')); Chris@0: $this->assertRaw(t('Are you sure you want to delete the custom menu link %item?', ['%item' => $link_title])); Chris@0: $this->drupalPostForm(NULL, [], t('Delete')); Chris@0: $this->assertUrl(Url::fromRoute('entity.menu.edit_form', ['menu' => $menu_name])); Chris@0: Chris@0: // Add nodes to use as links for menu links. Chris@0: $node1 = $this->drupalCreateNode(['type' => 'article']); Chris@0: $node2 = $this->drupalCreateNode(['type' => 'article']); Chris@0: $node3 = $this->drupalCreateNode(['type' => 'article']); Chris@0: $node4 = $this->drupalCreateNode(['type' => 'article']); Chris@0: // Create a node with an alias. Chris@0: $node5 = $this->drupalCreateNode([ Chris@0: 'type' => 'article', Chris@0: 'path' => [ Chris@0: 'alias' => '/node5', Chris@0: ], Chris@0: ]); Chris@0: Chris@0: // Verify add link button. Chris@0: $this->drupalGet('admin/structure/menu'); Chris@0: $this->assertLinkByHref('admin/structure/menu/manage/' . $menu_name . '/add', 0, "The add menu link button URL is correct"); Chris@0: Chris@0: // Verify form defaults. Chris@0: $this->doMenuLinkFormDefaultsTest(); Chris@0: Chris@0: // Add menu links. Chris@0: $item1 = $this->addMenuLink('', '/node/' . $node1->id(), $menu_name, TRUE); Chris@0: $item2 = $this->addMenuLink($item1->getPluginId(), '/node/' . $node2->id(), $menu_name, FALSE); Chris@0: $item3 = $this->addMenuLink($item2->getPluginId(), '/node/' . $node3->id(), $menu_name); Chris@0: Chris@0: // Hierarchy Chris@0: // <$menu_name> Chris@0: // - item1 Chris@0: // -- item2 Chris@0: // --- item3 Chris@0: Chris@0: $this->assertMenuLink($item1->getPluginId(), [ Chris@0: 'children' => [$item2->getPluginId(), $item3->getPluginId()], Chris@0: 'parents' => [$item1->getPluginId()], Chris@0: // We assert the language code here to make sure that the language Chris@0: // selection element degrades gracefully without the Language module. Chris@0: 'langcode' => 'en', Chris@0: ]); Chris@0: $this->assertMenuLink($item2->getPluginId(), [ Chris@0: 'children' => [$item3->getPluginId()], Chris@0: 'parents' => [$item2->getPluginId(), $item1->getPluginId()], Chris@0: // See above. Chris@0: 'langcode' => 'en', Chris@0: ]); Chris@0: $this->assertMenuLink($item3->getPluginId(), [ Chris@0: 'children' => [], Chris@0: 'parents' => [$item3->getPluginId(), $item2->getPluginId(), $item1->getPluginId()], Chris@0: // See above. Chris@0: 'langcode' => 'en', Chris@0: ]); Chris@0: Chris@0: // Verify menu links. Chris@0: $this->verifyMenuLink($item1, $node1); Chris@0: $this->verifyMenuLink($item2, $node2, $item1, $node1); Chris@0: $this->verifyMenuLink($item3, $node3, $item2, $node2); Chris@0: Chris@0: // Add more menu links. Chris@0: $item4 = $this->addMenuLink('', '/node/' . $node4->id(), $menu_name); Chris@0: $item5 = $this->addMenuLink($item4->getPluginId(), '/node/' . $node5->id(), $menu_name); Chris@0: // Create a menu link pointing to an alias. Chris@0: $item6 = $this->addMenuLink($item4->getPluginId(), '/node5', $menu_name, TRUE, '0'); Chris@0: Chris@0: // Hierarchy Chris@0: // <$menu_name> Chris@0: // - item1 Chris@0: // -- item2 Chris@0: // --- item3 Chris@0: // - item4 Chris@0: // -- item5 Chris@0: // -- item6 Chris@0: Chris@0: $this->assertMenuLink($item4->getPluginId(), [ Chris@0: 'children' => [$item5->getPluginId(), $item6->getPluginId()], Chris@0: 'parents' => [$item4->getPluginId()], Chris@0: // See above. Chris@0: 'langcode' => 'en', Chris@0: ]); Chris@0: $this->assertMenuLink($item5->getPluginId(), [ Chris@0: 'children' => [], Chris@0: 'parents' => [$item5->getPluginId(), $item4->getPluginId()], Chris@0: 'langcode' => 'en', Chris@0: ]); Chris@0: $this->assertMenuLink($item6->getPluginId(), [ Chris@0: 'children' => [], Chris@0: 'parents' => [$item6->getPluginId(), $item4->getPluginId()], Chris@0: 'route_name' => 'entity.node.canonical', Chris@0: 'route_parameters' => ['node' => $node5->id()], Chris@0: 'url' => '', Chris@0: // See above. Chris@0: 'langcode' => 'en', Chris@0: ]); Chris@0: Chris@0: // Modify menu links. Chris@0: $this->modifyMenuLink($item1); Chris@0: $this->modifyMenuLink($item2); Chris@0: Chris@0: // Toggle menu links. Chris@0: $this->toggleMenuLink($item1); Chris@0: $this->toggleMenuLink($item2); Chris@0: Chris@0: // Move link and verify that descendants are updated. Chris@0: $this->moveMenuLink($item2, $item5->getPluginId(), $menu_name); Chris@0: // Hierarchy Chris@0: // <$menu_name> Chris@0: // - item1 Chris@0: // - item4 Chris@0: // -- item5 Chris@0: // --- item2 Chris@0: // ---- item3 Chris@0: // -- item6 Chris@0: Chris@0: $this->assertMenuLink($item1->getPluginId(), [ Chris@0: 'children' => [], Chris@0: 'parents' => [$item1->getPluginId()], Chris@0: // See above. Chris@0: 'langcode' => 'en', Chris@0: ]); Chris@0: $this->assertMenuLink($item4->getPluginId(), [ Chris@0: 'children' => [$item5->getPluginId(), $item6->getPluginId(), $item2->getPluginId(), $item3->getPluginId()], Chris@0: 'parents' => [$item4->getPluginId()], Chris@0: // See above. Chris@0: 'langcode' => 'en', Chris@0: ]); Chris@0: Chris@0: $this->assertMenuLink($item5->getPluginId(), [ Chris@0: 'children' => [$item2->getPluginId(), $item3->getPluginId()], Chris@0: 'parents' => [$item5->getPluginId(), $item4->getPluginId()], Chris@0: // See above. Chris@0: 'langcode' => 'en', Chris@0: ]); Chris@0: $this->assertMenuLink($item2->getPluginId(), [ Chris@0: 'children' => [$item3->getPluginId()], Chris@0: 'parents' => [$item2->getPluginId(), $item5->getPluginId(), $item4->getPluginId()], Chris@0: // See above. Chris@0: 'langcode' => 'en', Chris@0: ]); Chris@0: $this->assertMenuLink($item3->getPluginId(), [ Chris@0: 'children' => [], Chris@0: 'parents' => [$item3->getPluginId(), $item2->getPluginId(), $item5->getPluginId(), $item4->getPluginId()], Chris@0: // See above. Chris@0: 'langcode' => 'en', Chris@0: ]); Chris@0: Chris@0: // Add 102 menu links with increasing weights, then make sure the last-added Chris@0: // item's weight doesn't get changed because of the old hardcoded delta=50. Chris@0: $items = []; Chris@0: for ($i = -50; $i <= 51; $i++) { Chris@0: $items[$i] = $this->addMenuLink('', '/node/' . $node1->id(), $menu_name, TRUE, strval($i)); Chris@0: } Chris@0: $this->assertMenuLink($items[51]->getPluginId(), ['weight' => '51']); Chris@0: Chris@0: // Disable a link and then re-enable the link via the overview form. Chris@0: $this->disableMenuLink($item1); Chris@0: $edit = []; Chris@0: $edit['links[menu_plugin_id:' . $item1->getPluginId() . '][enabled]'] = TRUE; Chris@0: $this->drupalPostForm('admin/structure/menu/manage/' . $item1->getMenuName(), $edit, t('Save')); Chris@0: Chris@0: // Mark item2, item4 and item5 as expanded. Chris@0: // This is done in order to show them on the frontpage. Chris@0: $item2->expanded->value = 1; Chris@0: $item2->save(); Chris@0: $item4->expanded->value = 1; Chris@0: $item4->save(); Chris@0: $item5->expanded->value = 1; Chris@0: $item5->save(); Chris@0: Chris@0: // Verify in the database. Chris@0: $this->assertMenuLink($item1->getPluginId(), ['enabled' => 1]); Chris@0: Chris@0: // Add an external link. Chris@0: $item7 = $this->addMenuLink('', 'https://www.drupal.org', $menu_name); Chris@0: $this->assertMenuLink($item7->getPluginId(), ['url' => 'https://www.drupal.org']); Chris@0: Chris@0: // Add menu item. Chris@0: $item8 = $this->addMenuLink('', '/', $menu_name); Chris@0: $this->assertMenuLink($item8->getPluginId(), ['route_name' => '']); Chris@0: $this->drupalGet(''); Chris@0: $this->assertResponse(200); Chris@0: // Make sure we get routed correctly. Chris@0: $this->clickLink($item8->getTitle()); Chris@0: $this->assertResponse(200); Chris@0: Chris@0: // Check invalid menu link parents. Chris@0: $this->checkInvalidParentMenuLinks(); Chris@0: Chris@0: // Save menu links for later tests. Chris@0: $this->items[] = $item1; Chris@0: $this->items[] = $item2; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Ensures that the proper default values are set when adding a menu link Chris@0: */ Chris@0: protected function doMenuLinkFormDefaultsTest() { Chris@0: $this->drupalGet("admin/structure/menu/manage/tools/add"); Chris@0: $this->assertResponse(200); Chris@0: Chris@0: $this->assertFieldByName('title[0][value]', ''); Chris@0: $this->assertFieldByName('link[0][uri]', ''); Chris@0: Chris@0: $this->assertNoFieldChecked('edit-expanded-value'); Chris@0: $this->assertFieldChecked('edit-enabled-value'); Chris@0: Chris@0: $this->assertFieldByName('description[0][value]', ''); Chris@0: $this->assertFieldByName('weight[0][value]', 0); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Adds and removes a menu link with a query string and fragment. Chris@0: */ Chris@0: public function testMenuQueryAndFragment() { Chris@0: $this->drupalLogin($this->adminUser); Chris@0: Chris@0: // Make a path with query and fragment on. Chris@0: $path = '/test-page?arg1=value1&arg2=value2'; Chris@0: $item = $this->addMenuLink('', $path); Chris@0: Chris@0: $this->drupalGet('admin/structure/menu/item/' . $item->id() . '/edit'); Chris@0: $this->assertFieldByName('link[0][uri]', $path, 'Path is found with both query and fragment.'); Chris@0: Chris@0: // Now change the path to something without query and fragment. Chris@0: $path = '/test-page'; Chris@0: $this->drupalPostForm('admin/structure/menu/item/' . $item->id() . '/edit', ['link[0][uri]' => $path], t('Save')); Chris@0: $this->drupalGet('admin/structure/menu/item/' . $item->id() . '/edit'); Chris@0: $this->assertFieldByName('link[0][uri]', $path, 'Path no longer has query or fragment.'); Chris@0: Chris@0: // Use #fragment and ensure that saving it does not lose its content. Chris@0: $path = '?arg1=value#fragment'; Chris@0: $item = $this->addMenuLink('', $path); Chris@0: Chris@0: $this->drupalGet('admin/structure/menu/item/' . $item->id() . '/edit'); Chris@0: $this->assertFieldByName('link[0][uri]', $path, 'Path is found with both query and fragment.'); Chris@0: Chris@0: $this->drupalPostForm('admin/structure/menu/item/' . $item->id() . '/edit', [], t('Save')); Chris@0: Chris@0: $this->drupalGet('admin/structure/menu/item/' . $item->id() . '/edit'); Chris@0: $this->assertFieldByName('link[0][uri]', $path, 'Path is found with both query and fragment.'); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Tests renaming the built-in menu. Chris@0: */ Chris@0: public function testSystemMenuRename() { Chris@0: $this->drupalLogin($this->adminUser); Chris@0: $edit = [ Chris@0: 'label' => $this->randomMachineName(16), Chris@0: ]; Chris@0: $this->drupalPostForm('admin/structure/menu/manage/main', $edit, t('Save')); Chris@0: Chris@0: // Make sure menu shows up with new name in block addition. Chris@0: $default_theme = $this->config('system.theme')->get('default'); Chris@0: $this->drupalget('admin/structure/block/list/' . $default_theme); Chris@0: $this->clickLinkPartialName('Place block'); Chris@0: $this->assertText($edit['label']); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Tests that menu items pointing to unpublished nodes are editable. Chris@0: */ Chris@0: public function testUnpublishedNodeMenuItem() { Chris@0: $this->drupalLogin($this->drupalCreateUser(['access administration pages', 'administer blocks', 'administer menu', 'create article content', 'bypass node access'])); Chris@0: // Create an unpublished node. Chris@0: $node = $this->drupalCreateNode([ Chris@0: 'type' => 'article', Chris@0: 'status' => NodeInterface::NOT_PUBLISHED, Chris@0: ]); Chris@0: Chris@0: $item = $this->addMenuLink('', '/node/' . $node->id()); Chris@0: $this->modifyMenuLink($item); Chris@0: Chris@0: // Test that a user with 'administer menu' but without 'bypass node access' Chris@0: // cannot see the menu item. Chris@0: $this->drupalLogout(); Chris@0: $this->drupalLogin($this->adminUser); Chris@0: $this->drupalGet('admin/structure/menu/manage/' . $item->getMenuName()); Chris@0: $this->assertNoText($item->getTitle(), "Menu link pointing to unpublished node is only visible to users with 'bypass node access' permission"); Chris@0: // The cache contexts associated with the (in)accessible menu links are Chris@0: // bubbled. See DefaultMenuLinkTreeManipulators::menuLinkCheckAccess(). Chris@0: $this->assertCacheContext('user.permissions'); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Tests the contextual links on a menu block. Chris@0: */ Chris@0: public function testBlockContextualLinks() { Chris@0: $this->drupalLogin($this->drupalCreateUser(['administer menu', 'access contextual links', 'administer blocks'])); Chris@0: $custom_menu = $this->addCustomMenu(); Chris@0: $this->addMenuLink('', '/', $custom_menu->id()); Chris@0: $block = $this->drupalPlaceBlock('system_menu_block:' . $custom_menu->id(), ['label' => 'Custom menu', 'provider' => 'system']); Chris@0: $this->drupalGet('test-page'); Chris@0: Chris@0: $id = 'block:block=' . $block->id() . ':langcode=en|menu:menu=' . $custom_menu->id() . ':langcode=en'; Chris@0: // @see \Drupal\contextual\Tests\ContextualDynamicContextTest:assertContextualLinkPlaceHolder() Chris@0: $this->assertRaw('
', format_string('Contextual link placeholder with id @id exists.', ['@id' => $id])); Chris@0: Chris@0: // Get server-rendered contextual links. Chris@0: // @see \Drupal\contextual\Tests\ContextualDynamicContextTest:renderContextualLinks() Chris@0: $post = ['ids[0]' => $id]; Chris@0: $response = $this->drupalPost('contextual/render', 'application/json', $post, ['query' => ['destination' => 'test-page']]); Chris@0: $this->assertResponse(200); Chris@0: $json = Json::decode($response); Chris@0: $this->assertIdentical($json[$id], ''); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Adds a menu link using the UI. Chris@0: * Chris@0: * @param string $parent Chris@0: * Optional parent menu link id. Chris@0: * @param string $path Chris@0: * The path to enter on the form. Defaults to the front page. Chris@0: * @param string $menu_name Chris@0: * Menu name. Defaults to 'tools'. Chris@0: * @param bool $expanded Chris@0: * Whether or not this menu link is expanded. Setting this to TRUE should Chris@0: * test whether it works when we do the authenticatedUser tests. Defaults Chris@0: * to FALSE. Chris@0: * @param string $weight Chris@0: * Menu weight. Defaults to 0. Chris@0: * Chris@0: * @return \Drupal\menu_link_content\Entity\MenuLinkContent Chris@0: * A menu link entity. Chris@0: */ Chris@0: public function addMenuLink($parent = '', $path = '/', $menu_name = 'tools', $expanded = FALSE, $weight = '0') { Chris@0: // View add menu link page. Chris@0: $this->drupalGet("admin/structure/menu/manage/$menu_name/add"); Chris@0: $this->assertResponse(200); Chris@0: Chris@0: $title = '!link_' . $this->randomMachineName(16); Chris@0: $edit = [ Chris@0: 'link[0][uri]' => $path, Chris@0: 'title[0][value]' => $title, Chris@0: 'description[0][value]' => '', Chris@0: 'enabled[value]' => 1, Chris@0: 'expanded[value]' => $expanded, Chris@0: 'menu_parent' => $menu_name . ':' . $parent, Chris@0: 'weight[0][value]' => $weight, Chris@0: ]; Chris@0: Chris@0: // Add menu link. Chris@0: $this->drupalPostForm(NULL, $edit, t('Save')); Chris@0: $this->assertResponse(200); Chris@0: $this->assertText('The menu link has been saved.'); Chris@0: Chris@0: $menu_links = entity_load_multiple_by_properties('menu_link_content', ['title' => $title]); Chris@0: Chris@0: $menu_link = reset($menu_links); Chris@0: $this->assertTrue($menu_link, 'Menu link was found in database.'); Chris@0: $this->assertMenuLink($menu_link->getPluginId(), ['menu_name' => $menu_name, 'children' => [], 'parent' => $parent]); Chris@0: Chris@0: return $menu_link; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Attempts to add menu link with invalid path or no access permission. Chris@0: */ Chris@0: public function addInvalidMenuLink() { Chris@0: foreach (['access' => '/admin/people/permissions'] as $type => $link_path) { Chris@0: $edit = [ Chris@0: 'link[0][uri]' => $link_path, Chris@0: 'title[0][value]' => 'title', Chris@0: ]; Chris@0: $this->drupalPostForm("admin/structure/menu/manage/{$this->menu->id()}/add", $edit, t('Save')); Chris@0: $this->assertRaw(t("The path '@link_path' is inaccessible.", ['@link_path' => $link_path]), 'Menu link was not created'); Chris@0: } Chris@0: } Chris@0: Chris@0: /** Chris@0: * Tests that parent options are limited by depth when adding menu links. Chris@0: */ Chris@0: public function checkInvalidParentMenuLinks() { Chris@0: $last_link = NULL; Chris@0: $created_links = []; Chris@0: Chris@0: // Get the max depth of the tree. Chris@0: $menu_link_tree = \Drupal::service('menu.link_tree'); Chris@0: $max_depth = $menu_link_tree->maxDepth(); Chris@0: Chris@0: // Create a maximum number of menu links, each a child of the previous. Chris@0: for ($i = 0; $i <= $max_depth - 1; $i++) { Chris@0: $parent = $last_link ? 'tools:' . $last_link->getPluginId() : 'tools:'; Chris@0: $title = 'title' . $i; Chris@0: $edit = [ Chris@0: 'link[0][uri]' => '/', Chris@0: 'title[0][value]' => $title, Chris@0: 'menu_parent' => $parent, Chris@0: 'description[0][value]' => '', Chris@0: 'enabled[value]' => 1, Chris@0: 'expanded[value]' => FALSE, Chris@0: 'weight[0][value]' => '0', Chris@0: ]; Chris@0: $this->drupalPostForm("admin/structure/menu/manage/{$this->menu->id()}/add", $edit, t('Save')); Chris@0: $menu_links = entity_load_multiple_by_properties('menu_link_content', ['title' => $title]); Chris@0: $last_link = reset($menu_links); Chris@0: $created_links[] = 'tools:' . $last_link->getPluginId(); Chris@0: } Chris@0: Chris@0: // The last link cannot be a parent in the new menu link form. Chris@0: $this->drupalGet('admin/structure/menu/manage/admin/add'); Chris@0: $value = 'tools:' . $last_link->getPluginId(); Chris@0: $this->assertNoOption('edit-menu-parent', $value, 'The invalid option is not there.'); Chris@0: Chris@0: // All but the last link can be parents in the new menu link form. Chris@0: array_pop($created_links); Chris@0: foreach ($created_links as $key => $link) { Chris@0: $this->assertOption('edit-menu-parent', $link, 'The valid option number ' . ($key + 1) . ' is there.'); Chris@0: } Chris@0: } Chris@0: Chris@0: /** Chris@0: * Verifies a menu link using the UI. Chris@0: * Chris@0: * @param \Drupal\menu_link_content\Entity\MenuLinkContent $item Chris@0: * Menu link. Chris@0: * @param object $item_node Chris@0: * Menu link content node. Chris@0: * @param \Drupal\menu_link_content\Entity\MenuLinkContent $parent Chris@0: * Parent menu link. Chris@0: * @param object $parent_node Chris@0: * Parent menu link content node. Chris@0: */ Chris@0: public function verifyMenuLink(MenuLinkContent $item, $item_node, MenuLinkContent $parent = NULL, $parent_node = NULL) { Chris@0: // View home page. Chris@0: $this->drupalGet(''); Chris@0: $this->assertResponse(200); Chris@0: Chris@0: // Verify parent menu link. Chris@0: if (isset($parent)) { Chris@0: // Verify menu link. Chris@0: $title = $parent->getTitle(); Chris@0: $this->assertLink($title, 0, 'Parent menu link was displayed'); Chris@0: Chris@0: // Verify menu link link. Chris@0: $this->clickLink($title); Chris@0: $title = $parent_node->label(); Chris@0: $this->assertTitle(t("@title | Drupal", ['@title' => $title]), 'Parent menu link link target was correct'); Chris@0: } Chris@0: Chris@0: // Verify menu link. Chris@0: $title = $item->getTitle(); Chris@0: $this->assertLink($title, 0, 'Menu link was displayed'); Chris@0: Chris@0: // Verify menu link link. Chris@0: $this->clickLink($title); Chris@0: $title = $item_node->label(); Chris@0: $this->assertTitle(t("@title | Drupal", ['@title' => $title]), 'Menu link link target was correct'); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Changes the parent of a menu link using the UI. Chris@0: * Chris@0: * @param \Drupal\menu_link_content\MenuLinkContentInterface $item Chris@0: * The menu link item to move. Chris@0: * @param int $parent Chris@0: * The id of the new parent. Chris@0: * @param string $menu_name Chris@0: * The menu the menu link will be moved to. Chris@0: */ Chris@0: public function moveMenuLink(MenuLinkContent $item, $parent, $menu_name) { Chris@0: $mlid = $item->id(); Chris@0: Chris@0: $edit = [ Chris@0: 'menu_parent' => $menu_name . ':' . $parent, Chris@0: ]; Chris@0: $this->drupalPostForm("admin/structure/menu/item/$mlid/edit", $edit, t('Save')); Chris@0: $this->assertResponse(200); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Modifies a menu link using the UI. Chris@0: * Chris@0: * @param \Drupal\menu_link_content\Entity\MenuLinkContent $item Chris@0: * Menu link entity. Chris@0: */ Chris@0: public function modifyMenuLink(MenuLinkContent $item) { Chris@0: $item->title->value = $this->randomMachineName(16); Chris@0: Chris@0: $mlid = $item->id(); Chris@0: $title = $item->getTitle(); Chris@0: Chris@0: // Edit menu link. Chris@0: $edit = []; Chris@0: $edit['title[0][value]'] = $title; Chris@0: $this->drupalPostForm("admin/structure/menu/item/$mlid/edit", $edit, t('Save')); Chris@0: $this->assertResponse(200); Chris@0: $this->assertText('The menu link has been saved.'); Chris@0: // Verify menu link. Chris@0: $this->drupalGet('admin/structure/menu/manage/' . $item->getMenuName()); Chris@0: $this->assertText($title, 'Menu link was edited'); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Resets a standard menu link using the UI. Chris@0: * Chris@0: * @param \Drupal\Core\Menu\MenuLinkInterface $menu_link Chris@0: * The Menu link. Chris@0: * @param int $old_weight Chris@0: * Original title for menu link. Chris@0: */ Chris@0: public function resetMenuLink(MenuLinkInterface $menu_link, $old_weight) { Chris@0: // Reset menu link. Chris@0: $this->drupalPostForm("admin/structure/menu/link/{$menu_link->getPluginId()}/reset", [], t('Reset')); Chris@0: $this->assertResponse(200); Chris@0: $this->assertRaw(t('The menu link was reset to its default settings.'), 'Menu link was reset'); Chris@0: Chris@0: // Verify menu link. Chris@0: $instance = \Drupal::service('plugin.manager.menu.link')->createInstance($menu_link->getPluginId()); Chris@0: $this->assertEqual($old_weight, $instance->getWeight(), 'Resets to the old weight.'); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Deletes a menu link using the UI. Chris@0: * Chris@0: * @param \Drupal\menu_link_content\Entity\MenuLinkContent $item Chris@0: * Menu link. Chris@0: */ Chris@0: public function deleteMenuLink(MenuLinkContent $item) { Chris@0: $mlid = $item->id(); Chris@0: $title = $item->getTitle(); Chris@0: Chris@0: // Delete menu link. Chris@0: $this->drupalPostForm("admin/structure/menu/item/$mlid/delete", [], t('Delete')); Chris@0: $this->assertResponse(200); Chris@0: $this->assertRaw(t('The menu link %title has been deleted.', ['%title' => $title]), 'Menu link was deleted'); Chris@0: Chris@0: // Verify deletion. Chris@0: $this->drupalGet(''); Chris@0: $this->assertNoText($title, 'Menu link was deleted'); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Alternately disables and enables a menu link. Chris@0: * Chris@0: * @param \Drupal\menu_link_content\Entity\MenuLinkContent $item Chris@0: * Menu link. Chris@0: */ Chris@0: public function toggleMenuLink(MenuLinkContent $item) { Chris@0: $this->disableMenuLink($item); Chris@0: Chris@0: // Verify menu link is absent. Chris@0: $this->drupalGet(''); Chris@0: $this->assertNoText($item->getTitle(), 'Menu link was not displayed'); Chris@0: $this->enableMenuLink($item); Chris@0: Chris@0: // Verify menu link is displayed. Chris@0: $this->drupalGet(''); Chris@0: $this->assertText($item->getTitle(), 'Menu link was displayed'); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Disables a menu link. Chris@0: * Chris@0: * @param \Drupal\menu_link_content\Entity\MenuLinkContent $item Chris@0: * Menu link. Chris@0: */ Chris@0: public function disableMenuLink(MenuLinkContent $item) { Chris@0: $mlid = $item->id(); Chris@0: $edit['enabled[value]'] = FALSE; Chris@0: $this->drupalPostForm("admin/structure/menu/item/$mlid/edit", $edit, t('Save')); Chris@0: Chris@0: // Unlike most other modules, there is no confirmation message displayed. Chris@0: // Verify in the database. Chris@0: $this->assertMenuLink($item->getPluginId(), ['enabled' => 0]); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Enables a menu link. Chris@0: * Chris@0: * @param \Drupal\menu_link_content\Entity\MenuLinkContent $item Chris@0: * Menu link. Chris@0: */ Chris@0: public function enableMenuLink(MenuLinkContent $item) { Chris@0: $mlid = $item->id(); Chris@0: $edit['enabled[value]'] = TRUE; Chris@0: $this->drupalPostForm("admin/structure/menu/item/$mlid/edit", $edit, t('Save')); Chris@0: Chris@0: // Verify in the database. Chris@0: $this->assertMenuLink($item->getPluginId(), ['enabled' => 1]); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Tests if administrative users other than user 1 can access the menu parents Chris@0: * AJAX callback. Chris@0: */ Chris@0: public function testMenuParentsJsAccess() { Chris@0: $admin = $this->drupalCreateUser(['administer menu']); Chris@0: $this->drupalLogin($admin); Chris@0: // Just check access to the callback overall, the POST data is irrelevant. Chris@0: $this->drupalGetAjax('admin/structure/menu/parents'); Chris@0: $this->assertResponse(200); Chris@0: Chris@0: // Do standard user tests. Chris@0: // Log in the user. Chris@0: $this->drupalLogin($this->authenticatedUser); Chris@0: $this->drupalGetAjax('admin/structure/menu/parents'); Chris@0: $this->assertResponse(403); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Returns standard menu link. Chris@0: * Chris@0: * @return \Drupal\Core\Menu\MenuLinkInterface Chris@0: * A menu link plugin. Chris@0: */ Chris@0: private function getStandardMenuLink() { Chris@0: // Retrieve menu link id of the Log out menu link, which will always be on Chris@0: // the front page. Chris@0: /** @var \Drupal\Core\Menu\MenuLinkManagerInterface $menu_link_manager */ Chris@0: $menu_link_manager = \Drupal::service('plugin.manager.menu.link'); Chris@0: $instance = $menu_link_manager->getInstance(['id' => 'user.logout']); Chris@0: Chris@0: $this->assertTrue((bool) $instance, 'Standard menu link was loaded'); Chris@0: return $instance; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Verifies the logged in user has the desired access to various menu pages. Chris@0: * Chris@0: * @param int $response Chris@0: * (optional) The expected HTTP response code. Defaults to 200. Chris@0: */ Chris@0: private function verifyAccess($response = 200) { Chris@0: // View menu help page. Chris@0: $this->drupalGet('admin/help/menu'); Chris@0: $this->assertResponse($response); Chris@0: if ($response == 200) { Chris@0: $this->assertText(t('Menu'), 'Menu help was displayed'); Chris@0: } Chris@0: Chris@0: // View menu build overview page. Chris@0: $this->drupalGet('admin/structure/menu'); Chris@0: $this->assertResponse($response); Chris@0: if ($response == 200) { Chris@0: $this->assertText(t('Menus'), 'Menu build overview page was displayed'); Chris@0: } Chris@0: Chris@0: // View tools menu customization page. Chris@0: $this->drupalGet('admin/structure/menu/manage/' . $this->menu->id()); Chris@0: $this->assertResponse($response); Chris@0: if ($response == 200) { Chris@0: $this->assertText(t('Tools'), 'Tools menu page was displayed'); Chris@0: } Chris@0: Chris@0: // View menu edit page for a static link. Chris@0: $item = $this->getStandardMenuLink(); Chris@0: $this->drupalGet('admin/structure/menu/link/' . $item->getPluginId() . '/edit'); Chris@0: $this->assertResponse($response); Chris@0: if ($response == 200) { Chris@0: $this->assertText(t('Edit menu item'), 'Menu edit page was displayed'); Chris@0: } Chris@0: Chris@0: // View add menu page. Chris@0: $this->drupalGet('admin/structure/menu/add'); Chris@0: $this->assertResponse($response); Chris@0: if ($response == 200) { Chris@0: $this->assertText(t('Menus'), 'Add menu page was displayed'); Chris@0: } Chris@0: } Chris@0: Chris@0: /** Chris@0: * Tests menu block settings. Chris@0: */ Chris@0: protected function doTestMenuBlock() { Chris@0: $menu_id = $this->menu->id(); Chris@0: $block_id = $this->blockPlacements[$menu_id]; Chris@0: $this->drupalGet('admin/structure/block/manage/' . $block_id); Chris@0: $this->drupalPostForm(NULL, [ Chris@0: 'settings[depth]' => 3, Chris@0: 'settings[level]' => 2, Chris@0: ], t('Save block')); Chris@0: $block = Block::load($block_id); Chris@0: $settings = $block->getPlugin()->getConfiguration(); Chris@0: $this->assertEqual($settings['depth'], 3); Chris@0: $this->assertEqual($settings['level'], 2); Chris@0: // Reset settings. Chris@0: $block->getPlugin()->setConfigurationValue('depth', 0); Chris@0: $block->getPlugin()->setConfigurationValue('level', 1); Chris@0: $block->save(); Chris@0: } Chris@0: Chris@0: }