public function UserRolesAssignmentTest::testAssignAndRemoveRole in Drupal 9
Same name and namespace in other branches
- 8 core/modules/user/tests/src/Functional/UserRolesAssignmentTest.php \Drupal\Tests\user\Functional\UserRolesAssignmentTest::testAssignAndRemoveRole()
- 10 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\FunctionalCode
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);
}