public function FormTest::testCheckboxProcessing in Drupal 8
Same name and namespace in other branches
- 9 core/modules/system/tests/src/Functional/Form/FormTest.php \Drupal\Tests\system\Functional\Form\FormTest::testCheckboxProcessing()
- 10 core/modules/system/tests/src/Functional/Form/FormTest.php \Drupal\Tests\system\Functional\Form\FormTest::testCheckboxProcessing()
Test default value handling for checkboxes.
See also
_form_test_checkbox()
File
- core/
modules/ system/ tests/ src/ Functional/ Form/ FormTest.php, line 369
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
->drupalPostForm('form-test/checkbox', $edit, t('Submit'));
$this
->assertRaw(t('@name field is required.', [
'@name' => 'required_checkbox',
]), 'A required checkbox is actually mandatory');
// Now try to submit the form correctly.
$this
->drupalPostForm(NULL, [
'required_checkbox' => 1,
], t('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),
]));
}
}