X-Git-Url: http://www.aleph1.co.uk/gitweb/?p=yaffs-website;a=blobdiff_plain;f=web%2Fcore%2Ftests%2FDrupal%2FTests%2FCore%2FAccess%2FCustomAccessCheckTest.php;fp=web%2Fcore%2Ftests%2FDrupal%2FTests%2FCore%2FAccess%2FCustomAccessCheckTest.php;h=aeb7f791bbe04828694bbed9c330bba983df1365;hp=9f0c2c9db40353650a29208d1290cb85dd93340d;hb=0bf8d09d2542548982e81a441b1f16e75873a04f;hpb=74df008bdbb3a11eeea356744f39b802369bda3c diff --git a/web/core/tests/Drupal/Tests/Core/Access/CustomAccessCheckTest.php b/web/core/tests/Drupal/Tests/Core/Access/CustomAccessCheckTest.php index 9f0c2c9db..aeb7f791b 100644 --- a/web/core/tests/Drupal/Tests/Core/Access/CustomAccessCheckTest.php +++ b/web/core/tests/Drupal/Tests/Core/Access/CustomAccessCheckTest.php @@ -9,8 +9,13 @@ namespace Drupal\Tests\Core\Access; use Drupal\Core\Access\AccessResult; use Drupal\Core\Access\CustomAccessCheck; +use Drupal\Core\Controller\ControllerResolver; +use Drupal\Core\Routing\RouteMatchInterface; +use Drupal\Core\Session\AccountInterface; use Drupal\Tests\UnitTestCase; use Symfony\Component\Routing\Route; +use Drupal\Core\DependencyInjection\ClassResolverInterface; +use Symfony\Bridge\PsrHttpMessage\HttpMessageFactoryInterface; /** * @coversDefaultClass \Drupal\Core\Access\CustomAccessCheck @@ -106,6 +111,34 @@ class CustomAccessCheckTest extends UnitTestCase { $this->assertEquals(AccessResult::allowed(), $this->accessChecker->access($route, $route_match, $account)); } + /** + * Tests the access method exception for invalid access callbacks. + */ + 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->getMock(RouteMatchInterface::class); + $account = $this->getMock(AccountInterface::class); + + $this->setExpectedException(\BadMethodCallException::class, '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); + } + } class TestController {