3 namespace Drupal\Tests\system\Functional\Form;
5 use Drupal\Tests\BrowserTestBase;
8 * Tests form redirection functionality.
12 class RedirectTest extends BrowserTestBase {
19 public static $modules = ['form_test', 'block'];
22 * Tests form redirection.
24 public function testRedirect() {
25 $path = 'form-test/redirect';
26 $options = ['query' => ['foo' => 'bar']];
27 $options['absolute'] = TRUE;
29 // Test basic redirection.
31 'redirection' => TRUE,
32 'destination' => $this->randomMachineName(),
34 $this->drupalPostForm($path, $edit, t('Submit'));
35 $this->assertUrl($edit['destination'], [], 'Basic redirection works.');
38 // Test without redirection.
40 'redirection' => FALSE,
42 $this->drupalPostForm($path, $edit, t('Submit'));
43 $this->assertUrl($path, [], 'When redirect is set to FALSE, there should be no redirection.');
45 // Test redirection with query parameters.
47 'redirection' => TRUE,
48 'destination' => $this->randomMachineName(),
50 $this->drupalPostForm($path, $edit, t('Submit'), $options);
51 $this->assertUrl($edit['destination'], [], 'Redirection with query parameters works.');
53 // Test without redirection but with query parameters.
55 'redirection' => FALSE,
57 $this->drupalPostForm($path, $edit, t('Submit'), $options);
58 $this->assertUrl($path, $options, 'When redirect is set to FALSE, there should be no redirection, and the query parameters should be passed along.');
60 // Test redirection back to the original path.
62 'redirection' => TRUE,
65 $this->drupalPostForm($path, $edit, t('Submit'));
66 $this->assertUrl($path, [], 'When using an empty redirection string, there should be no redirection.');
68 // Test redirection back to the original path with query parameters.
70 'redirection' => TRUE,
73 $this->drupalPostForm($path, $edit, t('Submit'), $options);
74 $this->assertUrl($path, $options, 'When using an empty redirection string, there should be no redirection, and the query parameters should be passed along.');
78 * Tests form redirection from 404/403 pages with the Block form.
80 public function testRedirectFromErrorPages() {
81 // Make sure the block containing the redirect form is placed.
82 $this->drupalPlaceBlock('redirect_form_block');
84 // Create a user that does not have permission to administer blocks.
85 $user = $this->drupalCreateUser(['administer themes']);
86 $this->drupalLogin($user);
88 // Visit page 'foo' (404 page) and submit the form. Verify it ends up
90 $expected = \Drupal::url('form_test.route1', [], ['query' => ['test1' => 'test2'], 'absolute' => TRUE]);
91 $this->drupalGet('foo');
92 $this->assertResponse(404);
93 $this->drupalPostForm(NULL, [], t('Submit'));
94 $this->assertResponse(200);
95 $this->assertUrl($expected, [], 'Redirected to correct URL/query.');
97 // Visit the block admin page (403 page) and submit the form. Verify it
98 // ends up at the right URL.
99 $this->drupalGet('admin/structure/block');
100 $this->assertResponse(403);
101 $this->drupalPostForm(NULL, [], t('Submit'));
102 $this->assertResponse(200);
103 $this->assertUrl($expected, [], 'Redirected to correct URL/query.');