Mercurial > hg > isophonics-drupal-site
comparison core/modules/user/tests/src/Functional/UserRoleAdminTest.php @ 14:1fec387a4317
Update Drupal core to 8.5.2 via Composer
author | Chris Cannam |
---|---|
date | Mon, 23 Apr 2018 09:46:53 +0100 |
parents | |
children | af1871eacc83 |
comparison
equal
deleted
inserted
replaced
13:5fb285c0d0e3 | 14:1fec387a4317 |
---|---|
1 <?php | |
2 | |
3 namespace Drupal\Tests\user\Functional; | |
4 | |
5 use Drupal\Tests\BrowserTestBase; | |
6 use Drupal\user\Entity\Role; | |
7 use Drupal\user\RoleInterface; | |
8 | |
9 /** | |
10 * Tests adding, editing and deleting user roles and changing role weights. | |
11 * | |
12 * @group user | |
13 */ | |
14 class UserRoleAdminTest extends BrowserTestBase { | |
15 | |
16 /** | |
17 * User with admin privileges. | |
18 * | |
19 * @var \Drupal\user\UserInterface | |
20 */ | |
21 protected $adminUser; | |
22 | |
23 /** | |
24 * Modules to enable. | |
25 * | |
26 * @var string[] | |
27 */ | |
28 public static $modules = ['block']; | |
29 | |
30 /** | |
31 * {@inheritdoc} | |
32 */ | |
33 protected function setUp() { | |
34 parent::setUp(); | |
35 $this->adminUser = $this->drupalCreateUser(['administer permissions', 'administer users']); | |
36 $this->drupalPlaceBlock('local_tasks_block'); | |
37 } | |
38 | |
39 /** | |
40 * Test adding, renaming and deleting roles. | |
41 */ | |
42 public function testRoleAdministration() { | |
43 $this->drupalLogin($this->adminUser); | |
44 $default_langcode = \Drupal::languageManager()->getDefaultLanguage()->getId(); | |
45 // Test presence of tab. | |
46 $this->drupalGet('admin/people/permissions'); | |
47 $tabs = $this->xpath('//ul[@class=:classes and //a[contains(., :text)]]', [ | |
48 ':classes' => 'tabs primary', | |
49 ':text' => 'Roles', | |
50 ]); | |
51 $this->assertEqual(count($tabs), 1, 'Found roles tab'); | |
52 | |
53 // Test adding a role. (In doing so, we use a role name that happens to | |
54 // correspond to an integer, to test that the role administration pages | |
55 // correctly distinguish between role names and IDs.) | |
56 $role_name = '123'; | |
57 $edit = ['label' => $role_name, 'id' => $role_name]; | |
58 $this->drupalPostForm('admin/people/roles/add', $edit, t('Save')); | |
59 $this->assertRaw(t('Role %label has been added.', ['%label' => 123])); | |
60 $role = Role::load($role_name); | |
61 $this->assertTrue(is_object($role), 'The role was successfully retrieved from the database.'); | |
62 | |
63 // Check that the role was created in site default language. | |
64 $this->assertEqual($role->language()->getId(), $default_langcode); | |
65 | |
66 // Try adding a duplicate role. | |
67 $this->drupalPostForm('admin/people/roles/add', $edit, t('Save')); | |
68 $this->assertRaw(t('The machine-readable name is already in use. It must be unique.'), 'Duplicate role warning displayed.'); | |
69 | |
70 // Test renaming a role. | |
71 $role_name = '456'; | |
72 $edit = ['label' => $role_name]; | |
73 $this->drupalPostForm("admin/people/roles/manage/{$role->id()}", $edit, t('Save')); | |
74 $this->assertRaw(t('Role %label has been updated.', ['%label' => $role_name])); | |
75 \Drupal::entityManager()->getStorage('user_role')->resetCache([$role->id()]); | |
76 $new_role = Role::load($role->id()); | |
77 $this->assertEqual($new_role->label(), $role_name, 'The role name has been successfully changed.'); | |
78 | |
79 // Test deleting a role. | |
80 $this->drupalGet("admin/people/roles/manage/{$role->id()}"); | |
81 $this->clickLink(t('Delete')); | |
82 $this->drupalPostForm(NULL, [], t('Delete')); | |
83 $this->assertRaw(t('The role %label has been deleted.', ['%label' => $role_name])); | |
84 $this->assertNoLinkByHref("admin/people/roles/manage/{$role->id()}", 'Role edit link removed.'); | |
85 \Drupal::entityManager()->getStorage('user_role')->resetCache([$role->id()]); | |
86 $this->assertFalse(Role::load($role->id()), 'A deleted role can no longer be loaded.'); | |
87 | |
88 // Make sure that the system-defined roles can be edited via the user | |
89 // interface. | |
90 $this->drupalGet('admin/people/roles/manage/' . RoleInterface::ANONYMOUS_ID); | |
91 $this->assertResponse(200, 'Access granted when trying to edit the built-in anonymous role.'); | |
92 $this->assertNoText(t('Delete role'), 'Delete button for the anonymous role is not present.'); | |
93 $this->drupalGet('admin/people/roles/manage/' . RoleInterface::AUTHENTICATED_ID); | |
94 $this->assertResponse(200, 'Access granted when trying to edit the built-in authenticated role.'); | |
95 $this->assertNoText(t('Delete role'), 'Delete button for the authenticated role is not present.'); | |
96 } | |
97 | |
98 /** | |
99 * Test user role weight change operation and ordering. | |
100 */ | |
101 public function testRoleWeightOrdering() { | |
102 $this->drupalLogin($this->adminUser); | |
103 $roles = user_roles(); | |
104 $weight = count($roles); | |
105 $new_role_weights = []; | |
106 $saved_rids = []; | |
107 | |
108 // Change the role weights to make the roles in reverse order. | |
109 $edit = []; | |
110 foreach ($roles as $role) { | |
111 $edit['entities[' . $role->id() . '][weight]'] = $weight; | |
112 $new_role_weights[$role->id()] = $weight; | |
113 $saved_rids[] = $role->id(); | |
114 $weight--; | |
115 } | |
116 $this->drupalPostForm('admin/people/roles', $edit, t('Save')); | |
117 $this->assertText(t('The role settings have been updated.'), 'The role settings form submitted successfully.'); | |
118 | |
119 // Load up the user roles with the new weights. | |
120 drupal_static_reset('user_roles'); | |
121 $roles = user_roles(); | |
122 $rids = []; | |
123 // Test that the role weights have been correctly saved. | |
124 foreach ($roles as $role) { | |
125 $this->assertEqual($role->getWeight(), $new_role_weights[$role->id()]); | |
126 $rids[] = $role->id(); | |
127 } | |
128 // The order of the roles should be reversed. | |
129 $this->assertIdentical($rids, array_reverse($saved_rids)); | |
130 } | |
131 | |
132 } |