You are here

class AliasManagerTest in Drupal 9

Same name and namespace in other branches
  1. 8 core/modules/path_alias/tests/src/Unit/AliasManagerTest.php \Drupal\Tests\path_alias\Unit\AliasManagerTest

@coversDefaultClass \Drupal\path_alias\AliasManager @group path_alias

Hierarchy

Expanded class hierarchy of AliasManagerTest

File

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

Namespace

Drupal\Tests\path_alias\Unit
View source
class AliasManagerTest extends UnitTestCase {

  /**
   * The alias manager.
   *
   * @var \Drupal\path_alias\AliasManager
   */
  protected $aliasManager;

  /**
   * Alias repository.
   *
   * @var \Drupal\path_alias\AliasRepositoryInterface|\PHPUnit\Framework\MockObject\MockObject
   */
  protected $aliasRepository;

  /**
   * Alias whitelist.
   *
   * @var \Drupal\path_alias\AliasWhitelistInterface|\PHPUnit\Framework\MockObject\MockObject
   */
  protected $aliasWhitelist;

  /**
   * Language manager.
   *
   * @var \Drupal\Core\Language\LanguageManagerInterface|\PHPUnit\Framework\MockObject\MockObject
   */
  protected $languageManager;

  /**
   * Cache backend.
   *
   * @var \Drupal\Core\Cache\CacheBackendInterface|\PHPUnit\Framework\MockObject\MockObject
   */
  protected $cache;

  /**
   * The internal cache key used by the alias manager.
   *
   * @var string
   */
  protected $cacheKey = 'preload-paths:key';

  /**
   * The cache key passed to the alias manager.
   *
   * @var string
   */
  protected $path = 'key';

  /**
   * {@inheritdoc}
   */
  protected function setUp() : void {
    parent::setUp();
    $this->aliasRepository = $this
      ->createMock(AliasRepositoryInterface::class);
    $this->aliasWhitelist = $this
      ->createMock('Drupal\\path_alias\\AliasWhitelistInterface');
    $this->languageManager = $this
      ->createMock('Drupal\\Core\\Language\\LanguageManagerInterface');
    $this->cache = $this
      ->createMock('Drupal\\Core\\Cache\\CacheBackendInterface');
    $this->aliasManager = new AliasManager($this->aliasRepository, $this->aliasWhitelist, $this->languageManager, $this->cache);
  }

