You are here

function FormsTestCase::testCheckboxProcessing in Drupal 7

Test default value handling for checkboxes.

See also

_form_test_checkbox()

File

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

Class

FormsTestCase
@file 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',
  )), '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' => 0,
    'checkbox_on' => 'checkbox_on',
    'checkbox_off' => 0,
    'zero_checkbox_on' => '0',
    'zero_checkbox_off' => 0,
  );
  foreach ($expected_values as $widget => $expected_value) {
    $this
      ->assertIdentical($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),
    )));
  }
}