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