function AccessManagerTest::testCheckNamedRouteWithUpcastedValues

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

Tests the checkNamedRoute with upcasted values.

See also

\Drupal\Core\Access\AccessManager::checkNamedRoute()

File

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

Class

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

Namespace

Drupal\Tests\Core\Access

Code

public function testCheckNamedRouteWithUpcastedValues() : void {
    $this->routeCollection = new RouteCollection();
    $route = new Route('/test-route-1/{value}', [], [
        '_test_access' => 'TRUE',
    ]);
    $this->routeCollection
        ->add('test_route_1', $route);
    $this->routeProvider = $this->createMock('Drupal\\Core\\Routing\\RouteProviderInterface');
    $this->routeProvider
        ->expects($this->any())
        ->method('getRouteByName')
        ->with('test_route_1')
        ->willReturn($route);
    $map[] = [
        'test_route_1',
        [
            'value' => 'example',
        ],
        '/test-route-1/example',
    ];
    $this->paramConverter = $this->createMock('Drupal\\Core\\ParamConverter\\ParamConverterManagerInterface');
    $this->paramConverter
        ->expects($this->atLeastOnce())
        ->method('convert')
        ->with([
        'value' => 'example',
        RouteObjectInterface::ROUTE_NAME => 'test_route_1',
        RouteObjectInterface::ROUTE_OBJECT => $route,
    ])
        ->willReturn([
        'value' => 'upcasted_value',
    ]);
    $this->setupAccessArgumentsResolverFactory($this->exactly(2))
        ->with($this->callback(function ($route_match) {
        return $route_match->getParameters()
            ->get('value') == 'upcasted_value';
    }));
    $access_check = $this->createMock('Drupal\\Tests\\Core\\Access\\TestAccessCheckInterface');
    $access_check->expects($this->atLeastOnce())
        ->method('applies')
        ->willReturn(TRUE);
    $access_check->expects($this->atLeastOnce())
        ->method('access')
        ->willReturn(AccessResult::forbidden());
    $this->container
        ->set('test_access', $access_check);
    $this->checkProvider = new CheckProvider([
        'test_access',
    ], $this->container);
    $this->checkProvider
        ->addCheckService('test_access', 'access');
    $this->checkProvider
        ->setChecks($this->routeCollection);
    $this->accessManager = new AccessManager($this->routeProvider, $this->paramConverter, $this->argumentsResolverFactory, $this->currentUser, $this->checkProvider);
    $this->assertEquals(FALSE, $this->accessManager
        ->checkNamedRoute('test_route_1', [
        'value' => 'example',
    ], $this->account));
    $this->assertEquals(AccessResult::forbidden(), $this->accessManager
        ->checkNamedRoute('test_route_1', [
        'value' => 'example',
    ], $this->account, TRUE));
}

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