UserCancelTest.php 25.7 KB
Newer Older
1 2 3 4
<?php

/**
 * @file
5
 * Contains \Drupal\user\Tests\UserCancelTest.
6 7 8 9
 */

namespace Drupal\user\Tests;

10
use Drupal\comment\Tests\CommentTestTrait;
11
use Drupal\simpletest\WebTestBase;
12
use Drupal\comment\CommentInterface;
13
use Drupal\comment\Entity\Comment;
14
use Drupal\user\Entity\User;
15 16

/**
17 18 19
 * Ensure that account cancellation methods work as expected.
 *
 * @group user
20 21
 */
class UserCancelTest extends WebTestBase {
22

23 24
  use CommentTestTrait;

25 26 27 28 29
  /**
   * Modules to enable.
   *
   * @var array
   */
30
  public static $modules = array('node', 'comment');
31

32
  protected function setUp() {
33 34 35 36 37
    parent::setUp();

    $this->drupalCreateContentType(array('type' => 'page', 'name' => 'Basic page'));
  }

38 39 40 41
  /**
   * Attempt to cancel account without permission.
   */
  function testUserCancelWithoutPermission() {
42
    $node_storage = $this->container->get('entity.manager')->getStorage('node');
43
    $this->config('user.settings')->set('cancel_method', 'user_cancel_reassign')->save();
44
    $user_storage = $this->container->get('entity.manager')->getStorage('user');
45 46 47 48

    // Create a user.
    $account = $this->drupalCreateUser(array());
    $this->drupalLogin($account);
49 50 51
    // Load a real user object.
    $user_storage->resetCache(array($account->id()));
    $account = $user_storage->load($account->id());
52 53

    // Create a node.
54
    $node = $this->drupalCreateNode(array('uid' => $account->id()));
55 56

    // Attempt to cancel account.
57
    $this->drupalGet('user/' . $account->id() . '/edit');
58
    $this->assertNoRaw(t('Cancel account'), 'No cancel account button displayed.');
59 60

    // Attempt bogus account cancellation request confirmation.
61
    $timestamp = $account->getLastLoginTime();
62
    $this->drupalGet("user/" . $account->id() . "/cancel/confirm/$timestamp/" . user_pass_rehash($account, $timestamp));
63
    $this->assertResponse(403, 'Bogus cancelling request rejected.');
64 65
    $user_storage->resetCache(array($account->id()));
    $account = $user_storage->load($account->id());
66
    $this->assertTrue($account->isActive(), 'User account was not canceled.');
67 68

    // Confirm user's content has not been altered.
69 70
    $node_storage->resetCache(array($node->id()));
    $test_node = $node_storage->load($node->id());
71
    $this->assertTrue(($test_node->getOwnerId() == $account->id() && $test_node->isPublished()), 'Node of the user has not been altered.');
72 73
  }

74 75 76 77 78
  /**
   * Test ability to change the permission for canceling users.
   */
  public function testUserCancelChangePermission() {
    \Drupal::service('module_installer')->install(array('user_form_test'));
79
    \Drupal::service('router.builder')->rebuild();
80 81 82 83 84 85 86 87 88 89 90 91 92
    $this->config('user.settings')->set('cancel_method', 'user_cancel_reassign')->save();

    // Create a regular user.
    $account = $this->drupalCreateUser(array());

    $admin_user = $this->drupalCreateUser(array('cancel other accounts'));
    $this->drupalLogin($admin_user);

    // Delete regular user.
    $this->drupalPostForm('user_form_test_cancel/' . $account->id(), array(), t('Cancel account'));

    // Confirm deletion.
    $this->assertRaw(t('%name has been deleted.', array('%name' => $account->getUsername())), 'User deleted.');
93
    $this->assertFalse(User::load($account->id()), 'User is not found in the database.');
94 95
  }

96 97 98 99 100 101 102
  /**
   * Tests that user account for uid 1 cannot be cancelled.
   *
   * This should never be possible, or the site owner would become unable to
   * administer the site.
   */
  function testUserCancelUid1() {
103 104
    $user_storage = $this->container->get('entity.manager')->getStorage('user');

105
    \Drupal::service('module_installer')->install(array('views'));
106
    \Drupal::service('router.builder')->rebuild();
107 108 109 110
    // Update uid 1's name and password to we know it.
    $password = user_password();
    $account = array(
      'name' => 'user1',
111
      'pass' => $this->container->get('password')->hash(trim($password)),
112 113 114
    );
    // We cannot use $account->save() here, because this would result in the
    // password being hashed again.
115
    db_update('users_field_data')
116 117 118 119 120
      ->fields($account)
      ->condition('uid', 1)
      ->execute();

    // Reload and log in uid 1.
121 122
    $user_storage->resetCache(array(1));
    $user1 = $user_storage->load(1);
123 124 125
    $user1->pass_raw = $password;

    // Try to cancel uid 1's account with a different user.
126 127
    $admin_user = $this->drupalCreateUser(array('administer users'));
    $this->drupalLogin($admin_user);
128
    $edit = array(
129 130
      'action' => 'user_cancel_user_action',
      'user_bulk_form[0]' => TRUE,
131
    );
132
    $this->drupalPostForm('admin/people', $edit, t('Apply'));
133 134

    // Verify that uid 1's account was not cancelled.
135 136
    $user_storage->resetCache(array(1));
    $user1 = $user_storage->load(1);
137
    $this->assertTrue($user1->isActive(), 'User #1 still exists and is not blocked.');
138 139 140 141 142 143
  }

