You are here

private function UserRolesAssignmentTest::userLoadAndCheckRoleAssigned in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 core/modules/user/src/Tests/UserRolesAssignmentTest.php \Drupal\user\Tests\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/src/Tests/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/src/Tests/UserRolesAssignmentTest.php
Tests that when creating a user the role can be assigned. And that it can be removed again.

File

core/modules/user/src/Tests/UserRolesAssignmentTest.php, line 87
Contains \Drupal\user\Tests\UserRolesAssignmentTest.

Class

UserRolesAssignmentTest
Tests that users can be assigned and unassigned roles.

Namespace

Drupal\user\Tests

Code

private function userLoadAndCheckRoleAssigned($account, $rid, $is_assigned = TRUE) {
  $user_storage = $this->container
    ->get('entity.manager')
    ->getStorage('user');
  $user_storage
    ->resetCache(array(
    $account
      ->id(),
  ));
  $account = $user_storage
    ->load($account
    ->id());
  if ($is_assigned) {
    $this
      ->assertFalse(array_search($rid, $account
      ->getRoles()) === FALSE, 'The role is present in the user object.');
  }
  else {
    $this
      ->assertTrue(array_search($rid, $account
      ->getRoles()) === FALSE, 'The role is not present in the user object.');
  }
}