function UserRolesAssignmentTest::testAssignAndRemoveRole

Same name in other branches
  1. 9 core/modules/user/tests/src/Functional/UserRolesAssignmentTest.php \Drupal\Tests\user\Functional\UserRolesAssignmentTest::testAssignAndRemoveRole()
  2. 8.9.x core/modules/user/tests/src/Functional/UserRolesAssignmentTest.php \Drupal\Tests\user\Functional\UserRolesAssignmentTest::testAssignAndRemoveRole()
  3. 10 core/modules/user/tests/src/Functional/UserRolesAssignmentTest.php \Drupal\Tests\user\Functional\UserRolesAssignmentTest::testAssignAndRemoveRole()

Test that user can be assigned role and that the role can be removed again.

File

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

Class

UserRolesAssignmentTest
Tests that users can be assigned and unassigned roles.

Namespace

Drupal\Tests\user\Functional

Code

public function testAssignAndRemoveRole() : void {
    $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);
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.