Index: modules/simpletest/tests/form.test =================================================================== RCS file: /cvs/drupal/drupal/modules/simpletest/tests/form.test,v retrieving revision 1.13 diff -u -r1.13 form.test --- modules/simpletest/tests/form.test 2 Jun 2009 13:47:26 -0000 1.13 +++ modules/simpletest/tests/form.test 3 Jun 2009 18:33:52 -0000 @@ -346,6 +346,60 @@ } /** + * Test the programmatic form submit behavior. + */ +class FormsProgrammaticSubmitFunctionalTest extends DrupalWebTestCase { + + function getInfo() { + return array( + 'name' => t('Programmatically submitted forms test'), + 'description' => t('Test the programmatic form submit behavior.'), + 'group' => t('Form API'), + ); + } + + function setUp() { + parent::setUp('form_test'); + } + + /** + * Test multiple programmatic form submits (see http://drupal.org/node/260934). + */ + function testMultipleSubmit() { + // Backup the current batch status and reset it to avoid conflicts + // while processing the dummy form submit handler. + $current_batch = $batch =& batch_get(); + $batch = array(); + + $this->submitDummyForm(); + $this->submitDummyForm('test 1'); + $this->submitDummyForm(); + $this->submitDummyForm('test 2'); + + // Restore the current batch status. + $batch = $current_batch; + } + + /** + * Helper function used to programmatically submit the dummy form + * defined in form_test.module with the given value. + * + * @param string $value + * The dummyfield submitted value. + */ + private function submitDummyForm($value = NULL) { + $form_state = array('values' => array('dummyfield' => $value)); + drupal_form_submit('_form_test_dummy_form', $form_state); + $errors = form_get_errors(); + $valid_form = empty($errors); + $result = empty($value) ? !$valid_form : $valid_form; + $args = array('%value' => $value, '%errors' => $valid_form ? '' : implode(' ', $errors)); + $this->assertTrue($result, t('Dummyfield value: %value
Error: %errors', $args)); + $this->assertTrue(!$valid_form || $form_state['dummy_submit'] == $value, t('Form executed')); + } +} + +/** * Test using drupal_form_submit in a batch. */ class FormAPITestCase extends DrupalWebTestCase { Index: modules/simpletest/tests/form_test.module =================================================================== RCS file: /cvs/drupal/drupal/modules/simpletest/tests/form_test.module,v retrieving revision 1.7 diff -u -r1.7 form_test.module --- modules/simpletest/tests/form_test.module 27 May 2009 18:34:00 -0000 1.7 +++ modules/simpletest/tests/form_test.module 3 Jun 2009 18:33:52 -0000 @@ -226,6 +226,35 @@ } /** + * Dummy form used to test programmatic form submits. + */ +function _form_test_dummy_form($form_state) { + return array( + 'dummyfield' => array( + '#title' => 'Dummyfield', + '#type' => 'textfield' + ) + ); +} + +/** + * In order to test the validation handler, the dummy value + * is explicitly required. + */ +function _form_test_dummy_form_validate($form, &$form_state) { + if (empty($form_state['values']['dummyfield'])) { + form_set_error('dummyfield', t('This field is required although dummy.')); + } +} + +/** + * Process the submitted dummy value. + */ +function _form_test_dummy_form_submit($form, &$form_state) { + $form_state['dummy_submit'] = $form_state['values']['dummyfield']; +} + +/** * Page callback for the batch/drupal_form_submit interaction test. * * When called without any arguments we set up a batch that calls @@ -233,7 +262,7 @@ * drupal_form_submit using the values specified in this function. * * The form's field test_value begins at 'initial_value', and is changed - * to 'form_submitted' when the form is submitted successfully. On + * to 'form_submitted' when the form is submitted successfully. On * completion this function is passed 'done' to complete the process. */ function form_test_drupal_form_submit_batch_api($arg = '') { Index: includes/form.inc =================================================================== RCS file: /cvs/drupal/drupal/includes/form.inc,v retrieving revision 1.339 diff -u -r1.339 form.inc --- includes/form.inc 2 Jun 2009 13:47:25 -0000 1.339 +++ includes/form.inc 3 Jun 2009 18:33:51 -0000 @@ -378,6 +378,10 @@ // Merge in default values. $form_state += form_state_defaults(); + // Reset form validation. + $form_state['must_validate'] = TRUE; + form_clear_error(); + drupal_prepare_form($form_id, $form, $form_state); drupal_process_form($form_id, $form, $form_state); }