AliasTest.php 9.88 KB
Newer Older
1 2 3 4
<?php

/**
 * @file
5
 * Contains Drupal\system\Tests\Path\AliasTest.
6 7 8 9
 */

namespace Drupal\system\Tests\Path;

10
use Drupal\Core\Cache\MemoryCounterBackend;
11
use Drupal\Core\Path\AliasStorage;
12
use Drupal\Core\Database\Database;
13
use Drupal\Core\Path\AliasManager;
14
use Drupal\Core\Path\AliasWhitelist;
15 16 17 18

/**
 * Tests path alias CRUD and lookup functionality.
 */
19
class AliasTest extends PathUnitTestBase {
20 21 22 23 24 25 26 27 28 29 30 31 32 33

  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'),
    );
  }

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

34
    //Create Path object.
35
    $aliasStorage = new AliasStorage($connection, $this->container->get('module_handler'));
36 37 38 39 40

    $aliases = $this->fixtures->sampleUrlAliases();

    //Create a few aliases
    foreach ($aliases as $idx => $alias) {
41
      $aliasStorage->save($alias['source'], $alias['alias'], $alias['langcode']);
42 43 44 45 46 47 48 49 50 51 52 53 54

      $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'];
55
      $loadedAlias = $aliasStorage->load(array('pid' => $pid));
56 57 58 59 60
      $this->assertEqual($loadedAlias, $alias, format_string('Loaded the expected path with pid %pid.', array('%pid' => $pid)));
    }

    //Update a few aliases
    foreach ($aliases as $alias) {
61
      $aliasStorage->save($alias['source'], $alias['alias'] . '_updated', $alias['langcode'], $alias['pid']);
62 63 64 65 66 67 68 69 70 71

      $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'];
72
      $aliasStorage->delete(array('pid' => $pid));
73 74 75 76 77 78 79 80 81 82 83 84 85 86

      $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.
87
    $aliasManager = $this->container->get('path.alias_manager');
88
    $aliasStorage = new AliasStorage($connection, $this->container->get('module_handler'));
89 90 91 92 93 94 95 96

    // 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',
    );

97
    $aliasStorage->save($path['source'], $path['alias']);
98 99
    $this->assertEqual($aliasManager->getAliasByPath($path['source']), $path['alias'], 'Basic alias lookup works.');
    $this->assertEqual($aliasManager->getPathByAlias($path['alias']), $path['source'], 'Basic source lookup works.');
100 101 102 103 104 105 106

    // Create a language specific alias for the default language (English).
    $path = array(
      'source' => "user/1",
      'alias' => "users/Dries",
      'langcode' => 'en',
    );
107
    $aliasStorage->save($path['source'], $path['alias'], $path['langcode']);
108
    // Hook that clears cache is not executed with unit tests.
109
    \Drupal::service('path.alias_manager')->cacheClear();
110 111
    $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.');
112 113 114 115 116 117

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

    // Create a language-specific (xx-lolspeak) alias for the same path.
    $path = array(
      'source' => "user/1",
      'alias' => 'LOL',
      'langcode' => 'xx-lolspeak',
    );
127
    $aliasStorage->save($path['source'], $path['alias'], $path['langcode']);
128
    $this->assertEqual($aliasManager->getAliasByPath($path['source']), "users/Dries", 'English alias still returned after entering a LOLspeak alias.');
129
    // The LOLspeak alias should be returned if we really want LOLspeak.
130
    $this->assertEqual($aliasManager->getAliasByPath($path['source'], 'xx-lolspeak'), 'LOL', 'LOLspeak alias returned if we specify xx-lolspeak to the alias manager.');
131 132 133 134 135 136 137 138

    // 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',
    );
139
    $aliasStorage->save($path['source'], $path['alias'], $path['langcode']);
140 141
    // Hook that clears cache is not executed with unit tests.
    $aliasManager->cacheClear();
142 143
    $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.');
144 145 146

    // Remove the English aliases, which should cause a fallback to the most
    // recently created language-neutral alias, 'bar'.
147
    $aliasStorage->delete(array('langcode' => 'en'));
148 149
    // Hook that clears cache is not executed with unit tests.
    $aliasManager->cacheClear();
