public function CheckboxTest::testFormCheckbox in Drupal 9
Same name and namespace in other branches
- 8 core/modules/system/tests/src/Functional/Form/CheckboxTest.php \Drupal\Tests\system\Functional\Form\CheckboxTest::testFormCheckbox()
- 10 core/modules/system/tests/src/Functional/Form/CheckboxTest.php \Drupal\Tests\system\Functional\Form\CheckboxTest::testFormCheckbox()
File
- core/modules/system/tests/src/Functional/Form/CheckboxTest.php, line 28
Class
- CheckboxTest
- Tests form API checkbox handling of various combinations of #default_value
and #return_value.
Namespace
Drupal\Tests\system\Functional\Form
Code
public function testFormCheckbox() {
foreach ([
FALSE,
NULL,
TRUE,
0,
'0',
'',
1,
'1',
'foobar',
'1foobar',
] as $default_value) {
foreach ([
'0',
'',
1,
'1',
'foobar',
'1foobar',
] as $return_value) {
$form_array = \Drupal::formBuilder()
->getForm('\\Drupal\\form_test\\Form\\FormTestCheckboxTypeJugglingForm', $default_value, $return_value);
$form = \Drupal::service('renderer')
->renderRoot($form_array);
if ($default_value === TRUE) {
$checked = TRUE;
}
elseif ($return_value === '0') {
$checked = $default_value === '0';
}
elseif ($return_value === '') {
$checked = $default_value === '';
}
elseif ($return_value === 1 || $return_value === '1') {
$checked = $default_value === 1 || $default_value === '1';
}
elseif ($return_value === 'foobar') {
$checked = $default_value === 'foobar';
}
elseif ($return_value === '1foobar') {
$checked = $default_value === '1foobar';
}
$checked_in_html = strpos($form, 'checked') !== FALSE;
$message = new FormattableMarkup('#default_value is %default_value #return_value is %return_value.', [
'%default_value' => var_export($default_value, TRUE),
'%return_value' => var_export($return_value, TRUE),
]);
$this
->assertSame($checked, $checked_in_html, $message);
}
}
$this
->drupalGet('form-test/checkboxes-zero/1');
$this
->submitForm([], 'Save');
$results = json_decode($this
->getSession()
->getPage()
->getContent());
$this
->assertSame([
0,
0,
0,
], $results->checkbox_off, 'All three in checkbox_off are zeroes: off.');
$this
->assertSame([
'0',
0,
0,
], $results->checkbox_zero_default, 'The first choice is on in checkbox_zero_default');
$this
->assertSame([
'0',
0,
0,
], $results->checkbox_string_zero_default, 'The first choice is on in checkbox_string_zero_default');
$this
->drupalGet('form-test/checkboxes-zero/1');
$this
->assertSession()
->fieldExists('checkbox_off[0]')
->check();
$this
->submitForm([], 'Save');
$results = json_decode($this
->getSession()
->getPage()
->getContent());
$this
->assertSame([
'0',
0,
0,
], $results->checkbox_off, 'The first choice is on in checkbox_off but the rest is not');
$this
->drupalGet('form-test/checkboxes-zero/0');
$this
->submitForm([], 'Save');
$checkboxes = $this
->xpath('//input[@type="checkbox"]');
$this
->assertCount(9, $checkboxes, 'Correct number of checkboxes found.');
foreach ($checkboxes as $checkbox) {
$checked = $checkbox
->isChecked();
$name = $checkbox
->getAttribute('name');
$this
->assertSame($checked, $name == 'checkbox_zero_default[0]' || $name == 'checkbox_string_zero_default[0]', new FormattableMarkup('Checkbox %name correctly checked', [
'%name' => $name,
]));
}
$this
->drupalGet('form-test/checkboxes-zero/0');
$this
->assertSession()
->fieldExists('checkbox_off[0]')
->check();
$this
->submitForm([], 'Save');
$checkboxes = $this
->xpath('//input[@type="checkbox"]');
$this
->assertCount(9, $checkboxes, 'Correct number of checkboxes found.');
foreach ($checkboxes as $checkbox) {
$checked = $checkbox
->isChecked();
$name = (string) $checkbox
->getAttribute('name');
$this
->assertSame($checked, $name == 'checkbox_off[0]' || $name == 'checkbox_zero_default[0]' || $name == 'checkbox_string_zero_default[0]', new FormattableMarkup('Checkbox %name correctly checked', [
'%name' => $name,
]));
}
}