5 * Contains \Drupal\KernelTests\Core\Routing\RouteProviderTest.
8 namespace Drupal\KernelTests\Core\Routing;
10 use Drupal\Component\Utility\Unicode;
11 use Drupal\Core\Cache\MemoryBackend;
12 use Drupal\Core\Database\Database;
13 use Drupal\Core\DependencyInjection\ContainerBuilder;
14 use Drupal\Core\KeyValueStore\KeyValueMemoryFactory;
15 use Drupal\Core\Path\CurrentPathStack;
16 use Drupal\Core\Routing\MatcherDumper;
17 use Drupal\Core\Routing\RouteProvider;
18 use Drupal\Core\State\State;
19 use Drupal\KernelTests\KernelTestBase;
20 use Drupal\language\Entity\ConfigurableLanguage;
21 use Drupal\Tests\Core\Routing\RoutingFixtures;
22 use Symfony\Component\HttpFoundation\Request;
23 use Symfony\Component\HttpFoundation\RequestStack;
24 use Symfony\Component\Routing\Exception\ResourceNotFoundException;
25 use Symfony\Component\Routing\Exception\RouteNotFoundException;
26 use Symfony\Component\Routing\Route;
27 use Symfony\Component\Routing\RouteCollection;
30 * Confirm that the default route provider is working correctly.
34 class RouteProviderTest extends KernelTestBase {
39 public static $modules = ['url_alter_test', 'system', 'language'];
42 * A collection of shared fixture data for tests.
44 * @var \Drupal\Tests\Core\Routing\RoutingFixtures
51 * @var \Drupal\Core\State\StateInterface
58 * @var \Drupal\Core\Path\CurrentPathStack
60 protected $currentPath;
65 * @var \Drupal\Core\Cache\MemoryBackend
70 * The inbound path processor.
72 * @var \Drupal\Core\PathProcessor\InboundPathProcessorInterface
74 protected $pathProcessor;
77 * The cache tags invalidator.
79 * @var \Drupal\Core\Cache\CacheTagsInvalidatorInterface
81 protected $cacheTagsInvalidator;
83 protected function setUp() {
85 $this->fixtures = new RoutingFixtures();
86 $this->state = new State(new KeyValueMemoryFactory());
87 $this->currentPath = new CurrentPathStack(new RequestStack());
88 $this->cache = new MemoryBackend();
89 $this->pathProcessor = \Drupal::service('path_processor_manager');
90 $this->cacheTagsInvalidator = \Drupal::service('cache_tags.invalidator');
96 public function register(ContainerBuilder $container) {
97 parent::register($container);
99 // Read the incoming path alias for these tests.
100 if ($container->hasDefinition('path_processor_alias')) {
101 $definition = $container->getDefinition('path_processor_alias');
102 $definition->addTag('path_processor_inbound');
106 protected function tearDown() {
107 $this->fixtures->dropTables(Database::getConnection());
113 * Confirms that the correct candidate outlines are generated.
115 public function testCandidateOutlines() {
117 $connection = Database::getConnection();
118 $provider = new TestRouteProvider($connection, $this->state, $this->currentPath, $this->cache, $this->pathProcessor, $this->cacheTagsInvalidator, 'test_routes');
120 $parts = ['node', '5', 'edit'];
122 $candidates = $provider->getCandidateOutlines($parts);
124 $candidates = array_flip($candidates);
126 $this->assertTrue(count($candidates) == 7, 'Correct number of candidates found');
127 $this->assertTrue(array_key_exists('/node/5/edit', $candidates), 'First candidate found.');
128 $this->assertTrue(array_key_exists('/node/5/%', $candidates), 'Second candidate found.');
129 $this->assertTrue(array_key_exists('/node/%/edit', $candidates), 'Third candidate found.');
130 $this->assertTrue(array_key_exists('/node/%/%', $candidates), 'Fourth candidate found.');
131 $this->assertTrue(array_key_exists('/node/5', $candidates), 'Fifth candidate found.');
132 $this->assertTrue(array_key_exists('/node/%', $candidates), 'Sixth candidate found.');
133 $this->assertTrue(array_key_exists('/node', $candidates), 'Seventh candidate found.');
137 * Don't fail when given an empty path.
139 public function testEmptyPathCandidatesOutlines() {
140 $provider = new TestRouteProvider(Database::getConnection(), $this->state, $this->currentPath, $this->cache, $this->pathProcessor, $this->cacheTagsInvalidator, 'test_routes');
141 $candidates = $provider->getCandidateOutlines([]);
142 $this->assertEqual(count($candidates), 0, 'Empty parts should return no candidates.');
146 * Confirms that we can find routes with the exact incoming path.
148 public function testExactPathMatch() {
149 $connection = Database::getConnection();
150 $provider = new RouteProvider($connection, $this->state, $this->currentPath, $this->cache, $this->pathProcessor, $this->cacheTagsInvalidator, 'test_routes');
152 $this->fixtures->createTables($connection);
154 $dumper = new MatcherDumper($connection, $this->state, 'test_routes');
155 $dumper->addRoutes($this->fixtures->sampleRouteCollection());
160 $request = Request::create($path, 'GET');
162 $routes = $provider->getRouteCollectionForRequest($request);
164 foreach ($routes as $route) {
165 $this->assertEqual($route->getPath(), $path, 'Found path has correct pattern');
170 * Confirms that we can find routes whose pattern would match the request.
172 public function testOutlinePathMatch() {
173 $connection = Database::getConnection();
174 $provider = new RouteProvider($connection, $this->state, $this->currentPath, $this->cache, $this->pathProcessor, $this->cacheTagsInvalidator, 'test_routes');
176 $this->fixtures->createTables($connection);
178 $dumper = new MatcherDumper($connection, $this->state, 'test_routes');
179 $dumper->addRoutes($this->fixtures->complexRouteCollection());
182 $path = '/path/1/one';
184 $request = Request::create($path, 'GET');
186 $routes = $provider->getRouteCollectionForRequest($request);
188 // All of the matching paths have the correct pattern.
189 foreach ($routes as $route) {
190 $this->assertEqual($route->compile()->getPatternOutline(), '/path/%/one', 'Found path has correct pattern');
193 $this->assertEqual(count($routes), 2, 'The correct number of routes was found.');
194 $this->assertNotNull($routes->get('route_a'), 'The first matching route was found.');
195 $this->assertNotNull($routes->get('route_b'), 'The second matching route was not found.');
199 * Data provider for testMixedCasePaths()
201 public function providerMixedCaseRoutePaths() {
203 ['/path/one', 'route_a'],
205 ['/PATH/one', 'route_a'],
206 ['/path/2/one', 'route_b', 'PUT'],
207 ['/paTH/3/one', 'route_b', 'PUT'],
208 // There should be no lower case of a Hebrew letter.
209 ['/somewhere/4/over/the/קainbow', 'route_c'],
210 ['/Somewhere/5/over/the/קainboW', 'route_c'],
211 ['/another/llama/aboUT/22', 'route_d'],
212 ['/another/llama/about/22', 'route_d'],
213 ['/place/meΦω', 'route_e', 'HEAD'],
214 ['/place/meφΩ', 'route_e', 'HEAD'],
219 * Confirms that we find routes using a case-insensitive path match.
221 * @dataProvider providerMixedCaseRoutePaths
223 public function testMixedCasePaths($path, $expected_route_name, $method = 'GET') {
224 // The case-insensitive behavior for higher UTF-8 characters depends on
225 // mb_strtolower() using mb_strtolower()
226 // but kernel tests do not currently run the check that enables it.
227 // @todo remove this when https://www.drupal.org/node/2849669 is fixed.
230 $connection = Database::getConnection();
231 $provider = new RouteProvider($connection, $this->state, $this->currentPath, $this->cache, $this->pathProcessor, $this->cacheTagsInvalidator, 'test_routes');
233 $this->fixtures->createTables($connection);
235 $dumper = new MatcherDumper($connection, $this->state, 'test_routes');
236 $dumper->addRoutes($this->fixtures->mixedCaseRouteCollection());
239 $request = Request::create($path, $method);
241 $routes = $provider->getRouteCollectionForRequest($request);
243 if ($expected_route_name) {
244 $this->assertEquals(1, count($routes), 'The correct number of routes was found.');
245 $this->assertNotNull($routes->get($expected_route_name), 'The first matching route was found.');
248 $this->assertEquals(0, count($routes), 'No routes matched.');
253 * Data provider for testMixedCasePaths()
255 public function providerDuplicateRoutePaths() {
256 // When matching routes with the same fit the route with the lowest-sorting
257 // name should end up first in the resulting route collection.
259 ['/path/one', 3, 'route_a'],
260 ['/PATH/one', 3, 'route_a'],
261 ['/path/two', 1, 'route_d'],
263 ['/place/meΦω', 2, 'route_e'],
264 ['/placE/meφΩ', 2, 'route_e'],
269 * Confirms that we find all routes with the same path.
271 * @dataProvider providerDuplicateRoutePaths
273 public function testDuplicateRoutePaths($path, $number, $expected_route_name = NULL) {
275 // The case-insensitive behavior for higher UTF-8 characters depends on
276 // mb_strtolower() using mb_strtolower()
277 // but kernel tests do not currently run the check that enables it.
278 // @todo remove this when https://www.drupal.org/node/2849669 is fixed.
281 $connection = Database::getConnection();
282 $provider = new RouteProvider($connection, $this->state, $this->currentPath, $this->cache, $this->pathProcessor, $this->cacheTagsInvalidator, 'test_routes');
284 $this->fixtures->createTables($connection);
286 $dumper = new MatcherDumper($connection, $this->state, 'test_routes');
287 $dumper->addRoutes($this->fixtures->duplicatePathsRouteCollection());
290 $request = Request::create($path);
291 $routes = $provider->getRouteCollectionForRequest($request);
292 $this->assertEquals($number, count($routes), 'The correct number of routes was found.');
293 if ($expected_route_name) {
294 $route_name = key(current($routes));
295 $this->assertEquals($expected_route_name, $route_name, 'The expected route name was found.');
300 * Confirms that a trailing slash on the request does not result in a 404.
302 public function testOutlinePathMatchTrailingSlash() {
303 $connection = Database::getConnection();
304 $provider = new RouteProvider($connection, $this->state, $this->currentPath, $this->cache, $this->pathProcessor, $this->cacheTagsInvalidator, 'test_routes');
306 $this->fixtures->createTables($connection);
308 $dumper = new MatcherDumper($connection, $this->state, 'test_routes');
309 $dumper->addRoutes($this->fixtures->complexRouteCollection());
312 $path = '/path/1/one/';
314 $request = Request::create($path, 'GET');
316 $routes = $provider->getRouteCollectionForRequest($request);
318 // All of the matching paths have the correct pattern.
319 foreach ($routes as $route) {
320 $this->assertEqual($route->compile()->getPatternOutline(), '/path/%/one', 'Found path has correct pattern');
323 $this->assertEqual(count($routes), 2, 'The correct number of routes was found.');
324 $this->assertNotNull($routes->get('route_a'), 'The first matching route was found.');
325 $this->assertNotNull($routes->get('route_b'), 'The second matching route was not found.');
329 * Confirms that we can find routes whose pattern would match the request.
331 public function testOutlinePathMatchDefaults() {
332 $connection = Database::getConnection();
333 $provider = new RouteProvider($connection, $this->state, $this->currentPath, $this->cache, $this->pathProcessor, $this->cacheTagsInvalidator, 'test_routes');
335 $this->fixtures->createTables($connection);
337 $collection = new RouteCollection();
338 $collection->add('poink', new Route('/some/path/{value}', [
342 $dumper = new MatcherDumper($connection, $this->state, 'test_routes');
343 $dumper->addRoutes($collection);
346 $path = '/some/path';
348 $request = Request::create($path, 'GET');
351 $routes = $provider->getRouteCollectionForRequest($request);
353 // All of the matching paths have the correct pattern.
354 foreach ($routes as $route) {
355 $this->assertEqual($route->compile()->getPatternOutline(), '/some/path', 'Found path has correct pattern');
358 $this->assertEqual(count($routes), 1, 'The correct number of routes was found.');
359 $this->assertNotNull($routes->get('poink'), 'The first matching route was found.');
361 catch (ResourceNotFoundException $e) {
362 $this->fail('No matching route found with default argument value.');
367 * Confirms that we can find routes whose pattern would match the request.
369 public function testOutlinePathMatchDefaultsCollision() {
370 $connection = Database::getConnection();
371 $provider = new RouteProvider($connection, $this->state, $this->currentPath, $this->cache, $this->pathProcessor, $this->cacheTagsInvalidator, 'test_routes');
373 $this->fixtures->createTables($connection);
375 $collection = new RouteCollection();
376 $collection->add('poink', new Route('/some/path/{value}', [
379 $collection->add('narf', new Route('/some/path/here'));
381 $dumper = new MatcherDumper($connection, $this->state, 'test_routes');
382 $dumper->addRoutes($collection);
385 $path = '/some/path';
387 $request = Request::create($path, 'GET');
390 $routes = $provider->getRouteCollectionForRequest($request);
392 // All of the matching paths have the correct pattern.
393 foreach ($routes as $route) {
394 $this->assertEqual($route->compile()->getPatternOutline(), '/some/path', 'Found path has correct pattern');
397 $this->assertEqual(count($routes), 1, 'The correct number of routes was found.');
398 $this->assertNotNull($routes->get('poink'), 'The first matching route was found.');
400 catch (ResourceNotFoundException $e) {
401 $this->fail('No matching route found with default argument value.');
406 * Confirms that we can find routes whose pattern would match the request.
408 public function testOutlinePathMatchDefaultsCollision2() {
409 $connection = Database::getConnection();
410 $provider = new RouteProvider($connection, $this->state, $this->currentPath, $this->cache, $this->pathProcessor, $this->cacheTagsInvalidator, 'test_routes');
412 $this->fixtures->createTables($connection);
414 $collection = new RouteCollection();
415 $collection->add('poink', new Route('/some/path/{value}', [
418 $collection->add('narf', new Route('/some/path/here'));
419 $collection->add('eep', new Route('/something/completely/different'));
421 $dumper = new MatcherDumper($connection, $this->state, 'test_routes');
422 $dumper->addRoutes($collection);
425 $path = '/some/path/here';
427 $request = Request::create($path, 'GET');
430 $routes = $provider->getRouteCollectionForRequest($request);
431 $routes_array = $routes->all();
433 $this->assertEqual(count($routes), 2, 'The correct number of routes was found.');
434 $this->assertEqual(['narf', 'poink'], array_keys($routes_array), 'Ensure the fitness was taken into account.');
435 $this->assertNotNull($routes->get('narf'), 'The first matching route was found.');
436 $this->assertNotNull($routes->get('poink'), 'The second matching route was found.');
437 $this->assertNull($routes->get('eep'), 'Non-matching route was not found.');
439 catch (ResourceNotFoundException $e) {
440 $this->fail('No matching route found with default argument value.');
445 * Confirms that we can find multiple routes that match the request equally.
447 public function testOutlinePathMatchDefaultsCollision3() {
448 $connection = Database::getConnection();
449 $provider = new RouteProvider($connection, $this->state, $this->currentPath, $this->cache, $this->pathProcessor, $this->cacheTagsInvalidator, 'test_routes');
451 $this->fixtures->createTables($connection);
453 $collection = new RouteCollection();
454 $collection->add('poink', new Route('/some/{value}/path'));
455 // Add a second route matching the same path pattern.
456 $collection->add('poink2', new Route('/some/{object}/path'));
457 $collection->add('narf', new Route('/some/here/path'));
458 $collection->add('eep', new Route('/something/completely/different'));
460 $dumper = new MatcherDumper($connection, $this->state, 'test_routes');
461 $dumper->addRoutes($collection);
464 $path = '/some/over-there/path';
466 $request = Request::create($path, 'GET');
469 $routes = $provider->getRouteCollectionForRequest($request);
470 $routes_array = $routes->all();
472 $this->assertEqual(count($routes), 2, 'The correct number of routes was found.');
473 $this->assertEqual(['poink', 'poink2'], array_keys($routes_array), 'Ensure the fitness and name were taken into account in the sort.');
474 $this->assertNotNull($routes->get('poink'), 'The first matching route was found.');
475 $this->assertNotNull($routes->get('poink2'), 'The second matching route was found.');
476 $this->assertNull($routes->get('eep'), 'Non-matching route was not found.');
478 catch (ResourceNotFoundException $e) {
479 $this->fail('No matching route found with default argument value.');
484 * Tests a route with a 0 as value.
486 public function testOutlinePathMatchZero() {
487 $connection = Database::getConnection();
488 $provider = new RouteProvider($connection, $this->state, $this->currentPath, $this->cache, $this->pathProcessor, $this->cacheTagsInvalidator, 'test_routes');
490 $this->fixtures->createTables($connection);
492 $collection = new RouteCollection();
493 $collection->add('poink', new Route('/some/path/{value}'));
495 $dumper = new MatcherDumper($connection, $this->state, 'test_routes');
496 $dumper->addRoutes($collection);
499 $path = '/some/path/0';
501 $request = Request::create($path, 'GET');
504 $routes = $provider->getRouteCollectionForRequest($request);
506 // All of the matching paths have the correct pattern.
507 foreach ($routes as $route) {
508 $this->assertEqual($route->compile()->getPatternOutline(), '/some/path/%', 'Found path has correct pattern');
511 $this->assertEqual(count($routes), 1, 'The correct number of routes was found.');
513 catch (ResourceNotFoundException $e) {
514 $this->fail('No matchout route found with 0 as argument value');
519 * Confirms that an exception is thrown when no matching path is found.
521 public function testOutlinePathNoMatch() {
522 $connection = Database::getConnection();
523 $provider = new RouteProvider($connection, $this->state, $this->currentPath, $this->cache, $this->pathProcessor, $this->cacheTagsInvalidator, 'test_routes');
525 $this->fixtures->createTables($connection);
527 $dumper = new MatcherDumper($connection, $this->state, 'test_routes');
528 $dumper->addRoutes($this->fixtures->complexRouteCollection());
531 $path = '/no/such/path';
533 $request = Request::create($path, 'GET');
535 $routes = $provider->getRoutesByPattern($path);
536 $this->assertFalse(count($routes), 'No path found with this pattern.');
538 $collection = $provider->getRouteCollectionForRequest($request);
539 $this->assertTrue(count($collection) == 0, 'Empty route collection found with this pattern.');
543 * Tests that route caching works.
545 public function testRouteCaching() {
546 $connection = Database::getConnection();
547 $language_manager = \Drupal::languageManager();
548 $provider = new RouteProvider($connection, $this->state, $this->currentPath, $this->cache, $this->pathProcessor, $this->cacheTagsInvalidator, 'test_routes', $language_manager);
550 $this->fixtures->createTables($connection);
552 $dumper = new MatcherDumper($connection, $this->state, 'test_routes');
553 $dumper->addRoutes($this->fixtures->sampleRouteCollection());
554 $dumper->addRoutes($this->fixtures->complexRouteCollection());
558 $path = '/path/add/one';
559 $request = Request::create($path, 'GET');
560 $provider->getRouteCollectionForRequest($request);
562 $cache = $this->cache->get('route:en:/path/add/one:');
563 $this->assertEqual('/path/add/one', $cache->data['path']);
564 $this->assertEqual([], $cache->data['query']);
565 $this->assertEqual(3, count($cache->data['routes']));
567 // A path with query parameters.
568 $path = '/path/add/one?foo=bar';
569 $request = Request::create($path, 'GET');
570 $provider->getRouteCollectionForRequest($request);
572 $cache = $this->cache->get('route:en:/path/add/one:foo=bar');
573 $this->assertEqual('/path/add/one', $cache->data['path']);
574 $this->assertEqual(['foo' => 'bar'], $cache->data['query']);
575 $this->assertEqual(3, count($cache->data['routes']));
577 // A path with placeholders.
578 $path = '/path/1/one';
579 $request = Request::create($path, 'GET');
580 $provider->getRouteCollectionForRequest($request);
582 $cache = $this->cache->get('route:en:/path/1/one:');
583 $this->assertEqual('/path/1/one', $cache->data['path']);
584 $this->assertEqual([], $cache->data['query']);
585 $this->assertEqual(2, count($cache->data['routes']));
587 // A path with a path alias.
588 /** @var \Drupal\Core\Path\AliasStorageInterface $path_storage */
589 $path_storage = \Drupal::service('path.alias_storage');
590 $path_storage->save('/path/add/one', '/path/add-one');
591 /** @var \Drupal\Core\Path\AliasManagerInterface $alias_manager */
592 $alias_manager = \Drupal::service('path.alias_manager');
593 $alias_manager->cacheClear();
595 $path = '/path/add-one';
596 $request = Request::create($path, 'GET');
597 $provider->getRouteCollectionForRequest($request);
599 $cache = $this->cache->get('route:en:/path/add-one:');
600 $this->assertEqual('/path/add/one', $cache->data['path']);
601 $this->assertEqual([], $cache->data['query']);
602 $this->assertEqual(3, count($cache->data['routes']));
604 // Test with a different current language by switching out the default
606 $swiss = ConfigurableLanguage::createFromLangcode('gsw-berne');
607 $language_manager->reset();
608 \Drupal::service('language.default')->set($swiss);
610 $path = '/path/add-one';
611 $request = Request::create($path, 'GET');
612 $provider->getRouteCollectionForRequest($request);
614 $cache = $this->cache->get('route:gsw-berne:/path/add-one:');
615 $this->assertEquals('/path/add/one', $cache->data['path']);
616 $this->assertEquals([], $cache->data['query']);
617 $this->assertEquals(3, count($cache->data['routes']));
621 * Test RouteProvider::getRouteByName() and RouteProvider::getRoutesByNames().
623 public function testRouteByName() {
624 $connection = Database::getConnection();
625 $provider = new RouteProvider($connection, $this->state, $this->currentPath, $this->cache, $this->pathProcessor, $this->cacheTagsInvalidator, 'test_routes');
627 $this->fixtures->createTables($connection);
629 $dumper = new MatcherDumper($connection, $this->state, 'test_routes');
630 $dumper->addRoutes($this->fixtures->sampleRouteCollection());
633 $route = $provider->getRouteByName('route_a');
634 $this->assertEqual($route->getPath(), '/path/one', 'The right route pattern was found.');
635 $this->assertEqual($route->getMethods(), ['GET'], 'The right route method was found.');
636 $route = $provider->getRouteByName('route_b');
637 $this->assertEqual($route->getPath(), '/path/one', 'The right route pattern was found.');
638 $this->assertEqual($route->getMethods(), ['PUT'], 'The right route method was found.');
640 $exception_thrown = FALSE;
642 $provider->getRouteByName('invalid_name');
644 catch (RouteNotFoundException $e) {
645 $exception_thrown = TRUE;
647 $this->assertTrue($exception_thrown, 'Random route was not found.');
649 $routes = $provider->getRoutesByNames(['route_c', 'route_d', $this->randomMachineName()]);
650 $this->assertEqual(count($routes), 2, 'Only two valid routes found.');
651 $this->assertEqual($routes['route_c']->getPath(), '/path/two');
652 $this->assertEqual($routes['route_d']->getPath(), '/path/three');
656 * Ensures that the routing system is capable of extreme long patterns.
658 public function testGetRoutesByPatternWithLongPatterns() {
659 $connection = Database::getConnection();
660 $provider = new TestRouteProvider($connection, $this->state, $this->currentPath, $this->cache, $this->pathProcessor, $this->cacheTagsInvalidator, 'test_routes');
662 $this->fixtures->createTables($connection);
663 // This pattern has only 3 parts, so we will get candidates, but no routes,
664 // even though we have not dumped the routes yet.
665 $shortest = '/test/1/test2';
666 $result = $provider->getRoutesByPattern($shortest);
667 $this->assertEqual($result->count(), 0);
668 $candidates = $provider->getCandidateOutlines(explode('/', trim($shortest, '/')));
669 $this->assertEqual(count($candidates), 7);
670 // A longer patten is not found and returns no candidates
671 $path_to_test = '/test/1/test2/2/test3/3/4/5/6/test4';
672 $result = $provider->getRoutesByPattern($path_to_test);
673 $this->assertEqual($result->count(), 0);
674 $candidates = $provider->getCandidateOutlines(explode('/', trim($path_to_test, '/')));
675 $this->assertEqual(count($candidates), 0);
677 // Add a matching route and dump it.
678 $dumper = new MatcherDumper($connection, $this->state, 'test_routes');
679 $collection = new RouteCollection();
680 $collection->add('long_pattern', new Route('/test/{v1}/test2/{v2}/test3/{v3}/{v4}/{v5}/{v6}/test4'));
681 $dumper->addRoutes($collection);
684 $result = $provider->getRoutesByPattern($path_to_test);
685 $this->assertEqual($result->count(), 1);
686 // We can't compare the values of the routes directly, nor use
687 // spl_object_hash() because they are separate instances.
688 $this->assertEqual(serialize($result->get('long_pattern')), serialize($collection->get('long_pattern')), 'The right route was found.');
689 // We now have a single candidate outline.
690 $candidates = $provider->getCandidateOutlines(explode('/', trim($path_to_test, '/')));
691 $this->assertEqual(count($candidates), 1);
692 // Longer and shorter patterns are not found. Both are longer than 3, so
693 // we should not have any candidates either. The fact that we do not
694 // get any candidates for a longer path is a security feature.
695 $longer = '/test/1/test2/2/test3/3/4/5/6/test4/trailing/more/parts';
696 $result = $provider->getRoutesByPattern($longer);
697 $this->assertEqual($result->count(), 0);
698 $candidates = $provider->getCandidateOutlines(explode('/', trim($longer, '/')));
699 $this->assertEqual(count($candidates), 1);
700 $shorter = '/test/1/test2/2/test3';
701 $result = $provider->getRoutesByPattern($shorter);
702 $this->assertEqual($result->count(), 0);
703 $candidates = $provider->getCandidateOutlines(explode('/', trim($shorter, '/')));
704 $this->assertEqual(count($candidates), 0);
705 // This pattern has only 3 parts, so we will get candidates, but no routes.
706 // This result is unchanged by running the dumper.
707 $result = $provider->getRoutesByPattern($shortest);
708 $this->assertEqual($result->count(), 0);
709 $candidates = $provider->getCandidateOutlines(explode('/', trim($shortest, '/')));
710 $this->assertEqual(count($candidates), 7);
714 * Tests getRoutesPaged().
716 public function testGetRoutesPaged() {
717 $connection = Database::getConnection();
718 $provider = new RouteProvider($connection, $this->state, $this->currentPath, $this->cache, $this->pathProcessor, $this->cacheTagsInvalidator, 'test_routes');
720 $this->fixtures->createTables($connection);
721 $dumper = new MatcherDumper($connection, $this->state, 'test_routes');
722 $dumper->addRoutes($this->fixtures->sampleRouteCollection());
725 $fixture_routes = $this->fixtures->staticSampleRouteCollection();
727 // Query all the routes.
728 $routes = $provider->getRoutesPaged(0);
729 $this->assertEqual(array_keys($routes), array_keys($fixture_routes));
732 $routes = $provider->getRoutesPaged(0, 0);
733 $this->assertEqual(array_keys($routes), []);
735 // Query a limited sets of routes.
736 $routes = $provider->getRoutesPaged(1, 2);
737 $this->assertEqual(array_keys($routes), array_slice(array_keys($fixture_routes), 1, 2));
742 class TestRouteProvider extends RouteProvider {
744 public function getCandidateOutlines(array $parts) {
745 return parent::getCandidateOutlines($parts);