RouteProviderTest.php 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741
  1. <?php
  2. /**
  3. * @file
  4. * Contains \Drupal\KernelTests\Core\Routing\RouteProviderTest.
  5. */
  6. namespace Drupal\KernelTests\Core\Routing;
  7. use Drupal\Core\Cache\MemoryBackend;
  8. use Drupal\Core\Database\Database;
  9. use Drupal\Core\DependencyInjection\ContainerBuilder;
  10. use Drupal\Core\KeyValueStore\KeyValueMemoryFactory;
  11. use Drupal\Core\Path\CurrentPathStack;
  12. use Drupal\Core\Routing\MatcherDumper;
  13. use Drupal\Core\Routing\RouteProvider;
  14. use Drupal\Core\State\State;
  15. use Drupal\KernelTests\KernelTestBase;
  16. use Drupal\language\Entity\ConfigurableLanguage;
  17. use Drupal\Tests\Core\Routing\RoutingFixtures;
  18. use Drupal\Tests\Traits\Core\PathAliasTestTrait;
  19. use Symfony\Component\HttpFoundation\Request;
  20. use Symfony\Component\HttpFoundation\RequestStack;
  21. use Symfony\Component\Routing\Exception\ResourceNotFoundException;
  22. use Symfony\Component\Routing\Exception\RouteNotFoundException;
  23. use Symfony\Component\Routing\Route;
  24. use Symfony\Component\Routing\RouteCollection;
  25. /**
  26. * Confirm that the default route provider is working correctly.
  27. *
  28. * @group Routing
  29. */
  30. class RouteProviderTest extends KernelTestBase {
  31. use PathAliasTestTrait;
  32. /**
  33. * Modules to enable.
  34. */
  35. public static $modules = [
  36. 'url_alter_test',
  37. 'system',
  38. 'language',
  39. 'path_alias',
  40. ];
  41. /**
  42. * A collection of shared fixture data for tests.
  43. *
  44. * @var \Drupal\Tests\Core\Routing\RoutingFixtures
  45. */
  46. protected $fixtures;
  47. /**
  48. * The state.
  49. *
  50. * @var \Drupal\Core\State\StateInterface
  51. */
  52. protected $state;
  53. /**
  54. * The current path.
  55. *
  56. * @var \Drupal\Core\Path\CurrentPathStack
  57. */
  58. protected $currentPath;
  59. /**
  60. * The cache backend.
  61. *
  62. * @var \Drupal\Core\Cache\MemoryBackend
  63. */
  64. protected $cache;
  65. /**
  66. * The inbound path processor.
  67. *
  68. * @var \Drupal\Core\PathProcessor\InboundPathProcessorInterface
  69. */
  70. protected $pathProcessor;
  71. /**
  72. * The cache tags invalidator.
  73. *
  74. * @var \Drupal\Core\Cache\CacheTagsInvalidatorInterface
  75. */
  76. protected $cacheTagsInvalidator;
  77. protected function setUp() {
  78. parent::setUp();
  79. $this->fixtures = new RoutingFixtures();
  80. $this->state = new State(new KeyValueMemoryFactory());
  81. $this->currentPath = new CurrentPathStack(new RequestStack());
  82. $this->cache = new MemoryBackend();
  83. $this->pathProcessor = \Drupal::service('path_processor_manager');
  84. $this->cacheTagsInvalidator = \Drupal::service('cache_tags.invalidator');
  85. $this->installEntitySchema('path_alias');
  86. }
  87. /**
  88. * {@inheritdoc}
  89. */
  90. public function register(ContainerBuilder $container) {
  91. parent::register($container);
  92. // Read the incoming path alias for these tests.
  93. if ($container->hasDefinition('path_alias.path_processor')) {
  94. $definition = $container->getDefinition('path_alias.path_processor');
  95. $definition->addTag('path_processor_inbound');
  96. }
  97. }
  98. protected function tearDown() {
  99. $this->fixtures->dropTables(Database::getConnection());
  100. parent::tearDown();
  101. }
  102. /**
  103. * Confirms that the correct candidate outlines are generated.
  104. */
  105. public function testCandidateOutlines() {
  106. $connection = Database::getConnection();
  107. $provider = new TestRouteProvider($connection, $this->state, $this->currentPath, $this->cache, $this->pathProcessor, $this->cacheTagsInvalidator, 'test_routes');
  108. $parts = ['node', '5', 'edit'];
  109. $candidates = $provider->getCandidateOutlines($parts);
  110. $candidates = array_flip($candidates);
  111. $this->assertCount(7, $candidates, 'Correct number of candidates found');
  112. $this->assertArrayHasKey('/node/5/edit', $candidates);
  113. $this->assertArrayHasKey('/node/5/%', $candidates);
  114. $this->assertArrayHasKey('/node/%/edit', $candidates);
  115. $this->assertArrayHasKey('/node/%/%', $candidates);
  116. $this->assertArrayHasKey('/node/5', $candidates);
  117. $this->assertArrayHasKey('/node/%', $candidates);
  118. $this->assertArrayHasKey('/node', $candidates);
  119. }
  120. /**
  121. * Don't fail when given an empty path.
  122. */
  123. public function testEmptyPathCandidatesOutlines() {
  124. $provider = new TestRouteProvider(Database::getConnection(), $this->state, $this->currentPath, $this->cache, $this->pathProcessor, $this->cacheTagsInvalidator, 'test_routes');
  125. $candidates = $provider->getCandidateOutlines([]);
  126. $this->assertCount(0, $candidates, 'Empty parts should return no candidates.');
  127. }
  128. /**
  129. * Confirms that we can find routes with the exact incoming path.
  130. */
  131. public function testExactPathMatch() {
  132. $connection = Database::getConnection();
  133. $provider = new RouteProvider($connection, $this->state, $this->currentPath, $this->cache, $this->pathProcessor, $this->cacheTagsInvalidator, 'test_routes');
  134. $this->fixtures->createTables($connection);
  135. $dumper = new MatcherDumper($connection, $this->state, 'test_routes');
  136. $dumper->addRoutes($this->fixtures->sampleRouteCollection());
  137. $dumper->dump();
  138. $path = '/path/one';
  139. $request = Request::create($path, 'GET');
  140. $routes = $provider->getRouteCollectionForRequest($request);
  141. foreach ($routes as $route) {
  142. $this->assertEqual($route->getPath(), $path, 'Found path has correct pattern');
  143. }
  144. }
  145. /**
  146. * Confirms that we can find routes whose pattern would match the request.
  147. */
  148. public function testOutlinePathMatch() {
  149. $connection = Database::getConnection();
  150. $provider = new RouteProvider($connection, $this->state, $this->currentPath, $this->cache, $this->pathProcessor, $this->cacheTagsInvalidator, 'test_routes');
  151. $this->fixtures->createTables($connection);
  152. $dumper = new MatcherDumper($connection, $this->state, 'test_routes');
  153. $dumper->addRoutes($this->fixtures->complexRouteCollection());
  154. $dumper->dump();
  155. $path = '/path/1/one';
  156. $request = Request::create($path, 'GET');
  157. $routes = $provider->getRouteCollectionForRequest($request);
  158. // All of the matching paths have the correct pattern.
  159. foreach ($routes as $route) {
  160. $this->assertEqual($route->compile()->getPatternOutline(), '/path/%/one', 'Found path has correct pattern');
  161. }
  162. $this->assertCount(2, $routes, 'The correct number of routes was found.');
  163. $this->assertNotNull($routes->get('route_a'), 'The first matching route was found.');
  164. $this->assertNotNull($routes->get('route_b'), 'The second matching route was not found.');
  165. }
  166. /**
  167. * Data provider for testMixedCasePaths()
  168. */
  169. public function providerMixedCaseRoutePaths() {
  170. return [
  171. ['/path/one', 'route_a'],
  172. ['/path/two', NULL],
  173. ['/PATH/one', 'route_a'],
  174. ['/path/2/one', 'route_b', 'PUT'],
  175. ['/paTH/3/one', 'route_b', 'PUT'],
  176. // There should be no lower case of a Hebrew letter.
  177. ['/somewhere/4/over/the/קainbow', 'route_c'],
  178. ['/Somewhere/5/over/the/קainboW', 'route_c'],
  179. ['/another/llama/aboUT/22', 'route_d'],
  180. ['/another/llama/about/22', 'route_d'],
  181. ['/place/meΦω', 'route_e', 'HEAD'],
  182. ['/place/meφΩ', 'route_e', 'HEAD'],
  183. ];
  184. }
  185. /**
  186. * Confirms that we find routes using a case-insensitive path match.
  187. *
  188. * @dataProvider providerMixedCaseRoutePaths
  189. */
  190. public function testMixedCasePaths($path, $expected_route_name, $method = 'GET') {
  191. $connection = Database::getConnection();
  192. $provider = new RouteProvider($connection, $this->state, $this->currentPath, $this->cache, $this->pathProcessor, $this->cacheTagsInvalidator, 'test_routes');
  193. $this->fixtures->createTables($connection);
  194. $dumper = new MatcherDumper($connection, $this->state, 'test_routes');
  195. $dumper->addRoutes($this->fixtures->mixedCaseRouteCollection());
  196. $dumper->dump();
  197. $request = Request::create($path, $method);
  198. $routes = $provider->getRouteCollectionForRequest($request);
  199. if ($expected_route_name) {
  200. $this->assertCount(1, $routes, 'The correct number of routes was found.');
  201. $this->assertNotNull($routes->get($expected_route_name), 'The first matching route was found.');
  202. }
  203. else {
  204. $this->assertCount(0, $routes, 'No routes matched.');
  205. }
  206. }
  207. /**
  208. * Data provider for testMixedCasePaths()
  209. */
  210. public function providerDuplicateRoutePaths() {
  211. // When matching routes with the same fit the route with the lowest-sorting
  212. // name should end up first in the resulting route collection.
  213. return [
  214. ['/path/one', 3, 'route_a'],
  215. ['/PATH/one', 3, 'route_a'],
  216. ['/path/two', 1, 'route_d'],
  217. ['/PATH/three', 0],
  218. ['/place/meΦω', 2, 'route_e'],
  219. ['/placE/meφΩ', 2, 'route_e'],
  220. ];
  221. }
  222. /**
  223. * Confirms that we find all routes with the same path.
  224. *
  225. * @dataProvider providerDuplicateRoutePaths
  226. */
  227. public function testDuplicateRoutePaths($path, $number, $expected_route_name = NULL) {
  228. $connection = Database::getConnection();
  229. $provider = new RouteProvider($connection, $this->state, $this->currentPath, $this->cache, $this->pathProcessor, $this->cacheTagsInvalidator, 'test_routes');
  230. $this->fixtures->createTables($connection);
  231. $dumper = new MatcherDumper($connection, $this->state, 'test_routes');
  232. $dumper->addRoutes($this->fixtures->duplicatePathsRouteCollection());
  233. $dumper->dump();
  234. $request = Request::create($path);
  235. $routes = $provider->getRouteCollectionForRequest($request);
  236. $this->assertEquals($number, count($routes), 'The correct number of routes was found.');
  237. if ($expected_route_name) {
  238. $route_name = key(current($routes));
  239. $this->assertEquals($expected_route_name, $route_name, 'The expected route name was found.');
  240. }
  241. }
  242. /**
  243. * Confirms that a trailing slash on the request does not result in a 404.
  244. */
  245. public function testOutlinePathMatchTrailingSlash() {
  246. $connection = Database::getConnection();
  247. $provider = new RouteProvider($connection, $this->state, $this->currentPath, $this->cache, $this->pathProcessor, $this->cacheTagsInvalidator, 'test_routes');
  248. $this->fixtures->createTables($connection);
  249. $dumper = new MatcherDumper($connection, $this->state, 'test_routes');
  250. $dumper->addRoutes($this->fixtures->complexRouteCollection());
  251. $dumper->dump();
  252. $path = '/path/1/one/';
  253. $request = Request::create($path, 'GET');
  254. $routes = $provider->getRouteCollectionForRequest($request);
  255. // All of the matching paths have the correct pattern.
  256. foreach ($routes as $route) {
  257. $this->assertEqual($route->compile()->getPatternOutline(), '/path/%/one', 'Found path has correct pattern');
  258. }
  259. $this->assertCount(2, $routes, 'The correct number of routes was found.');
  260. $this->assertNotNull($routes->get('route_a'), 'The first matching route was found.');
  261. $this->assertNotNull($routes->get('route_b'), 'The second matching route was not found.');
  262. }
  263. /**
  264. * Confirms that we can find routes whose pattern would match the request.
  265. */
  266. public function testOutlinePathMatchDefaults() {
  267. $connection = Database::getConnection();
  268. $provider = new RouteProvider($connection, $this->state, $this->currentPath, $this->cache, $this->pathProcessor, $this->cacheTagsInvalidator, 'test_routes');
  269. $this->fixtures->createTables($connection);
  270. $collection = new RouteCollection();
  271. $collection->add('poink', new Route('/some/path/{value}', [
  272. 'value' => 'poink',
  273. ]));
  274. $dumper = new MatcherDumper($connection, $this->state, 'test_routes');
  275. $dumper->addRoutes($collection);
  276. $dumper->dump();
  277. $path = '/some/path';
  278. $request = Request::create($path, 'GET');
  279. try {
  280. $routes = $provider->getRouteCollectionForRequest($request);
  281. // All of the matching paths have the correct pattern.
  282. foreach ($routes as $route) {
  283. $this->assertEqual($route->compile()->getPatternOutline(), '/some/path', 'Found path has correct pattern');
  284. }
  285. $this->assertCount(1, $routes, 'The correct number of routes was found.');
  286. $this->assertNotNull($routes->get('poink'), 'The first matching route was found.');
  287. }
  288. catch (ResourceNotFoundException $e) {
  289. $this->fail('No matching route found with default argument value.');
  290. }
  291. }
  292. /**
  293. * Confirms that we can find routes whose pattern would match the request.
  294. */
  295. public function testOutlinePathMatchDefaultsCollision() {
  296. $connection = Database::getConnection();
  297. $provider = new RouteProvider($connection, $this->state, $this->currentPath, $this->cache, $this->pathProcessor, $this->cacheTagsInvalidator, 'test_routes');
  298. $this->fixtures->createTables($connection);
  299. $collection = new RouteCollection();
  300. $collection->add('poink', new Route('/some/path/{value}', [
  301. 'value' => 'poink',
  302. ]));
  303. $collection->add('narf', new Route('/some/path/here'));
  304. $dumper = new MatcherDumper($connection, $this->state, 'test_routes');
  305. $dumper->addRoutes($collection);
  306. $dumper->dump();
  307. $path = '/some/path';
  308. $request = Request::create($path, 'GET');
  309. try {
  310. $routes = $provider->getRouteCollectionForRequest($request);
  311. // All of the matching paths have the correct pattern.
  312. foreach ($routes as $route) {
  313. $this->assertEqual($route->compile()->getPatternOutline(), '/some/path', 'Found path has correct pattern');
  314. }
  315. $this->assertCount(1, $routes, 'The correct number of routes was found.');
  316. $this->assertNotNull($routes->get('poink'), 'The first matching route was found.');
  317. }
  318. catch (ResourceNotFoundException $e) {
  319. $this->fail('No matching route found with default argument value.');
  320. }
  321. }
  322. /**
  323. * Confirms that we can find routes whose pattern would match the request.
  324. */
  325. public function testOutlinePathMatchDefaultsCollision2() {
  326. $connection = Database::getConnection();
  327. $provider = new RouteProvider($connection, $this->state, $this->currentPath, $this->cache, $this->pathProcessor, $this->cacheTagsInvalidator, 'test_routes');
  328. $this->fixtures->createTables($connection);
  329. $collection = new RouteCollection();
  330. $collection->add('poink', new Route('/some/path/{value}', [
  331. 'value' => 'poink',
  332. ]));
  333. $collection->add('narf', new Route('/some/path/here'));
  334. $collection->add('eep', new Route('/something/completely/different'));
  335. $dumper = new MatcherDumper($connection, $this->state, 'test_routes');
  336. $dumper->addRoutes($collection);
  337. $dumper->dump();
  338. $path = '/some/path/here';
  339. $request = Request::create($path, 'GET');
  340. try {
  341. $routes = $provider->getRouteCollectionForRequest($request);
  342. $routes_array = $routes->all();
  343. $this->assertCount(2, $routes, 'The correct number of routes was found.');
  344. $this->assertEqual(['narf', 'poink'], array_keys($routes_array), 'Ensure the fitness was taken into account.');
  345. $this->assertNotNull($routes->get('narf'), 'The first matching route was found.');
  346. $this->assertNotNull($routes->get('poink'), 'The second matching route was found.');
  347. $this->assertNull($routes->get('eep'), 'Non-matching route was not found.');
  348. }
  349. catch (ResourceNotFoundException $e) {
  350. $this->fail('No matching route found with default argument value.');
  351. }
  352. }
  353. /**
  354. * Confirms that we can find multiple routes that match the request equally.
  355. */
  356. public function testOutlinePathMatchDefaultsCollision3() {
  357. $connection = Database::getConnection();
  358. $provider = new RouteProvider($connection, $this->state, $this->currentPath, $this->cache, $this->pathProcessor, $this->cacheTagsInvalidator, 'test_routes');
  359. $this->fixtures->createTables($connection);
  360. $collection = new RouteCollection();
  361. $collection->add('poink', new Route('/some/{value}/path'));
  362. // Add a second route matching the same path pattern.
  363. $collection->add('poink2', new Route('/some/{object}/path'));
  364. $collection->add('narf', new Route('/some/here/path'));
  365. $collection->add('eep', new Route('/something/completely/different'));
  366. $dumper = new MatcherDumper($connection, $this->state, 'test_routes');
  367. $dumper->addRoutes($collection);
  368. $dumper->dump();
  369. $path = '/some/over-there/path';
  370. $request = Request::create($path, 'GET');
  371. try {
  372. $routes = $provider->getRouteCollectionForRequest($request);
  373. $routes_array = $routes->all();
  374. $this->assertCount(2, $routes, 'The correct number of routes was found.');
  375. $this->assertEqual(['poink', 'poink2'], array_keys($routes_array), 'Ensure the fitness and name were taken into account in the sort.');
  376. $this->assertNotNull($routes->get('poink'), 'The first matching route was found.');
  377. $this->assertNotNull($routes->get('poink2'), 'The second matching route was found.');
  378. $this->assertNull($routes->get('eep'), 'Non-matching route was not found.');
  379. }
  380. catch (ResourceNotFoundException $e) {
  381. $this->fail('No matching route found with default argument value.');
  382. }
  383. }
  384. /**
  385. * Tests a route with a 0 as value.
  386. */
  387. public function testOutlinePathMatchZero() {
  388. $connection = Database::getConnection();
  389. $provider = new RouteProvider($connection, $this->state, $this->currentPath, $this->cache, $this->pathProcessor, $this->cacheTagsInvalidator, 'test_routes');
  390. $this->fixtures->createTables($connection);
  391. $collection = new RouteCollection();
  392. $collection->add('poink', new Route('/some/path/{value}'));
  393. $dumper = new MatcherDumper($connection, $this->state, 'test_routes');
  394. $dumper->addRoutes($collection);
  395. $dumper->dump();
  396. $path = '/some/path/0';
  397. $request = Request::create($path, 'GET');
  398. try {
  399. $routes = $provider->getRouteCollectionForRequest($request);
  400. // All of the matching paths have the correct pattern.
  401. foreach ($routes as $route) {
  402. $this->assertEqual($route->compile()->getPatternOutline(), '/some/path/%', 'Found path has correct pattern');
  403. }
  404. $this->assertCount(1, $routes, 'The correct number of routes was found.');
  405. }
  406. catch (ResourceNotFoundException $e) {
  407. $this->fail('No matchout route found with 0 as argument value');
  408. }
  409. }
  410. /**
  411. * Confirms that an exception is thrown when no matching path is found.
  412. */
  413. public function testOutlinePathNoMatch() {
  414. $connection = Database::getConnection();
  415. $provider = new RouteProvider($connection, $this->state, $this->currentPath, $this->cache, $this->pathProcessor, $this->cacheTagsInvalidator, 'test_routes');
  416. $this->fixtures->createTables($connection);
  417. $dumper = new MatcherDumper($connection, $this->state, 'test_routes');
  418. $dumper->addRoutes($this->fixtures->complexRouteCollection());
  419. $dumper->dump();
  420. $path = '/no/such/path';
  421. $request = Request::create($path, 'GET');
  422. $routes = $provider->getRoutesByPattern($path);
  423. $this->assertEmpty($routes, 'No path found with this pattern.');
  424. $collection = $provider->getRouteCollectionForRequest($request);
  425. $this->assertEmpty($collection, 'Empty route collection found with this pattern.');
  426. }
  427. /**
  428. * Tests that route caching works.
  429. */
  430. public function testRouteCaching() {
  431. $connection = Database::getConnection();
  432. $language_manager = \Drupal::languageManager();
  433. $provider = new RouteProvider($connection, $this->state, $this->currentPath, $this->cache, $this->pathProcessor, $this->cacheTagsInvalidator, 'test_routes', $language_manager);
  434. $this->fixtures->createTables($connection);
  435. $dumper = new MatcherDumper($connection, $this->state, 'test_routes');
  436. $dumper->addRoutes($this->fixtures->sampleRouteCollection());
  437. $dumper->addRoutes($this->fixtures->complexRouteCollection());
  438. $dumper->dump();
  439. // A simple path.
  440. $path = '/path/add/one';
  441. $request = Request::create($path, 'GET');
  442. $provider->getRouteCollectionForRequest($request);
  443. $cache = $this->cache->get('route:[language]=en:/path/add/one:');
  444. $this->assertEqual('/path/add/one', $cache->data['path']);
  445. $this->assertEqual([], $cache->data['query']);
  446. $this->assertCount(3, $cache->data['routes']);
  447. // A path with query parameters.
  448. $path = '/path/add/one?foo=bar';
  449. $request = Request::create($path, 'GET');
  450. $provider->getRouteCollectionForRequest($request);
  451. $cache = $this->cache->get('route:[language]=en:/path/add/one:foo=bar');
  452. $this->assertEqual('/path/add/one', $cache->data['path']);
  453. $this->assertEqual(['foo' => 'bar'], $cache->data['query']);
  454. $this->assertCount(3, $cache->data['routes']);
  455. // A path with placeholders.
  456. $path = '/path/1/one';
  457. $request = Request::create($path, 'GET');
  458. $provider->getRouteCollectionForRequest($request);
  459. $cache = $this->cache->get('route:[language]=en:/path/1/one:');
  460. $this->assertEqual('/path/1/one', $cache->data['path']);
  461. $this->assertEqual([], $cache->data['query']);
  462. $this->assertCount(2, $cache->data['routes']);
  463. // A path with a path alias.
  464. $this->createPathAlias('/path/add/one', '/path/add-one');
  465. /** @var \Drupal\path_alias\AliasManagerInterface $alias_manager */
  466. $alias_manager = \Drupal::service('path_alias.manager');
  467. $alias_manager->cacheClear();
  468. $path = '/path/add-one';
  469. $request = Request::create($path, 'GET');
  470. $provider->getRouteCollectionForRequest($request);
  471. $cache = $this->cache->get('route:[language]=en:/path/add-one:');
  472. $this->assertEqual('/path/add/one', $cache->data['path']);
  473. $this->assertEqual([], $cache->data['query']);
  474. $this->assertCount(3, $cache->data['routes']);
  475. // Test with a different current language by switching out the default
  476. // language.
  477. $swiss = ConfigurableLanguage::createFromLangcode('gsw-berne');
  478. $language_manager->reset();
  479. \Drupal::service('language.default')->set($swiss);
  480. $path = '/path/add-one';
  481. $request = Request::create($path, 'GET');
  482. $provider->getRouteCollectionForRequest($request);
  483. $cache = $this->cache->get('route:[language]=gsw-berne:/path/add-one:');
  484. $this->assertEquals('/path/add/one', $cache->data['path']);
  485. $this->assertEquals([], $cache->data['query']);
  486. $this->assertCount(3, $cache->data['routes']);
  487. }
  488. /**
  489. * Test RouteProvider::getRouteByName() and RouteProvider::getRoutesByNames().
  490. */
  491. public function testRouteByName() {
  492. $connection = Database::getConnection();
  493. $provider = new RouteProvider($connection, $this->state, $this->currentPath, $this->cache, $this->pathProcessor, $this->cacheTagsInvalidator, 'test_routes');
  494. $this->fixtures->createTables($connection);
  495. $dumper = new MatcherDumper($connection, $this->state, 'test_routes');
  496. $dumper->addRoutes($this->fixtures->sampleRouteCollection());
  497. $dumper->dump();
  498. $route = $provider->getRouteByName('route_a');
  499. $this->assertEqual($route->getPath(), '/path/one', 'The right route pattern was found.');
  500. $this->assertEqual($route->getMethods(), ['GET'], 'The right route method was found.');
  501. $route = $provider->getRouteByName('route_b');
  502. $this->assertEqual($route->getPath(), '/path/one', 'The right route pattern was found.');
  503. $this->assertEqual($route->getMethods(), ['PUT'], 'The right route method was found.');
  504. $exception_thrown = FALSE;
  505. try {
  506. $provider->getRouteByName('invalid_name');
  507. }
  508. catch (RouteNotFoundException $e) {
  509. $exception_thrown = TRUE;
  510. }
  511. $this->assertTrue($exception_thrown, 'Random route was not found.');
  512. $routes = $provider->getRoutesByNames(['route_c', 'route_d', $this->randomMachineName()]);
  513. $this->assertCount(2, $routes, 'Only two valid routes found.');
  514. $this->assertEqual($routes['route_c']->getPath(), '/path/two');
  515. $this->assertEqual($routes['route_d']->getPath(), '/path/three');
  516. }
  517. /**
  518. * Ensures that the routing system is capable of extreme long patterns.
  519. */
  520. public function testGetRoutesByPatternWithLongPatterns() {
  521. $connection = Database::getConnection();
  522. $provider = new TestRouteProvider($connection, $this->state, $this->currentPath, $this->cache, $this->pathProcessor, $this->cacheTagsInvalidator, 'test_routes');
  523. $this->fixtures->createTables($connection);
  524. // This pattern has only 3 parts, so we will get candidates, but no routes,
  525. // even though we have not dumped the routes yet.
  526. $shortest = '/test/1/test2';
  527. $result = $provider->getRoutesByPattern($shortest);
  528. $this->assertEqual($result->count(), 0);
  529. $candidates = $provider->getCandidateOutlines(explode('/', trim($shortest, '/')));
  530. $this->assertCount(7, $candidates);
  531. // A longer patten is not found and returns no candidates
  532. $path_to_test = '/test/1/test2/2/test3/3/4/5/6/test4';
  533. $result = $provider->getRoutesByPattern($path_to_test);
  534. $this->assertEqual($result->count(), 0);
  535. $candidates = $provider->getCandidateOutlines(explode('/', trim($path_to_test, '/')));
  536. $this->assertCount(0, $candidates);
  537. // Add a matching route and dump it.
  538. $dumper = new MatcherDumper($connection, $this->state, 'test_routes');
  539. $collection = new RouteCollection();
  540. $collection->add('long_pattern', new Route('/test/{v1}/test2/{v2}/test3/{v3}/{v4}/{v5}/{v6}/test4'));
  541. $dumper->addRoutes($collection);
  542. $dumper->dump();
  543. $result = $provider->getRoutesByPattern($path_to_test);
  544. $this->assertEqual($result->count(), 1);
  545. // We can't compare the values of the routes directly, nor use
  546. // spl_object_hash() because they are separate instances.
  547. $this->assertEqual(serialize($result->get('long_pattern')), serialize($collection->get('long_pattern')), 'The right route was found.');
  548. // We now have a single candidate outline.
  549. $candidates = $provider->getCandidateOutlines(explode('/', trim($path_to_test, '/')));
  550. $this->assertCount(1, $candidates);
  551. // Longer and shorter patterns are not found. Both are longer than 3, so
  552. // we should not have any candidates either. The fact that we do not
  553. // get any candidates for a longer path is a security feature.
  554. $longer = '/test/1/test2/2/test3/3/4/5/6/test4/trailing/more/parts';
  555. $result = $provider->getRoutesByPattern($longer);
  556. $this->assertEqual($result->count(), 0);
  557. $candidates = $provider->getCandidateOutlines(explode('/', trim($longer, '/')));
  558. $this->assertCount(1, $candidates);
  559. $shorter = '/test/1/test2/2/test3';
  560. $result = $provider->getRoutesByPattern($shorter);
  561. $this->assertEqual($result->count(), 0);
  562. $candidates = $provider->getCandidateOutlines(explode('/', trim($shorter, '/')));
  563. $this->assertCount(0, $candidates);
  564. // This pattern has only 3 parts, so we will get candidates, but no routes.
  565. // This result is unchanged by running the dumper.
  566. $result = $provider->getRoutesByPattern($shortest);
  567. $this->assertEqual($result->count(), 0);
  568. $candidates = $provider->getCandidateOutlines(explode('/', trim($shortest, '/')));
  569. $this->assertCount(7, $candidates);
  570. }
  571. /**
  572. * Tests getRoutesPaged().
  573. */
  574. public function testGetRoutesPaged() {
  575. $connection = Database::getConnection();
  576. $provider = new RouteProvider($connection, $this->state, $this->currentPath, $this->cache, $this->pathProcessor, $this->cacheTagsInvalidator, 'test_routes');
  577. $this->fixtures->createTables($connection);
  578. $dumper = new MatcherDumper($connection, $this->state, 'test_routes');
  579. $dumper->addRoutes($this->fixtures->sampleRouteCollection());
  580. $dumper->dump();
  581. $fixture_routes = $this->fixtures->staticSampleRouteCollection();
  582. // Query all the routes.
  583. $routes = $provider->getRoutesPaged(0);
  584. $this->assertEqual(array_keys($routes), array_keys($fixture_routes));
  585. // Query non routes.
  586. $routes = $provider->getRoutesPaged(0, 0);
  587. $this->assertEqual(array_keys($routes), []);
  588. // Query a limited sets of routes.
  589. $routes = $provider->getRoutesPaged(1, 2);
  590. $this->assertEqual(array_keys($routes), array_slice(array_keys($fixture_routes), 1, 2));
  591. }
  592. }
  593. class TestRouteProvider extends RouteProvider {
  594. public function getCandidateOutlines(array $parts) {
  595. return parent::getCandidateOutlines($parts);
  596. }
  597. }