/[drupal]/drupal/modules/menu/menu.test
ViewVC logotype

Contents of /drupal/modules/menu/menu.test

Parent Directory Parent Directory | Revision Log Revision Log | View Revision Graph Revision Graph


Revision 1.25 - (show annotations) (download) (as text)
Fri Nov 6 03:17:59 2009 UTC (2 weeks, 5 days ago) by webchick
Branch: MAIN
CVS Tags: DRUPAL-7-0-UNSTABLE-10
Changes since 1.24: +86 -1 lines
File MIME type: text/x-php
#322703 by BarisW, sun, Shai, and dman: Use on/off checkbox to enable and prepopulate node's menu item.
1 <?php
2 // $Id: menu.test,v 1.24 2009/10/11 03:07:18 webchick Exp $
3
4 /**
5 * @file
6 * Tests file for the menu module.
7 */
8
9 class MenuTestCase extends DrupalWebTestCase {
10 protected $big_user;
11 protected $std_user;
12 protected $menu;
13 protected $items;
14
15 public static function getInfo() {
16 return array(
17 'name' => 'Menu link creation/deletion',
18 '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.',
19 'group' => 'Menu'
20 );
21 }
22
23 function setUp() {
24 parent::setUp('menu');
25 // Create users.
26 $this->big_user = $this->drupalCreateUser(array('access administration pages', 'administer blocks', 'administer menu', 'create article content'));
27 $this->std_user = $this->drupalCreateUser(array());
28 }
29
30 /**
31 * Login users, add menus and menu links, and test menu functionality through the admin and user interfaces.
32 */
33 function testMenu() {
34 // Login the user.
35 $this->drupalLogin($this->big_user);
36 $this->items = array();
37
38 // Do standard menu tests.
39 $this->doStandardMenuTests();
40
41 // Do custom menu tests.
42 $this->doCustomMenuTests();
43
44 // Do standard user tests.
45 // Login the user.
46 $this->drupalLogin($this->std_user);
47 $this->verifyAccess(403);
48 foreach ($this->items as $item) {
49 $node = node_load(substr($item['link_path'], 5)); // Paths were set as 'node/$nid'.
50 $this->verifyMenuLink($item, $node);
51 }
52
53 // Login the user.
54 $this->drupalLogin($this->big_user);
55
56 // Delete menu links.
57 foreach ($this->items as $item) {
58 $this->deleteMenuLink($item);
59 }
60
61 // Delete custom menu.
62 $this->deleteCustomMenu($this->menu);
63
64 // Modify and reset a standard menu link.
65 $item = $this->getStandardMenuLink();
66 $old_title = $item['link_title'];
67 $this->modifyMenuLink($item);
68 $item = menu_link_load($item['mlid']);
69 // Verify that a change to the description is saved.
70 $description = $this->randomName(16);
71 $item['options']['attributes']['title'] = $description;
72 menu_link_save($item);
73 $saved_item = menu_link_load($item['mlid']);
74 $this->assertEqual($description, $saved_item['options']['attributes']['title'], t('Saving an existing link updates the description (title attribute)'));
75 $this->resetMenuLink($item, $old_title);
76 }
77
78 /**
79 * Test standard menu functionality using navigation menu.
80 *
81 */
82 function doStandardMenuTests() {
83 $this->doMenuTests();
84 $this->addInvalidMenuLink();
85 }
86
87 /**
88 * Test custom menu functionality using navigation menu.
89 *
90 */
91 function doCustomMenuTests() {
92 $this->menu = $this->addCustomMenu();
93 $this->doMenuTests($this->menu['menu_name']);
94 $this->addInvalidMenuLink($this->menu['menu_name']);
95 $this->addCustomMenuCRUD();
96 }
97
98 /**
99 * Add custom menu using CRUD functions.
100 */
101 function addCustomMenuCRUD() {
102 // Add a new custom menu.
103 $menu_name = substr(md5($this->randomName(16)), 0, MENU_MAX_MENU_NAME_LENGTH_UI);
104 $title = $this->randomName(16);
105
106 $menu = array(
107 'menu_name' => $menu_name,
108 'title' => $title,
109 'description' => 'Description text',
110 );
111 menu_save($menu);
112
113 // Assert the new menu.
114 $this->drupalGet('admin/structure/menu/manage/' . $menu_name . '/edit');
115 $this->assertText($title, t('Custom menu was added.'));
116
117 // Edit the menu.
118 $new_title = $this->randomName(16);
119 $menu['title'] = $new_title;
120 menu_save($menu);
121 $this->drupalGet('admin/structure/menu/manage/' . $menu_name . '/edit');
122 $this->assertText($new_title, t('Custom menu was edited.'));
123 }
124
125 /**
126 * Add custom menu.
127 */
128 function addCustomMenu() {
129 // Add custom menu.
130
131 // Try adding a menu using a menu_name that is too long.
132 $this->drupalGet('admin/structure/menu/add');
133 $menu_name = substr(md5($this->randomName(16)), 0, MENU_MAX_MENU_NAME_LENGTH_UI + 1);
134 $title = $this->randomName(16);
135 $edit = array(
136 'menu_name' => $menu_name,
137 'description' => '',
138 'title' => $title,
139 );
140 $this->drupalPost('admin/structure/menu/add', $edit, t('Save'));
141
142 // Verify that using a menu_name that is too long results in a validation message.
143 $this->assertText(format_plural(MENU_MAX_MENU_NAME_LENGTH_UI, "The menu name can't be longer than 1 character.", "The menu name can't be longer than @count characters."), t('Validation failed when menu name is too long.'));
144
145 // Change the menu_name so it no longer exceeds the maximum length.
146 $menu_name = substr(md5($this->randomName(16)), 0, MENU_MAX_MENU_NAME_LENGTH_UI);
147 $edit['menu_name'] = $menu_name;
148 $this->drupalPost('admin/structure/menu/add', $edit, t('Save'));
149
150 // Verify that no validation error is given for menu_name length.
151 $this->assertNoText(format_plural(MENU_MAX_MENU_NAME_LENGTH_UI, "The menu name can't be longer than 1 character.", "The menu name can't be longer than @count characters."), t('Validation failed when menu name is too long.'));
152 // Unlike most other modules, there is no confirmation message displayed.
153
154 $this->drupalGet('admin/structure/menu');
155 $this->assertText($title, 'Menu created');
156
157 // Enable the custom menu block.
158 $menu_name = 'menu-' . $menu_name; // Drupal prepends the name with 'menu-'.
159 $edit = array();
160 $edit['menu_' . $menu_name . '[region]'] = 'sidebar_first';
161 $this->drupalPost('admin/structure/block', $edit, t('Save blocks'));
162 $this->assertResponse(200);
163 $this->assertText(t('The block settings have been updated.'), t('Custom menu block was enabled'));
164
165 return menu_load($menu_name);
166 }
167
168 /**
169 * Delete custom menu.
170 *
171 * @param string $menu_name Custom menu name.
172 */
173 function deleteCustomMenu($menu) {
174 $menu_name = $this->menu['menu_name'];
175 $title = $this->menu['title'];
176
177 // Delete custom menu.
178 $this->drupalPost("admin/structure/menu/manage/$menu_name/delete", array(), t('Delete'));
179 $this->assertResponse(200);
180 $this->assertRaw(t('The custom menu %title has been deleted.', array('%title' => $title)), t('Custom menu was deleted'));
181 $this->assertFalse(menu_load($menu_name), 'Custom menu was deleted');
182 // Test if all menu links associated to the menu were removed from database.
183 $result = db_query("SELECT menu_name FROM {menu_links} WHERE menu_name = :menu_name", array(':menu_name' => $menu_name))->fetchField();
184 $this->assertFalse($result, t('All menu links associated to the custom menu were deleted.'));
185 }
186
187 /**
188 * Test menu functionality using navigation menu.
189 *
190 */
191 function doMenuTests($menu_name = 'navigation') {
192 // Add nodes to use as links for menu links.
193 $node1 = $this->drupalCreateNode(array('type' => 'article'));
194 $node2 = $this->drupalCreateNode(array('type' => 'article'));
195
196 // Add menu links.
197 $item1 = $this->addMenuLink(0, 'node/' . $node1->nid, $menu_name);
198 $item2 = $this->addMenuLink($item1['mlid'], 'node/' . $node2->nid, $menu_name);
199
200 // Verify menu links.
201 $this->verifyMenuLink($item1, $node1);
202 $this->verifyMenuLink($item2, $node2, $item1, $node1);
203
204 // Modify menu links.
205 $this->modifyMenuLink($item1);
206 $this->modifyMenuLink($item2);
207
208 // Toggle menu links.
209 $this->toggleMenuLink($item1);
210 $this->toggleMenuLink($item2);
211
212 // Enable a link via the overview form.
213 $this->disableMenuLink($item1);
214 $edit = array();
215
216 // Note in the UI the 'mlid:x[hidden]' form element maps to enabled, or
217 // NOT hidden.
218 $edit['mlid:' . $item1['mlid'] . '[hidden]'] = TRUE;
219 $this->drupalPost('admin/structure/menu/manage/' . $item1['menu_name'], $edit, t('Save configuration'));
220
221 // Verify in the database.
222 $hidden = db_query("SELECT hidden FROM {menu_links} WHERE mlid = :mlid", array(':mlid' => $item1['mlid']))->fetchField();
223 $this->assertEqual($hidden, 0, t('Link is not hidden in the database table when enabled via the overview form'));
224
225 // Save menu links for later tests.
226 $this->items[] = $item1;
227 $this->items[] = $item2;
228 }
229
230 /**
231 * Add a menu link using the menu module UI.
232 *
233 * @param integer $plid Parent menu link id.
234 * @param string $link Link path.
235 * @param string $menu_name Menu name.
236 * @return array Menu link created.
237 */
238 function addMenuLink($plid = 0, $link = '<front>', $menu_name = 'navigation') {
239 // View add menu link page.
240 $this->drupalGet("admin/structure/menu/manage/$menu_name/add");
241 $this->assertResponse(200);
242
243 $title = '!link_' . $this->randomName(16);
244 $edit = array(
245 'menu[link_path]' => $link,
246 'menu[link_title]' => $title,
247 'menu[description]' => '',
248 'menu[enabled]' => TRUE, // Use this to disable the menu and test.
249 'menu[expanded]' => TRUE, // Setting this to true should test whether it works when we do the std_user tests.
250 'menu[parent]' => $menu_name . ':' . $plid,
251 'menu[weight]' => '0',
252 );
253
254 // Add menu link.
255 $this->drupalPost("admin/structure/menu/manage/$menu_name/add", $edit, t('Save'));
256 $this->assertResponse(200);
257 // Unlike most other modules, there is no confirmation message displayed.
258
259 $this->assertText($title, 'Menu link was added');
260
261 // Retrieve menu link.
262 $item = db_query("SELECT * FROM {menu_links} WHERE link_title = :title", array(':title' => $title))->fetchAssoc();
263
264 // Check the structure in the DB of the two menu links.
265 // In general, if $n = $item['depth'] then $item['p'. $n] == $item['mlid'] and $item['p' . ($n - 1)] == $item['plid'] (unless depth == 0).
266 // All $item['p' . $n] for $n > depth must be 0.
267 // We know link1 is at the top level, so $item1['deptj'] == 1 and $item1['plid'] == 0.
268 // We know that the parent of link2 is link1, so $item2['plid'] == $item1['mlid'].
269 // Both menu links were created in the navigation menu.
270 $this->assertTrue($item['menu_name'] == $menu_name && $item['plid'] == $plid && $item['link_path'] == $link && $item['link_title'] == $title, 'Menu link has correct data');
271 if ($plid == 0) {
272 $this->assertTrue($item['depth'] == 1 && !$item['has_children'] && $item['p1'] == $item['mlid'] && $item['p2'] == 0, 'Menu link has correct data');
273 }
274 else {
275 $this->assertTrue($item['depth'] == 2 && !$item['has_children'] && $item['p1'] == $plid && $item['p2'] == $item['mlid'], 'Menu link has correct data');
276 }
277
278 return $item;
279 }
280
281 /**
282 * Attempt to add menu link with invalid path or no access permission.
283 *
284 * @param string $menu_name Menu name.
285 */
286 function addInvalidMenuLink($menu_name = 'navigation') {
287 foreach (array('-&-', 'admin/config/people/permissions') as $link_path) {
288 $edit = array(
289 'menu[link_path]' => $link_path,
290 'menu[link_title]' => 'title',
291 );
292 $this->drupalPost("admin/structure/menu/manage/$menu_name/add", $edit, t('Save'));
293 $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');
294 }
295 }
296
297 /**
298 * Verify a menu link using the menu module UI.
299 *
300 * @param array $item Menu link.
301 * @param object $item_node Menu link content node.
302 * @param array $parent Parent menu link.
303 * @param object $parent_node Parent menu link content node.
304 */
305 function verifyMenuLink($item, $item_node, $parent = NULL, $parent_node = NULL) {
306 // View home page.
307 $this->drupalGet('');
308 $this->assertResponse(200);
309
310 // Verify parent menu link.
311 if (isset($parent)) {
312 // Verify menu link.
313 $title = $parent['link_title'];
314 $this->assertText($title, 'Parent menu link was displayed');
315
316 // Verify menu link link.
317 $this->clickLink($title);
318 $title = $parent_node->title[FIELD_LANGUAGE_NONE][0]['value'];
319 $this->assertTitle(t("@title | Drupal", array('@title' => $title)), t('Parent menu link link target was correct'));
320 }
321
322 // Verify menu link.
323 $title = $item['link_title'];
324 $this->assertText($title, 'Menu link was displayed');
325
326 // Verify menu link link.
327 $this->clickLink($title);
328 $title = $item_node->title[FIELD_LANGUAGE_NONE][0]['value'];
329 $this->assertTitle(t("@title | Drupal", array('@title' => $title)), t('Menu link link target was correct'));
330 }
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
340 $mlid = $item['mlid'];
341 $title = $item['link_title'];
342
343 // Edit menu link.
344 $edit = array();
345 $edit['menu[link_title]'] = $title;
346 $this->drupalPost("admin/structure/menu/item/$mlid/edit", $edit, t('Save'));
347 $this->assertResponse(200);
348 // Unlike most other modules, there is no confirmation message displayed.
349
350 // Verify menu link.
351 $this->drupalGet('admin/structure/menu/manage/' . $item['menu_name']);
352 $this->assertText($title, 'Menu link was edited');
353 }
354
355 /**
356 * Reset a standard menu link using the menu module UI.
357 *
358 * @param array $item Menu link.
359 * @param string $old_title Original title for menu link.
360 */
361 function resetMenuLink($item, $old_title) {
362 $mlid = $item['mlid'];
363 $title = $item['link_title'];
364
365 // Reset menu link.
366 $this->drupalPost("admin/structure/menu/item/$mlid/reset", array(), t('Reset'));
367 $this->assertResponse(200);
368 $this->assertRaw(t('The menu link was reset to its default settings.'), t('Menu link was reset'));
369
370 // Verify menu link.
371 $this->drupalGet('');
372 $this->assertNoText($title, 'Menu link was reset');
373
374 // Verify menu link.
375 $this->drupalGet('');
376 $this->assertText($old_title, 'Menu link was reset');
377 }
378
379 /**
380 * Delete a menu link using the menu module UI.
381 *
382 * @param array $item Menu link.
383 */
384 function deleteMenuLink($item) {
385 $mlid = $item['mlid'];
386 $title = $item['link_title'];
387
388 // Delete menu link.
389 $this->drupalPost("admin/structure/menu/item/$mlid/delete", array(), t('Confirm'));
390 $this->assertResponse(200);
391 $this->assertRaw(t('The menu link %title has been deleted.', array('%title' => $title)), t('Menu link was deleted'));
392
393 // Verify deletion.
394 $this->drupalGet('');
395 $this->assertNoText($title, 'Menu link was deleted');
396 }
397
398 /**
399 * Alternately disable and enable a menu link.
400 *
401 * @param $item
402 * Menu link.
403 */
404 function toggleMenuLink($item) {
405 $this->disableMenuLink($item);
406
407 // Verify menu link is absent.
408 $this->drupalGet('');
409 $this->assertNoText($item['link_title'], 'Menu link was not displayed');
410 $this->enableMenuLink($item);
411
412 // Verify menu link is displayed.
413 $this->drupalGet('');
414 $this->assertText($item['link_title'], 'Menu link was displayed');
415 }
416
417 /**
418 * Disable a menu link.
419 *
420 * @param $item
421 * Menu link.
422 */
423 function disableMenuLink($item) {
424 $mlid = $item['mlid'];
425 $edit['menu[enabled]'] = FALSE;
426 $this->drupalPost("admin/structure/menu/item/$mlid/edit", $edit, t('Save'));
427
428 // Unlike most other modules, there is no confirmation message displayed.
429 // Verify in the database.
430 $hidden = db_query("SELECT hidden FROM {menu_links} WHERE mlid = :mlid", array(':mlid' => $mlid))->fetchField();
431 $this->assertEqual($hidden, 1, t('Link is hidden in the database table'));
432 }
433
434 /**
435 * Enable a menu link.
436 *
437 * @param $item
438 * Menu link.
439 */
440 function enableMenuLink($item) {
441 $mlid = $item['mlid'];
442 $edit['menu[enabled]'] = TRUE;
443 $this->drupalPost("admin/structure/menu/item/$mlid/edit", $edit, t('Save'));
444
445 // Verify in the database.
446 $hidden = db_query("SELECT hidden FROM {menu_links} WHERE mlid = :mlid", array(':mlid' => $mlid))->fetchField();
447 $this->assertEqual($hidden, 0, t('Link is not hidden in the database table'));
448 }
449
450 /**
451 * Get standard menu link.
452 */
453 private function getStandardMenuLink() {
454 // Retrieve menu link id of the Log out menu link, which will always be on the front page.
455 $mlid = db_query("SELECT mlid FROM {menu_links} WHERE module = 'system' AND router_path = 'user/logout'")->fetchField();
456 $this->assertTrue($mlid > 0, 'Standard menu link id was found');
457 // Load menu link.
458 // Use api function so that link is translated for rendering.
459 $item = menu_link_load($mlid);
460 $this->assertTrue((bool)$item, 'Standard menu link was loaded');
461 return $item;
462 }
463
464 /**
465 * Verify the logged in user has the desired access to the various menu nodes.
466 *
467 * @param integer $response HTTP response code.
468 */
469 private function verifyAccess($response = 200) {
470 // View menu help node.
471 $this->drupalGet('admin/help/menu');
472 $this->assertResponse($response);
473 if ($response == 200) {
474 $this->assertText(t('Menu'), t('Menu help was displayed'));
475 }
476
477 // View menu build overview node.
478 $this->drupalGet('admin/structure/menu');
479 $this->assertResponse($response);
480 if ($response == 200) {
481 $this->assertText(t('Menus'), t('Menu build overview node was displayed'));
482 }
483
484 // View navigation menu customization node.
485 $this->drupalGet('admin/structure/menu/manage/navigation');
486 $this->assertResponse($response);
487 if ($response == 200) {
488 $this->assertText(t('Navigation'), t('Navigation menu node was displayed'));
489 }
490
491 // View menu edit node.
492 $item = $this->getStandardMenuLink();
493 $this->drupalGet('admin/structure/menu/item/' . $item['mlid'] . '/edit');
494 $this->assertResponse($response);
495 if ($response == 200) {
496 $this->assertText(t('Edit menu item'), t('Menu edit node was displayed'));
497 }
498
499 // View menu settings node.
500 $this->drupalGet('admin/structure/menu/settings');
501 $this->assertResponse($response);
502 if ($response == 200) {
503 $this->assertText(t('Menus'), t('Menu settings node was displayed'));
504 }
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'), t('Add menu node was displayed'));
511 }
512 }
513 }
514
515 /**
516 * Test menu settings for nodes.
517 */
518 class MenuNodeTestCase extends DrupalWebTestCase {
519 public static function getInfo() {
520 return array(
521 'name' => 'Menu settings for nodes',
522 'description' => 'Add, edit, and delete a node with menu link.',
523 'group' => 'Menu',
524 );
525 }
526
527 function setUp() {
528 parent::setUp('menu');
529
530 $this->admin_user = $this->drupalCreateUser(array(
531 'access administration pages',
532 'administer content types',
533 'administer menu',
534 'create page content',
535 'edit any page content',
536 'delete any page content',
537 ));
538 $this->drupalLogin($this->admin_user);
539 }
540
541 /**
542 * Test creating, editing, deleting menu links via node form widget.
543 */
544 function testMenuNodeFormWidget() {
545 // Enable Navigation menu as available menu.
546 $edit = array(
547 'menu_options[navigation]' => 1,
548 );
549 $this->drupalPost('admin/structure/types/manage/page', $edit, t('Save content type'));
550 // Change default parent item to Navigation menu, so we can assert more
551 // easily.
552 $edit = array(
553 'menu_parent' => 'navigation:0',
554 );
555 $this->drupalPost('admin/structure/types/manage/page', $edit, t('Save content type'));
556
557 // Create a node.
558 $node_title = $this->randomName();
559 $edit = array(
560 'title[zxx][0][value]' => $node_title,
561 'body[zxx][0][value]' => $this->randomString(),
562 );
563 $this->drupalPost('node/add/page', $edit, t('Save'));
564 $node = $this->drupalGetNodeByTitle($node_title);
565 // Assert that there is no link for the node.
566 $this->drupalGet('');
567 $this->assertNoLink($node_title);
568
569 // Edit the node, enable the menu link setting, but skip the link title.
570 $edit = array(
571 'menu[enabled]' => 1,
572 );
573 $this->drupalPost('node/' . $node->nid . '/edit', $edit, t('Save'));
574 // Assert that there is no link for the node.
575 $this->drupalGet('');
576 $this->assertNoLink($node_title);
577
578 // Edit the node and create a menu link.
579 $edit = array(
580 'menu[enabled]' => 1,
581 'menu[link_title]' => $node_title,
582 );
583 $this->drupalPost('node/' . $node->nid . '/edit', $edit, t('Save'));
584 // Assert that the link exists.
585 $this->drupalGet('');
586 $this->assertLink($node_title);
587
588 // Edit the node and remove the menu link.
589 $edit = array(
590 'menu[enabled]' => FALSE,
591 );
592 $this->drupalPost('node/' . $node->nid . '/edit', $edit, t('Save'));
593 // Assert that there is no link for the node.
594 $this->drupalGet('');
595 $this->assertNoLink($node_title);
596 }
597 }
598

  ViewVC Help
Powered by ViewVC 1.1.2