Projet

Général

Profil

Paste
Télécharger (18,2 ko) Statistiques
| Branche: | Révision:

root / drupal7 / sites / all / modules / admin_menu / tests / admin_menu.test @ 76df55b7

1
<?php
2

    
3
/**
4
 * @file
5
 * Tests for the Administration menu module.
6
 */
7

    
8
/**
9
 * Base class for all administration menu web test cases.
10
 */
11
class AdminMenuWebTestCase extends DrupalWebTestCase {
12
  protected $profile = 'testing';
13

    
14
  protected $basePermissions = array(
15
    'system' => 'access administration pages',
16
    'admin_menu' => 'access administration menu',
17
  );
18

    
19
  function setUp() {
20
    // Enable admin menu module and any other modules.
21
    $modules = func_get_args();
22
    $modules = isset($modules[0]) ? $modules[0] : $modules;
23
    $modules[] = 'admin_menu';
24
    parent::setUp($modules);
25

    
26
    // Disable client-side caching.
27
    variable_set('admin_menu_cache_client', FALSE);
28
    // Disable Clean URLs to ensure drupal.org testbot compatibility.
29
    variable_set('clean_url', 0);
30
  }
31

    
32
  /**
33
   * Check that an element exists in HTML markup.
34
   *
35
   * @param $xpath
36
   *   An XPath expression.
37
   * @param array $arguments
38
   *   (optional) An associative array of XPath replacement tokens to pass to
39
   *   DrupalWebTestCase::buildXPathQuery().
40
   * @param $message
41
   *   The message to display along with the assertion.
42
   * @param $group
43
   *   The type of assertion - examples are "Browser", "PHP".
44
   *
45
   * @return
46
   *   TRUE if the assertion succeeded, FALSE otherwise.
47
   */
48
  protected function assertElementByXPath($xpath, array $arguments = array(), $message, $group = 'Other') {
49
    $elements = $this->xpath($xpath, $arguments);
50
    return $this->assertTrue(!empty($elements[0]), $message, $group);
51
  }
52

    
53
  /**
54
   * Check that an element does not exist in HTML markup.
55
   *
56
   * @param $xpath
57
   *   An XPath expression.
58
   * @param array $arguments
59
   *   (optional) An associative array of XPath replacement tokens to pass to
60
   *   DrupalWebTestCase::buildXPathQuery().
61
   * @param $message
62
   *   The message to display along with the assertion.
63
   * @param $group
64
   *   The type of assertion - examples are "Browser", "PHP".
65
   *
66
   * @return
67
   *   TRUE if the assertion succeeded, FALSE otherwise.
68
   */
69
  protected function assertNoElementByXPath($xpath, array $arguments = array(), $message, $group = 'Other') {
70
    $elements = $this->xpath($xpath, $arguments);
71
    return $this->assertTrue(empty($elements), $message, $group);
72
  }
73

    
74
  /**
75
   * Asserts that links appear in the menu in a specified trail.
76
   *
77
   * @param array $trail
78
   *   A list of menu link titles to assert in the menu.
79
   */
80
  protected function assertLinkTrailByTitle(array $trail) {
81
    $xpath = array();
82
    $args = array();
83
    $message = '';
84
    foreach ($trail as $i => $title) {
85
      $xpath[] = '/li/a[text()=:title' . $i . ']';
86
      $args[':title' . $i] = $title;
87
      $message .= ($i ? ' » ' : '') . check_plain($title);
88
    }
89
    $xpath = '//div[@id="admin-menu"]/div/ul' . implode('/parent::li/ul', $xpath);
90
    $this->assertElementByXPath($xpath, $args, $message . ' link found.');
91
  }
92

    
93
  /**
94
   * Asserts that no link appears in the menu for a specified trail.
95
   *
96
   * @param array $trail
97
   *   A list of menu link titles to assert in the menu.
98
   */
99
  protected function assertNoLinkTrailByTitle(array $trail) {
100
    $xpath = array();
101
    $args = array();
102
    $message = '';
103
    foreach ($trail as $i => $title) {
104
      $xpath[] = '/li/a[text()=:title' . $i . ']';
105
      $args[':title' . $i] = $title;
106
      $message .= ($i ? ' » ' : '') . check_plain($title);
107
    }
108
    $xpath = '//div[@id="admin-menu"]/div/ul' . implode('/parent::li/ul', $xpath);
109
    $this->assertNoElementByXPath($xpath, $args, $message . ' link not found.');
110
  }
111
}
112

    
113
/**
114
 * Tests menu links depending on user permissions.
115
 */
