function PathAliasTest::testDuplicateNodeAlias

Same name and namespace in other branches
  1. 8.9.x core/modules/path/tests/src/Functional/PathAliasTest.php \Drupal\Tests\path\Functional\PathAliasTest::testDuplicateNodeAlias()
  2. 10 core/modules/path/tests/src/Functional/PathAliasTest.php \Drupal\Tests\path\Functional\PathAliasTest::testDuplicateNodeAlias()
  3. 11.x 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
Tests modifying path aliases from the UI.

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->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()
        ->statusMessageContains("The alias {$edit['path[0][alias]']} is already in use in this language.", 'error');
    $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()
        ->statusMessageContains('1 error has been found: URL alias', 'error');
    // 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');
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.