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