116
class AdminMenuPermissionsTestCase extends AdminMenuWebTestCase {
117
  public static function getInfo() {
118
    return array(
119
      'name' => 'Menu link access permissions',
120
      'description' => 'Tests appearance of menu links depending on user permissions.',
121
      'group' => 'Administration menu',
122
    );
123
  }
124

    
125
  function setUp() {
126
    parent::setUp(array('node'));
127
  }
128

    
129
  /**
130
   * Test that the links are added to the page (no JS testing).
131
   */
132
  function testPermissions() {
133
    module_enable(array('contact'));
134
    $this->resetAll();
135

    
136
    // Anonymous users should not see the menu.
137
    $this->drupalGet('');
138
    $this->assertNoElementByXPath('//div[@id="admin-menu"]', array(), t('Administration menu not found.'));
139

    
140
    // Create a user who
141
    // - can access content overview
142
    // - cannot access drupal.org links
143
    // - cannot administer Contact module
144
    $permissions = $this->basePermissions + array(
145
      'access content overview',
146
    );
147
    $admin_user = $this->drupalCreateUser($permissions);
148
    $this->drupalLogin($admin_user);
149

    
150
    // Check that the user can see the admin links, but not the drupal links.
151
    $this->assertElementByXPath('//div[@id="admin-menu"]', array(), 'Administration menu found.');
152
    $this->assertElementByXPath('//div[@id="admin-menu"]//a[contains(@href, :path)]', array(':path' => 'admin/content'), 'Content link found.');
153
    $this->assertNoElementByXPath('//div[@id="admin-menu"]//a[@href=:path]', array(':path' => 'http://drupal.org'), 'Icon » Drupal.org link not found.');
154
    $this->assertNoElementByXPath('//div[@id="admin-menu"]//a[contains(@href, :path)]', array(':path' => 'admin/structure/contact'), 'Structure » Contact link not found.');
155

    
156
    // Create a user "reversed" to the above; i.e., who
157
    // - cannot access content overview
158
    // - can access drupal.org links
159
    // - can administer Contact module
160
    $permissions = $this->basePermissions + array(
161
      'display drupal links',
162
      'administer contact forms',
163
    );
164
    $admin_user2 = $this->drupalCreateUser($permissions);
165
    $this->drupalLogin($admin_user2);
166
    $this->assertElementByXPath('//div[@id="admin-menu"]', array(), 'Administration menu found.');
167
    $this->assertNoElementByXPath('//div[@id="admin-menu"]//a[contains(@href, :path)]', array(':path' => 'admin/content'), 'Content link not found.');
168
    $this->assertElementByXPath('//div[@id="admin-menu"]//a[@href=:path]', array(':path' => 'http://drupal.org'), 'Icon » Drupal.org link found.');
169
    $this->assertElementByXPath('//div[@id="admin-menu"]//a[contains(@href, :path)]', array(':path' => 'admin/structure/contact'), 'Structure » Contact link found.');
170
  }
171

    
172
  /**
173
   * Tests handling of links pointing to category/overview pages.
174
   */
175
  function testCategories() {
176
    // Create a user with minimum permissions.
177
    $admin_user = $this->drupalCreateUser($this->basePermissions);
178
    $this->drupalLogin($admin_user);
179

    
180
    // Verify that no category links appear.
181
    $this->assertNoLinkTrailByTitle(array(t('Structure')));
182
    $this->assertNoLinkTrailByTitle(array(t('Configuration')));
183

    
184
    // Create a user with access to one configuration category.
185
    $permissions = $this->basePermissions + array(
186
      'administer users',
187
    );
188
    $admin_user = $this->drupalCreateUser($permissions);
189
    $this->drupalLogin($admin_user);
190

    
191
    // Verify that only expected category links appear.
192
    $this->assertNoLinkTrailByTitle(array(t('Structure')));
193
    $this->assertLinkTrailByTitle(array(t('People')));
194
    $this->assertLinkTrailByTitle(array(t('Configuration')));
195
    $this->assertLinkTrailByTitle(array(t('Configuration'), t('People')));
196
    // Random picks are sufficient.
197
    $this->assertNoLinkTrailByTitle(array(t('Configuration'), t('Media')));
198
    $this->assertNoLinkTrailByTitle(array(t('Configuration'), t('System')));
199
  }
200

    
201
  /**
202
   * Tests that user role and permission changes are properly taken up.
203
   */
204
  function testPermissionChanges() {
205
    // Create a user who is able to change permissions.
206
    $permissions = $this->basePermissions + array(
207
      'administer permissions',
208
    );
209
    $admin_user = $this->drupalCreateUser($permissions);
210
    $this->drupalLogin($admin_user);
211

    
212
    // Extract the user role ID that was created for above permissions.
213
    $rid = key(array_diff_key($admin_user->roles, array(DRUPAL_AUTHENTICATED_RID => 0)));
214

    
215
    // Verify that Configuration does not appear.
216
    $this->assertNoLinkTrailByTitle(array(t('Configuration')));
217
    // Grant the 'administer site configuration' permission to ourselves.
218
    $edit = array(
219
      $rid . '[administer site configuration]' => TRUE,
220
    );
221
    $this->drupalPost('admin/people/permissions', $edit, t('Save permissions'));
222
    // Verify that Configuration appears.
223
    $this->assertLinkTrailByTitle(array(t('Configuration')));
224

    
225
    // Verify that Structure » Content types does not appear.
226
    $this->assertNoLinkTrailByTitle(array(t('Structure'), t('Content types')));
227
    // Create a new role.
228
    $edit = array(
229
      'name' => 'test',
230
    );
231
    $this->drupalPost('admin/people/permissions/roles', $edit, t('Add role'));
232
    // It should be safe to assume that the new role gets the next ID.
233
    $test_rid = $rid + 1;
234
    // Grant the 'administer content types' permission for the role.
235
    $edit = array(
236
      $test_rid . '[administer content types]' => TRUE,
237
    );
238
    $this->drupalPost('admin/people/permissions/' . $test_rid, $edit, t('Save permissions'));
239
    // Verify that Structure » Content types does not appear.
240
    $this->assertNoLinkTrailByTitle(array(t('Structure'), t('Content types')));
241

    
242
    // Assign the role to ourselves.
243
    $edit = array(
244
      'roles[' . $test_rid . ']' => TRUE,
245
    );
246
    $this->drupalPost('user/' . $admin_user->uid . '/edit', $edit, t('Save'));
247
    // Verify that Structure » Content types appears.
248
    $this->assertLinkTrailByTitle(array(t('Structure'), t('Content types')));
249

    
250
    // Delete the role.
251
    $this->drupalPost('admin/people/permissions/roles/edit/' . $test_rid, array(), t('Delete role'));
252
    $this->drupalPost(NULL, array(), t('Delete'));
253
    // Verify that Structure » Content types does not appear.
254
    $this->assertNoLinkTrailByTitle(array(t('Structure'), t('Content types')));
255
  }
256
}
257

    
258
/**
259
 * Tests appearance, localization, and escaping of dynamic links.
260
 */
