Security update for Core, with self-updated composer
[yaffs-website] / web / core / tests / Drupal / Tests / Core / Routing / RoutePreloaderTest.php
1 <?php
2
3 namespace Drupal\Tests\Core\Routing;
4
5 use Drupal\Core\Routing\RoutePreloader;
6 use Drupal\Tests\UnitTestCase;
7 use Symfony\Component\EventDispatcher\Event;
8 use Symfony\Component\HttpFoundation\Request;
9 use Symfony\Component\Routing\Route;
10 use Symfony\Component\Routing\RouteCollection;
11
12 /**
13  * @coversDefaultClass \Drupal\Core\Routing\RoutePreloader
14  * @group Routing
15  */
16 class RoutePreloaderTest extends UnitTestCase {
17
18   /**
19    * The mocked route provider.
20    *
21    * @var \Drupal\Core\Routing\RouteProviderInterface|\PHPUnit_Framework_MockObject_MockObject
22    */
23   protected $routeProvider;
24
25   /**
26    * The mocked state.
27    *
28    * @var \Drupal\Core\State\StateInterface|\PHPUnit_Framework_MockObject_MockObject
29    */
30   protected $state;
31
32   /**
33    * The tested preloader.
34    *
35    * @var \Drupal\Core\Routing\RoutePreloader
36    */
37   protected $preloader;
38
39   /**
40    * The mocked cache.
41    *
42    * @var \Drupal\Core\Cache\CacheBackendInterface|\PHPUnit_Framework_MockObject_MockObject
43    */
44   protected $cache;
45
46   /**
47    * {@inheritdoc}
48    */
49   protected function setUp() {
50     $this->routeProvider = $this->getMock('Drupal\Core\Routing\PreloadableRouteProviderInterface');
51     $this->state = $this->getMock('\Drupal\Core\State\StateInterface');
52     $this->cache = $this->getMock('Drupal\Core\Cache\CacheBackendInterface');
53     $this->preloader = new RoutePreloader($this->routeProvider, $this->state, $this->cache);
54   }
55
56   /**
57    * Tests onAlterRoutes with just admin routes.
58    */
59   public function testOnAlterRoutesWithAdminRoutes() {
60     $event = $this->getMockBuilder('Drupal\Core\Routing\RouteBuildEvent')
61       ->disableOriginalConstructor()
62       ->getMock();
63     $route_collection = new RouteCollection();
64     $route_collection->add('test', new Route('/admin/foo', ['_controller' => 'Drupal\ExampleController']));
65     $route_collection->add('test2', new Route('/admin/bar', ['_controller' => 'Drupal\ExampleController']));
66     $event->expects($this->once())
67       ->method('getRouteCollection')
68       ->will($this->returnValue($route_collection));
69
70     $this->state->expects($this->once())
71       ->method('set')
72       ->with('routing.non_admin_routes', []);
73     $this->preloader->onAlterRoutes($event);
74     $this->preloader->onFinishedRoutes(new Event());
75   }
76
77   /**
78    * Tests onAlterRoutes with "admin" appearing in the path.
79    */
80   public function testOnAlterRoutesWithAdminPathNoAdminRoute() {
81     $event = $this->getMockBuilder('Drupal\Core\Routing\RouteBuildEvent')
82       ->disableOriginalConstructor()
83       ->getMock();
84     $route_collection = new RouteCollection();
85     $route_collection->add('test', new Route('/foo/admin/foo', ['_controller' => 'Drupal\ExampleController']));
86     $route_collection->add('test2', new Route('/bar/admin/bar', ['_controller' => 'Drupal\ExampleController']));
87     $route_collection->add('test3', new Route('/administrator/a', ['_controller' => 'Drupal\ExampleController']));
88     $route_collection->add('test4', new Route('/admin', ['_controller' => 'Drupal\ExampleController']));
89     $event->expects($this->once())
90       ->method('getRouteCollection')
91       ->will($this->returnValue($route_collection));
92
93     $this->state->expects($this->once())
94       ->method('set')
95       ->with('routing.non_admin_routes', ['test', 'test2', 'test3']);
96     $this->preloader->onAlterRoutes($event);
97     $this->preloader->onFinishedRoutes(new Event());
98   }
99
100
101   /**
102    * Tests onAlterRoutes with admin routes and non admin routes.
103    */
104   public function testOnAlterRoutesWithNonAdminRoutes() {
105     $event = $this->getMockBuilder('Drupal\Core\Routing\RouteBuildEvent')
106       ->disableOriginalConstructor()
107       ->getMock();
108     $route_collection = new RouteCollection();
109     $route_collection->add('test', new Route('/admin/foo', ['_controller' => 'Drupal\ExampleController']));
110     $route_collection->add('test2', new Route('/bar', ['_controller' => 'Drupal\ExampleController']));
111     // Non content routes, like ajax callbacks should be ignored.
112     $route_collection->add('test3', new Route('/bar', ['_controller' => 'Drupal\ExampleController']));
113     $event->expects($this->once())
114       ->method('getRouteCollection')
115       ->will($this->returnValue($route_collection));
116
117     $this->state->expects($this->once())
118       ->method('set')
119       ->with('routing.non_admin_routes', ['test2', 'test3']);
120     $this->preloader->onAlterRoutes($event);
121     $this->preloader->onFinishedRoutes(new Event());
122   }
123
124   /**
125    * Tests onRequest on a non html request.
126    */
127   public function testOnRequestNonHtml() {
128     $event = $this->getMockBuilder('\Symfony\Component\HttpKernel\Event\KernelEvent')
129       ->disableOriginalConstructor()
130       ->getMock();
131     $request = new Request();
132     $request->setRequestFormat('non-html');
133     $event->expects($this->any())
134       ->method('getRequest')
135       ->will($this->returnValue($request));
136
137     $this->routeProvider->expects($this->never())
138       ->method('getRoutesByNames');
139     $this->state->expects($this->never())
140       ->method('get');
141
142     $this->preloader->onRequest($event);
143   }
144
145   /**
146    * Tests onRequest on a html request.
147    */
148   public function testOnRequestOnHtml() {
149     $event = $this->getMockBuilder('\Symfony\Component\HttpKernel\Event\KernelEvent')
150       ->disableOriginalConstructor()
151       ->getMock();
152     $request = new Request();
153     $request->setRequestFormat('html');
154     $event->expects($this->any())
155       ->method('getRequest')
156       ->will($this->returnValue($request));
157
158     $this->routeProvider->expects($this->once())
159       ->method('preLoadRoutes')
160       ->with(['test2']);
161     $this->state->expects($this->once())
162       ->method('get')
163       ->with('routing.non_admin_routes')
164       ->will($this->returnValue(['test2']));
165
166     $this->preloader->onRequest($event);
167   }
168
169 }