You are here

function FormTest::testCheckboxProcessing in Zircon Profile 8.0

Same name and namespace in other branches
  1. 8 core/modules/system/src/Tests/Form/FormTest.php \Drupal\system\Tests\Form\FormTest::testCheckboxProcessing()

Test default value handling for checkboxes.

See also

_form_test_checkbox()

File

core/modules/system/src/Tests/Form/FormTest.php, line 346
Contains \Drupal\system\Tests\Form\FormTest.

Class

FormTest
Tests various form element validation mechanisms.

Namespace

Drupal\system\Tests\Form

Code

function testCheckboxProcessing() {

  // First, try to submit without the required checkbox.
  $edit = array();
  $this
    ->drupalPostForm('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 = Json::decode($this
    ->drupalPostForm(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),
    )));
  }
}