View source
<?php
namespace Drupal\Tests\path_alias\Kernel;
use Drupal\KernelTests\KernelTestBase;
use Drupal\Core\Path\AliasManager as CoreAliasManager;
use Drupal\path_alias\AliasManager;
use Drupal\path_alias\Entity\PathAlias;
use Drupal\path_alias_deprecated_test\AliasManagerDecorator;
use Drupal\path_alias_deprecated_test\NewAliasManager;
use Drupal\path_alias_deprecated_test\OverriddenAliasManager;
use Drupal\path_alias_deprecated_test\PathAliasDeprecatedTestServiceProvider;
class DeprecatedServicesTest extends KernelTestBase {
public static $modules = [
'path_alias',
'path_alias_deprecated_test',
];
protected function setUp() {
parent::setUp();
$this
->installEntitySchema('path_alias');
}
public function testAliasServicesDeprecation() {
$this->container
->get('path.alias_manager');
$this->container
->get('path_processor_alias');
$this->container
->get('path_subscriber');
}
public function testOverriddenServiceImplementation() {
$class = $this
->setServiceClass(OverriddenAliasManager::class);
$this
->assertServiceClass('path.alias_manager', $class);
$this
->assertServiceClass('path_alias.manager', AliasManager::class);
}
public function testNewServiceImplementation() {
$class = $this
->setServiceClass(NewAliasManager::class);
$this
->assertServiceClass('path.alias_manager', $class);
$this
->assertServiceClass('path_alias.manager', AliasManager::class);
}
public function testDecoratorForOverriddenServiceImplementation() {
$this
->setServiceClass(OverriddenAliasManager::class, TRUE);
$this
->assertServiceClass('path.alias_manager', AliasManagerDecorator::class);
$this
->assertServiceClass('path_alias.manager', AliasManager::class);
}
public function testDecoratorForNewServiceImplementation() {
$this
->setServiceClass(NewAliasManager::class, TRUE);
$this
->assertServiceClass('path.alias_manager', AliasManagerDecorator::class);
$this
->assertServiceClass('path_alias.manager', AliasManager::class);
}
public function testDefaultImplementations() {
$this
->assertServiceClass('path.alias_manager', CoreAliasManager::class);
$this
->assertServiceClass('path_alias.manager', AliasManager::class);
}
public function testRegularImplementation() {
$this
->assertServiceClass('path_alias.manager', AliasManager::class);
}
public function testAliasManagerSharedState() {
$legacy_alias_manager = $this->container
->get('path.alias_manager');
$alias_manager = $this->container
->get('path_alias.manager');
$cache_key = $this
->randomMachineName();
$alias_manager
->setCacheKey($cache_key);
$this
->assertSharedProperty('preload-paths:' . $cache_key, $legacy_alias_manager, 'cacheKey');
$invalid_alias = '/' . $this
->randomMachineName();
$alias_manager
->getPathByAlias($invalid_alias);
$this
->assertSharedProperty([
'en' => [
$invalid_alias => TRUE,
],
], $legacy_alias_manager, 'noPath');
$this
->assertSharedProperty(FALSE, $legacy_alias_manager, 'preloadedPathLookups');
$alias = PathAlias::create([
'path' => '/' . $this
->randomMachineName(),
'alias' => $invalid_alias . '2',
]);
$alias
->save();
$this
->assertSharedProperty([], $legacy_alias_manager, 'preloadedPathLookups');
$state = $this->container
->get('state');
$state
->set('router.path_roots', [
ltrim($alias
->getPath(), '/'),
]);
$alias_manager
->getAliasByPath($alias
->getPath());
$this
->assertSharedProperty([
'en' => [
$alias
->getPath() => $alias
->getAlias(),
],
], $legacy_alias_manager, 'lookupMap');
$invalid_path = $alias
->getPath() . '/' . $this
->randomMachineName();
$alias_manager
->getAliasByPath($invalid_path);
$this
->assertSharedProperty([
'en' => [
$invalid_path => TRUE,
],
], $legacy_alias_manager, 'noAlias');
}
protected function assertSharedProperty($expected, CoreAliasManager $legacy_alias_manager, $property) {
$reflector = new \ReflectionProperty(get_class($legacy_alias_manager), $property);
$reflector
->setAccessible(TRUE);
$this
->assertSame($expected, $reflector
->getValue($legacy_alias_manager));
}
protected function assertServiceClass($service_id, $expected_class) {
$service = $this->container
->get($service_id);
$this
->assertSame(get_class($service), $expected_class);
}
protected function setServiceClass($class, $use_decorator = FALSE) {
PathAliasDeprecatedTestServiceProvider::$newClass = $class;
PathAliasDeprecatedTestServiceProvider::$useDecorator = $use_decorator;
$this->container
->get('kernel')
->rebuildContainer();
return $class;
}
}