db backup prior to drupal security update
[yaffs-website] / web / core / modules / system / src / Tests / Form / CheckboxTest.php
1 <?php
2
3 namespace Drupal\system\Tests\Form;
4
5 use Drupal\simpletest\WebTestBase;
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 WebTestBase {
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     $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');
65
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"]');
70
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]));
76     }
77     $edit = ['checkbox_off[0]' => '0'];
78     $this->drupalPostForm('form-test/checkboxes-zero/0', $edit, 'Save');
79     $checkboxes = $this->xpath('//input[@type="checkbox"]');
80
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]));
86     }
87   }
88
89 }