You are here

public function RouteProviderTest::testRouteCaching in Drupal 8

Same name and namespace in other branches
  1. 9 core/tests/Drupal/KernelTests/Core/Routing/RouteProviderTest.php \Drupal\KernelTests\Core\Routing\RouteProviderTest::testRouteCaching()
  2. 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 540
Contains \Drupal\KernelTests\Core\Routing\RouteProviderTest.

Class

RouteProviderTest
Confirm that the default route provider is working correctly.

Namespace

Drupal\KernelTests\Core\Routing

Code

public function testRouteCaching() {
  $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, '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:/path/add/one:');
  $this
    ->assertEqual('/path/add/one', $cache->data['path']);
  $this
    ->assertEqual([], $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:/path/add/one:foo=bar');
  $this
    ->assertEqual('/path/add/one', $cache->data['path']);
  $this
    ->assertEqual([
    'foo' => 'bar',
  ], $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:/path/1/one:');
  $this
    ->assertEqual('/path/1/one', $cache->data['path']);
  $this
    ->assertEqual([], $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:/path/add-one:');
  $this
    ->assertEqual('/path/add/one', $cache->data['path']);
  $this
    ->assertEqual([], $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:/path/add-one:');
  $this
    ->assertEquals('/path/add/one', $cache->data['path']);
  $this
    ->assertEquals([], $cache->data['query']);
  $this
    ->assertCount(3, $cache->data['routes']);
}