RouteProviderTest.php 27.7 KB
Newer Older
1 2 3 4
<?php

/**
 * @file
5
 * Contains \Drupal\KernelTests\Core\Routing\RouteProviderTest.
6 7
 */

8
namespace Drupal\KernelTests\Core\Routing;
9

10 11 12
use Drupal\Core\Cache\MemoryBackend;
use Drupal\Core\Database\Database;
use Drupal\Core\DependencyInjection\ContainerBuilder;
13
use Drupal\Core\KeyValueStore\KeyValueMemoryFactory;
14
use Drupal\Core\Path\CurrentPathStack;
15 16
use Drupal\Core\Routing\MatcherDumper;
use Drupal\Core\Routing\RouteProvider;
17
use Drupal\Core\State\State;
18
use Drupal\KernelTests\KernelTestBase;
19
use Drupal\language\Entity\ConfigurableLanguage;
20
use Drupal\Tests\Core\Routing\RoutingFixtures;
21
use Symfony\Component\HttpFoundation\Request;
22
use Symfony\Component\HttpFoundation\RequestStack;
23
use Symfony\Component\Routing\Exception\ResourceNotFoundException;
24
use Symfony\Component\Routing\Exception\RouteNotFoundException;
25 26 27 28
use Symfony\Component\Routing\Route;
use Symfony\Component\Routing\RouteCollection;

/**
29 30 31
 * Confirm that the default route provider is working correctly.
 *
 * @group Routing
32
 */
33
class RouteProviderTest extends KernelTestBase {
34

35 36 37
  /**
   * Modules to enable.
   */
38
  public static $modules = ['url_alter_test', 'system', 'language'];
39

40 41 42
  /**
   * A collection of shared fixture data for tests.
   *
43
   * @var \Drupal\Tests\Core\Routing\RoutingFixtures
44 45 46
   */
  protected $fixtures;

47 48 49
  /**
   * The state.
   *
50
   * @var \Drupal\Core\State\StateInterface
51 52 53
   */
  protected $state;

54 55 56 57 58 59 60
  /**
   * The current path.
   *
   * @var \Drupal\Core\Path\CurrentPathStack
   */
  protected $currentPath;

61 62 63 64 65 66 67 68 69 70 71 72 73 74
  /**
   * The cache backend.
   *
   * @var \Drupal\Core\Cache\MemoryBackend
   */
  protected $cache;

  /**
   * The inbound path processor.
   *
   * @var \Drupal\Core\PathProcessor\InboundPathProcessorInterface
   */
  protected $pathProcessor;

75 76 77 78 79 80 81
  /**
   * The cache tags invalidator.
   *
   * @var \Drupal\Core\Cache\CacheTagsInvalidatorInterface
   */
  protected $cacheTagsInvalidator;

82
  protected function setUp() {
83
    parent::setUp();
84
    $this->fixtures = new RoutingFixtures();
85
    $this->state = new State(new KeyValueMemoryFactory());
86
    $this->currentPath = new CurrentPathStack(new RequestStack());
87
    $this->cache = new MemoryBackend();
88
    $this->pathProcessor = \Drupal::service('path_processor_manager');
89
    $this->cacheTagsInvalidator = \Drupal::service('cache_tags.invalidator');
90 91 92 93 94
  }

  /**
   * {@inheritdoc}
   */
95 96
  public function register(ContainerBuilder $container) {
    parent::register($container);
97

98
    // Read the incoming path alias for these tests.
99 100 101 102
    if ($container->hasDefinition('path_processor_alias')) {
      $definition = $container->getDefinition('path_processor_alias');
      $definition->addTag('path_processor_inbound');
    }
103 104
  }

105
  protected function tearDown() {
106 107 108 109 110 111 112 113 114 115 116
    $this->fixtures->dropTables(Database::getConnection());

    parent::tearDown();
  }

