More updates to stop using dev or alpha or beta versions.
[yaffs-website] / web / core / modules / system / src / Tests / System / AdminTest.php
1 <?php
2
3 namespace Drupal\system\Tests\System;
4
5 use Drupal\Core\Menu\MenuTreeParameters;
6 use Drupal\simpletest\WebTestBase;
7
8 /**
9  * Tests output on administrative pages and compact mode functionality.
10  *
11  * @group system
12  */
13 class AdminTest extends WebTestBase {
14
15   /**
16    * User account with all available permissions
17    *
18    * @var \Drupal\Core\Session\AccountInterface
19    */
20   protected $adminUser;
21
22   /**
23    * User account with limited access to administration pages.
24    *
25    * @var \Drupal\Core\Session\AccountInterface
26    */
27   protected $webUser;
28
29   /**
30    * Modules to enable.
31    *
32    * @var array
33    */
34   public static $modules = ['locale'];
35
36   protected function setUp() {
37     // testAdminPages() requires Locale module.
38     parent::setUp();
39
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',
47     ]);
48     $this->drupalLogin($this->adminUser);
49   }
50
51   /**
52    * Tests output on administrative listing pages.
53    */
54   public function testAdminPages() {
55     // Go to Administration.
56     $this->drupalGet('admin');
57
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());
65     }
66
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.
72     $admin_list_pages = [
73       'admin/index',
74       'admin/config',
75       'admin/config/regional',
76     ];
77
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
81       // pages.
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");
95       }
96
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");
112       }
113     }
114   }
115
116   /**
117    * Returns all top level menu links.
118    *
119    * @return \Drupal\Core\Menu\MenuLinkInterface[]
120    */
121   protected function getTopLevelMenuLinks() {
122     $menu_tree = \Drupal::menuTree();
123
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);
128     $manipulators = [
129       ['callable' => 'menu.default_tree_manipulators:checkAccess'],
130       ['callable' => 'menu.default_tree_manipulators:flatten'],
131     ];
132     $tree = $menu_tree->transform($tree, $manipulators);
133
134     // Transform the tree to a list of menu links.
135     $menu_links = [];
136     foreach ($tree as $element) {
137       $menu_links[] = $element->link;
138     }
139
140     return $menu_links;
141   }
142
143   /**
144    * Test compact mode.
145    */
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();
151
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.');
160
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.');
169   }
170
171 }