View source
<?php
namespace Drupal\system\Tests\Form;
use Drupal\Core\Form\FormState;
use Drupal\simpletest\WebTestBase;
class ElementsTableSelectTest extends WebTestBase {
public static $modules = array(
'form_test',
);
function testMultipleTrue() {
$this
->drupalGet('form_test/tableselect/multiple-true');
$this
->assertNoText(t('Empty text.'), 'Empty text should not be displayed.');
$this
->assertFieldByXPath('//th[@class="select-all"]', NULL, 'Presence of the "Select all" checkbox.');
$rows = array(
'row1',
'row2',
'row3',
);
foreach ($rows as $row) {
$this
->assertFieldByXPath('//input[@type="checkbox"]', $row, format_string('Checkbox for value @row.', array(
'@row' => $row,
)));
}
}
function testMultipleFalse() {
$this
->drupalGet('form_test/tableselect/multiple-false');
$this
->assertNoText(t('Empty text.'), 'Empty text should not be displayed.');
$this
->assertNoFieldByXPath('//th[@class="select-all"]', '', 'Absence of the "Select all" checkbox.');
$rows = array(
'row1',
'row2',
'row3',
);
foreach ($rows as $row) {
$this
->assertFieldByXPath('//input[@type="radio"]', $row, format_string('Radio button for value @row.', array(
'@row' => $row,
)));
}
}
function testTableselectColSpan() {
$this
->drupalGet('form_test/tableselect/colspan');
$this
->assertText(t('Three'), 'Presence of the third column');
$this
->assertNoText(t('Four'), 'Absence of a fourth column');
$table_head = $this
->xpath('//thead');
$this
->assertEqual(count($table_head[0]->tr->th), 4, 'There are four column headers');
$table_body = $this
->xpath('//tbody');
for ($i = 0; $i <= 1; $i++) {
$this
->assertEqual(count($table_body[0]->tr[$i]->td), 5, format_string('There are five cells in row @row.', array(
'@row' => $i,
)));
}
$this
->assertEqual(count($table_body[0]->tr[2]->td), 3, 'There are three cells in row 3.');
}
function testEmptyText() {
$this
->drupalGet('form_test/tableselect/empty-text');
$this
->assertText(t('Empty text.'), 'Empty text should be displayed.');
}
function testMultipleTrueSubmit() {
$edit = array();
$edit['tableselect[row1]'] = TRUE;
$this
->drupalPostForm('form_test/tableselect/multiple-true', $edit, 'Submit');
$this
->assertText(t('Submitted: row1 = row1'), 'Checked checkbox row1');
$this
->assertText(t('Submitted: row2 = 0'), 'Unchecked checkbox row2.');
$this
->assertText(t('Submitted: row3 = 0'), 'Unchecked checkbox row3.');
$edit['tableselect[row1]'] = TRUE;
$edit['tableselect[row3]'] = TRUE;
$this
->drupalPostForm('form_test/tableselect/multiple-true', $edit, 'Submit');
$this
->assertText(t('Submitted: row1 = row1'), 'Checked checkbox row1.');
$this
->assertText(t('Submitted: row2 = 0'), 'Unchecked checkbox row2.');
$this
->assertText(t('Submitted: row3 = row3'), 'Checked checkbox row3.');
}
function testMultipleFalseSubmit() {
$edit['tableselect'] = 'row1';
$this
->drupalPostForm('form_test/tableselect/multiple-false', $edit, 'Submit');
$this
->assertText(t('Submitted: row1'), 'Selected radio button');
}
function testAdvancedSelect() {
$this
->drupalGet('form_test/tableselect/advanced-select/multiple-true-default');
$this
->assertFieldByXPath('//th[@class="select-all"]', NULL, 'Display a "Select all" checkbox by default when #multiple is TRUE.');
$this
->drupalGet('form_test/tableselect/advanced-select/multiple-true-no-advanced-select');
$this
->assertNoFieldByXPath('//th[@class="select-all"]', NULL, 'Do not display a "Select all" checkbox when #js_select is FALSE.');
$this
->drupalGet('form_test/tableselect/advanced-select/multiple-false-default');
$this
->assertNoFieldByXPath('//th[@class="select-all"]', NULL, 'Do not display a "Select all" checkbox when #multiple is FALSE.');
$this
->drupalGet('form_test/tableselect/advanced-select/multiple-false-advanced-select');
$this
->assertNoFieldByXPath('//th[@class="select-all"]', NULL, 'Do not display a "Select all" checkbox when #multiple is FALSE, even when #js_select is TRUE.');
}
function testMultipleTrueOptionchecker() {
list($header, $options) = _form_test_tableselect_get_data();
$form['tableselect'] = array(
'#type' => 'tableselect',
'#header' => $header,
'#options' => $options,
);
list(, , $errors) = $this
->formSubmitHelper($form, array(
'tableselect' => array(
'row1' => 'row1',
),
));
$this
->assertFalse(isset($errors['tableselect']), 'Option checker allows valid values for checkboxes.');
list(, , $errors) = $this
->formSubmitHelper($form, array(
'tableselect' => array(
'non_existing_value' => 'non_existing_value',
),
));
$this
->assertTrue(isset($errors['tableselect']), 'Option checker disallows invalid values for checkboxes.');
}
function testMultipleFalseOptionchecker() {
list($header, $options) = _form_test_tableselect_get_data();
$form['tableselect'] = array(
'#type' => 'tableselect',
'#header' => $header,
'#options' => $options,
'#multiple' => FALSE,
);
list(, , $errors) = $this
->formSubmitHelper($form, array(
'tableselect' => 'row1',
));
$this
->assertFalse(isset($errors['tableselect']), 'Option checker allows valid values for radio buttons.');
list(, , $errors) = $this
->formSubmitHelper($form, array(
'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'] = array(
'#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_get_messages();
$form_state
->clearErrors();
return array(
$form,
$form_state,
$errors,
);
}
}