PageRenderTime 40ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/core/modules/system/src/Tests/System/AdminTest.php

http://github.com/drupal/drupal
PHP | 171 lines | 92 code | 21 blank | 58 comment | 4 complexity | d9ebc798bed6d13552e6b3ce839a6e19 MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.1
  1. <?php
  2. namespace Drupal\system\Tests\System;
  3. use Drupal\Core\Menu\MenuTreeParameters;
  4. use Drupal\simpletest\WebTestBase;
  5. /**
  6. * Tests output on administrative pages and compact mode functionality.
  7. *
  8. * @group system
  9. */
  10. class AdminTest extends WebTestBase {
  11. /**
  12. * User account with all available permissions
  13. *
  14. * @var \Drupal\Core\Session\AccountInterface
  15. */
  16. protected $adminUser;
  17. /**
  18. * User account with limited access to administration pages.
  19. *
  20. * @var \Drupal\Core\Session\AccountInterface
  21. */
  22. protected $webUser;
  23. /**
  24. * Modules to enable.
  25. *
  26. * @var array
  27. */
  28. public static $modules = array('locale');
  29. protected function setUp() {
  30. // testAdminPages() requires Locale module.
  31. parent::setUp();
  32. // Create an administrator with all permissions, as well as a regular user
  33. // who can only access administration pages and perform some Locale module
  34. // administrative tasks, but not all of them.
  35. $this->adminUser = $this->drupalCreateUser(array_keys(\Drupal::service('user.permissions')->getPermissions()));
  36. $this->webUser = $this->drupalCreateUser(array(
  37. 'access administration pages',
  38. 'translate interface',
  39. ));
  40. $this->drupalLogin($this->adminUser);
  41. }
  42. /**
  43. * Tests output on administrative listing pages.
  44. */
  45. function testAdminPages() {
  46. // Go to Administration.
  47. $this->drupalGet('admin');
  48. // Verify that all visible, top-level administration links are listed on
  49. // the main administration page.
  50. foreach ($this->getTopLevelMenuLinks() as $item) {
  51. $this->assertLink($item->getTitle());
  52. $this->assertLinkByHref($item->getUrlObject()->toString());
  53. // The description should appear below the link.
  54. $this->assertText($item->getDescription());
  55. }
  56. // For each administrative listing page on which the Locale module appears,
  57. // verify that there are links to the module's primary configuration pages,
  58. // but no links to its individual sub-configuration pages. Also verify that
  59. // a user with access to only some Locale module administration pages only
  60. // sees links to the pages they have access to.
  61. $admin_list_pages = array(
  62. 'admin/index',
  63. 'admin/config',
  64. 'admin/config/regional',
  65. );
  66. foreach ($admin_list_pages as $page) {
  67. // For the administrator, verify that there are links to Locale's primary
  68. // configuration pages, but no links to individual sub-configuration
  69. // pages.
  70. $this->drupalLogin($this->adminUser);
  71. $this->drupalGet($page);
  72. $this->assertLinkByHref('admin/config');
  73. $this->assertLinkByHref('admin/config/regional/settings');
  74. $this->assertLinkByHref('admin/config/regional/date-time');
  75. $this->assertLinkByHref('admin/config/regional/language');
  76. $this->assertNoLinkByHref('admin/config/regional/language/detection/session');
  77. $this->assertNoLinkByHref('admin/config/regional/language/detection/url');
  78. $this->assertLinkByHref('admin/config/regional/translate');
  79. // On admin/index only, the administrator should also see a "Configure
  80. // permissions" link for the Locale module.
  81. if ($page == 'admin/index') {
  82. $this->assertLinkByHref("admin/people/permissions#module-locale");
  83. }
  84. // For a less privileged user, verify that there are no links to Locale's
  85. // primary configuration pages, but a link to the translate page exists.
  86. $this->drupalLogin($this->webUser);
  87. $this->drupalGet($page);
  88. $this->assertLinkByHref('admin/config');
  89. $this->assertNoLinkByHref('admin/config/regional/settings');
  90. $this->assertNoLinkByHref('admin/config/regional/date-time');
  91. $this->assertNoLinkByHref('admin/config/regional/language');
  92. $this->assertNoLinkByHref('admin/config/regional/language/detection/session');
  93. $this->assertNoLinkByHref('admin/config/regional/language/detection/url');
  94. $this->assertLinkByHref('admin/config/regional/translate');
  95. // This user cannot configure permissions, so even on admin/index should
  96. // not see a "Configure permissions" link for the Locale module.
  97. if ($page == 'admin/index') {
  98. $this->assertNoLinkByHref("admin/people/permissions#module-locale");
  99. }
  100. }
  101. }
  102. /**
  103. * Returns all top level menu links.
  104. *
  105. * @return \Drupal\Core\Menu\MenuLinkInterface[]
  106. */
  107. protected function getTopLevelMenuLinks() {
  108. $menu_tree = \Drupal::menuTree();
  109. // The system.admin link is normally the parent of all top-level admin links.
  110. $parameters = new MenuTreeParameters();
  111. $parameters->setRoot('system.admin')->excludeRoot()->setTopLevelOnly()->onlyEnabledLinks();
  112. $tree = $menu_tree->load(NULL, $parameters);
  113. $manipulators = array(
  114. array('callable' => 'menu.default_tree_manipulators:checkAccess'),
  115. array('callable' => 'menu.default_tree_manipulators:flatten'),
  116. );
  117. $tree = $menu_tree->transform($tree, $manipulators);
  118. // Transform the tree to a list of menu links.
  119. $menu_links = array();
  120. foreach ($tree as $element) {
  121. $menu_links[] = $element->link;
  122. }
  123. return $menu_links;
  124. }
  125. /**
  126. * Test compact mode.
  127. */
  128. function testCompactMode() {
  129. // The front page defaults to 'user/login', which redirects to 'user/{user}'
  130. // for authenticated users. We cannot use '<front>', since this does not
  131. // match the redirected url.
  132. $frontpage_url = 'user/' . $this->adminUser->id();
  133. $this->drupalGet('admin/compact/on');
  134. $this->assertResponse(200, 'A valid page is returned after turning on compact mode.');
  135. $this->assertUrl($frontpage_url, array(), 'The user is redirected to the front page after turning on compact mode.');
  136. $this->assertTrue($this->cookies['Drupal.visitor.admin_compact_mode']['value'], 'Compact mode turns on.');
  137. $this->drupalGet('admin/compact/on');
  138. $this->assertTrue($this->cookies['Drupal.visitor.admin_compact_mode']['value'], 'Compact mode remains on after a repeat call.');
  139. $this->drupalGet('');
  140. $this->assertTrue($this->cookies['Drupal.visitor.admin_compact_mode']['value'], 'Compact mode persists on new requests.');
  141. $this->drupalGet('admin/compact/off');
  142. $this->assertResponse(200, 'A valid page is returned after turning off compact mode.');
  143. $this->assertUrl($frontpage_url, array(), 'The user is redirected to the front page after turning off compact mode.');
  144. $this->assertEqual($this->cookies['Drupal.visitor.admin_compact_mode']['value'], 'deleted', 'Compact mode turns off.');
  145. $this->drupalGet('admin/compact/off');
  146. $this->assertEqual($this->cookies['Drupal.visitor.admin_compact_mode']['value'], 'deleted', 'Compact mode remains off after a repeat call.');
  147. $this->drupalGet('');
  148. $this->assertTrue($this->cookies['Drupal.visitor.admin_compact_mode']['value'], 'Compact mode persists on new requests.');
  149. }
  150. }