diff --git a/core/modules/system/src/Tests/Form/ElementsLabelsTest.php b/core/modules/system/src/Tests/Form/ElementsLabelsTest.php index 970f299..98721bf 100644 --- a/core/modules/system/src/Tests/Form/ElementsLabelsTest.php +++ b/core/modules/system/src/Tests/Form/ElementsLabelsTest.php @@ -94,4 +94,32 @@ function testFormLabels() { $elements = $this->xpath('//div[@id="edit-form-radios-title-attribute"]'); $this->assertEqual($elements[0]['title'], 'Radios test' . ' (' . t('Required') . ')', 'Title attribute found.'); } + + /** + * Tests different display options for form element descriptions. + */ + function testFormDescriptions() { + $test = $this->drupalGet('form_test/form-descriptions'); + + // Check #description placement with #description_display='after'. + $field_id = 'edit-form-textfield-test-description-after'; + $description_id = $field_id . '--description'; + $elements = $this->xpath('//input[@id="' . $field_id . '" and @aria-describedby="' . $description_id . '"]/following-sibling::div[@id="' . $description_id . '"]'); + $this->assertTrue(isset($elements[0]), t('Properly places the #description element after the form item.')); + + // Check #description placement with #description_display='before'. + $field_id = 'edit-form-textfield-test-description-before'; + $description_id = $field_id . '--description'; + $elements = $this->xpath('//input[@id="' . $field_id . '" and @aria-describedby="' . $description_id . '"]/preceding-sibling::div[@id="' . $description_id . '"]'); + $this->assertTrue(isset($elements[0]), t('Properly places the #description element before the form item.')); + + // Check if the class is 'visually-hidden' on the form element description + // for the option with #description_display='invisible' and also check that + // the description is placed after the form element. + $field_id = 'edit-form-textfield-test-description-invisible'; + $description_id = $field_id . '--description'; + $elements = $this->xpath('//input[@id="' . $field_id . '" and @aria-describedby="' . $description_id . '"]/following-sibling::div[contains(@class, "visually-hidden")]'); + $this->assertTrue(isset($elements[0]), t('Properly renders the #description element visually-hidden.')); + } + } 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 86d7fc0..109d8f3 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 @@ -378,6 +378,14 @@ form_test.button_class: requirements: _access: 'TRUE' +form_test.description_display: + path: '/form_test/form-descriptions' + defaults: + _form: '\Drupal\form_test\Form\FormTestDescriptionForm' + _title: 'Form description test' + requirements: + _access: 'TRUE' + form_test.group_details: path: '/form-test/group-details' defaults: diff --git a/core/modules/system/tests/modules/form_test/src/Form/FormTestDescriptionForm.php b/core/modules/system/tests/modules/form_test/src/Form/FormTestDescriptionForm.php new file mode 100644 index 0000000..1b500bb --- /dev/null +++ b/core/modules/system/tests/modules/form_test/src/Form/FormTestDescriptionForm.php @@ -0,0 +1,63 @@ + 'textfield', + '#title' => 'Textfield test for description before element', + '#description' => 'Textfield test for description before element', + '#description_display' => 'before', + ); + + $form['form_textfield_test_description_after'] = array( + '#type' => 'textfield', + '#title' => 'Textfield test for description after element', + '#description' => 'Textfield test for description after element', + '#description_display' => 'after', + ); + + $form['form_textfield_test_description_invisible'] = array( + '#type' => 'textfield', + '#title' => 'Textfield test for visually-hidden description', + '#description' => 'Textfield test for visually-hidden description', + '#description_display' => 'invisible', + ); + + return $form; + } + + /** + * {@inheritdoc} + */ + public function submitForm(array &$form, FormStateInterface $form_state) { + // The test that uses this form does not submit the form so this is empty. + } + +}