diff options
author | Dries Buytaert <dries@buytaert.net> | 2010-07-26 13:07:59 +0000 |
---|---|---|
committer | Dries Buytaert <dries@buytaert.net> | 2010-07-26 13:07:59 +0000 |
commit | 123bf46c432f290e0cabfbb357c434fc61285b89 (patch) | |
tree | b5f3ad0d0f60ebbfa1219d3f26d24e3b0552b86f /modules | |
parent | 803101b589a439d2f489ce267dc432ba13efed0b (diff) | |
download | brdo-123bf46c432f290e0cabfbb357c434fc61285b89.tar.gz brdo-123bf46c432f290e0cabfbb357c434fc61285b89.tar.bz2 |
- Patch #827430 by David_Rothstein: drupal_form_submit() no longer works with checkboxes.
Diffstat (limited to 'modules')
-rw-r--r-- | modules/simpletest/tests/form.test | 51 | ||||
-rw-r--r-- | modules/simpletest/tests/form_test.module | 22 |
2 files changed, 49 insertions, 24 deletions
diff --git a/modules/simpletest/tests/form.test b/modules/simpletest/tests/form.test index d139f4dcb..2e1564a6e 100644 --- a/modules/simpletest/tests/form.test +++ b/modules/simpletest/tests/form.test @@ -973,10 +973,19 @@ class FormsProgrammaticTestCase extends DrupalWebTestCase { $current_batch = $batch =& batch_get(); $batch = array(); - $this->submitForm(); - $this->submitForm('test 1'); - $this->submitForm(); - $this->submitForm('test 2'); + // Test that a programmatic form submission is rejected when a required + // textfield is omitted and correctly processed when it is provided. + $this->submitForm(array(), FALSE); + $this->submitForm(array('textfield' => 'test 1'), TRUE); + $this->submitForm(array(), FALSE); + $this->submitForm(array('textfield' => 'test 2'), TRUE); + + // Test that a programmatic form submission can turn on and off checkboxes + // which are, by default, checked. + $this->submitForm(array('textfield' => 'dummy value', 'checkboxes' => array(1 => 1, 2 => 2)), TRUE); + $this->submitForm(array('textfield' => 'dummy value', 'checkboxes' => array(1 => 1, 2 => NULL)), TRUE); + $this->submitForm(array('textfield' => 'dummy value', 'checkboxes' => array(1 => NULL, 2 => 2)), TRUE); + $this->submitForm(array('textfield' => 'dummy value', 'checkboxes' => array(1 => NULL, 2 => NULL)), TRUE); // Restore the current batch status. $batch = $current_batch; @@ -984,30 +993,36 @@ class FormsProgrammaticTestCase extends DrupalWebTestCase { /** * Helper function used to programmatically submit the form defined in - * form_test.module with the given value. + * form_test.module with the given values. * - * @param string $value - * The field value to be submitted. + * @param $values + * An array of field values to be submitted. + * @param $valid_input + * A boolean indicating whether or not the form submission is expected to + * be valid. */ - private function submitForm($value = NULL) { - // Programmatically submit the given value. - $form_state = array('values' => array('submitted_field' => $value)); + private function submitForm($values, $valid_input) { + // Programmatically submit the given values. + $form_state = array('values' => $values); drupal_form_submit('form_test_programmatic_form', $form_state); + // Check that the form returns an error when expected, and vice versa. $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<br/>Validation handler errors: %errors', $args)); + $args = array( + '%values' => print_r($values, TRUE), + '%errors' => $valid_form ? t('None') : implode(' ', $errors), + ); + $this->assertTrue($valid_input == $valid_form, t('Input values: %values<br/>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 + // By fetching the values 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))); + $stored_values = $form_state['storage']['programmatic_form_submit']; + foreach ($values as $key => $value) { + $this->assertTrue(isset($stored_values[$key]) && $stored_values[$key] == $value, t('Submission handler correctly executed: %stored_key is %stored_value', array('%stored_key' => $key, '%stored_value' => print_r($value, TRUE)))); + } } } } diff --git a/modules/simpletest/tests/form_test.module b/modules/simpletest/tests/form_test.module index 6eb209fa5..2fb4937de 100644 --- a/modules/simpletest/tests/form_test.module +++ b/modules/simpletest/tests/form_test.module @@ -1051,10 +1051,20 @@ function form_test_form_form_test_state_persist_alter(&$form, &$form_state) { * Form builder to test programmatic form submissions. */ function form_test_programmatic_form($form, &$form_state) { - $form['submitted_field'] = array( - '#title' => 'Submitted', + $form['textfield'] = array( + '#title' => 'Textfield', '#type' => 'textfield', ); + $form['checkboxes'] = array( + '#type' => 'checkboxes', + '#options' => array( + 1 => 'First checkbox', + 2 => 'Second checkbox', + ), + // Both checkboxes are selected by default so that we can test the ability + // of programmatic form submissions to uncheck them. + '#default_value' => array(1, 2), + ); return $form; } @@ -1066,8 +1076,8 @@ function form_test_programmatic_form($form, &$form_state) { * 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.')); + if (empty($form_state['values']['textfield'])) { + form_set_error('textfield', t('Textfield is required.')); } } @@ -1075,10 +1085,10 @@ function form_test_programmatic_form_validate($form, &$form_state) { * 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. + * submitted values 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']; + $form_state['storage']['programmatic_form_submit'] = $form_state['values']; } /** |