function FormsTestCase::testCheckboxProcessing

7.x form.test FormsTestCase::testCheckboxProcessing()

Test default value handling for checkboxes.

See also

_form_test_checkbox()

File

drupal-7.x/modules/simpletest/tests/form.test, line 233
Unit tests for the Drupal Form API.

Class

FormsTestCase

Code

function testCheckboxProcessing() {
  // First, try to submit without the required checkbox.
  $edit = array();
  $this->drupalPost('form-test/checkbox', $edit, t('Submit'));
  $this->assertRaw(t('!name field is required.', array('!name' => 'required_checkbox')), 'A required checkbox is actually mandatory');

  // Now try to submit the form correctly.
  $values = drupal_json_decode($this->drupalPost(NULL, array('required_checkbox' => 1), t('Submit')));
  $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, format_string('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),
    )));
  }
}