UserCancelTest.php 21.5 KB
Newer Older
1 2 3 4 5 6 7 8 9 10
<?php

/**
 * @file
 * Definition of Drupal\user\Tests\UserCancelTest.
 */

namespace Drupal\user\Tests;

use Drupal\simpletest\WebTestBase;
11
use Drupal\comment\CommentInterface;
12 13 14 15 16

/**
 * Test cancelling a user.
 */
class UserCancelTest extends WebTestBase {
17

18 19 20 21 22
  /**
   * Modules to enable.
   *
   * @var array
   */
23
  public static $modules = array('node', 'comment');
24

25 26 27 28 29 30 31 32
  public static function getInfo() {
    return array(
      'name' => 'Cancel account',
      'description' => 'Ensure that account cancellation methods work as expected.',
      'group' => 'User',
    );
  }

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

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

39 40 41 42
  /**
   * Attempt to cancel account without permission.
   */
  function testUserCancelWithoutPermission() {
43
    \Drupal::config('user.settings')->set('cancel_method', 'user_cancel_reassign')->save();
44 45 46 47 48

    // Create a user.
    $account = $this->drupalCreateUser(array());
    $this->drupalLogin($account);
    // Load real user object.
49
    $account = user_load($account->id(), TRUE);
50 51

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

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

    // Attempt bogus account cancellation request confirmation.
59 60
    $timestamp = $account->getLastLoginTime();
    $this->drupalGet("user/" . $account->id() . "/cancel/confirm/$timestamp/" . user_pass_rehash($account->getPassword(), $timestamp, $account->getLastLoginTime()));
61
    $this->assertResponse(403, 'Bogus cancelling request rejected.');
62
    $account = user_load($account->id());
63
    $this->assertTrue($account->isActive(), 'User account was not canceled.');
64 65

    // Confirm user's content has not been altered.
66
    $test_node = node_load($node->id(), TRUE);
67
    $this->assertTrue(($test_node->getOwnerId() == $account->id() && $test_node->isPublished()), 'Node of the user has not been altered.');
68 69 70 71 72 73 74 75 76
  }

  /**
   * 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() {
77
    \Drupal::moduleHandler()->install(array('views'));
78 79 80 81
    // Update uid 1's name and password to we know it.
    $password = user_password();
    $account = array(
      'name' => 'user1',
82
      'pass' => $this->container->get('password')->hash(trim($password)),
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98
    );
    // We cannot use $account->save() here, because this would result in the
    // password being hashed again.
    db_update('users')
      ->fields($account)
      ->condition('uid', 1)
      ->execute();

    // Reload and log in uid 1.
    $user1 = user_load(1, TRUE);
    $user1->pass_raw = $password;

    // Try to cancel uid 1's account with a different user.
    $this->admin_user = $this->drupalCreateUser(array('administer users'));
    $this->drupalLogin($this->admin_user);
    $edit = array(
99 100
      'action' => 'user_cancel_user_action',
      'user_bulk_form[0]' => TRUE,
101
    );
102
    $this->drupalPostForm('admin/people', $edit, t('Apply'));
103 104 105

    // Verify that uid 1's account was not cancelled.
    $user1 = user_load(1, TRUE);
106
    $this->assertTrue($user1->isActive(), 'User #1 still exists and is not blocked.');
107 108 109 110 111 112
  }

  /**
   * Attempt invalid account cancellations.
   */
  function testUserCancelInvalid() {
113
    \Drupal::config('user.settings')->set('cancel_method', 'user_cancel_reassign')->save();
114 115 116 117 118

    // Create a user.
    $account = $this->drupalCreateUser(array('cancel account'));
    $this->drupalLogin($account);
    // Load real user object.
119
    $account = user_load($account->id(), TRUE);
120 121

    // Create a node.
122
    $node = $this->drupalCreateNode(array('uid' => $account->id()));
123 124

    // Attempt to cancel account.
125
    $this->drupalPostForm('user/' . $account->id() . '/edit', NULL, t('Cancel account'));
126 127 128

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

    // Attempt bogus account cancellation request confirmation.
    $bogus_timestamp = $timestamp + 60;
134
    $this->drupalGet("user/" . $account->id() . "/cancel/confirm/$bogus_timestamp/" . user_pass_rehash($account->getPassword(), $bogus_timestamp, $account->getLastLoginTime()));
135
    $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.');
136
    $account = user_load($account->id());
137
    $this->assertTrue($account->isActive(), 'User account was not canceled.');
138 139 140

    // Attempt expired account cancellation request confirmation.
    $bogus_timestamp = $timestamp - 86400 - 60;
141
    $this->drupalGet("user/" . $account->id() . "/cancel/confirm/$bogus_timestamp/" . user_pass_rehash($account->getPassword(), $bogus_timestamp, $account->getLastLoginTime()));
142
    $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.');
143
    $account = user_load($account->id(), TRUE);
144
    $this->assertTrue($account->isActive(), 'User account was not canceled.');
145 146

    // Confirm user's content has not been altered.
147
    $test_node = node_load($node->id(), TRUE);
148
    $this->assertTrue(($test_node->getOwnerId() == $account->id() && $test_node->isPublished()), 'Node of the user has not been altered.');
149 150 151 152 153 154
  }

