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.');
37 // Test without redirection.
39 'redirection' => FALSE,
41 $this->drupalPostForm($path, $edit, t('Submit'));
42 $this->assertUrl($path, [], 'When redirect is set to FALSE, there should be no redirection.');
44 // Test redirection with query parameters.
46 'redirection' => TRUE,
47 'destination' => $this->randomMachineName(),
49 $this->drupalPostForm($path, $edit, t('Submit'), $options);
50 $this->assertUrl($edit['destination'], [], 'Redirection with query parameters works.');
52 // Test without redirection but with query parameters.
54 'redirection' => FALSE,
56 $this->drupalPostForm($path, $edit, t('Submit'), $options);
57 $this->assertUrl($path, $options, 'When redirect is set to FALSE, there should be no redirection, and the query parameters should be passed along.');
59 // Test redirection back to the original path.
61 'redirection' => TRUE,
64 $this->drupalPostForm($path, $edit, t('Submit'));
65 $this->assertUrl($path, [], 'When using an empty redirection string, there should be no redirection.');
67 // Test redirection back to the original path with query parameters.
69 'redirection' => TRUE,
72 $this->drupalPostForm($path, $edit, t('Submit'), $options);
73 $this->assertUrl($path, $options, 'When using an empty redirection string, there should be no redirection, and the query parameters should be passed along.');
77 * Tests form redirection from 404/403 pages with the Block form.
79 public function testRedirectFromErrorPages() {
80 // Make sure the block containing the redirect form is placed.
81 $this->drupalPlaceBlock('redirect_form_block');
83 // Create a user that does not have permission to administer blocks.
84 $user = $this->drupalCreateUser(['administer themes']);
85 $this->drupalLogin($user);
87 // Visit page 'foo' (404 page) and submit the form. Verify it ends up
89 $expected = \Drupal::url('form_test.route1', [], ['query' => ['test1' => 'test2'], 'absolute' => TRUE]);
90 $this->drupalGet('foo');
91 $this->assertResponse(404);
92 $this->drupalPostForm(NULL, [], t('Submit'));
93 $this->assertResponse(200);
94 $this->assertUrl($expected, [], 'Redirected to correct URL/query.');
96 // Visit the block admin page (403 page) and submit the form. Verify it
97 // ends up at the right URL.
98 $this->drupalGet('admin/structure/block');
99 $this->assertResponse(403);
100 $this->drupalPostForm(NULL, [], t('Submit'));
101 $this->assertResponse(200);
102 $this->assertUrl($expected, [], 'Redirected to correct URL/query.');