You are here

public function UserRolesAssignmentTest::testAssignAndRemoveRole in Drupal 10

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

Tests that a user can be assigned a role and that the role can be removed again.

File

core/modules/user/tests/src/Functional/UserRolesAssignmentTest.php, line 32

Class

UserRolesAssignmentTest
Tests that users can be assigned and unassigned roles.

Namespace

Drupal\Tests\user\Functional

Code

public function testAssignAndRemoveRole() {
  $rid = $this
    ->drupalCreateRole([
    'administer users',
  ]);
  $account = $this
    ->drupalCreateUser();

  // Assign the role to the user.
  $this
    ->drupalGet('user/' . $account
    ->id() . '/edit');
  $this
    ->submitForm([
    "roles[{$rid}]" => $rid,
  ], 'Save');
  $this
    ->assertSession()
    ->pageTextContains('The changes have been saved.');
  $this
    ->assertSession()
    ->checkboxChecked('edit-roles-' . $rid);
  $this
    ->userLoadAndCheckRoleAssigned($account, $rid);

  // Remove the role from the user.
  $this
    ->drupalGet('user/' . $account
    ->id() . '/edit');
  $this
    ->submitForm([
    "roles[{$rid}]" => FALSE,
  ], 'Save');
  $this
    ->assertSession()
    ->pageTextContains('The changes have been saved.');
  $this
    ->assertSession()
    ->checkboxNotChecked('edit-roles-' . $rid);
  $this
    ->userLoadAndCheckRoleAssigned($account, $rid, FALSE);
}