function CustomAccessCheckTest::testAccessException

Same name in other branches
  1. 9 core/tests/Drupal/Tests/Core/Access/CustomAccessCheckTest.php \Drupal\Tests\Core\Access\CustomAccessCheckTest::testAccessException()
  2. 8.9.x 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 114

Class

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

Namespace

Drupal\Tests\Core\Access

Code

public function testAccessException() : void {
    // Create callableResolver mock to return InvalidArgumentException.
    $this->callableResolver = $this->getMockBuilder(CallableResolver::class)
        ->disableOriginalConstructor()
        ->getMock();
    $this->callableResolver
        ->expects($this->any())
        ->method('getCallableFromDefinition')
        ->willThrowException(new \InvalidArgumentException());
    // Overwrite the access checker using the newly mocked callable resolve.
    $this->accessChecker = new CustomAccessCheck($this->callableResolver, $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);
    $request = Request::create('/foo?example=muh');
    $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, $request);
}

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