You are here

public function ElementsLabelsTest::testFormDescriptions in Drupal 9

Same name and namespace in other branches
  1. 8 core/modules/system/tests/src/Functional/Form/ElementsLabelsTest.php \Drupal\Tests\system\Functional\Form\ElementsLabelsTest::testFormDescriptions()

Tests different display options for form element descriptions.

File

core/modules/system/tests/src/Functional/Form/ElementsLabelsTest.php, line 104

Class

ElementsLabelsTest
Tests form element labels, required markers and associated output.

Namespace

Drupal\Tests\system\Functional\Form

Code

public function testFormDescriptions() {
  $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';
  $this
    ->assertSession()
    ->elementExists('xpath', '//input[@id="' . $field_id . '" and @aria-describedby="' . $description_id . '"]/following-sibling::div[@id="' . $description_id . '"]');

  // Check #description placement with #description_display='before'.
  $field_id = 'edit-form-textfield-test-description-before';
  $description_id = $field_id . '--description';
  $this
    ->assertSession()
    ->elementExists('xpath', '//input[@id="' . $field_id . '" and @aria-describedby="' . $description_id . '"]/preceding-sibling::div[@id="' . $description_id . '"]');

  // 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';
  $this
    ->assertSession()
    ->elementExists('xpath', '//input[@id="' . $field_id . '" and @aria-describedby="' . $description_id . '"]/following-sibling::div[contains(@class, "visually-hidden")]');
}