Further Drupal 8.6.4 changes. Some core files were not committed before a commit...
[yaffs-website] / web / core / modules / system / tests / src / Functional / Form / CheckboxTest.php
1 <?php
2
3 namespace Drupal\Tests\system\Functional\Form;
4
5 use Drupal\Tests\BrowserTestBase;
6
7 /**
8  * Tests form API checkbox handling of various combinations of #default_value
9  * and #return_value.
10  *
11  * @group Form
12  */
13 class CheckboxTest extends BrowserTestBase {
14
15   /**
16    * Modules to enable.
17    *
18    * @var array
19    */
20   public static $modules = ['form_test'];
21
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) {
33           $checked = TRUE;
34         }
35         elseif ($return_value === '0') {
36           $checked = ($default_value === '0');
37         }
38         elseif ($return_value === '') {
39           $checked = ($default_value === '');
40         }
41         elseif ($return_value === 1 || $return_value === '1') {
42           $checked = ($default_value === 1 || $default_value === '1');
43         }
44         elseif ($return_value === 'foobar') {
45           $checked = ($default_value === 'foobar');
46         }
47         elseif ($return_value === '1foobar') {
48           $checked = ($default_value === '1foobar');
49         }
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);
53       }
54     }
55
56     // Ensure that $form_state->getValues() is populated correctly for a
57     // checkboxes group that includes a 0-indexed array of options.
58     $this->drupalPostForm('form-test/checkboxes-zero/1', [], 'Save');
59     $results = json_decode($this->getSession()->getPage()->getContent());
60     $this->assertIdentical($results->checkbox_off, [0, 0, 0], 'All three in checkbox_off are zeroes: off.');
61     $this->assertIdentical($results->checkbox_zero_default, ['0', 0, 0], 'The first choice is on in checkbox_zero_default');
62     $this->assertIdentical($results->checkbox_string_zero_default, ['0', 0, 0], 'The first choice is on in checkbox_string_zero_default');
63     // Due to Mink driver differences, we cannot submit an empty checkbox value
64     // to drupalPostForm(), even if that empty value is the 'true' value for
65     // the checkbox.
66     $this->drupalGet('form-test/checkboxes-zero/1');
67     $this->assertSession()->fieldExists('checkbox_off[0]')->check();
68     $this->drupalPostForm(NULL, NULL, 'Save');
69     $results = json_decode($this->getSession()->getPage()->getContent());
70     $this->assertIdentical($results->checkbox_off, ['0', 0, 0], 'The first choice is on in checkbox_off but the rest is not');
71
72     // Ensure that each checkbox is rendered correctly for a checkboxes group
73     // that includes a 0-indexed array of options.
74     $this->drupalPostForm('form-test/checkboxes-zero/0', [], 'Save');
75     $checkboxes = $this->xpath('//input[@type="checkbox"]');
76
77     $this->assertIdentical(count($checkboxes), 9, 'Correct number of checkboxes found.');
78     foreach ($checkboxes as $checkbox) {
79       $checked = $checkbox->isChecked();
80       $name = $checkbox->getAttribute('name');
81       $this->assertIdentical($checked, $name == 'checkbox_zero_default[0]' || $name == 'checkbox_string_zero_default[0]', format_string('Checkbox %name correctly checked', ['%name' => $name]));
82     }
83     // Due to Mink driver differences, we cannot submit an empty checkbox value
84     // to drupalPostForm(), even if that empty value is the 'true' value for
85     // the checkbox.
86     $this->drupalGet('form-test/checkboxes-zero/0');
87     $this->assertSession()->fieldExists('checkbox_off[0]')->check();
88     $this->drupalPostForm(NULL, NULL, 'Save');
89     $checkboxes = $this->xpath('//input[@type="checkbox"]');
90
91     $this->assertIdentical(count($checkboxes), 9, 'Correct number of checkboxes found.');
92     foreach ($checkboxes as $checkbox) {
93       $checked = $checkbox->isChecked();
94       $name = (string) $checkbox->getAttribute('name');
95       $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]));
96     }
97   }
98
99 }