Security update for Core, with self-updated composer
[yaffs-website] / web / core / modules / user / src / Tests / Views / BulkFormTest.php
1 <?php
2
3 namespace Drupal\user\Tests\Views;
4
5 use Drupal\user\Entity\User;
6 use Drupal\user\RoleInterface;
7 use Drupal\views\Views;
8
9 /**
10  * Tests a user bulk form.
11  *
12  * @group user
13  * @see \Drupal\user\Plugin\views\field\UserBulkForm
14  */
15 class BulkFormTest extends UserTestBase {
16
17   /**
18    * Modules to enable.
19    *
20    * @var array
21    */
22   public static $modules = ['views_ui'];
23
24   /**
25    * Views used by this test.
26    *
27    * @var array
28    */
29   public static $testViews = ['test_user_bulk_form', 'test_user_bulk_form_combine_filter'];
30
31   /**
32    * Tests the user bulk form.
33    */
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');
38
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);
44
45     // Test submitting the page with no selection.
46     $edit = [
47       'action' => 'user_block_user_action',
48     ];
49     $this->drupalPostForm('test-user-bulk-form', $edit, t('Apply to selected items'));
50     $this->assertText(t('No users selected.'));
51
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]);
56     $role = key($roles);
57
58     $this->assertFalse($account->hasRole($role), 'The user currently does not have a custom role.');
59     $edit = [
60       'user_bulk_form[1]' => TRUE,
61       'action' => 'user_add_role_action.' . $role,
62     ];
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.');
68
69     $edit = [
70       'user_bulk_form[1]' => TRUE,
71       'action' => 'user_remove_role_action.' . $role,
72     ];
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.');
78
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.');
82     $edit = [
83       'user_bulk_form[1]' => TRUE,
84       'action' => 'user_block_user_action',
85     ];
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.');
92
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();
97
98     // Ensure the anonymous user is found.
99     $this->drupalGet('test-user-bulk-form');
100     $this->assertText($this->config('user.settings')->get('anonymous'));
101
102     // Attempt to block the anonymous user.
103     $edit = [
104       'user_bulk_form[0]' => TRUE,
105       'action' => 'user_block_user_action',
106     ];
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.');
110
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;
114     $edit = [
115       'options[include_exclude]' => 'exclude',
116       "options[selected_actions][$action_id]" => $action_id,
117     ];
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);
127   }
128
129   /**
130    * Tests the user bulk form with a combined field filter on the bulk column.
131    */
132   public function testBulkFormCombineFilter() {
133     // Add a user.
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']));
138   }
139
140 }