RoutePreloaderTest.php 5.7 KB

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