View source
<?php
namespace Drupal\Tests\system\Functional\Form;
use Drupal\Core\Form\FormState;
use Drupal\Tests\BrowserTestBase;
class ElementsTableSelectTest extends BrowserTestBase {
protected static $modules = [
'form_test',
];
protected $defaultTheme = 'stark';
public function testMultipleTrue() {
$this
->drupalGet('form_test/tableselect/multiple-true');
$this
->assertSession()
->responseNotContains('Empty text.');
$this
->assertNotEmpty($this
->xpath('//th[@class="select-all"]'), 'Presence of the "Select all" checkbox.');
$rows = [
'row1',
'row2',
'row3',
];
foreach ($rows as $row) {
$this
->assertNotEmpty($this
->xpath('//input[@type="checkbox"]', [
$row,
]), "Checkbox for the value {$row}.");
}
}
public function testMultipleFalse() {
$this
->drupalGet('form_test/tableselect/multiple-false');
$this
->assertSession()
->pageTextNotContains('Empty text.');
$this
->assertSession()
->elementNotExists('xpath', '//th[@class="select-all"]');
$rows = [
'row1',
'row2',
'row3',
];
foreach ($rows as $row) {
$this
->assertNotEmpty($this
->xpath('//input[@type="radio"]', [
$row,
], "Radio button value: {$row}"));
}
}
public function testTableSelectColSpan() {
$this
->drupalGet('form_test/tableselect/colspan');
$this
->assertSession()
->pageTextContains('Three');
$this
->assertSession()
->pageTextNotContains('Four');
$table_head = $this
->xpath('//thead/tr/th');
$this
->assertCount(4, $table_head, 'There are four column headers');
for ($i = 0; $i <= 1; $i++) {
$this
->assertCount(5, $this
->xpath('//tbody/tr[' . ($i + 1) . ']/td'), 'There are five cells in row ' . $i);
}
$this
->assertCount(3, $this
->xpath('//tbody/tr[3]/td'), 'There are three cells in row 3.');
}
public function testEmptyText() {
$this
->drupalGet('form_test/tableselect/empty-text');
$this
->assertSession()
->pageTextContains('Empty text.');
}
public function testMultipleTrueSubmit() {
$edit = [];
$edit['tableselect[row1]'] = TRUE;
$this
->drupalGet('form_test/tableselect/multiple-true');
$this
->submitForm($edit, 'Submit');
$assert_session = $this
->assertSession();
$assert_session
->pageTextContains('Submitted: row1 = row1');
$assert_session
->pageTextContains('Submitted: row2 = 0');
$assert_session
->pageTextContains('Submitted: row3 = 0');
$edit['tableselect[row1]'] = TRUE;
$edit['tableselect[row3]'] = TRUE;
$this
->drupalGet('form_test/tableselect/multiple-true');
$this
->submitForm($edit, 'Submit');
$assert_session
->pageTextContains('Submitted: row1 = row1');
$assert_session
->pageTextContains('Submitted: row2 = 0');
$assert_session
->pageTextContains('Submitted: row3 = row3');
}
public function testMultipleFalseSubmit() {
$edit['tableselect'] = 'row1';
$this
->drupalGet('form_test/tableselect/multiple-false');
$this
->submitForm($edit, 'Submit');
$this
->assertSession()
->pageTextContains('Submitted: row1');
}
public function testAdvancedSelect() {
$this
->drupalGet('form_test/tableselect/advanced-select/multiple-true-default');
$this
->assertSession()
->elementExists('xpath', '//th[@class="select-all"]');
$this
->drupalGet('form_test/tableselect/advanced-select/multiple-true-no-advanced-select');
$this
->assertSession()
->elementNotExists('xpath', '//th[@class="select-all"]');
$this
->drupalGet('form_test/tableselect/advanced-select/multiple-false-default');
$this
->assertSession()
->elementNotExists('xpath', '//th[@class="select-all"]');
$this
->drupalGet('form_test/tableselect/advanced-select/multiple-false-advanced-select');
$this
->assertSession()
->elementNotExists('xpath', '//th[@class="select-all"]');
}
public function testMultipleTrueOptionchecker() {
list($header, $options) = _form_test_tableselect_get_data();
$form['tableselect'] = [
'#type' => 'tableselect',
'#header' => $header,
'#options' => $options,
];
list(, , $errors) = $this
->formSubmitHelper($form, [
'tableselect' => [
'row1' => 'row1',
],
]);
$this
->assertFalse(isset($errors['tableselect']), 'Option checker allows valid values for checkboxes.');
list(, , $errors) = $this
->formSubmitHelper($form, [
'tableselect' => [
'non_existing_value' => 'non_existing_value',
],
]);
$this
->assertTrue(isset($errors['tableselect']), 'Option checker disallows invalid values for checkboxes.');
}
public function testMultipleFalseOptionchecker() {
list($header, $options) = _form_test_tableselect_get_data();
$form['tableselect'] = [
'#type' => 'tableselect',
'#header' => $header,
'#options' => $options,
'#multiple' => FALSE,
];
list(, , $errors) = $this
->formSubmitHelper($form, [
'tableselect' => 'row1',
]);
$this
->assertFalse(isset($errors['tableselect']), 'Option checker allows valid values for radio buttons.');
list(, , $errors) = $this
->formSubmitHelper($form, [
'tableselect' => 'non_existing_value',
]);
$this
->assertTrue(isset($errors['tableselect']), 'Option checker disallows invalid values for radio buttons.');
}
private function formSubmitHelper($form, $edit) {
$form_id = $this
->randomMachineName();
$form_state = new FormState();
$form['op'] = [
'#type' => 'submit',
'#value' => t('Submit'),
];
$form['#token'] = FALSE;
$edit['form_id'] = $form_id;
$form_state
->disableRedirect();
$form_state
->setUserInput($edit);
$form_state
->setFormObject(new StubForm($form_id, $form));
\Drupal::formBuilder()
->prepareForm($form_id, $form, $form_state);
\Drupal::formBuilder()
->processForm($form_id, $form, $form_state);
$errors = $form_state
->getErrors();
\Drupal::messenger()
->deleteAll();
$form_state
->clearErrors();
return [
$form,
$form_state,
$errors,
];
}
}