  /**
   * Tests the getPathByAlias method for an alias that have no matching path.
   *
   * @covers ::getPathByAlias
   */
  public function testGetPathByAliasNoMatch() {
    $alias = '/' . $this
      ->randomMachineName();
    $language = new Language([
      'id' => 'en',
    ]);
    $this->languageManager
      ->expects($this
      ->any())
      ->method('getCurrentLanguage')
      ->with(LanguageInterface::TYPE_URL)
      ->will($this
      ->returnValue($language));
    $this->aliasRepository
      ->expects($this
      ->once())
      ->method('lookupByAlias')
      ->with($alias, $language
      ->getId())
      ->will($this
      ->returnValue(NULL));
    $this
      ->assertEquals($alias, $this->aliasManager
      ->getPathByAlias($alias));

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

  /**
   * Tests the getPathByAlias method for an alias that have a matching path.
   *
   * @covers ::getPathByAlias
   */
  public function testGetPathByAliasMatch() {
    $alias = $this
      ->randomMachineName();
    $path = $this
      ->randomMachineName();
    $language = $this
      ->setUpCurrentLanguage();
    $this->aliasRepository
      ->expects($this
      ->once())
      ->method('lookupByAlias')
      ->with($alias, $language
      ->getId())
      ->will($this
      ->returnValue([
      'path' => $path,
    ]));
    $this
      ->assertEquals($path, $this->aliasManager
      ->getPathByAlias($alias));

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

  /**
   * Tests the getPathByAlias method when a langcode is passed explicitly.
   *
   * @covers ::getPathByAlias
   */
  public function testGetPathByAliasLangcode() {
    $alias = $this
      ->randomMachineName();
    $path = $this
      ->randomMachineName();
    $this->languageManager
      ->expects($this
      ->never())
      ->method('getCurrentLanguage');
    $this->aliasRepository
      ->expects($this
      ->once())
      ->method('lookupByAlias')
      ->with($alias, 'de')
      ->will($this
      ->returnValue([
      'path' => $path,
    ]));
    $this
      ->assertEquals($path, $this->aliasManager
      ->getPathByAlias($alias, 'de'));

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

  /**
   * Tests the getAliasByPath method for a path that is not in the whitelist.
   *
   * @covers ::getAliasByPath
   */
  public function testGetAliasByPathWhitelist() {
    $path_part1 = $this
      ->randomMachineName();
    $path_part2 = $this
      ->randomMachineName();
    $path = '/' . $path_part1 . '/' . $path_part2;
    $this
      ->setUpCurrentLanguage();
    $this->aliasWhitelist
      ->expects($this
      ->any())
      ->method('get')
      ->with($path_part1)
      ->will($this
      ->returnValue(FALSE));

    // The whitelist returns FALSE for that path part, so the storage should
    // never be called.
    $this->aliasRepository
      ->expects($this
      ->never())
      ->method('lookupBySystemPath');
    $this
      ->assertEquals($path, $this->aliasManager
      ->getAliasByPath($path));
  }

  /**
   * Tests the getAliasByPath method for a path that has no matching alias.
   *
   * @covers ::getAliasByPath
   */
  public function testGetAliasByPathNoMatch() {
    $path_part1 = $this
      ->randomMachineName();
    $path_part2 = $this
      ->randomMachineName();
    $path = '/' . $path_part1 . '/' . $path_part2;
    $language = $this
      ->setUpCurrentLanguage();
    $this->aliasManager
      ->setCacheKey($this->path);
    $this->aliasWhitelist
      ->expects($this
      ->any())
      ->method('get')
      ->with($path_part1)
      ->will($this
      ->returnValue(TRUE));
    $this->aliasRepository
      ->expects($this
      ->once())
      ->method('lookupBySystemPath')
      ->with($path, $language
      ->getId())
      ->will($this
      ->returnValue(NULL));
    $this
      ->assertEquals($path, $this->aliasManager
      ->getAliasByPath($path));

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

    // This needs to write out the cache.
    $this->cache
      ->expects($this
      ->once())
      ->method('set')
      ->with($this->cacheKey, [
      $language
        ->getId() => [
        $path,
      ],
    ], (int) $_SERVER['REQUEST_TIME'] + 60 * 60 * 24);
    $this->aliasManager
      ->writeCache();
  }

  /**
   * Tests the getAliasByPath method for a path that has a matching alias.
   *
   * @covers ::getAliasByPath
   * @covers ::writeCache
   */
  public function testGetAliasByPathMatch() {
    $path_part1 = $this
      ->randomMachineName();
    $path_part2 = $this
      ->randomMachineName();
    $path = '/' . $path_part1 . '/' . $path_part2;
    $alias = $this
      ->randomMachineName();
    $language = $this
      ->setUpCurrentLanguage();
    $this->aliasManager
      ->setCacheKey($this->path);
    $this->aliasWhitelist
      ->expects($this
      ->any())
      ->method('get')
      ->with($path_part1)
      ->will($this
      ->returnValue(TRUE));
    $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));

    // This needs to write out the cache.
    $this->cache
      ->expects($this
      ->once())
      ->method('set')
      ->with($this->cacheKey, [
      $language
        ->getId() => [
        $path,
      ],
    ], (int) $_SERVER['REQUEST_TIME'] + 60 * 60 * 24);
    $this->aliasManager
      ->writeCache();
  }

  /**
   * Tests the getAliasByPath method for a path that is preloaded.
   *
   * @covers ::getAliasByPath
   * @covers ::writeCache
   */
  public function testGetAliasByPathCachedMatch() {
    $path_part1 = $this
      ->randomMachineName();
    $path_part2 = $this
      ->randomMachineName();
    $path = '/' . $path_part1 . '/' . $path_part2;
    $alias = $this
      ->randomMachineName();
    $language = $this
      ->setUpCurrentLanguage();
    $cached_paths = [
      $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));
    $this->aliasRepository
      ->expects($this
      ->once())
      ->method('preloadPathAlias')
      ->with($cached_paths[$language
      ->getId()], $language
      ->getId())
      ->will($this
      ->returnValue([
      $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();
  }

  /**
   * Tests the getAliasByPath cache when a different language is requested.
   *
   * @covers ::getAliasByPath
   * @covers ::writeCache
   */
  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();
  }

  /**
   * Tests the getAliasByPath cache with a preloaded path without alias.
   *
   * @covers ::getAliasByPath
   * @covers ::writeCache
   */
  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 = [
      $language
        ->getId() => [
        $cached_path,
        $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));
    $this->aliasRepository
      ->expects($this
      ->once())
      ->method('preloadPathAlias')
      ->with($cached_paths[$language
      ->getId()], $language
      ->getId())
      ->will($this
      ->returnValue([
      $cached_path => $cached_alias,
    ]));

    // LookupPathAlias() should not be called.
    $this->aliasRepository
      ->expects($this
      ->never())
      ->method('lookupBySystemPath');
    $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();
  }

  /**
   * Tests the getAliasByPath cache with an unpreloaded path without alias.
   *
   * @covers ::getAliasByPath
   * @covers ::writeCache
   */
  public function testGetAliasByPathUncachedMissNoAlias() {
    $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 = [
      $language
        ->getId() => [
        $cached_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));
    $this->aliasRepository
      ->expects($this
      ->once())
      ->method('preloadPathAlias')
      ->with($cached_paths[$language
      ->getId()], $language
      ->getId())
      ->will($this
      ->returnValue([
      $cached_path => $cached_alias,
    ]));
    $this->aliasRepository
      ->expects($this
      ->once())
      ->method('lookupBySystemPath')
      ->with($path, $language
      ->getId())
      ->will($this
      ->returnValue(NULL));
    $this
      ->assertEquals($path, $this->aliasManager
      ->getAliasByPath($path));

    // Call it twice to test the static cache.
    $this
      ->assertEquals($path, $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();
  }

  /**
   * @covers ::cacheClear
   */
  public function testCacheClear() {
    $path = '/path';
    $alias = '/alias';
    $language = $this
      ->setUpCurrentLanguage();
    $this->aliasRepository
      ->expects($this
      ->exactly(2))
      ->method('lookupBySystemPath')
      ->with($path, $language
      ->getId())
      ->willReturn([
      'alias' => $alias,
    ]);
    $this->aliasWhitelist
      ->expects($this
      ->any())
      ->method('get')
      ->willReturn(TRUE);

    // Populate the lookup map.
    $this
      ->assertEquals($alias, $this->aliasManager
      ->getAliasByPath($path, $language
      ->getId()));

    // Check that the cache is populated.
    $this->aliasRepository
      ->expects($this
      ->never())
      ->method('lookupByAlias');
    $this
      ->assertEquals($path, $this->aliasManager
      ->getPathByAlias($alias, $language
      ->getId()));

    // Clear specific source.
    $this->aliasManager
      ->cacheClear($path);

    // Ensure cache has been cleared (this will be the 2nd call to
    // `lookupPathAlias` if cache is cleared).
    $this
      ->assertEquals($alias, $this->aliasManager
      ->getAliasByPath($path, $language
      ->getId()));

    // Clear non-existent source.
    $this->aliasManager
      ->cacheClear('non-existent');
  }

  /**
   * Tests the getAliasByPath cache with an unpreloaded path with alias.
   *
   * @covers ::getAliasByPath
   * @covers ::writeCache
   */
  public function testGetAliasByPathUncachedMissWithAlias() {
    $path_part1 = $this
      ->randomMachineName();
    $path_part2 = $this
      ->randomMachineName();
    $path = '/' . $path_part1 . '/' . $path_part2;
    $cached_path = $this
      ->randomMachineName();
    $cached_no_alias_path = $this
      ->randomMachineName();
    $cached_alias = $this
      ->randomMachineName();
    $new_alias = $this
      ->randomMachineName();
    $language = $this
      ->setUpCurrentLanguage();
    $cached_paths = [
      $language
        ->getId() => [
        $cached_path,
        $cached_no_alias_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));
    $this->aliasRepository
      ->expects($this
      ->once())
      ->method('preloadPathAlias')
      ->with($cached_paths[$language
      ->getId()], $language
      ->getId())
      ->will($this
      ->returnValue([
      $cached_path => $cached_alias,
    ]));
    $this->aliasRepository
      ->expects($this
      ->once())
      ->method('lookupBySystemPath')
      ->with($path, $language
      ->getId())
      ->will($this
      ->returnValue([
      'alias' => $new_alias,
    ]));
    $this
      ->assertEquals($new_alias, $this->aliasManager
      ->getAliasByPath($path));

    // Call it twice to test the static cache.
    $this
      ->assertEquals($new_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();
  }

  /**
   * Sets up the current language.
   *
   * @return \Drupal\Core\Language\LanguageInterface
   *   The current language object.
   */
  protected function setUpCurrentLanguage() {
    $language = new Language([
      'id' => 'en',
    ]);
    $this->languageManager
      ->expects($this
      ->any())
      ->method('getCurrentLanguage')
      ->with(LanguageInterface::TYPE_URL)
      ->will($this
      ->returnValue($language));
    return $language;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
AliasManagerTest::$aliasManager protected property The alias manager.
AliasManagerTest::$aliasRepository protected property Alias repository.
AliasManagerTest::$aliasWhitelist protected property Alias whitelist.
AliasManagerTest::$cache protected property Cache backend.
AliasManagerTest::$cacheKey protected property The internal cache key used by the alias manager.
AliasManagerTest::$languageManager protected property Language manager.
AliasManagerTest::$path protected property The cache key passed to the alias manager.
AliasManagerTest::setUp protected function Overrides UnitTestCase::setUp
AliasManagerTest::setUpCurrentLanguage protected function Sets up the current language.
AliasManagerTest::testCacheClear public function @covers ::cacheClear
AliasManagerTest::testGetAliasByPathCachedMatch public function Tests the getAliasByPath method for a path that is preloaded.
AliasManagerTest::testGetAliasByPathCachedMissLanguage public function Tests the getAliasByPath cache when a different language is requested.
AliasManagerTest::testGetAliasByPathCachedMissNoAlias public function Tests the getAliasByPath cache with a preloaded path without alias.
AliasManagerTest::testGetAliasByPathMatch public function Tests the getAliasByPath method for a path that has a matching alias.
AliasManagerTest::testGetAliasByPathNoMatch public function Tests the getAliasByPath method for a path that has no matching alias.
AliasManagerTest::testGetAliasByPathUncachedMissNoAlias public function Tests the getAliasByPath cache with an unpreloaded path without alias.
AliasManagerTest::testGetAliasByPathUncachedMissWithAlias public function Tests the getAliasByPath cache with an unpreloaded path with alias.
AliasManagerTest::testGetAliasByPathWhitelist public function Tests the getAliasByPath method for a path that is not in the whitelist.
AliasManagerTest::testGetPathByAliasLangcode public function Tests the getPathByAlias method when a langcode is passed explicitly.
AliasManagerTest::testGetPathByAliasMatch public function Tests the getPathByAlias method for an alias that have a matching path.
AliasManagerTest::testGetPathByAliasNoMatch public function Tests the getPathByAlias method for an alias that have no matching path.
PhpUnitWarnings::$deprecationWarnings private static property Deprecation warnings from PHPUnit to raise with @trigger_error().
PhpUnitWarnings::addWarning public function Converts PHPUnit deprecation warnings to E_USER_DEPRECATED.
UnitTestCase::$randomGenerator protected property The random generator.
UnitTestCase::$root protected property The app root. 1
UnitTestCase::assertArrayEquals Deprecated protected function Asserts if two arrays are equal by sorting them first.
UnitTestCase::getClassResolverStub protected function Returns a stub class resolver.
UnitTestCase::getConfigFactoryStub public function Returns a stub config factory that behaves according to the passed array.
UnitTestCase::getConfigStorageStub public function Returns a stub config storage that returns the supplied configuration.
UnitTestCase::getContainerWithCacheTagsInvalidator protected function Sets up a container with a cache tags invalidator.
UnitTestCase::getRandomGenerator protected function Gets the random generator for the utility methods.
UnitTestCase::getStringTranslationStub public function Returns a stub translation manager that just returns the passed string.
UnitTestCase::randomMachineName public function Generates a unique random string containing letters and numbers.
UnitTestCase::setUpBeforeClass public static function