  /**
   * Attempt invalid account cancellations.
   */
  function testUserCancelInvalid() {
144
    $node_storage = $this->container->get('entity.manager')->getStorage('node');
145
    $this->config('user.settings')->set('cancel_method', 'user_cancel_reassign')->save();
146
    $user_storage = $this->container->get('entity.manager')->getStorage('user');
147 148 149 150

    // Create a user.
    $account = $this->drupalCreateUser(array('cancel account'));
    $this->drupalLogin($account);
151 152 153
    // Load a real user object.
    $user_storage->resetCache(array($account->id()));
    $account = $user_storage->load($account->id());
154 155

    // Create a node.
156
    $node = $this->drupalCreateNode(array('uid' => $account->id()));
157 158

    // Attempt to cancel account.
159
    $this->drupalPostForm('user/' . $account->id() . '/edit', NULL, t('Cancel account'));
160 161 162

    // Confirm account cancellation.
    $timestamp = time();
163
    $this->drupalPostForm(NULL, NULL, t('Cancel account'));
164
    $this->assertText(t('A confirmation request to cancel your account has been sent to your email address.'), 'Account cancellation request mailed message displayed.');
165 166 167

    // Attempt bogus account cancellation request confirmation.
    $bogus_timestamp = $timestamp + 60;
168
    $this->drupalGet("user/" . $account->id() . "/cancel/confirm/$bogus_timestamp/" . user_pass_rehash($account, $bogus_timestamp));
169
    $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.');
170 171
    $user_storage->resetCache(array($account->id()));
    $account = $user_storage->load($account->id());
172
    $this->assertTrue($account->isActive(), 'User account was not canceled.');
173 174 175

    // Attempt expired account cancellation request confirmation.
    $bogus_timestamp = $timestamp - 86400 - 60;
176
    $this->drupalGet("user/" . $account->id() . "/cancel/confirm/$bogus_timestamp/" . user_pass_rehash($account, $bogus_timestamp));
177
    $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.');
178 179
    $user_storage->resetCache(array($account->id()));
    $account = $user_storage->load($account->id());
180
    $this->assertTrue($account->isActive(), 'User account was not canceled.');
181 182

    // Confirm user's content has not been altered.
183 184
    $node_storage->resetCache(array($node->id()));
    $test_node = $node_storage->load($node->id());
185
    $this->assertTrue(($test_node->getOwnerId() == $account->id() && $test_node->isPublished()), 'Node of the user has not been altered.');
186 187 188 189 190 191
  }

