You are here

function AliasTest::testLookupPath in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 core/modules/system/src/Tests/Path/AliasTest.php \Drupal\system\Tests\Path\AliasTest::testLookupPath()

File

core/modules/system/src/Tests/Path/AliasTest.php, line 81
Contains \Drupal\system\Tests\Path\AliasTest.

Class

AliasTest
Tests path alias CRUD and lookup functionality.

Namespace

Drupal\system\Tests\Path

Code

function testLookupPath() {

  //Prepare database table.
  $connection = Database::getConnection();
  $this->fixtures
    ->createTables($connection);

  //Create AliasManager and Path object.
  $aliasManager = $this->container
    ->get('path.alias_manager');
  $aliasStorage = new AliasStorage($connection, $this->container
    ->get('module_handler'));

  // Test the situation where the source is the same for multiple aliases.
  // Start with a language-neutral alias, which we will override.
  $path = array(
    'source' => "/user/1",
    'alias' => '/foo',
  );
  $aliasStorage
    ->save($path['source'], $path['alias']);
  $this
    ->assertEqual($aliasManager
    ->getAliasByPath($path['source']), $path['alias'], 'Basic alias lookup works.');
  $this
    ->assertEqual($aliasManager
    ->getPathByAlias($path['alias']), $path['source'], 'Basic source lookup works.');

  // Create a language specific alias for the default language (English).
  $path = array(
    'source' => "/user/1",
    'alias' => "/users/Dries",
    'langcode' => 'en',
  );
  $aliasStorage
    ->save($path['source'], $path['alias'], $path['langcode']);

  // Hook that clears cache is not executed with unit tests.
  \Drupal::service('path.alias_manager')
    ->cacheClear();
  $this
    ->assertEqual($aliasManager
    ->getAliasByPath($path['source']), $path['alias'], 'English alias overrides language-neutral alias.');
  $this
    ->assertEqual($aliasManager
    ->getPathByAlias($path['alias']), $path['source'], 'English source overrides language-neutral source.');

  // Create a language-neutral alias for the same path, again.
  $path = array(
    'source' => "/user/1",
    'alias' => '/bar',
  );
  $aliasStorage
    ->save($path['source'], $path['alias']);
  $this
    ->assertEqual($aliasManager
    ->getAliasByPath($path['source']), "/users/Dries", 'English alias still returned after entering a language-neutral alias.');

  // Create a language-specific (xx-lolspeak) alias for the same path.
  $path = array(
    'source' => "/user/1",
    'alias' => '/LOL',
    'langcode' => 'xx-lolspeak',
  );
  $aliasStorage
    ->save($path['source'], $path['alias'], $path['langcode']);
  $this
    ->assertEqual($aliasManager
    ->getAliasByPath($path['source']), "/users/Dries", 'English alias still returned after entering a LOLspeak alias.');

  // The LOLspeak alias should be returned if we really want LOLspeak.
  $this
    ->assertEqual($aliasManager
    ->getAliasByPath($path['source'], 'xx-lolspeak'), '/LOL', '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 = array(
    'source' => "/user/1",
    'alias' => '/users/my-new-path',
    'langcode' => 'en',
  );
  $aliasStorage
    ->save($path['source'], $path['alias'], $path['langcode']);

  // Hook that clears cache is not executed with unit tests.
  $aliasManager
    ->cacheClear();
  $this
    ->assertEqual($aliasManager
    ->getAliasByPath($path['source']), $path['alias'], 'Recently created English alias returned.');
  $this
    ->assertEqual($aliasManager
    ->getPathByAlias($path['alias']), $path['source'], 'Recently created English source returned.');

  // Remove the English aliases, which should cause a fallback to the most
  // recently created language-neutral alias, 'bar'.
  $aliasStorage
    ->delete(array(
    'langcode' => 'en',
  ));

  // Hook that clears cache is not executed with unit tests.
  $aliasManager
    ->cacheClear();
  $this
    ->assertEqual($aliasManager
    ->getAliasByPath($path['source']), '/bar', '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.
  $aliasStorage
    ->save('/user/2', '/bar');

  // Hook that clears cache is not executed with unit tests.
  $aliasManager
    ->cacheClear();
  $this
    ->assertEqual($aliasManager
    ->getPathByAlias('/bar'), '/user/2', 'Newer alias record is returned when comparing two LanguageInterface::LANGCODE_NOT_SPECIFIED paths with the same alias.');
}