1 redirect.test RedirectFunctionalTest::testPathChangeRedirects()

Tests renaming automatic aliases in a circular loop.

File

core/modules/redirect/tests/redirect.test, line 222
Unit tests for the redirect module.

Class

RedirectFunctionalTest

Code

function testPathChangeRedirects() {
  // Create an initial post node with a path alias.
  $node = $this->backdropCreateNode(array('type' => 'post', 'path' => array('alias' => 'first-alias')));
  $node_path = 'node/' . $node->nid;

  // Change the node's alias will create an automatic redirect from 'first-alias' to the node.
  $this->backdropPost("$node_path/edit", array('path[alias]' => 'second-alias'), 'Save');
  $redirect = redirect_load_by_source('first-alias');
  $this->assertRedirect($redirect);
  $this->assertEqual(url($node_path), base_path() . 'second-alias');

  $this->backdropPost("$node_path/edit", array('path[alias]' => 'first-alias'), 'Save');
  $redirect = redirect_load_by_source('second-alias');
  $this->assertRedirect($redirect);
  $this->assertEqual(url($node_path), base_path() . 'first-alias');

  $this->backdropPost("$node_path/edit", array('path[alias]' => 'second-alias'), 'Save');
  $redirect = redirect_load_by_source('first-alias');
  $this->assertRedirect($redirect);
  $this->assertEqual(url($node_path), base_path() . 'second-alias');

  // Do a similar redirect loop test for manually created redirects.
  $node = $this->backdropCreateNode(array('type' => 'post', 'path' => array('alias' => 'manual-test')));
  $node_path = 'node/' . $node->nid;

  // Redirects not allowed to point to their source path.
  $edit = array(
    'source' => 'manual-test',
    'redirect' => $node_path,
  );
  $this->backdropPost('admin/config/urls/redirect/add', $edit, t('Save redirect'));
  $this->assertText(t('You are attempting to redirect the page to itself. This will result in an infinite loop.'));

  // Redirect that points to the alias.
  $edit = array(
    'source' => 'manual-redirect',
    'redirect' => 'manual-test',
  );
  $this->backdropPost('admin/config/urls/redirect/add', $edit, t('Save redirect'));
  $this->assertText(t('The redirect has been saved.'));

  // The redirect will resolve from "manual-test" (the alias) to the internal
  // path, which should be "node/2".
  $redirect = redirect_load_by_source('manual-redirect');
  $this->assertRedirect($redirect);
  $this->assertEqual($redirect->redirect, $node_path);

  // Update the node to give it the manual redirect path.
  $this->backdropPost("$node_path/edit", array('path[alias]' => 'manual-redirect'), 'Save');

  // Now the redirect takes on what had previously been the alias.
  $redirect = redirect_load_by_source('manual-test');
  $this->assertRedirect($redirect);
  $this->assertEqual($redirect->redirect, $node_path);
}