public function PathAliasTest::testDuplicateNodeAlias in Drupal 9
Same name and namespace in other branches
- 8 core/modules/path/tests/src/Functional/PathAliasTest.php \Drupal\Tests\path\Functional\PathAliasTest::testDuplicateNodeAlias()
Tests that duplicate aliases fail validation.
File
- core/
modules/ path/ tests/ src/ Functional/ PathAliasTest.php, line 419
Class
- PathAliasTest
- Add, edit, delete, and change alias and verify its consistency in the database.
Namespace
Drupal\Tests\path\FunctionalCode
public function testDuplicateNodeAlias() {
// Create one node with a random alias.
$node_one = $this
->drupalCreateNode();
$edit = [];
$edit['path[0][alias]'] = '/' . $this
->randomMachineName();
$this
->drupalGet('node/' . $node_one
->id() . '/edit');
$this
->submitForm($edit, 'Save');
// Now create another node and try to set the same alias.
$node_two = $this
->drupalCreateNode();
$this
->drupalGet('node/' . $node_two
->id() . '/edit');
$this
->submitForm($edit, 'Save');
$this
->assertSession()
->pageTextContains("The alias {$edit['path[0][alias]']} is already in use in this language.");
$path_alias = $this
->assertSession()
->fieldExists('path[0][alias]');
$this
->assertSession()
->fieldValueEquals('path[0][alias]', $edit['path[0][alias]']);
$this
->assertTrue($path_alias
->hasClass('error'));
// Behavior here differs with the inline_form_errors module enabled.
// Enable the inline_form_errors module and try this again. This module
// improves validation with a link in the error message(s) to the fields
// which have invalid input.
$this
->assertTrue($this->container
->get('module_installer')
->install([
'inline_form_errors',
], TRUE), 'Installed inline_form_errors.');
// Attempt to edit the second node again, as before.
$this
->drupalGet('node/' . $node_two
->id() . '/edit');
$this
->submitForm($edit, 'Preview');
// This error should still be present next to the field.
$this
->assertSession()
->pageTextContains("The alias {$edit['path[0][alias]']} is already in use in this language.");
// The validation error set for the page should include this text.
$this
->assertSession()
->pageTextContains('1 error has been found: URL alias');
// The text 'URL alias' should be a link.
$this
->assertSession()
->linkExists('URL alias');
// The link should be to the ID of the URL alias field.
$this
->assertSession()
->linkByHrefExists('#edit-path-0-alias');
}