function PathAliasTest::testNodeAlias

Tests alias functionality through the node interfaces.

File

drupal/core/modules/path/lib/Drupal/path/Tests/PathAliasTest.php, line 143
Definition of Drupal\path\Tests\PathAliasTest.

Class

PathAliasTest
Tests path alias functionality.

Namespace

Drupal\path\Tests

Code

function testNodeAlias() {

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

  // Create alias.
  $edit = array();
  $edit['path[alias]'] = $this
    ->randomName(8);
  $this
    ->drupalPost('node/' . $node1->nid . '/edit', $edit, t('Save'));

  // Confirm that the alias works.
  $this
    ->drupalGet($edit['path[alias]']);
  $this
    ->assertText($node1
    ->label(), 'Alias works.');
  $this
    ->assertResponse(200);

  // Change alias to one containing "exotic" characters.
  $previous = $edit['path[alias]'];
  $edit['path[alias]'] = "- ._~!\$'\"()*@[]?&+%#,;=:" . "%23%25%26%2B%2F%3F" . "éøïвβ中國書۞";

  // Characters from various non-ASCII alphabets.
  $this
    ->drupalPost('node/' . $node1->nid . '/edit', $edit, t('Save'));

  // Confirm that the alias works.
  $this
    ->drupalGet($edit['path[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[alias]'] the same.
  $this
    ->drupalPost('node/' . $node2->nid . '/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
    ->drupalPost('node/' . $node1->nid . '/edit', array(
    'path[alias]' => '',
  ), t('Save'));

  // Confirm that the alias no longer works.
  $this
    ->drupalGet($edit['path[alias]']);
  $this
    ->assertNoText($node1
    ->label(), 'Alias was successfully deleted.');
  $this
    ->assertResponse(404);
}