You are here

public function AccessManagerTest::testSetChecksWithDynamicAccessChecker in Drupal 10

Same name and namespace in other branches
  1. 8 core/tests/Drupal/Tests/Core/Access/AccessManagerTest.php \Drupal\Tests\Core\Access\AccessManagerTest::testSetChecksWithDynamicAccessChecker()
  2. 9 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 159
Contains \Drupal\Tests\Core\Access\AccessManagerTest.

Class

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

Namespace

Drupal\Tests\Core\Access

Code

public function testSetChecksWithDynamicAccessChecker() {

  // Setup the access manager.
  $this->accessManager = new AccessManager($this->routeProvider, $this->paramConverter, $this->argumentsResolverFactory, $this->currentUser, $this->checkProvider);

  // Setup the dynamic access checker.
  $access_check = $this
    ->createMock('Drupal\\Tests\\Core\\Access\\TestAccessCheckInterface');
  $this->container
    ->set('test_access', $access_check);
  $this->container
    ->setParameter('dynamic_access_check_services', [
    'test_access',
  ]);
  $this->checkProvider
    ->addCheckService('test_access', 'access');
  $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'));
}