function FormTest::testSelect
Same name in other branches
- 9 core/modules/system/tests/src/Functional/Form/FormTest.php \Drupal\Tests\system\Functional\Form\FormTest::testSelect()
- 10 core/modules/system/tests/src/Functional/Form/FormTest.php \Drupal\Tests\system\Functional\Form\FormTest::testSelect()
- 11.x core/modules/system/tests/src/Functional/Form/FormTest.php \Drupal\Tests\system\Functional\Form\FormTest::testSelect()
Tests validation of #type 'select' elements.
File
-
core/
modules/ system/ tests/ src/ Functional/ Form/ FormTest.php, line 398
Class
- FormTest
- Tests various form element validation mechanisms.
Namespace
Drupal\Tests\system\Functional\FormCode
public function testSelect() {
$form = \Drupal::formBuilder()->getForm('Drupal\\form_test\\Form\\FormTestSelectForm');
$this->drupalGet('form-test/select');
// Verify that the options are escaped as expected.
$this->assertEscaped('<strong>four</strong>');
$this->assertNoRaw('<strong>four</strong>');
// Posting without any values should throw validation errors.
$this->drupalPostForm(NULL, [], 'Submit');
$no_errors = [
'select',
'select_required',
'select_optional',
'empty_value',
'empty_value_one',
'no_default_optional',
'no_default_empty_option_optional',
'no_default_empty_value_optional',
'multiple',
'multiple_no_default',
];
foreach ($no_errors as $key) {
$this->assertNoText(t('@name field is required.', [
'@name' => $form[$key]['#title'],
]));
}
$expected_errors = [
'no_default',
'no_default_empty_option',
'no_default_empty_value',
'no_default_empty_value_one',
'multiple_no_default_required',
];
foreach ($expected_errors as $key) {
$this->assertText(t('@name field is required.', [
'@name' => $form[$key]['#title'],
]));
}
// Post values for required fields.
$edit = [
'no_default' => 'three',
'no_default_empty_option' => 'three',
'no_default_empty_value' => 'three',
'no_default_empty_value_one' => 'three',
'multiple_no_default_required[]' => 'three',
];
$this->drupalPostForm(NULL, $edit, 'Submit');
$values = Json::decode($this->getSession()
->getPage()
->getContent());
// Verify expected values.
$expected = [
'select' => 'one',
'empty_value' => 'one',
'empty_value_one' => 'one',
'no_default' => 'three',
'no_default_optional' => 'one',
'no_default_optional_empty_value' => '',
'no_default_empty_option' => 'three',
'no_default_empty_option_optional' => '',
'no_default_empty_value' => 'three',
'no_default_empty_value_one' => 'three',
'no_default_empty_value_optional' => 0,
'multiple' => [
'two' => 'two',
],
'multiple_no_default' => [],
'multiple_no_default_required' => [
'three' => 'three',
],
];
foreach ($expected as $key => $value) {
$this->assertIdentical($values[$key], $value, new FormattableMarkup('@name: @actual is equal to @expected.', [
'@name' => $key,
'@actual' => var_export($values[$key], TRUE),
'@expected' => var_export($value, TRUE),
]));
}
}
Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.