menu.test 26 KB

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