83020b431a91def69bfedacb26720f7172a641b3
[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     // Update uid 1's name and password to we know it.
103     $password = user_password();
104     $account = [
105       'name' => 'user1',
106       'pass' => $this->container->get('password')->hash(trim($password)),
107     ];
108     // We cannot use $account->save() here, because this would result in the
109     // password being hashed again.
110     db_update('users_field_data')
111       ->fields($account)
112       ->condition('uid', 1)
113       ->execute();
114
115     // Reload and log in uid 1.
116     $user_storage->resetCache([1]);
117     $user1 = $user_storage->load(1);
118     $user1->pass_raw = $password;
119
120     // Try to cancel uid 1's account with a different user.
121     $admin_user = $this->drupalCreateUser(['administer users']);
122     $this->drupalLogin($admin_user);
123     $edit = [
124       'action' => 'user_cancel_user_action',
125       'user_bulk_form[0]' => TRUE,
126     ];
127     $this->drupalPostForm('admin/people', $edit, t('Apply to selected items'));
128
129     // Verify that uid 1's account was not cancelled.
130     $user_storage->resetCache([1]);
131     $user1 = $user_storage->load(1);
132     $this->assertTrue($user1->isActive(), 'User #1 still exists and is not blocked.');
133   }
134
135   /**
136    * Attempt invalid account cancellations.
137    */
138   public function testUserCancelInvalid() {
139     $node_storage = $this->container->get('entity.manager')->getStorage('node');
140     $this->config('user.settings')->set('cancel_method', 'user_cancel_reassign')->save();
141     $user_storage = $this->container->get('entity.manager')->getStorage('user');
142
143     // Create a user.
144     $account = $this->drupalCreateUser(['cancel account']);
145     $this->drupalLogin($account);
146     // Load a real user object.
147     $user_storage->resetCache([$account->id()]);
148     $account = $user_storage->load($account->id());
149
150     // Create a node.
151     $node = $this->drupalCreateNode(['uid' => $account->id()]);
152
153     // Attempt to cancel account.
154     $this->drupalPostForm('user/' . $account->id() . '/edit', NULL, t('Cancel account'));
155
156     // Confirm account cancellation.
157     $timestamp = time();
158     $this->drupalPostForm(NULL, NULL, t('Cancel account'));
159     $this->assertText(t('A confirmation request to cancel your account has been sent to your email address.'), 'Account cancellation request mailed message displayed.');
160
161     // Attempt bogus account cancellation request confirmation.
162     $bogus_timestamp = $timestamp + 60;
163     $this->drupalGet("user/" . $account->id() . "/cancel/confirm/$bogus_timestamp/" . user_pass_rehash($account, $bogus_timestamp));
164     $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.');
165     $user_storage->resetCache([$account->id()]);
166     $account = $user_storage->load($account->id());
167     $this->assertTrue($account->isActive(), 'User account was not canceled.');
168
169     // Attempt expired account cancellation request confirmation.
170     $bogus_timestamp = $timestamp - 86400 - 60;
171     $this->drupalGet("user/" . $account->id() . "/cancel/confirm/$bogus_timestamp/" . user_pass_rehash($account, $bogus_timestamp));
172     $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.');
173     $user_storage->resetCache([$account->id()]);
174     $account = $user_storage->load($account->id());
175     $this->assertTrue($account->isActive(), 'User account was not canceled.');
176
177     // Confirm user's content has not been altered.
178     $node_storage->resetCache([$node->id()]);
179     $test_node = $node_storage->load($node->id());
180     $this->assertTrue(($test_node->getOwnerId() == $account->id() && $test_node->isPublished()), 'Node of the user has not been altered.');
181   }
182
183   /**
184    * Disable account and keep all content.
185    */
186   public function testUserBlock() {
187     $this->config('user.settings')->set('cancel_method', 'user_cancel_block')->save();
188     $user_storage = $this->container->get('entity.manager')->getStorage('user');
189
190     // Create a user.
191     $web_user = $this->drupalCreateUser(['cancel account']);
192     $this->drupalLogin($web_user);
193
194     // Load a real user object.
195     $user_storage->resetCache([$web_user->id()]);
196     $account = $user_storage->load($web_user->id());
197
198     // Attempt to cancel account.
199     $this->drupalGet('user/' . $account->id() . '/edit');
200     $this->drupalPostForm(NULL, NULL, t('Cancel account'));
201     $this->assertText(t('Are you sure you want to cancel your account?'), 'Confirmation form to cancel account displayed.');
202     $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.');
203     $this->assertNoText(t('Select the method to cancel the account above.'), 'Does not allow user to select account cancellation method.');
204
205     // Confirm account cancellation.
206     $timestamp = time();
207
208     $this->drupalPostForm(NULL, NULL, t('Cancel account'));
209     $this->assertText(t('A confirmation request to cancel your account has been sent to your email address.'), 'Account cancellation request mailed message displayed.');
210
211     // Confirm account cancellation request.
212     $this->drupalGet("user/" . $account->id() . "/cancel/confirm/$timestamp/" . user_pass_rehash($account, $timestamp));
213     $user_storage->resetCache([$account->id()]);
214     $account = $user_storage->load($account->id());
215     $this->assertTrue($account->isBlocked(), 'User has been blocked.');
216
217     // Confirm that the confirmation message made it through to the end user.
218     $this->assertRaw(t('%name has been disabled.', ['%name' => $account->getUsername()]), "Confirmation message displayed to user.");
219   }
220
221   /**
222    * Disable account and unpublish all content.
223    */
224   public function testUserBlockUnpublish() {
225     $node_storage = $this->container->get('entity.manager')->getStorage('node');
226     $this->config('user.settings')->set('cancel_method', 'user_cancel_block_unpublish')->save();
227     // Create comment field on page.
228     $this->addDefaultCommentField('node', 'page');
229     $user_storage = $this->container->get('entity.manager')->getStorage('user');
230
231     // Create a user.
232     $account = $this->drupalCreateUser(['cancel account']);
233     $this->drupalLogin($account);
234     // Load a real user object.
235     $user_storage->resetCache([$account->id()]);
236     $account = $user_storage->load($account->id());
237
238     // Create a node with two revisions.
239     $node = $this->drupalCreateNode(['uid' => $account->id()]);
240     $settings = get_object_vars($node);
241     $settings['revision'] = 1;
242     $node = $this->drupalCreateNode($settings);
243
244     // Add a comment to the page.
245     $comment_subject = $this->randomMachineName(8);
246     $comment_body = $this->randomMachineName(8);
247     $comment = Comment::create([
248       'subject' => $comment_subject,
249       'comment_body' => $comment_body,
250       'entity_id' => $node->id(),
251       'entity_type' => 'node',
252       'field_name' => 'comment',
253       'status' => CommentInterface::PUBLISHED,
254       'uid' => $account->id(),
255     ]);
256     $comment->save();
257
258     // Attempt to cancel account.
259     $this->drupalGet('user/' . $account->id() . '/edit');
260     $this->drupalPostForm(NULL, NULL, t('Cancel account'));
261     $this->assertText(t('Are you sure you want to cancel your account?'), 'Confirmation form to cancel account displayed.');
262     $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.');
263
264     // Confirm account cancellation.
265     $timestamp = time();
266     $this->drupalPostForm(NULL, NULL, t('Cancel account'));
267     $this->assertText(t('A confirmation request to cancel your account has been sent to your email address.'), 'Account cancellation request mailed message displayed.');
268
269     // Confirm account cancellation request.
270     $this->drupalGet("user/" . $account->id() . "/cancel/confirm/$timestamp/" . user_pass_rehash($account, $timestamp));
271     $user_storage->resetCache([$account->id()]);
272     $account = $user_storage->load($account->id());
273     $this->assertTrue($account->isBlocked(), 'User has been blocked.');
274
275     // Confirm user's content has been unpublished.
276     $node_storage->resetCache([$node->id()]);
277     $test_node = $node_storage->load($node->id());
278     $this->assertFalse($test_node->isPublished(), 'Node of the user has been unpublished.');
279     $test_node = node_revision_load($node->getRevisionId());
280     $this->assertFalse($test_node->isPublished(), 'Node revision of the user has been unpublished.');
281
282     $storage = \Drupal::entityManager()->getStorage('comment');
283     $storage->resetCache([$comment->id()]);
284     $comment = $storage->load($comment->id());
285     $this->assertFalse($comment->isPublished(), 'Comment of the user has been unpublished.');
286
287     // Confirm that the confirmation message made it through to the end user.
288     $this->assertRaw(t('%name has been disabled.', ['%name' => $account->getUsername()]), "Confirmation message displayed to user.");
289   }
290
291   /**
292    * Delete account and anonymize all content.
293    */
294   public function testUserAnonymize() {
295     $node_storage = $this->container->get('entity.manager')->getStorage('node');
296     $this->config('user.settings')->set('cancel_method', 'user_cancel_reassign')->save();
297     // Create comment field on page.
298     $this->addDefaultCommentField('node', 'page');
299     $user_storage = $this->container->get('entity.manager')->getStorage('user');
300
301     // Create a user.
302     $account = $this->drupalCreateUser(['cancel account']);
303     $this->drupalLogin($account);
304     // Load a real user object.
305     $user_storage->resetCache([$account->id()]);
306     $account = $user_storage->load($account->id());
307
308     // Create a simple node.
309     $node = $this->drupalCreateNode(['uid' => $account->id()]);
310
311     // Add a comment to the page.
312     $comment_subject = $this->randomMachineName(8);
313     $comment_body = $this->randomMachineName(8);
314     $comment = Comment::create([
315       'subject' => $comment_subject,
316       'comment_body' => $comment_body,
317       'entity_id' => $node->id(),
318       'entity_type' => 'node',
319       'field_name' => 'comment',
320       'status' => CommentInterface::PUBLISHED,
321       'uid' => $account->id(),
322     ]);
323     $comment->save();
324
325     // Create a node with two revisions, the initial one belonging to the
326     // cancelling user.
327     $revision_node = $this->drupalCreateNode(['uid' => $account->id()]);
328     $revision = $revision_node->getRevisionId();
329     $settings = get_object_vars($revision_node);
330     $settings['revision'] = 1;
331     $settings['uid'] = 1; // Set new/current revision to someone else.
332     $revision_node = $this->drupalCreateNode($settings);
333
334     // Attempt to cancel account.
335     $this->drupalGet('user/' . $account->id() . '/edit');
336     $this->drupalPostForm(NULL, NULL, t('Cancel account'));
337     $this->assertText(t('Are you sure you want to cancel your account?'), 'Confirmation form to cancel account displayed.');
338     $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.');
339
340     // Confirm account cancellation.
341     $timestamp = time();
342     $this->drupalPostForm(NULL, NULL, t('Cancel account'));
343     $this->assertText(t('A confirmation request to cancel your account has been sent to your email address.'), 'Account cancellation request mailed message displayed.');
344
345     // Confirm account cancellation request.
346     $this->drupalGet("user/" . $account->id() . "/cancel/confirm/$timestamp/" . user_pass_rehash($account, $timestamp));
347     $user_storage->resetCache([$account->id()]);
348     $this->assertFalse($user_storage->load($account->id()), 'User is not found in the database.');
349
350     // Confirm that user's content has been attributed to anonymous user.
351     $anonymous_user = User::getAnonymousUser();
352     $node_storage->resetCache([$node->id()]);
353     $test_node = $node_storage->load($node->id());
354     $this->assertTrue(($test_node->getOwnerId() == 0 && $test_node->isPublished()), 'Node of the user has been attributed to anonymous user.');
355     $test_node = node_revision_load($revision, TRUE);
356     $this->assertTrue(($test_node->getRevisionUser()->id() == 0 && $test_node->isPublished()), 'Node revision of the user has been attributed to anonymous user.');
357     $node_storage->resetCache([$revision_node->id()]);
358     $test_node = $node_storage->load($revision_node->id());
359     $this->assertTrue(($test_node->getOwnerId() != 0 && $test_node->isPublished()), "Current revision of the user's node was not attributed to anonymous user.");
360
361     $storage = \Drupal::entityManager()->getStorage('comment');
362     $storage->resetCache([$comment->id()]);
363     $test_comment = $storage->load($comment->id());
364     $this->assertTrue(($test_comment->getOwnerId() == 0 && $test_comment->isPublished()), 'Comment of the user has been attributed to anonymous user.');
365     $this->assertEqual($test_comment->getAuthorName(), $anonymous_user->getDisplayName(), 'Comment of the user has been attributed to anonymous user name.');
366
367     // Confirm that the confirmation message made it through to the end user.
368     $this->assertRaw(t('%name has been deleted.', ['%name' => $account->getUsername()]), "Confirmation message displayed to user.");
369   }
370
371   /**
372    * Delete account and anonymize all content using a batch process.
373    */
374   public function testUserAnonymizeBatch() {
375     $node_storage = $this->container->get('entity.manager')->getStorage('node');
376     $this->config('user.settings')->set('cancel_method', 'user_cancel_reassign')->save();
377     $user_storage = $this->container->get('entity.manager')->getStorage('user');
378
379     // Create a user.
380     $account = $this->drupalCreateUser(['cancel account']);
381     $this->drupalLogin($account);
382     // Load a real user object.
383     $user_storage->resetCache([$account->id()]);
384     $account = $user_storage->load($account->id());
385
386     // Create 11 nodes in order to trigger batch processing in
387     // node_mass_update().
388     $nodes = [];
389     for ($i = 0; $i < 11; $i++) {
390       $node = $this->drupalCreateNode(['uid' => $account->id()]);
391       $nodes[$node->id()] = $node;
392     }
393
394     // Attempt to cancel account.
395     $this->drupalGet('user/' . $account->id() . '/edit');
396     $this->drupalPostForm(NULL, NULL, t('Cancel account'));
397     $this->assertText(t('Are you sure you want to cancel your account?'), 'Confirmation form to cancel account displayed.');
398     $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.');
399
400     // Confirm account cancellation.
401     $timestamp = time();
402     $this->drupalPostForm(NULL, NULL, t('Cancel account'));
403     $this->assertText(t('A confirmation request to cancel your account has been sent to your email address.'), 'Account cancellation request mailed message displayed.');
404
405     // Confirm account cancellation request.
406     $this->drupalGet("user/" . $account->id() . "/cancel/confirm/$timestamp/" . user_pass_rehash($account, $timestamp));
407     $user_storage->resetCache([$account->id()]);
408     $this->assertFalse($user_storage->load($account->id()), 'User is not found in the database.');
409
410     // Confirm that user's content has been attributed to anonymous user.
411     $node_storage->resetCache(array_keys($nodes));
412     $test_nodes = $node_storage->loadMultiple(array_keys($nodes));
413     foreach ($test_nodes as $test_node) {
414       $this->assertTrue(($test_node->getOwnerId() == 0 && $test_node->isPublished()), 'Node ' . $test_node->id() . ' of the user has been attributed to anonymous user.');
415     }
416   }
417
418   /**
419    * Delete account and remove all content.
420    */
421   public function testUserDelete() {
422     $node_storage = $this->container->get('entity.manager')->getStorage('node');
423     $this->config('user.settings')->set('cancel_method', 'user_cancel_delete')->save();
424     \Drupal::service('module_installer')->install(['comment']);
425     $this->resetAll();
426     $this->addDefaultCommentField('node', 'page');
427     $user_storage = $this->container->get('entity.manager')->getStorage('user');
428
429     // Create a user.
430     $account = $this->drupalCreateUser(['cancel account', 'post comments', 'skip comment approval']);
431     $this->drupalLogin($account);
432     // Load a real user object.
433     $user_storage->resetCache([$account->id()]);
434     $account = $user_storage->load($account->id());
435
436     // Create a simple node.
437     $node = $this->drupalCreateNode(['uid' => $account->id()]);
438
439     // Create comment.
440     $edit = [];
441     $edit['subject[0][value]'] = $this->randomMachineName(8);
442     $edit['comment_body[0][value]'] = $this->randomMachineName(16);
443
444     $this->drupalPostForm('comment/reply/node/' . $node->id() . '/comment', $edit, t('Preview'));
445     $this->drupalPostForm(NULL, [], t('Save'));
446     $this->assertText(t('Your comment has been posted.'));
447     $comments = entity_load_multiple_by_properties('comment', ['subject' => $edit['subject[0][value]']]);
448     $comment = reset($comments);
449     $this->assertTrue($comment->id(), 'Comment found.');
450
451     // Create a node with two revisions, the initial one belonging to the
452     // cancelling user.
453     $revision_node = $this->drupalCreateNode(['uid' => $account->id()]);
454     $revision = $revision_node->getRevisionId();
455     $settings = get_object_vars($revision_node);
456     $settings['revision'] = 1;
457     $settings['uid'] = 1; // Set new/current revision to someone else.
458     $revision_node = $this->drupalCreateNode($settings);
459
460     // Attempt to cancel account.
461     $this->drupalGet('user/' . $account->id() . '/edit');
462     $this->drupalPostForm(NULL, NULL, t('Cancel account'));
463     $this->assertText(t('Are you sure you want to cancel your account?'), 'Confirmation form to cancel account displayed.');
464     $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.');
465
466     // Confirm account cancellation.
467     $timestamp = time();
468     $this->drupalPostForm(NULL, NULL, t('Cancel account'));
469     $this->assertText(t('A confirmation request to cancel your account has been sent to your email address.'), 'Account cancellation request mailed message displayed.');
470
471     // Confirm account cancellation request.
472     $this->drupalGet("user/" . $account->id() . "/cancel/confirm/$timestamp/" . user_pass_rehash($account, $timestamp));
473     $user_storage->resetCache([$account->id()]);
474     $this->assertFalse($user_storage->load($account->id()), 'User is not found in the database.');
475
476     // Confirm that user's content has been deleted.
477     $node_storage->resetCache([$node->id()]);
478     $this->assertFalse($node_storage->load($node->id()), 'Node of the user has been deleted.');
479     $this->assertFalse(node_revision_load($revision), 'Node revision of the user has been deleted.');
480     $node_storage->resetCache([$revision_node->id()]);
481     $this->assertTrue($node_storage->load($revision_node->id()), "Current revision of the user's node was not deleted.");
482     \Drupal::entityManager()->getStorage('comment')->resetCache([$comment->id()]);
483     $this->assertFalse(Comment::load($comment->id()), 'Comment of the user has been deleted.');
484
485     // Confirm that the confirmation message made it through to the end user.
486     $this->assertRaw(t('%name has been deleted.', ['%name' => $account->getUsername()]), "Confirmation message displayed to user.");
487   }
488
489   /**
490    * Create an administrative user and delete another user.
491    */
492   public function testUserCancelByAdmin() {
493     $this->config('user.settings')->set('cancel_method', 'user_cancel_reassign')->save();
494
495     // Create a regular user.
496     $account = $this->drupalCreateUser([]);
497
498     // Create administrative user.
499     $admin_user = $this->drupalCreateUser(['administer users']);
500     $this->drupalLogin($admin_user);
501
502     // Delete regular user.
503     $this->drupalGet('user/' . $account->id() . '/edit');
504     $this->drupalPostForm(NULL, NULL, t('Cancel account'));
505     $this->assertRaw(t('Are you sure you want to cancel the account %name?', ['%name' => $account->getUsername()]), 'Confirmation form to cancel account displayed.');
506     $this->assertText(t('Select the method to cancel the account above.'), 'Allows to select account cancellation method.');
507
508     // Confirm deletion.
509     $this->drupalPostForm(NULL, NULL, t('Cancel account'));
510     $this->assertRaw(t('%name has been deleted.', ['%name' => $account->getUsername()]), 'User deleted.');
511     $this->assertFalse(User::load($account->id()), 'User is not found in the database.');
512   }
513
514   /**
515    * Tests deletion of a user account without an email address.
516    */
517   public function testUserWithoutEmailCancelByAdmin() {
518     $this->config('user.settings')->set('cancel_method', 'user_cancel_reassign')->save();
519
520     // Create a regular user.
521     $account = $this->drupalCreateUser([]);
522     // This user has no email address.
523     $account->mail = '';
524     $account->save();
525
526     // Create administrative user.
527     $admin_user = $this->drupalCreateUser(['administer users']);
528     $this->drupalLogin($admin_user);
529
530     // Delete regular user without email address.
531     $this->drupalGet('user/' . $account->id() . '/edit');
532     $this->drupalPostForm(NULL, NULL, t('Cancel account'));
533     $this->assertRaw(t('Are you sure you want to cancel the account %name?', ['%name' => $account->getUsername()]), 'Confirmation form to cancel account displayed.');
534     $this->assertText(t('Select the method to cancel the account above.'), 'Allows to select account cancellation method.');
535
536     // Confirm deletion.
537     $this->drupalPostForm(NULL, NULL, t('Cancel account'));
538     $this->assertRaw(t('%name has been deleted.', ['%name' => $account->getUsername()]), 'User deleted.');
539     $this->assertFalse(User::load($account->id()), 'User is not found in the database.');
540   }
541
542   /**
543    * Create an administrative user and mass-delete other users.
544    */
545   public function testMassUserCancelByAdmin() {
546     \Drupal::service('module_installer')->install(['views']);
547     \Drupal::service('router.builder')->rebuild();
548     $this->config('user.settings')->set('cancel_method', 'user_cancel_reassign')->save();
549     $user_storage = $this->container->get('entity.manager')->getStorage('user');
550     // Enable account cancellation notification.
551     $this->config('user.settings')->set('notify.status_canceled', TRUE)->save();
552
553     // Create administrative user.
554     $admin_user = $this->drupalCreateUser(['administer users']);
555     $this->drupalLogin($admin_user);
556
557     // Create some users.
558     $users = [];
559     for ($i = 0; $i < 3; $i++) {
560       $account = $this->drupalCreateUser([]);
561       $users[$account->id()] = $account;
562     }
563
564     // Cancel user accounts, including own one.
565     $edit = [];
566     $edit['action'] = 'user_cancel_user_action';
567     for ($i = 0; $i <= 4; $i++) {
568       $edit['user_bulk_form[' . $i . ']'] = TRUE;
569     }
570     $this->drupalPostForm('admin/people', $edit, t('Apply to selected items'));
571     $this->assertText(t('Are you sure you want to cancel these user accounts?'), 'Confirmation form to cancel accounts displayed.');
572     $this->assertText(t('When cancelling these accounts'), 'Allows to select account cancellation method.');
573     $this->assertText(t('Require email confirmation to cancel account'), 'Allows to send confirmation mail.');
574     $this->assertText(t('Notify user when account is canceled'), 'Allows to send notification mail.');
575
576     // Confirm deletion.
577     $this->drupalPostForm(NULL, NULL, t('Cancel accounts'));
578     $status = TRUE;
579     foreach ($users as $account) {
580       $status = $status && (strpos($this->getTextContent(), $account->getUsername() . ' has been deleted.') !== FALSE);
581       $user_storage->resetCache([$account->id()]);
582       $status = $status && !$user_storage->load($account->id());
583     }
584     $this->assertTrue($status, 'Users deleted and not found in the database.');
585
586     // Ensure that admin account was not cancelled.
587     $this->assertText(t('A confirmation request to cancel your account has been sent to your email address.'), 'Account cancellation request mailed message displayed.');
588     $admin_user = $user_storage->load($admin_user->id());
589     $this->assertTrue($admin_user->isActive(), 'Administrative user is found in the database and enabled.');
590
591     // Verify that uid 1's account was not cancelled.
592     $user_storage->resetCache([1]);
593     $user1 = $user_storage->load(1);
594     $this->assertTrue($user1->isActive(), 'User #1 still exists and is not blocked.');
595   }
596
597   /**
598    * Tests user cancel with node access.
599    */
600   public function testUserDeleteWithContentAndNodeAccess() {
601
602     \Drupal::service('module_installer')->install(['node_access_test']);
603     // Rebuild node access.
604     node_access_rebuild();
605
606     $account = $this->drupalCreateUser(['access content']);
607     $node = $this->drupalCreateNode(['type' => 'page', 'uid' => $account->id()]);
608     $account->delete();
609     $load2 = \Drupal::entityTypeManager()->getStorage('node')->load($node->id());
610     $this->assertTrue(empty($load2));
611   }
612
613 }