261
class AdminMenuDynamicLinksTestCase extends AdminMenuWebTestCase {
262
  public static function getInfo() {
263
    return array(
264
      'name' => 'Dynamic links',
265
      'description' => 'Tests appearance, localization, and escaping of dynamic links.',
266
      'group' => 'Administration menu',
267
    );
268
  }
269

    
270
  function setUp() {
271
    parent::setUp(array('node'));
272
  }
273

    
274
  /**
275
   * Tests node type links.
276
   */
277
  function testNode() {
278
    $type = $this->drupalCreateContentType(array('type' => 'article', 'name' => 'Article'));
279
    // Create a content-type with special characters.
280
    $type = $this->drupalCreateContentType(array('type' => 'special', 'name' => 'Cool & Special'));
281

    
282
    $permissions = $this->basePermissions + array(
283
      'administer content types',
284
      'create article content',
285
      'create special content',
286
    );
287
    $this->admin_user = $this->drupalCreateUser($permissions);
288
    $this->drupalLogin($this->admin_user);
289

    
290
    // Verify that dynamic links are displayed.
291
    $this->drupalGet('');
292
    $this->assertElementByXPath('//div[@id="admin-menu"]', array(), t('Administration menu found.'));
293
    $this->assertElementByXPath('//div[@id="admin-menu"]//a[contains(@href, :path)]', array(':path' => 'admin/structure/types'), "Structure » Content types link found.");
294

    
295
    // Verify link title output escaping.
296
    $this->assertNoRaw('Cool & Special');
297
    $this->assertRaw(check_plain('Cool & Special'));
298

    
299
    // Verify Manage content type links.
300
    $links = array(
301
      'admin/structure/types/manage/article' => 'Article',
302
      'admin/structure/types/manage/special' => 'Cool & Special',
303
    );
304
    foreach ($links as $path => $title) {
305
      $this->assertElementByXPath('//div[@id="admin-menu"]//a[contains(@href, :path) and text()=:title]', array(
306
        ':path' => $path,
307
        ':title' => $title,
308
      ), "Structure » Content types » $title link found.");
309
    }
310

    
311
    // Verify Add content links.
312
    $links = array(
313
      'node/add/article' => 'Article',
314
      'node/add/special' => 'Cool & Special',
315
    );
316
    foreach ($links as $path => $title) {
317
      $this->assertElementByXPath('//div[@id="admin-menu"]//a[contains(@href, :path) and text()=:title]', array(
318
        ':path' => $path,
319
        ':title' => $title,
320
      ), "Add content » $title link found.");
321
    }
322
  }
323

    
324
  /**
325
   * Tests Add content links.
326
   */
327
  function testNodeAdd() {
328
    $type = $this->drupalCreateContentType(array('type' => 'article', 'name' => 'Article'));
329

    
330
    // Verify that "Add content" does not appear for unprivileged users.
331
    $permissions = $this->basePermissions + array(
332
      'access content',
333
    );
334
    $this->web_user = $this->drupalCreateUser($permissions);
335
    $this->drupalLogin($this->web_user);
336
    $this->assertNoText(t('Add content'));
337

    
338
    // Verify "Add content" appears below "Content" for administrative users.
339
    $permissions = $this->basePermissions + array(
340
      'access content overview',
341
      'access content',
342
      'create article content',
343
    );
344
    $this->admin_user = $this->drupalCreateUser($permissions);
345
    $this->drupalLogin($this->admin_user);
346
    $this->assertLinkTrailByTitle(array(
347
      t('Content'),
348
      t('Add content'),
349
    ));
350

    
351
    // Verify "Add content" appears on the top-level for regular users.
352
    $permissions = $this->basePermissions + array(
353
      'access content',
354
      'create article content',
355
    );
356
    $this->web_user = $this->drupalCreateUser($permissions);
357
    $this->drupalLogin($this->web_user);
358
    $this->assertLinkTrailByTitle(array(
359
      t('Add content'),
360
    ));
361
  }
362
}
363

    
364
/**
365
 * Tests appearance of different types of links.
366
 */
