class AccessAwareRouterTest
Same name in other branches
- 9 core/tests/Drupal/Tests/Core/Routing/AccessAwareRouterTest.php \Drupal\Tests\Core\Routing\AccessAwareRouterTest
- 8.9.x core/tests/Drupal/Tests/Core/Routing/AccessAwareRouterTest.php \Drupal\Tests\Core\Routing\AccessAwareRouterTest
- 11.x core/tests/Drupal/Tests/Core/Routing/AccessAwareRouterTest.php \Drupal\Tests\Core\Routing\AccessAwareRouterTest
@coversDefaultClass \Drupal\Core\Routing\AccessAwareRouter @group Routing
Hierarchy
- class \Drupal\Tests\UnitTestCase extends \PHPUnit\Framework\TestCase uses \Drupal\Tests\Traits\PhpUnitWarnings, \Drupal\Tests\PhpUnitCompatibilityTrait, \Prophecy\PhpUnit\ProphecyTrait, \Symfony\Bridge\PhpUnit\ExpectDeprecationTrait, \Drupal\Tests\RandomGeneratorTrait
- class \Drupal\Tests\Core\Routing\AccessAwareRouterTest extends \Drupal\Tests\UnitTestCase
Expanded class hierarchy of AccessAwareRouterTest
File
-
core/
tests/ Drupal/ Tests/ Core/ Routing/ AccessAwareRouterTest.php, line 22
Namespace
Drupal\Tests\Core\RoutingView source
class AccessAwareRouterTest extends UnitTestCase {
/**
* @var \Symfony\Component\Routing\RouterInterface|\PHPUnit\Framework\MockObject\MockObject
*/
protected RouterInterface|MockObject $router;
/**
* @var \Symfony\Component\Routing\Route
*/
protected $route;
/**
* @var \Drupal\Core\Routing\Router|\PHPUnit\Framework\MockObject\MockObject
*/
protected $coreRouter;
/**
* @var \Drupal\Core\Access\AccessManagerInterface|\PHPUnit\Framework\MockObject\MockObject
*/
protected $accessManager;
/**
* @var \Drupal\Core\Session\AccountInterface||\PHPUnit\Framework\MockObject\MockObject
*/
protected $currentUser;
/**
* @var \Drupal\Core\Routing\AccessAwareRouter
*/
protected $accessAwareRouter;
/**
* {@inheritdoc}
*/
protected function setUp() : void {
parent::setUp();
$this->route = new Route('test');
$this->accessManager = $this->createMock('Drupal\\Core\\Access\\AccessManagerInterface');
$this->currentUser = $this->createMock('Drupal\\Core\\Session\\AccountInterface');
}
/**
* Sets up a chain router with matchRequest.
*/
protected function setupRouter() {
$this->router = $this->getMockBuilder('Drupal\\Core\\Routing\\Router')
->disableOriginalConstructor()
->getMock();
$this->router
->expects($this->once())
->method('matchRequest')
->willReturn([
RouteObjectInterface::ROUTE_OBJECT => $this->route,
]);
$this->accessAwareRouter = new AccessAwareRouter($this->router, $this->accessManager, $this->currentUser);
}
/**
* Tests the matchRequest() function for access allowed.
*/
public function testMatchRequestAllowed() : void {
$this->setupRouter();
$request = new Request();
$access_result = AccessResult::allowed();
$this->accessManager
->expects($this->once())
->method('checkRequest')
->with($request)
->willReturn($access_result);
$parameters = $this->accessAwareRouter
->matchRequest($request);
$expected = [
RouteObjectInterface::ROUTE_OBJECT => $this->route,
AccessAwareRouterInterface::ACCESS_RESULT => $access_result,
];
$this->assertSame($expected, $request->attributes
->all());
$this->assertSame($expected, $parameters);
}
/**
* Tests the matchRequest() function for access denied.
*/
public function testMatchRequestDenied() : void {
$this->setupRouter();
$request = new Request();
$access_result = AccessResult::forbidden();
$this->accessManager
->expects($this->once())
->method('checkRequest')
->with($request)
->willReturn($access_result);
$this->expectException(AccessDeniedHttpException::class);
$this->accessAwareRouter
->matchRequest($request);
}
/**
* Tests the matchRequest() function for access denied with reason message.
*/
public function testCheckAccessResultWithReason() : void {
$this->setupRouter();
$request = new Request();
$reason = $this->getRandomGenerator()
->string();
$access_result = AccessResult::forbidden($reason);
$this->accessManager
->expects($this->once())
->method('checkRequest')
->with($request)
->willReturn($access_result);
$this->expectException(AccessDeniedHttpException::class);
$this->expectExceptionMessage($reason);
$this->accessAwareRouter
->matchRequest($request);
}
/**
* Ensure that methods are passed to the wrapped router.
*
* @covers ::__call
*/
public function testCall() : void {
$mock_router = $this->createMock(RouterInterface::class);
$this->router = $this->getMockBuilder(MockRouterInterface::class)
->disableOriginalConstructor()
->onlyMethods([
'getRouteCollection',
'match',
'getContext',
'setContext',
'generate',
'add',
])
->getMock();
$this->router
->expects($this->once())
->method('add')
->with($mock_router)
->willReturnSelf();
$this->accessAwareRouter = new AccessAwareRouter($this->router, $this->accessManager, $this->currentUser);
$this->accessAwareRouter
->add($mock_router);
}
}
Members
Title Sort descending | Deprecated | Modifiers | Object type | Summary | Overriden Title | Overrides |
---|---|---|---|---|---|---|
AccessAwareRouterTest::$accessAwareRouter | protected | property | ||||
AccessAwareRouterTest::$accessManager | protected | property | ||||
AccessAwareRouterTest::$coreRouter | protected | property | ||||
AccessAwareRouterTest::$currentUser | protected | property | ||||
AccessAwareRouterTest::$route | protected | property | ||||
AccessAwareRouterTest::$router | protected | property | ||||
AccessAwareRouterTest::setUp | protected | function | Overrides UnitTestCase::setUp | |||
AccessAwareRouterTest::setupRouter | protected | function | Sets up a chain router with matchRequest. | |||
AccessAwareRouterTest::testCall | public | function | Ensure that methods are passed to the wrapped router. | |||
AccessAwareRouterTest::testCheckAccessResultWithReason | public | function | Tests the matchRequest() function for access denied with reason message. | |||
AccessAwareRouterTest::testMatchRequestAllowed | public | function | Tests the matchRequest() function for access allowed. | |||
AccessAwareRouterTest::testMatchRequestDenied | public | function | Tests the matchRequest() function for access denied. | |||
PhpUnitWarnings::$deprecationWarnings | private static | property | Deprecation warnings from PHPUnit to raise with @trigger_error(). | |||
PhpUnitWarnings::addWarning | public | function | Converts PHPUnit deprecation warnings to E_USER_DEPRECATED. | |||
RandomGeneratorTrait::getRandomGenerator | protected | function | Gets the random generator for the utility methods. | |||
RandomGeneratorTrait::randomMachineName | protected | function | Generates a unique random string containing letters and numbers. | |||
RandomGeneratorTrait::randomObject | public | function | Generates a random PHP object. | |||
RandomGeneratorTrait::randomString | public | function | Generates a pseudo-random string of ASCII characters of codes 32 to 126. | |||
RandomGeneratorTrait::randomStringValidate | Deprecated | public | function | Callback for random string validation. | ||
UnitTestCase::$root | protected | property | The app root. | 1 | ||
UnitTestCase::getClassResolverStub | protected | function | Returns a stub class resolver. | |||
UnitTestCase::getConfigFactoryStub | public | function | Returns a stub config factory that behaves according to the passed array. | |||
UnitTestCase::getConfigStorageStub | public | function | Returns a stub config storage that returns the supplied configuration. | |||
UnitTestCase::getContainerWithCacheTagsInvalidator | protected | function | Sets up a container with a cache tags invalidator. | |||
UnitTestCase::getStringTranslationStub | public | function | Returns a stub translation manager that just returns the passed string. | |||
UnitTestCase::setUpBeforeClass | public static | function | ||||
UnitTestCase::__get | public | function |
Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.