FormsTestCase::testCheckboxProcessing

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

Test default value handling for checkboxes.

See also

_form_test_checkbox()

File

modules/simpletest/tests/form.test, line 195
Unit tests for the Drupal Form API.

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')), t('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, 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),
    )));
  }
}
Login or register to post comments