You are here

public function AliasManagerTest::testGetAliasByPathCachedMissNoAlias in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 core/tests/Drupal/Tests/Core/Path/AliasManagerTest.php \Drupal\Tests\Core\Path\AliasManagerTest::testGetAliasByPathCachedMissNoAlias()

Tests the getAliasByPath cache with a preloaded path without alias.

@covers ::getAliasByPath @covers ::writeCache

File

core/tests/Drupal/Tests/Core/Path/AliasManagerTest.php, line 355
Contains \Drupal\Tests\Core\Path\AliasManagerTest.

Class

AliasManagerTest
@coversDefaultClass \Drupal\Core\Path\AliasManager @group Path

Namespace

Drupal\Tests\Core\Path

Code

public function testGetAliasByPathCachedMissNoAlias() {
  $path_part1 = $this
    ->randomMachineName();
  $path_part2 = $this
    ->randomMachineName();
  $path = '/' . $path_part1 . '/' . $path_part2;
  $cached_path = $this
    ->randomMachineName();
  $cached_alias = $this
    ->randomMachineName();
  $language = $this
    ->setUpCurrentLanguage();
  $cached_paths = array(
    $language
      ->getId() => array(
      $cached_path,
      $path,
    ),
  );
  $this->cache
    ->expects($this
    ->once())
    ->method('get')
    ->with($this->cacheKey)
    ->will($this
    ->returnValue((object) array(
    'data' => $cached_paths,
  )));

  // Simulate a request so that the preloaded paths are fetched.
  $this->aliasManager
    ->setCacheKey($this->path);
  $this->aliasWhitelist
    ->expects($this
    ->any())
    ->method('get')
    ->with($path_part1)
    ->will($this
    ->returnValue(TRUE));
  $this->aliasStorage
    ->expects($this
    ->once())
    ->method('preloadPathAlias')
    ->with($cached_paths[$language
    ->getId()], $language
    ->getId())
    ->will($this
    ->returnValue(array(
    $cached_path => $cached_alias,
  )));

  // LookupPathAlias() should not be called.
  $this->aliasStorage
    ->expects($this
    ->never())
    ->method('lookupPathAlias');
  $this
    ->assertEquals($path, $this->aliasManager
    ->getAliasByPath($path));

  // Call it twice to test the static cache.
  $this
    ->assertEquals($path, $this->aliasManager
    ->getAliasByPath($path));

  // This must not write to the cache again.
  $this->cache
    ->expects($this
    ->never())
    ->method('set');
  $this->aliasManager
    ->writeCache();
}