3 namespace Drupal\Tests\path\Functional;
5 use Drupal\Core\Cache\Cache;
6 use Drupal\Core\Database\Database;
10 * Add, edit, delete, and change alias and verify its consistency in the
15 class PathAliasTest extends PathTestBase {
22 public static $modules = ['path'];
24 protected function setUp() {
27 // Create test user and log in.
28 $web_user = $this->drupalCreateUser(['create page content', 'edit own page content', 'administer url aliases', 'create url aliases', 'access content overview']);
29 $this->drupalLogin($web_user);
33 * Tests the path cache.
35 public function testPathCache() {
37 $node1 = $this->drupalCreateNode();
41 $edit['source'] = '/node/' . $node1->id();
42 $edit['alias'] = '/' . $this->randomMachineName(8);
43 $this->drupalPostForm('admin/config/search/path/add', $edit, t('Save'));
45 // Check the path alias whitelist cache.
46 $whitelist = \Drupal::cache('bootstrap')->get('path_alias_whitelist');
47 $this->assertTrue($whitelist->data['node']);
48 $this->assertFalse($whitelist->data['admin']);
50 // Visit the system path for the node and confirm a cache entry is
52 \Drupal::cache('data')->deleteAll();
53 // Make sure the path is not converted to the alias.
54 $this->drupalGet(trim($edit['source'], '/'), ['alias' => TRUE]);
55 $this->assertTrue(\Drupal::cache('data')->get('preload-paths:' . $edit['source']), 'Cache entry was created.');
57 // Visit the alias for the node and confirm a cache entry is created.
58 \Drupal::cache('data')->deleteAll();
59 // @todo Remove this once https://www.drupal.org/node/2480077 lands.
60 Cache::invalidateTags(['rendered']);
61 $this->drupalGet(trim($edit['alias'], '/'));
62 $this->assertTrue(\Drupal::cache('data')->get('preload-paths:' . $edit['source']), 'Cache entry was created.');
66 * Tests alias functionality through the admin interfaces.
68 public function testAdminAlias() {
70 $node1 = $this->drupalCreateNode();
74 $edit['source'] = '/node/' . $node1->id();
75 $edit['alias'] = '/' . $this->getRandomGenerator()->word(8);
76 $this->drupalPostForm('admin/config/search/path/add', $edit, t('Save'));
78 // Confirm that the alias works.
79 $this->drupalGet($edit['alias']);
80 $this->assertText($node1->label(), 'Alias works.');
81 $this->assertResponse(200);
82 // Confirm that the alias works in a case-insensitive way.
83 $this->assertTrue(ctype_lower(ltrim($edit['alias'], '/')));
84 $this->drupalGet($edit['alias']);
85 $this->assertText($node1->label(), 'Alias works lower case.');
86 $this->assertResponse(200);
87 $this->drupalGet(mb_strtoupper($edit['alias']));
88 $this->assertText($node1->label(), 'Alias works upper case.');
89 $this->assertResponse(200);
91 // Change alias to one containing "exotic" characters.
92 $pid = $this->getPID($edit['alias']);
94 $previous = $edit['alias'];
95 // Lower-case letters.
96 $edit['alias'] = '/alias' .
97 // "Special" ASCII characters.
98 "- ._~!$'\"()*@[]?&+%#,;=:" .
99 // Characters that look like a percent-escaped string.
100 "%23%25%26%2B%2F%3F" .
101 // Characters from various non-ASCII alphabets.
103 $connection = Database::getConnection();
104 if ($connection->databaseType() != 'sqlite') {
105 // When using LIKE for case-insensitivity, the SQLite driver is
106 // currently unable to find the upper-case versions of non-ASCII
108 // @todo fix this in https://www.drupal.org/node/2607432
109 $edit['alias'] .= "ïвβéø";
111 $this->drupalPostForm('admin/config/search/path/edit/' . $pid, $edit, t('Save'));
113 // Confirm that the alias works.
114 $this->drupalGet(mb_strtoupper($edit['alias']));
115 $this->assertText($node1->label(), 'Changed alias works.');
116 $this->assertResponse(200);
118 $this->container->get('path.alias_manager')->cacheClear();
119 // Confirm that previous alias no longer works.
120 $this->drupalGet($previous);
121 $this->assertNoText($node1->label(), 'Previous alias no longer works.');
122 $this->assertResponse(404);
124 // Create second test node.
125 $node2 = $this->drupalCreateNode();
127 // Set alias to second test node.
128 $edit['source'] = '/node/' . $node2->id();
129 // leave $edit['alias'] the same
130 $this->drupalPostForm('admin/config/search/path/add', $edit, t('Save'));
132 // Confirm no duplicate was created.
133 $this->assertRaw(t('The alias %alias is already in use in this language.', ['%alias' => $edit['alias']]), 'Attempt to move alias was rejected.');
136 $edit_upper['alias'] = mb_strtoupper($edit['alias']);
137 $this->drupalPostForm('admin/config/search/path/add', $edit_upper, t('Save'));
138 $this->assertRaw(t('The alias %alias could not be added because it is already in use in this language with different capitalization: %stored_alias.', [
139 '%alias' => $edit_upper['alias'],
140 '%stored_alias' => $edit['alias'],
141 ]), 'Attempt to move upper-case alias was rejected.');
144 $this->drupalGet('admin/config/search/path/edit/' . $pid);
145 $this->clickLink(t('Delete'));
146 $this->assertRaw(t('Are you sure you want to delete path alias %name?', ['%name' => $edit['alias']]));
147 $this->drupalPostForm(NULL, [], t('Confirm'));
149 // Confirm that the alias no longer works.
150 $this->drupalGet($edit['alias']);
151 $this->assertNoText($node1->label(), 'Alias was successfully deleted.');
152 $this->assertResponse(404);
154 // Create a really long alias.
156 $edit['source'] = '/node/' . $node1->id();
157 $alias = '/' . $this->randomMachineName(128);
158 $edit['alias'] = $alias;
159 // The alias is shortened to 50 characters counting the ellipsis.
160 $truncated_alias = substr($alias, 0, 47);
161 $this->drupalPostForm('admin/config/search/path/add', $edit, t('Save'));
162 $this->assertNoText($alias, 'The untruncated alias was not found.');
163 // The 'truncated' alias will always be found.
164 $this->assertText($truncated_alias, 'The truncated alias was found.');
166 // Create third test node.
167 $node3 = $this->drupalCreateNode();
169 // Create absolute path alias.
171 $edit['source'] = '/node/' . $node3->id();
172 $node3_alias = '/' . $this->randomMachineName(8);
173 $edit['alias'] = $node3_alias;
174 $this->drupalPostForm('admin/config/search/path/add', $edit, t('Save'));
176 // Create fourth test node.
177 $node4 = $this->drupalCreateNode();
179 // Create alias with trailing slash.
181 $edit['source'] = '/node/' . $node4->id();
182 $node4_alias = '/' . $this->randomMachineName(8);
183 $edit['alias'] = $node4_alias . '/';
184 $this->drupalPostForm('admin/config/search/path/add', $edit, t('Save'));
186 // Confirm that the alias with trailing slash is not found.
187 $this->assertNoText($edit['alias'], 'The absolute alias was not found.');
188 // The alias without trailing flash is found.
189 $this->assertText(trim($edit['alias'], '/'), 'The alias without trailing slash was found.');
191 // Update an existing alias to point to a different source.
192 $pid = $this->getPID($node4_alias);
194 $edit['alias'] = $node4_alias;
195 $edit['source'] = '/node/' . $node2->id();
196 $this->drupalPostForm('admin/config/search/path/edit/' . $pid, $edit, t('Save'));
197 $this->assertText('The alias has been saved.');
198 $this->drupalGet($edit['alias']);
199 $this->assertNoText($node4->label(), 'Previous alias no longer works.');
200 $this->assertText($node2->label(), 'Alias works.');
201 $this->assertResponse(200);
203 // Update an existing alias to use a duplicate alias.
204 $pid = $this->getPID($node3_alias);
206 $edit['alias'] = $node4_alias;
207 $edit['source'] = '/node/' . $node3->id();
208 $this->drupalPostForm('admin/config/search/path/edit/' . $pid, $edit, t('Save'));
209 $this->assertRaw(t('The alias %alias is already in use in this language.', ['%alias' => $edit['alias']]));
211 // Create an alias without a starting slash.
212 $node5 = $this->drupalCreateNode();
215 $edit['source'] = 'node/' . $node5->id();
216 $node5_alias = $this->randomMachineName(8);
217 $edit['alias'] = $node5_alias . '/';
218 $this->drupalPostForm('admin/config/search/path/add', $edit, t('Save'));
220 $this->assertUrl('admin/config/search/path/add');
221 $this->assertText('The source path has to start with a slash.');
222 $this->assertText('The alias path has to start with a slash.');
226 * Tests alias functionality through the node interfaces.
228 public function testNodeAlias() {
230 $node1 = $this->drupalCreateNode();
234 $edit['path[0][alias]'] = '/' . $this->randomMachineName(8);
235 $this->drupalPostForm('node/' . $node1->id() . '/edit', $edit, t('Save'));
237 // Confirm that the alias works.
238 $this->drupalGet($edit['path[0][alias]']);
239 $this->assertText($node1->label(), 'Alias works.');
240 $this->assertResponse(200);
242 // Confirm the 'canonical' and 'shortlink' URLs.
243 $elements = $this->xpath("//link[contains(@rel, 'canonical') and contains(@href, '" . $edit['path[0][alias]'] . "')]");
244 $this->assertTrue(!empty($elements), 'Page contains canonical link URL.');
245 $elements = $this->xpath("//link[contains(@rel, 'shortlink') and contains(@href, 'node/" . $node1->id() . "')]");
246 $this->assertTrue(!empty($elements), 'Page contains shortlink URL.');
248 $previous = $edit['path[0][alias]'];
249 // Change alias to one containing "exotic" characters.
250 // Lower-case letters.
251 $edit['path[0][alias]'] = '/alias' .
252 // "Special" ASCII characters.
253 "- ._~!$'\"()*@[]?&+%#,;=:" .
254 // Characters that look like a percent-escaped string.
255 "%23%25%26%2B%2F%3F" .
256 // Characters from various non-ASCII alphabets.
258 $connection = Database::getConnection();
259 if ($connection->databaseType() != 'sqlite') {
260 // When using LIKE for case-insensitivity, the SQLite driver is
261 // currently unable to find the upper-case versions of non-ASCII
263 // @todo fix this in https://www.drupal.org/node/2607432
264 $edit['path[0][alias]'] .= "ïвβéø";
266 $this->drupalPostForm('node/' . $node1->id() . '/edit', $edit, t('Save'));
268 // Confirm that the alias works.
269 $this->drupalGet(mb_strtoupper($edit['path[0][alias]']));
270 $this->assertText($node1->label(), 'Changed alias works.');
271 $this->assertResponse(200);
273 // Make sure that previous alias no longer works.
274 $this->drupalGet($previous);
275 $this->assertNoText($node1->label(), 'Previous alias no longer works.');
276 $this->assertResponse(404);
278 // Create second test node.
279 $node2 = $this->drupalCreateNode();
281 // Set alias to second test node.
282 // Leave $edit['path[0][alias]'] the same.
283 $this->drupalPostForm('node/' . $node2->id() . '/edit', $edit, t('Save'));
285 // Confirm that the alias didn't make a duplicate.
286 $this->assertText(t('The alias is already in use.'), 'Attempt to moved alias was rejected.');
289 $this->drupalPostForm('node/' . $node1->id() . '/edit', ['path[0][alias]' => ''], t('Save'));
291 // Confirm that the alias no longer works.
292 $this->drupalGet($edit['path[0][alias]']);
293 $this->assertNoText($node1->label(), 'Alias was successfully deleted.');
294 $this->assertResponse(404);
296 // Create third test node.
297 $node3 = $this->drupalCreateNode();
299 // Set its path alias to an absolute path.
300 $edit = ['path[0][alias]' => '/' . $this->randomMachineName(8)];
301 $this->drupalPostForm('node/' . $node3->id() . '/edit', $edit, t('Save'));
303 // Confirm that the alias was converted to a relative path.
304 $this->drupalGet(trim($edit['path[0][alias]'], '/'));
305 $this->assertText($node3->label(), 'Alias became relative.');
306 $this->assertResponse(200);
308 // Create fourth test node.
309 $node4 = $this->drupalCreateNode();
311 // Set its path alias to have a trailing slash.
312 $edit = ['path[0][alias]' => '/' . $this->randomMachineName(8) . '/'];
313 $this->drupalPostForm('node/' . $node4->id() . '/edit', $edit, t('Save'));
315 // Confirm that the alias was converted to a relative path.
316 $this->drupalGet(trim($edit['path[0][alias]'], '/'));
317 $this->assertText($node4->label(), 'Alias trimmed trailing slash.');
318 $this->assertResponse(200);
320 // Create fifth test node.
321 $node5 = $this->drupalCreateNode();
324 $edit = ['path[0][alias]' => '/' . $this->randomMachineName(8)];
325 $this->drupalPostForm('node/' . $node5->id() . '/edit', $edit, t('Save'));
327 // Delete the node and check that the path alias is also deleted.
329 $path_alias = \Drupal::service('path.alias_storage')->lookupPathAlias('/node/' . $node5->id(), $node5->language()->getId());
330 $this->assertFalse($path_alias, 'Alias was successfully deleted when the referenced node was deleted.');
332 // Create sixth test node.
333 $node6 = $this->drupalCreateNode();
335 // Create an invalid alias with two leading slashes and verify that the
336 // extra slash is removed when the link is generated. This ensures that URL
337 // aliases cannot be used to inject external URLs.
338 // @todo The user interface should either display an error message or
339 // automatically trim these invalid aliases, rather than allowing them to
340 // be silently created, at which point the functional aspects of this
341 // test will need to be moved elsewhere and switch to using a
342 // programmatically-created alias instead.
343 $alias = $this->randomMachineName(8);
344 $edit = ['path[0][alias]' => '//' . $alias];
345 $this->drupalPostForm($node6->toUrl('edit-form'), $edit, t('Save'));
346 $this->drupalGet(Url::fromRoute('system.admin_content'));
347 // This checks the link href before clicking it, rather than using
348 // \Drupal\Tests\BrowserTestBase::assertSession()->addressEquals() after
349 // clicking it, because the test browser does not always preserve the
350 // correct number of slashes in the URL when it visits internal links;
351 // using \Drupal\Tests\BrowserTestBase::assertSession()->addressEquals()
352 // would actually make the test pass unconditionally on the testbot (or
353 // anywhere else where Drupal is installed in a subdirectory).
354 $link_xpath = $this->xpath('//a[normalize-space(text())=:label]', [':label' => $node6->getTitle()]);
355 $link_href = $link_xpath[0]->getAttribute('href');
356 $this->assertEquals($link_href, base_path() . $alias);
357 $this->clickLink($node6->getTitle());
358 $this->assertResponse(404);
362 * Returns the path ID.
364 * @param string $alias
365 * A string containing an aliased path.
368 * Integer representing the path ID.
370 public function getPID($alias) {
371 return db_query("SELECT pid FROM {url_alias} WHERE alias = :alias", [':alias' => $alias])->fetchField();
375 * Tests that duplicate aliases fail validation.
377 public function testDuplicateNodeAlias() {
378 // Create one node with a random alias.
379 $node_one = $this->drupalCreateNode();
381 $edit['path[0][alias]'] = '/' . $this->randomMachineName();
382 $this->drupalPostForm('node/' . $node_one->id() . '/edit', $edit, t('Save'));
384 // Now create another node and try to set the same alias.
385 $node_two = $this->drupalCreateNode();
386 $this->drupalPostForm('node/' . $node_two->id() . '/edit', $edit, t('Save'));
387 $this->assertText(t('The alias is already in use.'));
388 $this->assertFieldByXPath("//input[@name='path[0][alias]' and contains(@class, 'error')]", $edit['path[0][alias]'], 'Textfield exists and has the error class.');
390 // Behavior here differs with the inline_form_errors module enabled.
391 // Enable the inline_form_errors module and try this again. This module
392 // improves validation with a link in the error message(s) to the fields
393 // which have invalid input.
394 $this->assertTrue($this->container->get('module_installer')->install(['inline_form_errors'], TRUE), 'Installed inline_form_errors.');
395 // Attempt to edit the second node again, as before.
396 $this->drupalPostForm('node/' . $node_two->id() . '/edit', $edit, t('Preview'));
397 // This error should still be present next to the field.
398 $this->assertSession()->pageTextContains(t('The alias is already in use.'), 'Field error found with expected text.');
399 // The validation error set for the page should include this text.
400 $this->assertSession()->pageTextContains(t('1 error has been found: URL alias'), 'Form error found with expected text.');
401 // The text 'URL alias' should be a link.
402 $this->assertSession()->linkExists('URL alias');
403 // The link should be to the ID of the URL alias field.
404 $this->assertSession()->linkByHrefExists('#edit-path-0-alias');