You are here

public function PathAliasTest::testNodeAlias in Drupal 9

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

Class

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

Namespace

Drupal\Tests\path\Functional

Code

public function testNodeAlias() {

  // Create test node.
  $node1 = $this
    ->drupalCreateNode();

  // Create alias.
  $edit = [];
  $edit['path[0][alias]'] = '/' . $this
    ->randomMachineName(8);
  $this
    ->drupalGet('node/' . $node1
    ->id() . '/edit');
  $this
    ->submitForm($edit, 'Save');

  // Confirm that the alias works.
  $this
    ->drupalGet($edit['path[0][alias]']);
  $this
    ->assertSession()
    ->pageTextContains($node1
    ->label());
  $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
    // cSpell:disable-next-line
    $edit['path[0][alias]'] .= "ïвβéø";
  }
  $this
    ->drupalGet('node/' . $node1
    ->id() . '/edit');
  $this
    ->submitForm($edit, 'Save');

  // Confirm that the alias works.
  $this
    ->drupalGet(mb_strtoupper($edit['path[0][alias]']));
  $this
    ->assertSession()
    ->pageTextContains($node1
    ->label());
  $this
    ->assertSession()
    ->statusCodeEquals(200);

  // Make sure that previous alias no longer works.
  $this
    ->drupalGet($previous);
  $this
    ->assertSession()
    ->pageTextNotContains($node1
    ->label());
  $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
    ->drupalGet('node/' . $node2
    ->id() . '/edit');
  $this
    ->submitForm($edit, '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
    ->drupalGet('node/' . $node1
    ->id() . '/edit');
  $this
    ->submitForm([
    'path[0][alias]' => '',
  ], 'Save');

  // Confirm that the alias no longer works.
  $this
    ->drupalGet($edit['path[0][alias]']);
  $this
    ->assertSession()
    ->pageTextNotContains($node1
    ->label());
  $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
    ->drupalGet('node/' . $node3
    ->id() . '/edit');
  $this
    ->submitForm($edit, 'Save');

  // Confirm that the alias was converted to a relative path.
  $this
    ->drupalGet(trim($edit['path[0][alias]'], '/'));
  $this
    ->assertSession()
    ->pageTextContains($node3
    ->label());
  $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
    ->drupalGet('node/' . $node4
    ->id() . '/edit');
  $this
    ->submitForm($edit, 'Save');

  // Confirm that the alias was converted to a relative path.
  $this
    ->drupalGet(trim($edit['path[0][alias]'], '/'));
  $this
    ->assertSession()
    ->pageTextContains($node4
    ->label());
  $this
    ->assertSession()
    ->statusCodeEquals(200);

  // Create fifth test node.
  $node5 = $this
    ->drupalCreateNode();

  // Set a path alias.
  $edit = [
    'path[0][alias]' => '/' . $this
      ->randomMachineName(8),
  ];
  $this
    ->drupalGet('node/' . $node5
    ->id() . '/edit');
  $this
    ->submitForm($edit, '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
    ->drupalGet($node6
    ->toUrl('edit-form'));
  $this
    ->submitForm($edit, '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).
  $this
    ->assertSession()
    ->elementAttributeContains('xpath', "//a[normalize-space(text())='{$node6->getTitle()}']", 'href', base_path() . $alias);
  $this
    ->clickLink($node6
    ->getTitle());
  $this
    ->assertSession()
    ->statusCodeEquals(404);
}