You are here

public function UserCancelTest::testUserAnonymize 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::testUserAnonymize()
  2. 10 core/modules/user/tests/src/Functional/UserCancelTest.php \Drupal\Tests\user\Functional\UserCancelTest::testUserAnonymize()

Delete account and anonymize all content.

File

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

Class

UserCancelTest
Ensure that account cancellation methods work as expected.

Namespace

Drupal\Tests\user\Functional

Code

public function testUserAnonymize() {
  $node_storage = $this->container
    ->get('entity_type.manager')
    ->getStorage('node');
  $this
    ->config('user.settings')
    ->set('cancel_method', 'user_cancel_reassign')
    ->save();

  // Create comment field on page.
  $this
    ->addDefaultCommentField('node', 'page');
  $user_storage = $this->container
    ->get('entity_type.manager')
    ->getStorage('user');

  // Create a user.
  $account = $this
    ->drupalCreateUser([
    'cancel account',
  ]);
  $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(),
  ]);

  // Add a comment to the page.
  $comment_subject = $this
    ->randomMachineName(8);
  $comment_body = $this
    ->randomMachineName(8);
  $comment = Comment::create([
    '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();

  // 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 be assigned to the {$this->config('user.settings')->get('anonymous')} user.");

  // 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 that user's content has been attributed to anonymous user.
  $anonymous_user = User::getAnonymousUser();
  $node_storage
    ->resetCache([
    $node
      ->id(),
  ]);
  $test_node = $node_storage
    ->load($node
    ->id());
  $this
    ->assertEquals(0, $test_node
    ->getOwnerId(), 'Node of the user has been attributed to anonymous user.');
  $this
    ->assertTrue($test_node
    ->isPublished());
  $test_node = node_revision_load($revision, TRUE);
  $this
    ->assertEquals(0, $test_node
    ->getRevisionUser()
    ->id(), 'Node revision of the user has been attributed to anonymous user.');
  $this
    ->assertTrue($test_node
    ->isPublished());
  $node_storage
    ->resetCache([
    $revision_node
      ->id(),
  ]);
  $test_node = $node_storage
    ->load($revision_node
    ->id());
  $this
    ->assertNotEquals(0, $test_node
    ->getOwnerId(), "Current revision of the user's node was not attributed to anonymous user.");
  $this
    ->assertTrue($test_node
    ->isPublished());
  $storage = \Drupal::entityTypeManager()
    ->getStorage('comment');
  $storage
    ->resetCache([
    $comment
      ->id(),
  ]);
  $test_comment = $storage
    ->load($comment
    ->id());
  $this
    ->assertEquals(0, $test_comment
    ->getOwnerId(), 'Comment of the user has been attributed to anonymous user.');
  $this
    ->assertTrue($test_comment
    ->isPublished());
  $this
    ->assertEquals($anonymous_user
    ->getDisplayName(), $test_comment
    ->getAuthorName(), 'Comment of the user has been attributed to anonymous user name.');

  // Confirm that the confirmation message made it through to the end user.
  $this
    ->assertSession()
    ->pageTextContains("{$account->getAccountName()} has been deleted.");
}