  /**
   * Disable account and keep all content.
   */
  function testUserBlock() {
192
    $this->config('user.settings')->set('cancel_method', 'user_cancel_block')->save();
193
    $user_storage = $this->container->get('entity.manager')->getStorage('user');
194 195 196 197 198

    // Create a user.
    $web_user = $this->drupalCreateUser(array('cancel account'));
    $this->drupalLogin($web_user);

199 200 201
    // Load a real user object.
    $user_storage->resetCache(array($web_user->id()));
    $account = $user_storage->load($web_user->id());
202 203

    // Attempt to cancel account.
204
    $this->drupalGet('user/' . $account->id() . '/edit');
205
    $this->drupalPostForm(NULL, NULL, t('Cancel account'));
206
    $this->assertText(t('Are you sure you want to cancel your account?'), 'Confirmation form to cancel account displayed.');
207
    $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.');
208
    $this->assertNoText(t('Select the method to cancel the account above.'), 'Does not allow user to select account cancellation method.');
209 210 211 212

    // Confirm account cancellation.
    $timestamp = time();

213
    $this->drupalPostForm(NULL, NULL, t('Cancel account'));
214
    $this->assertText(t('A confirmation request to cancel your account has been sent to your email address.'), 'Account cancellation request mailed message displayed.');
215 216

    // Confirm account cancellation request.
217
    $this->drupalGet("user/" . $account->id() . "/cancel/confirm/$timestamp/" . user_pass_rehash($account, $timestamp));
218 219
    $user_storage->resetCache(array($account->id()));
    $account = $user_storage->load($account->id());
220
    $this->assertTrue($account->isBlocked(), 'User has been blocked.');
221

222
    // Confirm that the confirmation message made it through to the end user.
223
    $this->assertRaw(t('%name has been disabled.', array('%name' => $account->getUsername())), "Confirmation message displayed to user.");
224 225 226 227 228 229
  }

  /**
   * Disable account and unpublish all content.
   */
  function testUserBlockUnpublish() {
230
    $node_storage = $this->container->get('entity.manager')->getStorage('node');
231
    $this->config('user.settings')->set('cancel_method', 'user_cancel_block_unpublish')->save();
232
    // Create comment field on page.
233
    $this->addDefaultCommentField('node', 'page');
234
    $user_storage = $this->container->get('entity.manager')->getStorage('user');
235 236 237 238

    // Create a user.
    $account = $this->drupalCreateUser(array('cancel account'));
    $this->drupalLogin($account);
239 240 241
    // Load a real user object.
    $user_storage->resetCache(array($account->id()));
    $account = $user_storage->load($account->id());
242 243

    // Create a node with two revisions.
244
    $node = $this->drupalCreateNode(array('uid' => $account->id()));
245 246 247 248
    $settings = get_object_vars($node);
    $settings['revision'] = 1;
    $node = $this->drupalCreateNode($settings);

249
    // Add a comment to the page.
250 251
    $comment_subject = $this->randomMachineName(8);
    $comment_body = $this->randomMachineName(8);
252 253 254 255 256 257 258 259 260 261 262
    $comment = entity_create('comment', array(
      'subject' => $comment_subject,
      'comment_body' => $comment_body,
      'entity_id' => $node->id(),
      'entity_type' => 'node',
      'field_name' => 'comment',
      'status' => CommentInterface::PUBLISHED,
      'uid' => $account->id(),
    ));
    $comment->save();

263
    // Attempt to cancel account.
264
    $this->drupalGet('user/' . $account->id() . '/edit');
265
    $this->drupalPostForm(NULL, NULL, t('Cancel account'));
266 267
    $this->assertText(t('Are you sure you want to cancel your account?'), 'Confirmation form to cancel account displayed.');
    $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.');
268 269 270

    // Confirm account cancellation.
    $timestamp = time();
271
    $this->drupalPostForm(NULL, NULL, t('Cancel account'));
272
    $this->assertText(t('A confirmation request to cancel your account has been sent to your email address.'), 'Account cancellation request mailed message displayed.');
273 274

    // Confirm account cancellation request.
275
    $this->drupalGet("user/" . $account->id() . "/cancel/confirm/$timestamp/" . user_pass_rehash($account, $timestamp));
276 277
    $user_storage->resetCache(array($account->id()));
    $account = $user_storage->load($account->id());
278
    $this->assertTrue($account->isBlocked(), 'User has been blocked.');
279 280

    // Confirm user's content has been unpublished.
281 282
    $node_storage->resetCache(array($node->id()));
    $test_node = $node_storage->load($node->id());
283 284 285
    $this->assertFalse($test_node->isPublished(), 'Node of the user has been unpublished.');
    $test_node = node_revision_load($node->getRevisionId());
    $this->assertFalse($test_node->isPublished(), 'Node revision of the user has been unpublished.');
286

287 288 289 290 291
    $storage = \Drupal::entityManager()->getStorage('comment');
    $storage->resetCache(array($comment->id()));
    $comment = $storage->load($comment->id());
    $this->assertFalse($comment->isPublished(), 'Comment of the user has been unpublished.');

292
    // Confirm that the confirmation message made it through to the end user.
293
    $this->assertRaw(t('%name has been disabled.', array('%name' => $account->getUsername())), "Confirmation message displayed to user.");
294 295 296 297 298 299
  }

