3 namespace Drupal\KernelTests\Core\Path;
5 use Drupal\Core\Cache\MemoryCounterBackend;
6 use Drupal\Core\Path\AliasStorage;
7 use Drupal\Core\Database\Database;
8 use Drupal\Core\Path\AliasManager;
9 use Drupal\Core\Path\AliasWhitelist;
12 * Tests path alias CRUD and lookup functionality.
16 class AliasTest extends PathUnitTestBase {
18 public function testCRUD() {
19 //Prepare database table.
20 $connection = Database::getConnection();
21 $this->fixtures->createTables($connection);
24 $aliasStorage = new AliasStorage($connection, $this->container->get('module_handler'));
26 $aliases = $this->fixtures->sampleUrlAliases();
28 //Create a few aliases
29 foreach ($aliases as $idx => $alias) {
30 $aliasStorage->save($alias['source'], $alias['alias'], $alias['langcode']);
32 $result = $connection->query('SELECT * FROM {url_alias} WHERE source = :source AND alias= :alias AND langcode = :langcode', [':source' => $alias['source'], ':alias' => $alias['alias'], ':langcode' => $alias['langcode']]);
33 $rows = $result->fetchAll();
35 $this->assertEqual(count($rows), 1, format_string('Created an entry for %alias.', ['%alias' => $alias['alias']]));
37 //Cache the pid for further tests.
38 $aliases[$idx]['pid'] = $rows[0]->pid;
42 foreach ($aliases as $alias) {
44 $loadedAlias = $aliasStorage->load(['pid' => $pid]);
45 $this->assertEqual($loadedAlias, $alias, format_string('Loaded the expected path with pid %pid.', ['%pid' => $pid]));
48 // Load alias by source path.
49 $loadedAlias = $aliasStorage->load(['source' => '/node/1']);
50 $this->assertEqual($loadedAlias['alias'], '/alias_for_node_1_und', 'The last created alias loaded by default.');
52 //Update a few aliases
53 foreach ($aliases as $alias) {
54 $fields = $aliasStorage->save($alias['source'], $alias['alias'] . '_updated', $alias['langcode'], $alias['pid']);
56 $this->assertEqual($alias['alias'], $fields['original']['alias']);
58 $result = $connection->query('SELECT pid FROM {url_alias} WHERE source = :source AND alias= :alias AND langcode = :langcode', [':source' => $alias['source'], ':alias' => $alias['alias'] . '_updated', ':langcode' => $alias['langcode']]);
59 $pid = $result->fetchField();
61 $this->assertEqual($pid, $alias['pid'], format_string('Updated entry for pid %pid.', ['%pid' => $pid]));
64 //Delete a few aliases
65 foreach ($aliases as $alias) {
67 $aliasStorage->delete(['pid' => $pid]);
69 $result = $connection->query('SELECT * FROM {url_alias} WHERE pid = :pid', [':pid' => $pid]);
70 $rows = $result->fetchAll();
72 $this->assertEqual(count($rows), 0, format_string('Deleted entry with pid %pid.', ['%pid' => $pid]));
76 public function testLookupPath() {
77 //Prepare database table.
78 $connection = Database::getConnection();
79 $this->fixtures->createTables($connection);
81 //Create AliasManager and Path object.
82 $aliasManager = $this->container->get('path.alias_manager');
83 $aliasStorage = new AliasStorage($connection, $this->container->get('module_handler'));
85 // Test the situation where the source is the same for multiple aliases.
86 // Start with a language-neutral alias, which we will override.
88 'source' => "/user/1",
92 $aliasStorage->save($path['source'], $path['alias']);
93 $this->assertEqual($aliasManager->getAliasByPath($path['source']), $path['alias'], 'Basic alias lookup works.');
94 $this->assertEqual($aliasManager->getPathByAlias($path['alias']), $path['source'], 'Basic source lookup works.');
96 // Create a language specific alias for the default language (English).
98 'source' => "/user/1",
99 'alias' => "/users/Dries",
102 $aliasStorage->save($path['source'], $path['alias'], $path['langcode']);
103 // Hook that clears cache is not executed with unit tests.
104 \Drupal::service('path.alias_manager')->cacheClear();
105 $this->assertEqual($aliasManager->getAliasByPath($path['source']), $path['alias'], 'English alias overrides language-neutral alias.');
106 $this->assertEqual($aliasManager->getPathByAlias($path['alias']), $path['source'], 'English source overrides language-neutral source.');
108 // Create a language-neutral alias for the same path, again.
110 'source' => "/user/1",
113 $aliasStorage->save($path['source'], $path['alias']);
114 $this->assertEqual($aliasManager->getAliasByPath($path['source']), "/users/Dries", 'English alias still returned after entering a language-neutral alias.');
116 // Create a language-specific (xx-lolspeak) alias for the same path.
118 'source' => "/user/1",
120 'langcode' => 'xx-lolspeak',
122 $aliasStorage->save($path['source'], $path['alias'], $path['langcode']);
123 $this->assertEqual($aliasManager->getAliasByPath($path['source']), "/users/Dries", 'English alias still returned after entering a LOLspeak alias.');
124 // The LOLspeak alias should be returned if we really want LOLspeak.
125 $this->assertEqual($aliasManager->getAliasByPath($path['source'], 'xx-lolspeak'), '/LOL', 'LOLspeak alias returned if we specify xx-lolspeak to the alias manager.');
127 // Create a new alias for this path in English, which should override the
128 // previous alias for "user/1".
130 'source' => "/user/1",
131 'alias' => '/users/my-new-path',
134 $aliasStorage->save($path['source'], $path['alias'], $path['langcode']);
135 // Hook that clears cache is not executed with unit tests.
136 $aliasManager->cacheClear();
137 $this->assertEqual($aliasManager->getAliasByPath($path['source']), $path['alias'], 'Recently created English alias returned.');
138 $this->assertEqual($aliasManager->getPathByAlias($path['alias']), $path['source'], 'Recently created English source returned.');
140 // Remove the English aliases, which should cause a fallback to the most
141 // recently created language-neutral alias, 'bar'.
142 $aliasStorage->delete(['langcode' => 'en']);
143 // Hook that clears cache is not executed with unit tests.
144 $aliasManager->cacheClear();
145 $this->assertEqual($aliasManager->getAliasByPath($path['source']), '/bar', 'Path lookup falls back to recently created language-neutral alias.');
147 // Test the situation where the alias and language are the same, but
148 // the source differs. The newer alias record should be returned.
149 $aliasStorage->save('/user/2', '/bar');
150 // Hook that clears cache is not executed with unit tests.
151 $aliasManager->cacheClear();
152 $this->assertEqual($aliasManager->getPathByAlias('/bar'), '/user/2', 'Newer alias record is returned when comparing two LanguageInterface::LANGCODE_NOT_SPECIFIED paths with the same alias.');
156 * Tests the alias whitelist.
158 public function testWhitelist() {
159 // Prepare database table.
160 $connection = Database::getConnection();
161 $this->fixtures->createTables($connection);
163 $memoryCounterBackend = new MemoryCounterBackend();
165 // Create AliasManager and Path object.
166 $aliasStorage = new AliasStorage($connection, $this->container->get('module_handler'));
167 $whitelist = new AliasWhitelist('path_alias_whitelist', $memoryCounterBackend, $this->container->get('lock'), $this->container->get('state'), $aliasStorage);
168 $aliasManager = new AliasManager($aliasStorage, $whitelist, $this->container->get('language_manager'), $memoryCounterBackend);
170 // No alias for user and admin yet, so should be NULL.
171 $this->assertNull($whitelist->get('user'));
172 $this->assertNull($whitelist->get('admin'));
174 // Non-existing path roots should be NULL too. Use a length of 7 to avoid
175 // possible conflict with random aliases below.
176 $this->assertNull($whitelist->get($this->randomMachineName()));
178 // Add an alias for user/1, user should get whitelisted now.
179 $aliasStorage->save('/user/1', '/' . $this->randomMachineName());
180 $aliasManager->cacheClear();
181 $this->assertTrue($whitelist->get('user'));
182 $this->assertNull($whitelist->get('admin'));
183 $this->assertNull($whitelist->get($this->randomMachineName()));
185 // Add an alias for admin, both should get whitelisted now.
186 $aliasStorage->save('/admin/something', '/' . $this->randomMachineName());
187 $aliasManager->cacheClear();
188 $this->assertTrue($whitelist->get('user'));
189 $this->assertTrue($whitelist->get('admin'));
190 $this->assertNull($whitelist->get($this->randomMachineName()));
192 // Remove the user alias again, whitelist entry should be removed.
193 $aliasStorage->delete(['source' => '/user/1']);
194 $aliasManager->cacheClear();
195 $this->assertNull($whitelist->get('user'));
196 $this->assertTrue($whitelist->get('admin'));
197 $this->assertNull($whitelist->get($this->randomMachineName()));
199 // Destruct the whitelist so that the caches are written.
200 $whitelist->destruct();
201 $this->assertEqual($memoryCounterBackend->getCounter('set', 'path_alias_whitelist'), 1);
202 $memoryCounterBackend->resetCounter();
204 // Re-initialize the whitelist using the same cache backend, should load
206 $whitelist = new AliasWhitelist('path_alias_whitelist', $memoryCounterBackend, $this->container->get('lock'), $this->container->get('state'), $aliasStorage);
207 $this->assertNull($whitelist->get('user'));
208 $this->assertTrue($whitelist->get('admin'));
209 $this->assertNull($whitelist->get($this->randomMachineName()));
210 $this->assertEqual($memoryCounterBackend->getCounter('get', 'path_alias_whitelist'), 1);
211 $this->assertEqual($memoryCounterBackend->getCounter('set', 'path_alias_whitelist'), 0);
213 // Destruct the whitelist, should not attempt to write the cache again.
214 $whitelist->destruct();
215 $this->assertEqual($memoryCounterBackend->getCounter('get', 'path_alias_whitelist'), 1);
216 $this->assertEqual($memoryCounterBackend->getCounter('set', 'path_alias_whitelist'), 0);
220 * Tests situation where the whitelist cache is deleted mid-request.
222 public function testWhitelistCacheDeletionMidRequest() {
223 // Prepare database table.
224 $connection = Database::getConnection();
225 $this->fixtures->createTables($connection);
227 $memoryCounterBackend = new MemoryCounterBackend();
229 // Create AliasManager and Path object.
230 $aliasStorage = new AliasStorage($connection, $this->container->get('module_handler'));
231 $whitelist = new AliasWhitelist('path_alias_whitelist', $memoryCounterBackend, $this->container->get('lock'), $this->container->get('state'), $aliasStorage);
232 $aliasManager = new AliasManager($aliasStorage, $whitelist, $this->container->get('language_manager'), $memoryCounterBackend);
234 // Whitelist cache should not exist at all yet.
235 $this->assertFalse($memoryCounterBackend->get('path_alias_whitelist'));
237 // Add some aliases for both menu routes we have.
238 $aliasStorage->save('/admin/something', '/' . $this->randomMachineName());
239 $aliasStorage->save('/user/something', '/' . $this->randomMachineName());
240 $aliasManager->cacheClear();
242 // Lookup admin path in whitelist. It will query the DB and figure out
243 // that it indeed has an alias, and add it to the internal whitelist and
244 // flag it to be peristed to cache.
245 $this->assertTrue($whitelist->get('admin'));
247 // Destruct the whitelist so it persists its cache.
248 $whitelist->destruct();
249 $this->assertEquals($memoryCounterBackend->getCounter('set', 'path_alias_whitelist'), 1);
250 // Cache data should have data for 'user' and 'admin', even though just
251 // 'admin' was looked up. This is because the cache is primed with all
252 // menu router base paths.
253 $this->assertEquals(['user' => FALSE, 'admin' => TRUE], $memoryCounterBackend->get('path_alias_whitelist')->data);
254 $memoryCounterBackend->resetCounter();
256 // Re-initialize the the whitelist and lookup an alias for the 'user' path.
257 // Whitelist should load data from its cache, see that it hasn't done a
258 // check for 'user' yet, perform the check, then mark the result to be
259 // persisted to cache.
260 $whitelist = new AliasWhitelist('path_alias_whitelist', $memoryCounterBackend, $this->container->get('lock'), $this->container->get('state'), $aliasStorage);
261 $this->assertTrue($whitelist->get('user'));
263 // Delete the whitelist cache. This could happen from an outside process,
264 // like a code deployment that performs a cache rebuild.
265 $memoryCounterBackend->delete('path_alias_whitelist');
267 // Destruct whitelist so it attempts to save the whitelist data to cache.
268 // However it should recognize that the previous cache entry was deleted
269 // from underneath it and not save anything to cache, to protect from
271 $whitelist->destruct();
272 $this->assertEquals($memoryCounterBackend->getCounter('set', 'path_alias_whitelist'), 0);
273 $this->assertFalse($memoryCounterBackend->get('path_alias_whitelist'));
274 $memoryCounterBackend->resetCounter();