You are here

function UserPermissionsTest::testUserRoleChangePermissions in Zircon Profile 8.0

Same name and namespace in other branches
  1. 8 core/modules/user/src/Tests/UserPermissionsTest.php \Drupal\user\Tests\UserPermissionsTest::testUserRoleChangePermissions()

Verify proper permission changes by user_role_change_permissions().

File

core/modules/user/src/Tests/UserPermissionsTest.php, line 143
Contains \Drupal\user\Tests\UserPermissionsTest.

Class

UserPermissionsTest
Verify that role permissions can be added and removed via the permissions page.

Namespace

Drupal\user\Tests

Code

function testUserRoleChangePermissions() {
  $permissions_hash_generator = $this->container
    ->get('user_permissions_hash_generator');
  $rid = $this->rid;
  $account = $this->adminUser;
  $previous_permissions_hash = $permissions_hash_generator
    ->generate($account);

  // Verify current permissions.
  $this
    ->assertFalse($account
    ->hasPermission('administer users'), 'User does not have "administer users" permission.');
  $this
    ->assertTrue($account
    ->hasPermission('access user profiles'), 'User has "access user profiles" permission.');
  $this
    ->assertTrue($account
    ->hasPermission('administer site configuration'), 'User has "administer site configuration" permission.');

  // Change permissions.
  $permissions = array(
    'administer users' => 1,
    'access user profiles' => 0,
  );
  user_role_change_permissions($rid, $permissions);

  // Verify proper permission changes.
  $this
    ->assertTrue($account
    ->hasPermission('administer users'), 'User now has "administer users" permission.');
  $this
    ->assertFalse($account
    ->hasPermission('access user profiles'), 'User no longer has "access user profiles" permission.');
  $this
    ->assertTrue($account
    ->hasPermission('administer site configuration'), 'User still has "administer site configuration" permission.');

  // Verify the permissions hash has changed.
  $current_permissions_hash = $permissions_hash_generator
    ->generate($account);
  $this
    ->assertNotEqual($previous_permissions_hash, $current_permissions_hash, 'Permissions hash has changed.');
}