  /**
   * Delete account and anonymize all content.
   */
  function testUserAnonymize() {
300
    $node_storage = $this->container->get('entity.manager')->getStorage('node');
301
    $this->config('user.settings')->set('cancel_method', 'user_cancel_reassign')->save();
302 303
    // Create comment field on page.
    $this->addDefaultCommentField('node', 'page');
304
    $user_storage = $this->container->get('entity.manager')->getStorage('user');
305 306 307 308

    // Create a user.
    $account = $this->drupalCreateUser(array('cancel account'));
    $this->drupalLogin($account);
309 310 311
    // Load a real user object.
    $user_storage->resetCache(array($account->id()));
    $account = $user_storage->load($account->id());
312 313

    // Create a simple node.
314
    $node = $this->drupalCreateNode(array('uid' => $account->id()));
315

316 317 318 319 320 321 322 323 324 325 326 327 328 329
    // Add a comment to the page.
    $comment_subject = $this->randomMachineName(8);
    $comment_body = $this->randomMachineName(8);
    $comment = entity_create('comment', array(
      'subject' => $comment_subject,
      'comment_body' => $comment_body,
      'entity_id' => $node->id(),
      'entity_type' => 'node',
      'field_name' => 'comment',
      'status' => CommentInterface::PUBLISHED,
      'uid' => $account->id(),
    ));
    $comment->save();

330 331
    // Create a node with two revisions, the initial one belonging to the
    // cancelling user.
332
    $revision_node = $this->drupalCreateNode(array('uid' => $account->id()));
333
    $revision = $revision_node->getRevisionId();
334 335 336 337 338 339
    $settings = get_object_vars($revision_node);
    $settings['revision'] = 1;
    $settings['uid'] = 1; // Set new/current revision to someone else.
    $revision_node = $this->drupalCreateNode($settings);

    // Attempt to cancel account.
340
    $this->drupalGet('user/' . $account->id() . '/edit');
341
    $this->drupalPostForm(NULL, NULL, t('Cancel account'));
342
    $this->assertText(t('Are you sure you want to cancel your account?'), 'Confirmation form to cancel account displayed.');
343
    $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.', array('%anonymous-name' => $this->config('user.settings')->get('anonymous'))), 'Informs that all content will be attributed to anonymous account.');
344 345 346

    // Confirm account cancellation.
    $timestamp = time();
347
    $this->drupalPostForm(NULL, NULL, t('Cancel account'));
348
    $this->assertText(t('A confirmation request to cancel your account has been sent to your email address.'), 'Account cancellation request mailed message displayed.');
349 350

    // Confirm account cancellation request.
351
    $this->drupalGet("user/" . $account->id() . "/cancel/confirm/$timestamp/" . user_pass_rehash($account, $timestamp));
352 353
    $user_storage->resetCache(array($account->id()));
    $this->assertFalse($user_storage->load($account->id()), 'User is not found in the database.');
354 355

    // Confirm that user's content has been attributed to anonymous user.
356
    $anonymous_user = User::getAnonymousUser();
357 358
    $node_storage->resetCache(array($node->id()));
    $test_node = $node_storage->load($node->id());
359
    $this->assertTrue(($test_node->getOwnerId() == 0 && $test_node->isPublished()), 'Node of the user has been attributed to anonymous user.');
360
    $test_node = node_revision_load($revision, TRUE);
361
    $this->assertTrue(($test_node->getRevisionAuthor()->id() == 0 && $test_node->isPublished()), 'Node revision of the user has been attributed to anonymous user.');
362 363
    $node_storage->resetCache(array($revision_node->id()));
    $test_node = $node_storage->load($revision_node->id());
364
    $this->assertTrue(($test_node->getOwnerId() != 0 && $test_node->isPublished()), "Current revision of the user's node was not attributed to anonymous user.");
365

366 367 368 369
    $storage = \Drupal::entityManager()->getStorage('comment');
    $storage->resetCache(array($comment->id()));
    $test_comment = $storage->load($comment->id());
    $this->assertTrue(($test_comment->getOwnerId() == 0 && $test_comment->isPublished()), 'Comment of the user has been attributed to anonymous user.');
370
    $this->assertEqual($test_comment->getAuthorName(), $anonymous_user->getDisplayName(), 'Comment of the user has been attributed to anonymous user name.');
371

372
    // Confirm that the confirmation message made it through to the end user.
373
    $this->assertRaw(t('%name has been deleted.', array('%name' => $account->getUsername())), "Confirmation message displayed to user.");
374 375 376 377 378 379
  }

