You are here

public function UserRolesAssignmentTest::testAssignAndRemoveRole in Drupal 8

Same name and namespace in other branches
  1. 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
    ->drupalPostForm('user/' . $account
    ->id() . '/edit', [
    "roles[{$rid}]" => $rid,
  ], t('Save'));
  $this
    ->assertText(t('The changes have been saved.'));
  $this
    ->assertFieldChecked('edit-roles-' . $rid, 'Role is assigned.');
  $this
    ->userLoadAndCheckRoleAssigned($account, $rid);

  // Remove the role from the user.
  $this
    ->drupalPostForm('user/' . $account
    ->id() . '/edit', [
    "roles[{$rid}]" => FALSE,
  ], t('Save'));
  $this
    ->assertText(t('The changes have been saved.'));
  $this
    ->assertNoFieldChecked('edit-roles-' . $rid, 'Role is removed from user.');
  $this
    ->userLoadAndCheckRoleAssigned($account, $rid, FALSE);
}