function UserRolesAssignmentTest::testAssignAndRemoveRole

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

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