Updated Drupal to 8.6. This goes with the following updates because it's possible...
[yaffs-website] / web / core / modules / user / tests / src / Functional / UserCancelTest.php
1 <?php
2
3 namespace Drupal\Tests\user\Functional;
4
5 use Drupal\comment\CommentInterface;
6 use Drupal\comment\Entity\Comment;
7 use Drupal\comment\Tests\CommentTestTrait;
8 use Drupal\Tests\BrowserTestBase;
9 use Drupal\user\Entity\User;
10
11 /**
12  * Ensure that account cancellation methods work as expected.
13  *
14  * @group user
15  */
16 class UserCancelTest extends BrowserTestBase {
17
18   use CommentTestTrait;
19
20   /**
21    * Modules to enable.
22    *
23    * @var array
24    */
25   public static $modules = ['node', 'comment'];
26
27   protected function setUp() {
28     parent::setUp();
29
30     $this->drupalCreateContentType(['type' => 'page', 'name' => 'Basic page']);
31   }
32
33   /**
34    * Attempt to cancel account without permission.
35    */
36   public function testUserCancelWithoutPermission() {
37     $node_storage = $this->container->get('entity.manager')->getStorage('node');
38     $this->config('user.settings')->set('cancel_method', 'user_cancel_reassign')->save();
39     $user_storage = $this->container->get('entity.manager')->getStorage('user');
40
41     // Create a user.
42     $account = $this->drupalCreateUser([]);
43     $this->drupalLogin($account);
44     // Load a real user object.
45     $user_storage->resetCache([$account->id()]);
46     $account = $user_storage->load($account->id());
47
48     // Create a node.
49     $node = $this->drupalCreateNode(['uid' => $account->id()]);
50
51     // Attempt to cancel account.
52     $this->drupalGet('user/' . $account->id() . '/edit');
53     $this->assertNoRaw(t('Cancel account'), 'No cancel account button displayed.');
54
55     // Attempt bogus account cancellation request confirmation.
56     $timestamp = $account->getLastLoginTime();
57     $this->drupalGet("user/" . $account->id() . "/cancel/confirm/$timestamp/" . user_pass_rehash($account, $timestamp));
58     $this->assertResponse(403, 'Bogus cancelling request rejected.');
59     $user_storage->resetCache([$account->id()]);
60     $account = $user_storage->load($account->id());
61     $this->assertTrue($account->isActive(), 'User account was not canceled.');
62
63     // Confirm user's content has not been altered.
64     $node_storage->resetCache([$node->id()]);
65     $test_node = $node_storage->load($node->id());
66     $this->assertTrue(($test_node->getOwnerId() == $account->id() && $test_node->isPublished()), 'Node of the user has not been altered.');
67   }
68
69   /**
70    * Test ability to change the permission for canceling users.
71    */
72   public function testUserCancelChangePermission() {
73     \Drupal::service('module_installer')->install(['user_form_test']);
74     \Drupal::service('router.builder')->rebuild();
75     $this->config('user.settings')->set('cancel_method', 'user_cancel_reassign')->save();
76
77     // Create a regular user.
78     $account = $this->drupalCreateUser([]);
79
80     $admin_user = $this->drupalCreateUser(['cancel other accounts']);
81     $this->drupalLogin($admin_user);
82
83     // Delete regular user.
84     $this->drupalPostForm('user_form_test_cancel/' . $account->id(), [], t('Cancel account'));
85
86     // Confirm deletion.
87     $this->assertRaw(t('%name has been deleted.', ['%name' => $account->getUsername()]), 'User deleted.');
88     $this->assertFalse(User::load($account->id()), 'User is not found in the database.');
89   }
90
91   /**
92    * Tests that user account for uid 1 cannot be cancelled.
93    *
94    * This should never be possible, or the site owner would become unable to
95    * administer the site.
96    */
97   public function testUserCancelUid1() {
98     $user_storage = $this->container->get('entity.manager')->getStorage('user');
99
100     \Drupal::service('module_installer')->install(['views']);
101     \Drupal::service('router.builder')->rebuild();
102
103     // Try to cancel uid 1's account with a different user.
104     $admin_user = $this->drupalCreateUser(['administer users']);
105     $this->drupalLogin($admin_user);
106     $edit = [
107       'action' => 'user_cancel_user_action',
108       'user_bulk_form[0]' => TRUE,
109     ];
110     $this->drupalPostForm('admin/people', $edit, t('Apply to selected items'));
111
112     // Verify that uid 1's account was not cancelled.
113     $user_storage->resetCache([1]);
114     $user1 = $user_storage->load(1);
115     $this->assertTrue($user1->isActive(), 'User #1 still exists and is not blocked.');
116   }
117
118   /**
119    * Attempt invalid account cancellations.
120    */
121   public function testUserCancelInvalid() {
122     $node_storage = $this->container->get('entity.manager')->getStorage('node');
123     $this->config('user.settings')->set('cancel_method', 'user_cancel_reassign')->save();
124     $user_storage = $this->container->get('entity.manager')->getStorage('user');
125
126     // Create a user.
127     $account = $this->drupalCreateUser(['cancel account']);
128     $this->drupalLogin($account);
129     // Load a real user object.
130     $user_storage->resetCache([$account->id()]);
131     $account = $user_storage->load($account->id());
132
133     // Create a node.
134     $node = $this->drupalCreateNode(['uid' => $account->id()]);
135
136     // Attempt to cancel account.
137     $this->drupalPostForm('user/' . $account->id() . '/edit', NULL, t('Cancel account'));
138
139     // Confirm account cancellation.
140     $timestamp = time();
141     $this->drupalPostForm(NULL, NULL, t('Cancel account'));
142     $this->assertText(t('A confirmation request to cancel your account has been sent to your email address.'), 'Account cancellation request mailed message displayed.');
143
144     // Attempt bogus account cancellation request confirmation.
145     $bogus_timestamp = $timestamp + 60;
146     $this->drupalGet("user/" . $account->id() . "/cancel/confirm/$bogus_timestamp/" . user_pass_rehash($account, $bogus_timestamp));
147     $this->assertText(t('You have tried to use an account cancellation link that has expired. Please request a new one using the form below.'), 'Bogus cancelling request rejected.');
148     $user_storage->resetCache([$account->id()]);
149     $account = $user_storage->load($account->id());
150     $this->assertTrue($account->isActive(), 'User account was not canceled.');
151
152     // Attempt expired account cancellation request confirmation.
153     $bogus_timestamp = $timestamp - 86400 - 60;
154     $this->drupalGet("user/" . $account->id() . "/cancel/confirm/$bogus_timestamp/" . user_pass_rehash($account, $bogus_timestamp));
155     $this->assertText(t('You have tried to use an account cancellation link that has expired. Please request a new one using the form below.'), 'Expired cancel account request rejected.');
156     $user_storage->resetCache([$account->id()]);
157     $account = $user_storage->load($account->id());
158     $this->assertTrue($account->isActive(), 'User account was not canceled.');
159
160     // Confirm user's content has not been altered.
161     $node_storage->resetCache([$node->id()]);
162     $test_node = $node_storage->load($node->id());
163     $this->assertTrue(($test_node->getOwnerId() == $account->id() && $test_node->isPublished()), 'Node of the user has not been altered.');
164   }
165
166   /**
167    * Disable account and keep all content.
168    */
169   public function testUserBlock() {
170     $this->config('user.settings')->set('cancel_method', 'user_cancel_block')->save();
171     $user_storage = $this->container->get('entity.manager')->getStorage('user');
172
173     // Create a user.
174     $web_user = $this->drupalCreateUser(['cancel account']);
175     $this->drupalLogin($web_user);
176
177     // Load a real user object.
178     $user_storage->resetCache([$web_user->id()]);
179     $account = $user_storage->load($web_user->id());
180
181     // Attempt to cancel account.
182     $this->drupalGet('user/' . $account->id() . '/edit');
183     $this->drupalPostForm(NULL, NULL, t('Cancel account'));
184     $this->assertText(t('Are you sure you want to cancel your account?'), 'Confirmation form to cancel account displayed.');
185     $this->assertText(t('Your account will be blocked and you will no longer be able to log in. All of your content will remain attributed to your username.'), 'Informs that all content will be remain as is.');
186     $this->assertNoText(t('Select the method to cancel the account above.'), 'Does not allow user to select account cancellation method.');
187
188     // Confirm account cancellation.
189     $timestamp = time();
190
191     $this->drupalPostForm(NULL, NULL, t('Cancel account'));
192     $this->assertText(t('A confirmation request to cancel your account has been sent to your email address.'), 'Account cancellation request mailed message displayed.');
193
194     // Confirm account cancellation request.
195     $this->drupalGet("user/" . $account->id() . "/cancel/confirm/$timestamp/" . user_pass_rehash($account, $timestamp));
196     $user_storage->resetCache([$account->id()]);
197     $account = $user_storage->load($account->id());
198     $this->assertTrue($account->isBlocked(), 'User has been blocked.');
199
200     // Confirm that the confirmation message made it through to the end user.
201     $this->assertRaw(t('%name has been disabled.', ['%name' => $account->getUsername()]), "Confirmation message displayed to user.");
202   }
203
204   /**
205    * Disable account and unpublish all content.
206    */
207   public function testUserBlockUnpublish() {
208     $node_storage = $this->container->get('entity.manager')->getStorage('node');
209     $this->config('user.settings')->set('cancel_method', 'user_cancel_block_unpublish')->save();
210     // Create comment field on page.
211     $this->addDefaultCommentField('node', 'page');
212     $user_storage = $this->container->get('entity.manager')->getStorage('user');
213
214     // Create a user.
215     $account = $this->drupalCreateUser(['cancel account']);
216     $this->drupalLogin($account);
217     // Load a real user object.
218     $user_storage->resetCache([$account->id()]);
219     $account = $user_storage->load($account->id());
220
221     // Create a node with two revisions.
222     $node = $this->drupalCreateNode(['uid' => $account->id()]);
223     $settings = get_object_vars($node);
224     $settings['revision'] = 1;
225     $node = $this->drupalCreateNode($settings);
226
227     // Add a comment to the page.
228     $comment_subject = $this->randomMachineName(8);
229     $comment_body = $this->randomMachineName(8);
230     $comment = Comment::create([
231       'subject' => $comment_subject,
232       'comment_body' => $comment_body,
233       'entity_id' => $node->id(),
234       'entity_type' => 'node',
235       'field_name' => 'comment',
236       'status' => CommentInterface::PUBLISHED,
237       'uid' => $account->id(),
238     ]);
239     $comment->save();
240
241     // Attempt to cancel account.
242     $this->drupalGet('user/' . $account->id() . '/edit');
243     $this->drupalPostForm(NULL, NULL, t('Cancel account'));
244     $this->assertText(t('Are you sure you want to cancel your account?'), 'Confirmation form to cancel account displayed.');
245     $this->assertText(t('Your account will be blocked and you will no longer be able to log in. All of your content will be hidden from everyone but administrators.'), 'Informs that all content will be unpublished.');
246
247     // Confirm account cancellation.
248     $timestamp = time();
249     $this->drupalPostForm(NULL, NULL, t('Cancel account'));
250     $this->assertText(t('A confirmation request to cancel your account has been sent to your email address.'), 'Account cancellation request mailed message displayed.');
251
252     // Confirm account cancellation request.
253     $this->drupalGet("user/" . $account->id() . "/cancel/confirm/$timestamp/" . user_pass_rehash($account, $timestamp));
254     // Confirm that the user was redirected to the front page.
255     $this->assertSession()->addressEquals('');
256     $this->assertSession()->statusCodeEquals(200);
257     // Confirm that the confirmation message made it through to the end user.
258     $this->assertRaw(t('%name has been disabled.', ['%name' => $account->getUsername()]), "Confirmation message displayed to user.");
259
260     $user_storage->resetCache([$account->id()]);
261     $account = $user_storage->load($account->id());
262     $this->assertTrue($account->isBlocked(), 'User has been blocked.');
263
264     // Confirm user's content has been unpublished.
265     $node_storage->resetCache([$node->id()]);
266     $test_node = $node_storage->load($node->id());
267     $this->assertFalse($test_node->isPublished(), 'Node of the user has been unpublished.');
268     $test_node = node_revision_load($node->getRevisionId());
269     $this->assertFalse($test_node->isPublished(), 'Node revision of the user has been unpublished.');
270
271     $storage = \Drupal::entityManager()->getStorage('comment');
272     $storage->resetCache([$comment->id()]);
273     $comment = $storage->load($comment->id());
274     $this->assertFalse($comment->isPublished(), 'Comment of the user has been unpublished.');
275   }
276
277   /**
278    * Delete account and anonymize all content.
279    */
280   public function testUserAnonymize() {
281     $node_storage = $this->container->get('entity.manager')->getStorage('node');
282     $this->config('user.settings')->set('cancel_method', 'user_cancel_reassign')->save();
283     // Create comment field on page.
284     $this->addDefaultCommentField('node', 'page');
285     $user_storage = $this->container->get('entity.manager')->getStorage('user');
286
287     // Create a user.
288     $account = $this->drupalCreateUser(['cancel account']);
289     $this->drupalLogin($account);
290     // Load a real user object.
291     $user_storage->resetCache([$account->id()]);
292     $account = $user_storage->load($account->id());
293
294     // Create a simple node.
295     $node = $this->drupalCreateNode(['uid' => $account->id()]);
296
297     // Add a comment to the page.
298     $comment_subject = $this->randomMachineName(8);
299     $comment_body = $this->randomMachineName(8);
300     $comment = Comment::create([
301       'subject' => $comment_subject,
302       'comment_body' => $comment_body,
303       'entity_id' => $node->id(),
304       'entity_type' => 'node',
305       'field_name' => 'comment',
306       'status' => CommentInterface::PUBLISHED,
307       'uid' => $account->id(),
308     ]);
309     $comment->save();
310
311     // Create a node with two revisions, the initial one belonging to the
312     // cancelling user.
313     $revision_node = $this->drupalCreateNode(['uid' => $account->id()]);
314     $revision = $revision_node->getRevisionId();
315     $settings = get_object_vars($revision_node);
316     $settings['revision'] = 1;
317     // Set new/current revision to someone else.
318     $settings['uid'] = 1;
319     $revision_node = $this->drupalCreateNode($settings);
320
321     // Attempt to cancel account.
322     $this->drupalGet('user/' . $account->id() . '/edit');
323     $this->drupalPostForm(NULL, NULL, t('Cancel account'));
324     $this->assertText(t('Are you sure you want to cancel your account?'), 'Confirmation form to cancel account displayed.');
325     $this->assertRaw(t('Your account will be removed and all account information deleted. All of your content will be assigned to the %anonymous-name user.', ['%anonymous-name' => $this->config('user.settings')->get('anonymous')]), 'Informs that all content will be attributed to anonymous account.');
326
327     // Confirm account cancellation.
328     $timestamp = time();
329     $this->drupalPostForm(NULL, NULL, t('Cancel account'));
330     $this->assertText(t('A confirmation request to cancel your account has been sent to your email address.'), 'Account cancellation request mailed message displayed.');
331
332     // Confirm account cancellation request.
333     $this->drupalGet("user/" . $account->id() . "/cancel/confirm/$timestamp/" . user_pass_rehash($account, $timestamp));
334     $user_storage->resetCache([$account->id()]);
335     $this->assertFalse($user_storage->load($account->id()), 'User is not found in the database.');
336
337     // Confirm that user's content has been attributed to anonymous user.
338     $anonymous_user = User::getAnonymousUser();
339     $node_storage->resetCache([$node->id()]);
340     $test_node = $node_storage->load($node->id());
341     $this->assertTrue(($test_node->getOwnerId() == 0 && $test_node->isPublished()), 'Node of the user has been attributed to anonymous user.');
342     $test_node = node_revision_load($revision, TRUE);
343     $this->assertTrue(($test_node->getRevisionUser()->id() == 0 && $test_node->isPublished()), 'Node revision of the user has been attributed to anonymous user.');
344     $node_storage->resetCache([$revision_node->id()]);
345     $test_node = $node_storage->load($revision_node->id());
346     $this->assertTrue(($test_node->getOwnerId() != 0 && $test_node->isPublished()), "Current revision of the user's node was not attributed to anonymous user.");
347
348     $storage = \Drupal::entityManager()->getStorage('comment');
349     $storage->resetCache([$comment->id()]);
350     $test_comment = $storage->load($comment->id());
351     $this->assertTrue(($test_comment->getOwnerId() == 0 && $test_comment->isPublished()), 'Comment of the user has been attributed to anonymous user.');
352     $this->assertEqual($test_comment->getAuthorName(), $anonymous_user->getDisplayName(), 'Comment of the user has been attributed to anonymous user name.');
353
354     // Confirm that the confirmation message made it through to the end user.
355     $this->assertRaw(t('%name has been deleted.', ['%name' => $account->getUsername()]), "Confirmation message displayed to user.");
356   }
357
358   /**
359    * Delete account and anonymize all content using a batch process.
360    */
361   public function testUserAnonymizeBatch() {
362     $node_storage = $this->container->get('entity.manager')->getStorage('node');
363     $this->config('user.settings')->set('cancel_method', 'user_cancel_reassign')->save();
364     $user_storage = $this->container->get('entity.manager')->getStorage('user');
365
366     // Create a user.
367     $account = $this->drupalCreateUser(['cancel account']);
368     $this->drupalLogin($account);
369     // Load a real user object.
370     $user_storage->resetCache([$account->id()]);
371     $account = $user_storage->load($account->id());
372
373     // Create 11 nodes in order to trigger batch processing in
374     // node_mass_update().
375     $nodes = [];
376     for ($i = 0; $i < 11; $i++) {
377       $node = $this->drupalCreateNode(['uid' => $account->id()]);
378       $nodes[$node->id()] = $node;
379     }
380
381     // Attempt to cancel account.
382     $this->drupalGet('user/' . $account->id() . '/edit');
383     $this->drupalPostForm(NULL, NULL, t('Cancel account'));
384     $this->assertText(t('Are you sure you want to cancel your account?'), 'Confirmation form to cancel account displayed.');
385     $this->assertRaw(t('Your account will be removed and all account information deleted. All of your content will be assigned to the %anonymous-name user.', ['%anonymous-name' => $this->config('user.settings')->get('anonymous')]), 'Informs that all content will be attributed to anonymous account.');
386
387     // Confirm account cancellation.
388     $timestamp = time();
389     $this->drupalPostForm(NULL, NULL, t('Cancel account'));
390     $this->assertText(t('A confirmation request to cancel your account has been sent to your email address.'), 'Account cancellation request mailed message displayed.');
391
392     // Confirm account cancellation request.
393     $this->drupalGet("user/" . $account->id() . "/cancel/confirm/$timestamp/" . user_pass_rehash($account, $timestamp));
394     $user_storage->resetCache([$account->id()]);
395     $this->assertFalse($user_storage->load($account->id()), 'User is not found in the database.');
396
397     // Confirm that user's content has been attributed to anonymous user.
398     $node_storage->resetCache(array_keys($nodes));
399     $test_nodes = $node_storage->loadMultiple(array_keys($nodes));
400     foreach ($test_nodes as $test_node) {
401       $this->assertTrue(($test_node->getOwnerId() == 0 && $test_node->isPublished()), 'Node ' . $test_node->id() . ' of the user has been attributed to anonymous user.');
402     }
403   }
404
405   /**
406    * Delete account and remove all content.
407    */
408   public function testUserDelete() {
409     $node_storage = $this->container->get('entity.manager')->getStorage('node');
410     $this->config('user.settings')->set('cancel_method', 'user_cancel_delete')->save();
411     \Drupal::service('module_installer')->install(['comment']);
412     $this->resetAll();
413     $this->addDefaultCommentField('node', 'page');
414     $user_storage = $this->container->get('entity.manager')->getStorage('user');
415
416     // Create a user.
417     $account = $this->drupalCreateUser(['cancel account', 'post comments', 'skip comment approval']);
418     $this->drupalLogin($account);
419     // Load a real user object.
420     $user_storage->resetCache([$account->id()]);
421     $account = $user_storage->load($account->id());
422
423     // Create a simple node.
424     $node = $this->drupalCreateNode(['uid' => $account->id()]);
425
426     // Create comment.
427     $edit = [];
428     $edit['subject[0][value]'] = $this->randomMachineName(8);
429     $edit['comment_body[0][value]'] = $this->randomMachineName(16);
430
431     $this->drupalPostForm('comment/reply/node/' . $node->id() . '/comment', $edit, t('Preview'));
432     $this->drupalPostForm(NULL, [], t('Save'));
433     $this->assertText(t('Your comment has been posted.'));
434     $comments = entity_load_multiple_by_properties('comment', ['subject' => $edit['subject[0][value]']]);
435     $comment = reset($comments);
436     $this->assertTrue($comment->id(), 'Comment found.');
437
438     // Create a node with two revisions, the initial one belonging to the
439     // cancelling user.
440     $revision_node = $this->drupalCreateNode(['uid' => $account->id()]);
441     $revision = $revision_node->getRevisionId();
442     $settings = get_object_vars($revision_node);
443     $settings['revision'] = 1;
444     // Set new/current revision to someone else.
445     $settings['uid'] = 1;
446     $revision_node = $this->drupalCreateNode($settings);
447
448     // Attempt to cancel account.
449     $this->drupalGet('user/' . $account->id() . '/edit');
450     $this->drupalPostForm(NULL, NULL, t('Cancel account'));
451     $this->assertText(t('Are you sure you want to cancel your account?'), 'Confirmation form to cancel account displayed.');
452     $this->assertText(t('Your account will be removed and all account information deleted. All of your content will also be deleted.'), 'Informs that all content will be deleted.');
453
454     // Confirm account cancellation.
455     $timestamp = time();
456     $this->drupalPostForm(NULL, NULL, t('Cancel account'));
457     $this->assertText(t('A confirmation request to cancel your account has been sent to your email address.'), 'Account cancellation request mailed message displayed.');
458
459     // Confirm account cancellation request.
460     $this->drupalGet("user/" . $account->id() . "/cancel/confirm/$timestamp/" . user_pass_rehash($account, $timestamp));
461     $user_storage->resetCache([$account->id()]);
462     $this->assertFalse($user_storage->load($account->id()), 'User is not found in the database.');
463
464     // Confirm that user's content has been deleted.
465     $node_storage->resetCache([$node->id()]);
466     $this->assertFalse($node_storage->load($node->id()), 'Node of the user has been deleted.');
467     $this->assertFalse(node_revision_load($revision), 'Node revision of the user has been deleted.');
468     $node_storage->resetCache([$revision_node->id()]);
469     $this->assertTrue($node_storage->load($revision_node->id()), "Current revision of the user's node was not deleted.");
470     \Drupal::entityManager()->getStorage('comment')->resetCache([$comment->id()]);
471     $this->assertFalse(Comment::load($comment->id()), 'Comment of the user has been deleted.');
472
473     // Confirm that the confirmation message made it through to the end user.
474     $this->assertRaw(t('%name has been deleted.', ['%name' => $account->getUsername()]), "Confirmation message displayed to user.");
475   }
476
477   /**
478    * Create an administrative user and delete another user.
479    */
480   public function testUserCancelByAdmin() {
481     $this->config('user.settings')->set('cancel_method', 'user_cancel_reassign')->save();
482
483     // Create a regular user.
484     $account = $this->drupalCreateUser([]);
485
486     // Create administrative user.
487     $admin_user = $this->drupalCreateUser(['administer users']);
488     $this->drupalLogin($admin_user);
489
490     // Delete regular user.
491     $this->drupalGet('user/' . $account->id() . '/edit');
492     $this->drupalPostForm(NULL, NULL, t('Cancel account'));
493     $this->assertRaw(t('Are you sure you want to cancel the account %name?', ['%name' => $account->getUsername()]), 'Confirmation form to cancel account displayed.');
494     $this->assertText(t('Select the method to cancel the account above.'), 'Allows to select account cancellation method.');
495
496     // Confirm deletion.
497     $this->drupalPostForm(NULL, NULL, t('Cancel account'));
498     $this->assertRaw(t('%name has been deleted.', ['%name' => $account->getUsername()]), 'User deleted.');
499     $this->assertFalse(User::load($account->id()), 'User is not found in the database.');
500   }
501
502   /**
503    * Tests deletion of a user account without an email address.
504    */
505   public function testUserWithoutEmailCancelByAdmin() {
506     $this->config('user.settings')->set('cancel_method', 'user_cancel_reassign')->save();
507
508     // Create a regular user.
509     $account = $this->drupalCreateUser([]);
510     // This user has no email address.
511     $account->mail = '';
512     $account->save();
513
514     // Create administrative user.
515     $admin_user = $this->drupalCreateUser(['administer users']);
516     $this->drupalLogin($admin_user);
517
518     // Delete regular user without email address.
519     $this->drupalGet('user/' . $account->id() . '/edit');
520     $this->drupalPostForm(NULL, NULL, t('Cancel account'));
521     $this->assertRaw(t('Are you sure you want to cancel the account %name?', ['%name' => $account->getUsername()]), 'Confirmation form to cancel account displayed.');
522     $this->assertText(t('Select the method to cancel the account above.'), 'Allows to select account cancellation method.');
523
524     // Confirm deletion.
525     $this->drupalPostForm(NULL, NULL, t('Cancel account'));
526     $this->assertRaw(t('%name has been deleted.', ['%name' => $account->getUsername()]), 'User deleted.');
527     $this->assertFalse(User::load($account->id()), 'User is not found in the database.');
528   }
529
530   /**
531    * Create an administrative user and mass-delete other users.
532    */
533   public function testMassUserCancelByAdmin() {
534     \Drupal::service('module_installer')->install(['views']);
535     \Drupal::service('router.builder')->rebuild();
536     $this->config('user.settings')->set('cancel_method', 'user_cancel_reassign')->save();
537     $user_storage = $this->container->get('entity.manager')->getStorage('user');
538     // Enable account cancellation notification.
539     $this->config('user.settings')->set('notify.status_canceled', TRUE)->save();
540
541     // Create administrative user.
542     $admin_user = $this->drupalCreateUser(['administer users']);
543     $this->drupalLogin($admin_user);
544
545     // Create some users.
546     $users = [];
547     for ($i = 0; $i < 3; $i++) {
548       $account = $this->drupalCreateUser([]);
549       $users[$account->id()] = $account;
550     }
551
552     // Cancel user accounts, including own one.
553     $edit = [];
554     $edit['action'] = 'user_cancel_user_action';
555     for ($i = 0; $i <= 4; $i++) {
556       $edit['user_bulk_form[' . $i . ']'] = TRUE;
557     }
558     $this->drupalPostForm('admin/people', $edit, t('Apply to selected items'));
559     $this->assertText(t('Are you sure you want to cancel these user accounts?'), 'Confirmation form to cancel accounts displayed.');
560     $this->assertText(t('When cancelling these accounts'), 'Allows to select account cancellation method.');
561     $this->assertText(t('Require email confirmation to cancel account'), 'Allows to send confirmation mail.');
562     $this->assertText(t('Notify user when account is canceled'), 'Allows to send notification mail.');
563
564     // Confirm deletion.
565     $this->drupalPostForm(NULL, NULL, t('Cancel accounts'));
566     $status = TRUE;
567     foreach ($users as $account) {
568       $status = $status && (strpos($this->getTextContent(), $account->getUsername() . ' has been deleted.') !== FALSE);
569       $user_storage->resetCache([$account->id()]);
570       $status = $status && !$user_storage->load($account->id());
571     }
572     $this->assertTrue($status, 'Users deleted and not found in the database.');
573
574     // Ensure that admin account was not cancelled.
575     $this->assertText(t('A confirmation request to cancel your account has been sent to your email address.'), 'Account cancellation request mailed message displayed.');
576     $admin_user = $user_storage->load($admin_user->id());
577     $this->assertTrue($admin_user->isActive(), 'Administrative user is found in the database and enabled.');
578
579     // Verify that uid 1's account was not cancelled.
580     $user_storage->resetCache([1]);
581     $user1 = $user_storage->load(1);
582     $this->assertTrue($user1->isActive(), 'User #1 still exists and is not blocked.');
583   }
584
585   /**
586    * Tests user cancel with node access.
587    */
588   public function testUserDeleteWithContentAndNodeAccess() {
589
590     \Drupal::service('module_installer')->install(['node_access_test']);
591     // Rebuild node access.
592     node_access_rebuild();
593
594     $account = $this->drupalCreateUser(['access content']);
595     $node = $this->drupalCreateNode(['type' => 'page', 'uid' => $account->id()]);
596     $account->delete();
597     $load2 = \Drupal::entityTypeManager()->getStorage('node')->load($node->id());
598     $this->assertTrue(empty($load2));
599   }
600
601 }