View source
<?php
namespace Drupal\Tests\path_alias\Unit;
use Drupal\Core\Language\Language;
use Drupal\Core\Language\LanguageInterface;
use Drupal\path_alias\AliasRepositoryInterface;
use Drupal\path_alias\AliasManager;
use Drupal\Tests\UnitTestCase;
class AliasManagerTest extends UnitTestCase {
protected $aliasManager;
protected $aliasStorage;
protected $aliasRepository;
protected $aliasWhitelist;
protected $languageManager;
protected $cache;
protected $cacheKey = 'preload-paths:key';
protected $path = 'key';
protected function setUp() {
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);
}
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));
$this
->assertEquals($alias, $this->aliasManager
->getPathByAlias($alias));
}
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));
$this
->assertEquals($path, $this->aliasManager
->getPathByAlias($alias));
}
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'));
$this
->assertEquals($path, $this->aliasManager
->getPathByAlias($alias, 'de'));
}
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));
$this->aliasRepository
->expects($this
->never())
->method('lookupBySystemPath');
$this
->assertEquals($path, $this->aliasManager
->getAliasByPath($path));
}
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));
$this
->assertEquals($path, $this->aliasManager
->getAliasByPath($path));
$this->cache
->expects($this
->once())
->method('set')
->with($this->cacheKey, [
$language
->getId() => [
$path,
],
], (int) $_SERVER['REQUEST_TIME'] + 60 * 60 * 24);
$this->aliasManager
->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));
$this
->assertEquals($alias, $this->aliasManager
->getAliasByPath($path));
$this->cache
->expects($this
->once())
->method('set')
->with($this->cacheKey, [
$language
->getId() => [
$path,
],
], (int) $_SERVER['REQUEST_TIME'] + 60 * 60 * 24);
$this->aliasManager
->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,
]));
$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,
]));
$this->aliasRepository
->expects($this
->never())
->method('lookupBySystemPath');
$this
->assertEquals($alias, $this->aliasManager
->getAliasByPath($path));
$this
->assertEquals($alias, $this->aliasManager
->getAliasByPath($path));
$this->cache
->expects($this
->never())
->method('set');
$this->aliasManager
->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,
]));
$this->aliasManager
->setCacheKey($this->path);
$this->aliasWhitelist
->expects($this
->any())
->method('get')
->with($path_part1)
->will($this
->returnValue(TRUE));
$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));
$this
->assertEquals($alias, $this->aliasManager
->getAliasByPath($path));
$this->cache
->expects($this
->never())
->method('set');
$this->aliasManager
->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,
]));
$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
->never())
->method('lookupBySystemPath');
$this
->assertEquals($path, $this->aliasManager
->getAliasByPath($path));
$this
->assertEquals($path, $this->aliasManager
->getAliasByPath($path));
$this->cache
->expects($this
->never())
->method('set');
$this->aliasManager
->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,
]));
$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));
$this
->assertEquals($path, $this->aliasManager
->getAliasByPath($path));
$this->cache
->expects($this
->never())
->method('set');
$this->aliasManager
->writeCache();
}
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);
$this
->assertEquals($alias, $this->aliasManager
->getAliasByPath($path, $language
->getId()));
$this->aliasRepository
->expects($this
->never())
->method('lookupByAlias');
$this
->assertEquals($path, $this->aliasManager
->getPathByAlias($alias, $language
->getId()));
$this->cache
->expects($this
->exactly(2))
->method('delete');
$this->aliasManager
->cacheClear($path);
$this
->assertEquals($alias, $this->aliasManager
->getAliasByPath($path, $language
->getId()));
$this->aliasManager
->cacheClear('non-existent');
}
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,
]));
$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));
$this
->assertEquals($new_alias, $this->aliasManager
->getAliasByPath($path));
$this->cache
->expects($this
->never())
->method('set');
$this->aliasManager
->writeCache();
}
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;
}
}