function CustomAccessCheckTest::testAccess

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

Tests the access method.

File

core/tests/Drupal/Tests/Core/Access/CustomAccessCheckTest.php, line 57

Class

CustomAccessCheckTest
@coversDefaultClass \Drupal\Core\Access\CustomAccessCheck @group Access

Namespace

Drupal\Tests\Core\Access

Code

public function testAccess() : void {
    $route_match = $this->createMock('Drupal\\Core\\Routing\\RouteMatchInterface');
    $this->callableResolver
        ->expects($this->exactly(4))
        ->method('getCallableFromDefinition')
        ->willReturnMap([
        [
            '\\Drupal\\Tests\\Core\\Access\\TestController::accessDeny',
            [
                new TestController(),
                'accessDeny',
            ],
        ],
        [
            '\\Drupal\\Tests\\Core\\Access\\TestController::accessAllow',
            [
                new TestController(),
                'accessAllow',
            ],
        ],
        [
            '\\Drupal\\Tests\\Core\\Access\\TestController::accessParameter',
            [
                new TestController(),
                'accessParameter',
            ],
        ],
        [
            '\\Drupal\\Tests\\Core\\Access\\TestController::accessRequest',
            [
                new TestController(),
                'accessRequest',
            ],
        ],
    ]);
    $resolver0 = $this->createMock('Drupal\\Component\\Utility\\ArgumentsResolverInterface');
    $resolver0->expects($this->once())
        ->method('getArguments')
        ->willReturn([]);
    $resolver1 = $this->createMock('Drupal\\Component\\Utility\\ArgumentsResolverInterface');
    $resolver1->expects($this->once())
        ->method('getArguments')
        ->willReturn([]);
    $resolver2 = $this->createMock('Drupal\\Component\\Utility\\ArgumentsResolverInterface');
    $resolver2->expects($this->once())
        ->method('getArguments')
        ->willReturn([
        'parameter' => 'TRUE',
    ]);
    $request = Request::create('/foo?example=muh');
    $resolver3 = $this->createMock('Drupal\\Component\\Utility\\ArgumentsResolverInterface');
    $resolver3->expects($this->once())
        ->method('getArguments')
        ->willReturn([
        'request' => $request,
    ]);
    $this->argumentsResolverFactory
        ->expects($this->exactly(4))
        ->method('getArgumentsResolver')
        ->willReturnOnConsecutiveCalls($resolver0, $resolver1, $resolver2, $resolver3);
    $route = new Route('/test-route', [], [
        '_custom_access' => '\\Drupal\\Tests\\Core\\Access\\TestController::accessDeny',
    ]);
    $account = $this->createMock('Drupal\\Core\\Session\\AccountInterface');
    $this->assertEquals(AccessResult::neutral(), $this->accessChecker
        ->access($route, $route_match, $account, $request));
    $route = new Route('/test-route', [], [
        '_custom_access' => '\\Drupal\\Tests\\Core\\Access\\TestController::accessAllow',
    ]);
    $this->assertEquals(AccessResult::allowed(), $this->accessChecker
        ->access($route, $route_match, $account, $request));
    $route = new Route('/test-route', [
        'parameter' => 'TRUE',
    ], [
        '_custom_access' => '\\Drupal\\Tests\\Core\\Access\\TestController::accessParameter',
    ]);
    $this->assertEquals(AccessResult::allowed(), $this->accessChecker
        ->access($route, $route_match, $account, $request));
    $route = new Route('/test-route', [
        'parameter' => 'TRUE',
    ], [
        '_custom_access' => '\\Drupal\\Tests\\Core\\Access\\TestController::accessRequest',
    ]);
    $this->assertEquals(AccessResult::allowed(), $this->accessChecker
        ->access($route, $route_match, $account, $request));
}

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