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()
- 8.9.x 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()
Tests validation of #type 'select' elements.
File
-
core/
modules/ system/ tests/ src/ Functional/ Form/ FormTest.php, line 407
Class
- FormTest
- Tests various form element validation mechanisms.
Namespace
Drupal\Tests\system\Functional\FormCode
public function testSelect() : void {
$form = \Drupal::formBuilder()->getForm('Drupal\\form_test\\Form\\FormTestSelectForm');
$this->drupalGet('form-test/select');
// Verify that the options are escaped as expected.
$this->assertSession()
->assertEscaped('<strong>four</strong>');
$this->assertSession()
->responseNotContains('<strong>four</strong>');
// Posting without any values should throw validation errors.
$this->submitForm([], '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->assertSession()
->pageTextNotContains($form[$key]['#title'] . ' field is required.');
}
$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->assertSession()
->pageTextContains($form[$key]['#title'] . ' field is required.');
}
// 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->submitForm($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->assertSame($value, $values[$key], sprintf('%s: %s is equal to %s.', $key, var_export($values[$key], TRUE), var_export($value, TRUE)));
}
}
Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.