View source
<?php
namespace Drupal\Tests\system\Functional\Form;
use Drupal\Core\Url;
use Drupal\Tests\BrowserTestBase;
class RedirectTest extends BrowserTestBase {
protected 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
->drupalGet($path);
$this
->submitForm($edit, 'Submit');
$this
->assertSession()
->addressEquals($edit['destination']);
$edit = [
'redirection' => FALSE,
];
$this
->drupalGet($path);
$this
->submitForm($edit, 'Submit');
$this
->assertSession()
->addressEquals($path);
$edit = [
'redirection' => TRUE,
'destination' => $this
->randomMachineName(),
];
$this
->drupalGet($path, $options);
$this
->submitForm($edit, 'Submit');
$this
->assertSession()
->addressEquals($edit['destination']);
$edit = [
'redirection' => FALSE,
];
$this
->drupalGet($path, $options);
$this
->submitForm($edit, 'Submit');
$this
->assertSession()
->addressEquals($path . '?foo=bar');
$edit = [
'redirection' => TRUE,
'destination' => '',
];
$this
->drupalGet($path);
$this
->submitForm($edit, 'Submit');
$this
->assertSession()
->addressEquals($path);
$edit = [
'redirection' => TRUE,
'destination' => '',
];
$this
->drupalGet($path, $options);
$this
->submitForm($edit, 'Submit');
$this
->assertSession()
->addressEquals($path . '?foo=bar');
}
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
->submitForm([], 'Submit');
$this
->assertSession()
->statusCodeEquals(200);
$this
->assertSession()
->addressEquals($expected);
$this
->drupalGet('admin/structure/block');
$this
->assertSession()
->statusCodeEquals(403);
$this
->submitForm([], 'Submit');
$this
->assertSession()
->statusCodeEquals(200);
$this
->assertSession()
->addressEquals($expected);
}
}