3 namespace Drupal\Tests\Core\Routing;
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;
13 * @coversDefaultClass \Drupal\Core\Routing\RoutePreloader
16 class RoutePreloaderTest extends UnitTestCase {
19 * The mocked route provider.
21 * @var \Drupal\Core\Routing\RouteProviderInterface|\PHPUnit_Framework_MockObject_MockObject
23 protected $routeProvider;
28 * @var \Drupal\Core\State\StateInterface|\PHPUnit_Framework_MockObject_MockObject
33 * The tested preloader.
35 * @var \Drupal\Core\Routing\RoutePreloader
42 * @var \Drupal\Core\Cache\CacheBackendInterface|\PHPUnit_Framework_MockObject_MockObject
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);
57 * Tests onAlterRoutes with just admin routes.
59 public function testOnAlterRoutesWithAdminRoutes() {
60 $event = $this->getMockBuilder('Drupal\Core\Routing\RouteBuildEvent')
61 ->disableOriginalConstructor()
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));
70 $this->state->expects($this->once())
72 ->with('routing.non_admin_routes', []);
73 $this->preloader->onAlterRoutes($event);
74 $this->preloader->onFinishedRoutes(new Event());
78 * Tests onAlterRoutes with "admin" appearing in the path.
80 public function testOnAlterRoutesWithAdminPathNoAdminRoute() {
81 $event = $this->getMockBuilder('Drupal\Core\Routing\RouteBuildEvent')
82 ->disableOriginalConstructor()
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));
93 $this->state->expects($this->once())
95 ->with('routing.non_admin_routes', ['test', 'test2', 'test3']);
96 $this->preloader->onAlterRoutes($event);
97 $this->preloader->onFinishedRoutes(new Event());
101 * Tests onAlterRoutes with admin routes and non admin routes.
103 public function testOnAlterRoutesWithNonAdminRoutes() {
104 $event = $this->getMockBuilder('Drupal\Core\Routing\RouteBuildEvent')
105 ->disableOriginalConstructor()
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));
116 $this->state->expects($this->once())
118 ->with('routing.non_admin_routes', ['test2', 'test3']);
119 $this->preloader->onAlterRoutes($event);
120 $this->preloader->onFinishedRoutes(new Event());
124 * Tests onRequest on a non html request.
126 public function testOnRequestNonHtml() {
127 $event = $this->getMockBuilder('\Symfony\Component\HttpKernel\Event\KernelEvent')
128 ->disableOriginalConstructor()
130 $request = new Request();
131 $request->setRequestFormat('non-html');
132 $event->expects($this->any())
133 ->method('getRequest')
134 ->will($this->returnValue($request));
136 $this->routeProvider->expects($this->never())
137 ->method('getRoutesByNames');
138 $this->state->expects($this->never())
141 $this->preloader->onRequest($event);
145 * Tests onRequest on a html request.
147 public function testOnRequestOnHtml() {
148 $event = $this->getMockBuilder('\Symfony\Component\HttpKernel\Event\KernelEvent')
149 ->disableOriginalConstructor()
151 $request = new Request();
152 $request->setRequestFormat('html');
153 $event->expects($this->any())
154 ->method('getRequest')
155 ->will($this->returnValue($request));
157 $this->routeProvider->expects($this->once())
158 ->method('preLoadRoutes')
160 $this->state->expects($this->once())
162 ->with('routing.non_admin_routes')
163 ->will($this->returnValue(['test2']));
165 $this->preloader->onRequest($event);