  /**
   * Delete account and remove all content.
   */
  function testUserDelete() {
380
    $node_storage = $this->container->get('entity.manager')->getStorage('node');
381
    $this->config('user.settings')->set('cancel_method', 'user_cancel_delete')->save();
382
    \Drupal::service('module_installer')->install(array('comment'));
383
    $this->resetAll();
384
    $this->addDefaultCommentField('node', 'page');
385
    $user_storage = $this->container->get('entity.manager')->getStorage('user');
386 387 388 389

    // Create a user.
    $account = $this->drupalCreateUser(array('cancel account', 'post comments', 'skip comment approval'));
    $this->drupalLogin($account);
390 391 392
    // Load a real user object.
    $user_storage->resetCache(array($account->id()));
    $account = $user_storage->load($account->id());
393 394

    // Create a simple node.
395
    $node = $this->drupalCreateNode(array('uid' => $account->id()));
396 397 398

    // Create comment.
    $edit = array();
399 400
    $edit['subject[0][value]'] = $this->randomMachineName(8);
    $edit['comment_body[0][value]'] = $this->randomMachineName(16);
401

402
    $this->drupalPostForm('comment/reply/node/' . $node->id() . '/comment', $edit, t('Preview'));
403
    $this->drupalPostForm(NULL, array(), t('Save'));
404
    $this->assertText(t('Your comment has been posted.'));
405
    $comments = entity_load_multiple_by_properties('comment', array('subject' => $edit['subject[0][value]']));
406
    $comment = reset($comments);
407
    $this->assertTrue($comment->id(), 'Comment found.');
408 409 410

    // Create a node with two revisions, the initial one belonging to the
    // cancelling user.
411
    $revision_node = $this->drupalCreateNode(array('uid' => $account->id()));
412
    $revision = $revision_node->getRevisionId();
413 414 415 416 417 418
    $settings = get_object_vars($revision_node);
    $settings['revision'] = 1;
    $settings['uid'] = 1; // Set new/current revision to someone else.
    $revision_node = $this->drupalCreateNode($settings);

    // Attempt to cancel account.
419
    $this->drupalGet('user/' . $account->id() . '/edit');
420
    $this->drupalPostForm(NULL, NULL, t('Cancel account'));
421 422
    $this->assertText(t('Are you sure you want to cancel your account?'), 'Confirmation form to cancel account displayed.');
    $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.');
423 424 425

    // Confirm account cancellation.
    $timestamp = time();
426
    $this->drupalPostForm(NULL, NULL, t('Cancel account'));
427
    $this->assertText(t('A confirmation request to cancel your account has been sent to your email address.'), 'Account cancellation request mailed message displayed.');
428 429

    // Confirm account cancellation request.
430
    $this->drupalGet("user/" . $account->id() . "/cancel/confirm/$timestamp/" . user_pass_rehash($account, $timestamp));
431 432
    $user_storage->resetCache(array($account->id()));
    $this->assertFalse($user_storage->load($account->id()), 'User is not found in the database.');
433 434

    // Confirm that user's content has been deleted.
435 436
    $node_storage->resetCache(array($node->id()));
    $this->assertFalse($node_storage->load($node->id()), 'Node of the user has been deleted.');
437
    $this->assertFalse(node_revision_load($revision), 'Node revision of the user has been deleted.');
438 439
    $node_storage->resetCache(array($revision_node->id()));
    $this->assertTrue($node_storage->load($revision_node->id()), "Current revision of the user's node was not deleted.");
440 441
    \Drupal::entityManager()->getStorage('comment')->resetCache(array($comment->id()));
    $this->assertFalse(Comment::load($comment->id()), 'Comment of the user has been deleted.');
442

443
    // Confirm that the confirmation message made it through to the end user.
444
    $this->assertRaw(t('%name has been deleted.', array('%name' => $account->getUsername())), "Confirmation message displayed to user.");
445 446 447 448 449 450
  }

