You are here

public function ElementsFieldsetTest::testFieldsetDescriptions in Drupal 9

Tests different display options for fieldset element descriptions.

File

core/modules/system/tests/src/Kernel/Form/ElementsFieldsetTest.php, line 112

Class

ElementsFieldsetTest
Tests fieldset element rendering and description placement.

Namespace

Drupal\Tests\system\Kernel\Form

Code

public function testFieldsetDescriptions() {
  $form_state = new FormState();
  $form = \Drupal::formBuilder()
    ->getForm($this);
  $this
    ->render($form);

  // Check #description placement with #description_display not set. By
  // default, the #description should appear after any fieldset elements.
  $field_id = 'edit-fieldset-default';
  $description_id = $field_id . '--description';
  $elements = $this
    ->xpath('//fieldset[@id="' . $field_id . '" and @aria-describedby="' . $description_id . '"]//div[@id="edit-meta-default"]/following-sibling::div[@id="' . $description_id . '"]');
  $this
    ->assertCount(1, $elements);

  // Check #description placement with #description_display set to 'before'.
  $field_id = 'edit-fieldset-before';
  $description_id = $field_id . '--description';
  $elements = $this
    ->xpath('//fieldset[@id="' . $field_id . '" and @aria-describedby="' . $description_id . '"]//div[@id="edit-meta-before"]/preceding-sibling::div[@id="' . $description_id . '"]');
  $this
    ->assertCount(1, $elements);

  // Check #description placement with #description_display set to 'after'.
  $field_id = 'edit-fieldset-after';
  $description_id = $field_id . '--description';
  $elements = $this
    ->xpath('//fieldset[@id="' . $field_id . '" and @aria-describedby="' . $description_id . '"]//div[@id="edit-meta-after"]/following-sibling::div[@id="' . $description_id . '"]');
  $this
    ->assertCount(1, $elements);

  // Check if the 'visually-hidden' class is set on the fieldset description
  // with #description_display set to 'invisible'. Also check that the
  // description is placed after the form element.
  $field_id = 'edit-fieldset-invisible';
  $description_id = $field_id . '--description';
  $elements = $this
    ->xpath('//fieldset[@id="' . $field_id . '" and @aria-describedby="' . $description_id . '"]//div[@id="edit-meta-invisible"]/following-sibling::div[contains(@class, "visually-hidden")]');
  $this
    ->assertCount(1, $elements);
  \Drupal::formBuilder()
    ->submitForm($this, $form_state);
  $errors = $form_state
    ->getErrors();
  $this
    ->assertEmpty($errors);
}