comparison core/modules/user/tests/src/Functional/UserRolesAssignmentTest.php @ 0:4c8ae668cc8c

Initial import (non-working)
author Chris Cannam
date Wed, 29 Nov 2017 16:09:58 +0000
parents
children 7a779792577d
comparison
equal deleted inserted replaced
-1:000000000000 0:4c8ae668cc8c
1 <?php
2
3 namespace Drupal\Tests\user\Functional;
4
5 use Drupal\Tests\BrowserTestBase;
6
7 /**
8 * Tests that users can be assigned and unassigned roles.
9 *
10 * @group user
11 */
12 class UserRolesAssignmentTest extends BrowserTestBase {
13
14 protected function setUp() {
15 parent::setUp();
16 $admin_user = $this->drupalCreateUser(['administer permissions', 'administer users']);
17 $this->drupalLogin($admin_user);
18 }
19
20 /**
21 * Tests that a user can be assigned a role and that the role can be removed
22 * again.
23 */
24 public function testAssignAndRemoveRole() {
25 $rid = $this->drupalCreateRole(['administer users']);
26 $account = $this->drupalCreateUser();
27
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);
33
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);
39 }
40
41 /**
42 * Tests that when creating a user the role can be assigned. And that it can
43 * be removed again.
44 */
45 public function testCreateUserWithRole() {
46 $rid = $this->drupalCreateRole(['administer users']);
47 // Create a new user and add the role at the same time.
48 $edit = [
49 'name' => $this->randomMachineName(),
50 'mail' => $this->randomMachineName() . '@example.com',
51 'pass[pass1]' => $pass = $this->randomString(),
52 'pass[pass2]' => $pass,
53 "roles[$rid]" => $rid,
54 ];
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']);
59
60 $this->drupalGet('user/' . $account->id() . '/edit');
61 $this->assertFieldChecked('edit-roles-' . $rid, 'Role is assigned.');
62 $this->userLoadAndCheckRoleAssigned($account, $rid);
63
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);
69 }
70
71 /**
72 * Check role on user object.
73 *
74 * @param object $account
75 * The user account to check.
76 * @param string $rid
77 * The role ID to search for.
78 * @param bool $is_assigned
79 * (optional) Whether to assert that $rid exists (TRUE) or not (FALSE).
80 * Defaults to TRUE.
81 */
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());
86 if ($is_assigned) {
87 $this->assertFalse(array_search($rid, $account->getRoles()) === FALSE, 'The role is present in the user object.');
88 }
89 else {
90 $this->assertTrue(array_search($rid, $account->getRoles()) === FALSE, 'The role is not present in the user object.');
91 }
92 }
93
94 }