123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724 |
- <?php
- class MenuTestCase extends DrupalWebTestCase {
- protected $big_user;
- protected $std_user;
- protected $menu;
- protected $items;
- public static function getInfo() {
- return array(
- 'name' => 'Menu link creation/deletion',
- 'description' => 'Add a custom menu, add menu links to the custom menu and Navigation menu, check their data, and delete them using the menu module UI.',
- 'group' => 'Menu'
- );
- }
- function setUp() {
- parent::setUp('menu');
-
- $this->big_user = $this->drupalCreateUser(array('access administration pages', 'administer blocks', 'administer menu', 'create article content'));
- $this->std_user = $this->drupalCreateUser(array());
- }
-
- function testMenu() {
-
- $this->drupalLogin($this->big_user);
- $this->items = array();
-
- $this->doStandardMenuTests();
-
- $this->doCustomMenuTests();
-
-
- $this->drupalLogin($this->std_user);
- $this->verifyAccess(403);
- foreach ($this->items as $item) {
- $node = node_load(substr($item['link_path'], 5));
- $this->verifyMenuLink($item, $node);
- }
-
- $this->drupalLogin($this->big_user);
-
- foreach ($this->items as $item) {
- $this->deleteMenuLink($item);
- }
-
- $this->deleteCustomMenu($this->menu);
-
- $item = $this->getStandardMenuLink();
- $old_title = $item['link_title'];
- $this->modifyMenuLink($item);
- $item = menu_link_load($item['mlid']);
-
- $description = $this->randomName(16);
- $item['options']['attributes']['title'] = $description;
- menu_link_save($item);
- $saved_item = menu_link_load($item['mlid']);
- $this->assertEqual($description, $saved_item['options']['attributes']['title'], t('Saving an existing link updates the description (title attribute)'));
- $this->resetMenuLink($item, $old_title);
- }
-
- function doStandardMenuTests() {
- $this->doMenuTests();
- $this->addInvalidMenuLink();
- }
-
- function doCustomMenuTests() {
- $this->menu = $this->addCustomMenu();
- $this->doMenuTests($this->menu['menu_name']);
- $this->addInvalidMenuLink($this->menu['menu_name']);
- $this->addCustomMenuCRUD();
- }
-
- function addCustomMenuCRUD() {
-
- $menu_name = substr(hash('sha256', $this->randomName(16)), 0, MENU_MAX_MENU_NAME_LENGTH_UI);
- $title = $this->randomName(16);
- $menu = array(
- 'menu_name' => $menu_name,
- 'title' => $title,
- 'description' => 'Description text',
- );
- menu_save($menu);
-
- $this->drupalGet('admin/structure/menu/manage/' . $menu_name . '/edit');
- $this->assertRaw($title, t('Custom menu was added.'));
-
- $new_title = $this->randomName(16);
- $menu['title'] = $new_title;
- menu_save($menu);
- $this->drupalGet('admin/structure/menu/manage/' . $menu_name . '/edit');
- $this->assertRaw($new_title, t('Custom menu was edited.'));
- }
-
- function addCustomMenu() {
-
-
- $this->drupalGet('admin/structure/menu/add');
- $menu_name = substr(hash('sha256', $this->randomName(16)), 0, MENU_MAX_MENU_NAME_LENGTH_UI + 1);
- $title = $this->randomName(16);
- $edit = array(
- 'menu_name' => $menu_name,
- 'description' => '',
- 'title' => $title,
- );
- $this->drupalPost('admin/structure/menu/add', $edit, t('Save'));
-
- $this->assertRaw(t('!name cannot be longer than %max characters but is currently %length characters long.', array(
- '!name' => t('Menu name'),
- '%max' => MENU_MAX_MENU_NAME_LENGTH_UI,
- '%length' => drupal_strlen($menu_name),
- )));
-
- $menu_name = substr(hash('sha256', $this->randomName(16)), 0, MENU_MAX_MENU_NAME_LENGTH_UI);
- $edit['menu_name'] = $menu_name;
- $this->drupalPost('admin/structure/menu/add', $edit, t('Save'));
-
- $this->assertNoRaw(t('!name cannot be longer than %max characters but is currently %length characters long.', array(
- '!name' => t('Menu name'),
- '%max' => MENU_MAX_MENU_NAME_LENGTH_UI,
- '%length' => drupal_strlen($menu_name),
- )));
-
- $this->drupalGet('admin/structure/menu');
- $this->assertText($title, 'Menu created');
-
- $menu_name = 'menu-' . $menu_name;
- $edit = array();
- $edit['blocks[menu_' . $menu_name . '][region]'] = 'sidebar_first';
- $this->drupalPost('admin/structure/block', $edit, t('Save blocks'));
- $this->assertResponse(200);
- $this->assertText(t('The block settings have been updated.'), t('Custom menu block was enabled'));
- return menu_load($menu_name);
- }
-
- function deleteCustomMenu($menu) {
- $menu_name = $this->menu['menu_name'];
- $title = $this->menu['title'];
-
- $this->drupalPost("admin/structure/menu/manage/$menu_name/delete", array(), t('Delete'));
- $this->assertResponse(200);
- $this->assertRaw(t('The custom menu %title has been deleted.', array('%title' => $title)), t('Custom menu was deleted'));
- $this->assertFalse(menu_load($menu_name), 'Custom menu was deleted');
-
- $result = db_query("SELECT menu_name FROM {menu_links} WHERE menu_name = :menu_name", array(':menu_name' => $menu_name))->fetchField();
- $this->assertFalse($result, t('All menu links associated to the custom menu were deleted.'));
- }
-
- function doMenuTests($menu_name = 'navigation') {
-
- $node1 = $this->drupalCreateNode(array('type' => 'article'));
- $node2 = $this->drupalCreateNode(array('type' => 'article'));
- $node3 = $this->drupalCreateNode(array('type' => 'article'));
- $node4 = $this->drupalCreateNode(array('type' => 'article'));
- $node5 = $this->drupalCreateNode(array('type' => 'article'));
-
- $item1 = $this->addMenuLink(0, 'node/' . $node1->nid, $menu_name);
- $item2 = $this->addMenuLink($item1['mlid'], 'node/' . $node2->nid, $menu_name, FALSE);
- $item3 = $this->addMenuLink($item2['mlid'], 'node/' . $node3->nid, $menu_name);
- $this->assertMenuLink($item1['mlid'], array('depth' => 1, 'has_children' => 1, 'p1' => $item1['mlid'], 'p2' => 0));
- $this->assertMenuLink($item2['mlid'], array('depth' => 2, 'has_children' => 1, 'p1' => $item1['mlid'], 'p2' => $item2['mlid'], 'p3' => 0));
- $this->assertMenuLink($item3['mlid'], array('depth' => 3, 'has_children' => 0, 'p1' => $item1['mlid'], 'p2' => $item2['mlid'], 'p3' => $item3['mlid'], 'p4' => 0));
-
- $this->verifyMenuLink($item1, $node1);
- $this->verifyMenuLink($item2, $node2, $item1, $node1);
- $this->verifyMenuLink($item3, $node3, $item2, $node2);
-
- $item4 = $this->addMenuLink(0, 'node/' . $node4->nid, $menu_name);
- $item5 = $this->addMenuLink($item4['mlid'], 'node/' . $node5->nid, $menu_name);
- $this->assertMenuLink($item4['mlid'], array('depth' => 1, 'has_children' => 1, 'p1' => $item4['mlid'], 'p2' => 0));
- $this->assertMenuLink($item5['mlid'], array('depth' => 2, 'has_children' => 0, 'p1' => $item4['mlid'], 'p2' => $item5['mlid'], 'p3' => 0));
-
- $this->modifyMenuLink($item1);
- $this->modifyMenuLink($item2);
-
- $this->toggleMenuLink($item1);
- $this->toggleMenuLink($item2);
-
- $this->moveMenuLink($item2, $item5['mlid'], $menu_name);
- $this->assertMenuLink($item1['mlid'], array('depth' => 1, 'has_children' => 0, 'p1' => $item1['mlid'], 'p2' => 0));
- $this->assertMenuLink($item4['mlid'], array('depth' => 1, 'has_children' => 1, 'p1' => $item4['mlid'], 'p2' => 0));
- $this->assertMenuLink($item5['mlid'], array('depth' => 2, 'has_children' => 1, 'p1' => $item4['mlid'], 'p2' => $item5['mlid'], 'p3' => 0));
- $this->assertMenuLink($item2['mlid'], array('depth' => 3, 'has_children' => 1, 'p1' => $item4['mlid'], 'p2' => $item5['mlid'], 'p3' => $item2['mlid'], 'p4' => 0));
- $this->assertMenuLink($item3['mlid'], array('depth' => 4, 'has_children' => 0, 'p1' => $item4['mlid'], 'p2' => $item5['mlid'], 'p3' => $item2['mlid'], 'p4' => $item3['mlid'], 'p5' => 0));
-
- $this->disableMenuLink($item1);
- $edit = array();
-
-
- $edit['mlid:' . $item1['mlid'] . '[hidden]'] = TRUE;
- $this->drupalPost('admin/structure/menu/manage/' . $item1['menu_name'], $edit, t('Save configuration'));
-
- $this->assertMenuLink($item1['mlid'], array('hidden' => 0));
-
- $this->items[] = $item1;
- $this->items[] = $item2;
- }
-
- function testMenuQueryAndFragment() {
- $this->drupalLogin($this->big_user);
-
- $path = 'node?arg1=value1&arg2=value2';
- $item = $this->addMenuLink(0, $path);
- $this->drupalGet('admin/structure/menu/item/' . $item['mlid'] . '/edit');
- $this->assertFieldByName('link_path', $path, t('Path is found with both query and fragment.'));
-
- $path = 'node';
- $this->drupalPost('admin/structure/menu/item/' . $item['mlid'] . '/edit', array('link_path' => $path), t('Save'));
- $this->drupalGet('admin/structure/menu/item/' . $item['mlid'] . '/edit');
- $this->assertFieldByName('link_path', $path, t('Path no longer has query or fragment.'));
- }
-
- function addMenuLink($plid = 0, $link = '<front>', $menu_name = 'navigation', $expanded = TRUE) {
-
- $this->drupalGet("admin/structure/menu/manage/$menu_name/add");
- $this->assertResponse(200);
- $title = '!link_' . $this->randomName(16);
- $edit = array(
- 'link_path' => $link,
- 'link_title' => $title,
- 'description' => '',
- 'enabled' => TRUE,
- 'expanded' => $expanded,
- 'parent' => $menu_name . ':' . $plid,
- 'weight' => '0',
- );
-
- $this->drupalPost(NULL, $edit, t('Save'));
- $this->assertResponse(200);
-
- $this->assertText($title, 'Menu link was added');
- $item = db_query('SELECT * FROM {menu_links} WHERE link_title = :title', array(':title' => $title))->fetchAssoc();
- $this->assertTrue(t('Menu link was found in database.'));
- $this->assertMenuLink($item['mlid'], array('menu_name' => $menu_name, 'link_path' => $link, 'has_children' => 0, 'plid' => $plid));
- return $item;
- }
-
- function addInvalidMenuLink($menu_name = 'navigation') {
- foreach (array('-&-', 'admin/people/permissions') as $link_path) {
- $edit = array(
- 'link_path' => $link_path,
- 'link_title' => 'title',
- );
- $this->drupalPost("admin/structure/menu/manage/$menu_name/add", $edit, t('Save'));
- $this->assertRaw(t("The path '@path' is either invalid or you do not have access to it.", array('@path' => $link_path)), 'Menu link was not created');
- }
- }
-
- function verifyMenuLink($item, $item_node, $parent = NULL, $parent_node = NULL) {
-
- $this->drupalGet('');
- $this->assertResponse(200);
-
- if (isset($parent)) {
-
- $title = $parent['link_title'];
- $this->assertLink($title, 0, 'Parent menu link was displayed');
-
- $this->clickLink($title);
- $title = $parent_node->title;
- $this->assertTitle(t("@title | Drupal", array('@title' => $title)), t('Parent menu link link target was correct'));
- }
-
- $title = $item['link_title'];
- $this->assertLink($title, 0, 'Menu link was displayed');
-
- $this->clickLink($title);
- $title = $item_node->title;
- $this->assertTitle(t("@title | Drupal", array('@title' => $title)), t('Menu link link target was correct'));
- }
-
- function moveMenuLink($item, $plid, $menu_name) {
- $mlid = $item['mlid'];
- $edit = array(
- 'parent' => $menu_name . ':' . $plid,
- );
- $this->drupalPost("admin/structure/menu/item/$mlid/edit", $edit, t('Save'));
- $this->assertResponse(200);
- }
-
- function modifyMenuLink(&$item) {
- $item['link_title'] = $this->randomName(16);
- $mlid = $item['mlid'];
- $title = $item['link_title'];
-
- $edit = array();
- $edit['link_title'] = $title;
- $this->drupalPost("admin/structure/menu/item/$mlid/edit", $edit, t('Save'));
- $this->assertResponse(200);
-
-
- $this->drupalGet('admin/structure/menu/manage/' . $item['menu_name']);
- $this->assertText($title, 'Menu link was edited');
- }
-
- function resetMenuLink($item, $old_title) {
- $mlid = $item['mlid'];
- $title = $item['link_title'];
-
- $this->drupalPost("admin/structure/menu/item/$mlid/reset", array(), t('Reset'));
- $this->assertResponse(200);
- $this->assertRaw(t('The menu link was reset to its default settings.'), t('Menu link was reset'));
-
- $this->drupalGet('');
- $this->assertNoText($title, 'Menu link was reset');
- $this->assertText($old_title, 'Menu link was reset');
- }
-
- function deleteMenuLink($item) {
- $mlid = $item['mlid'];
- $title = $item['link_title'];
-
- $this->drupalPost("admin/structure/menu/item/$mlid/delete", array(), t('Confirm'));
- $this->assertResponse(200);
- $this->assertRaw(t('The menu link %title has been deleted.', array('%title' => $title)), t('Menu link was deleted'));
-
- $this->drupalGet('');
- $this->assertNoText($title, 'Menu link was deleted');
- }
-
- function toggleMenuLink($item) {
- $this->disableMenuLink($item);
-
- $this->drupalGet('');
- $this->assertNoText($item['link_title'], 'Menu link was not displayed');
- $this->enableMenuLink($item);
-
- $this->drupalGet('');
- $this->assertText($item['link_title'], 'Menu link was displayed');
- }
-
- function disableMenuLink($item) {
- $mlid = $item['mlid'];
- $edit['enabled'] = FALSE;
- $this->drupalPost("admin/structure/menu/item/$mlid/edit", $edit, t('Save'));
-
-
- $this->assertMenuLink($mlid, array('hidden' => 1));
- }
-
- function enableMenuLink($item) {
- $mlid = $item['mlid'];
- $edit['enabled'] = TRUE;
- $this->drupalPost("admin/structure/menu/item/$mlid/edit", $edit, t('Save'));
-
- $this->assertMenuLink($mlid, array('hidden' => 0));
- }
-
- function assertMenuLink($mlid, array $expected_item) {
-
- $item = db_query('SELECT * FROM {menu_links} WHERE mlid = :mlid', array(':mlid' => $mlid))->fetchAssoc();
- $options = unserialize($item['options']);
- if (!empty($options['query'])) {
- $item['link_path'] .= '?' . drupal_http_build_query($options['query']);
- }
- if (!empty($options['fragment'])) {
- $item['link_path'] .= '#' . $options['fragment'];
- }
- foreach ($expected_item as $key => $value) {
- $this->assertEqual($item[$key], $value, t('Parameter %key had expected value.', array('%key' => $key)));
- }
- }
-
- private function getStandardMenuLink() {
-
- $mlid = db_query("SELECT mlid FROM {menu_links} WHERE module = 'system' AND router_path = 'user/logout'")->fetchField();
- $this->assertTrue($mlid > 0, 'Standard menu link id was found');
-
-
- $item = menu_link_load($mlid);
- $this->assertTrue((bool) $item, 'Standard menu link was loaded');
- return $item;
- }
-
- private function verifyAccess($response = 200) {
-
- $this->drupalGet('admin/help/menu');
- $this->assertResponse($response);
- if ($response == 200) {
- $this->assertText(t('Menu'), t('Menu help was displayed'));
- }
-
- $this->drupalGet('admin/structure/menu');
- $this->assertResponse($response);
- if ($response == 200) {
- $this->assertText(t('Menus'), t('Menu build overview node was displayed'));
- }
-
- $this->drupalGet('admin/structure/menu/manage/navigation');
- $this->assertResponse($response);
- if ($response == 200) {
- $this->assertText(t('Navigation'), t('Navigation menu node was displayed'));
- }
-
- $item = $this->getStandardMenuLink();
- $this->drupalGet('admin/structure/menu/item/' . $item['mlid'] . '/edit');
- $this->assertResponse($response);
- if ($response == 200) {
- $this->assertText(t('Edit menu item'), t('Menu edit node was displayed'));
- }
-
- $this->drupalGet('admin/structure/menu/settings');
- $this->assertResponse($response);
- if ($response == 200) {
- $this->assertText(t('Menus'), t('Menu settings node was displayed'));
- }
-
- $this->drupalGet('admin/structure/menu/add');
- $this->assertResponse($response);
- if ($response == 200) {
- $this->assertText(t('Menus'), t('Add menu node was displayed'));
- }
- }
- }
- class MenuNodeTestCase extends DrupalWebTestCase {
- public static function getInfo() {
- return array(
- 'name' => 'Menu settings for nodes',
- 'description' => 'Add, edit, and delete a node with menu link.',
- 'group' => 'Menu',
- );
- }
- function setUp() {
- parent::setUp('menu');
- $this->admin_user = $this->drupalCreateUser(array(
- 'access administration pages',
- 'administer content types',
- 'administer menu',
- 'create page content',
- 'edit any page content',
- 'delete any page content',
- ));
- $this->drupalLogin($this->admin_user);
- }
-
- function testMenuNodeFormWidget() {
-
- $edit = array(
- 'menu_options[navigation]' => 1,
- );
- $this->drupalPost('admin/structure/types/manage/page', $edit, t('Save content type'));
-
-
- $edit = array(
- 'menu_parent' => 'navigation:0',
- );
- $this->drupalPost('admin/structure/types/manage/page', $edit, t('Save content type'));
-
- $node_title = $this->randomName();
- $language = LANGUAGE_NONE;
- $edit = array(
- "title" => $node_title,
- "body[$language][0][value]" => $this->randomString(),
- );
- $this->drupalPost('node/add/page', $edit, t('Save'));
- $node = $this->drupalGetNodeByTitle($node_title);
-
- $this->drupalGet('');
- $this->assertNoLink($node_title);
-
- $edit = array(
- 'menu[enabled]' => 1,
- );
- $this->drupalPost('node/' . $node->nid . '/edit', $edit, t('Save'));
-
- $this->drupalGet('');
- $this->assertNoLink($node_title);
-
- $edit = array(
- 'menu[enabled]' => 1,
- 'menu[link_title]' => $node_title,
- 'menu[weight]' => 17,
- );
- $this->drupalPost('node/' . $node->nid . '/edit', $edit, t('Save'));
-
- $this->drupalGet('');
- $this->assertLink($node_title);
- $this->drupalGet('node/' . $node->nid . '/edit');
- $this->assertOptionSelected('edit-menu-weight', 17, t('Menu weight correct in edit form'));
-
- $edit = array(
- 'menu[enabled]' => FALSE,
- );
- $this->drupalPost('node/' . $node->nid . '/edit', $edit, t('Save'));
-
- $this->drupalGet('');
- $this->assertNoLink($node_title);
-
- $item = array(
- 'link_path' => 'node/' . $node->nid,
- 'link_title' => $this->randomName(16),
- 'menu_name' => 'management',
- );
- menu_link_save($item);
-
- $this->drupalGet('node/' . $node->nid . '/edit');
- $this->assertText('Provide a menu link', t('Link in not allowed menu not shown in node edit form'));
-
- $this->drupalPost('node/' . $node->nid . '/edit', $edit, t('Save'));
- $link = menu_link_load($item['mlid']);
- $this->assertTrue($link, t('Link in not allowed menu still exists after saving node'));
-
- $item['menu_name'] = 'navigation';
- menu_link_save($item);
-
- $child_node = $this->drupalCreateNode(array('type' => 'article'));
-
- $child_item = array(
- 'link_path' => 'node/'. $child_node->nid,
- 'link_title' => $this->randomName(16),
- 'plid' => $item['mlid'],
- );
- menu_link_save($child_item);
-
- $this->drupalGet('node/'. $node->nid .'/edit');
-
- $this->assertNoOption('edit-menu-parent', 'navigation:'. $item['mlid']);
- $this->assertNoOption('edit-menu-parent', 'navigation:'. $child_item['mlid']);
-
- $this->assertNoOption('edit-menu-parent', 'management:0');
- }
-
- protected function assertNoOption($id, $option, $message = '') {
- $selects = $this->xpath('//select[@id=:id]', array(':id' => $id));
- $options = $this->xpath('//select[@id=:id]//option[@value=:option]', array(':id' => $id, ':option' => $option));
- return $this->assertTrue(isset($selects[0]) && !isset($options[0]), $message ? $message : t('Option @option for field @id does not exist.', array('@option' => $option, '@id' => $id)), t('Browser'));
- }
- }
|