1 path.test PathLookupTest::testBackdropLookupPath()

Test that backdrop_lookup_path() returns the correct path.

File

core/modules/simpletest/tests/path.test, line 227
Tests for path.inc.

Class

PathLookupTest
Unit test for backdrop_lookup_path().

Code

function testBackdropLookupPath() {
  $account = $this->backdropCreateUser();
  $uid = $account->uid;
  $name = $account->name;

  // 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/$uid",
    'alias' => 'foo',
  );
  path_save($path);
  $this->assertEqual(backdrop_lookup_path('alias', $path['source']), $path['alias'], 'Basic alias lookup works.');
  $this->assertEqual(backdrop_lookup_path('source', $path['alias']), $path['source'], 'Basic source lookup works.');

  // Create a language specific alias for the default language (English).
  $path = array(
    'source' => "user/$uid",
    'alias' => "users/$name",
    'langcode' => 'en',
  );
  path_save($path);
  $this->assertEqual(backdrop_lookup_path('alias', $path['source']), $path['alias'], 'English alias overrides language-neutral alias.');
  $this->assertEqual(backdrop_lookup_path('source', $path['alias']), $path['source'], 'English source overrides language-neutral source.');

  // Create a language-neutral alias for the same path, again.
  $path = array(
    'source' => "user/$uid",
    'alias' => 'bar',
  );
  path_save($path);
  $this->assertEqual(backdrop_lookup_path('alias', $path['source']), "users/$name", '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/$uid",
    'alias' => 'LOL',
    'langcode' => 'xx-lolspeak',
  );
  path_save($path);
  $this->assertEqual(backdrop_lookup_path('alias', $path['source']), "users/$name", 'English alias still returned after entering a LOLspeak alias.');
  // The LOLspeak alias should be returned if we really want LOLspeak.
  $this->assertEqual(backdrop_lookup_path('alias', $path['source'], 'xx-lolspeak'), 'LOL', 'LOLspeak alias returned if we specify xx-lolspeak to backdrop_lookup_path().');

  // Create a new alias for this path in English, which should override the
  // previous alias for "user/$uid".
  $path = array(
    'source' => "user/$uid",
    'alias' => 'users/my-new-path',
    'langcode' => 'en',
  );
  path_save($path);
  $this->assertEqual(backdrop_lookup_path('alias', $path['source']), $path['alias'], 'Recently created English alias returned.');
  $this->assertEqual(backdrop_lookup_path('source', $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'.
  db_delete('url_alias')
    ->condition('langcode', 'en')
    ->execute();
  backdrop_clear_path_cache();
  $this->assertEqual(backdrop_lookup_path('alias', $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.
  $account2 = $this->backdropCreateUser();
  $path = array(
    'source' => 'user/' . $account2->uid,
    'alias' => 'bar',
  );
  path_save($path);
  $this->assertEqual(backdrop_lookup_path('source', $path['alias']), $path['source'], 'Newer alias record is returned when comparing two LANGUAGE_NONE paths with the same alias.');
}