diff --git a/includes/form.inc b/includes/form.inc index f41cce4a735..9b67da8a3f9 100644 --- a/includes/form.inc +++ b/includes/form.inc @@ -1978,8 +1978,22 @@ function form_type_checkboxes_value($element, $input = FALSE) { } return $value; } + elseif (is_array($input)) { + // Programmatic form submissions use NULL to indicate that a checkbox + // should be unchecked; see drupal_form_submit(). We therefore remove all + // NULL elements from the array before constructing the return value, to + // simulate the behavior of web browsers (which do not send unchecked + // checkboxes to the server at all). This will not affect non-programmatic + // form submissions, since a checkbox can never legitimately be NULL. + foreach ($input as $key => $value) { + if (is_null($value)) { + unset($input[$key]); + } + } + return drupal_map_assoc($input); + } else { - return is_array($input) ? drupal_map_assoc($input) : array(); + return array(); } } diff --git a/modules/simpletest/tests/form.test b/modules/simpletest/tests/form.test index d139f4dcb7d..2e1564a6ea3 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
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
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 6eb209fa536..2fb4937dec3 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']; } /**