3 namespace Drupal\system\Tests\Form;
5 use Drupal\Core\Form\FormState;
6 use Drupal\simpletest\WebTestBase;
7 use Drupal\Tests\system\Functional\Form\StubForm;
10 * Tests the tableselect form element for expected behavior.
14 class ElementsTableSelectTest extends WebTestBase {
21 public static $modules = ['form_test'];
24 * Test the display of checkboxes when #multiple is TRUE.
26 public function testMultipleTrue() {
28 $this->drupalGet('form_test/tableselect/multiple-true');
30 $this->assertNoText(t('Empty text.'), 'Empty text should not be displayed.');
32 // Test for the presence of the Select all rows tableheader.
33 $this->assertFieldByXPath('//th[@class="select-all"]', NULL, 'Presence of the "Select all" checkbox.');
35 $rows = ['row1', 'row2', 'row3'];
36 foreach ($rows as $row) {
37 $this->assertFieldByXPath('//input[@type="checkbox"]', $row, format_string('Checkbox for value @row.', ['@row' => $row]));
42 * Test the presence of ajax functionality for all options.
44 public function testAjax() {
45 $rows = ['row1', 'row2', 'row3'];
46 // Test checkboxes (#multiple == TRUE).
47 foreach ($rows as $row) {
48 $element = 'tableselect[' . $row . ']';
49 $edit = [$element => TRUE];
50 $result = $this->drupalPostAjaxForm('form_test/tableselect/multiple-true', $edit, $element);
51 $this->assertFalse(empty($result), t('Ajax triggers on checkbox for @row.', ['@row' => $row]));
53 // Test radios (#multiple == FALSE).
54 $element = 'tableselect';
55 foreach ($rows as $row) {
56 $edit = [$element => $row];
57 $result = $this->drupalPostAjaxForm('form_test/tableselect/multiple-false', $edit, $element);
58 $this->assertFalse(empty($result), t('Ajax triggers on radio for @row.', ['@row' => $row]));
63 * Test the display of radios when #multiple is FALSE.
65 public function testMultipleFalse() {
66 $this->drupalGet('form_test/tableselect/multiple-false');
68 $this->assertNoText(t('Empty text.'), 'Empty text should not be displayed.');
70 // Test for the absence of the Select all rows tableheader.
71 $this->assertNoFieldByXPath('//th[@class="select-all"]', '', 'Absence of the "Select all" checkbox.');
73 $rows = ['row1', 'row2', 'row3'];
74 foreach ($rows as $row) {
75 $this->assertFieldByXPath('//input[@type="radio"]', $row, format_string('Radio button for value @row.', ['@row' => $row]));
80 * Tests the display when #colspan is set.
82 public function testTableselectColSpan() {
83 $this->drupalGet('form_test/tableselect/colspan');
85 $this->assertText(t('Three'), 'Presence of the third column');
86 $this->assertNoText(t('Four'), 'Absence of a fourth column');
88 // There should be three labeled column headers and 1 for the input.
89 $table_head = $this->xpath('//thead');
90 $this->assertEqual(count($table_head[0]->tr->th), 4, 'There are four column headers');
92 $table_body = $this->xpath('//tbody');
93 // The first two body rows should each have 5 table cells: One for the
94 // radio, one cell in the first column, one cell in the second column,
95 // and two cells in the third column which has colspan 2.
96 for ($i = 0; $i <= 1; $i++) {
97 $this->assertEqual(count($table_body[0]->tr[$i]->td), 5, format_string('There are five cells in row @row.', ['@row' => $i]));
99 // The third row should have 3 cells, one for the radio, one spanning the
100 // first and second column, and a third in column 3 (which has colspan 3).
101 $this->assertEqual(count($table_body[0]->tr[2]->td), 3, 'There are three cells in row 3.');
105 * Test the display of the #empty text when #options is an empty array.
107 public function testEmptyText() {
108 $this->drupalGet('form_test/tableselect/empty-text');
109 $this->assertText(t('Empty text.'), 'Empty text should be displayed.');
113 * Test the submission of single and multiple values when #multiple is TRUE.
115 public function testMultipleTrueSubmit() {
117 // Test a submission with one checkbox checked.
119 $edit['tableselect[row1]'] = TRUE;
120 $this->drupalPostForm('form_test/tableselect/multiple-true', $edit, 'Submit');
122 $this->assertText(t('Submitted: row1 = row1'), 'Checked checkbox row1');
123 $this->assertText(t('Submitted: row2 = 0'), 'Unchecked checkbox row2.');
124 $this->assertText(t('Submitted: row3 = 0'), 'Unchecked checkbox row3.');
126 // Test a submission with multiple checkboxes checked.
127 $edit['tableselect[row1]'] = TRUE;
128 $edit['tableselect[row3]'] = TRUE;
129 $this->drupalPostForm('form_test/tableselect/multiple-true', $edit, 'Submit');
131 $this->assertText(t('Submitted: row1 = row1'), 'Checked checkbox row1.');
132 $this->assertText(t('Submitted: row2 = 0'), 'Unchecked checkbox row2.');
133 $this->assertText(t('Submitted: row3 = row3'), 'Checked checkbox row3.');
138 * Test submission of values when #multiple is FALSE.
140 public function testMultipleFalseSubmit() {
141 $edit['tableselect'] = 'row1';
142 $this->drupalPostForm('form_test/tableselect/multiple-false', $edit, 'Submit');
143 $this->assertText(t('Submitted: row1'), 'Selected radio button');
147 * Test the #js_select property.
149 public function testAdvancedSelect() {
150 // When #multiple = TRUE a Select all checkbox should be displayed by default.
151 $this->drupalGet('form_test/tableselect/advanced-select/multiple-true-default');
152 $this->assertFieldByXPath('//th[@class="select-all"]', NULL, 'Display a "Select all" checkbox by default when #multiple is TRUE.');
154 // When #js_select is set to FALSE, a "Select all" checkbox should not be displayed.
155 $this->drupalGet('form_test/tableselect/advanced-select/multiple-true-no-advanced-select');
156 $this->assertNoFieldByXPath('//th[@class="select-all"]', NULL, 'Do not display a "Select all" checkbox when #js_select is FALSE.');
158 // A "Select all" checkbox never makes sense when #multiple = FALSE, regardless of the value of #js_select.
159 $this->drupalGet('form_test/tableselect/advanced-select/multiple-false-default');
160 $this->assertNoFieldByXPath('//th[@class="select-all"]', NULL, 'Do not display a "Select all" checkbox when #multiple is FALSE.');
162 $this->drupalGet('form_test/tableselect/advanced-select/multiple-false-advanced-select');
163 $this->assertNoFieldByXPath('//th[@class="select-all"]', NULL, 'Do not display a "Select all" checkbox when #multiple is FALSE, even when #js_select is TRUE.');
168 * Test the whether the option checker gives an error on invalid tableselect values for checkboxes.
170 public function testMultipleTrueOptionchecker() {
172 list($header, $options) = _form_test_tableselect_get_data();
174 $form['tableselect'] = [
175 '#type' => 'tableselect',
176 '#header' => $header,
177 '#options' => $options,
180 // Test with a valid value.
181 list(, , $errors) = $this->formSubmitHelper($form, ['tableselect' => ['row1' => 'row1']]);
182 $this->assertFalse(isset($errors['tableselect']), 'Option checker allows valid values for checkboxes.');
184 // Test with an invalid value.
185 list(, , $errors) = $this->formSubmitHelper($form, ['tableselect' => ['non_existing_value' => 'non_existing_value']]);
186 $this->assertTrue(isset($errors['tableselect']), 'Option checker disallows invalid values for checkboxes.');
192 * Test the whether the option checker gives an error on invalid tableselect values for radios.
194 public function testMultipleFalseOptionchecker() {
196 list($header, $options) = _form_test_tableselect_get_data();
198 $form['tableselect'] = [
199 '#type' => 'tableselect',
200 '#header' => $header,
201 '#options' => $options,
202 '#multiple' => FALSE,
205 // Test with a valid value.
206 list(, , $errors) = $this->formSubmitHelper($form, ['tableselect' => 'row1']);
207 $this->assertFalse(isset($errors['tableselect']), 'Option checker allows valid values for radio buttons.');
209 // Test with an invalid value.
210 list(, , $errors) = $this->formSubmitHelper($form, ['tableselect' => 'non_existing_value']);
211 $this->assertTrue(isset($errors['tableselect']), 'Option checker disallows invalid values for radio buttons.');
216 * Helper function for the option check test to submit a form while collecting errors.
218 * @param $form_element
219 * A form element to test.
221 * An array containing post data.
224 * An array containing the processed form, the form_state and any errors.
226 private function formSubmitHelper($form, $edit) {
227 $form_id = $this->randomMachineName();
228 $form_state = new FormState();
230 $form['op'] = ['#type' => 'submit', '#value' => t('Submit')];
231 // The form token CSRF protection should not interfere with this test, so we
232 // bypass it by setting the token to FALSE.
233 $form['#token'] = FALSE;
235 $edit['form_id'] = $form_id;
237 // Disable page redirect for forms submitted programmatically. This is a
238 // solution to skip the redirect step (there are no pages, then the redirect
240 $form_state->disableRedirect();
241 $form_state->setUserInput($edit);
242 $form_state->setFormObject(new StubForm($form_id, $form));
244 \Drupal::formBuilder()->prepareForm($form_id, $form, $form_state);
246 \Drupal::formBuilder()->processForm($form_id, $form, $form_state);
248 $errors = $form_state->getErrors();
250 // Clear errors and messages.
251 drupal_get_messages();
252 $form_state->clearErrors();
254 // Return the processed form together with form_state and errors
255 // to allow the caller lowlevel access to the form.
256 return [$form, $form_state, $errors];