function UserRoleAdminTest::testRoleWeightOrdering

Test user role weight change operation and ordering.

File

drupal/core/modules/user/lib/Drupal/user/Tests/UserRoleAdminTest.php, line 83
Definition of Drupal\user\Tests\UserRoleAdminTest.

Class

UserRoleAdminTest
Test case to test adding, editing and deleting roles.

Namespace

Drupal\user\Tests

Code

function testRoleWeightOrdering() {
  $this
    ->drupalLogin($this->admin_user);
  $roles = user_roles();
  $weight = count($roles);
  $new_role_weights = array();
  $saved_rids = array();

  // Change the role weights to make the roles in reverse order.
  $edit = array();
  foreach ($roles as $role) {
    $edit['entities[' . $role
      ->id() . '][weight]'] = $weight;
    $new_role_weights[$role
      ->id()] = $weight;
    $saved_rids[] = $role->id;
    $weight--;
  }
  $this
    ->drupalPost('admin/people/roles', $edit, t('Save order'));
  $this
    ->assertText(t('The role settings have been updated.'), 'The role settings form submitted successfully.');

  // Load up the user roles with the new weights.
  drupal_static_reset('user_roles');
  $roles = user_roles();
  $rids = array();

  // Test that the role weights have been correctly saved.
  foreach ($roles as $role) {
    $this
      ->assertEqual($role->weight, $new_role_weights[$role
      ->id()]);
    $rids[] = $role->id;
  }

  // The order of the roles should be reversed.
  $this
    ->assertIdentical($rids, array_reverse($saved_rids));
}