function AliasManagerTest::testGetAliasByPathCachedMatch

Same name and namespace in other branches
  1. 9 core/modules/path_alias/tests/src/Unit/AliasManagerTest.php \Drupal\Tests\path_alias\Unit\AliasManagerTest::testGetAliasByPathCachedMatch()
  2. 8.9.x core/modules/path_alias/tests/src/Unit/AliasManagerTest.php \Drupal\Tests\path_alias\Unit\AliasManagerTest::testGetAliasByPathCachedMatch()
  3. 11.x core/modules/path_alias/tests/src/Unit/AliasManagerTest.php \Drupal\Tests\path_alias\Unit\AliasManagerTest::testGetAliasByPathCachedMatch()

Tests the getAliasByPath method for a path that is preloaded.

@covers ::getAliasByPath
@covers ::writeCache

File

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

Class

AliasManagerTest
@coversDefaultClass \Drupal\path_alias\AliasManager[[api-linebreak]] @group path_alias

Namespace

Drupal\Tests\path_alias\Unit

Code

public function testGetAliasByPathCachedMatch() : void {
  $path_part1 = $this->randomMachineName();
  $path_part2 = $this->randomMachineName();
  $path = '/' . $path_part1 . '/' . $path_part2;
  $alias = $this->randomMachineName();
  $language = $this->setUpCurrentLanguage();
  // Use a set of cached paths where the tested path is in any position, not
  // only in the first one.
  $cached_paths = [
    $language->getId() => [
      '/another/path',
      $path,
    ],
  ];
  $this->cache
    ->expects($this->once())
    ->method('get')
    ->with($this->cacheKey)
    ->willReturn((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)
    ->willReturn(TRUE);
  $this->aliasRepository
    ->expects($this->once())
    ->method('preloadPathAlias')
    ->with($cached_paths[$language->getId()], $language->getId())
    ->willReturn([
    $path => $alias,
  ]);
  // LookupPathAlias should not be called.
  $this->aliasRepository
    ->expects($this->never())
    ->method('lookupBySystemPath');
  $this->assertEquals($alias, $this->aliasManager
    ->getAliasByPath($path));
  // Call it twice to test the static cache.
  $this->assertEquals($alias, $this->aliasManager
    ->getAliasByPath($path));
  // This must not write to the cache again.
  $this->cache
    ->expects($this->never())
    ->method('set');
  $this->aliasManager
    ->writeCache();
}

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