You are here

public function UserRoleAdminTest::testRoleAdministration in Drupal 9

Same name and namespace in other branches
  1. 8 core/modules/user/tests/src/Functional/UserRoleAdminTest.php \Drupal\Tests\user\Functional\UserRoleAdminTest::testRoleAdministration()

Tests adding, renaming and deleting roles.

File

core/modules/user/tests/src/Functional/UserRoleAdminTest.php, line 50

Class

UserRoleAdminTest
Tests adding, editing and deleting user roles and changing role weights.

Namespace

Drupal\Tests\user\Functional

Code

public function testRoleAdministration() {
  $this
    ->drupalLogin($this->adminUser);
  $default_langcode = \Drupal::languageManager()
    ->getDefaultLanguage()
    ->getId();

  // Test presence of tab.
  $this
    ->drupalGet('admin/people/permissions');
  $this
    ->assertSession()
    ->elementsCount('xpath', '//ul[@class="tabs primary" and //a[contains(., "Roles")]]', 1);

  // 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 = [
    'label' => $role_name,
    'id' => $role_name,
  ];
  $this
    ->drupalGet('admin/people/roles/add');
  $this
    ->submitForm($edit, 'Save');
  $this
    ->assertSession()
    ->pageTextContains("Role 123 has been added.");
  $role = Role::load($role_name);
  $this
    ->assertIsObject($role);

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

  // Try adding a duplicate role.
  $this
    ->drupalGet('admin/people/roles/add');
  $this
    ->submitForm($edit, 'Save');
  $this
    ->assertSession()
    ->pageTextContains("The machine-readable name is already in use. It must be unique.");

  // Test renaming a role.
  $role_name = '456';
  $edit = [
    'label' => $role_name,
  ];
  $this
    ->drupalGet("admin/people/roles/manage/{$role->id()}");
  $this
    ->submitForm($edit, 'Save');
  $this
    ->assertSession()
    ->pageTextContains("Role {$role_name} has been updated.");
  \Drupal::entityTypeManager()
    ->getStorage('user_role')
    ->resetCache([
    $role
      ->id(),
  ]);
  $new_role = Role::load($role
    ->id());
  $this
    ->assertEquals($role_name, $new_role
    ->label(), 'The role name has been successfully changed.');

  // Test deleting a role.
  $this
    ->drupalGet("admin/people/roles/manage/{$role->id()}");
  $this
    ->clickLink('Delete');
  $this
    ->submitForm([], 'Delete');
  $this
    ->assertSession()
    ->pageTextContains("Role {$role_name} has been deleted.");
  $this
    ->assertSession()
    ->linkByHrefNotExists("admin/people/roles/manage/{$role->id()}", 'Role edit link removed.');
  \Drupal::entityTypeManager()
    ->getStorage('user_role')
    ->resetCache([
    $role
      ->id(),
  ]);
  $this
    ->assertNull(Role::load($role
    ->id()), '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/' . RoleInterface::ANONYMOUS_ID);
  $this
    ->assertSession()
    ->statusCodeEquals(200);
  $this
    ->assertSession()
    ->pageTextNotContains('Delete role');
  $this
    ->drupalGet('admin/people/roles/manage/' . RoleInterface::AUTHENTICATED_ID);
  $this
    ->assertSession()
    ->statusCodeEquals(200);
  $this
    ->assertSession()
    ->pageTextNotContains('Delete role');
}