function PathAliasTest::testNodeAlias
Same name in other branches
- 9 core/modules/path/tests/src/Functional/PathAliasTest.php \Drupal\Tests\path\Functional\PathAliasTest::testNodeAlias()
- 10 core/modules/path/tests/src/Functional/PathAliasTest.php \Drupal\Tests\path\Functional\PathAliasTest::testNodeAlias()
- 11.x core/modules/path/tests/src/Functional/PathAliasTest.php \Drupal\Tests\path\Functional\PathAliasTest::testNodeAlias()
Tests alias functionality through the node interfaces.
File
-
core/
modules/ path/ tests/ src/ Functional/ PathAliasTest.php, line 239
Class
- PathAliasTest
- Add, edit, delete, and change alias and verify its consistency in the database.
Namespace
Drupal\Tests\path\FunctionalCode
public function testNodeAlias() {
// Create test node.
$node1 = $this->drupalCreateNode();
// Create alias.
$edit = [];
$edit['path[0][alias]'] = '/' . $this->randomMachineName(8);
$this->drupalPostForm('node/' . $node1->id() . '/edit', $edit, t('Save'));
// Confirm that the alias works.
$this->drupalGet($edit['path[0][alias]']);
$this->assertText($node1->label(), 'Alias works.');
$this->assertSession()
->statusCodeEquals(200);
// Confirm the 'canonical' and 'shortlink' URLs.
$elements = $this->xpath("//link[contains(@rel, 'canonical') and contains(@href, '" . $edit['path[0][alias]'] . "')]");
$this->assertTrue(!empty($elements), 'Page contains canonical link URL.');
$elements = $this->xpath("//link[contains(@rel, 'shortlink') and contains(@href, 'node/" . $node1->id() . "')]");
$this->assertTrue(!empty($elements), 'Page contains shortlink URL.');
$previous = $edit['path[0][alias]'];
// Change alias to one containing "exotic" characters.
// Lower-case letters.
$edit['path[0][alias]'] = '/alias' . "- ._~!\$'\"()*@[]?&+%#,;=:" . "%23%25%26%2B%2F%3F" . "中國書۞";
$connection = Database::getConnection();
if ($connection->databaseType() != 'sqlite') {
// When using LIKE for case-insensitivity, the SQLite driver is
// currently unable to find the upper-case versions of non-ASCII
// characters.
// @todo fix this in https://www.drupal.org/node/2607432
$edit['path[0][alias]'] .= "ïвβéø";
}
$this->drupalPostForm('node/' . $node1->id() . '/edit', $edit, t('Save'));
// Confirm that the alias works.
$this->drupalGet(mb_strtoupper($edit['path[0][alias]']));
$this->assertText($node1->label(), 'Changed alias works.');
$this->assertSession()
->statusCodeEquals(200);
// Make sure that previous alias no longer works.
$this->drupalGet($previous);
$this->assertNoText($node1->label(), 'Previous alias no longer works.');
$this->assertSession()
->statusCodeEquals(404);
// Create second test node.
$node2 = $this->drupalCreateNode();
// Set alias to second test node.
// Leave $edit['path[0][alias]'] the same.
$this->drupalPostForm('node/' . $node2->id() . '/edit', $edit, t('Save'));
// Confirm that the alias didn't make a duplicate.
$this->assertSession()
->pageTextContains("The alias {$edit['path[0][alias]']} is already in use in this language.");
// Delete alias.
$this->drupalPostForm('node/' . $node1->id() . '/edit', [
'path[0][alias]' => '',
], t('Save'));
// Confirm that the alias no longer works.
$this->drupalGet($edit['path[0][alias]']);
$this->assertNoText($node1->label(), 'Alias was successfully deleted.');
$this->assertSession()
->statusCodeEquals(404);
// Create third test node.
$node3 = $this->drupalCreateNode();
// Set its path alias to an absolute path.
$edit = [
'path[0][alias]' => '/' . $this->randomMachineName(8),
];
$this->drupalPostForm('node/' . $node3->id() . '/edit', $edit, t('Save'));
// Confirm that the alias was converted to a relative path.
$this->drupalGet(trim($edit['path[0][alias]'], '/'));
$this->assertText($node3->label(), 'Alias became relative.');
$this->assertSession()
->statusCodeEquals(200);
// Create fourth test node.
$node4 = $this->drupalCreateNode();
// Set its path alias to have a trailing slash.
$edit = [
'path[0][alias]' => '/' . $this->randomMachineName(8) . '/',
];
$this->drupalPostForm('node/' . $node4->id() . '/edit', $edit, t('Save'));
// Confirm that the alias was converted to a relative path.
$this->drupalGet(trim($edit['path[0][alias]'], '/'));
$this->assertText($node4->label(), 'Alias trimmed trailing slash.');
$this->assertSession()
->statusCodeEquals(200);
// Create fifth test node.
$node5 = $this->drupalCreateNode();
// Set a path alias.
$edit = [
'path[0][alias]' => '/' . $this->randomMachineName(8),
];
$this->drupalPostForm('node/' . $node5->id() . '/edit', $edit, t('Save'));
// Delete the node and check that the path alias is also deleted.
$node5->delete();
$path_alias = \Drupal::service('path_alias.repository')->lookUpBySystemPath('/node/' . $node5->id(), $node5->language()
->getId());
$this->assertNull($path_alias, 'Alias was successfully deleted when the referenced node was deleted.');
// Create sixth test node.
$node6 = $this->drupalCreateNode();
// Test the special case where the alias is '0'.
$edit = [
'path[0][alias]' => '0',
];
$this->drupalGet($node6->toUrl('edit-form'));
$this->submitForm($edit, 'Save');
$this->assertSession()
->pageTextContains('The alias path has to start with a slash.');
// Create an invalid alias with two leading slashes and verify that the
// extra slash is removed when the link is generated. This ensures that URL
// aliases cannot be used to inject external URLs.
// @todo The user interface should either display an error message or
// automatically trim these invalid aliases, rather than allowing them to
// be silently created, at which point the functional aspects of this
// test will need to be moved elsewhere and switch to using a
// programmatically-created alias instead.
$alias = $this->randomMachineName(8);
$edit = [
'path[0][alias]' => '//' . $alias,
];
$this->drupalPostForm($node6->toUrl('edit-form'), $edit, t('Save'));
$this->drupalGet(Url::fromRoute('system.admin_content'));
// This checks the link href before clicking it, rather than using
// \Drupal\Tests\BrowserTestBase::assertSession()->addressEquals() after
// clicking it, because the test browser does not always preserve the
// correct number of slashes in the URL when it visits internal links;
// using \Drupal\Tests\BrowserTestBase::assertSession()->addressEquals()
// would actually make the test pass unconditionally on the testbot (or
// anywhere else where Drupal is installed in a subdirectory).
$link_xpath = $this->xpath('//a[normalize-space(text())=:label]', [
':label' => $node6->getTitle(),
]);
$link_href = $link_xpath[0]->getAttribute('href');
$this->assertEquals($link_href, base_path() . $alias);
$this->clickLink($node6->getTitle());
$this->assertSession()
->statusCodeEquals(404);
}
Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.