You are here

private function RoleAssignPermissionTest::userLoadAndCheckRoleAssigned in RoleAssign 8

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 RoleAssignPermissionTest::userLoadAndCheckRoleAssigned()
RoleAssignPermissionTest::testRoleAssignAdminUser in tests/src/Functional/RoleAssignPermissionTest.php
Tests that an admin user with "administer permissions" can add all roles.
RoleAssignPermissionTest::testRoleAssignRestrictedUser in tests/src/Functional/RoleAssignPermissionTest.php
Tests that a restricted user can only (un)assign configured roles.

File

tests/src/Functional/RoleAssignPermissionTest.php, line 184

Class

RoleAssignPermissionTest
Tests that users can (un)assign roles based on the RoleAssign settings.

Namespace

Drupal\Tests\roleassign\Functional

Code

private function userLoadAndCheckRoleAssigned($account, $rid, $is_assigned = TRUE) {
  $user_storage = \Drupal::entityTypeManager()
    ->getStorage('user');
  $user_storage
    ->resetCache([
    $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.');
  }
}