150
    $this->assertEqual($aliasManager->getAliasByPath($path['source']), 'bar', 'Path lookup falls back to recently created language-neutral alias.');
151 152 153

    // Test the situation where the alias and language are the same, but
    // the source differs. The newer alias record should be returned.
154
    $aliasStorage->save('user/2', 'bar');
155 156
    // Hook that clears cache is not executed with unit tests.
    $aliasManager->cacheClear();
157
    $this->assertEqual($aliasManager->getPathByAlias('bar'), 'user/2', 'Newer alias record is returned when comparing two Language::LANGCODE_NOT_SPECIFIED paths with the same alias.');
158
  }
159 160 161 162 163 164 165 166

  /**
   * Tests the alias whitelist.
   */
  function testWhitelist() {
    // Prepare database table.
    $connection = Database::getConnection();
    $this->fixtures->createTables($connection);
167

168
    $memoryCounterBackend = new MemoryCounterBackend('default');
169

170
    // Create AliasManager and Path object.
171
    $aliasStorage = new AliasStorage($connection, $this->container->get('module_handler'));
172
    $whitelist = new AliasWhitelist('path_alias_whitelist', $memoryCounterBackend, $this->container->get('lock'), $this->container->get('state'), $aliasStorage);
173
    $aliasManager = new AliasManager($aliasStorage, $whitelist, $this->container->get('language_manager'), $memoryCounterBackend);
174 175

    // No alias for user and admin yet, so should be NULL.
176 177
    $this->assertNull($whitelist->get('user'));
    $this->assertNull($whitelist->get('admin'));
178 179 180

    // Non-existing path roots should be NULL too. Use a length of 7 to avoid
    // possible conflict with random aliases below.
181
    $this->assertNull($whitelist->get($this->randomName()));
182 183

    // Add an alias for user/1, user should get whitelisted now.
184
    $aliasStorage->save('user/1', $this->randomName());
185
    $aliasManager->cacheClear();
186 187 188
    $this->assertTrue($whitelist->get('user'));
    $this->assertNull($whitelist->get('admin'));
    $this->assertNull($whitelist->get($this->randomName()));
189 190

    // Add an alias for admin, both should get whitelisted now.
191
    $aliasStorage->save('admin/something', $this->randomName());
192
    $aliasManager->cacheClear();
193 194 195
    $this->assertTrue($whitelist->get('user'));
    $this->assertTrue($whitelist->get('admin'));
    $this->assertNull($whitelist->get($this->randomName()));
196 197

    // Remove the user alias again, whitelist entry should be removed.
198
    $aliasStorage->delete(array('source' => 'user/1'));
199
    $aliasManager->cacheClear();
200 201 202
    $this->assertNull($whitelist->get('user'));
    $this->assertTrue($whitelist->get('admin'));
    $this->assertNull($whitelist->get($this->randomName()));
203

204 205 206 207 208 209 210
    // Destruct the whitelist so that the caches are written.
    $whitelist->destruct();
    $this->assertEqual($memoryCounterBackend->getCounter('set', 'path_alias_whitelist'), 1);
    $memoryCounterBackend->resetCounter();

    // Re-initialize the whitelist using the same cache backend, should load
    // from cache.
211
    $whitelist = new AliasWhitelist('path_alias_whitelist', $memoryCounterBackend, $this->container->get('lock'), $this->container->get('state'), $aliasStorage);
212 213 214 215 216 217 218 219 220 221
    $this->assertNull($whitelist->get('user'));
    $this->assertTrue($whitelist->get('admin'));
    $this->assertNull($whitelist->get($this->randomName()));
    $this->assertEqual($memoryCounterBackend->getCounter('get', 'path_alias_whitelist'), 1);
    $this->assertEqual($memoryCounterBackend->getCounter('set', 'path_alias_whitelist'), 0);

    // Destruct the whitelist, should not attempt to write the cache again.
    $whitelist->destruct();
    $this->assertEqual($memoryCounterBackend->getCounter('get', 'path_alias_whitelist'), 1);
    $this->assertEqual($memoryCounterBackend->getCounter('set', 'path_alias_whitelist'), 0);
222
  }
223

224
}