You are here

public function CustomAccessCheckTest::testAccessException in Drupal 8

Tests the access method exception for invalid access callbacks.

File

core/tests/Drupal/Tests/Core/Access/CustomAccessCheckTest.php, line 117
Contains \Drupal\Tests\Core\Access\CustomAccessCheckTest.

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);
}