Projet

Général

Profil

Paste
Télécharger (25,5 ko) Statistiques
| Branche: | Révision:

root / htmltest / modules / menu / menu.test @ 85ad3d82

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
   * Get standard menu link.
518
   */
519
  private function getStandardMenuLink() {
520
    // Retrieve menu link id of the Log out menu link, which will always be on the front page.
521
    $mlid = db_query("SELECT mlid FROM {menu_links} WHERE module = 'system' AND router_path = 'user/logout'")->fetchField();
522
    $this->assertTrue($mlid > 0, 'Standard menu link id was found');
523
    // Load menu link.
524
    // Use api function so that link is translated for rendering.
525
    $item = menu_link_load($mlid);
526
    $this->assertTrue((bool) $item, 'Standard menu link was loaded');
527
    return $item;
528
  }
529

    
530
  /**
531
   * Verify the logged in user has the desired access to the various menu nodes.
532
   *
533
   * @param integer $response HTTP response code.
534
   */
535
  private function verifyAccess($response = 200) {
536
    // View menu help node.
537
    $this->drupalGet('admin/help/menu');
538
    $this->assertResponse($response);
539
    if ($response == 200) {
540
      $this->assertText(t('Menu'), 'Menu help was displayed');
541
    }
542

    
543
    // View menu build overview node.
544
    $this->drupalGet('admin/structure/menu');
545
    $this->assertResponse($response);
546
    if ($response == 200) {
547
      $this->assertText(t('Menus'), 'Menu build overview node was displayed');
548
    }
549

    
550
    // View navigation menu customization node.
551
    $this->drupalGet('admin/structure/menu/manage/navigation');
552
        $this->assertResponse($response);
553
    if ($response == 200) {
554
      $this->assertText(t('Navigation'), 'Navigation menu node was displayed');
555
    }
556

    
557
    // View menu edit node.
558
    $item = $this->getStandardMenuLink();
559
    $this->drupalGet('admin/structure/menu/item/' . $item['mlid'] . '/edit');
560
    $this->assertResponse($response);
561
    if ($response == 200) {
562
      $this->assertText(t('Edit menu item'), 'Menu edit node was displayed');
563
    }
564

    
565
    // View menu settings node.
566
    $this->drupalGet('admin/structure/menu/settings');
567
    $this->assertResponse($response);
568
    if ($response == 200) {
569
      $this->assertText(t('Menus'), 'Menu settings node was displayed');
570
    }
571

    
572
    // View add menu node.
573
    $this->drupalGet('admin/structure/menu/add');
574
    $this->assertResponse($response);
575
    if ($response == 200) {
576
      $this->assertText(t('Menus'), 'Add menu node was displayed');
577
    }
578
  }
579
}
580

    
581
/**
582
 * Test menu settings for nodes.
583
 */
