You are here

function PathAliasTest::testNodeAlias in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 core/modules/path/src/Tests/PathAliasTest.php \Drupal\path\Tests\PathAliasTest::testNodeAlias()

Tests alias functionality through the node interfaces.

File

core/modules/path/src/Tests/PathAliasTest.php, line 230
Contains \Drupal\path\Tests\PathAliasTest.

Class

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

Namespace

Drupal\path\Tests

Code

function testNodeAlias() {

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

  // Create alias.
  $edit = array();
  $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
    ->assertResponse(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.
  $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(Unicode::strtoupper($edit['path[0][alias]']));
  $this
    ->assertText($node1
    ->label(), 'Changed alias works.');
  $this
    ->assertResponse(200);

  // Make sure that previous alias no longer works.
  $this
    ->drupalGet($previous);
  $this
    ->assertNoText($node1
    ->label(), 'Previous alias no longer works.');
  $this
    ->assertResponse(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
    ->assertText(t('The alias is already in use.'), 'Attempt to moved alias was rejected.');

  // Delete alias.
  $this
    ->drupalPostForm('node/' . $node1
    ->id() . '/edit', array(
    '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
    ->assertResponse(404);

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

  // Set its path alias to an absolute path.
  $edit = array(
    '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
    ->assertResponse(200);

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

  // Set its path alias to have a trailing slash.
  $edit = array(
    '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
    ->assertResponse(200);
}