You are here

public function FormElementLabelTest::testAttributes in Drupal 9

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

Ensures that attributes can be placed for form element label.

File

core/modules/system/tests/src/Kernel/Form/FormElementLabelTest.php, line 23

Class

FormElementLabelTest
Tests for form_element_label theme hook.

Namespace

Drupal\Tests\system\Kernel\Form

Code

public function testAttributes() {
  $render_array = [
    '#type' => 'label',
    '#attributes' => [
      'class' => [
        'kitten',
      ],
    ],
    '#title' => 'Kittens',
    '#title_display' => 'above',
  ];
  $css_selector_converter = new CssSelectorConverter();
  $this
    ->render($render_array);
  $elements = $this
    ->xpath($css_selector_converter
    ->toXPath('.kitten'));
  $this
    ->assertCount(1, $elements);

  // Add label attributes to a form element.
  $render_array = [
    '#type' => 'textfield',
    '#label_attributes' => [
      'class' => [
        'meow',
      ],
    ],
    '#title' => 'Kitten sounds',
  ];
  $this
    ->render($render_array);
  $elements = $this
    ->xpath($css_selector_converter
    ->toXPath('label.meow'));
  $this
    ->assertCount(1, $elements);
}