public function FormTest::testCheckboxProcessing in Drupal 9
Same name and namespace in other branches
- 8 core/modules/system/tests/src/Functional/Form/FormTest.php \Drupal\Tests\system\Functional\Form\FormTest::testCheckboxProcessing()
Tests default value handling for checkboxes.
See also
_form_test_checkbox()
File
- core/modules/ system/ tests/ src/ Functional/ Form/ FormTest.php, line 378 
Class
- FormTest
- Tests various form element validation mechanisms.
Namespace
Drupal\Tests\system\Functional\FormCode
public function testCheckboxProcessing() {
  // First, try to submit without the required checkbox.
  $edit = [];
  $this
    ->drupalGet('form-test/checkbox');
  $this
    ->submitForm($edit, 'Submit');
  $this
    ->assertSession()
    ->pageTextContains("required_checkbox field is required.");
  // Now try to submit the form correctly.
  $this
    ->submitForm([
    'required_checkbox' => 1,
  ], 'Submit');
  $values = Json::decode($this
    ->getSession()
    ->getPage()
    ->getContent());
  $expected_values = [
    '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
      ->assertSame($values[$widget], $expected_value, new FormattableMarkup('Checkbox %widget returns expected value (expected: %expected, got: %value)', [
      '%widget' => var_export($widget, TRUE),
      '%expected' => var_export($expected_value, TRUE),
      '%value' => var_export($values[$widget], TRUE),
    ]));
  }
}