View source
<?php
namespace Drupal\Tests\system\Functional\Form;
use Drupal\Core\Url;
use Drupal\Tests\BrowserTestBase;
class RedirectTest extends BrowserTestBase {
public static $modules = [
'form_test',
'block',
];
protected $defaultTheme = 'stark';
public function testRedirect() {
$path = 'form-test/redirect';
$options = [
'query' => [
'foo' => 'bar',
],
];
$options['absolute'] = TRUE;
$edit = [
'redirection' => TRUE,
'destination' => $this
->randomMachineName(),
];
$this
->drupalPostForm($path, $edit, t('Submit'));
$this
->assertUrl($edit['destination'], [], 'Basic redirection works.');
$edit = [
'redirection' => FALSE,
];
$this
->drupalPostForm($path, $edit, t('Submit'));
$this
->assertUrl($path, [], 'When redirect is set to FALSE, there should be no redirection.');
$edit = [
'redirection' => TRUE,
'destination' => $this
->randomMachineName(),
];
$this
->drupalPostForm($path, $edit, t('Submit'), $options);
$this
->assertUrl($edit['destination'], [], 'Redirection with query parameters works.');
$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.');
$edit = [
'redirection' => TRUE,
'destination' => '',
];
$this
->drupalPostForm($path, $edit, t('Submit'));
$this
->assertUrl($path, [], 'When using an empty redirection string, there should be no redirection.');
$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.');
}
public function testRedirectFromErrorPages() {
$this
->drupalPlaceBlock('redirect_form_block');
$user = $this
->drupalCreateUser([
'administer themes',
]);
$this
->drupalLogin($user);
$expected = Url::fromRoute('form_test.route1', [], [
'query' => [
'test1' => 'test2',
],
'absolute' => TRUE,
])
->toString();
$this
->drupalGet('foo');
$this
->assertSession()
->statusCodeEquals(404);
$this
->drupalPostForm(NULL, [], t('Submit'));
$this
->assertSession()
->statusCodeEquals(200);
$this
->assertUrl($expected, [], 'Redirected to correct URL/query.');
$this
->drupalGet('admin/structure/block');
$this
->assertSession()
->statusCodeEquals(403);
$this
->drupalPostForm(NULL, [], t('Submit'));
$this
->assertSession()
->statusCodeEquals(200);
$this
->assertUrl($expected, [], 'Redirected to correct URL/query.');
}
}