  /**
   * Confirms that the correct candidate outlines are generated.
   */
  public function testCandidateOutlines() {

    $connection = Database::getConnection();
117
    $provider = new TestRouteProvider($connection, $this->state, $this->currentPath, $this->cache, $this->pathProcessor, $this->cacheTagsInvalidator, 'test_routes');
118

119
    $parts = ['node', '5', 'edit'];
120 121 122 123 124

    $candidates = $provider->getCandidateOutlines($parts);

    $candidates = array_flip($candidates);

125
    $this->assertTrue(count($candidates) == 7, 'Correct number of candidates found');
126 127 128 129
    $this->assertTrue(array_key_exists('/node/5/edit', $candidates), 'First candidate found.');
    $this->assertTrue(array_key_exists('/node/5/%', $candidates), 'Second candidate found.');
    $this->assertTrue(array_key_exists('/node/%/edit', $candidates), 'Third candidate found.');
    $this->assertTrue(array_key_exists('/node/%/%', $candidates), 'Fourth candidate found.');
130 131 132
    $this->assertTrue(array_key_exists('/node/5', $candidates), 'Fifth candidate found.');
    $this->assertTrue(array_key_exists('/node/%', $candidates), 'Sixth candidate found.');
    $this->assertTrue(array_key_exists('/node', $candidates), 'Seventh candidate found.');
133 134
  }

135 136 137 138 139 140 141 142 143
  /**
   * Don't fail when given an empty path.
   */
  public function testEmptyPathCandidatesOutlines() {
    $provider = new TestRouteProvider(Database::getConnection(), $this->state, $this->currentPath, $this->cache, $this->pathProcessor, $this->cacheTagsInvalidator, 'test_routes');
    $candidates = $provider->getCandidateOutlines([]);
    $this->assertEqual(count($candidates), 0, 'Empty parts should return no candidates.');
  }

144 145 146
  /**
   * Confirms that we can find routes with the exact incoming path.
   */
147
  public function testExactPathMatch() {
148
    $connection = Database::getConnection();
149
    $provider = new RouteProvider($connection, $this->state, $this->currentPath, $this->cache, $this->pathProcessor, $this->cacheTagsInvalidator, 'test_routes');
150 151 152

    $this->fixtures->createTables($connection);

153
    $dumper = new MatcherDumper($connection, $this->state, 'test_routes');
154 155 156 157 158 159 160 161 162 163
    $dumper->addRoutes($this->fixtures->sampleRouteCollection());
    $dumper->dump();

    $path = '/path/one';

    $request = Request::create($path, 'GET');

    $routes = $provider->getRouteCollectionForRequest($request);

    foreach ($routes as $route) {
164
      $this->assertEqual($route->getPath(), $path, 'Found path has correct pattern');
165 166 167 168 169 170
    }
  }

  /**
   * Confirms that we can find routes whose pattern would match the request.
   */
171
  public function testOutlinePathMatch() {
172
    $connection = Database::getConnection();
173
    $provider = new RouteProvider($connection, $this->state, $this->currentPath, $this->cache, $this->pathProcessor, $this->cacheTagsInvalidator, 'test_routes');
174 175 176

    $this->fixtures->createTables($connection);

177
    $dumper = new MatcherDumper($connection, $this->state, 'test_routes');
178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197
    $dumper->addRoutes($this->fixtures->complexRouteCollection());
    $dumper->dump();

    $path = '/path/1/one';

    $request = Request::create($path, 'GET');

    $routes = $provider->getRouteCollectionForRequest($request);

    // All of the matching paths have the correct pattern.
    foreach ($routes as $route) {
      $this->assertEqual($route->compile()->getPatternOutline(), '/path/%/one', 'Found path has correct pattern');
    }

    $this->assertEqual(count($routes), 2, 'The correct number of routes was found.');
    $this->assertNotNull($routes->get('route_a'), 'The first matching route was found.');
    $this->assertNotNull($routes->get('route_b'), 'The second matching route was not found.');
  }

