/** * Asserts various aspects of a menu link entity. * * @param string $id * The link ID. * @param string $title * The expected title of the link. * @param string $menu * The expected ID of the menu to which the link will belong. * @param string $description * The link's expected description. * @param bool $enabled * Whether the link is enabled. * @param bool $expanded * Whether the link is expanded * @param array $attributes * Additional attributes the link is expected to have. * @param string $uri * The expected URI of the link. * @param int $weight * The expected weight of the link. */ protected function assertEntity($id, $title, $menu, $description, $enabled, $expanded, array $attributes, $uri, $weight) { /** @var \Drupal\menu_link_content\MenuLinkContentInterface $menu_link */ $menu_link = MenuLinkContent::load($id); $this->assertTrue($menu_link instanceof MenuLinkContentInterface); $this->assertIdentical($title, $menu_link->getTitle()); $this->assertIdentical($menu, $menu_link->getMenuName()); // The migration sets the description of the link to the value of the // 'title' attribute. Bit strange, but there you go. $this->assertIdentical($description, $menu_link->getDescription()); $this->assertIdentical($enabled, $menu_link->isEnabled()); $this->assertIdentical($expanded, $menu_link->isExpanded()); $this->assertIdentical($attributes, $menu_link->link->options); $this->assertIdentical($uri, $menu_link->link->uri); $this->assertIdentical($weight, $menu_link->getWeight()); }
/** * Tests the MenuLinkContentDeleteForm class. */ public function testMenuLinkContentDeleteForm() { // Add new menu item. $this->drupalPostForm('admin/structure/menu/manage/admin/add', ['title[0][value]' => t('Front page'), 'link[0][uri]' => '<front>'], t('Save')); $this->assertText(t('The menu link has been saved.')); $menu_link = MenuLinkContent::load(1); $this->drupalGet($menu_link->urlInfo('delete-form')); $this->assertRaw(t('Are you sure you want to delete the custom menu link %name?', ['%name' => $menu_link->label()])); $this->assertLink(t('Cancel')); // Make sure cancel link points to link edit $this->assertLinkByHref($menu_link->url('edit-form')); \Drupal::service('module_installer')->install(['menu_ui']); // Make sure cancel URL points to menu_ui route now. $this->drupalGet($menu_link->urlInfo('delete-form')); $menu = Menu::load($menu_link->getMenuName()); $this->assertLinkByHref($menu->url('edit-form')); $this->drupalPostForm(NULL, [], t('Delete')); $this->assertRaw(t('The menu link %title has been deleted.', ['%title' => $menu_link->label()])); }
/** * Tests migration of menu links. */ public function testMenuLinks() { $menu_link = MenuLinkContent::load(138); $this->assertIdentical('Test 1', $menu_link->getTitle()); $this->assertIdentical('secondary-links', $menu_link->getMenuName()); $this->assertIdentical('Test menu link 1', $menu_link->getDescription()); $this->assertIdentical(TRUE, $menu_link->isEnabled()); $this->assertIdentical(FALSE, $menu_link->isExpanded()); $this->assertIdentical(['attributes' => ['title' => 'Test menu link 1']], $menu_link->link->options); $this->assertIdentical('internal:/user/login', $menu_link->link->uri); $this->assertIdentical(-50, $menu_link->getWeight()); $menu_link = MenuLinkContent::load(139); $this->assertIdentical('Test 2', $menu_link->getTitle()); $this->assertIdentical('secondary-links', $menu_link->getMenuName()); $this->assertIdentical('Test menu link 2', $menu_link->getDescription()); $this->assertIdentical(TRUE, $menu_link->isEnabled()); $this->assertIdentical(TRUE, $menu_link->isExpanded()); $this->assertIdentical(['query' => 'foo=bar', 'attributes' => ['title' => 'Test menu link 2']], $menu_link->link->options); $this->assertIdentical('internal:/admin', $menu_link->link->uri); $this->assertIdentical(-49, $menu_link->getWeight()); $menu_link = MenuLinkContent::load(140); $this->assertIdentical('Drupal.org', $menu_link->getTitle()); $this->assertIdentical('secondary-links', $menu_link->getMenuName()); $this->assertIdentical(NULL, $menu_link->getDescription()); $this->assertIdentical(TRUE, $menu_link->isEnabled()); $this->assertIdentical(FALSE, $menu_link->isExpanded()); $this->assertIdentical(['attributes' => ['title' => '']], $menu_link->link->options); $this->assertIdentical('https://www.drupal.org', $menu_link->link->uri); $this->assertIdentical(-50, $menu_link->getWeight()); // assert that missing title attributes don't stop or break migration. $menu_link = MenuLinkContent::load(393); $this->assertIdentical('Test 3', $menu_link->getTitle()); $this->assertIdentical('secondary-links', $menu_link->getMenuName()); $this->assertIdentical(NULL, $menu_link->getDescription()); $this->assertIdentical(TRUE, $menu_link->isEnabled()); $this->assertIdentical(FALSE, $menu_link->isExpanded()); $this->assertIdentical([], $menu_link->link->options); $this->assertIdentical('internal:/user/login', $menu_link->link->uri); $this->assertIdentical(-47, $menu_link->getWeight()); }
/** * Test creating, editing, deleting menu links via node form widget. */ function testMenuNodeFormWidget() { // Verify that cacheability metadata is bubbled from the menu link tree // access checking that is performed when determining the "default parent // item" options in menu_ui_form_node_type_form_alter(). The "log out" link // adds the "user.roles:authenticated" cache context. $this->drupalGet('admin/structure/types/manage/page'); $this->assertCacheContext('user.roles:authenticated'); // Disable the default main menu, so that no menus are enabled. $edit = array('menu_options[main]' => FALSE); $this->drupalPostForm('admin/structure/types/manage/page', $edit, t('Save content type')); // Verify that no menu settings are displayed and nodes can be created. $this->drupalGet('node/add/page'); $this->assertText(t('Create Basic page')); $this->assertNoText(t('Menu settings')); $node_title = $this->randomMachineName(); $edit = array('title[0][value]' => $node_title, 'body[0][value]' => $this->randomString()); $this->drupalPostForm(NULL, $edit, t('Save')); $node = $this->drupalGetNodeByTitle($node_title); $this->assertEqual($node->getTitle(), $edit['title[0][value]']); // Test that we cannot set a menu item from a menu that is not set as // available. $edit = array('menu_options[tools]' => 1, 'menu_parent' => 'main:'); $this->drupalPostForm('admin/structure/types/manage/page', $edit, t('Save content type')); $this->assertText(t('The selected menu item is not under one of the selected menus.')); $this->assertNoRaw(t('The content type %name has been updated.', array('%name' => 'Basic page'))); // Enable Tools menu as available menu. $edit = array('menu_options[main]' => 1, 'menu_options[tools]' => 1, 'menu_parent' => 'main:'); $this->drupalPostForm('admin/structure/types/manage/page', $edit, t('Save content type')); $this->assertRaw(t('The content type %name has been updated.', array('%name' => 'Basic page'))); // Test that we can preview a node that will create a menu item. $edit = array('title[0][value]' => $node_title, 'menu[enabled]' => 1, 'menu[title]' => 'Test preview'); $this->drupalPostForm('node/add/page', $edit, t('Preview')); // Create a node. $node_title = $this->randomMachineName(); $edit = array('title[0][value]' => $node_title, 'body[0][value]' => $this->randomString()); $this->drupalPostForm('node/add/page', $edit, t('Save')); $node = $this->drupalGetNodeByTitle($node_title); // Assert that there is no link for the node. $this->drupalGet('test-page'); $this->assertNoLink($node_title); // Edit the node, enable the menu link setting, but skip the link title. $edit = array('menu[enabled]' => 1); $this->drupalPostForm('node/' . $node->id() . '/edit', $edit, t('Save')); // Assert that there is no link for the node. $this->drupalGet('test-page'); $this->assertNoLink($node_title); // Use not only the save button, but also the two special buttons: // 'Save and publish' as well as 'Save and keep published'. // These buttons just appear for 'administer nodes' users. $admin_user = $this->drupalCreateUser(['access administration pages', 'administer content types', 'administer nodes', 'administer menu', 'create page content', 'edit any page content']); $this->drupalLogin($admin_user); foreach (['Save and unpublish' => FALSE, 'Save and keep unpublished' => FALSE, 'Save and publish' => TRUE, 'Save and keep published' => TRUE] as $submit => $visible) { $edit = ['menu[enabled]' => 1, 'menu[title]' => $node_title]; $this->drupalPostForm('node/' . $node->id() . '/edit', $edit, $submit); // Assert that the link exists. $this->drupalGet('test-page'); if ($visible) { $this->assertLink($node_title, 0, 'Found a menu link after submitted with ' . $submit); } else { $this->assertNoLink($node_title, 'Found no menu link after submitted with ' . $submit); } } // Log back in as normal user. $this->drupalLogin($this->editor); // Edit the node and create a menu link. $edit = array('menu[enabled]' => 1, 'menu[title]' => $node_title, 'menu[weight]' => 17); $this->drupalPostForm('node/' . $node->id() . '/edit', $edit, t('Save')); // Assert that the link exists. $this->drupalGet('test-page'); $this->assertLink($node_title); $this->drupalGet('node/' . $node->id() . '/edit'); $this->assertFieldById('edit-menu-weight', 17, 'Menu weight correct in edit form'); // Disable the menu link, then edit the node--the link should stay disabled. $link_id = menu_ui_get_menu_link_defaults($node)['entity_id']; /** @var \Drupal\menu_link_content\Entity\MenuLinkContent $link */ $link = MenuLinkContent::load($link_id); $link->set('enabled', FALSE); $link->save(); $this->drupalPostForm($node->urlInfo('edit-form'), $edit, t('Save')); $link = MenuLinkContent::load($link_id); $this->assertFalse($link->isEnabled(), 'Saving a node with a disabled menu link keeps the menu link disabled.'); // Edit the node and remove the menu link. $edit = array('menu[enabled]' => FALSE); $this->drupalPostForm('node/' . $node->id() . '/edit', $edit, t('Save')); // Assert that there is no link for the node. $this->drupalGet('test-page'); $this->assertNoLink($node_title); // Add a menu link to the Administration menu. $item = entity_create('menu_link_content', array('link' => [['uri' => 'entity:node/' . $node->id()]], 'title' => $this->randomMachineName(16), 'menu_name' => 'admin')); $item->save(); // Assert that disabled Administration menu is not shown on the // node/$nid/edit page. $this->drupalGet('node/' . $node->id() . '/edit'); $this->assertText('Provide a menu link', 'Link in not allowed menu not shown in node edit form'); // Assert that the link is still in the Administration menu after save. $this->drupalPostForm('node/' . $node->id() . '/edit', $edit, t('Save')); $link = MenuLinkContent::load($item->id()); $this->assertTrue($link, 'Link in not allowed menu still exists after saving node'); // Move the menu link back to the Tools menu. $item->menu_name->value = 'tools'; $item->save(); // Create a second node. $child_node = $this->drupalCreateNode(array('type' => 'article')); // Assign a menu link to the second node, being a child of the first one. $child_item = entity_create('menu_link_content', array('link' => [['uri' => 'entity:node/' . $child_node->id()]], 'title' => $this->randomMachineName(16), 'parent' => $item->getPluginId(), 'menu_name' => $item->getMenuName())); $child_item->save(); // Edit the first node. $this->drupalGet('node/' . $node->id() . '/edit'); // Assert that it is not possible to set the parent of the first node to itself or the second node. $this->assertNoOption('edit-menu-menu-parent', 'tools:' . $item->getPluginId()); $this->assertNoOption('edit-menu-menu-parent', 'tools:' . $child_item->getPluginId()); // Assert that unallowed Administration menu is not available in options. $this->assertNoOption('edit-menu-menu-parent', 'admin:'); }
/** * Tests the regression in https://www.drupal.org/node/2532490. */ public function testDefaultMenuTabRegression() { $this->container->get('module_installer')->install(['menu_ui', 'menu_link_content', 'toolbar', 'system']); $admin_user = $this->drupalCreateUser(['administer views', 'administer blocks', 'bypass node access', 'access user profiles', 'view all revisions', 'administer permissions', 'administer menu', 'link to any page', 'access toolbar']); $this->drupalLogin($admin_user); $edit = ['title[0][value]' => 'Menu title', 'link[0][uri]' => '/admin/foo', 'menu_parent' => 'admin:system.admin']; $this->drupalPostForm('admin/structure/menu/manage/admin/add', $edit, t('Save')); $menu_items = \Drupal::entityManager()->getStorage('menu_link_content')->getQuery()->sort('id', 'DESC')->pager(1)->execute(); $menu_item = end($menu_items); /** @var \Drupal\menu_link_content\MenuLinkContentInterface $menu_link_content */ $menu_link_content = MenuLinkContent::load($menu_item); $edit = []; $edit['label'] = $this->randomMachineName(16); $view_id = $edit['id'] = strtolower($this->randomMachineName(16)); $edit['description'] = $this->randomMachineName(16); $edit['page[create]'] = TRUE; $edit['page[path]'] = 'admin/foo'; $this->drupalPostForm('admin/structure/views/add', $edit, t('Save and edit')); $parameters = new MenuTreeParameters(); $parameters->addCondition('id', $menu_link_content->getPluginId()); $result = \Drupal::menuTree()->load('admin', $parameters); $plugin_definition = end($result)->link->getPluginDefinition(); $this->assertEqual('view.' . $view_id . '.page_1', $plugin_definition['route_name']); $this->clickLink(t('No menu')); $this->drupalPostForm(NULL, ['menu[type]' => 'default tab', 'menu[title]' => 'Menu title'], t('Apply')); $this->assertText('Default tab options'); $this->drupalPostForm(NULL, ['tab_options[type]' => 'normal', 'tab_options[title]' => 'Parent title'], t('Apply')); $this->drupalPostForm(NULL, [], t('Save')); // Assert that saving the view will not cause an exception. $this->assertResponse(200); }
/** * Test automatic reparenting of menu links. */ function testMenuLinkReparenting($module = 'menu_test') { // Check the initial hierarchy. $links = $this->createLinkHierarchy($module); $expected_hierarchy = array('parent' => '', 'child-1' => 'parent', 'child-1-1' => 'child-1', 'child-1-2' => 'child-1', 'child-2' => 'parent'); $this->assertMenuLinkParents($links, $expected_hierarchy); // Start over, and move child-1 under child-2, and check that all the // children of child-1 have been moved too. $links = $this->createLinkHierarchy($module); /* @var \Drupal\Core\Menu\MenuLinkInterface $menu_link_plugin */ $this->menuLinkManager->updateDefinition($links['child-1'], array('parent' => $links['child-2'])); // Verify that the entity was updated too. $menu_link_plugin = $this->menuLinkManager->createInstance($links['child-1']); $entity = \Drupal::entityManager()->loadEntityByUuid('menu_link_content', $menu_link_plugin->getDerivativeId()); $this->assertEqual($entity->getParentId(), $links['child-2']); $expected_hierarchy = array('parent' => '', 'child-1' => 'child-2', 'child-1-1' => 'child-1', 'child-1-2' => 'child-1', 'child-2' => 'parent'); $this->assertMenuLinkParents($links, $expected_hierarchy); // Start over, and delete child-1, and check that the children of child-1 // have been reassigned to the parent. $links = $this->createLinkHierarchy($module); $this->menuLinkManager->removeDefinition($links['child-1']); $expected_hierarchy = array('parent' => FALSE, 'child-1-1' => 'parent', 'child-1-2' => 'parent', 'child-2' => 'parent'); $this->assertMenuLinkParents($links, $expected_hierarchy); // Try changing the parent at the entity level. $definition = $this->menuLinkManager->getDefinition($links['child-1-2']); $entity = MenuLinkContent::load($definition['metadata']['entity_id']); $entity->parent->value = ''; $entity->save(); $expected_hierarchy = array('parent' => '', 'child-1-1' => 'parent', 'child-1-2' => '', 'child-2' => 'parent'); $this->assertMenuLinkParents($links, $expected_hierarchy); // @todo Figure out what makes sense to test in terms of automatic // re-parenting. https://www.drupal.org/node/2309531 }
/** * Test creating, editing, deleting menu links via node form widget. */ function testMenuNodeFormWidget() { // Disable the default main menu, so that no menus are enabled. $edit = array('menu_options[main]' => FALSE); $this->drupalPostForm('admin/structure/types/manage/page', $edit, t('Save content type')); // Verify that no menu settings are displayed and nodes can be created. $this->drupalGet('node/add/page'); $this->assertText(t('Create Basic page')); $this->assertNoText(t('Menu settings')); $node_title = $this->randomMachineName(); $edit = array('title[0][value]' => $node_title, 'body[0][value]' => $this->randomString()); $this->drupalPostForm(NULL, $edit, t('Save')); $node = $this->drupalGetNodeByTitle($node_title); $this->assertEqual($node->getTitle(), $edit['title[0][value]']); // Test that we cannot set a menu item from a menu that is not set as // available. $edit = array('menu_options[tools]' => 1, 'menu_parent' => 'main:'); $this->drupalPostForm('admin/structure/types/manage/page', $edit, t('Save content type')); $this->assertText(t('The selected menu item is not under one of the selected menus.')); $this->assertNoRaw(t('The content type %name has been updated.', array('%name' => 'Basic page'))); // Enable Tools menu as available menu. $edit = array('menu_options[main]' => 1, 'menu_options[tools]' => 1, 'menu_parent' => 'main:'); $this->drupalPostForm('admin/structure/types/manage/page', $edit, t('Save content type')); $this->assertRaw(t('The content type %name has been updated.', array('%name' => 'Basic page'))); // Create a node. $node_title = $this->randomMachineName(); $edit = array('title[0][value]' => $node_title, 'body[0][value]' => $this->randomString()); $this->drupalPostForm('node/add/page', $edit, t('Save')); $node = $this->drupalGetNodeByTitle($node_title); // Assert that there is no link for the node. $this->drupalGet('test-page'); $this->assertNoLink($node_title); // Edit the node, enable the menu link setting, but skip the link title. $edit = array('menu[enabled]' => 1); $this->drupalPostForm('node/' . $node->id() . '/edit', $edit, t('Save')); // Assert that there is no link for the node. $this->drupalGet('test-page'); $this->assertNoLink($node_title); // Edit the node and create a menu link. $edit = array('menu[enabled]' => 1, 'menu[title]' => $node_title, 'menu[weight]' => 17); $this->drupalPostForm('node/' . $node->id() . '/edit', $edit, t('Save')); // Assert that the link exists. $this->drupalGet('test-page'); $this->assertLink($node_title); $this->drupalGet('node/' . $node->id() . '/edit'); $this->assertFieldById('edit-menu-weight', 17, 'Menu weight correct in edit form'); // Edit the node and remove the menu link. $edit = array('menu[enabled]' => FALSE); $this->drupalPostForm('node/' . $node->id() . '/edit', $edit, t('Save')); // Assert that there is no link for the node. $this->drupalGet('test-page'); $this->assertNoLink($node_title); // Add a menu link to the Administration menu. $item = entity_create('menu_link_content', array('route_name' => 'entity.node.canonical', 'route_parameters' => array('node' => $node->id()), 'title' => $this->randomMachineName(16), 'menu_name' => 'admin')); $item->save(); // Assert that disabled Administration menu is not shown on the // node/$nid/edit page. $this->drupalGet('node/' . $node->id() . '/edit'); $this->assertText('Provide a menu link', 'Link in not allowed menu not shown in node edit form'); // Assert that the link is still in the Administration menu after save. $this->drupalPostForm('node/' . $node->id() . '/edit', $edit, t('Save')); $link = MenuLinkContent::load($item->id()); $this->assertTrue($link, 'Link in not allowed menu still exists after saving node'); // Move the menu link back to the Tools menu. $item->menu_name->value = 'tools'; $item->save(); // Create a second node. $child_node = $this->drupalCreateNode(array('type' => 'article')); // Assign a menu link to the second node, being a child of the first one. $child_item = entity_create('menu_link_content', array('route_name' => 'entity.node.canonical', 'route_parameters' => array('node' => $child_node->id()), 'title' => $this->randomMachineName(16), 'parent' => $item->getPluginId(), 'menu_name' => $item->getMenuName())); $child_item->save(); // Edit the first node. $this->drupalGet('node/' . $node->id() . '/edit'); // Assert that it is not possible to set the parent of the first node to itself or the second node. $this->assertNoOption('edit-menu-menu-parent', 'tools:' . $item->getPluginId()); $this->assertNoOption('edit-menu-menu-parent', 'tools:' . $child_item->getPluginId()); // Assert that unallowed Administration menu is not available in options. $this->assertNoOption('edit-menu-menu-parent', 'admin:'); }