Projet

Général

Profil

Paste
Télécharger (26,1 ko) Statistiques
| Branche: | Révision:

root / drupal7 / modules / menu / menu.test @ db2d93dd

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

    
77
  /**
78
   * Test standard menu functionality using navigation menu.
79
   *
80
   */
81
  function doStandardMenuTests() {
82
    $this->doMenuTests();
83
    $this->addInvalidMenuLink();
84
  }
85

    
86
  /**
87
   * Test custom menu functionality using navigation menu.
88
   *
89
   */
90
  function doCustomMenuTests() {
91
    $this->menu = $this->addCustomMenu();
92
    $this->doMenuTests($this->menu['menu_name']);
93
    $this->addInvalidMenuLink($this->menu['menu_name']);
94
    $this->addCustomMenuCRUD();
95
  }
96

    
97
  /**
98
   * Add custom menu using CRUD functions.
99
   */
100
  function addCustomMenuCRUD() {
101
    // Add a new custom menu.
102
    $menu_name = substr(hash('sha256', $this->randomName(16)), 0, MENU_MAX_MENU_NAME_LENGTH_UI);
103
    $title = $this->randomName(16);
104

    
105
    $menu = array(
106
      'menu_name' => $menu_name,
107
      'title' => $title,
108
      'description' => 'Description text',
109
    );
110
    menu_save($menu);
111

    
112
    // Assert the new menu.
113
    $this->drupalGet('admin/structure/menu/manage/' . $menu_name . '/edit');
114
    $this->assertRaw($title, 'Custom menu was added.');
115

    
116
    // Edit the menu.
117
    $new_title = $this->randomName(16);
118
    $menu['title'] = $new_title;
119
    menu_save($menu);
120
    $this->drupalGet('admin/structure/menu/manage/' . $menu_name . '/edit');
121
    $this->assertRaw($new_title, 'Custom menu was edited.');
122
  }
123

    
124
  /**
125
   * Add custom menu.
126
   */
127
  function addCustomMenu() {
128
    // Add custom menu.
129

    
130
    // Try adding a menu using a menu_name that is too long.
131
    $this->drupalGet('admin/structure/menu/add');
132
    $menu_name = substr(hash('sha256', $this->randomName(16)), 0, MENU_MAX_MENU_NAME_LENGTH_UI + 1);
133
    $title = $this->randomName(16);
134
    $edit = array(
135
      'menu_name' => $menu_name,
136
      'description' => '',
137
      'title' =>  $title,
138
    );
139
    $this->drupalPost('admin/structure/menu/add', $edit, t('Save'));
140

    
141
    // Verify that using a menu_name that is too long results in a validation message.
142
    $this->assertRaw(t('!name cannot be longer than %max characters but is currently %length characters long.', array(
143
      '!name' => t('Menu name'),
144
      '%max' => MENU_MAX_MENU_NAME_LENGTH_UI,
145
      '%length' => drupal_strlen($menu_name),
146
    )));
147

    
148
    // Change the menu_name so it no longer exceeds the maximum length.
149
    $menu_name = substr(hash('sha256', $this->randomName(16)), 0, MENU_MAX_MENU_NAME_LENGTH_UI);
150
    $edit['menu_name'] = $menu_name;
151
    $this->drupalPost('admin/structure/menu/add', $edit, t('Save'));
152

    
153
    // Verify that no validation error is given for menu_name length.
154
    $this->assertNoRaw(t('!name cannot be longer than %max characters but is currently %length characters long.', array(
155
      '!name' => t('Menu name'),
156
      '%max' => MENU_MAX_MENU_NAME_LENGTH_UI,
157
      '%length' => drupal_strlen($menu_name),
158
    )));
159
    // Unlike most other modules, there is no confirmation message displayed.
160

    
161
    $this->drupalGet('admin/structure/menu');
162
    $this->assertText($title, 'Menu created');
163

    
164
    // Enable the custom menu block.
165
    $menu_name = 'menu-' . $menu_name; // Drupal prepends the name with 'menu-'.
166
    $edit = array();
167
    $edit['blocks[menu_' . $menu_name . '][region]'] = 'sidebar_first';
168
    $this->drupalPost('admin/structure/block', $edit, t('Save blocks'));
169
    $this->assertResponse(200);
170
    $this->assertText(t('The block settings have been updated.'), 'Custom menu block was enabled');
171

    
172
    return menu_load($menu_name);
173
  }
