You are here

public function RouteProviderTest::testRouteCaching 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::testRouteCaching()

Tests that route caching works.

File

core/modules/system/src/Tests/Routing/RouteProviderTest.php, line 436
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 testRouteCaching() {
  $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
    ->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:/path/add/one:');
  $this
    ->assertEqual('/path/add/one', $cache->data['path']);
  $this
    ->assertEqual([], $cache->data['query']);
  $this
    ->assertEqual(3, count($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:/path/add/one:foo=bar');
  $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);
  $cache = $this->cache
    ->get('route:/path/1/one:');
  $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');
  $path_storage
    ->save('/path/add/one', '/path/add-one');

  /** @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);
  $cache = $this->cache
    ->get('route:/path/add-one:');
  $this
    ->assertEqual('/path/add/one', $cache->data['path']);
  $this
    ->assertEqual([], $cache->data['query']);
  $this
    ->assertEqual(3, count($cache->data['routes']));
}