9889cd65bb116c41c4f61bc0d90c7997729930b3
[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     // Set new/current revision to someone else.
332     $settings['uid'] = 1;
333     $revision_node = $this->drupalCreateNode($settings);
334
335     // Attempt to cancel account.
336     $this->drupalGet('user/' . $account->id() . '/edit');
337     $this->drupalPostForm(NULL, NULL, t('Cancel account'));
338     $this->assertText(t('Are you sure you want to cancel your account?'), 'Confirmation form to cancel account displayed.');
339     $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.');
340
341     // Confirm account cancellation.
342     $timestamp = time();
343     $this->drupalPostForm(NULL, NULL, t('Cancel account'));
344     $this->assertText(t('A confirmation request to cancel your account has been sent to your email address.'), 'Account cancellation request mailed message displayed.');
345
346     // Confirm account cancellation request.
347     $this->drupalGet("user/" . $account->id() . "/cancel/confirm/$timestamp/" . user_pass_rehash($account, $timestamp));
348     $user_storage->resetCache([$account->id()]);
349     $this->assertFalse($user_storage->load($account->id()), 'User is not found in the database.');
350
351     // Confirm that user's content has been attributed to anonymous user.
352     $anonymous_user = User::getAnonymousUser();
353     $node_storage->resetCache([$node->id()]);
354     $test_node = $node_storage->load($node->id());
355     $this->assertTrue(($test_node->getOwnerId() == 0 && $test_node->isPublished()), 'Node of the user has been attributed to anonymous user.');
356     $test_node = node_revision_load($revision, TRUE);
357     $this->assertTrue(($test_node->getRevisionUser()->id() == 0 && $test_node->isPublished()), 'Node revision of the user has been attributed to anonymous user.');
358     $node_storage->resetCache([$revision_node->id()]);
359     $test_node = $node_storage->load($revision_node->id());
360     $this->assertTrue(($test_node->getOwnerId() != 0 && $test_node->isPublished()), "Current revision of the user's node was not attributed to anonymous user.");
361
362     $storage = \Drupal::entityManager()->getStorage('comment');
363     $storage->resetCache([$comment->id()]);
364     $test_comment = $storage->load($comment->id());
365     $this->assertTrue(($test_comment->getOwnerId() == 0 && $test_comment->isPublished()), 'Comment of the user has been attributed to anonymous user.');
366     $this->assertEqual($test_comment->getAuthorName(), $anonymous_user->getDisplayName(), 'Comment of the user has been attributed to anonymous user name.');
367
368     // Confirm that the confirmation message made it through to the end user.
369     $this->assertRaw(t('%name has been deleted.', ['%name' => $account->getUsername()]), "Confirmation message displayed to user.");
370   }
371
372   /**
373    * Delete account and anonymize all content using a batch process.
374    */
375   public function testUserAnonymizeBatch() {
376     $node_storage = $this->container->get('entity.manager')->getStorage('node');
377     $this->config('user.settings')->set('cancel_method', 'user_cancel_reassign')->save();
378     $user_storage = $this->container->get('entity.manager')->getStorage('user');
379
380     // Create a user.
381     $account = $this->drupalCreateUser(['cancel account']);
382     $this->drupalLogin($account);
383     // Load a real user object.
384     $user_storage->resetCache([$account->id()]);
385     $account = $user_storage->load($account->id());
386
387     // Create 11 nodes in order to trigger batch processing in
388     // node_mass_update().
389     $nodes = [];
390     for ($i = 0; $i < 11; $i++) {
391       $node = $this->drupalCreateNode(['uid' => $account->id()]);
392       $nodes[$node->id()] = $node;
393     }
394
395     // Attempt to cancel account.
396     $this->drupalGet('user/' . $account->id() . '/edit');
397     $this->drupalPostForm(NULL, NULL, t('Cancel account'));
398     $this->assertText(t('Are you sure you want to cancel your account?'), 'Confirmation form to cancel account displayed.');
399     $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.');
400
401     // Confirm account cancellation.
402     $timestamp = time();
403     $this->drupalPostForm(NULL, NULL, t('Cancel account'));
404     $this->assertText(t('A confirmation request to cancel your account has been sent to your email address.'), 'Account cancellation request mailed message displayed.');
405
406     // Confirm account cancellation request.
407     $this->drupalGet("user/" . $account->id() . "/cancel/confirm/$timestamp/" . user_pass_rehash($account, $timestamp));
408     $user_storage->resetCache([$account->id()]);
409     $this->assertFalse($user_storage->load($account->id()), 'User is not found in the database.');
410
411     // Confirm that user's content has been attributed to anonymous user.
412     $node_storage->resetCache(array_keys($nodes));
413     $test_nodes = $node_storage->loadMultiple(array_keys($nodes));
414     foreach ($test_nodes as $test_node) {
415       $this->assertTrue(($test_node->getOwnerId() == 0 && $test_node->isPublished()), 'Node ' . $test_node->id() . ' of the user has been attributed to anonymous user.');
416     }
417   }
418
419   /**
420    * Delete account and remove all content.
421    */
422   public function testUserDelete() {
423     $node_storage = $this->container->get('entity.manager')->getStorage('node');
424     $this->config('user.settings')->set('cancel_method', 'user_cancel_delete')->save();
425     \Drupal::service('module_installer')->install(['comment']);
426     $this->resetAll();
427     $this->addDefaultCommentField('node', 'page');
428     $user_storage = $this->container->get('entity.manager')->getStorage('user');
429
430     // Create a user.
431     $account = $this->drupalCreateUser(['cancel account', 'post comments', 'skip comment approval']);
432     $this->drupalLogin($account);
433     // Load a real user object.
434     $user_storage->resetCache([$account->id()]);
435     $account = $user_storage->load($account->id());
436
437     // Create a simple node.
438     $node = $this->drupalCreateNode(['uid' => $account->id()]);
439
440     // Create comment.
441     $edit = [];
442     $edit['subject[0][value]'] = $this->randomMachineName(8);
443     $edit['comment_body[0][value]'] = $this->randomMachineName(16);
444
445     $this->drupalPostForm('comment/reply/node/' . $node->id() . '/comment', $edit, t('Preview'));
446     $this->drupalPostForm(NULL, [], t('Save'));
447     $this->assertText(t('Your comment has been posted.'));
448     $comments = entity_load_multiple_by_properties('comment', ['subject' => $edit['subject[0][value]']]);
449     $comment = reset($comments);
450     $this->assertTrue($comment->id(), 'Comment found.');
451
452     // Create a node with two revisions, the initial one belonging to the
453     // cancelling user.
454     $revision_node = $this->drupalCreateNode(['uid' => $account->id()]);
455     $revision = $revision_node->getRevisionId();
456     $settings = get_object_vars($revision_node);
457     $settings['revision'] = 1;
458     // Set new/current revision to someone else.
459     $settings['uid'] = 1;
460     $revision_node = $this->drupalCreateNode($settings);
461
462     // Attempt to cancel account.
463     $this->drupalGet('user/' . $account->id() . '/edit');
464     $this->drupalPostForm(NULL, NULL, t('Cancel account'));
465     $this->assertText(t('Are you sure you want to cancel your account?'), 'Confirmation form to cancel account displayed.');
466     $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.');
467
468     // Confirm account cancellation.
469     $timestamp = time();
470     $this->drupalPostForm(NULL, NULL, t('Cancel account'));
471     $this->assertText(t('A confirmation request to cancel your account has been sent to your email address.'), 'Account cancellation request mailed message displayed.');
472
473     // Confirm account cancellation request.
474     $this->drupalGet("user/" . $account->id() . "/cancel/confirm/$timestamp/" . user_pass_rehash($account, $timestamp));
475     $user_storage->resetCache([$account->id()]);
476     $this->assertFalse($user_storage->load($account->id()), 'User is not found in the database.');
477
478     // Confirm that user's content has been deleted.
479     $node_storage->resetCache([$node->id()]);
480     $this->assertFalse($node_storage->load($node->id()), 'Node of the user has been deleted.');
481     $this->assertFalse(node_revision_load($revision), 'Node revision of the user has been deleted.');
482     $node_storage->resetCache([$revision_node->id()]);
483     $this->assertTrue($node_storage->load($revision_node->id()), "Current revision of the user's node was not deleted.");
484     \Drupal::entityManager()->getStorage('comment')->resetCache([$comment->id()]);
485     $this->assertFalse(Comment::load($comment->id()), 'Comment of the user has been deleted.');
486
487     // Confirm that the confirmation message made it through to the end user.
488     $this->assertRaw(t('%name has been deleted.', ['%name' => $account->getUsername()]), "Confirmation message displayed to user.");
489   }
490
491   /**
492    * Create an administrative user and delete another user.
493    */
494   public function testUserCancelByAdmin() {
495     $this->config('user.settings')->set('cancel_method', 'user_cancel_reassign')->save();
496
497     // Create a regular user.
498     $account = $this->drupalCreateUser([]);
499
500     // Create administrative user.
501     $admin_user = $this->drupalCreateUser(['administer users']);
502     $this->drupalLogin($admin_user);
503
504     // Delete regular user.
505     $this->drupalGet('user/' . $account->id() . '/edit');
506     $this->drupalPostForm(NULL, NULL, t('Cancel account'));
507     $this->assertRaw(t('Are you sure you want to cancel the account %name?', ['%name' => $account->getUsername()]), 'Confirmation form to cancel account displayed.');
508     $this->assertText(t('Select the method to cancel the account above.'), 'Allows to select account cancellation method.');
509
510     // Confirm deletion.
511     $this->drupalPostForm(NULL, NULL, t('Cancel account'));
512     $this->assertRaw(t('%name has been deleted.', ['%name' => $account->getUsername()]), 'User deleted.');
513     $this->assertFalse(User::load($account->id()), 'User is not found in the database.');
514   }
515
516   /**
517    * Tests deletion of a user account without an email address.
518    */
519   public function testUserWithoutEmailCancelByAdmin() {
520     $this->config('user.settings')->set('cancel_method', 'user_cancel_reassign')->save();
521
522     // Create a regular user.
523     $account = $this->drupalCreateUser([]);
524     // This user has no email address.
525     $account->mail = '';
526     $account->save();
527
528     // Create administrative user.
529     $admin_user = $this->drupalCreateUser(['administer users']);
530     $this->drupalLogin($admin_user);
531
532     // Delete regular user without email address.
533     $this->drupalGet('user/' . $account->id() . '/edit');
534     $this->drupalPostForm(NULL, NULL, t('Cancel account'));
535     $this->assertRaw(t('Are you sure you want to cancel the account %name?', ['%name' => $account->getUsername()]), 'Confirmation form to cancel account displayed.');
536     $this->assertText(t('Select the method to cancel the account above.'), 'Allows to select account cancellation method.');
537
538     // Confirm deletion.
539     $this->drupalPostForm(NULL, NULL, t('Cancel account'));
540     $this->assertRaw(t('%name has been deleted.', ['%name' => $account->getUsername()]), 'User deleted.');
541     $this->assertFalse(User::load($account->id()), 'User is not found in the database.');
542   }
543
544   /**
545    * Create an administrative user and mass-delete other users.
546    */
547   public function testMassUserCancelByAdmin() {
548     \Drupal::service('module_installer')->install(['views']);
549     \Drupal::service('router.builder')->rebuild();
550     $this->config('user.settings')->set('cancel_method', 'user_cancel_reassign')->save();
551     $user_storage = $this->container->get('entity.manager')->getStorage('user');
552     // Enable account cancellation notification.
553     $this->config('user.settings')->set('notify.status_canceled', TRUE)->save();
554
555     // Create administrative user.
556     $admin_user = $this->drupalCreateUser(['administer users']);
557     $this->drupalLogin($admin_user);
558
559     // Create some users.
560     $users = [];
561     for ($i = 0; $i < 3; $i++) {
562       $account = $this->drupalCreateUser([]);
563       $users[$account->id()] = $account;
564     }
565
566     // Cancel user accounts, including own one.
567     $edit = [];
568     $edit['action'] = 'user_cancel_user_action';
569     for ($i = 0; $i <= 4; $i++) {
570       $edit['user_bulk_form[' . $i . ']'] = TRUE;
571     }
572     $this->drupalPostForm('admin/people', $edit, t('Apply to selected items'));
573     $this->assertText(t('Are you sure you want to cancel these user accounts?'), 'Confirmation form to cancel accounts displayed.');
574     $this->assertText(t('When cancelling these accounts'), 'Allows to select account cancellation method.');
575     $this->assertText(t('Require email confirmation to cancel account'), 'Allows to send confirmation mail.');
576     $this->assertText(t('Notify user when account is canceled'), 'Allows to send notification mail.');
577
578     // Confirm deletion.
579     $this->drupalPostForm(NULL, NULL, t('Cancel accounts'));
580     $status = TRUE;
581     foreach ($users as $account) {
582       $status = $status && (strpos($this->getTextContent(), $account->getUsername() . ' has been deleted.') !== FALSE);
583       $user_storage->resetCache([$account->id()]);
584       $status = $status && !$user_storage->load($account->id());
585     }
586     $this->assertTrue($status, 'Users deleted and not found in the database.');
587
588     // Ensure that admin account was not cancelled.
589     $this->assertText(t('A confirmation request to cancel your account has been sent to your email address.'), 'Account cancellation request mailed message displayed.');
590     $admin_user = $user_storage->load($admin_user->id());
591     $this->assertTrue($admin_user->isActive(), 'Administrative user is found in the database and enabled.');
592
593     // Verify that uid 1's account was not cancelled.
594     $user_storage->resetCache([1]);
595     $user1 = $user_storage->load(1);
596     $this->assertTrue($user1->isActive(), 'User #1 still exists and is not blocked.');
597   }
598
599   /**
600    * Tests user cancel with node access.
601    */
602   public function testUserDeleteWithContentAndNodeAccess() {
603
604     \Drupal::service('module_installer')->install(['node_access_test']);
605     // Rebuild node access.
606     node_access_rebuild();
607
608     $account = $this->drupalCreateUser(['access content']);
609     $node = $this->drupalCreateNode(['type' => 'page', 'uid' => $account->id()]);
610     $account->delete();
611     $load2 = \Drupal::entityTypeManager()->getStorage('node')->load($node->id());
612     $this->assertTrue(empty($load2));
613   }
614
615 }