3 namespace Drupal\Tests\system\Functional\Form;
5 use Drupal\Core\Form\FormState;
6 use Drupal\Tests\BrowserTestBase;
9 * Tests the tableselect form element for expected behavior.
13 class ElementsTableSelectTest extends BrowserTestBase {
20 public static $modules = ['form_test'];
23 * Test the display of checkboxes when #multiple is TRUE.
25 public function testMultipleTrue() {
27 $this->drupalGet('form_test/tableselect/multiple-true');
29 $this->assertSession()->responseNotContains('Empty text.', 'Empty text should not be displayed.');
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.');
34 $rows = ['row1', 'row2', 'row3'];
35 foreach ($rows as $row) {
36 $this->assertNotEmpty($this->xpath('//input[@type="checkbox"]', [$row]), "Checkbox for the value $row.");
41 * Test the display of radios when #multiple is FALSE.
43 public function testMultipleFalse() {
44 $this->drupalGet('form_test/tableselect/multiple-false');
46 $this->assertSession()->pageTextNotContains('Empty text.');
48 // Test for the absence of the Select all rows tableheader.
49 $this->assertFalse($this->xpath('//th[@class="select-all"]'));
51 $rows = ['row1', 'row2', 'row3'];
52 foreach ($rows as $row) {
53 $this->assertNotEmpty($this->xpath('//input[@type="radio"]', [$row], "Radio button value: $row"));
58 * Tests the display when #colspan is set.
60 public function testTableSelectColSpan() {
61 $this->drupalGet('form_test/tableselect/colspan');
63 $this->assertSession()->pageTextContains('Three', 'Presence of the third column');
64 $this->assertSession()->pageTextNotContains('Four', 'Absence of a fourth column');
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');
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);
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.');
82 * Test the display of the #empty text when #options is an empty array.
84 public function testEmptyText() {
85 $this->drupalGet('form_test/tableselect/empty-text');
86 $this->assertSession()->pageTextContains('Empty text.', 'Empty text should be displayed.');
90 * Test the submission of single and multiple values when #multiple is TRUE.
92 public function testMultipleTrueSubmit() {
94 // Test a submission with one checkbox checked.
96 $edit['tableselect[row1]'] = TRUE;
97 $this->drupalPostForm('form_test/tableselect/multiple-true', $edit, 'Submit');
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.');
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');
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.');
116 * Test submission of values when #multiple is FALSE.
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');
125 * Test the #js_select property.
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"]');
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"]'));
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"]'));
140 $this->drupalGet('form_test/tableselect/advanced-select/multiple-false-advanced-select');
141 $this->assertFalse($this->xpath('//th[@class="select-all"]'));
145 * Test the whether the option checker gives an error on invalid tableselect values for checkboxes.
147 public function testMultipleTrueOptionchecker() {
149 list($header, $options) = _form_test_tableselect_get_data();
151 $form['tableselect'] = [
152 '#type' => 'tableselect',
153 '#header' => $header,
154 '#options' => $options,
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.');
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.');
168 * Test the whether the option checker gives an error on invalid tableselect values for radios.
170 public function testMultipleFalseOptionchecker() {
172 list($header, $options) = _form_test_tableselect_get_data();
174 $form['tableselect'] = [
175 '#type' => 'tableselect',
176 '#header' => $header,
177 '#options' => $options,
178 '#multiple' => FALSE,
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.');
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.');
191 * Helper function for the option check test to submit a form while collecting errors.
193 * @param $form_element
194 * A form element to test.
196 * An array containing post data.
199 * An array containing the processed form, the form_state and any errors.
201 private function formSubmitHelper($form, $edit) {
202 $form_id = $this->randomMachineName();
203 $form_state = new FormState();
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;
210 $edit['form_id'] = $form_id;
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
215 $form_state->disableRedirect();
216 $form_state->setUserInput($edit);
217 $form_state->setFormObject(new StubForm($form_id, $form));
219 \Drupal::formBuilder()->prepareForm($form_id, $form, $form_state);
221 \Drupal::formBuilder()->processForm($form_id, $form, $form_state);
223 $errors = $form_state->getErrors();
225 // Clear errors and messages.
226 \Drupal::messenger()->deleteAll();
227 $form_state->clearErrors();
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];