function GotoTest::testDrupalGoto

Tests drupal_goto().

File

drupal/core/modules/system/lib/Drupal/system/Tests/Common/GotoTest.php, line 35
Definition of Drupal\system\Tests\Common\GotoTest.

Class

GotoTest
Tests drupal_goto() and hook_drupal_goto_alter().

Namespace

Drupal\system\Tests\Common

Code

function testDrupalGoto() {
  $this
    ->drupalGet('common-test/drupal_goto/redirect');
  $headers = $this
    ->drupalGetHeaders(TRUE);
  list(, $status) = explode(' ', $headers[0][':status'], 3);
  $this
    ->assertEqual($status, 302, 'Expected response code was sent.');
  $this
    ->assertText('drupal_goto', 'Drupal goto redirect succeeded.');
  $this
    ->assertEqual($this
    ->getUrl(), url('common-test/drupal_goto', array(
    'absolute' => TRUE,
  )), 'Drupal goto redirected to expected URL.');
  $this
    ->drupalGet('common-test/drupal_goto/redirect_advanced');
  $headers = $this
    ->drupalGetHeaders(TRUE);
  list(, $status) = explode(' ', $headers[0][':status'], 3);
  $this
    ->assertEqual($status, 301, 'Expected response code was sent.');
  $this
    ->assertText('drupal_goto', 'Drupal goto redirect succeeded.');
  $this
    ->assertEqual($this
    ->getUrl(), url('common-test/drupal_goto', array(
    'query' => array(
      'foo' => '123',
    ),
    'absolute' => TRUE,
  )), 'Drupal goto redirected to expected URL.');

  // Test that drupal_goto() respects ?destination=xxx. Use a complicated URL
  // to test that the path is encoded and decoded properly.
  $destination = 'common-test/drupal_goto/destination?foo=%2525&bar=123';
  $this
    ->drupalGet('common-test/drupal_goto/redirect', array(
    'query' => array(
      'destination' => $destination,
    ),
  ));
  $this
    ->assertText('drupal_goto', 'Drupal goto redirect with destination succeeded.');
  $this
    ->assertEqual($this
    ->getUrl(), url('common-test/drupal_goto/destination', array(
    'query' => array(
      'foo' => '%25',
      'bar' => '123',
    ),
    'absolute' => TRUE,
  )), 'Drupal goto redirected to given query string destination.');

  // Test that drupal_goto() respects ?destination=xxx with an absolute URL
  // that points to this Drupal installation.
  $destination = url('common-test/drupal_goto/alt', array(
    'absolute' => TRUE,
  ));
  $this
    ->drupalGet('common-test/drupal_goto/redirect', array(
    'query' => array(
      'destination' => $destination,
    ),
  ));
  $this
    ->assertText('drupal_goto_alt', 'Drupal goto redirect with absolute URL destination that points to this Drupal installation succeeded.');
  $this
    ->assertEqual($this
    ->getUrl(), url('common-test/drupal_goto/alt', array(
    'absolute' => TRUE,
  )), 'Drupal goto redirected to given query string destination with absolute URL that points to this Drupal installation.');

  // Test that drupal_goto() fails to respect ?destination=xxx with an absolute URL
  // that does not point to this Drupal installation.
  $destination = 'http://pagedoesnotexist';
  $this
    ->drupalGet('common-test/drupal_goto/redirect', array(
    'query' => array(
      'destination' => $destination,
    ),
  ));
  $this
    ->assertText('drupal_goto', 'Drupal goto fails to redirect with absolute URL destination that does not point to this Drupal installation.');
  $this
    ->assertNotEqual($this
    ->getUrl(), $destination, 'Drupal goto failed to redirect to given query string destination with absolute URL that does not point to this Drupal installation.');
}