function CustomAccessCheckTest::testAccessException

Same name and namespace 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. 10 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 <a href="/api/drupal/core%21lib%21Drupal%21Core%21Access%21CustomAccessCheck.php/class/CustomAccessCheck/11.x" title="Defines an access checker that allows specifying a custom method for access." class="local">\Drupal\Core\Access\CustomAccessCheck</a> @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.