367
class AdminMenuLinkTypesTestCase extends AdminMenuWebTestCase {
368
  public static function getInfo() {
369
    return array(
370
      'name' => 'Link types',
371
      'description' => 'Tests appearance of different types of links.',
372
      'group' => 'Administration menu',
373
    );
374
  }
375

    
376
  function setUp() {
377
    parent::setUp(array('help'));
378

    
379
    $permissions = module_invoke_all('permission');
380
    $permissions = array_keys($permissions);
381
    $this->admin_user = $this->drupalCreateUser($permissions);
382
    $this->drupalLogin($this->admin_user);
383
  }
384

    
385
  /**
386
   * Tests appearance of different router item link types.
387
   */
388
  function testLinkTypes() {
389
    // Verify that MENU_NORMAL_ITEMs appear.
390
    $this->assertLinkTrailByTitle(array(
391
      t('Configuration'),
392
      t('System'),
393
      t('Site information'),
394
    ));
395

    
396
    // Verify that MENU_LOCAL_TASKs appear.
397
    $this->assertLinkTrailByTitle(array(t('People'), t('Permissions')));
398
    $this->assertLinkTrailByTitle(array(t('Appearance'), t('Settings')));
399
    $this->assertLinkTrailByTitle(array(t('Modules'), t('Uninstall')));
400

    
401
    // Verify that MENU_LOCAL_ACTIONs appear.
402
    $this->assertLinkTrailByTitle(array(
403
      t('People'),
404
      t('Add user'),
405
    ));
406

    
407
    // Verify that MENU_DEFAULT_LOCAL_TASKs do NOT appear.
408
    $this->assertNoLinkTrailByTitle(array(t('Modules'), t('List')));
409
    $this->assertNoLinkTrailByTitle(array(t('People'), t('List')));
410
    $this->assertNoLinkTrailByTitle(array(t('People'), t('Permissions'), t('Permissions')));
411
    $this->assertNoLinkTrailByTitle(array(t('Appearance'), t('List')));
412

    
413
    // Verify that MENU_VISIBLE_IN_BREADCRUMB items (exact type) do NOT appear.
414
    $this->assertNoLinkTrailByTitle(array(t('Modules'), t('Uninstall'), t('Uninstall')));
415
    $this->assertNoLinkTrailByTitle(array(t('Help'), 'admin_menu'));
416

    
417
    // Verify that special "Index" link appears below icon.
418
    $this->assertElementByXPath('//div[@id="admin-menu"]//a[contains(@href, :path) and text()=:title]', array(
419
      ':path' => 'admin/index',
420
      ':title' => t('Index'),
421
    ), "Icon » Index link found.");
422
  }
423
}
424

    
425
/**
426
 * Tests customized menu links.
427
 */
