Upgraded drupal core with security updates
[yaffs-website] / web / core / tests / Drupal / Tests / Core / Routing / AccessAwareRouterTest.php
1 <?php
2
3 namespace Drupal\Tests\Core\Routing;
4
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;
13
14 /**
15  * @coversDefaultClass \Drupal\Core\Routing\AccessAwareRouter
16  * @group Routing
17  */
18 class AccessAwareRouterTest extends UnitTestCase {
19
20   /**
21    * @var \Symfony\Component\Routing\Route
22    */
23   protected $route;
24
25   /**
26    * @var \Symfony\Cmf\Component\Routing\ChainRouter|\PHPUnit_Framework_MockObject_MockObject
27    */
28   protected $chainRouter;
29
30   /**
31    * @var \Drupal\Core\Access\AccessManagerInterface|\PHPUnit_Framework_MockObject_MockObject
32    */
33   protected $accessManager;
34
35   /**
36    * @var \Drupal\Core\Session\AccountInterface||\PHPUnit_Framework_MockObject_MockObject
37    */
38   protected $currentUser;
39
40   /**
41    * @var \Drupal\Core\Routing\AccessAwareRouter
42    */
43   protected $router;
44
45   /**
46    * {@inheritdoc}
47    */
48   protected function setUp() {
49     parent::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');
53   }
54
55   /**
56    * Sets up a chain router with matchRequest.
57    */
58   protected function setupRouter() {
59     $this->chainRouter = $this->getMockBuilder('Symfony\Cmf\Component\Routing\ChainRouter')
60       ->disableOriginalConstructor()
61       ->getMock();
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);
66   }
67
68   /**
69    * Tests the matchRequest() function for access allowed.
70    */
71   public function testMatchRequestAllowed() {
72     $this->setupRouter();
73     $request = new Request();
74     $access_result = AccessResult::allowed();
75     $this->accessManager->expects($this->once())
76       ->method('checkRequest')
77       ->with($request)
78       ->willReturn($access_result);
79     $parameters = $this->router->matchRequest($request);
80     $expected = [
81       RouteObjectInterface::ROUTE_OBJECT => $this->route,
82       AccessAwareRouterInterface::ACCESS_RESULT => $access_result,
83     ];
84     $this->assertSame($expected, $request->attributes->all());
85     $this->assertSame($expected, $parameters);
86   }
87
88   /**
89    * Tests the matchRequest() function for access denied.
90    */
91   public function testMatchRequestDenied() {
92     $this->setupRouter();
93     $request = new Request();
94     $access_result = AccessResult::forbidden();
95     $this->accessManager->expects($this->once())
96       ->method('checkRequest')
97       ->with($request)
98       ->willReturn($access_result);
99     $this->setExpectedException(AccessDeniedHttpException::class);
100     $this->router->matchRequest($request);
101   }
102
103   /**
104    * Tests the matchRequest() function for access denied with reason message.
105    */
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')
113       ->with($request)
114       ->willReturn($access_result);
115     $this->setExpectedException(AccessDeniedHttpException::class, $reason);
116     $this->router->matchRequest($request);
117   }
118
119   /**
120    * Ensure that methods are passed to the wrapped router.
121    *
122    * @covers ::__call
123    */
124   public function testCall() {
125     $mock_router = $this->getMock('Symfony\Component\Routing\RouterInterface');
126
127     $this->chainRouter = $this->getMockBuilder('Symfony\Cmf\Component\Routing\ChainRouter')
128       ->disableOriginalConstructor()
129       ->setMethods(['add'])
130       ->getMock();
131     $this->chainRouter->expects($this->once())
132       ->method('add')
133       ->with($mock_router)
134       ->willReturnSelf();
135     $this->router = new AccessAwareRouter($this->chainRouter, $this->accessManager, $this->currentUser);
136
137     $this->router->add($mock_router);
138   }
139
140 }