174

    
175
  /**
176
   * Delete custom menu.
177
   *
178
   * @param string $menu_name Custom menu name.
179
   */
180
  function deleteCustomMenu($menu) {
181
    $menu_name = $this->menu['menu_name'];
182
    $title = $this->menu['title'];
183

    
184
    // Delete custom menu.
185
    $this->drupalPost("admin/structure/menu/manage/$menu_name/delete", array(), t('Delete'));
186
    $this->assertResponse(200);
187
    $this->assertRaw(t('The custom menu %title has been deleted.', array('%title' => $title)), 'Custom menu was deleted');
188
    $this->assertFalse(menu_load($menu_name), 'Custom menu was deleted');
189
    // Test if all menu links associated to the menu were removed from database.
190
    $result = db_query("SELECT menu_name FROM {menu_links} WHERE menu_name = :menu_name", array(':menu_name' => $menu_name))->fetchField();
191
    $this->assertFalse($result, 'All menu links associated to the custom menu were deleted.');
192
  }
193

    
194
  /**
195
   * Test menu functionality using navigation menu.
196
   *
197
   */
198
  function doMenuTests($menu_name = 'navigation') {
199
    // Add nodes to use as links for menu links.
200
    $node1 = $this->drupalCreateNode(array('type' => 'article'));
201
    $node2 = $this->drupalCreateNode(array('type' => 'article'));
202
    $node3 = $this->drupalCreateNode(array('type' => 'article'));
203
    $node4 = $this->drupalCreateNode(array('type' => 'article'));
204
    $node5 = $this->drupalCreateNode(array('type' => 'article'));
205

    
206
    // Add menu links.
207
    $item1 = $this->addMenuLink(0, 'node/' . $node1->nid, $menu_name);
208
    $item2 = $this->addMenuLink($item1['mlid'], 'node/' . $node2->nid, $menu_name, FALSE);
209
    $item3 = $this->addMenuLink($item2['mlid'], 'node/' . $node3->nid, $menu_name);
210
    $this->assertMenuLink($item1['mlid'], array('depth' => 1, 'has_children' => 1, 'p1' => $item1['mlid'], 'p2' => 0));
211
    $this->assertMenuLink($item2['mlid'], array('depth' => 2, 'has_children' => 1, 'p1' => $item1['mlid'], 'p2' => $item2['mlid'], 'p3' => 0));
212
    $this->assertMenuLink($item3['mlid'], array('depth' => 3, 'has_children' => 0, 'p1' => $item1['mlid'], 'p2' => $item2['mlid'], 'p3' => $item3['mlid'], 'p4' => 0));
213

    
214
    // Verify menu links.
215
    $this->verifyMenuLink($item1, $node1);
216
    $this->verifyMenuLink($item2, $node2, $item1, $node1);
217
    $this->verifyMenuLink($item3, $node3, $item2, $node2);
218

    
219
    // Add more menu links.
220
    $item4 = $this->addMenuLink(0, 'node/' . $node4->nid, $menu_name);
221
    $item5 = $this->addMenuLink($item4['mlid'], 'node/' . $node5->nid, $menu_name);
222
    $this->assertMenuLink($item4['mlid'], array('depth' => 1, 'has_children' => 1, 'p1' => $item4['mlid'], 'p2' => 0));
223
    $this->assertMenuLink($item5['mlid'], array('depth' => 2, 'has_children' => 0, 'p1' => $item4['mlid'], 'p2' => $item5['mlid'], 'p3' => 0));
224

    
225
    // Modify menu links.
226
    $this->modifyMenuLink($item1);
227
    $this->modifyMenuLink($item2);
228

    
229
    // Toggle menu links.
230
    $this->toggleMenuLink($item1);
231
    $this->toggleMenuLink($item2);
232

    
233
    // Move link and verify that descendants are updated.
234
    $this->moveMenuLink($item2, $item5['mlid'], $menu_name);
235
    $this->assertMenuLink($item1['mlid'], array('depth' => 1, 'has_children' => 0, 'p1' => $item1['mlid'], 'p2' => 0));
236
    $this->assertMenuLink($item4['mlid'], array('depth' => 1, 'has_children' => 1, 'p1' => $item4['mlid'], 'p2' => 0));
237
    $this->assertMenuLink($item5['mlid'], array('depth' => 2, 'has_children' => 1, 'p1' => $item4['mlid'], 'p2' => $item5['mlid'], 'p3' => 0));
238
    $this->assertMenuLink($item2['mlid'], array('depth' => 3, 'has_children' => 1, 'p1' => $item4['mlid'], 'p2' => $item5['mlid'], 'p3' => $item2['mlid'], 'p4' => 0));
239
    $this->assertMenuLink($item3['mlid'], array('depth' => 4, 'has_children' => 0, 'p1' => $item4['mlid'], 'p2' => $item5['mlid'], 'p3' => $item2['mlid'], 'p4' => $item3['mlid'], 'p5' => 0));
240

    
241
    // Enable a link via the overview form.
242
    $this->disableMenuLink($item1);
243
    $edit = array();
244

    
245
    // Note in the UI the 'mlid:x[hidden]' form element maps to enabled, or
246
    // NOT hidden.
247
    $edit['mlid:' . $item1['mlid'] . '[hidden]'] = TRUE;
248
    $this->drupalPost('admin/structure/menu/manage/' . $item1['menu_name'], $edit, t('Save configuration'));
249

    
250
    // Verify in the database.
251
    $this->assertMenuLink($item1['mlid'], array('hidden' => 0));
252

    
253
    // Save menu links for later tests.
254
    $this->items[] = $item1;
255
    $this->items[] = $item2;
256
  }