  /**
   * Create an administrative user and delete another user.
   */
  function testUserCancelByAdmin() {
451
    $this->config('user.settings')->set('cancel_method', 'user_cancel_reassign')->save();
452 453 454 455 456 457 458 459 460

    // Create a regular user.
    $account = $this->drupalCreateUser(array());

    // Create administrative user.
    $admin_user = $this->drupalCreateUser(array('administer users'));
    $this->drupalLogin($admin_user);

    // Delete regular user.
461
    $this->drupalGet('user/' . $account->id() . '/edit');
462
    $this->drupalPostForm(NULL, NULL, t('Cancel account'));
463
    $this->assertRaw(t('Are you sure you want to cancel the account %name?', array('%name' => $account->getUsername())), 'Confirmation form to cancel account displayed.');
464
    $this->assertText(t('Select the method to cancel the account above.'), 'Allows to select account cancellation method.');
465 466

    // Confirm deletion.
467
    $this->drupalPostForm(NULL, NULL, t('Cancel account'));
468
    $this->assertRaw(t('%name has been deleted.', array('%name' => $account->getUsername())), 'User deleted.');
469
    $this->assertFalse(User::load($account->id()), 'User is not found in the database.');
470 471 472
  }

  /**
473
   * Tests deletion of a user account without an email address.
474 475
   */
  function testUserWithoutEmailCancelByAdmin() {
476
    $this->config('user.settings')->set('cancel_method', 'user_cancel_reassign')->save();
477 478 479

    // Create a regular user.
    $account = $this->drupalCreateUser(array());
480
    // This user has no email address.
481 482 483 484 485 486 487
    $account->mail = '';
    $account->save();

    // Create administrative user.
    $admin_user = $this->drupalCreateUser(array('administer users'));
    $this->drupalLogin($admin_user);

488
    // Delete regular user without email address.
489
    $this->drupalGet('user/' . $account->id() . '/edit');
490
    $this->drupalPostForm(NULL, NULL, t('Cancel account'));
491
    $this->assertRaw(t('Are you sure you want to cancel the account %name?', array('%name' => $account->getUsername())), 'Confirmation form to cancel account displayed.');
492
    $this->assertText(t('Select the method to cancel the account above.'), 'Allows to select account cancellation method.');
493 494

    // Confirm deletion.
495
    $this->drupalPostForm(NULL, NULL, t('Cancel account'));
496
    $this->assertRaw(t('%name has been deleted.', array('%name' => $account->getUsername())), 'User deleted.');
497
    $this->assertFalse(User::load($account->id()), 'User is not found in the database.');
498 499 500 501 502 503
  }

