function UserRoleAdminTest::testRoleWeightOrdering

Same name in other branches
  1. 9 core/modules/user/tests/src/Functional/UserRoleAdminTest.php \Drupal\Tests\user\Functional\UserRoleAdminTest::testRoleWeightOrdering()
  2. 8.9.x core/modules/user/tests/src/Functional/UserRoleAdminTest.php \Drupal\Tests\user\Functional\UserRoleAdminTest::testRoleWeightOrdering()
  3. 11.x core/modules/user/tests/src/Functional/UserRoleAdminTest.php \Drupal\Tests\user\Functional\UserRoleAdminTest::testRoleWeightOrdering()

Tests user role weight change operation and ordering.

File

core/modules/user/tests/src/Functional/UserRoleAdminTest.php, line 114

Class

UserRoleAdminTest
Tests adding, editing and deleting user roles and changing role weights.

Namespace

Drupal\Tests\user\Functional

Code

public function testRoleWeightOrdering() : void {
    $this->drupalLogin($this->adminUser);
    $roles = Role::loadMultiple();
    $weight = count($roles);
    $new_role_weights = [];
    $saved_rids = [];
    // Change the role weights to make the roles in reverse order.
    $edit = [];
    foreach ($roles as $role) {
        $edit['entities[' . $role->id() . '][weight]'] = $weight;
        $new_role_weights[$role->id()] = $weight;
        $saved_rids[] = $role->id();
        $weight--;
    }
    $this->drupalGet('admin/people/roles');
    $this->submitForm($edit, 'Save');
    $this->assertSession()
        ->pageTextContains('The role settings have been updated.');
    // Load up the user roles with the new weights.
    $roles = Role::loadMultiple();
    $rids = [];
    // Test that the role weights have been correctly saved.
    foreach ($roles as $role) {
        $this->assertEquals($role->getWeight(), $new_role_weights[$role->id()]);
        $rids[] = $role->id();
    }
    // The order of the roles should be reversed.
    $this->assertSame(array_reverse($saved_rids), $rids);
}

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