function AccessManagerTest::testCheck

Same name in other branches
  1. 9 core/tests/Drupal/Tests/Core/Access/AccessManagerTest.php \Drupal\Tests\Core\Access\AccessManagerTest::testCheck()
  2. 8.9.x core/tests/Drupal/Tests/Core/Access/AccessManagerTest.php \Drupal\Tests\Core\Access\AccessManagerTest::testCheck()
  3. 10 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 186

Class

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

Namespace

Drupal\Tests\Core\Access

Code

public function testCheck() : void {
    $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));
}

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