You are here

public function DelegatableRolesTest::testAssignableRoles in Role Delegation 8

Test the roles that can be assigned by a given user.

@covers ::getAssignableRoles

File

tests/src/Kernel/DelegatableRolesTest.php, line 51

Class

DelegatableRolesTest
@coversDefaultClass \Drupal\role_delegation\DelegatableRoles

Namespace

Drupal\Tests\role_delegation\Kernel

Code

public function testAssignableRoles() {
  $rid1 = $this
    ->createRole([]);
  $rid2 = $this
    ->createRole([]);
  $rid3 = $this
    ->createRole([]);

  // Test the 'assign all roles permission'. We have to merge in the roles of
  // the account as well because createUser() creates a new role.
  $account = $this
    ->createUser([
    'assign all roles',
  ]);
  $this
    ->assertEquals(array_merge([
    $rid1,
    $rid2,
    $rid3,
  ], $account
    ->getRoles(TRUE)), array_keys($this->delegatableRoles
    ->getAssignableRoles($account)));

  // If they have these two roles, they can assign exactly those two roles.
  $account = $this
    ->createUser([
    "assign {$rid1} role",
    "assign {$rid2} role",
  ]);
  $this
    ->assertEquals([
    $rid1,
    $rid2,
  ], array_keys($this->delegatableRoles
    ->getAssignableRoles($account)));

  // Doesn't matter what permissions they have here, they can never assign
  // anonymous or authenticated roles.
  $account = $this
    ->createUser([
    'administer users',
    'administer permissions',
  ]);
  $this
    ->assertEquals([], $this->delegatableRoles
    ->getAssignableRoles($account));
}