function DeprecatedServicesTest::testAliasManagerSharedState

Test that the new alias manager and the legacy ones share the same state.

@expectedDeprecation The \Drupal\Core\Path\AliasManager class is deprecated in drupal:8.8.0 and is removed from drupal:9.0.0. Instead, use \Drupal\path_alias\AliasManager. See https://drupal.org/node/3092086

File

core/modules/path_alias/tests/src/Kernel/DeprecatedServicesTest.php, line 105

Class

DeprecatedServicesTest
Tests deprecation of path alias core services and the related BC logic.

Namespace

Drupal\Tests\path_alias\Kernel

Code

public function testAliasManagerSharedState() {
  /** @var \Drupal\Core\Path\AliasManager $legacy_alias_manager */
  $legacy_alias_manager = $this->container
    ->get('path.alias_manager');
  /** @var \Drupal\path_alias\AliasManager $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');
  /** @var \Drupal\path_alias\Entity\PathAlias $alias */
  $alias = PathAlias::create([
    'path' => '/' . $this->randomMachineName(),
    'alias' => $invalid_alias . '2',
  ]);
  $alias->save();
  $this->assertSharedProperty([], $legacy_alias_manager, 'preloadedPathLookups');
  /** @var \Drupal\Core\State\StateInterface $state */
  $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');
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.