3 namespace Drupal\user\Tests\Views;
4 use Drupal\Component\Utility\SafeMarkup;
5 use Drupal\user\Entity\User;
8 * Tests if entity access is respected on a user bulk form.
11 * @see \Drupal\user\Plugin\views\field\UserBulkForm
12 * @see \Drupal\user\Tests\Views\BulkFormTest
14 class BulkFormAccessTest extends UserTestBase {
21 public static $modules = ['user_access_test'];
24 * Views used by this test.
28 public static $testViews = ['test_user_bulk_form'];
31 * Tests if users that may not be edited, can not be edited in bulk.
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.');
39 // Log in as user admin.
40 $admin_user = $this->drupalCreateUser(['administer users']);
41 $this->drupalLogin($admin_user);
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.');
48 // Test blocking the account "no_edit".
50 'user_bulk_form[' . ($no_edit_user->id() - 1) . ']' => TRUE,
51 'action' => 'user_block_user_action',
53 $this->drupalPostForm('test-user-bulk-form', $edit, t('Apply to selected items'));
54 $this->assertResponse(200);
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(),
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.');
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));
71 'user_bulk_form[' . ($normal_user->id() - 1) . ']' => TRUE,
72 'action' => 'user_block_user_action',
74 $this->drupalPostForm('test-user-bulk-form', $edit, t('Apply to selected items'));
76 $normal_user = User::load($normal_user->id());
77 $this->assertTrue($normal_user->isBlocked(), 'The user is blocked.');
79 // Log in as user without the 'administer users' permission.
80 $this->drupalLogin($this->drupalCreateUser());
83 'user_bulk_form[' . ($normal_user->id() - 1) . ']' => TRUE,
84 'action' => 'user_unblock_user_action',
86 $this->drupalPostForm('test-user-bulk-form', $edit, t('Apply to selected items'));
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.');
94 * Tests if users that may not be deleted, can not be deleted in bulk.
96 public function testUserDeleteAccess() {
97 // Create two authenticated users.
98 $account = $this->drupalCreateUser([], 'no_delete');
99 $account2 = $this->drupalCreateUser([], 'may_delete');
101 // Log in as user admin.
102 $this->drupalLogin($this->drupalCreateUser(['administer users']));
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.');
111 // Test deleting the accounts "no_delete" and "may_delete".
113 'user_bulk_form[' . ($account->id() - 1) . ']' => TRUE,
114 'user_bulk_form[' . ($account2->id() - 1) . ']' => TRUE,
115 'action' => 'user_cancel_user_action',
117 $this->drupalPostForm('test-user-bulk-form', $edit, t('Apply to selected items'));
119 'user_cancel_method' => 'user_cancel_delete',
121 $this->drupalPostForm(NULL, $edit, t('Cancel accounts'));
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.');