function UserAccountFormFieldsTest::assertFieldOrder

Same name in other branches
  1. 8.9.x core/modules/user/tests/src/Kernel/UserAccountFormFieldsTest.php \Drupal\Tests\user\Kernel\UserAccountFormFieldsTest::assertFieldOrder()
  2. 10 core/modules/user/tests/src/Kernel/UserAccountFormFieldsTest.php \Drupal\Tests\user\Kernel\UserAccountFormFieldsTest::assertFieldOrder()
  3. 11.x 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.

@internal

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 94

Class

UserAccountFormFieldsTest
Verifies the field order in user account forms.

Namespace

Drupal\Tests\user\Kernel

Code

protected function assertFieldOrder(array $elements) : void {
    $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}).");
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.