Chris@0
|
1 <?php
|
Chris@0
|
2
|
Chris@0
|
3 namespace Drupal\Tests\user\Functional;
|
Chris@0
|
4
|
Chris@0
|
5 use Drupal\Tests\BrowserTestBase;
|
Chris@0
|
6
|
Chris@0
|
7 /**
|
Chris@0
|
8 * Tests that users can be assigned and unassigned roles.
|
Chris@0
|
9 *
|
Chris@0
|
10 * @group user
|
Chris@0
|
11 */
|
Chris@0
|
12 class UserRolesAssignmentTest extends BrowserTestBase {
|
Chris@0
|
13
|
Chris@0
|
14 protected function setUp() {
|
Chris@0
|
15 parent::setUp();
|
Chris@0
|
16 $admin_user = $this->drupalCreateUser(['administer permissions', 'administer users']);
|
Chris@0
|
17 $this->drupalLogin($admin_user);
|
Chris@0
|
18 }
|
Chris@0
|
19
|
Chris@0
|
20 /**
|
Chris@0
|
21 * Tests that a user can be assigned a role and that the role can be removed
|
Chris@0
|
22 * again.
|
Chris@0
|
23 */
|
Chris@12
|
24 public function testAssignAndRemoveRole() {
|
Chris@0
|
25 $rid = $this->drupalCreateRole(['administer users']);
|
Chris@0
|
26 $account = $this->drupalCreateUser();
|
Chris@0
|
27
|
Chris@0
|
28 // Assign the role to the user.
|
Chris@0
|
29 $this->drupalPostForm('user/' . $account->id() . '/edit', ["roles[$rid]" => $rid], t('Save'));
|
Chris@0
|
30 $this->assertText(t('The changes have been saved.'));
|
Chris@0
|
31 $this->assertFieldChecked('edit-roles-' . $rid, 'Role is assigned.');
|
Chris@0
|
32 $this->userLoadAndCheckRoleAssigned($account, $rid);
|
Chris@0
|
33
|
Chris@0
|
34 // Remove the role from the user.
|
Chris@0
|
35 $this->drupalPostForm('user/' . $account->id() . '/edit', ["roles[$rid]" => FALSE], t('Save'));
|
Chris@0
|
36 $this->assertText(t('The changes have been saved.'));
|
Chris@0
|
37 $this->assertNoFieldChecked('edit-roles-' . $rid, 'Role is removed from user.');
|
Chris@0
|
38 $this->userLoadAndCheckRoleAssigned($account, $rid, FALSE);
|
Chris@0
|
39 }
|
Chris@0
|
40
|
Chris@0
|
41 /**
|
Chris@0
|
42 * Tests that when creating a user the role can be assigned. And that it can
|
Chris@0
|
43 * be removed again.
|
Chris@0
|
44 */
|
Chris@0
|
45 public function testCreateUserWithRole() {
|
Chris@0
|
46 $rid = $this->drupalCreateRole(['administer users']);
|
Chris@0
|
47 // Create a new user and add the role at the same time.
|
Chris@0
|
48 $edit = [
|
Chris@0
|
49 'name' => $this->randomMachineName(),
|
Chris@0
|
50 'mail' => $this->randomMachineName() . '@example.com',
|
Chris@0
|
51 'pass[pass1]' => $pass = $this->randomString(),
|
Chris@0
|
52 'pass[pass2]' => $pass,
|
Chris@0
|
53 "roles[$rid]" => $rid,
|
Chris@0
|
54 ];
|
Chris@0
|
55 $this->drupalPostForm('admin/people/create', $edit, t('Create new account'));
|
Chris@0
|
56 $this->assertText(t('Created a new user account for @name.', ['@name' => $edit['name']]));
|
Chris@0
|
57 // Get the newly added user.
|
Chris@0
|
58 $account = user_load_by_name($edit['name']);
|
Chris@0
|
59
|
Chris@0
|
60 $this->drupalGet('user/' . $account->id() . '/edit');
|
Chris@0
|
61 $this->assertFieldChecked('edit-roles-' . $rid, 'Role is assigned.');
|
Chris@0
|
62 $this->userLoadAndCheckRoleAssigned($account, $rid);
|
Chris@0
|
63
|
Chris@0
|
64 // Remove the role again.
|
Chris@0
|
65 $this->drupalPostForm('user/' . $account->id() . '/edit', ["roles[$rid]" => FALSE], t('Save'));
|
Chris@0
|
66 $this->assertText(t('The changes have been saved.'));
|
Chris@0
|
67 $this->assertNoFieldChecked('edit-roles-' . $rid, 'Role is removed from user.');
|
Chris@0
|
68 $this->userLoadAndCheckRoleAssigned($account, $rid, FALSE);
|
Chris@0
|
69 }
|
Chris@0
|
70
|
Chris@0
|
71 /**
|
Chris@0
|
72 * Check role on user object.
|
Chris@0
|
73 *
|
Chris@0
|
74 * @param object $account
|
Chris@0
|
75 * The user account to check.
|
Chris@0
|
76 * @param string $rid
|
Chris@0
|
77 * The role ID to search for.
|
Chris@0
|
78 * @param bool $is_assigned
|
Chris@0
|
79 * (optional) Whether to assert that $rid exists (TRUE) or not (FALSE).
|
Chris@0
|
80 * Defaults to TRUE.
|
Chris@0
|
81 */
|
Chris@0
|
82 private function userLoadAndCheckRoleAssigned($account, $rid, $is_assigned = TRUE) {
|
Chris@0
|
83 $user_storage = $this->container->get('entity.manager')->getStorage('user');
|
Chris@0
|
84 $user_storage->resetCache([$account->id()]);
|
Chris@0
|
85 $account = $user_storage->load($account->id());
|
Chris@0
|
86 if ($is_assigned) {
|
Chris@0
|
87 $this->assertFalse(array_search($rid, $account->getRoles()) === FALSE, 'The role is present in the user object.');
|
Chris@0
|
88 }
|
Chris@0
|
89 else {
|
Chris@0
|
90 $this->assertTrue(array_search($rid, $account->getRoles()) === FALSE, 'The role is not present in the user object.');
|
Chris@0
|
91 }
|
Chris@0
|
92 }
|
Chris@0
|
93
|
Chris@0
|
94 }
|