PageRenderTime 43ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/core/tests/Drupal/Tests/Core/Routing/RouteBuilderTest.php

https://gitlab.com/reasonat/test8
PHP | 325 lines | 176 code | 56 blank | 93 comment | 3 complexity | 7498e609ae6463c3ce92b5819314a7ce MD5 | raw file
  1. <?php
  2. /**
  3. * @file
  4. * Contains \Drupal\Tests\Core\Routing\RouteBuilderTest.
  5. */
  6. namespace Drupal\Tests\Core\Routing;
  7. use Drupal\Component\Discovery\YamlDiscovery;
  8. use Drupal\Core\DependencyInjection\ContainerBuilder;
  9. use Drupal\Core\Routing\RouteBuilder;
  10. use Drupal\Core\Routing\RouteBuildEvent;
  11. use Drupal\Core\Routing\RoutingEvents;
  12. use Drupal\Tests\UnitTestCase;
  13. use Symfony\Component\Routing\Route;
  14. use Symfony\Component\Routing\RouteCollection;
  15. /**
  16. * @coversDefaultClass \Drupal\Core\Routing\RouteBuilder
  17. * @group Routing
  18. */
  19. class RouteBuilderTest extends UnitTestCase {
  20. /**
  21. * The actual tested route builder.
  22. *
  23. * @var \Drupal\Core\Routing\RouteBuilder
  24. */
  25. protected $routeBuilder;
  26. /**
  27. * The mocked matcher dumper.
  28. *
  29. * @var \Drupal\Core\Routing\MatcherDumperInterface|\PHPUnit_Framework_MockObject_MockObject
  30. */
  31. protected $dumper;
  32. /**
  33. * The mocked lock backend.
  34. *
  35. * @var \Drupal\Core\Lock\LockBackendInterface|\PHPUnit_Framework_MockObject_MockObject
  36. */
  37. protected $lock;
  38. /**
  39. * The mocked event dispatcher.
  40. *
  41. * @var \Symfony\Component\EventDispatcher\EventDispatcherInterface|\PHPUnit_Framework_MockObject_MockObject
  42. */
  43. protected $dispatcher;
  44. /**
  45. * The mocked YAML discovery.
  46. *
  47. * @var \Drupal\Component\Discovery\YamlDiscovery|\PHPUnit_Framework_MockObject_MockObject
  48. */
  49. protected $yamlDiscovery;
  50. /**
  51. * The module handler.
  52. *
  53. * @var \Drupal\Core\Extension\ModuleHandlerInterface
  54. */
  55. protected $moduleHandler;
  56. /**
  57. * The controller resolver.
  58. *
  59. * @var \Drupal\Core\Controller\ControllerResolverInterface|\PHPUnit_Framework_MockObject_MockObject
  60. */
  61. protected $controllerResolver;
  62. /**
  63. * @var \Drupal\Core\Access\CheckProviderInterface|\PHPUnit_Framework_MockObject_MockObject
  64. */
  65. protected $checkProvider;
  66. protected function setUp() {
  67. $this->dumper = $this->getMock('Drupal\Core\Routing\MatcherDumperInterface');
  68. $this->lock = $this->getMock('Drupal\Core\Lock\LockBackendInterface');
  69. $this->dispatcher = $this->getMock('\Symfony\Component\EventDispatcher\EventDispatcherInterface');
  70. $this->moduleHandler = $this->getMock('Drupal\Core\Extension\ModuleHandlerInterface');
  71. $this->controllerResolver = $this->getMock('Drupal\Core\Controller\ControllerResolverInterface');
  72. $this->yamlDiscovery = $this->getMockBuilder('\Drupal\Component\Discovery\YamlDiscovery')
  73. ->disableOriginalConstructor()
  74. ->getMock();
  75. $this->checkProvider = $this->getMock('\Drupal\Core\Access\CheckProviderInterface');
  76. $this->routeBuilder = new TestRouteBuilder($this->dumper, $this->lock, $this->dispatcher, $this->moduleHandler, $this->controllerResolver, $this->checkProvider);
  77. $this->routeBuilder->setYamlDiscovery($this->yamlDiscovery);
  78. }
  79. /**
  80. * Tests that the route rebuilding both locks and unlocks.
  81. */
  82. public function testRebuildLockingUnlocking() {
  83. $this->lock->expects($this->once())
  84. ->method('acquire')
  85. ->with('router_rebuild')
  86. ->will($this->returnValue(TRUE));
  87. $this->lock->expects($this->once())
  88. ->method('release')
  89. ->with('router_rebuild');
  90. $this->yamlDiscovery->expects($this->any())
  91. ->method('findAll')
  92. ->will($this->returnValue(array()));
  93. $this->assertTrue($this->routeBuilder->rebuild());
  94. }
  95. /**
  96. * Tests route rebuilding with a blocking lock.
  97. */
  98. public function testRebuildBlockingLock() {
  99. $this->lock->expects($this->once())
  100. ->method('acquire')
  101. ->with('router_rebuild')
  102. ->will($this->returnValue(FALSE));
  103. $this->lock->expects($this->once())
  104. ->method('wait')
  105. ->with('router_rebuild');
  106. $this->lock->expects($this->never())
  107. ->method('release');
  108. $this->yamlDiscovery->expects($this->never())
  109. ->method('findAll');
  110. $this->assertFalse($this->routeBuilder->rebuild());
  111. }
  112. /**
  113. * Tests that provided routes by a module is put into the dumper/dispatcher.
  114. *
  115. * @see \Drupal\Core\Routing\RouteBuilder::rebuild()
  116. */
  117. public function testRebuildWithStaticModuleRoutes() {
  118. $this->lock->expects($this->once())
  119. ->method('acquire')
  120. ->with('router_rebuild')
  121. ->will($this->returnValue(TRUE));
  122. $routing_fixtures = new RoutingFixtures();
  123. $routes = $routing_fixtures->staticSampleRouteCollection();
  124. $this->yamlDiscovery->expects($this->once())
  125. ->method('findAll')
  126. ->will($this->returnValue(array('test_module' => $routes)));
  127. $route_collection = $routing_fixtures->sampleRouteCollection();
  128. $route_build_event = new RouteBuildEvent($route_collection);
  129. // Ensure that the alter routes events are fired.
  130. $this->dispatcher->expects($this->at(0))
  131. ->method('dispatch')
  132. ->with(RoutingEvents::DYNAMIC, $route_build_event);
  133. $this->dispatcher->expects($this->at(1))
  134. ->method('dispatch')
  135. ->with(RoutingEvents::ALTER, $route_build_event);
  136. // Ensure that access checks are set.
  137. $this->checkProvider->expects($this->once())
  138. ->method('setChecks')
  139. ->with($route_collection);
  140. // Ensure that the routes are set to the dumper and dumped.
  141. $this->dumper->expects($this->at(0))
  142. ->method('addRoutes')
  143. ->with($route_collection);
  144. $this->dumper->expects($this->at(1))
  145. ->method('dump')
  146. ->with();
  147. $this->assertTrue($this->routeBuilder->rebuild());
  148. }
  149. /**
  150. * Tests the rebuild with routes provided by a callback.
  151. *
  152. * @see \Drupal\Core\Routing\RouteBuilder::rebuild()
  153. */
  154. public function testRebuildWithProviderBasedRoutes() {
  155. $this->lock->expects($this->once())
  156. ->method('acquire')
  157. ->with('router_rebuild')
  158. ->will($this->returnValue(TRUE));
  159. $this->yamlDiscovery->expects($this->once())
  160. ->method('findAll')
  161. ->will($this->returnValue(array(
  162. 'test_module' => array(
  163. 'route_callbacks' => array(
  164. '\Drupal\Tests\Core\Routing\TestRouteSubscriber::routesFromArray',
  165. 'test_module.route_service:routesFromCollection',
  166. ),
  167. ),
  168. )));
  169. $container = new ContainerBuilder();
  170. $container->set('test_module.route_service', new TestRouteSubscriber());
  171. $this->controllerResolver->expects($this->any())
  172. ->method('getControllerFromDefinition')
  173. ->will($this->returnCallback(function ($controller) use ($container) {
  174. $count = substr_count($controller, ':');
  175. if ($count == 1) {
  176. list($service, $method) = explode(':', $controller, 2);
  177. $object = $container->get($service);
  178. }
  179. else {
  180. list($class, $method) = explode('::', $controller, 2);
  181. $object = new $class();
  182. }
  183. return array($object, $method);
  184. }));
  185. $route_collection_filled = new RouteCollection();
  186. $route_collection_filled->add('test_route.1', new Route('/test-route/1'));
  187. $route_collection_filled->add('test_route.2', new Route('/test-route/2'));
  188. $route_build_event = new RouteBuildEvent($route_collection_filled);
  189. // Ensure that the alter routes events are fired.
  190. $this->dispatcher->expects($this->at(0))
  191. ->method('dispatch')
  192. ->with(RoutingEvents::DYNAMIC, $route_build_event);
  193. $this->dispatcher->expects($this->at(1))
  194. ->method('dispatch')
  195. ->with(RoutingEvents::ALTER, $route_build_event);
  196. // Ensure that access checks are set.
  197. $this->checkProvider->expects($this->once())
  198. ->method('setChecks')
  199. ->with($route_collection_filled);
  200. // Ensure that the routes are set to the dumper and dumped.
  201. $this->dumper->expects($this->at(0))
  202. ->method('addRoutes')
  203. ->with($route_collection_filled);
  204. $this->dumper->expects($this->at(1))
  205. ->method('dump');
  206. $this->assertTrue($this->routeBuilder->rebuild());
  207. }
  208. /**
  209. * Tests \Drupal\Core\Routing\RouteBuilder::rebuildIfNeeded() method.
  210. */
  211. public function testRebuildIfNeeded() {
  212. $this->lock->expects($this->once())
  213. ->method('acquire')
  214. ->with('router_rebuild')
  215. ->will($this->returnValue(TRUE));
  216. $this->lock->expects($this->once())
  217. ->method('release')
  218. ->with('router_rebuild');
  219. $this->yamlDiscovery->expects($this->any())
  220. ->method('findAll')
  221. ->will($this->returnValue(array()));
  222. $this->routeBuilder->setRebuildNeeded();
  223. // This will trigger a successful rebuild.
  224. $this->assertTrue($this->routeBuilder->rebuildIfNeeded());
  225. // This will not trigger a rebuild.
  226. $this->assertFalse($this->routeBuilder->rebuildIfNeeded());
  227. }
  228. }
  229. /**
  230. * Extends the core route builder with a setter method for the YAML discovery.
  231. */
  232. class TestRouteBuilder extends RouteBuilder {
  233. /**
  234. * The mocked YAML discovery.
  235. *
  236. * @var \Drupal\Component\Discovery\YamlDiscovery|\PHPUnit_Framework_MockObject_MockObject
  237. */
  238. protected $yamlDiscovery;
  239. /**
  240. * Sets the YAML discovery.
  241. *
  242. * @param \Drupal\Component\Discovery\YamlDiscovery $yaml_discovery
  243. * The YAML discovery to set.
  244. */
  245. public function setYamlDiscovery(YamlDiscovery $yaml_discovery) {
  246. $this->yamlDiscovery = $yaml_discovery;
  247. }
  248. /**
  249. * {@inheritdoc}
  250. */
  251. protected function getRouteDefinitions() {
  252. return $this->yamlDiscovery->findAll();
  253. }
  254. }
  255. /**
  256. * Provides a callback for route definition.
  257. */
  258. class TestRouteSubscriber {
  259. public function routesFromArray() {
  260. return array(
  261. 'test_route.1' => new Route('/test-route/1'),
  262. );
  263. }
  264. public function routesFromCollection() {
  265. $collection = new RouteCollection();
  266. $collection->add('test_route.2', new Route('/test-route/2'));
  267. return $collection;
  268. }
  269. }