Security update for Core, with self-updated composer
[yaffs-website] / web / core / modules / user / src / Tests / Views / BulkFormAccessTest.php
1 <?php
2
3 namespace Drupal\user\Tests\Views;
4
5 use Drupal\Component\Utility\SafeMarkup;
6 use Drupal\user\Entity\User;
7
8 /**
9  * Tests if entity access is respected on a user bulk form.
10  *
11  * @group user
12  * @see \Drupal\user\Plugin\views\field\UserBulkForm
13  * @see \Drupal\user\Tests\Views\BulkFormTest
14  */
15 class BulkFormAccessTest extends UserTestBase {
16
17   /**
18    * Modules to enable.
19    *
20    * @var array
21    */
22   public static $modules = ['user_access_test'];
23
24   /**
25    * Views used by this test.
26    *
27    * @var array
28    */
29   public static $testViews = ['test_user_bulk_form'];
30
31   /**
32    * Tests if users that may not be edited, can not be edited in bulk.
33    */
34   public function testUserEditAccess() {
35     // Create an authenticated user.
36     $no_edit_user = $this->drupalCreateUser([], 'no_edit');
37     // Ensure this account is not blocked.
38     $this->assertFalse($no_edit_user->isBlocked(), 'The user is not blocked.');
39
40     // Log in as user admin.
41     $admin_user = $this->drupalCreateUser(['administer users']);
42     $this->drupalLogin($admin_user);
43
44     // Ensure that the account "no_edit" can not be edited.
45     $this->drupalGet('user/' . $no_edit_user->id() . '/edit');
46     $this->assertFalse($no_edit_user->access('update', $admin_user));
47     $this->assertResponse(403, 'The user may not be edited.');
48
49     // Test blocking the account "no_edit".
50     $edit = [
51       'user_bulk_form[' . ($no_edit_user->id() - 1) . ']' => TRUE,
52       'action' => 'user_block_user_action',
53     ];
54     $this->drupalPostForm('test-user-bulk-form', $edit, t('Apply to selected items'));
55     $this->assertResponse(200);
56
57     $this->assertRaw(SafeMarkup::format('No access to execute %action on the @entity_type_label %entity_label.', [
58       '%action' => 'Block the selected user(s)',
59       '@entity_type_label' => 'User',
60       '%entity_label' => $no_edit_user->label(),
61     ]));
62
63     // Re-load the account "no_edit" and ensure it is not blocked.
64     $no_edit_user = User::load($no_edit_user->id());
65     $this->assertFalse($no_edit_user->isBlocked(), 'The user is not blocked.');
66
67     // Create a normal user which can be edited by the admin user
68     $normal_user = $this->drupalCreateUser();
69     $this->assertTrue($normal_user->access('update', $admin_user));
70
71     $edit = [
72       'user_bulk_form[' . ($normal_user->id() - 1) . ']' => TRUE,
73       'action' => 'user_block_user_action',
74     ];
75     $this->drupalPostForm('test-user-bulk-form', $edit, t('Apply to selected items'));
76
77     $normal_user = User::load($normal_user->id());
78     $this->assertTrue($normal_user->isBlocked(), 'The user is blocked.');
79
80     // Log in as user without the 'administer users' permission.
81     $this->drupalLogin($this->drupalCreateUser());
82
83     $edit = [
84       'user_bulk_form[' . ($normal_user->id() - 1) . ']' => TRUE,
85       'action' => 'user_unblock_user_action',
86     ];
87     $this->drupalPostForm('test-user-bulk-form', $edit, t('Apply to selected items'));
88
89     // Re-load the normal user and ensure it is still blocked.
90     $normal_user = User::load($normal_user->id());
91     $this->assertTrue($normal_user->isBlocked(), 'The user is still blocked.');
92   }
93
94   /**
95    * Tests if users that may not be deleted, can not be deleted in bulk.
96    */
97   public function testUserDeleteAccess() {
98     // Create two authenticated users.
99     $account = $this->drupalCreateUser([], 'no_delete');
100     $account2 = $this->drupalCreateUser([], 'may_delete');
101
102     // Log in as user admin.
103     $this->drupalLogin($this->drupalCreateUser(['administer users']));
104
105     // Ensure that the account "no_delete" can not be deleted.
106     $this->drupalGet('user/' . $account->id() . '/cancel');
107     $this->assertResponse(403, 'The user "no_delete" may not be deleted.');
108     // Ensure that the account "may_delete" *can* be deleted.
109     $this->drupalGet('user/' . $account2->id() . '/cancel');
110     $this->assertResponse(200, 'The user "may_delete" may be deleted.');
111
112     // Test deleting the accounts "no_delete" and "may_delete".
113     $edit = [
114       'user_bulk_form[' . ($account->id() - 1) . ']' => TRUE,
115       'user_bulk_form[' . ($account2->id() - 1) . ']' => TRUE,
116       'action' => 'user_cancel_user_action',
117     ];
118     $this->drupalPostForm('test-user-bulk-form', $edit, t('Apply to selected items'));
119     $edit = [
120       'user_cancel_method' => 'user_cancel_delete',
121     ];
122     $this->drupalPostForm(NULL, $edit, t('Cancel accounts'));
123
124     // Ensure the account "no_delete" still exists.
125     $account = User::load($account->id());
126     $this->assertNotNull($account, 'The user "no_delete" is not deleted.');
127     // Ensure the account "may_delete" no longer exists.
128     $account = User::load($account2->id());
129     $this->assertNull($account, 'The user "may_delete" is deleted.');
130   }
131
132 }