You are here

function AliasTest::testCRUD in Zircon Profile 8.0

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

File

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

Class

AliasTest
Tests path alias CRUD and lookup functionality.

Namespace

Drupal\system\Tests\Path

Code

function testCRUD() {

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

  //Create Path object.
  $aliasStorage = new AliasStorage($connection, $this->container
    ->get('module_handler'));
  $aliases = $this->fixtures
    ->sampleUrlAliases();

  //Create a few aliases
  foreach ($aliases as $idx => $alias) {
    $aliasStorage
      ->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 = $aliasStorage
      ->load(array(
      'pid' => $pid,
    ));
    $this
      ->assertEqual($loadedAlias, $alias, format_string('Loaded the expected path with pid %pid.', array(
      '%pid' => $pid,
    )));
  }

  // Load alias by source path.
  $loadedAlias = $aliasStorage
    ->load(array(
    'source' => '/node/1',
  ));
  $this
    ->assertEqual($loadedAlias['alias'], '/alias_for_node_1_und', 'The last created alias loaded by default.');

  //Update a few aliases
  foreach ($aliases as $alias) {
    $fields = $aliasStorage
      ->save($alias['source'], $alias['alias'] . '_updated', $alias['langcode'], $alias['pid']);
    $this
      ->assertEqual($alias['alias'], $fields['original']['alias']);
    $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'];
    $aliasStorage
      ->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,
    )));
  }
}