3 namespace Drupal\system\Tests\Form;
5 use Drupal\simpletest\WebTestBase;
8 * Tests form API checkbox handling of various combinations of #default_value
13 class CheckboxTest extends WebTestBase {
20 public static $modules = ['form_test'];
22 public function testFormCheckbox() {
23 // Ensure that the checked state is determined and rendered correctly for
24 // tricky combinations of default and return values.
25 foreach ([FALSE, NULL, TRUE, 0, '0', '', 1, '1', 'foobar', '1foobar'] as $default_value) {
26 // Only values that can be used for array indices are supported for
27 // #return_value, with the exception of integer 0, which is not supported.
28 // @see \Drupal\Core\Render\Element\Checkbox::processCheckbox().
29 foreach (['0', '', 1, '1', 'foobar', '1foobar'] as $return_value) {
30 $form_array = \Drupal::formBuilder()->getForm('\Drupal\form_test\Form\FormTestCheckboxTypeJugglingForm', $default_value, $return_value);
31 $form = \Drupal::service('renderer')->renderRoot($form_array);
32 if ($default_value === TRUE) {
35 elseif ($return_value === '0') {
36 $checked = ($default_value === '0');
38 elseif ($return_value === '') {
39 $checked = ($default_value === '');
41 elseif ($return_value === 1 || $return_value === '1') {
42 $checked = ($default_value === 1 || $default_value === '1');
44 elseif ($return_value === 'foobar') {
45 $checked = ($default_value === 'foobar');
47 elseif ($return_value === '1foobar') {
48 $checked = ($default_value === '1foobar');
50 $checked_in_html = strpos($form, 'checked') !== FALSE;
51 $message = format_string('#default_value is %default_value #return_value is %return_value.', ['%default_value' => var_export($default_value, TRUE), '%return_value' => var_export($return_value, TRUE)]);
52 $this->assertIdentical($checked, $checked_in_html, $message);
56 // Ensure that $form_state->getValues() is populated correctly for a
57 // checkboxes group that includes a 0-indexed array of options.
58 $results = json_decode($this->drupalPostForm('form-test/checkboxes-zero/1', [], 'Save'));
59 $this->assertIdentical($results->checkbox_off, [0, 0, 0], 'All three in checkbox_off are zeroes: off.');
60 $this->assertIdentical($results->checkbox_zero_default, ['0', 0, 0], 'The first choice is on in checkbox_zero_default');
61 $this->assertIdentical($results->checkbox_string_zero_default, ['0', 0, 0], 'The first choice is on in checkbox_string_zero_default');
62 $edit = ['checkbox_off[0]' => '0'];
63 $results = json_decode($this->drupalPostForm('form-test/checkboxes-zero/1', $edit, 'Save'));
64 $this->assertIdentical($results->checkbox_off, ['0', 0, 0], 'The first choice is on in checkbox_off but the rest is not');
66 // Ensure that each checkbox is rendered correctly for a checkboxes group
67 // that includes a 0-indexed array of options.
68 $this->drupalPostForm('form-test/checkboxes-zero/0', [], 'Save');
69 $checkboxes = $this->xpath('//input[@type="checkbox"]');
71 $this->assertIdentical(count($checkboxes), 9, 'Correct number of checkboxes found.');
72 foreach ($checkboxes as $checkbox) {
73 $checked = isset($checkbox['checked']);
74 $name = (string) $checkbox['name'];
75 $this->assertIdentical($checked, $name == 'checkbox_zero_default[0]' || $name == 'checkbox_string_zero_default[0]', format_string('Checkbox %name correctly checked', ['%name' => $name]));
77 $edit = ['checkbox_off[0]' => '0'];
78 $this->drupalPostForm('form-test/checkboxes-zero/0', $edit, 'Save');
79 $checkboxes = $this->xpath('//input[@type="checkbox"]');
81 $this->assertIdentical(count($checkboxes), 9, 'Correct number of checkboxes found.');
82 foreach ($checkboxes as $checkbox) {
83 $checked = isset($checkbox['checked']);
84 $name = (string) $checkbox['name'];
85 $this->assertIdentical($checked, $name == 'checkbox_off[0]' || $name == 'checkbox_zero_default[0]' || $name == 'checkbox_string_zero_default[0]', format_string('Checkbox %name correctly checked', ['%name' => $name]));