cms/drupal/modules/menu/menu.test
changeset 541 e756a8c72c3d
equal deleted inserted replaced
540:07239de796bb 541:e756a8c72c3d
       
     1 <?php
       
     2 
       
     3 /**
       
     4  * @file
       
     5  * Tests for menu.module.
       
     6  */
       
     7 
       
     8 class MenuTestCase extends DrupalWebTestCase {
       
     9   protected $big_user;
       
    10   protected $std_user;
       
    11   protected $menu;
       
    12   protected $items;
       
    13 
       
    14   public static function getInfo() {
       
    15     return array(
       
    16       'name' => 'Menu link creation/deletion',
       
    17       '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.',
       
    18       'group' => 'Menu'
       
    19     );
       
    20   }
       
    21 
       
    22   function setUp() {
       
    23     parent::setUp('menu');
       
    24     // Create users.
       
    25     $this->big_user = $this->drupalCreateUser(array('access administration pages', 'administer blocks', 'administer menu', 'create article content'));
       
    26     $this->std_user = $this->drupalCreateUser(array());
       
    27   }
       
    28 
       
    29   /**
       
    30    * Login users, add menus and menu links, and test menu functionality through the admin and user interfaces.
       
    31    */
       
    32   function testMenu() {
       
    33     // Login the user.
       
    34     $this->drupalLogin($this->big_user);
       
    35     $this->items = array();
       
    36 
       
    37     // Do standard menu tests.
       
    38     $this->doStandardMenuTests();
       
    39 
       
    40     // Do custom menu tests.
       
    41     $this->doCustomMenuTests();
       
    42 
       
    43     // Do standard user tests.
       
    44     // Login the user.
       
    45     $this->drupalLogin($this->std_user);
       
    46     $this->verifyAccess(403);
       
    47     foreach ($this->items as $item) {
       
    48       $node = node_load(substr($item['link_path'], 5)); // Paths were set as 'node/$nid'.
       
    49       $this->verifyMenuLink($item, $node);
       
    50     }
       
    51 
       
    52     // Login the user.
       
    53     $this->drupalLogin($this->big_user);
       
    54 
       
    55     // Delete menu links.
       
    56     foreach ($this->items as $item) {
       
    57       $this->deleteMenuLink($item);
       
    58     }
       
    59 
       
    60     // Delete custom menu.
       
    61     $this->deleteCustomMenu($this->menu);
       
    62 
       
    63     // Modify and reset a standard menu link.
       
    64     $item = $this->getStandardMenuLink();
       
    65     $old_title = $item['link_title'];
       
    66     $this->modifyMenuLink($item);
       
    67     $item = menu_link_load($item['mlid']);
       
    68     // Verify that a change to the description is saved.
       
    69     $description = $this->randomName(16);
       
    70     $item['options']['attributes']['title']  = $description;
       
    71     menu_link_save($item);
       
    72     $saved_item = menu_link_load($item['mlid']);
       
    73     $this->assertEqual($description, $saved_item['options']['attributes']['title'], 'Saving an existing link updates the description (title attribute)');
       
    74     $this->resetMenuLink($item, $old_title);
       
    75 
       
    76     // Test that the page title is correct when a local task appears in a
       
    77     // top-level menu item. See https://www.drupal.org/node/1973262.
       
    78     $item = $this->addMenuLink(0, 'user/register', 'user-menu');
       
    79     $this->drupalGet('user/password');
       
    80     $this->assertNoTitle('Home | Drupal');
       
    81     $this->drupalLogout();
       
    82     $this->drupalGet('user/register');
       
    83     $this->assertTitle($item['link_title'] . ' | Drupal');
       
    84     $this->drupalGet('user');
       
    85     $this->assertNoTitle('Home | Drupal');
       
    86   }
       
    87 
       
    88   /**
       
    89    * Test standard menu functionality using navigation menu.
       
    90    *
       
    91    */
       
    92   function doStandardMenuTests() {
       
    93     $this->doMenuTests();
       
    94     $this->addInvalidMenuLink();
       
    95   }
       
    96 
       
    97   /**
       
    98    * Test custom menu functionality using navigation menu.
       
    99    *
       
   100    */
       
   101   function doCustomMenuTests() {
       
   102     $this->menu = $this->addCustomMenu();
       
   103     $this->doMenuTests($this->menu['menu_name']);
       
   104     $this->addInvalidMenuLink($this->menu['menu_name']);
       
   105     $this->addCustomMenuCRUD();
       
   106   }
       
   107 
       
   108   /**
       
   109    * Add custom menu using CRUD functions.
       
   110    */
       
   111   function addCustomMenuCRUD() {
       
   112     // Add a new custom menu.
       
   113     $menu_name = substr(hash('sha256', $this->randomName(16)), 0, MENU_MAX_MENU_NAME_LENGTH_UI);
       
   114     $title = $this->randomName(16);
       
   115 
       
   116     $menu = array(
       
   117       'menu_name' => $menu_name,
       
   118       'title' => $title,
       
   119       'description' => 'Description text',
       
   120     );
       
   121     menu_save($menu);
       
   122 
       
   123     // Assert the new menu.
       
   124     $this->drupalGet('admin/structure/menu/manage/' . $menu_name . '/edit');
       
   125     $this->assertRaw($title, 'Custom menu was added.');
       
   126 
       
   127     // Edit the menu.
       
   128     $new_title = $this->randomName(16);
       
   129     $menu['title'] = $new_title;
       
   130     menu_save($menu);
       
   131     $this->drupalGet('admin/structure/menu/manage/' . $menu_name . '/edit');
       
   132     $this->assertRaw($new_title, 'Custom menu was edited.');
       
   133   }
       
   134 
       
   135   /**
       
   136    * Add custom menu.
       
   137    */
       
   138   function addCustomMenu() {
       
   139     // Add custom menu.
       
   140 
       
   141     // Try adding a menu using a menu_name that is too long.
       
   142     $this->drupalGet('admin/structure/menu/add');
       
   143     $menu_name = substr(hash('sha256', $this->randomName(16)), 0, MENU_MAX_MENU_NAME_LENGTH_UI + 1);
       
   144     $title = $this->randomName(16);
       
   145     $edit = array(
       
   146       'menu_name' => $menu_name,
       
   147       'description' => '',
       
   148       'title' =>  $title,
       
   149     );
       
   150     $this->drupalPost('admin/structure/menu/add', $edit, t('Save'));
       
   151 
       
   152     // Verify that using a menu_name that is too long results in a validation message.
       
   153     $this->assertRaw(t('!name cannot be longer than %max characters but is currently %length characters long.', array(
       
   154       '!name' => t('Menu name'),
       
   155       '%max' => MENU_MAX_MENU_NAME_LENGTH_UI,
       
   156       '%length' => drupal_strlen($menu_name),
       
   157     )));
       
   158 
       
   159     // Change the menu_name so it no longer exceeds the maximum length.
       
   160     $menu_name = substr(hash('sha256', $this->randomName(16)), 0, MENU_MAX_MENU_NAME_LENGTH_UI);
       
   161     $edit['menu_name'] = $menu_name;
       
   162     $this->drupalPost('admin/structure/menu/add', $edit, t('Save'));
       
   163 
       
   164     // Verify that no validation error is given for menu_name length.
       
   165     $this->assertNoRaw(t('!name cannot be longer than %max characters but is currently %length characters long.', array(
       
   166       '!name' => t('Menu name'),
       
   167       '%max' => MENU_MAX_MENU_NAME_LENGTH_UI,
       
   168       '%length' => drupal_strlen($menu_name),
       
   169     )));
       
   170     // Unlike most other modules, there is no confirmation message displayed.
       
   171 
       
   172     $this->drupalGet('admin/structure/menu');
       
   173     $this->assertText($title, 'Menu created');
       
   174 
       
   175     // Enable the custom menu block.
       
   176     $menu_name = 'menu-' . $menu_name; // Drupal prepends the name with 'menu-'.
       
   177     $edit = array();
       
   178     $edit['blocks[menu_' . $menu_name . '][region]'] = 'sidebar_first';
       
   179     $this->drupalPost('admin/structure/block', $edit, t('Save blocks'));
       
   180     $this->assertResponse(200);
       
   181     $this->assertText(t('The block settings have been updated.'), 'Custom menu block was enabled');
       
   182 
       
   183     return menu_load($menu_name);
       
   184   }
       
   185 
       
   186   /**
       
   187    * Delete custom menu.
       
   188    *
       
   189    * @param string $menu_name Custom menu name.
       
   190    */
       
   191   function deleteCustomMenu($menu) {
       
   192     $menu_name = $this->menu['menu_name'];
       
   193     $title = $this->menu['title'];
       
   194 
       
   195     // Delete custom menu.
       
   196     $this->drupalPost("admin/structure/menu/manage/$menu_name/delete", array(), t('Delete'));
       
   197     $this->assertResponse(200);
       
   198     $this->assertRaw(t('The custom menu %title has been deleted.', array('%title' => $title)), 'Custom menu was deleted');
       
   199     $this->assertFalse(menu_load($menu_name), 'Custom menu was deleted');
       
   200     // Test if all menu links associated to the menu were removed from database.
       
   201     $result = db_query("SELECT menu_name FROM {menu_links} WHERE menu_name = :menu_name", array(':menu_name' => $menu_name))->fetchField();
       
   202     $this->assertFalse($result, 'All menu links associated to the custom menu were deleted.');
       
   203   }
       
   204 
       
   205   /**
       
   206    * Test menu functionality using navigation menu.
       
   207    *
       
   208    */
       
   209   function doMenuTests($menu_name = 'navigation') {
       
   210     // Add nodes to use as links for menu links.
       
   211     $node1 = $this->drupalCreateNode(array('type' => 'article'));
       
   212     $node2 = $this->drupalCreateNode(array('type' => 'article'));
       
   213     $node3 = $this->drupalCreateNode(array('type' => 'article'));
       
   214     $node4 = $this->drupalCreateNode(array('type' => 'article'));
       
   215     $node5 = $this->drupalCreateNode(array('type' => 'article'));
       
   216 
       
   217     // Add menu links.
       
   218     $item1 = $this->addMenuLink(0, 'node/' . $node1->nid, $menu_name);
       
   219     $item2 = $this->addMenuLink($item1['mlid'], 'node/' . $node2->nid, $menu_name, FALSE);
       
   220     $item3 = $this->addMenuLink($item2['mlid'], 'node/' . $node3->nid, $menu_name);
       
   221     $this->assertMenuLink($item1['mlid'], array('depth' => 1, 'has_children' => 1, 'p1' => $item1['mlid'], 'p2' => 0));
       
   222     $this->assertMenuLink($item2['mlid'], array('depth' => 2, 'has_children' => 1, 'p1' => $item1['mlid'], 'p2' => $item2['mlid'], 'p3' => 0));
       
   223     $this->assertMenuLink($item3['mlid'], array('depth' => 3, 'has_children' => 0, 'p1' => $item1['mlid'], 'p2' => $item2['mlid'], 'p3' => $item3['mlid'], 'p4' => 0));
       
   224 
       
   225     // Verify menu links.
       
   226     $this->verifyMenuLink($item1, $node1);
       
   227     $this->verifyMenuLink($item2, $node2, $item1, $node1);
       
   228     $this->verifyMenuLink($item3, $node3, $item2, $node2);
       
   229 
       
   230     // Add more menu links.
       
   231     $item4 = $this->addMenuLink(0, 'node/' . $node4->nid, $menu_name);
       
   232     $item5 = $this->addMenuLink($item4['mlid'], 'node/' . $node5->nid, $menu_name);
       
   233     $this->assertMenuLink($item4['mlid'], array('depth' => 1, 'has_children' => 1, 'p1' => $item4['mlid'], 'p2' => 0));
       
   234     $this->assertMenuLink($item5['mlid'], array('depth' => 2, 'has_children' => 0, 'p1' => $item4['mlid'], 'p2' => $item5['mlid'], 'p3' => 0));
       
   235 
       
   236     // Modify menu links.
       
   237     $this->modifyMenuLink($item1);
       
   238     $this->modifyMenuLink($item2);
       
   239 
       
   240     // Toggle menu links.
       
   241     $this->toggleMenuLink($item1);
       
   242     $this->toggleMenuLink($item2);
       
   243 
       
   244     // Move link and verify that descendants are updated.
       
   245     $this->moveMenuLink($item2, $item5['mlid'], $menu_name);
       
   246     $this->assertMenuLink($item1['mlid'], array('depth' => 1, 'has_children' => 0, 'p1' => $item1['mlid'], 'p2' => 0));
       
   247     $this->assertMenuLink($item4['mlid'], array('depth' => 1, 'has_children' => 1, 'p1' => $item4['mlid'], 'p2' => 0));
       
   248     $this->assertMenuLink($item5['mlid'], array('depth' => 2, 'has_children' => 1, 'p1' => $item4['mlid'], 'p2' => $item5['mlid'], 'p3' => 0));
       
   249     $this->assertMenuLink($item2['mlid'], array('depth' => 3, 'has_children' => 1, 'p1' => $item4['mlid'], 'p2' => $item5['mlid'], 'p3' => $item2['mlid'], 'p4' => 0));
       
   250     $this->assertMenuLink($item3['mlid'], array('depth' => 4, 'has_children' => 0, 'p1' => $item4['mlid'], 'p2' => $item5['mlid'], 'p3' => $item2['mlid'], 'p4' => $item3['mlid'], 'p5' => 0));
       
   251 
       
   252     // Enable a link via the overview form.
       
   253     $this->disableMenuLink($item1);
       
   254     $edit = array();
       
   255 
       
   256     // Note in the UI the 'mlid:x[hidden]' form element maps to enabled, or
       
   257     // NOT hidden.
       
   258     $edit['mlid:' . $item1['mlid'] . '[hidden]'] = TRUE;
       
   259     $this->drupalPost('admin/structure/menu/manage/' . $item1['menu_name'], $edit, t('Save configuration'));
       
   260 
       
   261     // Verify in the database.
       
   262     $this->assertMenuLink($item1['mlid'], array('hidden' => 0));
       
   263 
       
   264     // Save menu links for later tests.
       
   265     $this->items[] = $item1;
       
   266     $this->items[] = $item2;
       
   267   }
       
   268 
       
   269   /**
       
   270    * Add and remove a menu link with a query string and fragment.
       
   271    */
       
   272   function testMenuQueryAndFragment() {
       
   273     $this->drupalLogin($this->big_user);
       
   274 
       
   275     // Make a path with query and fragment on.
       
   276     $path = 'node?arg1=value1&arg2=value2';
       
   277     $item = $this->addMenuLink(0, $path);
       
   278 
       
   279     $this->drupalGet('admin/structure/menu/item/' . $item['mlid'] . '/edit');
       
   280     $this->assertFieldByName('link_path', $path, 'Path is found with both query and fragment.');
       
   281 
       
   282     // Now change the path to something without query and fragment.
       
   283     $path = 'node';
       
   284     $this->drupalPost('admin/structure/menu/item/' . $item['mlid'] . '/edit', array('link_path' => $path), t('Save'));
       
   285     $this->drupalGet('admin/structure/menu/item/' . $item['mlid'] . '/edit');
       
   286     $this->assertFieldByName('link_path', $path, 'Path no longer has query or fragment.');
       
   287   }
       
   288 
       
   289   /**
       
   290    * Add a menu link using the menu module UI.
       
   291    *
       
   292    * @param integer $plid Parent menu link id.
       
   293    * @param string $link Link path.
       
   294    * @param string $menu_name Menu name.
       
   295    * @return array Menu link created.
       
   296    */
       
   297   function addMenuLink($plid = 0, $link = '<front>', $menu_name = 'navigation', $expanded = TRUE) {
       
   298     // View add menu link page.
       
   299     $this->drupalGet("admin/structure/menu/manage/$menu_name/add");
       
   300     $this->assertResponse(200);
       
   301 
       
   302     $title = '!link_' . $this->randomName(16);
       
   303     $edit = array(
       
   304       'link_path' => $link,
       
   305       'link_title' => $title,
       
   306       'description' => '',
       
   307       'enabled' => TRUE, // Use this to disable the menu and test.
       
   308       'expanded' => $expanded, // Setting this to true should test whether it works when we do the std_user tests.
       
   309       'parent' =>  $menu_name . ':' . $plid,
       
   310       'weight' => '0',
       
   311     );
       
   312 
       
   313     // Add menu link.
       
   314     $this->drupalPost(NULL, $edit, t('Save'));
       
   315     $this->assertResponse(200);
       
   316     // Unlike most other modules, there is no confirmation message displayed.
       
   317     $this->assertText($title, 'Menu link was added');
       
   318 
       
   319     $item = db_query('SELECT * FROM {menu_links} WHERE link_title = :title', array(':title' => $title))->fetchAssoc();
       
   320     $this->assertTrue(t('Menu link was found in database.'));
       
   321     $this->assertMenuLink($item['mlid'], array('menu_name' => $menu_name, 'link_path' => $link, 'has_children' => 0, 'plid' => $plid));
       
   322 
       
   323     return $item;
       
   324   }
       
   325 
       
   326   /**
       
   327    * Attempt to add menu link with invalid path or no access permission.
       
   328    *
       
   329    * @param string $menu_name Menu name.
       
   330    */
       
   331   function addInvalidMenuLink($menu_name = 'navigation') {
       
   332     foreach (array('-&-', 'admin/people/permissions', '#') as $link_path) {
       
   333       $edit = array(
       
   334         'link_path' => $link_path,
       
   335         'link_title' => 'title',
       
   336       );
       
   337       $this->drupalPost("admin/structure/menu/manage/$menu_name/add", $edit, t('Save'));
       
   338       $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');
       
   339     }
       
   340   }
       
   341 
       
   342   /**
       
   343    * Verify a menu link using the menu module UI.
       
   344    *
       
   345    * @param array $item Menu link.
       
   346    * @param object $item_node Menu link content node.
       
   347    * @param array $parent Parent menu link.
       
   348    * @param object $parent_node Parent menu link content node.
       
   349    */
       
   350   function verifyMenuLink($item, $item_node, $parent = NULL, $parent_node = NULL) {
       
   351     // View home page.
       
   352     $this->drupalGet('');
       
   353     $this->assertResponse(200);
       
   354 
       
   355     // Verify parent menu link.
       
   356     if (isset($parent)) {
       
   357       // Verify menu link.
       
   358       $title = $parent['link_title'];
       
   359       $this->assertLink($title, 0, 'Parent menu link was displayed');
       
   360 
       
   361       // Verify menu link link.
       
   362       $this->clickLink($title);
       
   363       $title = $parent_node->title;
       
   364       $this->assertTitle(t("@title | Drupal", array('@title' => $title)), 'Parent menu link link target was correct');
       
   365     }
       
   366 
       
   367     // Verify menu link.
       
   368     $title = $item['link_title'];
       
   369     $this->assertLink($title, 0, 'Menu link was displayed');
       
   370 
       
   371     // Verify menu link link.
       
   372     $this->clickLink($title);
       
   373     $title = $item_node->title;
       
   374     $this->assertTitle(t("@title | Drupal", array('@title' => $title)), 'Menu link link target was correct');
       
   375   }
       
   376 
       
   377   /**
       
   378    * Change the parent of a menu link using the menu module UI.
       
   379    */
       
   380   function moveMenuLink($item, $plid, $menu_name) {
       
   381     $mlid = $item['mlid'];
       
   382 
       
   383     $edit = array(
       
   384       'parent' => $menu_name . ':' . $plid,
       
   385     );
       
   386     $this->drupalPost("admin/structure/menu/item/$mlid/edit", $edit, t('Save'));
       
   387     $this->assertResponse(200);
       
   388   }
       
   389 
       
   390   /**
       
   391    * Modify a menu link using the menu module UI.
       
   392    *
       
   393    * @param array $item Menu link passed by reference.
       
   394    */
       
   395   function modifyMenuLink(&$item) {
       
   396     $item['link_title'] = $this->randomName(16);
       
   397 
       
   398     $mlid = $item['mlid'];
       
   399     $title = $item['link_title'];
       
   400 
       
   401     // Edit menu link.
       
   402     $edit = array();
       
   403     $edit['link_title'] = $title;
       
   404     $this->drupalPost("admin/structure/menu/item/$mlid/edit", $edit, t('Save'));
       
   405     $this->assertResponse(200);
       
   406     // Unlike most other modules, there is no confirmation message displayed.
       
   407 
       
   408     // Verify menu link.
       
   409     $this->drupalGet('admin/structure/menu/manage/' . $item['menu_name']);
       
   410     $this->assertText($title, 'Menu link was edited');
       
   411   }
       
   412 
       
   413   /**
       
   414    * Reset a standard menu link using the menu module UI.
       
   415    *
       
   416    * @param array $item Menu link.
       
   417    * @param string $old_title Original title for menu link.
       
   418    */
       
   419   function resetMenuLink($item, $old_title) {
       
   420     $mlid = $item['mlid'];
       
   421     $title = $item['link_title'];
       
   422 
       
   423     // Reset menu link.
       
   424     $this->drupalPost("admin/structure/menu/item/$mlid/reset", array(), t('Reset'));
       
   425     $this->assertResponse(200);
       
   426     $this->assertRaw(t('The menu link was reset to its default settings.'), 'Menu link was reset');
       
   427 
       
   428     // Verify menu link.
       
   429     $this->drupalGet('');
       
   430     $this->assertNoText($title, 'Menu link was reset');
       
   431     $this->assertText($old_title, 'Menu link was reset');
       
   432   }
       
   433 
       
   434   /**
       
   435    * Delete a menu link using the menu module UI.
       
   436    *
       
   437    * @param array $item Menu link.
       
   438    */
       
   439   function deleteMenuLink($item) {
       
   440     $mlid = $item['mlid'];
       
   441     $title = $item['link_title'];
       
   442 
       
   443     // Delete menu link.
       
   444     $this->drupalPost("admin/structure/menu/item/$mlid/delete", array(), t('Confirm'));
       
   445     $this->assertResponse(200);
       
   446     $this->assertRaw(t('The menu link %title has been deleted.', array('%title' => $title)), 'Menu link was deleted');
       
   447 
       
   448     // Verify deletion.
       
   449     $this->drupalGet('');
       
   450     $this->assertNoText($title, 'Menu link was deleted');
       
   451   }
       
   452 
       
   453   /**
       
   454    * Alternately disable and enable a menu link.
       
   455    *
       
   456    * @param $item
       
   457    *   Menu link.
       
   458    */
       
   459   function toggleMenuLink($item) {
       
   460     $this->disableMenuLink($item);
       
   461 
       
   462     // Verify menu link is absent.
       
   463     $this->drupalGet('');
       
   464     $this->assertNoText($item['link_title'], 'Menu link was not displayed');
       
   465     $this->enableMenuLink($item);
       
   466 
       
   467     // Verify menu link is displayed.
       
   468     $this->drupalGet('');
       
   469     $this->assertText($item['link_title'], 'Menu link was displayed');
       
   470   }
       
   471 
       
   472   /**
       
   473    * Disable a menu link.
       
   474    *
       
   475    * @param $item
       
   476    *   Menu link.
       
   477    */
       
   478   function disableMenuLink($item) {
       
   479     $mlid = $item['mlid'];
       
   480     $edit['enabled'] = FALSE;
       
   481     $this->drupalPost("admin/structure/menu/item/$mlid/edit", $edit, t('Save'));
       
   482 
       
   483     // Unlike most other modules, there is no confirmation message displayed.
       
   484     // Verify in the database.
       
   485     $this->assertMenuLink($mlid, array('hidden' => 1));
       
   486   }
       
   487 
       
   488   /**
       
   489    * Enable a menu link.
       
   490    *
       
   491    * @param $item
       
   492    *   Menu link.
       
   493    */
       
   494   function enableMenuLink($item) {
       
   495     $mlid = $item['mlid'];
       
   496     $edit['enabled'] = TRUE;
       
   497     $this->drupalPost("admin/structure/menu/item/$mlid/edit", $edit, t('Save'));
       
   498 
       
   499     // Verify in the database.
       
   500     $this->assertMenuLink($mlid, array('hidden' => 0));
       
   501   }
       
   502 
       
   503   /**
       
   504    * Fetch the menu item from the database and compare it to the specified
       
   505    * array.
       
   506    *
       
   507    * @param $mlid
       
   508    *   Menu item id.
       
   509    * @param $item
       
   510    *   Array containing properties to verify.
       
   511    */
       
   512   function assertMenuLink($mlid, array $expected_item) {
       
   513     // Retrieve menu link.
       
   514     $item = db_query('SELECT * FROM {menu_links} WHERE mlid = :mlid', array(':mlid' => $mlid))->fetchAssoc();
       
   515     $options = unserialize($item['options']);
       
   516     if (!empty($options['query'])) {
       
   517       $item['link_path'] .= '?' . drupal_http_build_query($options['query']);
       
   518     }
       
   519     if (!empty($options['fragment'])) {
       
   520       $item['link_path'] .= '#' . $options['fragment'];
       
   521     }
       
   522     foreach ($expected_item as $key => $value) {
       
   523       $this->assertEqual($item[$key], $value, format_string('Parameter %key had expected value.', array('%key' => $key)));
       
   524     }
       
   525   }
       
   526 
       
   527   /**
       
   528    * Test administrative users other than user 1 can access the menu parents AJAX callback.
       
   529    */
       
   530   public function testMenuParentsJsAccess() {
       
   531     $admin = $this->drupalCreateUser(array('administer menu'));
       
   532     $this->drupalLogin($admin);
       
   533     // Just check access to the callback overall, the POST data is irrelevant.
       
   534     $this->drupalGetAJAX('admin/structure/menu/parents');
       
   535     $this->assertResponse(200);
       
   536 
       
   537     // Do standard user tests.
       
   538     // Login the user.
       
   539     $this->drupalLogin($this->std_user);
       
   540     $this->drupalGetAJAX('admin/structure/menu/parents');
       
   541     $this->assertResponse(403);
       
   542   }
       
   543 
       
   544   /**
       
   545    * Get standard menu link.
       
   546    */
       
   547   private function getStandardMenuLink() {
       
   548     // Retrieve menu link id of the Log out menu link, which will always be on the front page.
       
   549     $mlid = db_query("SELECT mlid FROM {menu_links} WHERE module = 'system' AND router_path = 'user/logout'")->fetchField();
       
   550     $this->assertTrue($mlid > 0, 'Standard menu link id was found');
       
   551     // Load menu link.
       
   552     // Use api function so that link is translated for rendering.
       
   553     $item = menu_link_load($mlid);
       
   554     $this->assertTrue((bool) $item, 'Standard menu link was loaded');
       
   555     return $item;
       
   556   }
       
   557 
       
   558   /**
       
   559    * Verify the logged in user has the desired access to the various menu nodes.
       
   560    *
       
   561    * @param integer $response HTTP response code.
       
   562    */
       
   563   private function verifyAccess($response = 200) {
       
   564     // View menu help node.
       
   565     $this->drupalGet('admin/help/menu');
       
   566     $this->assertResponse($response);
       
   567     if ($response == 200) {
       
   568       $this->assertText(t('Menu'), 'Menu help was displayed');
       
   569     }
       
   570 
       
   571     // View menu build overview node.
       
   572     $this->drupalGet('admin/structure/menu');
       
   573     $this->assertResponse($response);
       
   574     if ($response == 200) {
       
   575       $this->assertText(t('Menus'), 'Menu build overview node was displayed');
       
   576     }
       
   577 
       
   578     // View navigation menu customization node.
       
   579     $this->drupalGet('admin/structure/menu/manage/navigation');
       
   580         $this->assertResponse($response);
       
   581     if ($response == 200) {
       
   582       $this->assertText(t('Navigation'), 'Navigation menu node was displayed');
       
   583     }
       
   584 
       
   585     // View menu edit node.
       
   586     $item = $this->getStandardMenuLink();
       
   587     $this->drupalGet('admin/structure/menu/item/' . $item['mlid'] . '/edit');
       
   588     $this->assertResponse($response);
       
   589     if ($response == 200) {
       
   590       $this->assertText(t('Edit menu item'), 'Menu edit node was displayed');
       
   591     }
       
   592 
       
   593     // View menu settings node.
       
   594     $this->drupalGet('admin/structure/menu/settings');
       
   595     $this->assertResponse($response);
       
   596     if ($response == 200) {
       
   597       $this->assertText(t('Menus'), 'Menu settings node was displayed');
       
   598     }
       
   599 
       
   600     // View add menu node.
       
   601     $this->drupalGet('admin/structure/menu/add');
       
   602     $this->assertResponse($response);
       
   603     if ($response == 200) {
       
   604       $this->assertText(t('Menus'), 'Add menu node was displayed');
       
   605     }
       
   606   }
       
   607 }
       
   608 
       
   609 /**
       
   610  * Test menu settings for nodes.
       
   611  */
       
   612 class MenuNodeTestCase extends DrupalWebTestCase {
       
   613   public static function getInfo() {
       
   614     return array(
       
   615       'name' => 'Menu settings for nodes',
       
   616       'description' => 'Add, edit, and delete a node with menu link.',
       
   617       'group' => 'Menu',
       
   618     );
       
   619   }
       
   620 
       
   621   function setUp() {
       
   622     parent::setUp('menu');
       
   623 
       
   624     $this->admin_user = $this->drupalCreateUser(array(
       
   625       'access administration pages',
       
   626       'administer content types',
       
   627       'administer menu',
       
   628       'create page content',
       
   629       'edit any page content',
       
   630       'delete any page content',
       
   631     ));
       
   632     $this->drupalLogin($this->admin_user);
       
   633   }
       
   634 
       
   635   /**
       
   636    * Test creating, editing, deleting menu links via node form widget.
       
   637    */
       
   638   function testMenuNodeFormWidget() {
       
   639     // Enable Navigation menu as available menu.
       
   640     $edit = array(
       
   641       'menu_options[navigation]' => 1,
       
   642     );
       
   643     $this->drupalPost('admin/structure/types/manage/page', $edit, t('Save content type'));
       
   644     // Change default parent item to Navigation menu, so we can assert more
       
   645     // easily.
       
   646     $edit = array(
       
   647       'menu_parent' => 'navigation:0',
       
   648     );
       
   649     $this->drupalPost('admin/structure/types/manage/page', $edit, t('Save content type'));
       
   650 
       
   651     // Verify that the menu link title on the node add form has the correct
       
   652     // maxlength.
       
   653     $this->drupalGet('node/add/page');
       
   654     $this->assertPattern('/<input .* id="edit-menu-link-title" .* maxlength="255" .* \/>/', 'Menu link title field has correct maxlength in node add form.');
       
   655 
       
   656     // Create a node with menu link disabled.
       
   657     $node_title = $this->randomName();
       
   658     $language = LANGUAGE_NONE;
       
   659     $edit = array(
       
   660       "title" => $node_title,
       
   661       "body[$language][0][value]" => $this->randomString(),
       
   662     );
       
   663     $this->drupalPost('node/add/page', $edit, t('Save'));
       
   664     $node = $this->drupalGetNodeByTitle($node_title);
       
   665     // Assert that there is no link for the node.
       
   666     $this->drupalGet('');
       
   667     $this->assertNoLink($node_title);
       
   668 
       
   669     // Edit the node, enable the menu link setting, but skip the link title.
       
   670     $edit = array(
       
   671       'menu[enabled]' => 1,
       
   672     );
       
   673     $this->drupalPost('node/' . $node->nid . '/edit', $edit, t('Save'));
       
   674     // Assert that there is no link for the node.
       
   675     $this->drupalGet('');
       
   676     $this->assertNoLink($node_title);
       
   677 
       
   678     // Edit the node and create a menu link.
       
   679     $edit = array(
       
   680       'menu[enabled]' => 1,
       
   681       'menu[link_title]' => $node_title,
       
   682       'menu[weight]' => 17,
       
   683     );
       
   684     $this->drupalPost('node/' . $node->nid . '/edit', $edit, t('Save'));
       
   685     // Assert that the link exists.
       
   686     $this->drupalGet('');
       
   687     $this->assertLink($node_title);
       
   688 
       
   689     $this->drupalGet('node/' . $node->nid . '/edit');
       
   690     $this->assertOptionSelected('edit-menu-weight', 17, 'Menu weight correct in edit form');
       
   691 
       
   692     // Verify that the menu link title on the node edit form has the correct
       
   693     // maxlength.
       
   694     $this->assertPattern('/<input .* id="edit-menu-link-title" .* maxlength="255" .* \/>/', 'Menu link title field has correct maxlength in node edit form.');
       
   695 
       
   696     // Edit the node and remove the menu link.
       
   697     $edit = array(
       
   698       'menu[enabled]' => FALSE,
       
   699     );
       
   700     $this->drupalPost('node/' . $node->nid . '/edit', $edit, t('Save'));
       
   701     // Assert that there is no link for the node.
       
   702     $this->drupalGet('');
       
   703     $this->assertNoLink($node_title);
       
   704 
       
   705     // Add a menu link to the Management menu.
       
   706     $item = array(
       
   707       'link_path' => 'node/' . $node->nid,
       
   708       'link_title' => $this->randomName(16),
       
   709       'menu_name' => 'management',
       
   710     );
       
   711     menu_link_save($item);
       
   712 
       
   713     // Assert that disabled Management menu is not shown on the node/$nid/edit page.
       
   714     $this->drupalGet('node/' . $node->nid . '/edit');
       
   715     $this->assertText('Provide a menu link', 'Link in not allowed menu not shown in node edit form');
       
   716     // Assert that the link is still in the management menu after save.
       
   717     $this->drupalPost('node/' . $node->nid . '/edit', $edit, t('Save'));
       
   718     $link = menu_link_load($item['mlid']);
       
   719     $this->assertTrue($link, 'Link in not allowed menu still exists after saving node');
       
   720 
       
   721     // Move the menu link back to the Navigation menu.
       
   722     $item['menu_name'] = 'navigation';
       
   723     menu_link_save($item);
       
   724     // Create a second node.
       
   725     $child_node = $this->drupalCreateNode(array('type' => 'article'));
       
   726     // Assign a menu link to the second node, being a child of the first one.
       
   727     $child_item = array(
       
   728       'link_path' => 'node/'. $child_node->nid,
       
   729       'link_title' => $this->randomName(16),
       
   730       'plid' => $item['mlid'],
       
   731     );
       
   732     menu_link_save($child_item);
       
   733     // Edit the first node.
       
   734     $this->drupalGet('node/'. $node->nid .'/edit');
       
   735     // Assert that it is not possible to set the parent of the first node to itself or the second node.
       
   736     $this->assertNoOption('edit-menu-parent', 'navigation:'. $item['mlid']);
       
   737     $this->assertNoOption('edit-menu-parent', 'navigation:'. $child_item['mlid']);
       
   738     // Assert that unallowed Management menu is not available in options.
       
   739     $this->assertNoOption('edit-menu-parent', 'management:0');
       
   740   }
       
   741 
       
   742   /**
       
   743    * Asserts that a select option in the current page does not exist.
       
   744    *
       
   745    * @param $id
       
   746    *   Id of select field to assert.
       
   747    * @param $option
       
   748    *   Option to assert.
       
   749    * @param $message
       
   750    *   Message to display.
       
   751    * @return
       
   752    *   TRUE on pass, FALSE on fail.
       
   753    *
       
   754    * @todo move to simpletest drupal_web_test_case.php.
       
   755    */
       
   756   protected function assertNoOption($id, $option, $message = '') {
       
   757     $selects = $this->xpath('//select[@id=:id]', array(':id' => $id));
       
   758     $options = $this->xpath('//select[@id=:id]//option[@value=:option]', array(':id' => $id, ':option' => $option));
       
   759     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'));
       
   760   }
       
   761 }