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