function CustomAccessCheckTest::testAccessException

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

Tests the access method exception for invalid access callbacks.

File

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

Class

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

Namespace

Drupal\Tests\Core\Access

Code

public function testAccessException() {
    // Create two mocks for the ControllerResolver constructor.
    $httpMessageFactory = $this->getMockBuilder(HttpMessageFactoryInterface::class)
        ->getMock();
    $controllerResolver = $this->getMockBuilder(ClassResolverInterface::class)
        ->getMock();
    // Re-create the controllerResolver mock with proxy to original methods.
    $this->controllerResolver = $this->getMockBuilder(ControllerResolver::class)
        ->setConstructorArgs([
        $httpMessageFactory,
        $controllerResolver,
    ])
        ->enableProxyingToOriginalMethods()
        ->getMock();
    // Overwrite the access checker using the newly mocked controller resolve.
    $this->accessChecker = new CustomAccessCheck($this->controllerResolver, $this->argumentsResolverFactory);
    // Add a route with a _custom_access route that doesn't exist.
    $route = new Route('/test-route', [], [
        '_custom_access' => '\\Drupal\\Tests\\Core\\Access\\NonExistentController::nonExistentMethod',
    ]);
    $route_match = $this->createMock(RouteMatchInterface::class);
    $account = $this->createMock(AccountInterface::class);
    $this->expectException(\BadMethodCallException::class);
    $this->expectExceptionMessage('The "\\Drupal\\Tests\\Core\\Access\\NonExistentController::nonExistentMethod" method is not callable as a _custom_access callback in route "/test-route"');
    // Run the access check.
    $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.