diff --git a/core/modules/filter/src/Tests/FilterFormTest.php b/core/modules/filter/src/Tests/FilterFormTest.php index 460b8a9..dde1698 100644 --- a/core/modules/filter/src/Tests/FilterFormTest.php +++ b/core/modules/filter/src/Tests/FilterFormTest.php @@ -93,6 +93,9 @@ protected function doFilterFormTestAsAdmin() { // 'filter_test' as the default value in this case. $this->assertOptions('edit-all-formats-default-format--2', $formats, 'filter_test'); $this->assertEnabledTextarea('edit-all-formats-default-missing-value'); + // If a missing format is set as the default, administrators must select a + // valid replacement format. + $this->assertRequiredSelectAndOptions('edit-all-formats-default-missing-format--2', $formats); // Test a text format element with a predefined list of formats. $formats = ['full_html', 'filter_test']; @@ -101,7 +104,9 @@ protected function doFilterFormTestAsAdmin() { $this->assertEnabledTextarea('edit-restricted-formats-default-value'); $this->assertOptions('edit-restricted-formats-default-format--2', $formats, 'full_html'); $this->assertEnabledTextarea('edit-restricted-formats-default-missing-value'); + $this->assertRequiredSelectAndOptions('edit-restricted-formats-default-missing-format--2', $formats); $this->assertEnabledTextarea('edit-restricted-formats-default-disallowed-value'); + $this->assertRequiredSelectAndOptions('edit-restricted-formats-default-disallowed-format--2', $formats); // Test a text format element with a fixed format. $formats = ['filter_test']; @@ -113,7 +118,9 @@ protected function doFilterFormTestAsAdmin() { // If the select has a missing or disallowed format, administrators must // explicitly choose the format. $this->assertEnabledTextarea('edit-single-format-default-missing-value'); + $this->assertRequiredSelectAndOptions('edit-single-format-default-missing-format--2', $formats); $this->assertEnabledTextarea('edit-single-format-default-disallowed-value'); + $this->assertRequiredSelectAndOptions('edit-single-format-default-disallowed-format--2', $formats); } /** @@ -243,7 +250,7 @@ protected function assertOptions($id, array $expected_options, $selected) { * TRUE if the assertion passed; FALSE otherwise. */ protected function assertRequiredSelectAndOptions($id, array $options) { - $select = $this->xpath('//select[@id=:id and contains(@required, "required")]', [ + $select = $this->xpath('//select[@id=:id and contains(@class, "required")]', [ ':id' => $id, ]); $select = reset($select); diff --git a/core/modules/system/src/Tests/Form/FormTest.php b/core/modules/system/src/Tests/Form/FormTest.php index b0e6a9d..a61c818 100644 --- a/core/modules/system/src/Tests/Form/FormTest.php +++ b/core/modules/system/src/Tests/Form/FormTest.php @@ -263,6 +263,7 @@ public function testInputWithInvalidToken() { 'textarea' => $this->randomString() . "\n", 'form_token' => 'invalid token', ]; + $this->drupalPostForm(Url::fromRoute('form_test.required'), $edit, 'Submit'); $this->assertFieldByXpath('//div[contains(@class, "error")]', NULL, 'Error message is displayed with invalid token even when required fields are filled.'); $this->assertText('The form has become outdated. Copy any unsaved work in the form below'); $this->assertFieldByName('textfield', $edit['textfield']); diff --git a/core/modules/system/tests/modules/form_test/form_test.routing.yml b/core/modules/system/tests/modules/form_test/form_test.routing.yml index 099d093..4233b1b 100644 --- a/core/modules/system/tests/modules/form_test/form_test.routing.yml +++ b/core/modules/system/tests/modules/form_test/form_test.routing.yml @@ -393,6 +393,14 @@ form_test.checkboxes_zero: requirements: _access: 'TRUE' +form_test.required: + path: '/form-test/required-fields' + defaults: + _form: '\Drupal\form_test\Form\FormTestRequiredFieldsForm' + _title: 'Required' + requirements: + _access: 'TRUE' + form_test.button_class: path: '/form-test/button-class' defaults: diff --git a/core/modules/system/tests/modules/form_test/src/Form/FormTestRequiredFieldsForm.php b/core/modules/system/tests/modules/form_test/src/Form/FormTestRequiredFieldsForm.php new file mode 100644 index 0000000..bdbc343 --- /dev/null +++ b/core/modules/system/tests/modules/form_test/src/Form/FormTestRequiredFieldsForm.php @@ -0,0 +1,44 @@ + $type, + '#required' => TRUE, + '#title' => $type, + ]; + } + $form['submit'] = [ + '#type' => 'submit', + '#value' => 'Submit', + ]; + return $form; + } + + /** + * {@inheritdoc} + */ + public function submitForm(array &$form, FormStateInterface $form_state) { + } + +}