Index: modules/user/user.test =================================================================== --- modules/user/user.test (revision 14803) +++ modules/user/user.test (working copy) @@ -1,5 +1,5 @@ 'User role assignment', + 'description' => 'Test assigning user roles to users.', + 'group' => 'User', + ); + } + + function setup() { + parent::setUp(); + $this->admin_user = $this->drupalCreateUser(array('administer permissions', 'administer users')); + } + + /** + * Tests the ability to assign/revoke roles to new users + */ + function testRoleAssignment() { + $this->drupalLogin($this->admin_user); + + $myTestUser = $this->drupalCreateUser(); + + // Loads a role from the database, then modify a role to save back to the database. We do this to ensure the test's sample data doesn't become obsolete upon a schema change. + $newRole = user_role_load(2); // authenticated user + + // We're going to check to make sure that we're loading the authenticated user role, because if we're not, then we can't guarantee the data for the rest of the test. + $this->assertTrue($newRole->name == 'authenticated user', t('User role loaded from the database is an authenticated user')); + + unset($newRole->rid); + $newRole->name = "Test Role"; + user_role_save($newRole); + $newRoleFromDB = user_role_load_by_name("Test Role"); + $this->assertTrue(is_object($newRoleFromDB), t('Role loaded successfully.')); + $newRoleId = $newRoleFromDB->rid; + + // save new roles to user + $edit = array( + 'roles' => array(), + ); + $edit['roles'][$newRoleId] = 'Test Role'; + $myTestUser = user_save($myTestUser, $edit); + + $this->assertTrue(user_access('access content', $myTestUser), t('User has permission from authenticated role.')); + $this->assertFalse(user_access('administer nodes', $myTestUser), t('User does not have permission to administer content.')); + + // grant an additional permission to test role. + user_role_grant_permissions($newRoleId, array('administer nodes')); + + // Manual check of permission in database, sanity check + $perm = db_query('SELECT rid FROM {role_permission} WHERE rid = :rid AND permission = :permission', + array( + ':rid' => $newRoleId, + ':permission' => 'administer nodes', + ))->fetchField(); + + $this->assertTrue($perm == $newRoleId, t('User permission sanity check.')); + + $this->assertTrue(user_access('administer nodes', $myTestUser), t('User has permission to access administer content')); + } +}