68f6136db61f5f61f284636febac5d4b5feec5bd
[yaffs-website] / web / modules / contrib / devel / src / Tests / DevelSwitchUserTest.php
1 <?php
2
3 namespace Drupal\devel\Tests;
4
5 use Drupal\Component\Render\FormattableMarkup;
6 use Drupal\simpletest\WebTestBase;
7
8 /**
9  * Tests switch user.
10  *
11  * @group devel
12  */
13 class DevelSwitchUserTest extends WebTestBase {
14
15   /**
16    * Modules to enable.
17    *
18    * @var array
19    */
20   public static $modules = ['devel', 'block'];
21
22   /**
23    * The block used by this test.
24    *
25    * @var \Drupal\block\BlockInterface
26    */
27   protected $block;
28
29   /**
30    * The devel user.
31    *
32    * @var \Drupal\user\Entity\User
33    */
34   protected $develUser;
35
36   /**
37    * The switch user.
38    *
39    * @var \Drupal\user\Entity\User
40    */
41   protected $switchUser;
42
43   /**
44    * The web user.
45    *
46    * @var \Drupal\user\Entity\User
47    */
48   protected $webUser;
49
50   /**
51    * Set up test.
52    */
53   protected function setUp() {
54     parent::setUp();
55
56     $this->block = $this->drupalPlaceBlock('devel_switch_user', ['id' => 'switch-user']);
57
58     $this->develUser = $this->drupalCreateUser(['access devel information', 'switch users']);
59     $this->switchUser = $this->drupalCreateUser(['switch users']);
60     $this->webUser = $this->drupalCreateUser();
61   }
62
63   /**
64    * Tests switch user.
65    */
66   public function testSwitchUser() {
67     $this->drupalLogin($this->webUser);
68
69     $this->drupalGet('');
70     $this->assertNoText($this->block->label(), 'Block title was not found.');
71
72     // Ensure that a token is required to switch user.
73     $this->drupalGet('/devel/switch/' . $this->webUser->getUsername());
74     $this->assertResponse(403);
75
76     $this->drupalLogin($this->develUser);
77
78     $this->drupalGet('');
79     $this->assertText($this->block->label(), 'Block title was found.');
80
81     // Ensure that if name in not passed the controller returns access denied.
82     $this->drupalGet('/devel/switch');
83     $this->assertResponse(403);
84
85     // Ensure that a token is required to switch user.
86     $this->drupalGet('/devel/switch/' . $this->switchUser->getUsername());
87     $this->assertResponse(403);
88
89     // Switch to another user account.
90     $this->drupalGet('/user/' . $this->switchUser->id());
91     $this->clickLink($this->switchUser->getUsername());
92     $this->assertSessionByUid($this->switchUser->id());
93     $this->assertNoSessionByUid($this->develUser->id());
94
95     // Switch back to initial account.
96     $this->clickLink($this->develUser->getUsername());
97     $this->assertNoSessionByUid($this->switchUser->id());
98     $this->assertSessionByUid($this->develUser->id());
99
100     // Use the search form to switch to another account.
101     $edit = ['userid' => $this->switchUser->getUsername()];
102     $this->drupalPostForm(NULL, $edit, t('Switch'));
103     $this->assertSessionByUid($this->switchUser->id());
104     $this->assertNoSessionByUid($this->develUser->id());
105   }
106
107   /**
108    * Tests the switch user block configuration.
109    */
110   public function testSwitchUserBlockConfiguration() {
111     $anonymous = \Drupal::config('user.settings')->get('anonymous');
112
113     // Create some users for the test.
114     for ($i = 0; $i < 12; $i++) {
115       $this->drupalCreateUser();
116     }
117
118     $this->drupalLogin($this->develUser);
119
120     $this->drupalGet('');
121     $this->assertText($this->block->label(), 'Block title was found.');
122
123     // Ensure that block default configuration is effectively used. The block
124     // default configuration is the following:
125     // - list_size : 12
126     // - include_anon : FALSE
127     // - show_form : TRUE
128     $this->assertSwitchUserSearchForm();
129     $this->assertSwitchUserListCount(12);
130     $this->assertSwitchUserListNoContainsUser($anonymous);
131
132     // Ensure that changing the list_size configuration property the number of
133     // user displayed in the list change.
134     $this->setBlockConfiguration('list_size', 4);
135     $this->drupalGet('');
136     $this->assertSwitchUserListCount(4);
137
138     // Ensure that changing the include_anon configuration property the
139     // anonymous user is displayed in the list.
140     $this->setBlockConfiguration('include_anon', TRUE);
141     $this->drupalGet('');
142     $this->assertSwitchUserListContainsUser($anonymous);
143
144     // Ensure that changing the show_form configuration property the
145     // form is not displayed.
146     $this->setBlockConfiguration('show_form', FALSE);
147     $this->drupalGet('');
148     $this->assertSwitchUserNoSearchForm();
149   }
150
151   /**
152    * Test the user list items.
153    */
154   public function testSwitchUserListItems() {
155     $anonymous = \Drupal::config('user.settings')->get('anonymous');
156
157     $this->setBlockConfiguration('list_size', 2);
158
159     // Login as web user so we are sure that this account is prioritized
160     // in the list if not enougth user with 'switch users' permission are
161     // present.
162     $this->drupalLogin($this->webUser);
163
164     $this->drupalLogin($this->develUser);
165     $this->drupalGet('');
166
167     // Ensure that user with 'switch users' permission are prioritized.
168     $this->assertSwitchUserListCount(2);
169     $this->assertSwitchUserListContainsUser($this->develUser->getUsername());
170     $this->assertSwitchUserListContainsUser($this->switchUser->getUsername());
171
172     // Ensure that blocked users are not shown in the list.
173     $this->switchUser->set('status', 0)->save();
174     $this->drupalGet('');
175     $this->assertSwitchUserListCount(2);
176     $this->assertSwitchUserListContainsUser($this->develUser->getUsername());
177     $this->assertSwitchUserListContainsUser($this->webUser->getUsername());
178     $this->assertSwitchUserListNoContainsUser($this->switchUser->getUsername());
179
180     // Ensure that anonymous user are prioritized if include_anon is set to true.
181     $this->setBlockConfiguration('include_anon', TRUE);
182     $this->drupalGet('');
183     $this->assertSwitchUserListCount(2);
184     $this->assertSwitchUserListContainsUser($this->develUser->getUsername());
185     $this->assertSwitchUserListContainsUser($anonymous);
186
187     // Ensure that the switch user block works properly even if no prioritized
188     // users are found (special handling for user 1).
189     $this->drupalLogout();
190     $this->develUser->delete();
191
192     $this->drupalLogin($this->rootUser);
193     $this->drupalGet('');
194     $this->assertSwitchUserListCount(2);
195     $this->assertSwitchUserListContainsUser($this->rootUser->getUsername());
196     $this->assertSwitchUserListContainsUser($anonymous);
197
198     // Ensure that the switch user block works properly even if no roles have
199     // the 'switch users' permission associated (special handling for user 1).
200     $roles = user_roles(TRUE, 'switch users');
201     \Drupal::entityTypeManager()->getStorage('user_role')->delete($roles);
202
203     $this->drupalGet('');
204     $this->assertSwitchUserListCount(2);
205     $this->assertSwitchUserListContainsUser($this->rootUser->getUsername());
206     $this->assertSwitchUserListContainsUser($anonymous);
207   }
208
209   /**
210    * Helper function for verify the number of items shown in the user list.
211    *
212    * @param int $number
213    *   The expected numer of items.
214    */
215   public function assertSwitchUserListCount($number) {
216     $result = $this->xpath('//div[@id=:block]//ul/li/a', [':block' => 'block-switch-user']);
217     $this->assert(count($result) == $number, 'The number of users shown in switch user is correct.');
218   }
219
220   /**
221    * Helper function for verify if the user list contains a username.
222    *
223    * @param string $username
224    *   The username to check.
225    */
226   public function assertSwitchUserListContainsUser($username) {
227     $result = $this->xpath('//div[@id=:block]//ul/li/a[normalize-space()=:user]', [':block' => 'block-switch-user', ':user' => $username]);
228     $this->assert(count($result) > 0, new FormattableMarkup('User "%user" is included in the switch user list.', ['%user' => $username]));
229   }
230
231   /**
232    * Helper function for verify if the user list not contains a username.
233    *
234    * @param string $username
235    *   The username to check.
236    */
237   public function assertSwitchUserListNoContainsUser($username) {
238     $result = $this->xpath('//div[@id=:block]//ul/li/a[normalize-space()=:user]', [':block' => 'block-switch-user', ':user' => $username]);
239     $this->assert(count($result) == 0, new FormattableMarkup('User "%user" is not included in the switch user list.', ['%user' => $username]));
240   }
241
242   /**
243    * Helper function for verify if the search form is shown.
244    */
245   public function assertSwitchUserSearchForm() {
246     $result = $this->xpath('//div[@id=:block]//form[contains(@class, :form)]', [':block' => 'block-switch-user', ':form' => 'devel-switchuser-form']);
247     $this->assert(count($result) > 0, 'The search form is shown.');
248   }
249
250   /**
251    * Helper function for verify if the search form is not shown.
252    */
253   public function assertSwitchUserNoSearchForm() {
254     $result = $this->xpath('//div[@id=:block]//form[contains(@class, :form)]', [':block' => 'block-switch-user', ':form' => 'devel-switchuser-form']);
255     $this->assert(count($result) == 0, 'The search form is not shown.');
256   }
257
258   /**
259    * Protected helper method to set the test block's configuration.
260    */
261   protected function setBlockConfiguration($key, $value) {
262     $block = $this->block->getPlugin();
263     $block->setConfigurationValue($key, $value);
264     $this->block->save();
265   }
266
267   /**
268    * Asserts that there is a session for a given user ID.
269    *
270    * Based off masquarade module.
271    *
272    * @param int $uid
273    *   The user ID for which to find a session record.
274    *
275    * TODO find a cleaner way to do this check.
276    */
277   protected function assertSessionByUid($uid) {
278     $query = \Drupal::database()->select('sessions');
279     $query->fields('sessions', array('uid'));
280     $query->condition('uid', $uid);
281     $result = $query->execute()->fetchAll();
282
283     if (empty($result)) {
284       $this->fail(new FormattableMarkup('No session found for uid @uid', array('@uid' => $uid)));
285     }
286     elseif (count($result) > 1) {
287       // If there is more than one session, then that must be unexpected.
288       $this->fail("Found more than 1 session for uid $uid.");
289     }
290     else {
291       $this->pass("Found session for uid $uid.");
292     }
293   }
294
295   /**
296    * Asserts that no session exists for a given uid.
297    *
298    * Based off masquarade module.
299    *
300    * @param int $uid
301    *   The user ID to assert.
302    *
303    * TODO find a cleaner way to do this check.
304    */
305   protected function assertNoSessionByUid($uid) {
306     $query = \Drupal::database()->select('sessions');
307     $query->fields('sessions', array('uid'));
308     $query->condition('uid', $uid);
309     $result = $query->execute()->fetchAll();
310     $this->assert(empty($result), "No session for uid $uid found.");
311   }
312
313 }