428
class AdminMenuCustomizedTestCase extends AdminMenuWebTestCase {
429
  public static function getInfo() {
430
    return array(
431
      'name' => 'Customized links',
432
      'description' => 'Tests customized menu links.',
433
      'group' => 'Administration menu',
434
    );
435
  }
436

    
437
  function setUp() {
438
    parent::setUp(array('menu'));
439

    
440
    $this->admin_user = $this->drupalCreateUser($this->basePermissions + array(
441
      'administer menu',
442
    ));
443
    $this->drupalLogin($this->admin_user);
444
  }
445

    
446
  /**
447
   * Test disabled custom links.
448
   */
449
  function testCustomDisabled() {
450
    $type = $this->drupalCreateContentType();
451
    $node = $this->drupalCreateNode(array('type' => $type->type));
452
    $text = $this->randomName();
453
    $xpath = $this->buildXPathQuery('//div[@id=:id]//a[contains(text(), :text)]', array(
454
      ':id' => 'admin-menu',
455
      ':text' => $text,
456
    ));
457

    
458
    // Verify that the link does not appear in the menu.
459
    $this->drupalGet('node');
460
    $elements = $this->xpath($xpath);
461
    $this->assertFalse($elements, 'Custom link not found.');
462

    
463
    // Add a custom link to the node to the menu.
464
    $edit = array(
465
      'link_path' => 'node/' . $node->nid,
466
      'link_title' => $text,
467
      'parent' => 'management:' . $this->queryMlidByPath('admin'),
468
    );
469
    $this->drupalPost('admin/structure/menu/manage/management/add', $edit, t('Save'));
470

    
471
    // Verify that the link appears in the menu.
472
    $this->drupalGet('node');
473
    $elements = $this->xpath($xpath);
474
    $this->assertTrue($elements, 'Custom link found.');
475

    
476
    // Disable the link.
477
    $edit = array(
478
      'enabled' => FALSE,
479
    );
480
    $this->drupalPost('admin/structure/menu/item/' . $this->queryMlidByPath('node/' . $node->nid) . '/edit', $edit, t('Save'));
481

    
482
    // Verify that the disabled link does not appear in the menu.
483
    $this->drupalGet('node');
484
    $elements = $this->xpath($xpath);
485
    $this->assertFalse($elements, 'Disabled custom link not found.');
486
  }
487

    
488
  /**
489
   * Tests external links.
490
   */
491
  function testCustomExternal() {
492
    // Add a custom link to the node to the menu.
493
    $edit = array(
494
      'link_path' => 'http://example.com',
495
      'link_title' => 'Example',
496
      'parent' => 'management:' . $this->queryMlidByPath('admin'),
497
    );
498
    $this->drupalPost('admin/structure/menu/manage/management/add', $edit, t('Save'));
499

    
500
    // Verify that the link appears in the menu.
501
    $this->drupalGet('');
502
    $elements = $this->xpath('//div[@id=:id]//a[@href=:href and contains(text(), :text)]', array(
503
      ':id' => 'admin-menu',
504
      ':href' => $edit['link_path'],
505
      ':text' => $edit['link_title'],
506
    ));
507
    $this->assertTrue($elements, 'External link found.');
508
  }
509

    
510
  /**
511
   * Returns the menu link ID for a given link path in the management menu.
512
   */
513
  protected function queryMlidByPath($path) {
514
    return db_query('SELECT mlid FROM {menu_links} WHERE menu_name = :menu AND link_path = :path', array(
515
      ':menu' => 'management',
516
      ':path' => $path,
517
    ))->fetchField();
518
  }
519
}
520