3 namespace Drupal\Tests\Core\Routing;
5 use Drupal\Core\Access\AccessResult;
6 use Drupal\Core\Routing\AccessAwareRouter;
7 use Drupal\Core\Routing\AccessAwareRouterInterface;
8 use Drupal\Tests\UnitTestCase;
9 use Symfony\Cmf\Component\Routing\RouteObjectInterface;
10 use Symfony\Component\HttpFoundation\Request;
11 use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;
12 use Symfony\Component\Routing\Route;
15 * @coversDefaultClass \Drupal\Core\Routing\AccessAwareRouter
18 class AccessAwareRouterTest extends UnitTestCase {
21 * @var \Symfony\Component\Routing\Route
26 * @var \Symfony\Cmf\Component\Routing\ChainRouter|\PHPUnit_Framework_MockObject_MockObject
28 protected $chainRouter;
31 * @var \Drupal\Core\Access\AccessManagerInterface|\PHPUnit_Framework_MockObject_MockObject
33 protected $accessManager;
36 * @var \Drupal\Core\Session\AccountInterface||\PHPUnit_Framework_MockObject_MockObject
38 protected $currentUser;
41 * @var \Drupal\Core\Routing\AccessAwareRouter
48 protected function setUp() {
50 $this->route = new Route('test');
51 $this->accessManager = $this->getMock('Drupal\Core\Access\AccessManagerInterface');
52 $this->currentUser = $this->getMock('Drupal\Core\Session\AccountInterface');
56 * Sets up a chain router with matchRequest.
58 protected function setupRouter() {
59 $this->chainRouter = $this->getMockBuilder('Symfony\Cmf\Component\Routing\ChainRouter')
60 ->disableOriginalConstructor()
62 $this->chainRouter->expects($this->once())
63 ->method('matchRequest')
64 ->will($this->returnValue([RouteObjectInterface::ROUTE_OBJECT => $this->route]));
65 $this->router = new AccessAwareRouter($this->chainRouter, $this->accessManager, $this->currentUser);
69 * Tests the matchRequest() function for access allowed.
71 public function testMatchRequestAllowed() {
73 $request = new Request();
74 $access_result = AccessResult::allowed();
75 $this->accessManager->expects($this->once())
76 ->method('checkRequest')
78 ->willReturn($access_result);
79 $parameters = $this->router->matchRequest($request);
81 RouteObjectInterface::ROUTE_OBJECT => $this->route,
82 AccessAwareRouterInterface::ACCESS_RESULT => $access_result,
84 $this->assertSame($expected, $request->attributes->all());
85 $this->assertSame($expected, $parameters);
89 * Tests the matchRequest() function for access denied.
91 public function testMatchRequestDenied() {
93 $request = new Request();
94 $access_result = AccessResult::forbidden();
95 $this->accessManager->expects($this->once())
96 ->method('checkRequest')
98 ->willReturn($access_result);
99 $this->setExpectedException(AccessDeniedHttpException::class);
100 $this->router->matchRequest($request);
104 * Tests the matchRequest() function for access denied with reason message.
106 public function testCheckAccessResultWithReason() {
107 $this->setupRouter();
108 $request = new Request();
109 $reason = $this->getRandomGenerator()->string();
110 $access_result = AccessResult::forbidden($reason);
111 $this->accessManager->expects($this->once())
112 ->method('checkRequest')
114 ->willReturn($access_result);
115 $this->setExpectedException(AccessDeniedHttpException::class, $reason);
116 $this->router->matchRequest($request);
120 * Ensure that methods are passed to the wrapped router.
124 public function testCall() {
125 $mock_router = $this->getMock('Symfony\Component\Routing\RouterInterface');
127 $this->chainRouter = $this->getMockBuilder('Symfony\Cmf\Component\Routing\ChainRouter')
128 ->disableOriginalConstructor()
129 ->setMethods(['add'])
131 $this->chainRouter->expects($this->once())
135 $this->router = new AccessAwareRouter($this->chainRouter, $this->accessManager, $this->currentUser);
137 $this->router->add($mock_router);