257

    
258
  /**
259
   * Add and remove a menu link with a query string and fragment.
260
   */
261
  function testMenuQueryAndFragment() {
262
    $this->drupalLogin($this->big_user);
263

    
264
    // Make a path with query and fragment on.
265
    $path = 'node?arg1=value1&arg2=value2';
266
    $item = $this->addMenuLink(0, $path);
267

    
268
    $this->drupalGet('admin/structure/menu/item/' . $item['mlid'] . '/edit');
269
    $this->assertFieldByName('link_path', $path, 'Path is found with both query and fragment.');
270

    
271
    // Now change the path to something without query and fragment.
272
    $path = 'node';
273
    $this->drupalPost('admin/structure/menu/item/' . $item['mlid'] . '/edit', array('link_path' => $path), t('Save'));
274
    $this->drupalGet('admin/structure/menu/item/' . $item['mlid'] . '/edit');
275
    $this->assertFieldByName('link_path', $path, 'Path no longer has query or fragment.');
276
  }
277

    
278
  /**
279
   * Add a menu link using the menu module UI.
280
   *
281
   * @param integer $plid Parent menu link id.
282
   * @param string $link Link path.
283
   * @param string $menu_name Menu name.
284
   * @return array Menu link created.
285
   */
286
  function addMenuLink($plid = 0, $link = '<front>', $menu_name = 'navigation', $expanded = TRUE) {
287
    // View add menu link page.
288
    $this->drupalGet("admin/structure/menu/manage/$menu_name/add");
289
    $this->assertResponse(200);
290

    
291
    $title = '!link_' . $this->randomName(16);
292
    $edit = array(
293
      'link_path' => $link,
294
      'link_title' => $title,
295
      'description' => '',
296
      'enabled' => TRUE, // Use this to disable the menu and test.
297
      'expanded' => $expanded, // Setting this to true should test whether it works when we do the std_user tests.
298
      'parent' =>  $menu_name . ':' . $plid,
299
      'weight' => '0',
300
    );
301

    
302
    // Add menu link.
303
    $this->drupalPost(NULL, $edit, t('Save'));
304
    $this->assertResponse(200);
305
    // Unlike most other modules, there is no confirmation message displayed.
306
    $this->assertText($title, 'Menu link was added');
307

    
308
    $item = db_query('SELECT * FROM {menu_links} WHERE link_title = :title', array(':title' => $title))->fetchAssoc();
309
    $this->assertTrue(t('Menu link was found in database.'));
310
    $this->assertMenuLink($item['mlid'], array('menu_name' => $menu_name, 'link_path' => $link, 'has_children' => 0, 'plid' => $plid));
311

    
312
    return $item;
313
  }
