You are here

protected function UserAccountFormFieldsTest::assertFieldOrder in Drupal 8

Same name and namespace in other branches
  1. 9 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 96

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
    ->assertEqual($name_index, $pass_index - 1, "'name' field ({$name_index}) appears before 'pass' field ({$pass_index}).");
  $this
    ->assertTrue($name_weight < $pass_weight, "'name' field weight ({$name_weight}) is smaller than 'pass' field weight ({$pass_weight}).");
}