diff --git a/core/includes/form.inc b/core/includes/form.inc index 395cca75beefaca8f96420c93310a4c65726ef19..66aff35f4ae928af6df1d2ff29b54256326ad703 100644 --- a/core/includes/form.inc +++ b/core/includes/form.inc @@ -1265,7 +1265,7 @@ function drupal_redirect_form($form_state) { $function($form_state['redirect']); } } - drupal_goto(current_path()); + drupal_goto(current_path(), array('query' => request()->query->all())); } } diff --git a/core/modules/system/lib/Drupal/system/Tests/Form/RedirectTest.php b/core/modules/system/lib/Drupal/system/Tests/Form/RedirectTest.php new file mode 100644 index 0000000000000000000000000000000000000000..646e539ab4b8397fef8c810e3928c4c12942de38 --- /dev/null +++ b/core/modules/system/lib/Drupal/system/Tests/Form/RedirectTest.php @@ -0,0 +1,85 @@ +<?php + +/** + * @file + * Definition of Drupal\system\Tests\Form\RedirectTest. + */ + +namespace Drupal\system\Tests\Form; + +use Drupal\simpletest\WebTestBase; + +/** + * Tests form redirection. + */ +class RedirectTest extends WebTestBase { + + public static function getInfo() { + return array( + 'name' => 'Form redirecting', + 'description' => 'Tests functionality of drupal_redirect_form().', + 'group' => 'Form API', + ); + } + + function setUp() { + parent::setUp(array('form_test')); + } + + /** + * Tests form redirection. + */ + function testRedirect() { + $path = 'form-test/redirect'; + $options = array('query' => array('foo' => 'bar')); + $options['absolute'] = TRUE; + + // Test basic redirection. + $edit = array( + 'redirection' => TRUE, + 'destination' => $this->randomName(), + ); + $this->drupalPost($path, $edit, t('Submit')); + $this->assertUrl($edit['destination'], array(), 'Basic redirection works.'); + + + // Test without redirection. + $edit = array( + 'redirection' => FALSE, + ); + $this->drupalPost($path, $edit, t('Submit')); + $this->assertUrl($path, array(), 'When redirect is set to FALSE, there should be no redirection.'); + + // Test redirection with query parameters. + $edit = array( + 'redirection' => TRUE, + 'destination' => $this->randomName(), + ); + $this->drupalPost($path, $edit, t('Submit'), $options); + $this->assertUrl($edit['destination'], array(), 'Redirection with query parameters works.'); + + // Test without redirection but with query parameters. + $edit = array( + 'redirection' => FALSE, + ); + $this->drupalPost($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 = array( + 'redirection' => TRUE, + 'destination' => '', + ); + $this->drupalPost($path, $edit, t('Submit')); + $this->assertUrl($path, array(), 'When using an empty redirection string, there should be no redirection.'); + + // Test redirection back to the original path with query parameters. + $edit = array( + 'redirection' => TRUE, + 'destination' => '', + ); + $this->drupalPost($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.'); + } + +} diff --git a/core/modules/system/tests/modules/form_test/form_test.module b/core/modules/system/tests/modules/form_test/form_test.module index ff6d54474a2e4d2ed29eda3ee8438a88d39de588..86e810d0db3813abab309fe4a42bd57bb57a4292 100644 --- a/core/modules/system/tests/modules/form_test/form_test.module +++ b/core/modules/system/tests/modules/form_test/form_test.module @@ -212,6 +212,14 @@ function form_test_menu() { 'type' => MENU_CALLBACK, ); + $items['form-test/redirect'] = array( + 'title' => 'Redirect test', + 'page callback' => 'drupal_get_form', + 'page arguments' => array('form_test_redirect'), + 'access callback' => TRUE, + 'type' => MENU_CALLBACK, + ); + $items['form_test/form-labels'] = array( 'title' => 'Form label test', 'page callback' => 'drupal_get_form', @@ -2043,6 +2051,43 @@ function form_test_clicked_button_submit($form, &$form_state) { drupal_set_message('Submit handler for form_test_clicked_button executed.'); } +/** + * Form builder to detect form redirect. + */ +function form_test_redirect($form, &$form_state) { + $form['redirection'] = array( + '#type' => 'checkbox', + '#title' => t('Use redirection'), + ); + $form['destination'] = array( + '#type' => 'textfield', + '#title' => t('Redirect destination'), + '#states' => array( + 'visible' => array( + ':input[name="redirection"]' => array('checked' => TRUE), + ), + ), + ); + $form['submit'] = array( + '#type' => 'submit', + '#value' => t('Submit'), + ); + + return $form; +} + +/** + * Form submit handler to test different redirect behaviours. + */ +function form_test_redirect_submit(&$form, &$form_state) { + if (!empty($form_state['values']['redirection'])) { + $form_state['redirect'] = !empty($form_state['values']['destination']) ? $form_state['values']['destination'] : NULL; + } + else { + $form_state['redirect'] = FALSE; + } +} + /** * Implements hook_form_FORM_ID_alter() for the registration form. */