Commit ff6eea71 authored by catch's avatar catch

Issue #2614504 by blackra, jibran, mohit_aghera, Yogesh Pawar, larowlan,...

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)
parent aaa668a1
......@@ -139,17 +139,20 @@ public function preSave(EntityStorageInterface $storage) {
} 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);
}
}
/**
......
......@@ -29,6 +29,12 @@ public function testCommentEmptyTitles() {
$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 @@ public function testCommentPopulatedTitles() {
$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.
......
......@@ -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 @@ public function testCommentItem() {
$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);
}
}
......@@ -144,7 +144,14 @@ public function testUsername() {
$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());
......
......@@ -69,6 +69,14 @@ public function testLocks() {
->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')
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment