annotate core/modules/user/tests/src/Functional/UserCancelTest.php @ 19:fa3358dc1485 tip

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