  /**
198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286
   * Data provider for testMixedCasePaths()
   */
  public function providerMixedCaseRoutePaths() {
    return [
      ['/path/one', 'route_a'],
      ['/path/two', NULL],
      ['/PATH/one', 'route_a'],
      ['/path/2/one', 'route_b', 'PUT'],
      ['/paTH/3/one', 'route_b', 'PUT'],
      // There should be no lower case of a Hebrew letter.
      ['/somewhere/4/over/the/קainbow', 'route_c'],
      ['/Somewhere/5/over/the/קainboW', 'route_c'],
      ['/another/llama/aboUT/22', 'route_d'],
      ['/another/llama/about/22', 'route_d'],
      ['/place/meΦω', 'route_e', 'HEAD'],
      ['/place/meφΩ', 'route_e', 'HEAD'],
    ];
  }

  /**
   * Confirms that we find routes using a case-insensitive path match.
   *
   * @dataProvider providerMixedCaseRoutePaths
   */
  public function testMixedCasePaths($path, $expected_route_name, $method = 'GET') {
    $connection = Database::getConnection();
    $provider = new RouteProvider($connection, $this->state, $this->currentPath, $this->cache, $this->pathProcessor, $this->cacheTagsInvalidator, 'test_routes');

    $this->fixtures->createTables($connection);

    $dumper = new MatcherDumper($connection, $this->state, 'test_routes');
    $dumper->addRoutes($this->fixtures->mixedCaseRouteCollection());
    $dumper->dump();

    $request = Request::create($path, $method);

    $routes = $provider->getRouteCollectionForRequest($request);

    if ($expected_route_name) {
      $this->assertEquals(1, count($routes), 'The correct number of routes was found.');
      $this->assertNotNull($routes->get($expected_route_name), 'The first matching route was found.');
    }
    else {
      $this->assertEquals(0, count($routes), 'No routes matched.');
    }
  }

  /**
   * Data provider for testMixedCasePaths()
   */
  public function providerDuplicateRoutePaths() {
    // When matching routes with the same fit the route with the lowest-sorting
    // name should end up first in the resulting route collection.
    return [
      ['/path/one', 3, 'route_a'],
      ['/PATH/one', 3, 'route_a'],
      ['/path/two', 1, 'route_d'],
      ['/PATH/three', 0],
      ['/place/meΦω', 2, 'route_e'],
      ['/placE/meφΩ', 2, 'route_e'],
    ];
  }

  /**
   * Confirms that we find all routes with the same path.
   *
   * @dataProvider providerDuplicateRoutePaths
   */
  public function testDuplicateRoutePaths($path, $number, $expected_route_name = NULL) {
    $connection = Database::getConnection();
    $provider = new RouteProvider($connection, $this->state, $this->currentPath, $this->cache, $this->pathProcessor, $this->cacheTagsInvalidator, 'test_routes');

    $this->fixtures->createTables($connection);

    $dumper = new MatcherDumper($connection, $this->state, 'test_routes');
    $dumper->addRoutes($this->fixtures->duplicatePathsRouteCollection());
    $dumper->dump();

    $request = Request::create($path);
    $routes = $provider->getRouteCollectionForRequest($request);
    $this->assertEquals($number, count($routes), 'The correct number of routes was found.');
    if ($expected_route_name) {
      $route_name = key(current($routes));
      $this->assertEquals($expected_route_name, $route_name, 'The expected route name was found.');
    }
  }

