#179932: required checkboxes are not validated.

From:  <>


---

 includes/form.inc                         |   23 +++++--
 modules/simpletest/tests/form.test        |   80 ++++++++++++------------
 modules/simpletest/tests/form_test.info   |    8 ++
 modules/simpletest/tests/form_test.module |   98 +++++++++++++++++++++++++++++
 4 files changed, 163 insertions(+), 46 deletions(-)

diff --git includes/form.inc includes/form.inc
index 3c4723f..98e42e7 100644
--- includes/form.inc
+++ includes/form.inc
@@ -1181,11 +1181,20 @@ function form_type_image_button_value($form, $edit = FALSE) {
  */
 function form_type_checkbox_value($form, $edit = FALSE) {
   if ($edit !== FALSE) {
-    if (empty($form['#disabled'])) {
-      return !empty($edit) ? $form['#return_value'] : 0;
+    if (isset($edit)) {
+      // A value is passed by the browser: the checkbox is on.
+      return $form['#return_value'];
     }
     else {
-      return $form['#default_value'];
+      if (!empty($form['#disabled'])) {
+        // Disabled checkbox values are not passed by the browser,
+        // use default instead.
+        return $form['#default_value'];
+      }
+      else {
+        // The checkbox is off.
+        return '';
+      }
     }
   }
 }
@@ -1966,17 +1975,19 @@ function theme_item($element) {
  * @ingroup themeable
  */
 function theme_checkbox($element) {
+  $t = get_t();
   _form_set_class($element, array('form-checkbox'));
   $checkbox = '<input ';
   $checkbox .= 'type="checkbox" ';
   $checkbox .= 'name="' . $element['#name'] . '" ';
   $checkbox .= 'id="' . $element['#id'] . '" ' ;
   $checkbox .= 'value="' . $element['#return_value'] . '" ';
-  $checkbox .= $element['#value'] ? ' checked="checked" ' : ' ';
+  $checkbox .= $element['#value'] == $element['#return_value'] ? ' checked="checked" ' : ' ';
   $checkbox .= drupal_attributes($element['#attributes']) . ' />';
 
   if (!is_null($element['#title'])) {
-    $checkbox = '<label class="option" for="' . $element['#id'] . '">' . $checkbox . ' ' . $element['#title'] . '</label>';
+    $required = !empty($element['#required']) ? ' <span class="form-required" title="' . $t('This field is required.') . '">*</span>' : '';
+    $checkbox = '<label class="option" for="' . $element['#id'] . '">' . $checkbox . ' ' . $element['#title'] . $required . '</label>';
   }
 
   unset($element['#title']);
@@ -2022,7 +2033,7 @@ function form_process_checkboxes($element) {
           '#processed' => TRUE,
           '#title' => $choice,
           '#return_value' => $key,
-          '#default_value' => isset($value[$key]),
+          '#default_value' => isset($value[$key]) ? $key : NULL,
           '#attributes' => $element['#attributes'],
           '#ahah' => isset($element['#ahah']) ? $element['#ahah'] : NULL,
         );
diff --git modules/simpletest/tests/form.test modules/simpletest/tests/form.test
index 6d5f977..6b6978d 100644
--- modules/simpletest/tests/form.test
+++ modules/simpletest/tests/form.test
@@ -6,26 +6,33 @@
  * Unit tests for the Drupal Form API.
  */
 
-class FormsTestCase extends DrupalWebTestCase {
+class FormsAPIUnitTest extends DrupalWebTestCase {
 
   function getInfo() {
     return array(
-      'name' => t('Required field validation'),
-      'description' => t('Carriage returns, tabs, and spaces are not valid content for a required field.'),
+      'name' => t('Field validation'),
+      'description' => t('Test various field validation mechanisms.'),
       'group' => t('Form API'),
     );
   }
 
+  function setUp() {
+    return parent::setUp('form_test');
+  }
+
   /**
    * Check several empty values for required forms elements.
    *
+   * Carriage returns, tabs, spaces, and unchecked checkbox elements are not
+   * valid content for a required field.
+   *
    * If the form field is found in form_get_errors() then the test pass.
    */
   function testRequiredFields() {
-    // Originates from http://drupal.org/node/117748
-    // Sets of empty strings and arrays
+    // Sets of empty strings and arrays.
     $empty_strings = array('""' => "", '"\n"' => "\n", '" "' => " ", '"\t"' => "\t", '" \n\t "' => " \n\t ", '"\n\n\n\n\n"' => "\n\n\n\n\n");
     $empty_arrays = array('array()' => array());
+    $empty_checkbox = array(NULL);
 
     $elements['textfield']['element'] = array('#title' => $this->randomName(), '#type' => 'textfield', '#required' => TRUE);
     $elements['textfield']['empty_values'] = $empty_strings;
@@ -42,6 +49,9 @@ class FormsTestCase extends DrupalWebTestCase {
     $elements['radios']['element'] = array('#title' => $this->randomName(), '#type' => 'radios', '#required' => TRUE, '#options' => array($this->randomName(), $this->randomName(), $this->randomName()));
     $elements['radios']['empty_values'] = $empty_arrays;
 
+    $elements['checkbox']['element'] = array('#title' => $this->randomName(), '#type' => 'checkbox', '#required' => TRUE, '#title' => $this->randomName());
+    $elements['checkbox']['empty_values'] = $empty_checkbox;
+
     $elements['checkboxes']['element'] = array('#title' => $this->randomName(), '#type' => 'checkboxes', '#required' => TRUE,'#options' => array($this->randomName(), $this->randomName(), $this->randomName()));
     $elements['checkboxes']['empty_values'] = $empty_arrays;
 
@@ -51,7 +61,7 @@ class FormsTestCase extends DrupalWebTestCase {
     $elements['file']['element'] = array('#title' => $this->randomName(), '#type' => 'file', '#required' => TRUE);
     $elements['file']['empty_values'] = $empty_strings;
 
-    // Go through all the elements and all the empty values for them
+    // Go through all the elements and all the empty values for them.
     foreach ($elements as $type => $data) {
       foreach ($data['empty_values'] as $key => $empty) {
         $form_id = $this->randomName();
@@ -71,42 +81,32 @@ class FormsTestCase extends DrupalWebTestCase {
     // Clear the expected form error messages so they don't appear as exceptions.
     drupal_get_messages();
   }
-}
-
-/**
- * Test form type functions for expected behavior.
- */
-class FormsTestTypeCase extends DrupalWebTestCase {
-  function getInfo() {
-    return array(
-      'name' => t('Form type-specific tests'),
-      'description' => t('Test form type functions for expected behavior.'),
-      'group' => t('Form API'),
-    );
-  }
 
   /**
-   * Test form_type_checkbox_value() function for expected behavior.
+   * Test default value handling for checkboxes.
+   *
+   * @see form_test_checkbox().
    */
-  function testFormCheckboxValue() {
-    $form['#return_value'] = $return_value = $this->randomName();
-    $form['#default_value'] = $default_value = $this->randomName();
-    // Element is disabled , and $edit is not empty.
-    $form['#disabled'] = TRUE;
-    $edit = array(1);
-    $this->assertEqual(form_type_checkbox_value($form, $edit), $default_value, t('form_type_checkbox_value() returns the default value when #disabled is set.'));
-
-    // Element is not disabled, $edit is not empty.
-    unset($form['#disabled']);
-    $this->assertEqual(form_type_checkbox_value($form, $edit), $return_value, t('form_type_checkbox_value() returns the return value when #disabled is not set.'));
-
-    // Element is not disabled, $edit is empty.
-    $edit = array();
-    $this->assertIdentical(form_type_checkbox_value($form, $edit), 0, t('form_type_checkbox_value() returns 0 when #disabled is not set, and $edit is empty.'));
-
-    // $edit is FALSE.
-    $edit = FALSE;
-    $this->assertNull(form_type_checkbox_value($form, $edit), t('form_type_checkbox_value() returns NULL when $edit is FALSE'));
+  function testCheckBoxProcessing() {
+    // First, try to submit without the required checkbox.
+    $this->drupalPost('form-test/checkbox', array(), t('Submit'));
+    if ($this->assertRaw(t('!name field is required.', array('!name' => 'required_checkbox')), t('A required checkbox is actually mandatory'))) {
+      // Now try to submit the form correctly.
+      $this->drupalPost(NULL, array('required_checkbox' => 1), t('Submit'));
+    }
+
+    $values = json_decode($this->drupalGetContent(), TRUE);
+    $expected_values = array(
+      'disabled_checkbox_on' => 'disabled_checkbox_on',
+      'disabled_checkbox_off' => '',
+      'checkbox_on' => 'checkbox_on',
+      'checkbox_off' => '',
+      'zero_checkbox_on' => '0',
+      'zero_checkbox_off' => '',
+    );
+
+    foreach ($expected_values as $widget => $expected_value) {
+      $this->assertEqual($values[$widget], $expected_value, t('Checkbox %widget returns expected value (expected: %expected, got: %value)', array('%widget' => var_export($widget, TRUE), '%expected' => var_export($expected_value, TRUE), '%value' => var_export($values[$widget], TRUE))));
+    }
   }
 }
-
diff --git modules/simpletest/tests/form_test.info modules/simpletest/tests/form_test.info
new file mode 100644
index 0000000..fb774f3
--- /dev/null
+++ modules/simpletest/tests/form_test.info
@@ -0,0 +1,8 @@
+; $Id $
+name = "Form API test"
+description = "Support module for Form API testing."
+package = Testing
+version = VERSION
+core = 7.x
+files[] = form_test.module
+; hidden = TRUE
diff --git modules/simpletest/tests/form_test.module modules/simpletest/tests/form_test.module
new file mode 100644
index 0000000..c0125f7
--- /dev/null
+++ modules/simpletest/tests/form_test.module
@@ -0,0 +1,98 @@
+<?php
+// $Id $
+
+/**
+ * @file
+ * Support module for Form API tests.
+ */
+
+/**
+ * Implementation of hook_menu().
+ */
+function form_test_menu() {
+  $items['form-test/checkbox'] = array(
+    'title' => t('Form test'),
+    'page callback' => 'drupal_get_form',
+    'page arguments' => array('form_test_checkbox'),
+    'access callback' => TRUE,
+    'type' => MENU_CALLBACK,
+  );
+  return $items;
+}
+
+/**
+ * Form definition.
+ */
+function form_test_checkbox() {
+  $form = array();
+
+  // A required checkbox.
+  $form['required_checkbox'] = array(
+    '#type' => 'checkbox',
+    '#required' => TRUE,
+    '#title' => 'required_checkbox',
+  );
+
+  // A disabled checkbox should get its default value back.
+  $form['disabled_checkbox_on'] = array(
+    '#type' => 'checkbox',
+    '#disabled' => TRUE,
+    '#return_value' => 'disabled_checkbox_on',
+    '#default_value' => 'disabled_checkbox_on',
+    '#title' => 'disabled_checkbox_on',
+  );
+  $form['disabled_checkbox_off'] = array(
+    '#type' => 'checkbox',
+    '#disabled' => TRUE,
+    '#return_value' => 'disabled_checkbox_off',
+    '#default_value' => NULL,
+    '#title' => 'disabled_checkbox_off',
+  );
+
+  // A checkbox is active when #default_value == #return_value.
+  $form['checkbox_on'] = array(
+    '#type' => 'checkbox',
+    '#return_value' => 'checkbox_on',
+    '#default_value' => 'checkbox_on',
+    '#title' => 'checkbox_on',
+  );
+
+  // But inactive in any other case.
+  $form['checkbox_off'] = array(
+    '#type' => 'checkbox',
+    '#return_value' => 'checkbox_off',
+    '#default_value' => 'checkbox_on',
+    '#title' => 'checkbox_off',
+  );
+
+  // Checkboxes with a #return_value of '0' are supported.
+  $form['zero_checkbox_on'] = array(
+    '#type' => 'checkbox',
+    '#return_value' => '0',
+    '#default_value' => '0',
+    '#title' => 'zero_checkbox_on',
+  );
+
+  // In that case, passing a #default_value != '0' means that the checkbox is off.
+  $form['zero_checkbox_off'] = array(
+    '#type' => 'checkbox',
+    '#return_value' => '0',
+    '#default_value' => 1,
+    '#title' => 'zero_checkbox_off',
+  );
+
+  $form['op'] = array(
+    '#type' => 'submit',
+    '#value' => t('Submit')
+  );
+
+  return $form;
+}
+
+/**
+ * Return the form values by JSON.
+ */
+function form_test_checkbox_submit($form_id, &$form_state) {
+  drupal_json($form_state['values']);
+  exit;
+}