  /**
   * Create an administrative user and mass-delete other users.
   */
  function testMassUserCancelByAdmin() {
504
    \Drupal::service('module_installer')->install(array('views'));
505
    \Drupal::service('router.builder')->rebuild();
506
    $this->config('user.settings')->set('cancel_method', 'user_cancel_reassign')->save();
507
    $user_storage = $this->container->get('entity.manager')->getStorage('user');
508
    // Enable account cancellation notification.
509
    $this->config('user.settings')->set('notify.status_canceled', TRUE)->save();
510 511 512 513 514 515 516 517 518

    // Create administrative user.
    $admin_user = $this->drupalCreateUser(array('administer users'));
    $this->drupalLogin($admin_user);

    // Create some users.
    $users = array();
    for ($i = 0; $i < 3; $i++) {
      $account = $this->drupalCreateUser(array());
519
      $users[$account->id()] = $account;
520 521 522 523
    }

    // Cancel user accounts, including own one.
    $edit = array();
524 525 526
    $edit['action'] = 'user_cancel_user_action';
    for ($i = 0; $i <= 4; $i++) {
      $edit['user_bulk_form[' . $i . ']'] = TRUE;
527
    }
528
    $this->drupalPostForm('admin/people', $edit, t('Apply'));
529 530
    $this->assertText(t('Are you sure you want to cancel these user accounts?'), 'Confirmation form to cancel accounts displayed.');
    $this->assertText(t('When cancelling these accounts'), 'Allows to select account cancellation method.');
531 532
    $this->assertText(t('Require email confirmation to cancel account'), 'Allows to send confirmation mail.');
    $this->assertText(t('Notify user when account is canceled'), 'Allows to send notification mail.');
533 534

    // Confirm deletion.
535
    $this->drupalPostForm(NULL, NULL, t('Cancel accounts'));
536 537
    $status = TRUE;
    foreach ($users as $account) {
538
      $status = $status && (strpos($this->content,  $account->getUsername() . '</em> has been deleted.') !== FALSE);
539 540
      $user_storage->resetCache(array($account->id()));
      $status = $status && !$user_storage->load($account->id());
541
    }
542
    $this->assertTrue($status, 'Users deleted and not found in the database.');
543 544

    // Ensure that admin account was not cancelled.
545
    $this->assertText(t('A confirmation request to cancel your account has been sent to your email address.'), 'Account cancellation request mailed message displayed.');
546
    $admin_user = $user_storage->load($admin_user->id());
547
    $this->assertTrue($admin_user->isActive(), 'Administrative user is found in the database and enabled.');
548 549

    // Verify that uid 1's account was not cancelled.
550 551
    $user_storage->resetCache(array(1));
    $user1 = $user_storage->load(1);
552
    $this->assertTrue($user1->isActive(), 'User #1 still exists and is not blocked.');
553
  }
554

555
}