3 namespace Drupal\user\Tests\Views;
5 use Drupal\user\Entity\User;
6 use Drupal\user\RoleInterface;
7 use Drupal\views\Views;
10 * Tests a user bulk form.
13 * @see \Drupal\user\Plugin\views\field\UserBulkForm
15 class BulkFormTest extends UserTestBase {
22 public static $modules = ['views_ui'];
25 * Views used by this test.
29 public static $testViews = ['test_user_bulk_form', 'test_user_bulk_form_combine_filter'];
32 * Tests the user bulk form.
34 public function testBulkForm() {
35 // Log in as a user without 'administer users'.
36 $this->drupalLogin($this->drupalCreateUser(['administer permissions']));
37 $user_storage = $this->container->get('entity.manager')->getStorage('user');
39 // Create an user which actually can change users.
40 $this->drupalLogin($this->drupalCreateUser(['administer users']));
41 $this->drupalGet('test-user-bulk-form');
42 $result = $this->cssSelect('#edit-action option');
43 $this->assertTrue(count($result) > 0);
45 // Test submitting the page with no selection.
47 'action' => 'user_block_user_action',
49 $this->drupalPostForm('test-user-bulk-form', $edit, t('Apply to selected items'));
50 $this->assertText(t('No users selected.'));
52 // Assign a role to a user.
53 $account = $user_storage->load($this->users[0]->id());
54 $roles = user_role_names(TRUE);
55 unset($roles[RoleInterface::AUTHENTICATED_ID]);
58 $this->assertFalse($account->hasRole($role), 'The user currently does not have a custom role.');
60 'user_bulk_form[1]' => TRUE,
61 'action' => 'user_add_role_action.' . $role,
63 $this->drupalPostForm(NULL, $edit, t('Apply to selected items'));
64 // Re-load the user and check their roles.
65 $user_storage->resetCache([$account->id()]);
66 $account = $user_storage->load($account->id());
67 $this->assertTrue($account->hasRole($role), 'The user now has the custom role.');
70 'user_bulk_form[1]' => TRUE,
71 'action' => 'user_remove_role_action.' . $role,
73 $this->drupalPostForm(NULL, $edit, t('Apply to selected items'));
74 // Re-load the user and check their roles.
75 $user_storage->resetCache([$account->id()]);
76 $account = $user_storage->load($account->id());
77 $this->assertFalse($account->hasRole($role), 'The user no longer has the custom role.');
79 // Block a user using the bulk form.
80 $this->assertTrue($account->isActive(), 'The user is not blocked.');
81 $this->assertRaw($account->label(), 'The user is found in the table.');
83 'user_bulk_form[1]' => TRUE,
84 'action' => 'user_block_user_action',
86 $this->drupalPostForm(NULL, $edit, t('Apply to selected items'));
87 // Re-load the user and check their status.
88 $user_storage->resetCache([$account->id()]);
89 $account = $user_storage->load($account->id());
90 $this->assertTrue($account->isBlocked(), 'The user is blocked.');
91 $this->assertNoRaw($account->label(), 'The user is not found in the table.');
93 // Remove the user status filter from the view.
94 $view = Views::getView('test_user_bulk_form');
95 $view->removeHandler('default', 'filter', 'status');
96 $view->storage->save();
98 // Ensure the anonymous user is found.
99 $this->drupalGet('test-user-bulk-form');
100 $this->assertText($this->config('user.settings')->get('anonymous'));
102 // Attempt to block the anonymous user.
104 'user_bulk_form[0]' => TRUE,
105 'action' => 'user_block_user_action',
107 $this->drupalPostForm(NULL, $edit, t('Apply to selected items'));
108 $anonymous_account = $user_storage->load(0);
109 $this->assertTrue($anonymous_account->isBlocked(), 'Ensure the anonymous user got blocked.');
111 // Test the list of available actions with a value that contains a dot.
112 $this->drupalLogin($this->drupalCreateUser(['administer permissions', 'administer views', 'administer users']));
113 $action_id = 'user_add_role_action.' . $role;
115 'options[include_exclude]' => 'exclude',
116 "options[selected_actions][$action_id]" => $action_id,
118 $this->drupalPostForm('admin/structure/views/nojs/handler/test_user_bulk_form/default/field/user_bulk_form', $edit, t('Apply'));
119 $this->drupalPostForm(NULL, [], t('Save'));
120 $this->drupalGet('test-user-bulk-form');
121 $this->assertNoOption('edit-action', $action_id);
122 $edit['options[include_exclude]'] = 'include';
123 $this->drupalPostForm('admin/structure/views/nojs/handler/test_user_bulk_form/default/field/user_bulk_form', $edit, t('Apply'));
124 $this->drupalPostForm(NULL, [], t('Save'));
125 $this->drupalGet('test-user-bulk-form');
126 $this->assertOption('edit-action', $action_id);
130 * Tests the user bulk form with a combined field filter on the bulk column.
132 public function testBulkFormCombineFilter() {
134 User::load($this->users[0]->id());
135 $view = Views::getView('test_user_bulk_form_combine_filter');
136 $errors = $view->validate();
137 $this->assertEqual(reset($errors['default']), t('Field %field set in %filter is not usable for this filter type. Combined field filter only works for simple fields.', ['%field' => 'User: Bulk update', '%filter' => 'Global: Combine fields filter']));