You are here

public function RouteProviderTest::testGetRoutesByPatternWithLongPatterns in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 core/modules/system/src/Tests/Routing/RouteProviderTest.php \Drupal\system\Tests\Routing\RouteProviderTest::testGetRoutesByPatternWithLongPatterns()

Ensures that the routing system is capable of extreme long patterns.

File

core/modules/system/src/Tests/Routing/RouteProviderTest.php, line 533
Contains \Drupal\system\Tests\Routing\RouteProviderTest.

Class

RouteProviderTest
Confirm that the default route provider is working correctly.

Namespace

Drupal\system\Tests\Routing

Code

public function testGetRoutesByPatternWithLongPatterns() {
  $connection = Database::getConnection();
  $provider = new TestRouteProvider($connection, $this->state, $this->currentPath, $this->cache, $this->pathProcessor, $this->cacheTagsInvalidator, 'test_routes');
  $this->fixtures
    ->createTables($connection);

  // 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');
  $collection = new RouteCollection();
  $collection
    ->add('long_pattern', new Route('/test/{v1}/test2/{v2}/test3/{v3}/{v4}/{v5}/{v6}/test4'));
  $dumper
    ->addRoutes($collection);
  $dumper
    ->dump();
  $result = $provider
    ->getRoutesByPattern($path_to_test);
  $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.');

  // 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);
}