class AliasManagerTest
Tests Drupal\path_alias\AliasManager.
Attributes
#[CoversClass(AliasManager::class)]
#[Group('path_alias')]
Hierarchy
- class \Drupal\Tests\UnitTestCase uses \Drupal\Tests\PhpUnitCompatibilityTrait, \Prophecy\PhpUnit\ProphecyTrait, \Drupal\TestTools\Extension\DeprecationBridge\ExpectDeprecationTrait, \Drupal\Tests\RandomGeneratorTrait extends \PHPUnit\Framework\TestCase
- class \Drupal\Tests\path_alias\Unit\AliasManagerTest extends \Drupal\Tests\UnitTestCase
Expanded class hierarchy of AliasManagerTest
File
-
core/
modules/ path_alias/ tests/ src/ Unit/ AliasManagerTest.php, line 19
Namespace
Drupal\Tests\path_alias\UnitView 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 prefix list.
*
* @var \Drupal\path_alias\AliasPrefixListInterface|\PHPUnit\Framework\MockObject\MockObject
*/
protected $aliasPrefixList;
/**
* 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->aliasPrefixList = $this->createMock('Drupal\\path_alias\\AliasPrefixListInterface');
$this->languageManager = $this->createMock('Drupal\\Core\\Language\\LanguageManagerInterface');
$this->cache = $this->createMock('Drupal\\Core\\Cache\\CacheBackendInterface');
$this->aliasManager = new AliasManager($this->aliasRepository, $this->aliasPrefixList, $this->languageManager, $this->cache, new Time());
}
/**
* Tests the getPathByAlias method for an alias that have no matching path.
*
* @legacy-covers ::getPathByAlias
*/
public function testGetPathByAliasNoMatch() : void {
$alias = '/' . $this->randomMachineName();
$language = new Language([
'id' => 'en',
]);
$this->languageManager
->expects($this->any())
->method('getCurrentLanguage')
->with(LanguageInterface::TYPE_URL)
->willReturn($language);
$this->aliasRepository
->expects($this->once())
->method('lookupByAlias')
->with($alias, $language->getId())
->willReturn(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.
*
* @legacy-covers ::getPathByAlias
*/
public function testGetPathByAliasMatch() : void {
$alias = $this->randomMachineName();
$path = $this->randomMachineName();
$language = $this->setUpCurrentLanguage();
$this->aliasRepository
->expects($this->once())
->method('lookupByAlias')
->with($alias, $language->getId())
->willReturn([
'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.
*
* @legacy-covers ::getPathByAlias
*/
public function testGetPathByAliasLangcode() : void {
$alias = $this->randomMachineName();
$path = $this->randomMachineName();
$this->languageManager
->expects($this->never())
->method('getCurrentLanguage');
$this->aliasRepository
->expects($this->once())
->method('lookupByAlias')
->with($alias, 'de')
->willReturn([
'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 prefix list.
*
* @legacy-covers ::getAliasByPath
*/
public function testGetAliasByPathPrefixList() {
$path_part1 = $this->randomMachineName();
$path_part2 = $this->randomMachineName();
$path = '/' . $path_part1 . '/' . $path_part2;
$this->setUpCurrentLanguage();
$this->aliasPrefixList
->expects($this->any())
->method('get')
->with($path_part1)
->willReturn(FALSE);
// The prefix list 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.
*
* @legacy-covers ::getAliasByPath
*/
public function testGetAliasByPathNoMatch() : void {
$path_part1 = $this->randomMachineName();
$path_part2 = $this->randomMachineName();
$path = '/' . $path_part1 . '/' . $path_part2;
$language = $this->setUpCurrentLanguage();
$this->aliasPrefixList
->expects($this->any())
->method('get')
->with($path_part1)
->willReturn(TRUE);
$this->aliasRepository
->expects($this->once())
->method('preloadPathAlias')
->with([
$path => $path,
], $language->getId())
->willReturn([]);
$this->assertEquals($path, $this->aliasManager
->getAliasByPath($path));
// Call it twice to test the static cache.
$this->assertEquals($path, $this->aliasManager
->getAliasByPath($path));
}
/**
* Tests the getAliasByPath method exception.
*
* @legacy-covers ::getAliasByPath
*/
public function testGetAliasByPathException() : void {
$this->expectException(\InvalidArgumentException::class);
$this->aliasManager
->getAliasByPath('no-leading-slash-here');
}
/**
* Tests the getAliasByPath method for a path that has a matching alias.
*
* @legacy-covers ::getAliasByPath
*/
public function testGetAliasByPathMatch() : void {
$path_part1 = $this->randomMachineName();
$path_part2 = $this->randomMachineName();
$path = '/' . $path_part1 . '/' . $path_part2;
$alias = $this->randomMachineName();
$language = $this->setUpCurrentLanguage();
$this->aliasPrefixList
->expects($this->any())
->method('get')
->with($path_part1)
->willReturn(TRUE);
$this->aliasRepository
->expects($this->once())
->method('preloadPathAlias')
->with([
$path => $path,
], $language->getId())
->willReturn([
$path => $alias,
]);
$this->assertEquals($alias, $this->aliasManager
->getAliasByPath($path));
// Call it twice to test the static cache.
$this->assertEquals($alias, $this->aliasManager
->getAliasByPath($path));
}
/**
* Tests the getAliasByPath cache when a different language is requested.
*
* @legacy-covers ::getAliasByPath
*/
public function testGetAliasByPathCachedMissLanguage() : void {
$path_part1 = $this->randomMachineName();
$path_part2 = $this->randomMachineName();
$path = '/' . $path_part1 . '/' . $path_part2;
$alias = $this->randomMachineName();
$language = $this->setUpCurrentLanguage();
// @todo Test no longer tests anything different useful. Call explicitly
// with two different language codes?
$this->aliasPrefixList
->expects($this->any())
->method('get')
->with($path_part1)
->willReturn(TRUE);
// The requested language is different than the cached, so this will
// need to load.
$this->aliasRepository
->expects($this->once())
->method('preloadPathAlias')
->with([
$path => $path,
], $language->getId())
->willReturn([
$path => $alias,
]);
$this->assertEquals($alias, $this->aliasManager
->getAliasByPath($path));
// Call it twice to test the static cache.
$this->assertEquals($alias, $this->aliasManager
->getAliasByPath($path));
}
/**
* Tests cache clear.
*
* @legacy-covers ::cacheClear
*/
public function testCacheClear() : void {
$path = '/path';
$alias = '/alias';
$language = $this->setUpCurrentLanguage();
$this->aliasRepository
->expects($this->exactly(2))
->method('preloadPathAlias')
->with([
$path => $path,
], $language->getId())
->willReturn([
$path => $alias,
]);
$this->aliasPrefixList
->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');
}
/**
* 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)
->willReturn($language);
return $language;
}
}
Members
| Title Sort descending | Modifiers | Object type | Summary | Overriden Title |
|---|---|---|---|---|
| AliasManagerTest::$aliasManager | protected | property | The alias manager. | |
| AliasManagerTest::$aliasPrefixList | protected | property | Alias prefix list. | |
| AliasManagerTest::$aliasRepository | protected | property | Alias repository. | |
| 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 | Tests cache clear. | |
| AliasManagerTest::testGetAliasByPathCachedMissLanguage | public | function | Tests the getAliasByPath cache when a different language is requested. | |
| AliasManagerTest::testGetAliasByPathException | public | function | Tests the getAliasByPath method exception. | |
| 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::testGetAliasByPathPrefixList | public | function | Tests the getAliasByPath method for a path that is not in the prefix list. | |
| 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. | |
| ExpectDeprecationTrait::expectDeprecation | public | function | Adds an expected deprecation. | |
| ExpectDeprecationTrait::setUpErrorHandler | public | function | Sets up the test error handler. | |
| ExpectDeprecationTrait::tearDownErrorHandler | public | function | Tears down the test error handler. | |
| RandomGeneratorTrait::getRandomGenerator | protected | function | Gets the random generator for the utility methods. | |
| RandomGeneratorTrait::randomMachineName | protected | function | Generates a unique random string containing letters and numbers. | |
| RandomGeneratorTrait::randomObject | public | function | Generates a random PHP object. | |
| RandomGeneratorTrait::randomString | public | function | Generates a pseudo-random string of ASCII characters of codes 32 to 126. | |
| UnitTestCase::$root | protected | property | The app root. | |
| 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::getContainerWithCacheTagsInvalidator | protected | function | Sets up a container with a cache tags invalidator. | |
| UnitTestCase::getStringTranslationStub | public | function | Returns a stub translation manager that just returns the passed string. | |
| UnitTestCase::setDebugDumpHandler | public static | function | Registers the dumper CLI handler when the DebugDump extension is enabled. | |
| UnitTestCase::setupMockIterator | protected | function | Set up a traversable class mock to return specific items when iterated. |
Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.