function UserRolesAssignmentTest::userLoadAndCheckRoleAssigned

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

Check role on user object.

Parameters

object $account: The user account to check.

string $rid: The role ID to search for.

bool $is_assigned: (optional) Whether to assert that $rid exists (TRUE) or not (FALSE). Defaults to TRUE.

2 calls to UserRolesAssignmentTest::userLoadAndCheckRoleAssigned()
UserRolesAssignmentTest::testAssignAndRemoveRole in core/modules/user/tests/src/Functional/UserRolesAssignmentTest.php
Tests that a user can be assigned a role and that the role can be removed again.
UserRolesAssignmentTest::testCreateUserWithRole in core/modules/user/tests/src/Functional/UserRolesAssignmentTest.php
Tests that when creating a user the role can be assigned. And that it can be removed again.

File

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

Class

UserRolesAssignmentTest
Tests that users can be assigned and unassigned roles.

Namespace

Drupal\Tests\user\Functional

Code

private function userLoadAndCheckRoleAssigned($account, $rid, $is_assigned = TRUE) {
    $user_storage = $this->container
        ->get('entity_type.manager')
        ->getStorage('user');
    $user_storage->resetCache([
        $account->id(),
    ]);
    $account = $user_storage->load($account->id());
    if ($is_assigned) {
        $this->assertContains($rid, $account->getRoles());
    }
    else {
        $this->assertNotContains($rid, $account->getRoles());
    }
}

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