You are here

public function PathAliasTest::testDuplicateNodeAlias in Drupal 8

Same name and namespace in other branches
  1. 9 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 398

Class

PathAliasTest
Add, edit, delete, and change alias and verify its consistency in the database.

Namespace

Drupal\Tests\path\Functional

Code

public function testDuplicateNodeAlias() {

  // Create one node with a random alias.
  $node_one = $this
    ->drupalCreateNode();
  $edit = [];
  $edit['path[0][alias]'] = '/' . $this
    ->randomMachineName();
  $this
    ->drupalPostForm('node/' . $node_one
    ->id() . '/edit', $edit, t('Save'));

  // Now create another node and try to set the same alias.
  $node_two = $this
    ->drupalCreateNode();
  $this
    ->drupalPostForm('node/' . $node_two
    ->id() . '/edit', $edit, t('Save'));
  $this
    ->assertSession()
    ->pageTextContains("The alias {$edit['path[0][alias]']} is already in use in this language.");
  $this
    ->assertFieldByXPath("//input[@name='path[0][alias]' and contains(@class, 'error')]", $edit['path[0][alias]'], 'Textfield exists and has the error class.');

  // 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
    ->drupalPostForm('node/' . $node_two
    ->id() . '/edit', $edit, t('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(t('1 error has been found: URL alias'), 'Form error found with expected text.');

  // 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');
}