Updated Drupal to 8.6. This goes with the following updates because it's possible...
[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    * Tests onAlterRoutes with admin routes and non admin routes.
102    */
103   public function testOnAlterRoutesWithNonAdminRoutes() {
104     $event = $this->getMockBuilder('Drupal\Core\Routing\RouteBuildEvent')
105       ->disableOriginalConstructor()
106       ->getMock();
107     $route_collection = new RouteCollection();
108     $route_collection->add('test', new Route('/admin/foo', ['_controller' => 'Drupal\ExampleController']));
109     $route_collection->add('test2', new Route('/bar', ['_controller' => 'Drupal\ExampleController']));
110     // Non content routes, like ajax callbacks should be ignored.
111     $route_collection->add('test3', new Route('/bar', ['_controller' => 'Drupal\ExampleController']));
112     $event->expects($this->once())
113       ->method('getRouteCollection')
114       ->will($this->returnValue($route_collection));
115
116     $this->state->expects($this->once())
117       ->method('set')
118       ->with('routing.non_admin_routes', ['test2', 'test3']);
119     $this->preloader->onAlterRoutes($event);
120     $this->preloader->onFinishedRoutes(new Event());
121   }
122
123   /**
124    * Tests onRequest on a non html request.
125    */
126   public function testOnRequestNonHtml() {
127     $event = $this->getMockBuilder('\Symfony\Component\HttpKernel\Event\KernelEvent')
128       ->disableOriginalConstructor()
129       ->getMock();
130     $request = new Request();
131     $request->setRequestFormat('non-html');
132     $event->expects($this->any())
133       ->method('getRequest')
134       ->will($this->returnValue($request));
135
136     $this->routeProvider->expects($this->never())
137       ->method('getRoutesByNames');
138     $this->state->expects($this->never())
139       ->method('get');
140
141     $this->preloader->onRequest($event);
142   }
143
144   /**
145    * Tests onRequest on a html request.
146    */
147   public function testOnRequestOnHtml() {
148     $event = $this->getMockBuilder('\Symfony\Component\HttpKernel\Event\KernelEvent')
149       ->disableOriginalConstructor()
150       ->getMock();
151     $request = new Request();
152     $request->setRequestFormat('html');
153     $event->expects($this->any())
154       ->method('getRequest')
155       ->will($this->returnValue($request));
156
157     $this->routeProvider->expects($this->once())
158       ->method('preLoadRoutes')
159       ->with(['test2']);
160     $this->state->expects($this->once())
161       ->method('get')
162       ->with('routing.non_admin_routes')
163       ->will($this->returnValue(['test2']));
164
165     $this->preloader->onRequest($event);
166   }
167
168 }