X-Git-Url: http://aleph1.co.uk/gitweb/?a=blobdiff_plain;f=web%2Fcore%2Fmodules%2Fsystem%2Ftests%2Fsrc%2FFunctional%2FForm%2FRedirectTest.php;fp=web%2Fcore%2Fmodules%2Fsystem%2Ftests%2Fsrc%2FFunctional%2FForm%2FRedirectTest.php;h=a4c9e15b3ce3668b498c53cbd45e03ca2a429bfc;hb=a2bd1bf0c2c1f1a17d188f4dc0726a45494cefae;hp=0000000000000000000000000000000000000000;hpb=57c063afa3f66b07c4bbddc2d6129a96d90f0aad;p=yaffs-website diff --git a/web/core/modules/system/tests/src/Functional/Form/RedirectTest.php b/web/core/modules/system/tests/src/Functional/Form/RedirectTest.php new file mode 100644 index 000000000..a4c9e15b3 --- /dev/null +++ b/web/core/modules/system/tests/src/Functional/Form/RedirectTest.php @@ -0,0 +1,106 @@ + ['foo' => 'bar']]; + $options['absolute'] = TRUE; + + // Test basic redirection. + $edit = [ + 'redirection' => TRUE, + 'destination' => $this->randomMachineName(), + ]; + $this->drupalPostForm($path, $edit, t('Submit')); + $this->assertUrl($edit['destination'], [], 'Basic redirection works.'); + + + // Test without redirection. + $edit = [ + 'redirection' => FALSE, + ]; + $this->drupalPostForm($path, $edit, t('Submit')); + $this->assertUrl($path, [], 'When redirect is set to FALSE, there should be no redirection.'); + + // Test redirection with query parameters. + $edit = [ + 'redirection' => TRUE, + 'destination' => $this->randomMachineName(), + ]; + $this->drupalPostForm($path, $edit, t('Submit'), $options); + $this->assertUrl($edit['destination'], [], 'Redirection with query parameters works.'); + + // Test without redirection but with query parameters. + $edit = [ + 'redirection' => FALSE, + ]; + $this->drupalPostForm($path, $edit, t('Submit'), $options); + $this->assertUrl($path, $options, 'When redirect is set to FALSE, there should be no redirection, and the query parameters should be passed along.'); + + // Test redirection back to the original path. + $edit = [ + 'redirection' => TRUE, + 'destination' => '', + ]; + $this->drupalPostForm($path, $edit, t('Submit')); + $this->assertUrl($path, [], 'When using an empty redirection string, there should be no redirection.'); + + // Test redirection back to the original path with query parameters. + $edit = [ + 'redirection' => TRUE, + 'destination' => '', + ]; + $this->drupalPostForm($path, $edit, t('Submit'), $options); + $this->assertUrl($path, $options, 'When using an empty redirection string, there should be no redirection, and the query parameters should be passed along.'); + } + + /** + * Tests form redirection from 404/403 pages with the Block form. + */ + public function testRedirectFromErrorPages() { + // Make sure the block containing the redirect form is placed. + $this->drupalPlaceBlock('redirect_form_block'); + + // Create a user that does not have permission to administer blocks. + $user = $this->drupalCreateUser(['administer themes']); + $this->drupalLogin($user); + + // Visit page 'foo' (404 page) and submit the form. Verify it ends up + // at the right URL. + $expected = \Drupal::url('form_test.route1', [], ['query' => ['test1' => 'test2'], 'absolute' => TRUE]); + $this->drupalGet('foo'); + $this->assertResponse(404); + $this->drupalPostForm(NULL, [], t('Submit')); + $this->assertResponse(200); + $this->assertUrl($expected, [], 'Redirected to correct URL/query.'); + + // Visit the block admin page (403 page) and submit the form. Verify it + // ends up at the right URL. + $this->drupalGet('admin/structure/block'); + $this->assertResponse(403); + $this->drupalPostForm(NULL, [], t('Submit')); + $this->assertResponse(200); + $this->assertUrl($expected, [], 'Redirected to correct URL/query.'); + } + +}