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. 10 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()

Test the access method.

File

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

Class

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

Namespace

Drupal\Tests\Core\Access

Code

public function testAccess() {
    $route_match = $this->createMock('Drupal\\Core\\Routing\\RouteMatchInterface');
    $this->controllerResolver
        ->expects($this->at(0))
        ->method('getControllerFromDefinition')
        ->with('\\Drupal\\Tests\\Core\\Access\\TestController::accessDeny')
        ->will($this->returnValue([
        new TestController(),
        'accessDeny',
    ]));
    $resolver0 = $this->createMock('Drupal\\Component\\Utility\\ArgumentsResolverInterface');
    $resolver0->expects($this->once())
        ->method('getArguments')
        ->will($this->returnValue([]));
    $this->argumentsResolverFactory
        ->expects($this->at(0))
        ->method('getArgumentsResolver')
        ->will($this->returnValue($resolver0));
    $this->controllerResolver
        ->expects($this->at(1))
        ->method('getControllerFromDefinition')
        ->with('\\Drupal\\Tests\\Core\\Access\\TestController::accessAllow')
        ->will($this->returnValue([
        new TestController(),
        'accessAllow',
    ]));
    $resolver1 = $this->createMock('Drupal\\Component\\Utility\\ArgumentsResolverInterface');
    $resolver1->expects($this->once())
        ->method('getArguments')
        ->will($this->returnValue([]));
    $this->argumentsResolverFactory
        ->expects($this->at(1))
        ->method('getArgumentsResolver')
        ->will($this->returnValue($resolver1));
    $this->controllerResolver
        ->expects($this->at(2))
        ->method('getControllerFromDefinition')
        ->with('\\Drupal\\Tests\\Core\\Access\\TestController::accessParameter')
        ->will($this->returnValue([
        new TestController(),
        'accessParameter',
    ]));
    $resolver2 = $this->createMock('Drupal\\Component\\Utility\\ArgumentsResolverInterface');
    $resolver2->expects($this->once())
        ->method('getArguments')
        ->will($this->returnValue([
        'parameter' => 'TRUE',
    ]));
    $this->argumentsResolverFactory
        ->expects($this->at(2))
        ->method('getArgumentsResolver')
        ->will($this->returnValue($resolver2));
    $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));
    $route = new Route('/test-route', [], [
        '_custom_access' => '\\Drupal\\Tests\\Core\\Access\\TestController::accessAllow',
    ]);
    $this->assertEquals(AccessResult::allowed(), $this->accessChecker
        ->access($route, $route_match, $account));
    $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));
}

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