1 path.test PathTestCase::testAdminAlias()

Tests alias functionality through the admin interfaces.

File

core/modules/path/tests/path.test, line 60
Tests for the Path module.

Class

PathTestCase
Provides a base class for testing the Path module.

Code

function testAdminAlias() {
  // Create test node.
  $node1 = $this->backdropCreateNode();

  // Create alias.
  $edit = array();
  $edit['source'] = 'node/' . $node1->nid;
  $edit['alias'] = $this->randomName(8);
  $this->backdropPost('admin/config/urls/path/add', $edit, t('Save URL alias'));

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

  // Change alias to one containing "exotic" characters.
  $pid = $this->getPID($edit['alias']);

  $previous = $edit['alias'];
  $edit['alias'] = "- ._~!$'\"()*@[]?&+%#,;=:" . // "Special" ASCII characters.
    "%23%25%26%2B%2F%3F" . // Characters that look like a percent-escaped string.
    "éøïвβ中國書۞"; // Characters from various non-ASCII alphabets.
  $this->backdropPost('admin/config/urls/path/edit/' . $pid, $edit, t('Save URL alias'));

  // Confirm that the alias works.
  $this->backdropGet($edit['alias']);
  $this->assertText($node1->title, 'Changed alias works.');
  $this->assertResponse(200);

  backdrop_static_reset('backdrop_lookup_path');
  // Confirm that previous alias no longer works.
  $this->backdropGet($previous);
  $this->assertNoText($node1->title, 'Previous alias no longer works.');
  $this->assertResponse(404);

  // Create second test node.
  $node2 = $this->backdropCreateNode();

  // Set alias to second test node.
  $edit['source'] = 'node/' . $node2->nid;
  // leave $edit['alias'] the same
  $this->backdropPost('admin/config/urls/path/add', $edit, t('Save URL alias'));

  // Confirm no duplicate was created.
  $this->assertRaw(t('The alias %alias is already in use in this language.', array('%alias' => $edit['alias'])), 'Attempt to move alias was rejected.');

  // Delete alias.
  $this->backdropPost('admin/config/urls/path/edit/' . $pid, array(), t('Delete'));
  $this->backdropPost(NULL, array(), t('Confirm'));

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

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

  // Create an invalid alias with a leading slash and verify that the 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->randomName(8);
  $edit = array('source' => 'node/' . $node3->nid, 'alias' => '/' . $alias);
  $this->backdropPost('admin/config/urls/path/add', $edit, t('Save URL alias'));
  $this->backdropGet('admin/content');
  // This checks the link href before clicking it, rather than using
  // BackdropWebTestCase::assertUrl() 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 BackdropWebTestCase::assertUrl()
  // would actually make the test pass unconditionally on the testbot (or
  // anywhere else where Backdrop is installed in a subdirectory).
  $link_xpath = $this->xpath('//a[normalize-space(text())=:label]', array(':label' => $node3->title));
  $link_href = (string) $link_xpath[0]['href'];
  $link_prefix = base_path() . (config_get('system.core', 'clean_url') ? '' : '?q=');
  $this->assertEqual($link_href, $link_prefix . $alias);
  $this->clickLink($node3->title);
  $this->assertResponse(404);
}