  /**
   * Confirms that a trailing slash on the request does not result in a 404.
287
   */
288
  public function testOutlinePathMatchTrailingSlash() {
289
    $connection = Database::getConnection();
290
    $provider = new RouteProvider($connection, $this->state, $this->currentPath, $this->cache, $this->pathProcessor, $this->cacheTagsInvalidator, 'test_routes');
291 292 293

    $this->fixtures->createTables($connection);

294
    $dumper = new MatcherDumper($connection, $this->state, 'test_routes');
295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316
    $dumper->addRoutes($this->fixtures->complexRouteCollection());
    $dumper->dump();

    $path = '/path/1/one/';

    $request = Request::create($path, 'GET');

    $routes = $provider->getRouteCollectionForRequest($request);

    // All of the matching paths have the correct pattern.
    foreach ($routes as $route) {
      $this->assertEqual($route->compile()->getPatternOutline(), '/path/%/one', 'Found path has correct pattern');
    }

    $this->assertEqual(count($routes), 2, 'The correct number of routes was found.');
    $this->assertNotNull($routes->get('route_a'), 'The first matching route was found.');
    $this->assertNotNull($routes->get('route_b'), 'The second matching route was not found.');
  }

  /**
   * Confirms that we can find routes whose pattern would match the request.
   */
317
  public function testOutlinePathMatchDefaults() {
318
    $connection = Database::getConnection();
319
    $provider = new RouteProvider($connection, $this->state, $this->currentPath, $this->cache, $this->pathProcessor, $this->cacheTagsInvalidator, 'test_routes');
320 321 322 323

    $this->fixtures->createTables($connection);

    $collection = new RouteCollection();
324
    $collection->add('poink', new Route('/some/path/{value}', [
325
      'value' => 'poink',
326
    ]));
327

328
    $dumper = new MatcherDumper($connection, $this->state, 'test_routes');
329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354
    $dumper->addRoutes($collection);
    $dumper->dump();

    $path = '/some/path';

    $request = Request::create($path, 'GET');

    try {
      $routes = $provider->getRouteCollectionForRequest($request);

      // All of the matching paths have the correct pattern.
      foreach ($routes as $route) {
        $this->assertEqual($route->compile()->getPatternOutline(), '/some/path', 'Found path has correct pattern');
      }

      $this->assertEqual(count($routes), 1, 'The correct number of routes was found.');
      $this->assertNotNull($routes->get('poink'), 'The first matching route was found.');
    }
    catch (ResourceNotFoundException $e) {
      $this->fail('No matching route found with default argument value.');
    }
  }

  /**
   * Confirms that we can find routes whose pattern would match the request.
   */
355
  public function testOutlinePathMatchDefaultsCollision() {
356
    $connection = Database::getConnection();
357
    $provider = new RouteProvider($connection, $this->state, $this->currentPath, $this->cache, $this->pathProcessor, $this->cacheTagsInvalidator, 'test_routes');
358 359 360 361

    $this->fixtures->createTables($connection);

    $collection = new RouteCollection();
362
    $collection->add('poink', new Route('/some/path/{value}', [
363
      'value' => 'poink',
364
    ]));
365 366
    $collection->add('narf', new Route('/some/path/here'));

367
    $dumper = new MatcherDumper($connection, $this->state, 'test_routes');
368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393
    $dumper->addRoutes($collection);
    $dumper->dump();

    $path = '/some/path';

    $request = Request::create($path, 'GET');

    try {
      $routes = $provider->getRouteCollectionForRequest($request);

      // All of the matching paths have the correct pattern.
      foreach ($routes as $route) {
        $this->assertEqual($route->compile()->getPatternOutline(), '/some/path', 'Found path has correct pattern');
      }

      $this->assertEqual(count($routes), 1, 'The correct number of routes was found.');
      $this->assertNotNull($routes->get('poink'), 'The first matching route was found.');
    }
    catch (ResourceNotFoundException $e) {
      $this->fail('No matching route found with default argument value.');
    }
  }