584
class MenuNodeTestCase extends DrupalWebTestCase {
585
  public static function getInfo() {
586
    return array(
587
      'name' => 'Menu settings for nodes',
588
      'description' => 'Add, edit, and delete a node with menu link.',
589
      'group' => 'Menu',
590
    );
591
  }
592

    
593
  function setUp() {
594
    parent::setUp('menu');
595

    
596
    $this->admin_user = $this->drupalCreateUser(array(
597
      'access administration pages',
598
      'administer content types',
599
      'administer menu',
600
      'create page content',
601
      'edit any page content',
602
      'delete any page content',
603
    ));
604
    $this->drupalLogin($this->admin_user);
605
  }
606

    
607
  /**
608
   * Test creating, editing, deleting menu links via node form widget.
609
   */
610
  function testMenuNodeFormWidget() {
611
    // Enable Navigation menu as available menu.
612
    $edit = array(
613
      'menu_options[navigation]' => 1,
614
    );
615
    $this->drupalPost('admin/structure/types/manage/page', $edit, t('Save content type'));
616
    // Change default parent item to Navigation menu, so we can assert more
617
    // easily.
618
    $edit = array(
619
      'menu_parent' => 'navigation:0',
620
    );
621
    $this->drupalPost('admin/structure/types/manage/page', $edit, t('Save content type'));
622

    
623
    // Create a node.
624
    $node_title = $this->randomName();
625
    $language = LANGUAGE_NONE;
626
    $edit = array(
627
      "title" => $node_title,
628
      "body[$language][0][value]" => $this->randomString(),
629
    );
630
    $this->drupalPost('node/add/page', $edit, t('Save'));
631
    $node = $this->drupalGetNodeByTitle($node_title);
632
    // Assert that there is no link for the node.
633
    $this->drupalGet('');
634
    $this->assertNoLink($node_title);
635

    
636
    // Edit the node, enable the menu link setting, but skip the link title.
637
    $edit = array(
638
      'menu[enabled]' => 1,
639
    );
640
    $this->drupalPost('node/' . $node->nid . '/edit', $edit, t('Save'));
641
    // Assert that there is no link for the node.
642
    $this->drupalGet('');
643
    $this->assertNoLink($node_title);
644

    
645
    // Edit the node and create a menu link.
646
    $edit = array(
647
      'menu[enabled]' => 1,
648
      'menu[link_title]' => $node_title,
649
      'menu[weight]' => 17,
650
    );
651
    $this->drupalPost('node/' . $node->nid . '/edit', $edit, t('Save'));
652
    // Assert that the link exists.
653
    $this->drupalGet('');
654
    $this->assertLink($node_title);
655

    
656
    $this->drupalGet('node/' . $node->nid . '/edit');
657
    $this->assertOptionSelected('edit-menu-weight', 17, 'Menu weight correct in edit form');
658

    
659
    // Edit the node and remove the menu link.
660
    $edit = array(
661
      'menu[enabled]' => FALSE,
662
    );
663
    $this->drupalPost('node/' . $node->nid . '/edit', $edit, t('Save'));
664
    // Assert that there is no link for the node.
665
    $this->drupalGet('');
666
    $this->assertNoLink($node_title);
667

    
668
    // Add a menu link to the Management menu.
669
    $item = array(
670
      'link_path' => 'node/' . $node->nid,
671
      'link_title' => $this->randomName(16),
672
      'menu_name' => 'management',
673
    );
674
    menu_link_save($item);
675

    
676
    // Assert that disabled Management menu is not shown on the node/$nid/edit page.
677
    $this->drupalGet('node/' . $node->nid . '/edit');
678
    $this->assertText('Provide a menu link', 'Link in not allowed menu not shown in node edit form');
679
    // Assert that the link is still in the management menu after save.
680
    $this->drupalPost('node/' . $node->nid . '/edit', $edit, t('Save'));
681
    $link = menu_link_load($item['mlid']);
682
    $this->assertTrue($link, 'Link in not allowed menu still exists after saving node');
683

    
684
    // Move the menu link back to the Navigation menu.
685
    $item['menu_name'] = 'navigation';
686
    menu_link_save($item);
687
    // Create a second node.
688
    $child_node = $this->drupalCreateNode(array('type' => 'article'));
689
    // Assign a menu link to the second node, being a child of the first one.
690
    $child_item = array(
691
      'link_path' => 'node/'. $child_node->nid,
692
      'link_title' => $this->randomName(16),
693
      'plid' => $item['mlid'],
694
    );
695
    menu_link_save($child_item);
696
    // Edit the first node.
697
    $this->drupalGet('node/'. $node->nid .'/edit');
698
    // Assert that it is not possible to set the parent of the first node to itself or the second node.
699
    $this->assertNoOption('edit-menu-parent', 'navigation:'. $item['mlid']);
700
    $this->assertNoOption('edit-menu-parent', 'navigation:'. $child_item['mlid']);
701
    // Assert that unallowed Management menu is not available in options.
702
    $this->assertNoOption('edit-menu-parent', 'management:0');
703
  }
704

    
705
  /**
706
   * Asserts that a select option in the current page does not exist.
707
   *
708
   * @param $id
709
   *   Id of select field to assert.
710
   * @param $option
711
   *   Option to assert.
712
   * @param $message
713
   *   Message to display.
714
   * @return
715
   *   TRUE on pass, FALSE on fail.
716
   *
717
   * @todo move to simpletest drupal_web_test_case.php.
718
   */
719
  protected function assertNoOption($id, $option, $message = '') {
720
    $selects = $this->xpath('//select[@id=:id]', array(':id' => $id));
721
    $options = $this->xpath('//select[@id=:id]//option[@value=:option]', array(':id' => $id, ':option' => $option));
722
    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'));
723
  }
724
}