  /**
   * Disable account and keep all content.
   */
  function testUserBlock() {
155
    \Drupal::config('user.settings')->set('cancel_method', 'user_cancel_block')->save();
156 157 158 159 160 161

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

    // Load real user object.
162
    $account = user_load($web_user->id(), TRUE);
163 164

    // Attempt to cancel account.
165
    $this->drupalGet('user/' . $account->id() . '/edit');
166
    $this->drupalPostForm(NULL, NULL, t('Cancel account'));
167 168 169
    $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 remain attributed to your user name.'), 'Informs that all content will be remain as is.');
    $this->assertNoText(t('Select the method to cancel the account above.'), 'Does not allow user to select account cancellation method.');
170 171 172 173

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

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

    // Confirm account cancellation request.
178
    $this->drupalGet("user/" . $account->id() . "/cancel/confirm/$timestamp/" . user_pass_rehash($account->getPassword(), $timestamp, $account->getLastLoginTime()));
179
    $account = user_load($account->id(), TRUE);
180
    $this->assertTrue($account->isBlocked(), 'User has been blocked.');
181

182
    // Confirm that the confirmation message made it through to the end user.
183
    $this->assertRaw(t('%name has been disabled.', array('%name' => $account->getUsername())), "Confirmation message displayed to user.");
184 185 186 187 188 189
  }