  /**
   * Confirms that we can find routes whose pattern would match the request.
   */
394
  public function testOutlinePathMatchDefaultsCollision2() {
395
    $connection = Database::getConnection();
396
    $provider = new RouteProvider($connection, $this->state, $this->currentPath, $this->cache, $this->pathProcessor, $this->cacheTagsInvalidator, 'test_routes');
397 398 399 400

    $this->fixtures->createTables($connection);

    $collection = new RouteCollection();
401
    $collection->add('poink', new Route('/some/path/{value}', [
402
      'value' => 'poink',
403
    ]));
404
    $collection->add('narf', new Route('/some/path/here'));
405
    $collection->add('eep', new Route('/something/completely/different'));
406

407
    $dumper = new MatcherDumper($connection, $this->state, 'test_routes');
408 409 410 411 412 413 414 415 416
    $dumper->addRoutes($collection);
    $dumper->dump();

    $path = '/some/path/here';

    $request = Request::create($path, 'GET');

    try {
      $routes = $provider->getRouteCollectionForRequest($request);
417
      $routes_array = $routes->all();
418

419
      $this->assertEqual(count($routes), 2, 'The correct number of routes was found.');
420
      $this->assertEqual(['narf', 'poink'], array_keys($routes_array), 'Ensure the fitness was taken into account.');
421
      $this->assertNotNull($routes->get('narf'), 'The first matching route was found.');
422
      $this->assertNotNull($routes->get('poink'), 'The second matching route was found.');
423 424 425 426 427 428 429 430 431 432
      $this->assertNull($routes->get('eep'), 'Non-matching route was not found.');
    }
    catch (ResourceNotFoundException $e) {
      $this->fail('No matching route found with default argument value.');
    }
  }

  /**
   * Confirms that we can find multiple routes that match the request equally.
   */
433
  public function testOutlinePathMatchDefaultsCollision3() {
434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458
    $connection = Database::getConnection();
    $provider = new RouteProvider($connection, $this->state, $this->currentPath, $this->cache, $this->pathProcessor, $this->cacheTagsInvalidator, 'test_routes');

    $this->fixtures->createTables($connection);

    $collection = new RouteCollection();
    $collection->add('poink', new Route('/some/{value}/path'));
    // Add a second route matching the same path pattern.
    $collection->add('poink2', new Route('/some/{object}/path'));
    $collection->add('narf', new Route('/some/here/path'));
    $collection->add('eep', new Route('/something/completely/different'));

    $dumper = new MatcherDumper($connection, $this->state, 'test_routes');
    $dumper->addRoutes($collection);
    $dumper->dump();

    $path = '/some/over-there/path';

    $request = Request::create($path, 'GET');

    try {
      $routes = $provider->getRouteCollectionForRequest($request);
      $routes_array = $routes->all();

      $this->assertEqual(count($routes), 2, 'The correct number of routes was found.');
459
      $this->assertEqual(['poink', 'poink2'], array_keys($routes_array), 'Ensure the fitness and name were taken into account in the sort.');
460 461 462
      $this->assertNotNull($routes->get('poink'), 'The first matching route was found.');
      $this->assertNotNull($routes->get('poink2'), 'The second matching route was found.');
      $this->assertNull($routes->get('eep'), 'Non-matching route was not found.');
463 464 465 466 467 468
    }
    catch (ResourceNotFoundException $e) {
      $this->fail('No matching route found with default argument value.');
    }
  }

469 470 471 472 473
  /**
   * Tests a route with a 0 as value.
   */
  public function testOutlinePathMatchZero() {
    $connection = Database::getConnection();
474
    $provider = new RouteProvider($connection, $this->state, $this->currentPath, $this->cache, $this->pathProcessor, $this->cacheTagsInvalidator, 'test_routes');
475 476 477 478 479 480

    $this->fixtures->createTables($connection);

    $collection = new RouteCollection();
    $collection->add('poink', new Route('/some/path/{value}'));

481
    $dumper = new MatcherDumper($connection, $this->state, 'test_routes');
482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503
    $dumper->addRoutes($collection);
    $dumper->dump();

    $path = '/some/path/0';

    $request = Request::create($path, 'GET');

    try {
      $routes = $provider->getRouteCollectionForRequest($request);

      // All of the matching paths have the correct pattern.
      foreach ($routes as $route) {
        $this->assertEqual($route->compile()->getPatternOutline(), '/some/path/%', 'Found path has correct pattern');
      }

      $this->assertEqual(count($routes), 1, 'The correct number of routes was found.');
    }
    catch (ResourceNotFoundException $e) {
      $this->fail('No matchout route found with 0 as argument value');
    }
  }

504 505 506
  /**
   * Confirms that an exception is thrown when no matching path is found.
   */
507
  public function testOutlinePathNoMatch() {
508
    $connection = Database::getConnection();
509
    $provider = new RouteProvider($connection, $this->state, $this->currentPath, $this->cache, $this->pathProcessor, $this->cacheTagsInvalidator, 'test_routes');
510 511 512

    $this->fixtures->createTables($connection);

513
    $dumper = new MatcherDumper($connection, $this->state, 'test_routes');
514 515 516 517 518 519 520
    $dumper->addRoutes($this->fixtures->complexRouteCollection());
    $dumper->dump();

    $path = '/no/such/path';

    $request = Request::create($path, 'GET');

521 522 523 524 525
    $routes = $provider->getRoutesByPattern($path);
    $this->assertFalse(count($routes), 'No path found with this pattern.');

    $collection = $provider->getRouteCollectionForRequest($request);
    $this->assertTrue(count($collection) == 0, 'Empty route collection found with this pattern.');
526 527 528
  }