314

    
315
  /**
316
   * Attempt to add menu link with invalid path or no access permission.
317
   *
318
   * @param string $menu_name Menu name.
319
   */
320
  function addInvalidMenuLink($menu_name = 'navigation') {
321
    foreach (array('-&-', 'admin/people/permissions', '#') as $link_path) {
322
      $edit = array(
323
        'link_path' => $link_path,
324
        'link_title' => 'title',
325
      );
326
      $this->drupalPost("admin/structure/menu/manage/$menu_name/add", $edit, t('Save'));
327
      $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');
328
    }
329
  }
330

    
331
  /**
332
   * Verify a menu link using the menu module UI.
333
   *
334
   * @param array $item Menu link.
335
   * @param object $item_node Menu link content node.
336
   * @param array $parent Parent menu link.
337
   * @param object $parent_node Parent menu link content node.
338
   */
339
  function verifyMenuLink($item, $item_node, $parent = NULL, $parent_node = NULL) {
340
    // View home page.
341
    $this->drupalGet('');
342
    $this->assertResponse(200);
343

    
344
    // Verify parent menu link.
345
    if (isset($parent)) {
346
      // Verify menu link.
347
      $title = $parent['link_title'];
348
      $this->assertLink($title, 0, 'Parent menu link was displayed');
349

    
350
      // Verify menu link link.
351
      $this->clickLink($title);
352
      $title = $parent_node->title;
353
      $this->assertTitle(t("@title | Drupal", array('@title' => $title)), 'Parent menu link link target was correct');
354
    }
355

    
356
    // Verify menu link.
357
    $title = $item['link_title'];
358
    $this->assertLink($title, 0, 'Menu link was displayed');
359

    
360
    // Verify menu link link.
361
    $this->clickLink($title);
362
    $title = $item_node->title;
363
    $this->assertTitle(t("@title | Drupal", array('@title' => $title)), 'Menu link link target was correct');
364
  }
365

    
366
  /**
367
   * Change the parent of a menu link using the menu module UI.
368
   */
369
  function moveMenuLink($item, $plid, $menu_name) {
370
    $mlid = $item['mlid'];
371

    
372
    $edit = array(
373
      'parent' => $menu_name . ':' . $plid,
374
    );
375
    $this->drupalPost("admin/structure/menu/item/$mlid/edit", $edit, t('Save'));
376
    $this->assertResponse(200);
377
  }
378

    
379
  /**
380
   * Modify a menu link using the menu module UI.
381
   *
382
   * @param array $item Menu link passed by reference.
383
   */
384
  function modifyMenuLink(&$item) {
385
    $item['link_title'] = $this->randomName(16);
386

    
387
    $mlid = $item['mlid'];
388
    $title = $item['link_title'];
389

    
390
    // Edit menu link.
391
    $edit = array();
392
    $edit['link_title'] = $title;
393
    $this->drupalPost("admin/structure/menu/item/$mlid/edit", $edit, t('Save'));
394
    $this->assertResponse(200);
395
    // Unlike most other modules, there is no confirmation message displayed.
396

    
397
    // Verify menu link.
398
    $this->drupalGet('admin/structure/menu/manage/' . $item['menu_name']);
399
    $this->assertText($title, 'Menu link was edited');
400
  }
401

    
402
  /**
403
   * Reset a standard menu link using the menu module UI.
404
   *
405
   * @param array $item Menu link.
406
   * @param string $old_title Original title for menu link.
407
   */
408
  function resetMenuLink($item, $old_title) {
409
    $mlid = $item['mlid'];
410
    $title = $item['link_title'];
411

    
412
    // Reset menu link.
413
    $this->drupalPost("admin/structure/menu/item/$mlid/reset", array(), t('Reset'));
414
    $this->assertResponse(200);
415
    $this->assertRaw(t('The menu link was reset to its default settings.'), 'Menu link was reset');
416

    
417
    // Verify menu link.
418
    $this->drupalGet('');
419
    $this->assertNoText($title, 'Menu link was reset');
420
    $this->assertText($old_title, 'Menu link was reset');
421
  }
