3 namespace Drupal\Tests\user\Functional;
5 use Drupal\Tests\BrowserTestBase;
8 * Tests that users can be assigned and unassigned roles.
12 class UserRolesAssignmentTest extends BrowserTestBase {
14 protected function setUp() {
16 $admin_user = $this->drupalCreateUser(['administer permissions', 'administer users']);
17 $this->drupalLogin($admin_user);
21 * Tests that a user can be assigned a role and that the role can be removed
24 public function testAssignAndRemoveRole() {
25 $rid = $this->drupalCreateRole(['administer users']);
26 $account = $this->drupalCreateUser();
28 // Assign the role to the user.
29 $this->drupalPostForm('user/' . $account->id() . '/edit', ["roles[$rid]" => $rid], t('Save'));
30 $this->assertText(t('The changes have been saved.'));
31 $this->assertFieldChecked('edit-roles-' . $rid, 'Role is assigned.');
32 $this->userLoadAndCheckRoleAssigned($account, $rid);
34 // Remove the role from the user.
35 $this->drupalPostForm('user/' . $account->id() . '/edit', ["roles[$rid]" => FALSE], t('Save'));
36 $this->assertText(t('The changes have been saved.'));
37 $this->assertNoFieldChecked('edit-roles-' . $rid, 'Role is removed from user.');
38 $this->userLoadAndCheckRoleAssigned($account, $rid, FALSE);
42 * Tests that when creating a user the role can be assigned. And that it can
45 public function testCreateUserWithRole() {
46 $rid = $this->drupalCreateRole(['administer users']);
47 // Create a new user and add the role at the same time.
49 'name' => $this->randomMachineName(),
50 'mail' => $this->randomMachineName() . '@example.com',
51 'pass[pass1]' => $pass = $this->randomString(),
52 'pass[pass2]' => $pass,
53 "roles[$rid]" => $rid,
55 $this->drupalPostForm('admin/people/create', $edit, t('Create new account'));
56 $this->assertText(t('Created a new user account for @name.', ['@name' => $edit['name']]));
57 // Get the newly added user.
58 $account = user_load_by_name($edit['name']);
60 $this->drupalGet('user/' . $account->id() . '/edit');
61 $this->assertFieldChecked('edit-roles-' . $rid, 'Role is assigned.');
62 $this->userLoadAndCheckRoleAssigned($account, $rid);
64 // Remove the role again.
65 $this->drupalPostForm('user/' . $account->id() . '/edit', ["roles[$rid]" => FALSE], t('Save'));
66 $this->assertText(t('The changes have been saved.'));
67 $this->assertNoFieldChecked('edit-roles-' . $rid, 'Role is removed from user.');
68 $this->userLoadAndCheckRoleAssigned($account, $rid, FALSE);
72 * Check role on user object.
74 * @param object $account
75 * The user account to check.
77 * The role ID to search for.
78 * @param bool $is_assigned
79 * (optional) Whether to assert that $rid exists (TRUE) or not (FALSE).
82 private function userLoadAndCheckRoleAssigned($account, $rid, $is_assigned = TRUE) {
83 $user_storage = $this->container->get('entity.manager')->getStorage('user');
84 $user_storage->resetCache([$account->id()]);
85 $account = $user_storage->load($account->id());
87 $this->assertFalse(array_search($rid, $account->getRoles()) === FALSE, 'The role is present in the user object.');
90 $this->assertTrue(array_search($rid, $account->getRoles()) === FALSE, 'The role is not present in the user object.');