function UserCancelTest::testUserAnonymizeTranslations

Same name in other branches
  1. 9 core/modules/user/tests/src/Functional/UserCancelTest.php \Drupal\Tests\user\Functional\UserCancelTest::testUserAnonymizeTranslations()
  2. 10 core/modules/user/tests/src/Functional/UserCancelTest.php \Drupal\Tests\user\Functional\UserCancelTest::testUserAnonymizeTranslations()

Delete account and anonymize all content and it's translations.

File

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

Class

UserCancelTest
Ensure that account cancellation methods work as expected.

Namespace

Drupal\Tests\user\Functional

Code

public function testUserAnonymizeTranslations() : void {
    $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');
    \Drupal::service('module_installer')->install([
        'language',
        'locale',
    ]);
    \Drupal::service('router.builder')->rebuildIfNeeded();
    ConfigurableLanguage::createFromLangcode('ur')->save();
    // Rebuild the container to update the default language container variable.
    $this->rebuildContainer();
    $account = $this->drupalCreateUser([
        'cancel account',
    ]);
    $this->drupalLogin($account);
    $user_storage->resetCache([
        $account->id(),
    ]);
    $account = $user_storage->load($account->id());
    $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();
    $comment->addTranslation('ur', [
        'subject' => 'ur ' . $comment->label(),
        'status' => CommentInterface::PUBLISHED,
    ])
        ->save();
    // Attempt to cancel account.
    $this->drupalGet('user/' . $account->id() . '/cancel');
    $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([], 'Confirm');
    $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();
    $storage = \Drupal::entityTypeManager()->getStorage('comment');
    $storage->resetCache([
        $comment->id(),
    ]);
    $test_comment = $storage->load($comment->id());
    $this->assertEquals(0, $test_comment->getOwnerId());
    $this->assertTrue($test_comment->isPublished(), 'Comment of the user has been attributed to anonymous user.');
    $this->assertEquals($anonymous_user->getDisplayName(), $test_comment->getAuthorName());
    $comment_translation = $test_comment->getTranslation('ur');
    $this->assertEquals(0, $comment_translation->getOwnerId());
    $this->assertTrue($comment_translation->isPublished(), 'Comment translation of the user has been attributed to anonymous user.');
    $this->assertEquals($anonymous_user->getDisplayName(), $comment_translation->getAuthorName());
    // Confirm that the confirmation message made it through to the end user.
    $this->assertSession()
        ->pageTextContains($account->getAccountName() . ' has been deleted.');
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.