422

    
423
  /**
424
   * Delete a menu link using the menu module UI.
425
   *
426
   * @param array $item Menu link.
427
   */
428
  function deleteMenuLink($item) {
429
    $mlid = $item['mlid'];
430
    $title = $item['link_title'];
431

    
432
    // Delete menu link.
433
    $this->drupalPost("admin/structure/menu/item/$mlid/delete", array(), t('Confirm'));
434
    $this->assertResponse(200);
435
    $this->assertRaw(t('The menu link %title has been deleted.', array('%title' => $title)), 'Menu link was deleted');
436

    
437
    // Verify deletion.
438
    $this->drupalGet('');
439
    $this->assertNoText($title, 'Menu link was deleted');
440
  }
441

    
442
  /**
443
   * Alternately disable and enable a menu link.
444
   *
445
   * @param $item
446
   *   Menu link.
447
   */
448
  function toggleMenuLink($item) {
449
    $this->disableMenuLink($item);
450

    
451
    // Verify menu link is absent.
452
    $this->drupalGet('');
453
    $this->assertNoText($item['link_title'], 'Menu link was not displayed');
454
    $this->enableMenuLink($item);
455

    
456
    // Verify menu link is displayed.
457
    $this->drupalGet('');
458
    $this->assertText($item['link_title'], 'Menu link was displayed');
459
  }
460

    
461
  /**
462
   * Disable a menu link.
463
   *
464
   * @param $item
465
   *   Menu link.
466
   */
467
  function disableMenuLink($item) {
468
    $mlid = $item['mlid'];
469
    $edit['enabled'] = FALSE;
470
    $this->drupalPost("admin/structure/menu/item/$mlid/edit", $edit, t('Save'));
471

    
472
    // Unlike most other modules, there is no confirmation message displayed.
473
    // Verify in the database.
474
    $this->assertMenuLink($mlid, array('hidden' => 1));
475
  }
476

    
477
  /**
478
   * Enable a menu link.
479
   *
480
   * @param $item
481
   *   Menu link.
482
   */
483
  function enableMenuLink($item) {
484
    $mlid = $item['mlid'];
485
    $edit['enabled'] = TRUE;
486
    $this->drupalPost("admin/structure/menu/item/$mlid/edit", $edit, t('Save'));
487

    
488
    // Verify in the database.
489
    $this->assertMenuLink($mlid, array('hidden' => 0));
490
  }
491

    
492
  /**
493
   * Fetch the menu item from the database and compare it to the specified
494
   * array.
495
   *
496
   * @param $mlid
497
   *   Menu item id.
498
   * @param $item
499
   *   Array containing properties to verify.
500
   */
501
  function assertMenuLink($mlid, array $expected_item) {
502
    // Retrieve menu link.
503
    $item = db_query('SELECT * FROM {menu_links} WHERE mlid = :mlid', array(':mlid' => $mlid))->fetchAssoc();
504
    $options = unserialize($item['options']);
505
    if (!empty($options['query'])) {
506
      $item['link_path'] .= '?' . drupal_http_build_query($options['query']);
507
    }
508
    if (!empty($options['fragment'])) {
509
      $item['link_path'] .= '#' . $options['fragment'];
510
    }
511
    foreach ($expected_item as $key => $value) {
512
      $this->assertEqual($item[$key], $value, format_string('Parameter %key had expected value.', array('%key' => $key)));
513
    }
514
  }
515

    
516
  /**
517
   * Test administrative users other than user 1 can access the menu parents AJAX callback.
518
   */
519
  public function testMenuParentsJsAccess() {
520
    $admin = $this->drupalCreateUser(array('administer menu'));
521
    $this->drupalLogin($admin);
522
    // Just check access to the callback overall, the POST data is irrelevant.
523
    $this->drupalGetAJAX('admin/structure/menu/parents');
524
    $this->assertResponse(200);
525

    
526
    // Do standard user tests.
527
    // Login the user.
528
    $this->drupalLogin($this->std_user);
529
    $this->drupalGetAJAX('admin/structure/menu/parents');
530
    $this->assertResponse(403);
531
  }
532

    
533
  /**
534
   * Get standard menu link.
535
   */
