You are here

public function AliasTest::testLookupPath in Drupal 8

Same name and namespace in other branches
  1. 9 core/modules/path_alias/tests/src/Kernel/AliasTest.php \Drupal\Tests\path_alias\Kernel\AliasTest::testLookupPath()
  2. 10 core/modules/path_alias/tests/src/Kernel/AliasTest.php \Drupal\Tests\path_alias\Kernel\AliasTest::testLookupPath()

@covers \Drupal\path_alias\AliasManager::getPathByAlias @covers \Drupal\path_alias\AliasManager::getAliasByPath

File

core/modules/path_alias/tests/src/Kernel/AliasTest.php, line 67

Class

AliasTest
Tests path alias CRUD and lookup functionality.

Namespace

Drupal\Tests\path_alias\Kernel

Code

public function testLookupPath() {

  // Create AliasManager and Path object.
  $aliasManager = $this->container
    ->get('path_alias.manager');

  // Test the situation where the source is the same for multiple aliases.
  // Start with a language-neutral alias, which we will override.
  $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.');

  // Create a language specific alias for the default language (English).
  $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.');

  // Create a language-neutral alias for the same path, again.
  $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.');

  // Create a language-specific (xx-lolspeak) alias for the same path.
  $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.');

  // The LOLspeak alias should be returned if we really want LOLspeak.
  $this
    ->assertEquals('/LOL', $aliasManager
    ->getAliasByPath($path_alias
    ->getPath(), 'xx-lolspeak'), 'LOLspeak alias returned if we specify xx-lolspeak to the alias manager.');

  // Create a new alias for this path in English, which should override the
  // previous alias for "user/1".
  $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.');

  // Remove the English aliases, which should cause a fallback to the most
  // recently created language-neutral alias, 'bar'.
  $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.');

  // Test the situation where the alias and language are the same, but
  // the source differs. The newer alias record should be returned.
  $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.');
}