public function UserCancelTest::testUserAnonymize in Drupal 8
Same name and namespace in other branches
- 9 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 286
Class
- UserCancelTest
- Ensure that account cancellation methods work as expected.
Namespace
Drupal\Tests\user\FunctionalCode
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
->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.', [
'%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([
$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
->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
->getRevisionUser()
->id() == 0 && $test_node
->isPublished(), 'Node revision of the user has been attributed to anonymous user.');
$node_storage
->resetCache([
$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::entityTypeManager()
->getStorage('comment');
$storage
->resetCache([
$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.', [
'%name' => $account
->getAccountName(),
]), "Confirmation message displayed to user.");
}