function RouteProviderTest::testRouteCaching

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

Tests that route caching works.

File

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

Class

RouteProviderTest
Confirm that the default route provider is working correctly.

Namespace

Drupal\KernelTests\Core\Routing

Code

public function testRouteCaching() : void {
    $connection = Database::getConnection();
    $language_manager = \Drupal::languageManager();
    $provider = new RouteProvider($connection, $this->state, $this->currentPath, $this->cache, $this->pathProcessor, $this->cacheTagsInvalidator, 'test_routes', $language_manager);
    $this->fixtures
        ->createTables($connection);
    $dumper = new MatcherDumper($connection, $this->state, $this->logger, 'test_routes');
    $dumper->addRoutes($this->fixtures
        ->sampleRouteCollection());
    $dumper->addRoutes($this->fixtures
        ->complexRouteCollection());
    $dumper->dump();
    // A simple path.
    $path = '/path/add/one';
    $request = Request::create($path, 'GET');
    $provider->getRouteCollectionForRequest($request);
    $cache = $this->cache
        ->get('route:[language]=en:[query_parameters]=:/path/add/one');
    $this->assertEquals('/path/add/one', $cache->data['path']);
    $this->assertEquals([], $cache->data['query']);
    $this->assertCount(3, $cache->data['routes']);
    // A path with query parameters.
    $path = '/path/add/one?foo=bar';
    $request = Request::create($path, 'GET');
    $provider->getRouteCollectionForRequest($request);
    $cache = $this->cache
        ->get('route:[language]=en:[query_parameters]=foo=bar,2fb8f40115dd1e695cbe23d4f97ce5b1fb697eee:/path/add/one');
    $this->assertEquals('/path/add/one', $cache->data['path']);
    $this->assertEquals([
        'foo' => 'bar',
    ], $cache->data['query']);
    $this->assertCount(3, $cache->data['routes']);
    // A path with multivalued query parameters.
    $path = '/path/add/one?foo=bar&foo2[]=bar2&foo2[]=bar3';
    $request = Request::create($path, 'GET');
    $provider->getRouteCollectionForRequest($request);
    $cache = $this->cache
        ->get('route:[language]=en:[query_parameters]=foo=bar&foo2%5B0%5D=bar2&foo2%5B1%5D=bar3,2d3a0851c4970a16be1c851a0e9946e6d10a3fe2:/path/add/one');
    $this->assertEquals('/path/add/one', $cache->data['path']);
    $this->assertEquals([
        'foo' => 'bar',
        'foo2' => [
            'bar2',
            'bar3',
        ],
    ], $cache->data['query']);
    $this->assertCount(3, $cache->data['routes']);
    // A path with placeholders.
    $path = '/path/1/one';
    $request = Request::create($path, 'GET');
    $provider->getRouteCollectionForRequest($request);
    $cache = $this->cache
        ->get('route:[language]=en:[query_parameters]=:/path/1/one');
    $this->assertEquals('/path/1/one', $cache->data['path']);
    $this->assertEquals([], $cache->data['query']);
    $this->assertCount(2, $cache->data['routes']);
    // A path with a path alias.
    $this->createPathAlias('/path/add/one', '/path/add-one');
    
    /** @var \Drupal\path_alias\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);
    $cache = $this->cache
        ->get('route:[language]=en:[query_parameters]=:/path/add-one');
    $this->assertEquals('/path/add/one', $cache->data['path']);
    $this->assertEquals([], $cache->data['query']);
    $this->assertCount(3, $cache->data['routes']);
    // 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:[language]=gsw-berne:[query_parameters]=:/path/add-one');
    $this->assertEquals('/path/add/one', $cache->data['path']);
    $this->assertEquals([], $cache->data['query']);
    $this->assertCount(3, $cache->data['routes']);
}

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