function UserRolesAccessPolicyTest::testCalculatePermissions

Same name in other branches
  1. 11.x core/tests/Drupal/Tests/Core/Session/UserRolesAccessPolicyTest.php \Drupal\Tests\Core\Session\UserRolesAccessPolicyTest::testCalculatePermissions()

Tests the calculatePermissions method.

@covers ::calculatePermissions @dataProvider calculatePermissionsProvider

Parameters

array $roles: The roles to grant the account.

bool $expect_admin_rights: Whether to expect admin rights to be granted.

File

core/tests/Drupal/Tests/Core/Session/UserRolesAccessPolicyTest.php, line 78

Class

UserRolesAccessPolicyTest
@coversDefaultClass \Drupal\Core\Session\UserRolesAccessPolicy @group Session

Namespace

Drupal\Tests\Core\Session

Code

public function testCalculatePermissions(array $roles, bool $expect_admin_rights) : void {
    $account = $this->prophesize(AccountInterface::class);
    $account->getRoles()
        ->willReturn(array_keys($roles));
    $total_permissions = $cache_tags = $mocked_roles = [];
    foreach ($roles as $role_id => $role) {
        $total_permissions = array_merge($total_permissions, $role['permissions']);
        $cache_tags[] = "config:user.role.{$role_id}";
        $mocked_role = $this->prophesize(RoleInterface::class);
        $mocked_role->getPermissions()
            ->willReturn($role['permissions']);
        $mocked_role->isAdmin()
            ->willReturn($role['is_admin']);
        $mocked_role->getCacheTags()
            ->willReturn([
            "config:user.role.{$role_id}",
        ]);
        $mocked_role->getCacheContexts()
            ->willReturn([]);
        $mocked_role->getCacheMaxAge()
            ->willReturn(Cache::PERMANENT);
        $mocked_roles[$role_id] = $mocked_role->reveal();
    }
    $role_storage = $this->prophesize(RoleStorageInterface::class);
    $role_storage->loadMultiple(array_keys($roles))
        ->willReturn($mocked_roles);
    $this->entityTypeManager
        ->getStorage('user_role')
        ->willReturn($role_storage->reveal());
    $calculated_permissions = $this->accessPolicy
        ->calculatePermissions($account->reveal(), AccessPolicyInterface::SCOPE_DRUPAL);
    if (!empty($roles)) {
        $this->assertCount(1, $calculated_permissions->getItems(), 'Only one calculated permissions item was added.');
        $item = $calculated_permissions->getItem();
        if ($expect_admin_rights) {
            $this->assertSame([], $item->getPermissions());
            $this->assertTrue($item->isAdmin());
        }
        else {
            $this->assertSame($total_permissions, $item->getPermissions());
            $this->assertFalse($item->isAdmin());
        }
    }
    $this->assertSame($cache_tags, $calculated_permissions->getCacheTags());
    $this->assertSame([
        'user.roles',
    ], $calculated_permissions->getCacheContexts());
    $this->assertSame(Cache::PERMANENT, $calculated_permissions->getCacheMaxAge());
}

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