Skip to content
Snippets Groups Projects
Commit 70f49902 authored by catch's avatar catch
Browse files

Issue #3166561 by Matroskeen, ravi.shankar, andregp, mohit_aghera,...

Issue #3166561 by Matroskeen, ravi.shankar, andregp, mohit_aghera, golddragon007, ranjith_kumar_k_u: Comment being deleted instead of reassigned to Anonymous user

(cherry picked from commit 1b0253bf)
parent a79231f4
No related branches found
No related tags found
23 merge requests!8506Draft: Issue #3456536 by ibrahim tameme,!5646Issue #3350972 by nod_: [random test failure]...,!5600Issue #3350972 by nod_: [random test failure]...,!5343Issue #3305066 by quietone, Rename RedirectLeadingSlashesSubscriber,!3603#ISSUE 3346218 Add a different message on edit comment,!3555Issue #2473873: Views entity operations lack cacheability support, resulting in incorrect dropbuttons,!3494Issue #3327018 by Spokje, longwave, xjm, mondrake: Update PHPStan to 1.9.3 and...,!3410Issue #3340128: UserLoginForm::submitForm has some dead code,!3389Issue #3325184 by Spokje, andypost, xjm, smustgrave: $this->configFactory is...,!3381Issue #3332363: Refactor Claro's menus-and-lists stylesheet,!3307Issue #3326193: CKEditor 5 can grow past the viewport when there is a lot of content,!3236Issue #3332419: Refactor Claro's messages stylesheet,!3231Draft: Issue #3049525 by longwave, fougere, larowlan, kim.pepper, AaronBauman, Wim...,!3212Issue #3294003: Refactor Claro's entity-meta stylesheet,!3194Issue #3330981: Fix PHPStan L1 error "Relying on entity queries to check access by default is deprecated...",!3143Issue #3313342: [PHP 8.1] Deprecated function: strpos(): Passing null to parameter #1 LayoutBuilderUiCacheContext.php on line 28,!3024Issue #3307509: Empty option for views bulk form,!2972Issue #1845004: Replace custom password hashing library with PHP 5.5 password_hash(),!2719Issue #3110137: Remove Classy from core.,!2688Issue #3261452: [PP-1] Remove tracker module from core,!2437Issue #3238257 by hooroomoo, Wim Leers: Fragment link pointing to <textarea>...,!2296Issue #3100732: Allow specifying `meta` data on JSON:API objects,!1626Issue #3256642: Make life better for database drivers that extend another database driver
...@@ -460,8 +460,15 @@ function comment_user_cancel($edit, UserInterface $account, $method) { ...@@ -460,8 +460,15 @@ function comment_user_cancel($edit, UserInterface $account, $method) {
/** @var \Drupal\comment\CommentInterface[] $comments */ /** @var \Drupal\comment\CommentInterface[] $comments */
$comments = \Drupal::entityTypeManager()->getStorage('comment')->loadByProperties(['uid' => $account->id()]); $comments = \Drupal::entityTypeManager()->getStorage('comment')->loadByProperties(['uid' => $account->id()]);
foreach ($comments as $comment) { foreach ($comments as $comment) {
$comment->setOwnerId(0); $langcodes = array_keys($comment->getTranslationLanguages());
$comment->setAuthorName(\Drupal::config('user.settings')->get('anonymous')); // For efficiency manually save the original comment before applying any
// changes.
$comment->original = clone $comment;
foreach ($langcodes as $langcode) {
$comment_translated = $comment->getTranslation($langcode);
$comment_translated->setOwnerId(0);
$comment_translated->setAuthorName(\Drupal::config('user.settings')->get('anonymous'));
}
$comment->save(); $comment->save();
} }
break; break;
......
...@@ -5,6 +5,7 @@ ...@@ -5,6 +5,7 @@
use Drupal\comment\CommentInterface; use Drupal\comment\CommentInterface;
use Drupal\comment\Entity\Comment; use Drupal\comment\Entity\Comment;
use Drupal\comment\Tests\CommentTestTrait; use Drupal\comment\Tests\CommentTestTrait;
use Drupal\language\Entity\ConfigurableLanguage;
use Drupal\node\Entity\Node; use Drupal\node\Entity\Node;
use Drupal\node\Entity\NodeType; use Drupal\node\Entity\NodeType;
use Drupal\Tests\BrowserTestBase; use Drupal\Tests\BrowserTestBase;
...@@ -655,4 +656,81 @@ public function testUserDeleteWithContentAndNodeAccess() { ...@@ -655,4 +656,81 @@ public function testUserDeleteWithContentAndNodeAccess() {
$this->assertEmpty($load2); $this->assertEmpty($load2);
} }
/**
* Delete account and anonymize all content and it's translations.
*/
public function testUserAnonymizeTranslations() {
$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()->responseContains(t('%name has been deleted.', ['%name' => $account->getAccountName()]));
}
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment