annotate core/modules/user/tests/src/Functional/UserCancelTest.php @ 0:4c8ae668cc8c

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