1 user.test UserPermissionsTestCase::testUserPermissionChanges()

Change user permissions and check user_access().

File

core/modules/user/tests/user.test, line 1422
Tests for user.module.

Class

UserPermissionsTestCase

Code

function testUserPermissionChanges() {
  $this->backdropLogin($this->admin_user);
  $role_name = $this->admin_role_name;
  $account = $this->admin_user;

  // Add a permission.
  $this->assertFalse(user_access('administer nodes', $account), 'User does not have "administer nodes" permission.');
  $edit = array();
  $edit[$role_name . '[administer nodes]'] = TRUE;
  $this->backdropPost('admin/config/people/permissions', $edit, t('Save permissions'));
  $this->assertText(t('The changes have been saved.'), 'Successful save message displayed.');
  backdrop_static_reset('user_roles');
  backdrop_static_reset('user_access');
  // Check the role and user account both have the permission.
  $this->assertTrue(user_role_has_permission($role_name, 'administer nodes'), 'Role now has "administer nodes" permission.');
  $this->assertTrue(user_access('administer nodes', $account), 'User now has "administer nodes" permission.');

  // Remove a permission.
  $this->assertTrue(user_role_has_permission($role_name, 'access user profiles'), 'Role has "access user profiles" permission.');
  $this->assertTrue(user_access('access user profiles', $account), 'User has "access user profiles" permission.');
  $edit = array();
  $edit[$role_name . '[access user profiles]'] = FALSE;
  $this->backdropPost('admin/config/people/permissions', $edit, t('Save permissions'));
  $this->assertText(t('The changes have been saved.'), 'Successful save message displayed.');
  backdrop_static_reset('user_roles');
  backdrop_static_reset('user_access');
  $this->assertFalse(user_role_has_permission($role_name, 'access user profiles'), 'Role no longer has "access user profiles" permission.');
  $this->assertFalse(user_access('access user profiles', $account), 'User no longer has "access user profiles" permission.');
}