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. 8.9.x 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
Test that user can be assigned role and that the role can be removed again.
UserRolesAssignmentTest::testCreateUserWithRole in core/modules/user/tests/src/Functional/UserRolesAssignmentTest.php
Tests assigning a role at user creation and removing the role.

File

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

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.