Chris@14: adminUser = $this->drupalCreateUser(['administer permissions', 'administer users']); Chris@14: $this->drupalPlaceBlock('local_tasks_block'); Chris@14: } Chris@14: Chris@14: /** Chris@14: * Test adding, renaming and deleting roles. Chris@14: */ Chris@14: public function testRoleAdministration() { Chris@14: $this->drupalLogin($this->adminUser); Chris@14: $default_langcode = \Drupal::languageManager()->getDefaultLanguage()->getId(); Chris@14: // Test presence of tab. Chris@14: $this->drupalGet('admin/people/permissions'); Chris@14: $tabs = $this->xpath('//ul[@class=:classes and //a[contains(., :text)]]', [ Chris@14: ':classes' => 'tabs primary', Chris@14: ':text' => 'Roles', Chris@14: ]); Chris@14: $this->assertEqual(count($tabs), 1, 'Found roles tab'); Chris@14: Chris@14: // Test adding a role. (In doing so, we use a role name that happens to Chris@14: // correspond to an integer, to test that the role administration pages Chris@14: // correctly distinguish between role names and IDs.) Chris@14: $role_name = '123'; Chris@14: $edit = ['label' => $role_name, 'id' => $role_name]; Chris@14: $this->drupalPostForm('admin/people/roles/add', $edit, t('Save')); Chris@14: $this->assertRaw(t('Role %label has been added.', ['%label' => 123])); Chris@14: $role = Role::load($role_name); Chris@14: $this->assertTrue(is_object($role), 'The role was successfully retrieved from the database.'); Chris@14: Chris@14: // Check that the role was created in site default language. Chris@14: $this->assertEqual($role->language()->getId(), $default_langcode); Chris@14: Chris@14: // Try adding a duplicate role. Chris@14: $this->drupalPostForm('admin/people/roles/add', $edit, t('Save')); Chris@14: $this->assertRaw(t('The machine-readable name is already in use. It must be unique.'), 'Duplicate role warning displayed.'); Chris@14: Chris@14: // Test renaming a role. Chris@14: $role_name = '456'; Chris@14: $edit = ['label' => $role_name]; Chris@14: $this->drupalPostForm("admin/people/roles/manage/{$role->id()}", $edit, t('Save')); Chris@14: $this->assertRaw(t('Role %label has been updated.', ['%label' => $role_name])); Chris@14: \Drupal::entityManager()->getStorage('user_role')->resetCache([$role->id()]); Chris@14: $new_role = Role::load($role->id()); Chris@14: $this->assertEqual($new_role->label(), $role_name, 'The role name has been successfully changed.'); Chris@14: Chris@14: // Test deleting a role. Chris@14: $this->drupalGet("admin/people/roles/manage/{$role->id()}"); Chris@14: $this->clickLink(t('Delete')); Chris@14: $this->drupalPostForm(NULL, [], t('Delete')); Chris@14: $this->assertRaw(t('The role %label has been deleted.', ['%label' => $role_name])); Chris@14: $this->assertNoLinkByHref("admin/people/roles/manage/{$role->id()}", 'Role edit link removed.'); Chris@14: \Drupal::entityManager()->getStorage('user_role')->resetCache([$role->id()]); Chris@14: $this->assertFalse(Role::load($role->id()), 'A deleted role can no longer be loaded.'); Chris@14: Chris@14: // Make sure that the system-defined roles can be edited via the user Chris@14: // interface. Chris@14: $this->drupalGet('admin/people/roles/manage/' . RoleInterface::ANONYMOUS_ID); Chris@14: $this->assertResponse(200, 'Access granted when trying to edit the built-in anonymous role.'); Chris@14: $this->assertNoText(t('Delete role'), 'Delete button for the anonymous role is not present.'); Chris@14: $this->drupalGet('admin/people/roles/manage/' . RoleInterface::AUTHENTICATED_ID); Chris@14: $this->assertResponse(200, 'Access granted when trying to edit the built-in authenticated role.'); Chris@14: $this->assertNoText(t('Delete role'), 'Delete button for the authenticated role is not present.'); Chris@14: } Chris@14: Chris@14: /** Chris@14: * Test user role weight change operation and ordering. Chris@14: */ Chris@14: public function testRoleWeightOrdering() { Chris@14: $this->drupalLogin($this->adminUser); Chris@14: $roles = user_roles(); Chris@14: $weight = count($roles); Chris@14: $new_role_weights = []; Chris@14: $saved_rids = []; Chris@14: Chris@14: // Change the role weights to make the roles in reverse order. Chris@14: $edit = []; Chris@14: foreach ($roles as $role) { Chris@14: $edit['entities[' . $role->id() . '][weight]'] = $weight; Chris@14: $new_role_weights[$role->id()] = $weight; Chris@14: $saved_rids[] = $role->id(); Chris@14: $weight--; Chris@14: } Chris@14: $this->drupalPostForm('admin/people/roles', $edit, t('Save')); Chris@14: $this->assertText(t('The role settings have been updated.'), 'The role settings form submitted successfully.'); Chris@14: Chris@14: // Load up the user roles with the new weights. Chris@14: $roles = user_roles(); Chris@14: $rids = []; Chris@14: // Test that the role weights have been correctly saved. Chris@14: foreach ($roles as $role) { Chris@14: $this->assertEqual($role->getWeight(), $new_role_weights[$role->id()]); Chris@14: $rids[] = $role->id(); Chris@14: } Chris@14: // The order of the roles should be reversed. Chris@14: $this->assertIdentical($rids, array_reverse($saved_rids)); Chris@14: } Chris@14: Chris@14: }