function UserRoleAdminTest::testRoleAdministration

Test adding, renaming and deleting roles.

File

drupal/core/modules/user/lib/Drupal/user/Tests/UserRoleAdminTest.php, line 33
Definition of Drupal\user\Tests\UserRoleAdminTest.

Class

UserRoleAdminTest
Test case to test adding, editing and deleting roles.

Namespace

Drupal\user\Tests

Code

function testRoleAdministration() {
  $this
    ->drupalLogin($this->admin_user);
  $default_langcode = language_default()->langcode;

  // Test adding a role. (In doing so, we use a role name that happens to
  // correspond to an integer, to test that the role administration pages
  // correctly distinguish between role names and IDs.)
  $role_name = '123';
  $edit = array(
    'label' => $role_name,
    'id' => $role_name,
  );
  $this
    ->drupalPost('admin/people/roles/add', $edit, t('Save'));
  $this
    ->assertRaw(t('Role %label has been added.', array(
    '%label' => 123,
  )));
  $role = entity_load('user_role', $role_name);
  $this
    ->assertTrue(is_object($role), 'The role was successfully retrieved from the database.');

  // Check that the role was created in site default language.
  $this
    ->assertEqual($role->langcode, $default_langcode);

  // Try adding a duplicate role.
  $this
    ->drupalPost('admin/people/roles/add', $edit, t('Save'));
  $this
    ->assertRaw(t('The machine-readable name is already in use. It must be unique.'), 'Duplicate role warning displayed.');

  // Test renaming a role.
  $old_name = $role_name;
  $role_name = '456';
  $edit = array(
    'label' => $role_name,
  );
  $this
    ->drupalPost("admin/people/roles/manage/{$role->id()}", $edit, t('Save'));
  $this
    ->assertRaw(t('Role %label has been updated.', array(
    '%label' => $role_name,
  )));
  $new_role = entity_load('user_role', $old_name);
  $this
    ->assertEqual($new_role
    ->label(), $role_name, 'The role name has been successfully changed.');

  // Test deleting a role.
  $this
    ->drupalPost("admin/people/roles/manage/{$role->id()}", array(), t('Delete'));
  $this
    ->drupalPost(NULL, array(), t('Delete'));
  $this
    ->assertRaw(t('Role %label has been deleted.', array(
    '%label' => $role_name,
  )));
  $this
    ->assertNoLinkByHref("admin/people/roles/manage/{$role->id()}", 'Role edit link removed.');
  $this
    ->assertFalse(entity_load('user_role', $role_name), 'A deleted role can no longer be loaded.');

  // Make sure that the system-defined roles can be edited via the user
  // interface.
  $this
    ->drupalGet('admin/people/roles/manage/' . DRUPAL_ANONYMOUS_RID);
  $this
    ->assertResponse(200, 'Access granted when trying to edit the built-in anonymous role.');
  $this
    ->assertNoText(t('Delete role'), 'Delete button for the anonymous role is not present.');
  $this
    ->drupalGet('admin/people/roles/manage/' . DRUPAL_AUTHENTICATED_RID);
  $this
    ->assertResponse(200, 'Access granted when trying to edit the built-in authenticated role.');
  $this
    ->assertNoText(t('Delete role'), 'Delete button for the authenticated role is not present.');
}