You are here

protected function UserAccountFormFieldsTest::assertFieldOrder in Drupal 9

Same name and namespace in other branches
  1. 8 core/modules/user/tests/src/Kernel/UserAccountFormFieldsTest.php \Drupal\Tests\user\Kernel\UserAccountFormFieldsTest::assertFieldOrder()

Asserts that the 'name' form element is directly before the 'pass' element.

Parameters

array $elements: A form array section that contains the user account form elements.

3 calls to UserAccountFormFieldsTest::assertFieldOrder()
UserAccountFormFieldsTest::testInstallConfigureForm in core/modules/user/tests/src/Kernel/UserAccountFormFieldsTest.php
Tests the root user account form section in the "Configure site" form.
UserAccountFormFieldsTest::testUserEditForm in core/modules/user/tests/src/Kernel/UserAccountFormFieldsTest.php
Tests the user edit form.
UserAccountFormFieldsTest::testUserRegistrationForm in core/modules/user/tests/src/Kernel/UserAccountFormFieldsTest.php
Tests the user registration form.

File

core/modules/user/tests/src/Kernel/UserAccountFormFieldsTest.php, line 93

Class

UserAccountFormFieldsTest
Verifies that the field order in user account forms is compatible with password managers of web browsers.

Namespace

Drupal\Tests\user\Kernel

Code

protected function assertFieldOrder(array $elements) {
  $name_index = 0;
  $name_weight = 0;
  $pass_index = 0;
  $pass_weight = 0;
  $index = 0;
  foreach ($elements as $key => $element) {
    if ($key === 'name') {
      $name_index = $index;
      $name_weight = $element['#weight'];
      $this
        ->assertTrue($element['#sorted'], "'name' field is #sorted.");
    }
    elseif ($key === 'pass') {
      $pass_index = $index;
      $pass_weight = $element['#weight'];
      $this
        ->assertTrue($element['#sorted'], "'pass' field is #sorted.");
    }
    $index++;
  }
  $this
    ->assertEquals($pass_index - 1, $name_index, "'name' field ({$name_index}) appears before 'pass' field ({$pass_index}).");
  $this
    ->assertLessThan($pass_weight, $name_weight, "'name' field weight ({$name_weight}) should be smaller than 'pass' field weight ({$pass_weight}).");
}