You are here

public function UserCancelTest::testUserDelete in Drupal 8

Same name and namespace in other branches
  1. 9 core/modules/user/tests/src/Functional/UserCancelTest.php \Drupal\Tests\user\Functional\UserCancelTest::testUserDelete()

Delete account and remove all content.

File

core/modules/user/tests/src/Functional/UserCancelTest.php, line 414

Class

UserCancelTest
Ensure that account cancellation methods work as expected.

Namespace

Drupal\Tests\user\Functional

Code

public function testUserDelete() {
  $node_storage = $this->container
    ->get('entity_type.manager')
    ->getStorage('node');
  $this
    ->config('user.settings')
    ->set('cancel_method', 'user_cancel_delete')
    ->save();
  \Drupal::service('module_installer')
    ->install([
    'comment',
  ]);
  $this
    ->resetAll();
  $this
    ->addDefaultCommentField('node', 'page');
  $user_storage = $this->container
    ->get('entity_type.manager')
    ->getStorage('user');

  // Create a user.
  $account = $this
    ->drupalCreateUser([
    'cancel account',
    'post comments',
    'skip comment approval',
  ]);
  $this
    ->drupalLogin($account);

  // Load a real user object.
  $user_storage
    ->resetCache([
    $account
      ->id(),
  ]);
  $account = $user_storage
    ->load($account
    ->id());

  // Create a simple node.
  $node = $this
    ->drupalCreateNode([
    'uid' => $account
      ->id(),
  ]);

  // Create comment.
  $edit = [];
  $edit['subject[0][value]'] = $this
    ->randomMachineName(8);
  $edit['comment_body[0][value]'] = $this
    ->randomMachineName(16);
  $this
    ->drupalPostForm('comment/reply/node/' . $node
    ->id() . '/comment', $edit, t('Preview'));
  $this
    ->drupalPostForm(NULL, [], t('Save'));
  $this
    ->assertText(t('Your comment has been posted.'));
  $comments = \Drupal::entityTypeManager()
    ->getStorage('comment')
    ->loadByProperties([
    'subject' => $edit['subject[0][value]'],
  ]);
  $comment = reset($comments);
  $this
    ->assertNotEmpty($comment
    ->id(), 'Comment found.');

  // Create a node with two revisions, the initial one belonging to the
  // cancelling user.
  $revision_node = $this
    ->drupalCreateNode([
    'uid' => $account
      ->id(),
  ]);
  $revision = $revision_node
    ->getRevisionId();
  $settings = get_object_vars($revision_node);
  $settings['revision'] = 1;

  // Set new/current revision to someone else.
  $settings['uid'] = 1;
  $revision_node = $this
    ->drupalCreateNode($settings);

  // Attempt to cancel account.
  $this
    ->drupalGet('user/' . $account
    ->id() . '/edit');
  $this
    ->drupalPostForm(NULL, NULL, t('Cancel account'));
  $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.');

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

  // Confirm account cancellation request.
  $this
    ->drupalGet("user/" . $account
    ->id() . "/cancel/confirm/{$timestamp}/" . user_pass_rehash($account, $timestamp));
  $user_storage
    ->resetCache([
    $account
      ->id(),
  ]);
  $this
    ->assertNull($user_storage
    ->load($account
    ->id()), 'User is not found in the database.');

  // Confirm that user's content has been deleted.
  $node_storage
    ->resetCache([
    $node
      ->id(),
  ]);
  $this
    ->assertNull($node_storage
    ->load($node
    ->id()), 'Node of the user has been deleted.');
  $this
    ->assertNull(node_revision_load($revision), 'Node revision of the user has been deleted.');
  $node_storage
    ->resetCache([
    $revision_node
      ->id(),
  ]);
  $this
    ->assertInstanceOf(Node::class, $node_storage
    ->load($revision_node
    ->id()));
  \Drupal::entityTypeManager()
    ->getStorage('comment')
    ->resetCache([
    $comment
      ->id(),
  ]);
  $this
    ->assertNull(Comment::load($comment
    ->id()), 'Comment of the user has been deleted.');

  // Confirm that the confirmation message made it through to the end user.
  $this
    ->assertRaw(t('%name has been deleted.', [
    '%name' => $account
      ->getAccountName(),
  ]), "Confirmation message displayed to user.");
}