536
  private function getStandardMenuLink() {
537
    // Retrieve menu link id of the Log out menu link, which will always be on the front page.
538
    $mlid = db_query("SELECT mlid FROM {menu_links} WHERE module = 'system' AND router_path = 'user/logout'")->fetchField();
539
    $this->assertTrue($mlid > 0, 'Standard menu link id was found');
540
    // Load menu link.
541
    // Use api function so that link is translated for rendering.
542
    $item = menu_link_load($mlid);
543
    $this->assertTrue((bool) $item, 'Standard menu link was loaded');
544
    return $item;
545
  }
546

    
547
  /**
548
   * Verify the logged in user has the desired access to the various menu nodes.
549
   *
550
   * @param integer $response HTTP response code.
551
   */
552
  private function verifyAccess($response = 200) {
553
    // View menu help node.
554
    $this->drupalGet('admin/help/menu');
555
    $this->assertResponse($response);
556
    if ($response == 200) {
557
      $this->assertText(t('Menu'), 'Menu help was displayed');
558
    }
559

    
560
    // View menu build overview node.
561
    $this->drupalGet('admin/structure/menu');
562
    $this->assertResponse($response);
563
    if ($response == 200) {
564
      $this->assertText(t('Menus'), 'Menu build overview node was displayed');
565
    }
566

    
567
    // View navigation menu customization node.
568
    $this->drupalGet('admin/structure/menu/manage/navigation');
569
        $this->assertResponse($response);
570
    if ($response == 200) {
571
      $this->assertText(t('Navigation'), 'Navigation menu node was displayed');
572
    }
573

    
574
    // View menu edit node.
575
    $item = $this->getStandardMenuLink();
576
    $this->drupalGet('admin/structure/menu/item/' . $item['mlid'] . '/edit');
577
    $this->assertResponse($response);
578
    if ($response == 200) {
579
      $this->assertText(t('Edit menu item'), 'Menu edit node was displayed');
580
    }
581

    
582
    // View menu settings node.
583
    $this->drupalGet('admin/structure/menu/settings');
584
    $this->assertResponse($response);
585
    if ($response == 200) {
586
      $this->assertText(t('Menus'), 'Menu settings node was displayed');
587
    }
588

    
589
    // View add menu node.
590
    $this->drupalGet('admin/structure/menu/add');
591
    $this->assertResponse($response);
592
    if ($response == 200) {
593
      $this->assertText(t('Menus'), 'Add menu node was displayed');
594
    }
595
  }
596
}
597

    
598
/**
599
 * Test menu settings for nodes.
600
 */
