diff --git a/core/modules/comment/comment.module b/core/modules/comment/comment.module index 035461f7b30bb3a174a6a967ec2a0a4cbb646126..debc2d3e9ae5a197089b50f68cbf858284f386db 100644 --- a/core/modules/comment/comment.module +++ b/core/modules/comment/comment.module @@ -499,6 +499,7 @@ function comment_user_cancel($edit, $account, $method) { $comments = entity_load_multiple_by_properties('comment', array('uid' => $account->id())); foreach ($comments as $comment) { $comment->setOwnerId(0); + $comment->setAuthorName(\Drupal::config('user.settings')->get('anonymous')); $comment->save(); } break; diff --git a/core/modules/user/src/Tests/UserCancelTest.php b/core/modules/user/src/Tests/UserCancelTest.php index 1b9288721d6c608316d02fc0b242ee2118d31a28..92e7ce0d096d21c8f6f6888a359534a0cb134733 100644 --- a/core/modules/user/src/Tests/UserCancelTest.php +++ b/core/modules/user/src/Tests/UserCancelTest.php @@ -11,6 +11,7 @@ use Drupal\simpletest\WebTestBase; use Drupal\comment\CommentInterface; use Drupal\comment\Entity\Comment; +use Drupal\user\Entity\User; /** * Ensure that account cancellation methods work as expected. @@ -281,6 +282,8 @@ function testUserBlockUnpublish() { 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'); // Create a user. $account = $this->drupalCreateUser(array('cancel account')); @@ -291,6 +294,20 @@ function testUserAnonymize() { // 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())); @@ -316,6 +333,7 @@ function testUserAnonymize() { $this->assertFalse(user_load($account->id(), TRUE), '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.'); @@ -325,6 +343,12 @@ function testUserAnonymize() { $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->getUsername(), '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."); }