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());
102 * Tests onAlterRoutes with admin routes and non admin routes.
104 public function testOnAlterRoutesWithNonAdminRoutes() {
105 $event = $this->getMockBuilder('Drupal\Core\Routing\RouteBuildEvent')
106 ->disableOriginalConstructor()
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));
117 $this->state->expects($this->once())
119 ->with('routing.non_admin_routes', ['test2', 'test3']);
120 $this->preloader->onAlterRoutes($event);
121 $this->preloader->onFinishedRoutes(new Event());
125 * Tests onRequest on a non html request.
127 public function testOnRequestNonHtml() {
128 $event = $this->getMockBuilder('\Symfony\Component\HttpKernel\Event\KernelEvent')
129 ->disableOriginalConstructor()
131 $request = new Request();
132 $request->setRequestFormat('non-html');
133 $event->expects($this->any())
134 ->method('getRequest')
135 ->will($this->returnValue($request));
137 $this->routeProvider->expects($this->never())
138 ->method('getRoutesByNames');
139 $this->state->expects($this->never())
142 $this->preloader->onRequest($event);
146 * Tests onRequest on a html request.
148 public function testOnRequestOnHtml() {
149 $event = $this->getMockBuilder('\Symfony\Component\HttpKernel\Event\KernelEvent')
150 ->disableOriginalConstructor()
152 $request = new Request();
153 $request->setRequestFormat('html');
154 $event->expects($this->any())
155 ->method('getRequest')
156 ->will($this->returnValue($request));
158 $this->routeProvider->expects($this->once())
159 ->method('preLoadRoutes')
161 $this->state->expects($this->once())
163 ->with('routing.non_admin_routes')
164 ->will($this->returnValue(['test2']));
166 $this->preloader->onRequest($event);