function AccessManagerTest::testSetChecksWithDynamicAccessChecker

Same name in other branches
  1. 9 core/tests/Drupal/Tests/Core/Access/AccessManagerTest.php \Drupal\Tests\Core\Access\AccessManagerTest::testSetChecksWithDynamicAccessChecker()
  2. 8.9.x core/tests/Drupal/Tests/Core/Access/AccessManagerTest.php \Drupal\Tests\Core\Access\AccessManagerTest::testSetChecksWithDynamicAccessChecker()
  3. 11.x core/tests/Drupal/Tests/Core/Access/AccessManagerTest.php \Drupal\Tests\Core\Access\AccessManagerTest::testSetChecksWithDynamicAccessChecker()

Tests setChecks with a dynamic access checker.

File

core/tests/Drupal/Tests/Core/Access/AccessManagerTest.php, line 155

Class

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

Namespace

Drupal\Tests\Core\Access

Code

public function testSetChecksWithDynamicAccessChecker() : void {
    // Setup the dynamic access checker.
    $access_check = $this->createMock('Drupal\\Tests\\Core\\Access\\TestAccessCheckInterface');
    $this->container
        ->set('test_access', $access_check);
    $this->checkProvider = new CheckProvider([
        'test_access',
    ], $this->container);
    $this->checkProvider
        ->addCheckService('test_access', 'access');
    // Setup the access manager.
    $this->accessManager = new AccessManager($this->routeProvider, $this->paramConverter, $this->argumentsResolverFactory, $this->currentUser, $this->checkProvider);
    $route = new Route('/test-path', [], [
        '_foo' => '1',
        '_bar' => '1',
    ]);
    $route2 = new Route('/test-path', [], [
        '_foo' => '1',
        '_bar' => '2',
    ]);
    $collection = new RouteCollection();
    $collection->add('test_route', $route);
    $collection->add('test_route2', $route2);
    $access_check->expects($this->exactly(2))
        ->method('applies')
        ->with($this->isInstanceOf('Symfony\\Component\\Routing\\Route'))
        ->willReturnCallback(function (Route $route) {
        return $route->getRequirement('_bar') == 2;
    });
    $this->checkProvider
        ->setChecks($collection);
    $this->assertEmpty($route->getOption('_access_checks'));
    $this->assertEquals([
        'test_access',
    ], $route2->getOption('_access_checks'));
}

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