Mercurial > hg > cmmr2012-drupal-site
comparison core/modules/path/tests/src/Functional/PathAliasTest.php @ 4:a9cd425dd02b
Update, including to Drupal core 8.6.10
author | Chris Cannam |
---|---|
date | Thu, 28 Feb 2019 13:11:55 +0000 |
parents | c75dbcec494b |
children |
comparison
equal
deleted
inserted
replaced
3:307d7a7fd348 | 4:a9cd425dd02b |
---|---|
1 <?php | 1 <?php |
2 | 2 |
3 namespace Drupal\Tests\path\Functional; | 3 namespace Drupal\Tests\path\Functional; |
4 | 4 |
5 use Drupal\Component\Utility\Unicode; | |
6 use Drupal\Core\Cache\Cache; | 5 use Drupal\Core\Cache\Cache; |
7 use Drupal\Core\Database\Database; | 6 use Drupal\Core\Database\Database; |
7 use Drupal\Core\Url; | |
8 | 8 |
9 /** | 9 /** |
10 * Add, edit, delete, and change alias and verify its consistency in the | 10 * Add, edit, delete, and change alias and verify its consistency in the |
11 * database. | 11 * database. |
12 * | 12 * |
23 | 23 |
24 protected function setUp() { | 24 protected function setUp() { |
25 parent::setUp(); | 25 parent::setUp(); |
26 | 26 |
27 // Create test user and log in. | 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']); | 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); | 29 $this->drupalLogin($web_user); |
30 } | 30 } |
31 | 31 |
32 /** | 32 /** |
33 * Tests the path cache. | 33 * Tests the path cache. |
82 // Confirm that the alias works in a case-insensitive way. | 82 // Confirm that the alias works in a case-insensitive way. |
83 $this->assertTrue(ctype_lower(ltrim($edit['alias'], '/'))); | 83 $this->assertTrue(ctype_lower(ltrim($edit['alias'], '/'))); |
84 $this->drupalGet($edit['alias']); | 84 $this->drupalGet($edit['alias']); |
85 $this->assertText($node1->label(), 'Alias works lower case.'); | 85 $this->assertText($node1->label(), 'Alias works lower case.'); |
86 $this->assertResponse(200); | 86 $this->assertResponse(200); |
87 $this->drupalGet(Unicode::strtoupper($edit['alias'])); | 87 $this->drupalGet(mb_strtoupper($edit['alias'])); |
88 $this->assertText($node1->label(), 'Alias works upper case.'); | 88 $this->assertText($node1->label(), 'Alias works upper case.'); |
89 $this->assertResponse(200); | 89 $this->assertResponse(200); |
90 | 90 |
91 // Change alias to one containing "exotic" characters. | 91 // Change alias to one containing "exotic" characters. |
92 $pid = $this->getPID($edit['alias']); | 92 $pid = $this->getPID($edit['alias']); |
109 $edit['alias'] .= "ïвβéø"; | 109 $edit['alias'] .= "ïвβéø"; |
110 } | 110 } |
111 $this->drupalPostForm('admin/config/search/path/edit/' . $pid, $edit, t('Save')); | 111 $this->drupalPostForm('admin/config/search/path/edit/' . $pid, $edit, t('Save')); |
112 | 112 |
113 // Confirm that the alias works. | 113 // Confirm that the alias works. |
114 $this->drupalGet(Unicode::strtoupper($edit['alias'])); | 114 $this->drupalGet(mb_strtoupper($edit['alias'])); |
115 $this->assertText($node1->label(), 'Changed alias works.'); | 115 $this->assertText($node1->label(), 'Changed alias works.'); |
116 $this->assertResponse(200); | 116 $this->assertResponse(200); |
117 | 117 |
118 $this->container->get('path.alias_manager')->cacheClear(); | 118 $this->container->get('path.alias_manager')->cacheClear(); |
119 // Confirm that previous alias no longer works. | 119 // Confirm that previous alias no longer works. |
131 | 131 |
132 // Confirm no duplicate was created. | 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.'); | 133 $this->assertRaw(t('The alias %alias is already in use in this language.', ['%alias' => $edit['alias']]), 'Attempt to move alias was rejected.'); |
134 | 134 |
135 $edit_upper = $edit; | 135 $edit_upper = $edit; |
136 $edit_upper['alias'] = Unicode::strtoupper($edit['alias']); | 136 $edit_upper['alias'] = mb_strtoupper($edit['alias']); |
137 $this->drupalPostForm('admin/config/search/path/add', $edit_upper, t('Save')); | 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.', [ | 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'], | 139 '%alias' => $edit_upper['alias'], |
140 '%stored_alias' => $edit['alias'], | 140 '%stored_alias' => $edit['alias'], |
141 ]), 'Attempt to move upper-case alias was rejected.'); | 141 ]), 'Attempt to move upper-case alias was rejected.'); |
264 $edit['path[0][alias]'] .= "ïвβéø"; | 264 $edit['path[0][alias]'] .= "ïвβéø"; |
265 } | 265 } |
266 $this->drupalPostForm('node/' . $node1->id() . '/edit', $edit, t('Save')); | 266 $this->drupalPostForm('node/' . $node1->id() . '/edit', $edit, t('Save')); |
267 | 267 |
268 // Confirm that the alias works. | 268 // Confirm that the alias works. |
269 $this->drupalGet(Unicode::strtoupper($edit['path[0][alias]'])); | 269 $this->drupalGet(mb_strtoupper($edit['path[0][alias]'])); |
270 $this->assertText($node1->label(), 'Changed alias works.'); | 270 $this->assertText($node1->label(), 'Changed alias works.'); |
271 $this->assertResponse(200); | 271 $this->assertResponse(200); |
272 | 272 |
273 // Make sure that previous alias no longer works. | 273 // Make sure that previous alias no longer works. |
274 $this->drupalGet($previous); | 274 $this->drupalGet($previous); |
326 | 326 |
327 // Delete the node and check that the path alias is also deleted. | 327 // Delete the node and check that the path alias is also deleted. |
328 $node5->delete(); | 328 $node5->delete(); |
329 $path_alias = \Drupal::service('path.alias_storage')->lookupPathAlias('/node/' . $node5->id(), $node5->language()->getId()); | 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.'); | 330 $this->assertFalse($path_alias, 'Alias was successfully deleted when the referenced node was deleted.'); |
331 | |
332 // Create sixth test node. | |
333 $node6 = $this->drupalCreateNode(); | |
334 | |
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); | |
331 } | 359 } |
332 | 360 |
333 /** | 361 /** |
334 * Returns the path ID. | 362 * Returns the path ID. |
335 * | 363 * |