  /**
529
   * Tests that route caching works.
530
   */
531
  public function testRouteCaching() {
532
    $connection = Database::getConnection();
533 534
    $language_manager = \Drupal::languageManager();
    $provider = new RouteProvider($connection, $this->state, $this->currentPath, $this->cache, $this->pathProcessor, $this->cacheTagsInvalidator, 'test_routes', $language_manager);
535 536 537

    $this->fixtures->createTables($connection);

538
    $dumper = new MatcherDumper($connection, $this->state, 'test_routes');
539
    $dumper->addRoutes($this->fixtures->sampleRouteCollection());
540
    $dumper->addRoutes($this->fixtures->complexRouteCollection());
541 542
    $dumper->dump();

543 544 545 546
    // A simple path.
    $path = '/path/add/one';
    $request = Request::create($path, 'GET');
    $provider->getRouteCollectionForRequest($request);
547

548
    $cache = $this->cache->get('route:en:/path/add/one:');
549 550 551
    $this->assertEqual('/path/add/one', $cache->data['path']);
    $this->assertEqual([], $cache->data['query']);
    $this->assertEqual(3, count($cache->data['routes']));
552

553 554 555 556 557
    // A path with query parameters.
    $path = '/path/add/one?foo=bar';
    $request = Request::create($path, 'GET');
    $provider->getRouteCollectionForRequest($request);

558
    $cache = $this->cache->get('route:en:/path/add/one:foo=bar');
559 560 561 562 563 564 565 566 567
    $this->assertEqual('/path/add/one', $cache->data['path']);
    $this->assertEqual(['foo' => 'bar'], $cache->data['query']);
    $this->assertEqual(3, count($cache->data['routes']));

    // A path with placeholders.
    $path = '/path/1/one';
    $request = Request::create($path, 'GET');
    $provider->getRouteCollectionForRequest($request);

568
    $cache = $this->cache->get('route:en:/path/1/one:');
569 570 571 572 573 574 575
    $this->assertEqual('/path/1/one', $cache->data['path']);
    $this->assertEqual([], $cache->data['query']);
    $this->assertEqual(2, count($cache->data['routes']));

    // A path with a path alias.
    /** @var \Drupal\Core\Path\AliasStorageInterface $path_storage */
    $path_storage = \Drupal::service('path.alias_storage');
576
    $path_storage->save('/path/add/one', '/path/add-one');
577 578 579 580 581 582 583 584
    /** @var \Drupal\Core\Path\AliasManagerInterface $alias_manager */
    $alias_manager = \Drupal::service('path.alias_manager');
    $alias_manager->cacheClear();

    $path = '/path/add-one';
    $request = Request::create($path, 'GET');
    $provider->getRouteCollectionForRequest($request);

585
    $cache = $this->cache->get('route:en:/path/add-one:');
586 587 588
    $this->assertEqual('/path/add/one', $cache->data['path']);
    $this->assertEqual([], $cache->data['query']);
    $this->assertEqual(3, count($cache->data['routes']));
589 590 591 592 593 594 595 596 597 598 599 600 601 602 603

    // Test with a different current language by switching out the default
    // language.
    $swiss = ConfigurableLanguage::createFromLangcode('gsw-berne');
    $language_manager->reset();
    \Drupal::service('language.default')->set($swiss);

    $path = '/path/add-one';
    $request = Request::create($path, 'GET');
    $provider->getRouteCollectionForRequest($request);

    $cache = $this->cache->get('route:gsw-berne:/path/add-one:');
    $this->assertEquals('/path/add/one', $cache->data['path']);
    $this->assertEquals([], $cache->data['query']);
    $this->assertEquals(3, count($cache->data['routes']));
604 605
  }

606 607 608
  /**
   * Test RouteProvider::getRouteByName() and RouteProvider::getRoutesByNames().
   */
609
  public function testRouteByName() {
610
    $connection = Database::getConnection();
611
    $provider = new RouteProvider($connection, $this->state, $this->currentPath, $this->cache, $this->pathProcessor, $this->cacheTagsInvalidator, 'test_routes');
612 613 614

    $this->fixtures->createTables($connection);

615
    $dumper = new MatcherDumper($connection, $this->state, 'test_routes');
616 617 618 619
    $dumper->addRoutes($this->fixtures->sampleRouteCollection());
    $dumper->dump();

    $route = $provider->getRouteByName('route_a');
620
    $this->assertEqual($route->getPath(), '/path/one', 'The right route pattern was found.');
621
    $this->assertEqual($route->getMethods(), ['GET'], 'The right route method was found.');
622
    $route = $provider->getRouteByName('route_b');
623
    $this->assertEqual($route->getPath(), '/path/one', 'The right route pattern was found.');
624
    $this->assertEqual($route->getMethods(), ['PUT'], 'The right route method was found.');
625 626 627 628 629 630 631 632 633 634

    $exception_thrown = FALSE;
    try {
      $provider->getRouteByName('invalid_name');
    }
    catch (RouteNotFoundException $e) {
      $exception_thrown = TRUE;
    }
    $this->assertTrue($exception_thrown, 'Random route was not found.');

635
    $routes = $provider->getRoutesByNames(['route_c', 'route_d', $this->randomMachineName()]);
636
    $this->assertEqual(count($routes), 2, 'Only two valid routes found.');
637 638
    $this->assertEqual($routes['route_c']->getPath(), '/path/two');
    $this->assertEqual($routes['route_d']->getPath(), '/path/three');
639 640
  }

641 642 643 644 645
  /**
   * Ensures that the routing system is capable of extreme long patterns.
   */
  public function testGetRoutesByPatternWithLongPatterns() {
    $connection = Database::getConnection();
646
    $provider = new TestRouteProvider($connection, $this->state, $this->currentPath, $this->cache, $this->pathProcessor, $this->cacheTagsInvalidator, 'test_routes');
647 648

    $this->fixtures->createTables($connection);
649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664
    // This pattern has only 3 parts, so we will get candidates, but no routes,
    // even though we have not dumped the routes yet.
    $shortest = '/test/1/test2';
    $result = $provider->getRoutesByPattern($shortest);
    $this->assertEqual($result->count(), 0);
    $candidates = $provider->getCandidateOutlines(explode('/', trim($shortest, '/')));
    $this->assertEqual(count($candidates), 7);
    // A longer patten is not found and returns no candidates
    $path_to_test = '/test/1/test2/2/test3/3/4/5/6/test4';
    $result = $provider->getRoutesByPattern($path_to_test);
    $this->assertEqual($result->count(), 0);
    $candidates = $provider->getCandidateOutlines(explode('/', trim($path_to_test, '/')));
    $this->assertEqual(count($candidates), 0);

    // Add a matching route and dump it.
    $dumper = new MatcherDumper($connection, $this->state, 'test_routes');
665 666 667 668 669
    $collection = new RouteCollection();
    $collection->add('long_pattern', new Route('/test/{v1}/test2/{v2}/test3/{v3}/{v4}/{v5}/{v6}/test4'));
    $dumper->addRoutes($collection);
    $dumper->dump();

670
    $result = $provider->getRoutesByPattern($path_to_test);
671 672 673 674
    $this->assertEqual($result->count(), 1);
    // We can't compare the values of the routes directly, nor use
    // spl_object_hash() because they are separate instances.
    $this->assertEqual(serialize($result->get('long_pattern')), serialize($collection->get('long_pattern')), 'The right route was found.');
675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696
    // We now have a single candidate outline.
    $candidates = $provider->getCandidateOutlines(explode('/', trim($path_to_test, '/')));
    $this->assertEqual(count($candidates), 1);
    // Longer and shorter patterns are not found. Both are longer than 3, so
    // we should not have any candidates either. The fact that we do not
    // get any candidates for a longer path is a security feature.
    $longer = '/test/1/test2/2/test3/3/4/5/6/test4/trailing/more/parts';
    $result = $provider->getRoutesByPattern($longer);
    $this->assertEqual($result->count(), 0);
    $candidates = $provider->getCandidateOutlines(explode('/', trim($longer, '/')));
    $this->assertEqual(count($candidates), 1);
    $shorter = '/test/1/test2/2/test3';
    $result = $provider->getRoutesByPattern($shorter);
    $this->assertEqual($result->count(), 0);
    $candidates = $provider->getCandidateOutlines(explode('/', trim($shorter, '/')));
    $this->assertEqual(count($candidates), 0);
    // This pattern has only 3 parts, so we will get candidates, but no routes.
    // This result is unchanged by running the dumper.
    $result = $provider->getRoutesByPattern($shortest);
    $this->assertEqual($result->count(), 0);
    $candidates = $provider->getCandidateOutlines(explode('/', trim($shortest, '/')));
    $this->assertEqual(count($candidates), 7);
697 698
  }

699 700 701 702 703
  /**
   * Tests getRoutesPaged().
   */
  public function testGetRoutesPaged() {
    $connection = Database::getConnection();
704
    $provider = new RouteProvider($connection, $this->state, $this->currentPath, $this->cache, $this->pathProcessor, $this->cacheTagsInvalidator, 'test_routes');
705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725

    $this->fixtures->createTables($connection);
    $dumper = new MatcherDumper($connection, $this->state, 'test_routes');
    $dumper->addRoutes($this->fixtures->sampleRouteCollection());
    $dumper->dump();

    $fixture_routes = $this->fixtures->staticSampleRouteCollection();

    // Query all the routes.
    $routes = $provider->getRoutesPaged(0);
    $this->assertEqual(array_keys($routes), array_keys($fixture_routes));

    // Query non routes.
    $routes = $provider->getRoutesPaged(0, 0);
    $this->assertEqual(array_keys($routes), []);

    // Query a limited sets of routes.
    $routes = $provider->getRoutesPaged(1, 2);
    $this->assertEqual(array_keys($routes), array_slice(array_keys($fixture_routes), 1, 2));
  }

726
}
727 728 729 730 731 732 733 734

class TestRouteProvider extends RouteProvider {

  public function getCandidateOutlines(array $parts) {
    return parent::getCandidateOutlines($parts);
  }

}