You are here

function UserCancelTest::testUserAnonymize in Zircon Profile 8.0

Same name and namespace in other branches
  1. 8 core/modules/user/src/Tests/UserCancelTest.php \Drupal\user\Tests\UserCancelTest::testUserAnonymize()

Delete account and anonymize all content.

File

core/modules/user/src/Tests/UserCancelTest.php, line 299
Contains \Drupal\user\Tests\UserCancelTest.

Class

UserCancelTest
Ensure that account cancellation methods work as expected.

Namespace

Drupal\user\Tests

Code

function testUserAnonymize() {
  $node_storage = $this->container
    ->get('entity.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.manager')
    ->getStorage('user');

  // Create a user.
  $account = $this
    ->drupalCreateUser(array(
    'cancel account',
  ));
  $this
    ->drupalLogin($account);

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

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

  // Add a comment to the page.
  $comment_subject = $this
    ->randomMachineName(8);
  $comment_body = $this
    ->randomMachineName(8);
  $comment = entity_create('comment', array(
    '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(array(
    'uid' => $account
      ->id(),
  ));
  $revision = $revision_node
    ->getRevisionId();
  $settings = get_object_vars($revision_node);
  $settings['revision'] = 1;
  $settings['uid'] = 1;

  // Set new/current revision to someone else.
  $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
    ->assertRaw(t('Your account will be removed and all account information deleted. All of your content will be assigned to the %anonymous-name user.', array(
    '%anonymous-name' => $this
      ->config('user.settings')
      ->get('anonymous'),
  )), 'Informs that all content will be attributed to anonymous account.');

  // 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(array(
    $account
      ->id(),
  ));
  $this
    ->assertFalse($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(array(
    $node
      ->id(),
  ));
  $test_node = $node_storage
    ->load($node
    ->id());
  $this
    ->assertTrue($test_node
    ->getOwnerId() == 0 && $test_node
    ->isPublished(), 'Node of the user has been attributed to anonymous user.');
  $test_node = node_revision_load($revision, TRUE);
  $this
    ->assertTrue($test_node
    ->getRevisionAuthor()
    ->id() == 0 && $test_node
    ->isPublished(), 'Node revision of the user has been attributed to anonymous user.');
  $node_storage
    ->resetCache(array(
    $revision_node
      ->id(),
  ));
  $test_node = $node_storage
    ->load($revision_node
    ->id());
  $this
    ->assertTrue($test_node
    ->getOwnerId() != 0 && $test_node
    ->isPublished(), "Current revision of the user's node was not attributed to anonymous user.");
  $storage = \Drupal::entityManager()
    ->getStorage('comment');
  $storage
    ->resetCache(array(
    $comment
      ->id(),
  ));
  $test_comment = $storage
    ->load($comment
    ->id());
  $this
    ->assertTrue($test_comment
    ->getOwnerId() == 0 && $test_comment
    ->isPublished(), 'Comment of the user has been attributed to anonymous user.');
  $this
    ->assertEqual($test_comment
    ->getAuthorName(), $anonymous_user
    ->getDisplayName(), 'Comment of the user has been attributed to anonymous user name.');

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