Chris@0: drupalCreateUser(['administer permissions', 'administer users']); Chris@0: $this->drupalLogin($admin_user); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Tests that a user can be assigned a role and that the role can be removed Chris@0: * again. Chris@0: */ Chris@12: public function testAssignAndRemoveRole() { Chris@0: $rid = $this->drupalCreateRole(['administer users']); Chris@0: $account = $this->drupalCreateUser(); Chris@0: Chris@0: // Assign the role to the user. Chris@0: $this->drupalPostForm('user/' . $account->id() . '/edit', ["roles[$rid]" => $rid], t('Save')); Chris@0: $this->assertText(t('The changes have been saved.')); Chris@0: $this->assertFieldChecked('edit-roles-' . $rid, 'Role is assigned.'); Chris@0: $this->userLoadAndCheckRoleAssigned($account, $rid); Chris@0: Chris@0: // Remove the role from the user. Chris@0: $this->drupalPostForm('user/' . $account->id() . '/edit', ["roles[$rid]" => FALSE], t('Save')); Chris@0: $this->assertText(t('The changes have been saved.')); Chris@0: $this->assertNoFieldChecked('edit-roles-' . $rid, 'Role is removed from user.'); Chris@0: $this->userLoadAndCheckRoleAssigned($account, $rid, FALSE); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Tests that when creating a user the role can be assigned. And that it can Chris@0: * be removed again. Chris@0: */ Chris@0: public function testCreateUserWithRole() { Chris@0: $rid = $this->drupalCreateRole(['administer users']); Chris@0: // Create a new user and add the role at the same time. Chris@0: $edit = [ Chris@0: 'name' => $this->randomMachineName(), Chris@0: 'mail' => $this->randomMachineName() . '@example.com', Chris@0: 'pass[pass1]' => $pass = $this->randomString(), Chris@0: 'pass[pass2]' => $pass, Chris@0: "roles[$rid]" => $rid, Chris@0: ]; Chris@0: $this->drupalPostForm('admin/people/create', $edit, t('Create new account')); Chris@0: $this->assertText(t('Created a new user account for @name.', ['@name' => $edit['name']])); Chris@0: // Get the newly added user. Chris@0: $account = user_load_by_name($edit['name']); Chris@0: Chris@0: $this->drupalGet('user/' . $account->id() . '/edit'); Chris@0: $this->assertFieldChecked('edit-roles-' . $rid, 'Role is assigned.'); Chris@0: $this->userLoadAndCheckRoleAssigned($account, $rid); Chris@0: Chris@0: // Remove the role again. Chris@0: $this->drupalPostForm('user/' . $account->id() . '/edit', ["roles[$rid]" => FALSE], t('Save')); Chris@0: $this->assertText(t('The changes have been saved.')); Chris@0: $this->assertNoFieldChecked('edit-roles-' . $rid, 'Role is removed from user.'); Chris@0: $this->userLoadAndCheckRoleAssigned($account, $rid, FALSE); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Check role on user object. Chris@0: * Chris@0: * @param object $account Chris@0: * The user account to check. Chris@0: * @param string $rid Chris@0: * The role ID to search for. Chris@0: * @param bool $is_assigned Chris@0: * (optional) Whether to assert that $rid exists (TRUE) or not (FALSE). Chris@0: * Defaults to TRUE. Chris@0: */ Chris@0: private function userLoadAndCheckRoleAssigned($account, $rid, $is_assigned = TRUE) { Chris@0: $user_storage = $this->container->get('entity.manager')->getStorage('user'); Chris@0: $user_storage->resetCache([$account->id()]); Chris@0: $account = $user_storage->load($account->id()); Chris@0: if ($is_assigned) { Chris@0: $this->assertFalse(array_search($rid, $account->getRoles()) === FALSE, 'The role is present in the user object.'); Chris@0: } Chris@0: else { Chris@0: $this->assertTrue(array_search($rid, $account->getRoles()) === FALSE, 'The role is not present in the user object.'); Chris@0: } Chris@0: } Chris@0: Chris@0: }