public function RoleAccessCheckTest::testRoleAccess

Tests role requirements on routes.

@dataProvider roleAccessProvider

Parameters

string $path: The path to check access for.

array $grant_accounts: A list of accounts which should have access to the given path.

array $deny_accounts: A list of accounts which should not have access to the given path.

See also

\Drupal\Tests\Core\Route\RouterRoleTest::getTestRouteCollection

\Drupal\Tests\Core\Route\RouterRoleTest::roleAccessProvider

File

drupal/core/tests/Drupal/Tests/Core/Route/RoleAccessCheckTest.php, line 157
Contains \Drupal\Tests\Core\Route\RoleAccessCheckTest.

Class

RoleAccessCheckTest
Defines tests for role based access in routes.

Namespace

Drupal\Tests\Core\Route

Code

public function testRoleAccess($path, $grant_accounts, $deny_accounts) {
  $role_access_check = new RoleAccessCheck();
  $collection = $this
    ->getTestRouteCollection();
  foreach ($grant_accounts as $account) {

    // @todo Replace the global user with a properly injection session.
    $GLOBALS['user'] = $account;
    $subrequest = Request::create($path, 'GET');
    $message = sprintf('Access granted for user with the roles %s on path: %s', implode(', ', $account->roles), $path);
    $this
      ->assertSame(AccessCheckInterface::ALLOW, $role_access_check
      ->access($collection
      ->get($path), $subrequest), $message);
  }

  // Check all users which don't have access.
  foreach ($deny_accounts as $account) {
    $GLOBALS['user'] = $account;
    $subrequest = Request::create($path, 'GET');
    $message = sprintf('Access denied for user %s with the roles %s on path: %s', $account->uid, implode(', ', $account->roles), $path);
    $has_access = $role_access_check
      ->access($collection
      ->get($path), $subrequest);
    $this
      ->assertSame(AccessCheckInterface::DENY, $has_access, $message);
  }
}