601
class MenuNodeTestCase extends DrupalWebTestCase {
602
  public static function getInfo() {
603
    return array(
604
      'name' => 'Menu settings for nodes',
605
      'description' => 'Add, edit, and delete a node with menu link.',
606
      'group' => 'Menu',
607
    );
608
  }
609

    
610
  function setUp() {
611
    parent::setUp('menu');
612

    
613
    $this->admin_user = $this->drupalCreateUser(array(
614
      'access administration pages',
615
      'administer content types',
616
      'administer menu',
617
      'create page content',
618
      'edit any page content',
619
      'delete any page content',
620
    ));
621
    $this->drupalLogin($this->admin_user);
622
  }
623

    
624
  /**
625
   * Test creating, editing, deleting menu links via node form widget.
626
   */
627
  function testMenuNodeFormWidget() {
628
    // Enable Navigation menu as available menu.
629
    $edit = array(
630
      'menu_options[navigation]' => 1,
631
    );
632
    $this->drupalPost('admin/structure/types/manage/page', $edit, t('Save content type'));
633
    // Change default parent item to Navigation menu, so we can assert more
634
    // easily.
635
    $edit = array(
636
      'menu_parent' => 'navigation:0',
637
    );
638
    $this->drupalPost('admin/structure/types/manage/page', $edit, t('Save content type'));
639

    
640
    // Create a node.
641
    $node_title = $this->randomName();
642
    $language = LANGUAGE_NONE;
643
    $edit = array(
644
      "title" => $node_title,
645
      "body[$language][0][value]" => $this->randomString(),
646
    );
647
    $this->drupalPost('node/add/page', $edit, t('Save'));
648
    $node = $this->drupalGetNodeByTitle($node_title);
649
    // Assert that there is no link for the node.
650
    $this->drupalGet('');
651
    $this->assertNoLink($node_title);
652

    
653
    // Edit the node, enable the menu link setting, but skip the link title.
654
    $edit = array(
655
      'menu[enabled]' => 1,
656
    );
657
    $this->drupalPost('node/' . $node->nid . '/edit', $edit, t('Save'));
658
    // Assert that there is no link for the node.
659
    $this->drupalGet('');
660
    $this->assertNoLink($node_title);
661

    
662
    // Edit the node and create a menu link.
663
    $edit = array(
664
      'menu[enabled]' => 1,
665
      'menu[link_title]' => $node_title,
666
      'menu[weight]' => 17,
667
    );
668
    $this->drupalPost('node/' . $node->nid . '/edit', $edit, t('Save'));
669
    // Assert that the link exists.
670
    $this->drupalGet('');
671
    $this->assertLink($node_title);
672

    
673
    $this->drupalGet('node/' . $node->nid . '/edit');
674
    $this->assertOptionSelected('edit-menu-weight', 17, 'Menu weight correct in edit form');
675

    
676
    // Edit the node and remove the menu link.
677
    $edit = array(
678
      'menu[enabled]' => FALSE,
679
    );
680
    $this->drupalPost('node/' . $node->nid . '/edit', $edit, t('Save'));
681
    // Assert that there is no link for the node.
682
    $this->drupalGet('');
683
    $this->assertNoLink($node_title);
684

    
685
    // Add a menu link to the Management menu.
686
    $item = array(
687
      'link_path' => 'node/' . $node->nid,
688
      'link_title' => $this->randomName(16),
689
      'menu_name' => 'management',
690
    );
691
    menu_link_save($item);
692

    
693
    // Assert that disabled Management menu is not shown on the node/$nid/edit page.
694
    $this->drupalGet('node/' . $node->nid . '/edit');
695
    $this->assertText('Provide a menu link', 'Link in not allowed menu not shown in node edit form');
696
    // Assert that the link is still in the management menu after save.
697
    $this->drupalPost('node/' . $node->nid . '/edit', $edit, t('Save'));
698
    $link = menu_link_load($item['mlid']);
699
    $this->assertTrue($link, 'Link in not allowed menu still exists after saving node');
700

    
701
    // Move the menu link back to the Navigation menu.
702
    $item['menu_name'] = 'navigation';
703
    menu_link_save($item);
704
    // Create a second node.
705
    $child_node = $this->drupalCreateNode(array('type' => 'article'));
706
    // Assign a menu link to the second node, being a child of the first one.
707
    $child_item = array(
708
      'link_path' => 'node/'. $child_node->nid,
709
      'link_title' => $this->randomName(16),
710
      'plid' => $item['mlid'],
711
    );
712
    menu_link_save($child_item);
713
    // Edit the first node.
714
    $this->drupalGet('node/'. $node->nid .'/edit');
715
    // Assert that it is not possible to set the parent of the first node to itself or the second node.
716
    $this->assertNoOption('edit-menu-parent', 'navigation:'. $item['mlid']);
717
    $this->assertNoOption('edit-menu-parent', 'navigation:'. $child_item['mlid']);
718
    // Assert that unallowed Management menu is not available in options.
719
    $this->assertNoOption('edit-menu-parent', 'management:0');
720
  }
721

    
722
  /**
723
   * Asserts that a select option in the current page does not exist.
724
   *
725
   * @param $id
726
   *   Id of select field to assert.
727
   * @param $option
728
   *   Option to assert.
729
   * @param $message
730
   *   Message to display.
731
   * @return
732
   *   TRUE on pass, FALSE on fail.
733
   *
734
   * @todo move to simpletest drupal_web_test_case.php.
735
   */
736
  protected function assertNoOption($id, $option, $message = '') {
737
    $selects = $this->xpath('//select[@id=:id]', array(':id' => $id));
738
    $options = $this->xpath('//select[@id=:id]//option[@value=:option]', array(':id' => $id, ':option' => $option));
739
    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'));
740
  }
741
}