summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorNathaniel Catchpole2017-09-05 12:41:51 (GMT)
committerNathaniel Catchpole2017-09-05 12:42:31 (GMT)
commitff6eea71c569ab5af31ac930b80f120ae4d04461 (patch)
tree356102b94b2bf772fa63b407672ee71784b8bd1b
parentaaa668a1e15f1b814abd9c0f848974ffdce88c92 (diff)
Issue #2614504 by blackra, jibran, mohit_aghera, Yogesh Pawar, larowlan, lokapujya, Munavijayalakshmi, thpoul, andypost, xjm: Values of 'name' & 'email' fields should be NULL when comment has author (uid > 0)
-rw-r--r--core/modules/comment/src/Entity/Comment.php13
-rw-r--r--core/modules/comment/src/Tests/CommentTitleTest.php12
-rw-r--r--core/modules/comment/tests/src/Kernel/CommentItemTest.php49
-rw-r--r--core/modules/comment/tests/src/Kernel/Views/CommentUserNameTest.php9
-rw-r--r--core/modules/comment/tests/src/Unit/Entity/CommentLockTest.php8
5 files changed, 85 insertions, 6 deletions
diff --git a/core/modules/comment/src/Entity/Comment.php b/core/modules/comment/src/Entity/Comment.php
index aeaf90e..c2643fe 100644
--- a/core/modules/comment/src/Entity/Comment.php
+++ b/core/modules/comment/src/Entity/Comment.php
@@ -139,17 +139,20 @@ class Comment extends ContentEntityBase implements CommentInterface {
} while (!\Drupal::lock()->acquire($lock_name));
$this->threadLock = $lock_name;
}
- // We test the value with '===' because we need to modify anonymous
- // users as well.
- if ($this->getOwnerId() === \Drupal::currentUser()->id() && \Drupal::currentUser()->isAuthenticated()) {
- $this->setAuthorName(\Drupal::currentUser()->getUsername());
- }
$this->setThread($thread);
if (!$this->getHostname()) {
// Ensure a client host from the current request.
$this->setHostname(\Drupal::request()->getClientIP());
}
}
+ // The entity fields for name and mail have no meaning if the user is not
+ // Anonymous. Set them to NULL to make it clearer that they are not used.
+ // For anonymous users see \Drupal\comment\CommentForm::form() for mail,
+ // and \Drupal\comment\CommentForm::buildEntity() for name setting.
+ if (!$this->getOwner()->isAnonymous()) {
+ $this->set('name', NULL);
+ $this->set('mail', NULL);
+ }
}
/**
diff --git a/core/modules/comment/src/Tests/CommentTitleTest.php b/core/modules/comment/src/Tests/CommentTitleTest.php
index 30d3935..1d24ad8 100644
--- a/core/modules/comment/src/Tests/CommentTitleTest.php
+++ b/core/modules/comment/src/Tests/CommentTitleTest.php
@@ -29,6 +29,12 @@ class CommentTitleTest extends CommentTestBase {
$subject_text = $this->randomMachineName();
$comment_text = $this->randomMachineName();
$comment = $this->postComment($this->node, $comment_text, $subject_text, TRUE);
+
+ // The entity fields for name and mail have no meaning if the user is not
+ // Anonymous.
+ $this->assertNull($comment->name->value);
+ $this->assertNull($comment->mail->value);
+
// Confirm that the comment was created.
$regex = '/<a id="comment-' . $comment->id() . '"(.*?)';
$regex .= $comment->comment_body->value . '(.*?)';
@@ -55,6 +61,12 @@ class CommentTitleTest extends CommentTestBase {
$subject_text = $this->randomMachineName();
$comment_text = $this->randomMachineName();
$comment1 = $this->postComment($this->node, $comment_text, $subject_text, TRUE);
+
+ // The entity fields for name and mail have no meaning if the user is not
+ // Anonymous.
+ $this->assertNull($comment1->name->value);
+ $this->assertNull($comment1->mail->value);
+
// Confirm that the comment was created.
$this->assertTrue($this->commentExists($comment1), 'Comment #1. Comment found.');
// Tests that markup is created for comment with heading.
diff --git a/core/modules/comment/tests/src/Kernel/CommentItemTest.php b/core/modules/comment/tests/src/Kernel/CommentItemTest.php
index 7b9a263..1a7fc65 100644
--- a/core/modules/comment/tests/src/Kernel/CommentItemTest.php
+++ b/core/modules/comment/tests/src/Kernel/CommentItemTest.php
@@ -2,6 +2,7 @@
namespace Drupal\Tests\comment\Kernel;
+use Drupal\comment\Entity\Comment;
use Drupal\comment\Plugin\Field\FieldType\CommentItemInterface;
use Drupal\comment\Tests\CommentTestTrait;
use Drupal\Core\Field\FieldItemListInterface;
@@ -64,4 +65,52 @@ class CommentItemTest extends FieldKernelTestBase {
$this->assertEqual('status', $mainProperty);
}
+ /**
+ * Tests comment author name.
+ */
+ public function testCommentAuthorName() {
+ $this->installEntitySchema('comment');
+
+ // Create some comments.
+ $comment = Comment::create([
+ 'subject' => 'My comment title',
+ 'uid' => 1,
+ 'name' => 'entity-test',
+ 'mail' => 'entity@localhost',
+ 'entity_type' => 'entity_test',
+ 'comment_type' => 'entity_test',
+ 'status' => 1,
+ ]);
+ $comment->save();
+
+ // The entity fields for name and mail have no meaning if the user is not
+ // Anonymous.
+ $this->assertNull($comment->name->value);
+ $this->assertNull($comment->mail->value);
+
+ $comment_anonymous = Comment::create([
+ 'subject' => 'Anonymous comment title',
+ 'uid' => 0,
+ 'name' => 'barry',
+ 'mail' => 'test@example.com',
+ 'homepage' => 'https://example.com',
+ 'entity_type' => 'entity_test',
+ 'comment_type' => 'entity_test',
+ 'status' => 1,
+ ]);
+ $comment_anonymous->save();
+
+ // The entity fields for name and mail have retained their values when
+ // comment belongs to an anonymous user.
+ $this->assertNotNull($comment_anonymous->name->value);
+ $this->assertNotNull($comment_anonymous->mail->value);
+
+ $comment_anonymous->setOwnerId(1)
+ ->save();
+ // The entity fields for name and mail have no meaning if the user is not
+ // Anonymous.
+ $this->assertNull($comment_anonymous->name->value);
+ $this->assertNull($comment_anonymous->mail->value);
+ }
+
}
diff --git a/core/modules/comment/tests/src/Kernel/Views/CommentUserNameTest.php b/core/modules/comment/tests/src/Kernel/Views/CommentUserNameTest.php
index cad7b2b..e1b3c94 100644
--- a/core/modules/comment/tests/src/Kernel/Views/CommentUserNameTest.php
+++ b/core/modules/comment/tests/src/Kernel/Views/CommentUserNameTest.php
@@ -144,7 +144,14 @@ class CommentUserNameTest extends ViewsKernelTestBase {
$this->assertLink('My comment title');
$this->assertLink('Anonymous comment title');
- $this->assertLink($this->adminUser->label());
+ // Display plugin of the view is showing the name field. When comment
+ // belongs to an authenticated user the name field has no value.
+ $comment_author = $this->xpath('//div[contains(@class, :class)]/span[normalize-space(text())=""]', [
+ ':class' => 'views-field-subject',
+ ]);
+ $this->assertTrue(!empty($comment_author));
+ // When comment belongs to an anonymous user the name field has a value and
+ // it is rendered correctly.
$this->assertLink('barry (not verified)');
$account_switcher->switchTo(new AnonymousUserSession());
diff --git a/core/modules/comment/tests/src/Unit/Entity/CommentLockTest.php b/core/modules/comment/tests/src/Unit/Entity/CommentLockTest.php
index 9dbe6d3..1289a8e 100644
--- a/core/modules/comment/tests/src/Unit/Entity/CommentLockTest.php
+++ b/core/modules/comment/tests/src/Unit/Entity/CommentLockTest.php
@@ -69,6 +69,14 @@ class CommentLockTest extends UnitTestCase {
->method('getThread')
->will($this->returnValue(''));
+ $anon_user = $this->getMock('Drupal\Core\Session\AccountInterface');
+ $anon_user->expects($this->any())
+ ->method('isAnonymous')
+ ->will($this->returnValue(TRUE));
+ $comment->expects($this->any())
+ ->method('getOwner')
+ ->will($this->returnValue($anon_user));
+
$parent_entity = $this->getMock('\Drupal\Core\Entity\ContentEntityInterface');
$parent_entity->expects($this->atLeastOnce())
->method('getCacheTagsToInvalidate')