You are here

public function RedirectTest::testRedirect in Drupal 8

Same name in this branch
  1. 8 core/modules/views_ui/tests/src/Functional/RedirectTest.php \Drupal\Tests\views_ui\Functional\RedirectTest::testRedirect()
  2. 8 core/modules/system/tests/src/Functional/Form/RedirectTest.php \Drupal\Tests\system\Functional\Form\RedirectTest::testRedirect()
Same name and namespace in other branches
  1. 9 core/modules/system/tests/src/Functional/Form/RedirectTest.php \Drupal\Tests\system\Functional\Form\RedirectTest::testRedirect()

Tests form redirection.

File

core/modules/system/tests/src/Functional/Form/RedirectTest.php, line 30

Class

RedirectTest
Tests form redirection functionality.

Namespace

Drupal\Tests\system\Functional\Form

Code

public function testRedirect() {
  $path = 'form-test/redirect';
  $options = [
    'query' => [
      'foo' => 'bar',
    ],
  ];
  $options['absolute'] = TRUE;

  // Test basic redirection.
  $edit = [
    'redirection' => TRUE,
    'destination' => $this
      ->randomMachineName(),
  ];
  $this
    ->drupalPostForm($path, $edit, t('Submit'));
  $this
    ->assertUrl($edit['destination'], [], 'Basic redirection works.');

  // Test without redirection.
  $edit = [
    'redirection' => FALSE,
  ];
  $this
    ->drupalPostForm($path, $edit, t('Submit'));
  $this
    ->assertUrl($path, [], 'When redirect is set to FALSE, there should be no redirection.');

  // Test redirection with query parameters.
  $edit = [
    'redirection' => TRUE,
    'destination' => $this
      ->randomMachineName(),
  ];
  $this
    ->drupalPostForm($path, $edit, t('Submit'), $options);
  $this
    ->assertUrl($edit['destination'], [], 'Redirection with query parameters works.');

  // Test without redirection but with query parameters.
  $edit = [
    'redirection' => FALSE,
  ];
  $this
    ->drupalPostForm($path, $edit, t('Submit'), $options);
  $this
    ->assertUrl($path, $options, 'When redirect is set to FALSE, there should be no redirection, and the query parameters should be passed along.');

  // Test redirection back to the original path.
  $edit = [
    'redirection' => TRUE,
    'destination' => '',
  ];
  $this
    ->drupalPostForm($path, $edit, t('Submit'));
  $this
    ->assertUrl($path, [], 'When using an empty redirection string, there should be no redirection.');

  // Test redirection back to the original path with query parameters.
  $edit = [
    'redirection' => TRUE,
    'destination' => '',
  ];
  $this
    ->drupalPostForm($path, $edit, t('Submit'), $options);
  $this
    ->assertUrl($path, $options, 'When using an empty redirection string, there should be no redirection, and the query parameters should be passed along.');
}