View source
<?php
namespace Drupal\Tests\path_alias\Kernel;
use Drupal\Core\Cache\MemoryCounterBackend;
use Drupal\Core\Language\LanguageInterface;
use Drupal\KernelTests\KernelTestBase;
use Drupal\path_alias\AliasManager;
use Drupal\path_alias\AliasWhitelist;
use Drupal\Tests\Traits\Core\PathAliasTestTrait;
class AliasTest extends KernelTestBase {
use PathAliasTestTrait;
protected static $modules = [
'path_alias',
];
protected function setUp() : void {
parent::setUp();
\Drupal::state()
->set('router.path_roots', [
'user',
'admin',
]);
$this
->installEntitySchema('path_alias');
}
public function testLookupBySystemPath() {
$this
->createPathAlias('/test-source-Case', '/test-alias');
$path_alias_repository = $this->container
->get('path_alias.repository');
$this
->assertEquals('/test-alias', $path_alias_repository
->lookupBySystemPath('/test-source-Case', LanguageInterface::LANGCODE_NOT_SPECIFIED)['alias']);
$this
->assertEquals('/test-alias', $path_alias_repository
->lookupBySystemPath('/test-source-case', LanguageInterface::LANGCODE_NOT_SPECIFIED)['alias']);
}
public function testLookupByAlias() {
$this
->createPathAlias('/test-source', '/test-alias-Case');
$path_alias_repository = $this->container
->get('path_alias.repository');
$this
->assertEquals('/test-source', $path_alias_repository
->lookupByAlias('/test-alias-Case', LanguageInterface::LANGCODE_NOT_SPECIFIED)['path']);
$this
->assertEquals('/test-source', $path_alias_repository
->lookupByAlias('/test-alias-case', LanguageInterface::LANGCODE_NOT_SPECIFIED)['path']);
}
public function testLookupPath() {
$aliasManager = $this->container
->get('path_alias.manager');
$path_alias = $this
->createPathAlias('/user/1', '/foo');
$this
->assertEquals($path_alias
->getAlias(), $aliasManager
->getAliasByPath($path_alias
->getPath()), 'Basic alias lookup works.');
$this
->assertEquals($path_alias
->getPath(), $aliasManager
->getPathByAlias($path_alias
->getAlias()), 'Basic source lookup works.');
$path_alias = $this
->createPathAlias('/user/1', '/users/Dries', 'en');
$this
->assertEquals($path_alias
->getAlias(), $aliasManager
->getAliasByPath($path_alias
->getPath()), 'English alias overrides language-neutral alias.');
$this
->assertEquals($path_alias
->getPath(), $aliasManager
->getPathByAlias($path_alias
->getAlias()), 'English source overrides language-neutral source.');
$path_alias = $this
->createPathAlias('/user/1', '/bar');
$this
->assertEquals("/users/Dries", $aliasManager
->getAliasByPath($path_alias
->getPath()), 'English alias still returned after entering a language-neutral alias.');
$path_alias = $this
->createPathAlias('/user/1', '/LOL', 'xx-lolspeak');
$this
->assertEquals("/users/Dries", $aliasManager
->getAliasByPath($path_alias
->getPath()), 'English alias still returned after entering a LOLspeak alias.');
$this
->assertEquals('/LOL', $aliasManager
->getAliasByPath($path_alias
->getPath(), 'xx-lolspeak'), 'LOLspeak alias returned if we specify xx-lolspeak to the alias manager.');
$path_alias = $this
->createPathAlias('/user/1', '/users/my-new-path', 'en');
$this
->assertEquals($path_alias
->getAlias(), $aliasManager
->getAliasByPath($path_alias
->getPath()), 'Recently created English alias returned.');
$this
->assertEquals($path_alias
->getPath(), $aliasManager
->getPathByAlias($path_alias
->getAlias()), 'Recently created English source returned.');
$path_alias_storage = $this->container
->get('entity_type.manager')
->getStorage('path_alias');
$entities = $path_alias_storage
->loadByProperties([
'langcode' => 'en',
]);
$path_alias_storage
->delete($entities);
$this
->assertEquals('/bar', $aliasManager
->getAliasByPath($path_alias
->getPath()), 'Path lookup falls back to recently created language-neutral alias.');
$this
->createPathAlias('/user/2', '/bar');
$aliasManager
->cacheClear();
$this
->assertEquals('/user/2', $aliasManager
->getPathByAlias('/bar'), 'Newer alias record is returned when comparing two LanguageInterface::LANGCODE_NOT_SPECIFIED paths with the same alias.');
}
public function testWhitelist() {
$memoryCounterBackend = new MemoryCounterBackend();
$whitelist = new AliasWhitelist('path_alias_whitelist', $memoryCounterBackend, $this->container
->get('lock'), $this->container
->get('state'), $this->container
->get('path_alias.repository'));
$aliasManager = new AliasManager($this->container
->get('path_alias.repository'), $whitelist, $this->container
->get('language_manager'), $memoryCounterBackend);
$this
->assertNull($whitelist
->get('user'));
$this
->assertNull($whitelist
->get('admin'));
$this
->assertNull($whitelist
->get($this
->randomMachineName()));
$this
->createPathAlias('/user/1', '/' . $this
->randomMachineName());
$aliasManager
->cacheClear();
$this
->assertTrue($whitelist
->get('user'));
$this
->assertNull($whitelist
->get('admin'));
$this
->assertNull($whitelist
->get($this
->randomMachineName()));
$this
->createPathAlias('/admin/something', '/' . $this
->randomMachineName());
$aliasManager
->cacheClear();
$this
->assertTrue($whitelist
->get('user'));
$this
->assertTrue($whitelist
->get('admin'));
$this
->assertNull($whitelist
->get($this
->randomMachineName()));
$path_alias_storage = $this->container
->get('entity_type.manager')
->getStorage('path_alias');
$entities = $path_alias_storage
->loadByProperties([
'path' => '/user/1',
]);
$path_alias_storage
->delete($entities);
$aliasManager
->cacheClear();
$this
->assertNull($whitelist
->get('user'));
$this
->assertTrue($whitelist
->get('admin'));
$this
->assertNull($whitelist
->get($this
->randomMachineName()));
$whitelist
->destruct();
$this
->assertEquals(1, $memoryCounterBackend
->getCounter('set', 'path_alias_whitelist'));
$memoryCounterBackend
->resetCounter();
$whitelist = new AliasWhitelist('path_alias_whitelist', $memoryCounterBackend, $this->container
->get('lock'), $this->container
->get('state'), $this->container
->get('path_alias.repository'));
$this
->assertNull($whitelist
->get('user'));
$this
->assertTrue($whitelist
->get('admin'));
$this
->assertNull($whitelist
->get($this
->randomMachineName()));
$this
->assertEquals(1, $memoryCounterBackend
->getCounter('get', 'path_alias_whitelist'));
$this
->assertEquals(0, $memoryCounterBackend
->getCounter('set', 'path_alias_whitelist'));
$whitelist
->destruct();
$this
->assertEquals(1, $memoryCounterBackend
->getCounter('get', 'path_alias_whitelist'));
$this
->assertEquals(0, $memoryCounterBackend
->getCounter('set', 'path_alias_whitelist'));
}
public function testWhitelistCacheDeletionMidRequest() {
$memoryCounterBackend = new MemoryCounterBackend();
$whitelist = new AliasWhitelist('path_alias_whitelist', $memoryCounterBackend, $this->container
->get('lock'), $this->container
->get('state'), $this->container
->get('path_alias.repository'));
$this
->assertFalse($memoryCounterBackend
->get('path_alias_whitelist'));
$this
->createPathAlias('/admin/something', '/' . $this
->randomMachineName());
$this
->createPathAlias('/user/something', '/' . $this
->randomMachineName());
$this
->assertTrue($whitelist
->get('admin'));
$whitelist
->destruct();
$this
->assertEquals(1, $memoryCounterBackend
->getCounter('set', 'path_alias_whitelist'));
$this
->assertEquals([
'user' => FALSE,
'admin' => TRUE,
], $memoryCounterBackend
->get('path_alias_whitelist')->data);
$memoryCounterBackend
->resetCounter();
$whitelist = new AliasWhitelist('path_alias_whitelist', $memoryCounterBackend, $this->container
->get('lock'), $this->container
->get('state'), $this->container
->get('path_alias.repository'));
$this
->assertTrue($whitelist
->get('user'));
$memoryCounterBackend
->delete('path_alias_whitelist');
$whitelist
->destruct();
$this
->assertEquals(0, $memoryCounterBackend
->getCounter('set', 'path_alias_whitelist'));
$this
->assertFalse($memoryCounterBackend
->get('path_alias_whitelist'));
$memoryCounterBackend
->resetCounter();
}
}