3 namespace Drupal\system\Tests\System;
5 use Drupal\Core\Menu\MenuTreeParameters;
6 use Drupal\simpletest\WebTestBase;
9 * Tests output on administrative pages and compact mode functionality.
13 class AdminTest extends WebTestBase {
16 * User account with all available permissions
18 * @var \Drupal\Core\Session\AccountInterface
23 * User account with limited access to administration pages.
25 * @var \Drupal\Core\Session\AccountInterface
34 public static $modules = ['locale'];
36 protected function setUp() {
37 // testAdminPages() requires Locale module.
40 // Create an administrator with all permissions, as well as a regular user
41 // who can only access administration pages and perform some Locale module
42 // administrative tasks, but not all of them.
43 $this->adminUser = $this->drupalCreateUser(array_keys(\Drupal::service('user.permissions')->getPermissions()));
44 $this->webUser = $this->drupalCreateUser([
45 'access administration pages',
46 'translate interface',
48 $this->drupalLogin($this->adminUser);
52 * Tests output on administrative listing pages.
54 public function testAdminPages() {
55 // Go to Administration.
56 $this->drupalGet('admin');
58 // Verify that all visible, top-level administration links are listed on
59 // the main administration page.
60 foreach ($this->getTopLevelMenuLinks() as $item) {
61 $this->assertLink($item->getTitle());
62 $this->assertLinkByHref($item->getUrlObject()->toString());
63 // The description should appear below the link.
64 $this->assertText($item->getDescription());
67 // For each administrative listing page on which the Locale module appears,
68 // verify that there are links to the module's primary configuration pages,
69 // but no links to its individual sub-configuration pages. Also verify that
70 // a user with access to only some Locale module administration pages only
71 // sees links to the pages they have access to.
75 'admin/config/regional',
78 foreach ($admin_list_pages as $page) {
79 // For the administrator, verify that there are links to Locale's primary
80 // configuration pages, but no links to individual sub-configuration
82 $this->drupalLogin($this->adminUser);
83 $this->drupalGet($page);
84 $this->assertLinkByHref('admin/config');
85 $this->assertLinkByHref('admin/config/regional/settings');
86 $this->assertLinkByHref('admin/config/regional/date-time');
87 $this->assertLinkByHref('admin/config/regional/language');
88 $this->assertNoLinkByHref('admin/config/regional/language/detection/session');
89 $this->assertNoLinkByHref('admin/config/regional/language/detection/url');
90 $this->assertLinkByHref('admin/config/regional/translate');
91 // On admin/index only, the administrator should also see a "Configure
92 // permissions" link for the Locale module.
93 if ($page == 'admin/index') {
94 $this->assertLinkByHref("admin/people/permissions#module-locale");
97 // For a less privileged user, verify that there are no links to Locale's
98 // primary configuration pages, but a link to the translate page exists.
99 $this->drupalLogin($this->webUser);
100 $this->drupalGet($page);
101 $this->assertLinkByHref('admin/config');
102 $this->assertNoLinkByHref('admin/config/regional/settings');
103 $this->assertNoLinkByHref('admin/config/regional/date-time');
104 $this->assertNoLinkByHref('admin/config/regional/language');
105 $this->assertNoLinkByHref('admin/config/regional/language/detection/session');
106 $this->assertNoLinkByHref('admin/config/regional/language/detection/url');
107 $this->assertLinkByHref('admin/config/regional/translate');
108 // This user cannot configure permissions, so even on admin/index should
109 // not see a "Configure permissions" link for the Locale module.
110 if ($page == 'admin/index') {
111 $this->assertNoLinkByHref("admin/people/permissions#module-locale");
117 * Returns all top level menu links.
119 * @return \Drupal\Core\Menu\MenuLinkInterface[]
121 protected function getTopLevelMenuLinks() {
122 $menu_tree = \Drupal::menuTree();
124 // The system.admin link is normally the parent of all top-level admin links.
125 $parameters = new MenuTreeParameters();
126 $parameters->setRoot('system.admin')->excludeRoot()->setTopLevelOnly()->onlyEnabledLinks();
127 $tree = $menu_tree->load(NULL, $parameters);
129 ['callable' => 'menu.default_tree_manipulators:checkAccess'],
130 ['callable' => 'menu.default_tree_manipulators:flatten'],
132 $tree = $menu_tree->transform($tree, $manipulators);
134 // Transform the tree to a list of menu links.
136 foreach ($tree as $element) {
137 $menu_links[] = $element->link;
146 public function testCompactMode() {
147 // The front page defaults to 'user/login', which redirects to 'user/{user}'
148 // for authenticated users. We cannot use '<front>', since this does not
149 // match the redirected url.
150 $frontpage_url = 'user/' . $this->adminUser->id();
152 $this->drupalGet('admin/compact/on');
153 $this->assertResponse(200, 'A valid page is returned after turning on compact mode.');
154 $this->assertUrl($frontpage_url, [], 'The user is redirected to the front page after turning on compact mode.');
155 $this->assertTrue($this->cookies['Drupal.visitor.admin_compact_mode']['value'], 'Compact mode turns on.');
156 $this->drupalGet('admin/compact/on');
157 $this->assertTrue($this->cookies['Drupal.visitor.admin_compact_mode']['value'], 'Compact mode remains on after a repeat call.');
158 $this->drupalGet('');
159 $this->assertTrue($this->cookies['Drupal.visitor.admin_compact_mode']['value'], 'Compact mode persists on new requests.');
161 $this->drupalGet('admin/compact/off');
162 $this->assertResponse(200, 'A valid page is returned after turning off compact mode.');
163 $this->assertUrl($frontpage_url, [], 'The user is redirected to the front page after turning off compact mode.');
164 $this->assertEqual($this->cookies['Drupal.visitor.admin_compact_mode']['value'], 'deleted', 'Compact mode turns off.');
165 $this->drupalGet('admin/compact/off');
166 $this->assertEqual($this->cookies['Drupal.visitor.admin_compact_mode']['value'], 'deleted', 'Compact mode remains off after a repeat call.');
167 $this->drupalGet('');
168 $this->assertTrue($this->cookies['Drupal.visitor.admin_compact_mode']['value'], 'Compact mode persists on new requests.');