You are here

public function FormElementMaxlengthTest::testAttributes in Drupal 10

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

Ensures maxlength attribute can be used in compatible elements.

File

core/modules/system/tests/src/Kernel/Form/FormElementMaxlengthTest.php, line 65

Class

FormElementMaxlengthTest
Tests the maxlength HTML attribute on form elements.

Namespace

Drupal\Tests\system\Kernel\Form

Code

public function testAttributes() {

  /** @var \Drupal\Core\Form\FormBuilderInterface $form_builder */
  $form_builder = $this->container
    ->get('form_builder');
  $form_state = new FormState();
  $elements = $form_builder
    ->buildForm($this, $form_state);
  $this
    ->render($elements);
  $css_selector_converter = new CssSelectorConverter();
  $elements = $this
    ->xpath($css_selector_converter
    ->toXPath('input[name=title][maxlength=255]'));
  $this
    ->assertCount(1, $elements, 'Text field has correct maxlength in form.');
  $elements = $this
    ->xpath($css_selector_converter
    ->toXPath('textarea[name=description][maxlength=255]'));
  $this
    ->assertCount(1, $elements, 'Textarea field has correct maxlength in form.');
}