3 namespace Drupal\user\Tests\Views;
5 use Drupal\Component\Utility\SafeMarkup;
6 use Drupal\user\Entity\User;
9 * Tests if entity access is respected on a user bulk form.
12 * @see \Drupal\user\Plugin\views\field\UserBulkForm
13 * @see \Drupal\user\Tests\Views\BulkFormTest
15 class BulkFormAccessTest extends UserTestBase {
22 public static $modules = ['user_access_test'];
25 * Views used by this test.
29 public static $testViews = ['test_user_bulk_form'];
32 * Tests if users that may not be edited, can not be edited in bulk.
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.');
40 // Log in as user admin.
41 $admin_user = $this->drupalCreateUser(['administer users']);
42 $this->drupalLogin($admin_user);
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.');
49 // Test blocking the account "no_edit".
51 'user_bulk_form[' . ($no_edit_user->id() - 1) . ']' => TRUE,
52 'action' => 'user_block_user_action',
54 $this->drupalPostForm('test-user-bulk-form', $edit, t('Apply to selected items'));
55 $this->assertResponse(200);
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(),
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.');
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));
72 'user_bulk_form[' . ($normal_user->id() - 1) . ']' => TRUE,
73 'action' => 'user_block_user_action',
75 $this->drupalPostForm('test-user-bulk-form', $edit, t('Apply to selected items'));
77 $normal_user = User::load($normal_user->id());
78 $this->assertTrue($normal_user->isBlocked(), 'The user is blocked.');
80 // Log in as user without the 'administer users' permission.
81 $this->drupalLogin($this->drupalCreateUser());
84 'user_bulk_form[' . ($normal_user->id() - 1) . ']' => TRUE,
85 'action' => 'user_unblock_user_action',
87 $this->drupalPostForm('test-user-bulk-form', $edit, t('Apply to selected items'));
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.');
95 * Tests if users that may not be deleted, can not be deleted in bulk.
97 public function testUserDeleteAccess() {
98 // Create two authenticated users.
99 $account = $this->drupalCreateUser([], 'no_delete');
100 $account2 = $this->drupalCreateUser([], 'may_delete');
102 // Log in as user admin.
103 $this->drupalLogin($this->drupalCreateUser(['administer users']));
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.');
112 // Test deleting the accounts "no_delete" and "may_delete".
114 'user_bulk_form[' . ($account->id() - 1) . ']' => TRUE,
115 'user_bulk_form[' . ($account2->id() - 1) . ']' => TRUE,
116 'action' => 'user_cancel_user_action',
118 $this->drupalPostForm('test-user-bulk-form', $edit, t('Apply to selected items'));
120 'user_cancel_method' => 'user_cancel_delete',
122 $this->drupalPostForm(NULL, $edit, t('Cancel accounts'));
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.');