You are here

public function UserCancelTest::testUserDelete in Drupal 9

Same name and namespace in other branches
  1. 8 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 465

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
    ->drupalGet('comment/reply/node/' . $node
    ->id() . '/comment');
  $this
    ->submitForm($edit, 'Preview');
  $this
    ->submitForm([], 'Save');
  $this
    ->assertSession()
    ->pageTextContains('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
    ->submitForm([], 'Cancel account');
  $this
    ->assertSession()
    ->pageTextContains('Are you sure you want to cancel your account?');
  $this
    ->assertSession()
    ->pageTextContains('Your account will be removed and all account information deleted. All of your content will also be deleted.');

  // Confirm account cancellation.
  $timestamp = time();
  $this
    ->submitForm([], 'Cancel account');
  $this
    ->assertSession()
    ->pageTextContains('A confirmation request to cancel your account has been sent to your email address.');

  // 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 there's only one session in the database. The user will be logged
  // out and their session migrated.
  // @see _user_cancel_session_regenerate()
  $this
    ->assertSame(1, (int) \Drupal::database()
    ->select('sessions', 's')
    ->countQuery()
    ->execute()
    ->fetchField());

  // 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
    ->assertSession()
    ->pageTextContains("{$account->getAccountName()} has been deleted.");
}