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 27 May 2009 20:56:23 -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 Index: modules/simpletest/tests/form.test =================================================================== RCS file: /cvs/drupal/drupal/modules/simpletest/tests/form.test,v retrieving revision 1.12 diff -u -r1.12 form.test --- modules/simpletest/tests/form.test 25 May 2009 05:20:16 -0000 1.12 +++ modules/simpletest/tests/form.test 27 May 2009 20:56:23 -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: includes/form.inc =================================================================== RCS file: /cvs/drupal/drupal/includes/form.inc,v retrieving revision 1.337 diff -u -r1.337 form.inc --- includes/form.inc 24 May 2009 17:39:30 -0000 1.337 +++ includes/form.inc 27 May 2009 20:56:22 -0000 @@ -378,6 +378,10 @@ // Merge in default values. $form_state += form_state_defaults(); + // Reset form validation. + $form_state['must_validate'] = TRUE; + form_set_error(NULL, '', TRUE); + drupal_prepare_form($form_id, $form, $form_state); drupal_process_form($form_id, $form, $form_state); }