function RedirectTest::testRedirect

Same name in this branch
  1. 10 core/modules/views_ui/tests/src/Functional/RedirectTest.php \Drupal\Tests\views_ui\Functional\RedirectTest::testRedirect()
Same name and namespace in other branches
  1. 11.x core/modules/system/tests/src/Functional/Form/RedirectTest.php \Drupal\Tests\system\Functional\Form\RedirectTest::testRedirect()
  2. 11.x core/modules/views_ui/tests/src/Functional/RedirectTest.php \Drupal\Tests\views_ui\Functional\RedirectTest::testRedirect()
  3. 9 core/modules/system/tests/src/Functional/Form/RedirectTest.php \Drupal\Tests\system\Functional\Form\RedirectTest::testRedirect()
  4. 9 core/modules/views_ui/tests/src/Functional/RedirectTest.php \Drupal\Tests\views_ui\Functional\RedirectTest::testRedirect()
  5. 8.9.x core/modules/system/tests/src/Functional/Form/RedirectTest.php \Drupal\Tests\system\Functional\Form\RedirectTest::testRedirect()
  6. 8.9.x core/modules/views_ui/tests/src/Functional/RedirectTest.php \Drupal\Tests\views_ui\Functional\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() : void {
  $path = 'form-test/redirect';
  $options = [
    'query' => [
      'foo' => 'bar',
    ],
  ];
  $options['absolute'] = TRUE;
  // Test basic redirection.
  $edit = [
    'redirection' => TRUE,
    'destination' => $this->randomMachineName(),
  ];
  $this->drupalGet($path);
  $this->submitForm($edit, 'Submit');
  $this->assertSession()
    ->addressEquals($edit['destination']);
  // Test without redirection.
  $edit = [
    'redirection' => FALSE,
  ];
  $this->drupalGet($path);
  $this->submitForm($edit, 'Submit');
  $this->assertSession()
    ->addressEquals($path);
  // Test redirection with query parameters.
  $edit = [
    'redirection' => TRUE,
    'destination' => $this->randomMachineName(),
  ];
  $this->drupalGet($path, $options);
  $this->submitForm($edit, 'Submit');
  $this->assertSession()
    ->addressEquals($edit['destination']);
  // Test without redirection but with query parameters.
  $edit = [
    'redirection' => FALSE,
  ];
  $this->drupalGet($path, $options);
  $this->submitForm($edit, 'Submit');
  // When redirect is set to FALSE, there should be no redirection, and the
  // query parameters should be passed along.
  $this->assertSession()
    ->addressEquals($path . '?foo=bar');
  // Test redirection back to the original path.
  $edit = [
    'redirection' => TRUE,
    'destination' => '',
  ];
  $this->drupalGet($path);
  $this->submitForm($edit, 'Submit');
  $this->assertSession()
    ->addressEquals($path);
  // Test redirection back to the original path with query parameters.
  $this->drupalGet($path, $options);
  $this->submitForm($edit, 'Submit');
  // When using an empty redirection string, there should be no redirection,
  // and the query parameters should be passed along.
  $this->assertSession()
    ->addressEquals($path . '?foo=bar');
  // Test basic redirection, ignoring the 'destination' query parameter.
  $options['query']['destination'] = $this->randomMachineName();
  $edit = [
    'redirection' => TRUE,
    'destination' => $this->randomMachineName(),
    'ignore_destination' => TRUE,
  ];
  $this->drupalGet($path, $options);
  $this->submitForm($edit, 'Submit');
  $this->assertSession()
    ->addressEquals($edit['destination']);
  // Test redirection with query param, ignoring the 'destination' query
  // parameter.
  $options['query']['destination'] = $this->randomMachineName();
  $edit = [
    'redirection' => TRUE,
    'destination' => $this->randomMachineName() . '?foo=bar',
    'ignore_destination' => TRUE,
  ];
  $this->drupalGet($path, $options);
  $this->submitForm($edit, 'Submit');
  $this->assertSession()
    ->addressEquals($edit['destination']);
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.