View source
<?php
namespace Drupal\Tests\comment\Functional;
use Drupal\comment\CommentManagerInterface;
use Drupal\comment\Entity\Comment;
use Drupal\user\RoleInterface;
class CommentStatisticsTest extends CommentTestBase {
protected $webUser2;
protected $defaultTheme = 'stark';
protected function setUp() : void {
parent::setUp();
$this->webUser2 = $this
->drupalCreateUser([
'post comments',
'create article content',
'edit own comments',
'post comments',
'skip comment approval',
'access comments',
'access content',
]);
}
public function testCommentNodeCommentStatistics() {
$node_storage = $this->container
->get('entity_type.manager')
->getStorage('node');
$this
->drupalLogin($this->adminUser);
$this
->setCommentPreview(DRUPAL_DISABLED);
$this
->setCommentForm(TRUE);
$this
->setCommentSubject(FALSE);
$this
->setCommentSettings('default_mode', CommentManagerInterface::COMMENT_MODE_THREADED, 'Comment paging changed.');
$this
->drupalLogout();
$node = $node_storage
->load($this->node
->id());
$this
->assertEquals($this->node
->getCreatedTime(), $node
->get('comment')->last_comment_timestamp, 'The initial value of node last_comment_timestamp is the node created date.');
$this
->assertNull($node
->get('comment')->last_comment_name, 'The initial value of node last_comment_name is NULL.');
$this
->assertEquals($this->webUser
->id(), $node
->get('comment')->last_comment_uid, 'The initial value of node last_comment_uid is the node uid.');
$this
->assertEquals(0, $node
->get('comment')->comment_count, 'The initial value of node comment_count is zero.');
$this
->drupalLogin($this->webUser2);
$comment_text = $this
->randomMachineName();
$this
->postComment($this->node, $comment_text);
$node_storage
->resetCache([
$this->node
->id(),
]);
$node = $node_storage
->load($this->node
->id());
$this
->assertSame('', $node
->get('comment')->last_comment_name, 'The value of node last_comment_name should be an empty string.');
$this
->assertEquals($this->webUser2
->id(), $node
->get('comment')->last_comment_uid, 'The value of node last_comment_uid is the comment #1 uid.');
$this
->assertEquals(1, $node
->get('comment')->comment_count, 'The value of node comment_count is 1.');
$this
->drupalLogin($this->adminUser);
user_role_change_permissions(RoleInterface::ANONYMOUS_ID, [
'access comments' => TRUE,
'post comments' => TRUE,
'skip comment approval' => FALSE,
]);
$this
->setCommentAnonymous('1');
$this
->drupalLogout();
$this
->drupalGet('comment/reply/node/' . $this->node
->id() . '/comment');
$anonymous_comment = $this
->postComment($this->node, $this
->randomMachineName(), '', TRUE);
$node_storage
->resetCache([
$this->node
->id(),
]);
$node = $node_storage
->load($this->node
->id());
$this
->assertSame('', $node
->get('comment')->last_comment_name, 'The value of node last_comment_name should be an empty string.');
$this
->assertEquals($this->webUser2
->id(), $node
->get('comment')->last_comment_uid, 'The value of node last_comment_uid is still the comment #1 uid.');
$this
->assertEquals(1, $node
->get('comment')->comment_count, 'The value of node comment_count is still 1.');
$this
->drupalLogin($this->adminUser);
user_role_change_permissions(RoleInterface::ANONYMOUS_ID, [
'access comments' => TRUE,
'post comments' => TRUE,
'skip comment approval' => TRUE,
]);
$this
->drupalLogout();
$this
->drupalGet('comment/reply/node/' . $this->node
->id() . '/comment');
$anonymous_comment = $this
->postComment($this->node, $this
->randomMachineName(), '', [
'name' => $this
->randomMachineName(),
]);
$comment_loaded = Comment::load($anonymous_comment
->id());
$node_storage
->resetCache([
$this->node
->id(),
]);
$node = $node_storage
->load($this->node
->id());
$this
->assertEquals($comment_loaded
->getAuthorName(), $node
->get('comment')->last_comment_name, 'The value of node last_comment_name is the name of the anonymous user.');
$this
->assertEquals(0, $node
->get('comment')->last_comment_uid, 'The value of node last_comment_uid is zero.');
$this
->assertEquals(2, $node
->get('comment')->comment_count, 'The value of node comment_count is 2.');
}
}