class AliasTest

Tests path alias CRUD and lookup functionality.

Hierarchy

Expanded class hierarchy of AliasTest

File

drupal/core/modules/system/lib/Drupal/system/Tests/Path/AliasTest.php, line 18
Definition of Drupal\system\Tests\Path\CrudTest.

Namespace

Drupal\system\Tests\Path
View source
class AliasTest extends DrupalUnitTestBase {
  public static function getInfo() {
    return array(
      'name' => t('Path Alias Unit Tests'),
      'description' => t('Tests path alias CRUD and lookup functionality.'),
      'group' => t('Path API'),
    );
  }
  public function setUp() {
    parent::setUp();
    $this->fixtures = new UrlAliasFixtures();
  }
  public function tearDown() {
    $this->fixtures
      ->dropTables(Database::getConnection());
    parent::tearDown();
  }
  function testCRUD() {

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

    //Create AliasManager and Path object.
    $aliasManager = new AliasManager($connection, $this->container
      ->get('keyvalue'));
    $path = new Path($connection, $aliasManager);
    $aliases = $this->fixtures
      ->sampleUrlAliases();

    //Create a few aliases
    foreach ($aliases as $idx => $alias) {
      $path
        ->save($alias['source'], $alias['alias'], $alias['langcode']);
      $result = $connection
        ->query('SELECT * FROM {url_alias} WHERE source = :source AND alias= :alias AND langcode = :langcode', array(
        ':source' => $alias['source'],
        ':alias' => $alias['alias'],
        ':langcode' => $alias['langcode'],
      ));
      $rows = $result
        ->fetchAll();
      $this
        ->assertEqual(count($rows), 1, format_string('Created an entry for %alias.', array(
        '%alias' => $alias['alias'],
      )));

      //Cache the pid for further tests.
      $aliases[$idx]['pid'] = $rows[0]->pid;
    }

    //Load a few aliases
    foreach ($aliases as $alias) {
      $pid = $alias['pid'];
      $loadedAlias = $path
        ->load(array(
        'pid' => $pid,
      ));
      $this
        ->assertEqual($loadedAlias, $alias, format_string('Loaded the expected path with pid %pid.', array(
        '%pid' => $pid,
      )));
    }

    //Update a few aliases
    foreach ($aliases as $alias) {
      $path
        ->save($alias['source'], $alias['alias'] . '_updated', $alias['langcode'], $alias['pid']);
      $result = $connection
        ->query('SELECT pid FROM {url_alias} WHERE source = :source AND alias= :alias AND langcode = :langcode', array(
        ':source' => $alias['source'],
        ':alias' => $alias['alias'] . '_updated',
        ':langcode' => $alias['langcode'],
      ));
      $pid = $result
        ->fetchField();
      $this
        ->assertEqual($pid, $alias['pid'], format_string('Updated entry for pid %pid.', array(
        '%pid' => $pid,
      )));
    }

    //Delete a few aliases
    foreach ($aliases as $alias) {
      $pid = $alias['pid'];
      $path
        ->delete(array(
        'pid' => $pid,
      ));
      $result = $connection
        ->query('SELECT * FROM {url_alias} WHERE pid = :pid', array(
        ':pid' => $pid,
      ));
      $rows = $result
        ->fetchAll();
      $this
        ->assertEqual(count($rows), 0, format_string('Deleted entry with pid %pid.', array(
        '%pid' => $pid,
      )));
    }
  }
  function testLookupPath() {

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

    //Create AliasManager and Path object.
    $aliasManager = new AliasManager($connection, $this->container
      ->get('keyvalue'));
    $pathObject = new Path($connection, $aliasManager);

    // 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',
    );
    $pathObject
      ->save($path['source'], $path['alias']);
    $this
      ->assertEqual($aliasManager
      ->getPathAlias($path['source']), $path['alias'], 'Basic alias lookup works.');
    $this
      ->assertEqual($aliasManager
      ->getSystemPath($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',
    );
    $pathObject
      ->save($path['source'], $path['alias'], $path['langcode']);
    $this
      ->assertEqual($aliasManager
      ->getPathAlias($path['source']), $path['alias'], 'English alias overrides language-neutral alias.');
    $this
      ->assertEqual($aliasManager
      ->getSystemPath($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',
    );
    $pathObject
      ->save($path['source'], $path['alias']);
    $this
      ->assertEqual($aliasManager
      ->getPathAlias($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',
    );
    $pathObject
      ->save($path['source'], $path['alias'], $path['langcode']);
    $this
      ->assertEqual($aliasManager
      ->getPathAlias($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
      ->getPathAlias($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',
    );
    $pathObject
      ->save($path['source'], $path['alias'], $path['langcode']);
    $this
      ->assertEqual($aliasManager
      ->getPathAlias($path['source']), $path['alias'], 'Recently created English alias returned.');
    $this
      ->assertEqual($aliasManager
      ->getSystemPath($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'.
    $pathObject
      ->delete(array(
      'langcode' => 'en',
    ));
    $this
      ->assertEqual($aliasManager
      ->getPathAlias($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.
    $pathObject
      ->save('user/2', 'bar');
    $this
      ->assertEqual($aliasManager
      ->getSystemPath('bar'), 'user/2', 'Newer alias record is returned when comparing two LANGUAGE_NOT_SPECIFIED paths with the same alias.');
  }

}

Members

Namesort descending Modifiers Type Description Overrides
AliasTest::getInfo public static function
AliasTest::setUp public function Sets up Drupal unit test environment. Overrides DrupalUnitTestBase::setUp
AliasTest::tearDown public function Deletes created files, database tables, and reverts all environment changes. Overrides TestBase::tearDown
AliasTest::testCRUD function
AliasTest::testLookupPath function
DrupalUnitTestBase::$moduleFiles private property
DrupalUnitTestBase::$moduleList private property Fixed module list being used by this test.
DrupalUnitTestBase::$modules public static property Modules to enable. 6
DrupalUnitTestBase::$themeData private property
DrupalUnitTestBase::$themeFiles private property
DrupalUnitTestBase::containerBuild public function Sets up the base service container for this test.
DrupalUnitTestBase::enableModules protected function Enables modules for this test.
DrupalUnitTestBase::installSchema protected function Installs a specific table from a module schema definition.
TestBase::$assertions protected property Assertions thrown in that test case.
TestBase::$databasePrefix protected property The database prefix of this test run.
TestBase::$originalFileDirectory protected property The original file directory, before it was changed for testing purposes.
TestBase::$originalPrefix protected property The original database prefix when running inside Simpletest.
TestBase::$results public property Current results of this test case.
TestBase::$setup protected property Flag to indicate whether the test has been set up.
TestBase::$setupDatabasePrefix protected property
TestBase::$setupEnvironment protected property
TestBase::$skipClasses protected property This class is skipped when looking for the source of an assertion.
TestBase::$testId protected property The test run ID.
TestBase::$timeLimit protected property Time limit for the test.
TestBase::$verbose protected property TRUE if verbose debugging is enabled.
TestBase::$verboseClassName protected property Safe class name for use in verbose output filenames.
TestBase::$verboseDirectory protected property Directory where verbose output files are put.
TestBase::$verboseDirectoryUrl protected property URL to the verbose output file directory.
TestBase::$verboseId protected property Incrementing identifier for verbose output filenames.
TestBase::assert protected function Internal helper: stores the assert.
TestBase::assertEqual protected function Check to see if two values are equal.
TestBase::assertFalse protected function Check to see if a value is false (an empty string, 0, NULL, or FALSE).
TestBase::assertIdentical protected function Check to see if two values are identical.
TestBase::assertIdenticalObject protected function Checks to see if two objects are identical.
TestBase::assertNotEqual protected function Check to see if two values are not equal.
TestBase::assertNotIdentical protected function Check to see if two values are not identical.
TestBase::assertNotNull protected function Check to see if a value is not NULL.
TestBase::assertNull protected function Check to see if a value is NULL.
TestBase::assertTrue protected function Check to see if a value is not false (not an empty string, 0, NULL, or FALSE).
TestBase::changeDatabasePrefix protected function Changes the database connection to the prefixed one.
TestBase::checkRequirements protected function Checks the matching requirements for Test. 3
TestBase::deleteAssert public static function Delete an assertion record by message ID.
TestBase::error protected function Fire an error assertion. 1
TestBase::errorHandler public function Handle errors during test runs.
TestBase::exceptionHandler protected function Handle exceptions.
TestBase::fail protected function Fire an assertion that is always negative.
TestBase::filePreDeleteCallback public static function Ensures test files are deletable within file_unmanaged_delete_recursive().
TestBase::generatePermutations public static function Converts a list of possible parameters into a stack of permutations.
TestBase::getAssertionCall protected function Cycles through backtrace until the first non-assertion method is found.
TestBase::getDatabaseConnection public static function Returns the database connection to the site running Simpletest.
TestBase::insertAssert public static function Store an assertion from outside the testing context.
TestBase::pass protected function Fire an assertion that is always positive.
TestBase::prepareDatabasePrefix protected function Generates a database prefix for running tests.
TestBase::prepareEnvironment protected function Prepares the current environment for running the test.
TestBase::randomName public static function Generates a random string containing letters and numbers.
TestBase::randomObject public static function Generates a random PHP object.
TestBase::randomString public static function Generates a random string of ASCII characters of codes 32 to 126.
TestBase::rebuildContainer protected function Rebuild drupal_container().
TestBase::run public function Run all tests in this class.
TestBase::verbose protected function Logs verbose message in a text file.
UnitTestBase::$configDirectories protected property
UnitTestBase::__construct function Constructor for UnitTestBase. Overrides TestBase::__construct 6