Index: includes/form.inc =================================================================== RCS file: /cvs/drupal/drupal/includes/form.inc,v retrieving revision 1.432 diff -u -p -r1.432 form.inc --- includes/form.inc 12 Feb 2010 15:11:29 -0000 1.432 +++ includes/form.inc 1 Mar 2010 17:13:24 -0000 @@ -481,6 +481,10 @@ function drupal_form_submit($form_id, &$ // Programmed forms are always submitted. $form_state['submitted'] = TRUE; + // 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); } Index: modules/simpletest/tests/form.test =================================================================== RCS file: /cvs/drupal/drupal/modules/simpletest/tests/form.test,v retrieving revision 1.37 diff -u -p -r1.37 form.test --- modules/simpletest/tests/form.test 12 Feb 2010 15:11:29 -0000 1.37 +++ modules/simpletest/tests/form.test 1 Mar 2010 17:13:24 -0000 @@ -784,3 +784,68 @@ class FormsRebuildTestCase extends Drupa $this->assertFieldById('edit-text-2', 'DEFAULT 2', t('A newly added textfield was initialized with its default value.')); } } + +/** + * Test the programmatic form submission behavior. + */ +class FormsProgrammaticTestCase extends DrupalWebTestCase { + + function getInfo() { + return array( + 'name' => 'Programmatic form submissions', + 'description' => 'Test the programmatic form submission behavior.', + 'group' => 'Form API', + ); + } + + function setUp() { + parent::setUp('form_test'); + } + + /** + * Test the programmatic form submission workflow. + */ + function testSubmissionWorkflow() { + // 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->submitForm(); + $this->submitForm('test 1'); + $this->submitForm(); + $this->submitForm('test 2'); + + // Restore the current batch status. + $batch = $current_batch; + } + + /** + * Helper function used to programmatically submit the form defined in + * form_test.module with the given value. + * + * @param string $value + * The field value to be submitted. + */ + private function submitForm($value = NULL) { + // Programmatically submit the given value. + $form_state = array('values' => array('submitted_field' => $value)); + drupal_form_submit('form_test_programmatic_form', $form_state); + + $errors = form_get_errors(); + $valid_form = empty($errors); + $valid_input = !empty($value); + + // If no value was passed the form should return an error and viceversa. + $args = array('%value' => $value, '%errors' => $valid_form ? '' : implode(' ', $errors)); + $this->assertTrue($valid_input == $valid_form, t('Input value: %value
Validation handler errors: %errors', $args)); + + // We check submitted values only if we have a valid input. + if ($valid_input) { + // By fetching the value from $form_state['storage'] we ensure that the + // submission handler was properly executed. + $submitted_value = $form_state['storage']['programmatic_form_submit']; + $this->assertTrue($submitted_value == $value, t('Submission handler correctly executed: %submitted_value', array('%submitted_value' => $submitted_value))); + } + } +} Index: modules/simpletest/tests/form_test.module =================================================================== RCS file: /cvs/drupal/drupal/modules/simpletest/tests/form_test.module,v retrieving revision 1.28 diff -u -p -r1.28 form_test.module --- modules/simpletest/tests/form_test.module 8 Feb 2010 22:16:11 -0000 1.28 +++ modules/simpletest/tests/form_test.module 1 Mar 2010 17:13:25 -0000 @@ -889,3 +889,37 @@ function form_test_form_form_test_state_ $form_state['cache'] = TRUE; } } + +/** + * Form builder to test programmatic form submissions. + */ +function form_test_programmatic_form($form, &$form_state) { + $form['submitted_field'] = array( + '#title' => 'Submitted', + '#type' => 'textfield', + ); + + return $form; +} + +/** + * Form validation handler for programmatic form submissions. + * + * To test that the validation handler is correctly executed, the field value is + * explicitly required here. + */ +function form_test_programmatic_form_validate($form, &$form_state) { + if (empty($form_state['values']['submitted_field'])) { + form_set_error('submitted_field', t('Submitted field is required.')); + } +} + +/** + * Form submit handler for programmatic form submissions. + * + * To test that the submission handler is correctly executed, we store the + * submitted value in a place we can access from the caller context. + */ +function form_test_programmatic_form_submit($form, &$form_state) { + $form_state['storage']['programmatic_form_submit'] = $form_state['values']['submitted_field']; +}