You are here

public function AliasManagerTest::testGetAliasByPathCachedMissLanguage in Drupal 8

Same name and namespace in other branches
  1. 9 core/modules/path_alias/tests/src/Unit/AliasManagerTest.php \Drupal\Tests\path_alias\Unit\AliasManagerTest::testGetAliasByPathCachedMissLanguage()
  2. 10 core/modules/path_alias/tests/src/Unit/AliasManagerTest.php \Drupal\Tests\path_alias\Unit\AliasManagerTest::testGetAliasByPathCachedMissLanguage()

Tests the getAliasByPath cache when a different language is requested.

@covers ::getAliasByPath @covers ::writeCache

File

core/modules/path_alias/tests/src/Unit/AliasManagerTest.php, line 308

Class

AliasManagerTest
@coversDefaultClass \Drupal\path_alias\AliasManager @group path_alias

Namespace

Drupal\Tests\path_alias\Unit

Code

public function testGetAliasByPathCachedMissLanguage() {
  $path_part1 = $this
    ->randomMachineName();
  $path_part2 = $this
    ->randomMachineName();
  $path = '/' . $path_part1 . '/' . $path_part2;
  $alias = $this
    ->randomMachineName();
  $language = $this
    ->setUpCurrentLanguage();
  $cached_language = new Language([
    'id' => 'de',
  ]);
  $cached_paths = [
    $cached_language
      ->getId() => [
      $path,
    ],
  ];
  $this->cache
    ->expects($this
    ->once())
    ->method('get')
    ->with($this->cacheKey)
    ->will($this
    ->returnValue((object) [
    '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));

  // The requested language is different than the cached, so this will
  // need to load.
  $this->aliasRepository
    ->expects($this
    ->never())
    ->method('preloadPathAlias');
  $this->aliasRepository
    ->expects($this
    ->once())
    ->method('lookupBySystemPath')
    ->with($path, $language
    ->getId())
    ->will($this
    ->returnValue([
    'alias' => $alias,
  ]));
  $this
    ->assertEquals($alias, $this->aliasManager
    ->getAliasByPath($path));

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

  // There is already a cache entry, so this should not write out to the
  // cache.
  $this->cache
    ->expects($this
    ->never())
    ->method('set');
  $this->aliasManager
    ->writeCache();
}