Version 1
[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
38     // Test without redirection.
39     $edit = [
40       'redirection' => FALSE,
41     ];
42     $this->drupalPostForm($path, $edit, t('Submit'));
43     $this->assertUrl($path, [], 'When redirect is set to FALSE, there should be no redirection.');
44
45     // Test redirection with query parameters.
46     $edit = [
47       'redirection' => TRUE,
48       'destination' => $this->randomMachineName(),
49     ];
50     $this->drupalPostForm($path, $edit, t('Submit'), $options);
51     $this->assertUrl($edit['destination'], [], 'Redirection with query parameters works.');
52
53     // Test without redirection but with query parameters.
54     $edit = [
55       'redirection' => FALSE,
56     ];
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.');
59
60     // Test redirection back to the original path.
61     $edit = [
62       'redirection' => TRUE,
63       'destination' => '',
64     ];
65     $this->drupalPostForm($path, $edit, t('Submit'));
66     $this->assertUrl($path, [], 'When using an empty redirection string, there should be no redirection.');
67
68     // Test redirection back to the original path with query parameters.
69     $edit = [
70       'redirection' => TRUE,
71       'destination' => '',
72     ];
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.');
75   }
76
77   /**
78    * Tests form redirection from 404/403 pages with the Block form.
79    */
80   public function testRedirectFromErrorPages() {
81     // Make sure the block containing the redirect form is placed.
82     $this->drupalPlaceBlock('redirect_form_block');
83
84     // Create a user that does not have permission to administer blocks.
85     $user = $this->drupalCreateUser(['administer themes']);
86     $this->drupalLogin($user);
87
88     // Visit page 'foo' (404 page) and submit the form. Verify it ends up
89     // at the right URL.
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.');
96
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.');
104   }
105
106 }