menu.test 27 KB

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