1 form.test FormsTestCase::testCheckboxProcessing()

Test default value handling for checkboxes.

See also

_form_test_checkbox()

File

core/modules/simpletest/tests/form.test, line 252
Unit tests for the Backdrop Form API.

Class

FormsTestCase

Code

function testCheckboxProcessing() {
  // First, try to submit without the required checkbox.
  $edit = array();
  $this->backdropPost('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 = backdrop_json_decode($this->backdropPost(NULL, array('required_checkbox' => 1), t('Submit')));
  $expected_values = array(
    'disabled_checkbox_on' => 'disabled_checkbox_on',
    'disabled_checkbox_off' => 0,
    'checkbox_on' => 'checkbox_on',
    'checkbox_off' => 0,
    'zero_checkbox_on' => '0',
    'zero_checkbox_off' => 0,
  );
  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),
    )));
  }
}