3 namespace Drupal\user\Tests;
5 use Drupal\simpletest\WebTestBase;
6 use Drupal\user\Entity\Role;
7 use Drupal\user\RoleInterface;
10 * Tests adding, editing and deleting user roles and changing role weights.
14 class UserRoleAdminTest extends WebTestBase {
17 * User with admin privileges.
19 * @var \Drupal\user\UserInterface
28 public static $modules = ['block'];
33 protected function setUp() {
35 $this->adminUser = $this->drupalCreateUser(['administer permissions', 'administer users']);
36 $this->drupalPlaceBlock('local_tasks_block');
40 * Test adding, renaming and deleting roles.
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' => t('Roles'),
51 $this->assertEqual(count($tabs), 1, 'Found roles tab');
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.)
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.');
63 // Check that the role was created in site default language.
64 $this->assertEqual($role->language()->getId(), $default_langcode);
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.');
70 // Test renaming a role.
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.');
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.');
88 // Make sure that the system-defined roles can be edited via the user
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.');
99 * Test user role weight change operation and ordering.
101 public function testRoleWeightOrdering() {
102 $this->drupalLogin($this->adminUser);
103 $roles = user_roles();
104 $weight = count($roles);
105 $new_role_weights = [];
108 // Change the role weights to make the roles in reverse order.
110 foreach ($roles as $role) {
111 $edit['entities[' . $role->id() . '][weight]'] = $weight;
112 $new_role_weights[$role->id()] = $weight;
113 $saved_rids[] = $role->id();
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.');
119 // Load up the user roles with the new weights.
120 drupal_static_reset('user_roles');
121 $roles = user_roles();
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();
128 // The order of the roles should be reversed.
129 $this->assertIdentical($rids, array_reverse($saved_rids));