Pull merge.
[yaffs-website] / web / core / modules / system / tests / src / Functional / Form / ElementsTableSelectTest.php
1 <?php
2
3 namespace Drupal\Tests\system\Functional\Form;
4
5 use Drupal\Core\Form\FormState;
6 use Drupal\Tests\BrowserTestBase;
7
8 /**
9  * Tests the tableselect form element for expected behavior.
10  *
11  * @group Form
12  */
13 class ElementsTableSelectTest extends BrowserTestBase {
14
15   /**
16    * Modules to enable.
17    *
18    * @var array
19    */
20   public static $modules = ['form_test'];
21
22   /**
23    * Test the display of checkboxes when #multiple is TRUE.
24    */
25   public function testMultipleTrue() {
26
27     $this->drupalGet('form_test/tableselect/multiple-true');
28
29     $this->assertSession()->responseNotContains('Empty text.', 'Empty text should not be displayed.');
30
31     // Test for the presence of the Select all rows tableheader.
32     $this->assertNotEmpty($this->xpath('//th[@class="select-all"]'), 'Presence of the "Select all" checkbox.');
33
34     $rows = ['row1', 'row2', 'row3'];
35     foreach ($rows as $row) {
36       $this->assertNotEmpty($this->xpath('//input[@type="checkbox"]', [$row]), "Checkbox for the value $row.");
37     }
38   }
39
40   /**
41    * Test the display of radios when #multiple is FALSE.
42    */
43   public function testMultipleFalse() {
44     $this->drupalGet('form_test/tableselect/multiple-false');
45
46     $this->assertSession()->pageTextNotContains('Empty text.');
47
48     // Test for the absence of the Select all rows tableheader.
49     $this->assertFalse($this->xpath('//th[@class="select-all"]'));
50
51     $rows = ['row1', 'row2', 'row3'];
52     foreach ($rows as $row) {
53       $this->assertNotEmpty($this->xpath('//input[@type="radio"]', [$row], "Radio button value: $row"));
54     }
55   }
56
57   /**
58    * Tests the display when #colspan is set.
59    */
60   public function testTableSelectColSpan() {
61     $this->drupalGet('form_test/tableselect/colspan');
62
63     $this->assertSession()->pageTextContains('Three', 'Presence of the third column');
64     $this->assertSession()->pageTextNotContains('Four', 'Absence of a fourth column');
65
66     // There should be three labeled column headers and 1 for the input.
67     $table_head = $this->xpath('//thead/tr/th');
68     $this->assertEquals(count($table_head), 4, 'There are four column headers');
69
70     // The first two body rows should each have 5 table cells: One for the
71     // radio, one cell in the first column, one cell in the second column,
72     // and two cells in the third column which has colspan 2.
73     for ($i = 0; $i <= 1; $i++) {
74       $this->assertEquals(count($this->xpath('//tbody/tr[' . ($i + 1) . ']/td')), 5, 'There are five cells in row ' . $i);
75     }
76     // The third row should have 3 cells, one for the radio, one spanning the
77     // first and second column, and a third in column 3 (which has colspan 3).
78     $this->assertEquals(count($this->xpath('//tbody/tr[3]/td')), 3, 'There are three cells in row 3.');
79   }
80
81   /**
82    * Test the display of the #empty text when #options is an empty array.
83    */
84   public function testEmptyText() {
85     $this->drupalGet('form_test/tableselect/empty-text');
86     $this->assertSession()->pageTextContains('Empty text.', 'Empty text should be displayed.');
87   }
88
89   /**
90    * Test the submission of single and multiple values when #multiple is TRUE.
91    */
92   public function testMultipleTrueSubmit() {
93
94     // Test a submission with one checkbox checked.
95     $edit = [];
96     $edit['tableselect[row1]'] = TRUE;
97     $this->drupalPostForm('form_test/tableselect/multiple-true', $edit, 'Submit');
98
99     $assert_session = $this->assertSession();
100     $assert_session->pageTextContains('Submitted: row1 = row1', 'Checked checkbox row1');
101     $assert_session->pageTextContains('Submitted: row2 = 0', 'Unchecked checkbox row2.');
102     $assert_session->pageTextContains('Submitted: row3 = 0', 'Unchecked checkbox row3.');
103
104     // Test a submission with multiple checkboxes checked.
105     $edit['tableselect[row1]'] = TRUE;
106     $edit['tableselect[row3]'] = TRUE;
107     $this->drupalPostForm('form_test/tableselect/multiple-true', $edit, 'Submit');
108
109     $assert_session->pageTextContains('Submitted: row1 = row1', 'Checked checkbox row1.');
110     $assert_session->pageTextContains('Submitted: row2 = 0', 'Unchecked checkbox row2.');
111     $assert_session->pageTextContains('Submitted: row3 = row3', 'Checked checkbox row3.');
112
113   }
114
115   /**
116    * Test submission of values when #multiple is FALSE.
117    */
118   public function testMultipleFalseSubmit() {
119     $edit['tableselect'] = 'row1';
120     $this->drupalPostForm('form_test/tableselect/multiple-false', $edit, 'Submit');
121     $this->assertSession()->pageTextContains('Submitted: row1', 'Selected radio button');
122   }
123
124   /**
125    * Test the #js_select property.
126    */
127   public function testAdvancedSelect() {
128     // When #multiple = TRUE a Select all checkbox should be displayed by default.
129     $this->drupalGet('form_test/tableselect/advanced-select/multiple-true-default');
130     $this->xpath('//th[@class="select-all"]');
131
132     // When #js_select is set to FALSE, a "Select all" checkbox should not be displayed.
133     $this->drupalGet('form_test/tableselect/advanced-select/multiple-true-no-advanced-select');
134     $this->assertFalse($this->xpath('//th[@class="select-all"]'));
135
136     // A "Select all" checkbox never makes sense when #multiple = FALSE, regardless of the value of #js_select.
137     $this->drupalGet('form_test/tableselect/advanced-select/multiple-false-default');
138     $this->assertFalse($this->xpath('//th[@class="select-all"]'));
139
140     $this->drupalGet('form_test/tableselect/advanced-select/multiple-false-advanced-select');
141     $this->assertFalse($this->xpath('//th[@class="select-all"]'));
142   }
143
144   /**
145    * Test the whether the option checker gives an error on invalid tableselect values for checkboxes.
146    */
147   public function testMultipleTrueOptionchecker() {
148
149     list($header, $options) = _form_test_tableselect_get_data();
150
151     $form['tableselect'] = [
152       '#type' => 'tableselect',
153       '#header' => $header,
154       '#options' => $options,
155     ];
156
157     // Test with a valid value.
158     list(, , $errors) = $this->formSubmitHelper($form, ['tableselect' => ['row1' => 'row1']]);
159     $this->assertFalse(isset($errors['tableselect']), 'Option checker allows valid values for checkboxes.');
160
161     // Test with an invalid value.
162     list(, , $errors) = $this->formSubmitHelper($form, ['tableselect' => ['non_existing_value' => 'non_existing_value']]);
163     $this->assertTrue(isset($errors['tableselect']), 'Option checker disallows invalid values for checkboxes.');
164
165   }
166
167   /**
168    * Test the whether the option checker gives an error on invalid tableselect values for radios.
169    */
170   public function testMultipleFalseOptionchecker() {
171
172     list($header, $options) = _form_test_tableselect_get_data();
173
174     $form['tableselect'] = [
175       '#type' => 'tableselect',
176       '#header' => $header,
177       '#options' => $options,
178       '#multiple' => FALSE,
179     ];
180
181     // Test with a valid value.
182     list(, , $errors) = $this->formSubmitHelper($form, ['tableselect' => 'row1']);
183     $this->assertFalse(isset($errors['tableselect']), 'Option checker allows valid values for radio buttons.');
184
185     // Test with an invalid value.
186     list(, , $errors) = $this->formSubmitHelper($form, ['tableselect' => 'non_existing_value']);
187     $this->assertTrue(isset($errors['tableselect']), 'Option checker disallows invalid values for radio buttons.');
188   }
189
190   /**
191    * Helper function for the option check test to submit a form while collecting errors.
192    *
193    * @param $form_element
194    *   A form element to test.
195    * @param $edit
196    *   An array containing post data.
197    *
198    * @return
199    *   An array containing the processed form, the form_state and any errors.
200    */
201   private function formSubmitHelper($form, $edit) {
202     $form_id = $this->randomMachineName();
203     $form_state = new FormState();
204
205     $form['op'] = ['#type' => 'submit', '#value' => t('Submit')];
206     // The form token CSRF protection should not interfere with this test, so we
207     // bypass it by setting the token to FALSE.
208     $form['#token'] = FALSE;
209
210     $edit['form_id'] = $form_id;
211
212     // Disable page redirect for forms submitted programmatically. This is a
213     // solution to skip the redirect step (there are no pages, then the redirect
214     // isn't possible).
215     $form_state->disableRedirect();
216     $form_state->setUserInput($edit);
217     $form_state->setFormObject(new StubForm($form_id, $form));
218
219     \Drupal::formBuilder()->prepareForm($form_id, $form, $form_state);
220
221     \Drupal::formBuilder()->processForm($form_id, $form, $form_state);
222
223     $errors = $form_state->getErrors();
224
225     // Clear errors and messages.
226     \Drupal::messenger()->deleteAll();
227     $form_state->clearErrors();
228
229     // Return the processed form together with form_state and errors
230     // to allow the caller lowlevel access to the form.
231     return [$form, $form_state, $errors];
232   }
233
234 }