  /**
   * Disable account and unpublish all content.
   */
  function testUserBlockUnpublish() {
190
    \Drupal::config('user.settings')->set('cancel_method', 'user_cancel_block_unpublish')->save();
191 192
    // Create comment field on page.
    \Drupal::service('comment.manager')->addDefaultField('node', 'page');
193 194 195 196 197

    // Create a user.
    $account = $this->drupalCreateUser(array('cancel account'));
    $this->drupalLogin($account);
    // Load real user object.
198
    $account = user_load($account->id(), TRUE);
199 200

    // Create a node with two revisions.
201
    $node = $this->drupalCreateNode(array('uid' => $account->id()));
202 203 204 205
    $settings = get_object_vars($node);
    $settings['revision'] = 1;
    $node = $this->drupalCreateNode($settings);

206 207 208 209 210 211 212 213 214 215 216 217 218 219
    // Add a comment to the page.
    $comment_subject = $this->randomName(8);
    $comment_body = $this->randomName(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();

220
    // Attempt to cancel account.
221
    $this->drupalGet('user/' . $account->id() . '/edit');
222
    $this->drupalPostForm(NULL, NULL, t('Cancel account'));
223 224
    $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.');
225 226 227

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

    // Confirm account cancellation request.
232
    $this->drupalGet("user/" . $account->id() . "/cancel/confirm/$timestamp/" . user_pass_rehash($account->getPassword(), $timestamp, $account->getLastLoginTime()));
233
    $account = user_load($account->id(), TRUE);
234
    $this->assertTrue($account->isBlocked(), 'User has been blocked.');
235 236

    // Confirm user's content has been unpublished.
237
    $test_node = node_load($node->id(), TRUE);
238 239 240
    $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.');
241

242 243 244 245 246
    $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.');

247
    // Confirm that the confirmation message made it through to the end user.
248
    $this->assertRaw(t('%name has been disabled.', array('%name' => $account->getUsername())), "Confirmation message displayed to user.");
249 250 251 252 253 254
  }

  /**
   * Delete account and anonymize all content.
   */
  function testUserAnonymize() {
255
    \Drupal::config('user.settings')->set('cancel_method', 'user_cancel_reassign')->save();
256 257 258 259 260

    // Create a user.
    $account = $this->drupalCreateUser(array('cancel account'));
    $this->drupalLogin($account);
    // Load real user object.
261
    $account = user_load($account->id(), TRUE);
262 263

    // Create a simple node.
264
    $node = $this->drupalCreateNode(array('uid' => $account->id()));
265 266 267

    // Create a node with two revisions, the initial one belonging to the
    // cancelling user.
268
    $revision_node = $this->drupalCreateNode(array('uid' => $account->id()));
269
    $revision = $revision_node->getRevisionId();
270 271 272 273 274 275
    $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.
276
    $this->drupalGet('user/' . $account->id() . '/edit');
277
    $this->drupalPostForm(NULL, NULL, t('Cancel account'));
278
    $this->assertText(t('Are you sure you want to cancel your account?'), 'Confirmation form to cancel account displayed.');
279
    $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' => \Drupal::config('user.settings')->get('anonymous'))), 'Informs that all content will be attributed to anonymous account.');
280 281 282

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

    // Confirm account cancellation request.
287
    $this->drupalGet("user/" . $account->id() . "/cancel/confirm/$timestamp/" . user_pass_rehash($account->getPassword(), $timestamp, $account->getLastLoginTime()));
288
    $this->assertFalse(user_load($account->id(), TRUE), 'User is not found in the database.');
289 290

    // Confirm that user's content has been attributed to anonymous user.
291
    $test_node = node_load($node->id(), TRUE);
292
    $this->assertTrue(($test_node->getOwnerId() == 0 && $test_node->isPublished()), 'Node of the user has been attributed to anonymous user.');
293
    $test_node = node_revision_load($revision, TRUE);
294
    $this->assertTrue(($test_node->getRevisionAuthor()->id() == 0 && $test_node->isPublished()), 'Node revision of the user has been attributed to anonymous user.');
295
    $test_node = node_load($revision_node->id(), TRUE);
296
    $this->assertTrue(($test_node->getOwnerId() != 0 && $test_node->isPublished()), "Current revision of the user's node was not attributed to anonymous user.");
297

298
    // Confirm that the confirmation message made it through to the end user.
299
    $this->assertRaw(t('%name has been deleted.', array('%name' => $account->getUsername())), "Confirmation message displayed to user.");
300 301 302 303 304 305
  }

  /**
   * Delete account and remove all content.
   */
  function testUserDelete() {
306
    \Drupal::config('user.settings')->set('cancel_method', 'user_cancel_delete')->save();
307
    \Drupal::moduleHandler()->install(array('comment'));
308
    $this->resetAll();
309
    $this->container->get('comment.manager')->addDefaultField('node', 'page');
310 311 312 313 314

    // Create a user.
    $account = $this->drupalCreateUser(array('cancel account', 'post comments', 'skip comment approval'));
    $this->drupalLogin($account);
    // Load real user object.
315
    $account = user_load($account->id(), TRUE);
316 317

    // Create a simple node.
318
    $node = $this->drupalCreateNode(array('uid' => $account->id()));
319 320 321 322

    // Create comment.
    $edit = array();
    $edit['subject'] = $this->randomName(8);
323
    $edit['comment_body[0][value]'] = $this->randomName(16);
324

325
    $this->drupalPostForm('comment/reply/node/' . $node->id() . '/comment', $edit, t('Preview'));
326
    $this->drupalPostForm(NULL, array(), t('Save'));
327
    $this->assertText(t('Your comment has been posted.'));
328
    $comments = entity_load_multiple_by_properties('comment', array('subject' => $edit['subject']));
329
    $comment = reset($comments);
330
    $this->assertTrue($comment->id(), 'Comment found.');
331 332 333

    // Create a node with two revisions, the initial one belonging to the
    // cancelling user.
334
    $revision_node = $this->drupalCreateNode(array('uid' => $account->id()));
335
    $revision = $revision_node->getRevisionId();
336 337 338 339 340 341
    $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.
342
    $this->drupalGet('user/' . $account->id() . '/edit');
343
    $this->drupalPostForm(NULL, NULL, t('Cancel account'));
344 345
    $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.');
346 347 348

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

    // Confirm account cancellation request.
353
    $this->drupalGet("user/" . $account->id() . "/cancel/confirm/$timestamp/" . user_pass_rehash($account->getPassword(), $timestamp, $account->getLastLoginTime()));
354
    $this->assertFalse(user_load($account->id(), TRUE), 'User is not found in the database.');
355 356

    // Confirm that user's content has been deleted.
357
    $this->assertFalse(node_load($node->id(), TRUE), 'Node of the user has been deleted.');
358
    $this->assertFalse(node_revision_load($revision), 'Node revision of the user has been deleted.');
359
    $this->assertTrue(node_load($revision_node->id(), TRUE), "Current revision of the user's node was not deleted.");
360
    $this->assertFalse(comment_load($comment->id(), TRUE), 'Comment of the user has been deleted.');
361

362
    // Confirm that the confirmation message made it through to the end user.
363
    $this->assertRaw(t('%name has been deleted.', array('%name' => $account->getUsername())), "Confirmation message displayed to user.");
364 365 366 367 368 369
  }

  /**
   * Create an administrative user and delete another user.
   */
  function testUserCancelByAdmin() {
370
    \Drupal::config('user.settings')->set('cancel_method', 'user_cancel_reassign')->save();
371 372 373 374 375 376 377 378 379

    // 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.
380
    $this->drupalGet('user/' . $account->id() . '/edit');
381
    $this->drupalPostForm(NULL, NULL, t('Cancel account'));
382
    $this->assertRaw(t('Are you sure you want to cancel the account %name?', array('%name' => $account->getUsername())), 'Confirmation form to cancel account displayed.');
383
    $this->assertText(t('Select the method to cancel the account above.'), 'Allows to select account cancellation method.');
384 385

    // Confirm deletion.
386
    $this->drupalPostForm(NULL, NULL, t('Cancel account'));
387
    $this->assertRaw(t('%name has been deleted.', array('%name' => $account->getUsername())), 'User deleted.');
388
    $this->assertFalse(user_load($account->id()), 'User is not found in the database.');
389 390 391
  }

  /**
392
   * Tests deletion of a user account without an email address.
393 394
   */
  function testUserWithoutEmailCancelByAdmin() {
395
    \Drupal::config('user.settings')->set('cancel_method', 'user_cancel_reassign')->save();
396 397 398

    // Create a regular user.
    $account = $this->drupalCreateUser(array());
399
    // This user has no email address.
400 401 402 403 404 405 406
    $account->mail = '';
    $account->save();

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

407
    // Delete regular user without email address.
408
    $this->drupalGet('user/' . $account->id() . '/edit');
409
    $this->drupalPostForm(NULL, NULL, t('Cancel account'));
410
    $this->assertRaw(t('Are you sure you want to cancel the account %name?', array('%name' => $account->getUsername())), 'Confirmation form to cancel account displayed.');
411
    $this->assertText(t('Select the method to cancel the account above.'), 'Allows to select account cancellation method.');
412 413

    // Confirm deletion.
414
    $this->drupalPostForm(NULL, NULL, t('Cancel account'));
415
    $this->assertRaw(t('%name has been deleted.', array('%name' => $account->getUsername())), 'User deleted.');
416
    $this->assertFalse(user_load($account->id()), 'User is not found in the database.');
417 418 419 420 421 422
  }

  /**
   * Create an administrative user and mass-delete other users.
   */
  function testMassUserCancelByAdmin() {
423
    \Drupal::moduleHandler()->install(array('views'));
424
    \Drupal::config('user.settings')->set('cancel_method', 'user_cancel_reassign')->save();
425
    // Enable account cancellation notification.
426
    \Drupal::config('user.settings')->set('notify.status_canceled', TRUE)->save();
427 428 429 430 431 432 433 434 435

    // 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());
436
      $users[$account->id()] = $account;
437 438 439 440
    }

    // Cancel user accounts, including own one.
    $edit = array();
441 442 443
    $edit['action'] = 'user_cancel_user_action';
    for ($i = 0; $i <= 4; $i++) {
      $edit['user_bulk_form[' . $i . ']'] = TRUE;
444
    }
445
    $this->drupalPostForm('admin/people', $edit, t('Apply'));
446 447
    $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.');
448
    $this->assertText(t('Require email confirmation to cancel account.'), 'Allows to send confirmation mail.');
449
    $this->assertText(t('Notify user when account is canceled.'), 'Allows to send notification mail.');
450 451

    // Confirm deletion.
452
    $this->drupalPostForm(NULL, NULL, t('Cancel accounts'));
453 454
    $status = TRUE;
    foreach ($users as $account) {
455
      $status = $status && (strpos($this->content, t('%name has been deleted.', array('%name' => $account->getUsername()))) !== FALSE);
456
      $status = $status && !user_load($account->id(), TRUE);
457
    }
458
    $this->assertTrue($status, 'Users deleted and not found in the database.');
459 460

    // Ensure that admin account was not cancelled.
461
    $this->assertText(t('A confirmation request to cancel your account has been sent to your email address.'), 'Account cancellation request mailed message displayed.');
462
    $admin_user = user_load($admin_user->id());
463
    $this->assertTrue($admin_user->isActive(), 'Administrative user is found in the database and enabled.');
464 465 466

    // Verify that uid 1's account was not cancelled.
    $user1 = user_load(1, TRUE);
467
    $this->assertTrue($user1->isActive(), 'User #1 still exists and is not blocked.');
468 469
  }
}