You are here

public function AccessManagerTest::testCheck in Drupal 9

Same name and namespace in other branches
  1. 8 core/tests/Drupal/Tests/Core/Access/AccessManagerTest.php \Drupal\Tests\Core\Access\AccessManagerTest::testCheck()

Tests \Drupal\Core\Access\AccessManager::check().

File

core/tests/Drupal/Tests/Core/Access/AccessManagerTest.php, line 190
Contains \Drupal\Tests\Core\Access\AccessManagerTest.

Class

AccessManagerTest
@coversDefaultClass \Drupal\Core\Access\AccessManager @group Access

Namespace

Drupal\Tests\Core\Access

Code

public function testCheck() {
  $route_matches = [];

  // Construct route match objects.
  foreach ($this->routeCollection
    ->all() as $route_name => $route) {
    $route_matches[$route_name] = new RouteMatch($route_name, $route, [], []);
  }

  // Check route access without any access checker defined yet.
  foreach ($route_matches as $route_match) {
    $this
      ->assertEquals(FALSE, $this->accessManager
      ->check($route_match, $this->account));
    $this
      ->assertEquals(AccessResult::neutral(), $this->accessManager
      ->check($route_match, $this->account, NULL, TRUE));
  }
  $this
    ->setupAccessChecker();

  // An access checker got setup, but the routes haven't been setup using
  // setChecks.
  foreach ($route_matches as $route_match) {
    $this
      ->assertEquals(FALSE, $this->accessManager
      ->check($route_match, $this->account));
    $this
      ->assertEquals(AccessResult::neutral(), $this->accessManager
      ->check($route_match, $this->account, NULL, TRUE));
  }

  // Now applicable access checks have been saved on each route object.
  $this->checkProvider
    ->setChecks($this->routeCollection);
  $this
    ->setupAccessArgumentsResolverFactory();
  $this
    ->assertEquals(FALSE, $this->accessManager
    ->check($route_matches['test_route_1'], $this->account));
  $this
    ->assertEquals(TRUE, $this->accessManager
    ->check($route_matches['test_route_2'], $this->account));
  $this
    ->assertEquals(FALSE, $this->accessManager
    ->check($route_matches['test_route_3'], $this->account));
  $this
    ->assertEquals(TRUE, $this->accessManager
    ->check($route_matches['test_route_4'], $this->account));
  $this
    ->assertEquals(AccessResult::neutral(), $this->accessManager
    ->check($route_matches['test_route_1'], $this->account, NULL, TRUE));
  $this
    ->assertEquals(AccessResult::allowed(), $this->accessManager
    ->check($route_matches['test_route_2'], $this->account, NULL, TRUE));
  $this
    ->assertEquals(AccessResult::forbidden(), $this->accessManager
    ->check($route_matches['test_route_3'], $this->account, NULL, TRUE));
  $this
    ->assertEquals(AccessResult::allowed(), $this->accessManager
    ->check($route_matches['test_route_4'], $this->account, NULL, TRUE));
}