62253a681bb5f78e9ca1f5f04dde21381845ace3
[yaffs-website] / web / core / modules / system / tests / src / Functional / Form / RedirectTest.php
1 <?php
2
3 namespace Drupal\Tests\system\Functional\Form;
4
5 use Drupal\Tests\BrowserTestBase;
6
7 /**
8  * Tests form redirection functionality.
9  *
10  * @group Form
11  */
12 class RedirectTest extends BrowserTestBase {
13
14   /**
15    * Modules to enable.
16    *
17    * @var array
18    */
19   public static $modules = ['form_test', 'block'];
20
21   /**
22    * Tests form redirection.
23    */
24   public function testRedirect() {
25     $path = 'form-test/redirect';
26     $options = ['query' => ['foo' => 'bar']];
27     $options['absolute'] = TRUE;
28
29     // Test basic redirection.
30     $edit = [
31       'redirection' => TRUE,
32       'destination' => $this->randomMachineName(),
33     ];
34     $this->drupalPostForm($path, $edit, t('Submit'));
35     $this->assertUrl($edit['destination'], [], 'Basic redirection works.');
36
37     // Test without redirection.
38     $edit = [
39       'redirection' => FALSE,
40     ];
41     $this->drupalPostForm($path, $edit, t('Submit'));
42     $this->assertUrl($path, [], 'When redirect is set to FALSE, there should be no redirection.');
43
44     // Test redirection with query parameters.
45     $edit = [
46       'redirection' => TRUE,
47       'destination' => $this->randomMachineName(),
48     ];
49     $this->drupalPostForm($path, $edit, t('Submit'), $options);
50     $this->assertUrl($edit['destination'], [], 'Redirection with query parameters works.');
51
52     // Test without redirection but with query parameters.
53     $edit = [
54       'redirection' => FALSE,
55     ];
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.');
58
59     // Test redirection back to the original path.
60     $edit = [
61       'redirection' => TRUE,
62       'destination' => '',
63     ];
64     $this->drupalPostForm($path, $edit, t('Submit'));
65     $this->assertUrl($path, [], 'When using an empty redirection string, there should be no redirection.');
66
67     // Test redirection back to the original path with query parameters.
68     $edit = [
69       'redirection' => TRUE,
70       'destination' => '',
71     ];
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.');
74   }
75
76   /**
77    * Tests form redirection from 404/403 pages with the Block form.
78    */
79   public function testRedirectFromErrorPages() {
80     // Make sure the block containing the redirect form is placed.
81     $this->drupalPlaceBlock('redirect_form_block');
82
83     // Create a user that does not have permission to administer blocks.
84     $user = $this->drupalCreateUser(['administer themes']);
85     $this->drupalLogin($user);
86
87     // Visit page 'foo' (404 page) and submit the form. Verify it ends up
88     // at the right URL.
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.');
95
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.');
103   }
104
105 }