function RouteProviderTest::testGetRoutesByPatternWithLongPatterns

Same name and namespace in other branches
  1. 9 core/tests/Drupal/KernelTests/Core/Routing/RouteProviderTest.php \Drupal\KernelTests\Core\Routing\RouteProviderTest::testGetRoutesByPatternWithLongPatterns()
  2. 8.9.x core/tests/Drupal/KernelTests/Core/Routing/RouteProviderTest.php \Drupal\KernelTests\Core\Routing\RouteProviderTest::testGetRoutesByPatternWithLongPatterns()
  3. 11.x core/tests/Drupal/KernelTests/Core/Routing/RouteProviderTest.php \Drupal\KernelTests\Core\Routing\RouteProviderTest::testGetRoutesByPatternWithLongPatterns()

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

File

core/tests/Drupal/KernelTests/Core/Routing/RouteProviderTest.php, line 698

Class

RouteProviderTest
Confirm that the default route provider is working correctly.

Namespace

Drupal\KernelTests\Core\Routing

Code

public function testGetRoutesByPatternWithLongPatterns() : void {
  $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->assertEquals(0, $result->count());
  $candidates = $provider->getCandidateOutlines(explode('/', trim($shortest, '/')));
  $this->assertCount(7, $candidates);
  // 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->assertEquals(0, $result->count());
  $candidates = $provider->getCandidateOutlines(explode('/', trim($path_to_test, '/')));
  $this->assertCount(0, $candidates);
  // Add a matching route and dump it.
  $dumper = new MatcherDumper($connection, $this->state, $this->logger, '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->assertEquals(1, $result->count());
  // We can't compare the values of the routes directly, nor use
  // spl_object_hash() because they are separate instances.
  $this->assertEquals(serialize($collection->get('long_pattern')), serialize($result->get('long_pattern')), 'The right route was found.');
  // We now have a single candidate outline.
  $candidates = $provider->getCandidateOutlines(explode('/', trim($path_to_test, '/')));
  $this->assertCount(1, $candidates);
  // 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->assertEquals(0, $result->count());
  $candidates = $provider->getCandidateOutlines(explode('/', trim($longer, '/')));
  $this->assertCount(1, $candidates);
  $shorter = '/test/1/test2/2/test3';
  $result = $provider->getRoutesByPattern($shorter);
  $this->assertEquals(0, $result->count());
  $candidates = $provider->getCandidateOutlines(explode('/', trim($shorter, '/')));
  $this->assertCount(0, $candidates);
  // 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->assertEquals(0, $result->count());
  $candidates = $provider->getCandidateOutlines(explode('/', trim($shortest, '/')));
  $this->assertCount(7, $candidates);
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.