0d1ec3a0451c4264a2c8b06e4a56861649631c8e
[yaffs-website] / web / core / modules / comment / src / Tests / CommentStatisticsTest.php
1 <?php
2
3 namespace Drupal\comment\Tests;
4
5 use Drupal\comment\CommentManagerInterface;
6 use Drupal\comment\Entity\Comment;
7 use Drupal\user\RoleInterface;
8
9 /**
10  * Tests comment statistics on nodes.
11  *
12  * @group comment
13  */
14 class CommentStatisticsTest extends CommentTestBase {
15
16   /**
17    * A secondary user for posting comments.
18    *
19    * @var \Drupal\user\UserInterface
20    */
21   protected $webUser2;
22
23   protected function setUp() {
24     parent::setUp();
25
26     // Create a second user to post comments.
27     $this->webUser2 = $this->drupalCreateUser([
28       'post comments',
29       'create article content',
30       'edit own comments',
31       'post comments',
32       'skip comment approval',
33       'access comments',
34       'access content',
35     ]);
36   }
37
38   /**
39    * Tests the node comment statistics.
40    */
41   public function testCommentNodeCommentStatistics() {
42     $node_storage = $this->container->get('entity.manager')->getStorage('node');
43     // Set comments to have subject and preview disabled.
44     $this->drupalLogin($this->adminUser);
45     $this->setCommentPreview(DRUPAL_DISABLED);
46     $this->setCommentForm(TRUE);
47     $this->setCommentSubject(FALSE);
48     $this->setCommentSettings('default_mode', CommentManagerInterface::COMMENT_MODE_THREADED, 'Comment paging changed.');
49     $this->drupalLogout();
50
51     // Checks the initial values of node comment statistics with no comment.
52     $node = $node_storage->load($this->node->id());
53     $this->assertEqual($node->get('comment')->last_comment_timestamp, $this->node->getCreatedTime(), 'The initial value of node last_comment_timestamp is the node created date.');
54     $this->assertEqual($node->get('comment')->last_comment_name, NULL, 'The initial value of node last_comment_name is NULL.');
55     $this->assertEqual($node->get('comment')->last_comment_uid, $this->webUser->id(), 'The initial value of node last_comment_uid is the node uid.');
56     $this->assertEqual($node->get('comment')->comment_count, 0, 'The initial value of node comment_count is zero.');
57
58     // Post comment #1 as web_user2.
59     $this->drupalLogin($this->webUser2);
60     $comment_text = $this->randomMachineName();
61     $this->postComment($this->node, $comment_text);
62
63     // Checks the new values of node comment statistics with comment #1.
64     // The node cache needs to be reset before reload.
65     $node_storage->resetCache([$this->node->id()]);
66     $node = $node_storage->load($this->node->id());
67     $this->assertEqual($node->get('comment')->last_comment_name, NULL, 'The value of node last_comment_name is NULL.');
68     $this->assertEqual($node->get('comment')->last_comment_uid, $this->webUser2->id(), 'The value of node last_comment_uid is the comment #1 uid.');
69     $this->assertEqual($node->get('comment')->comment_count, 1, 'The value of node comment_count is 1.');
70
71     // Prepare for anonymous comment submission (comment approval enabled).
72     $this->drupalLogin($this->adminUser);
73     user_role_change_permissions(RoleInterface::ANONYMOUS_ID, [
74       'access comments' => TRUE,
75       'post comments' => TRUE,
76       'skip comment approval' => FALSE,
77     ]);
78     // Ensure that the poster can leave some contact info.
79     $this->setCommentAnonymous('1');
80     $this->drupalLogout();
81
82     // Post comment #2 as anonymous (comment approval enabled).
83     $this->drupalGet('comment/reply/node/' . $this->node->id() . '/comment');
84     $anonymous_comment = $this->postComment($this->node, $this->randomMachineName(), '', TRUE);
85
86     // Checks the new values of node comment statistics with comment #2 and
87     // ensure they haven't changed since the comment has not been moderated.
88     // The node needs to be reloaded with the cache reset.
89     $node_storage->resetCache([$this->node->id()]);
90     $node = $node_storage->load($this->node->id());
91     $this->assertEqual($node->get('comment')->last_comment_name, NULL, 'The value of node last_comment_name is still NULL.');
92     $this->assertEqual($node->get('comment')->last_comment_uid, $this->webUser2->id(), 'The value of node last_comment_uid is still the comment #1 uid.');
93     $this->assertEqual($node->get('comment')->comment_count, 1, 'The value of node comment_count is still 1.');
94
95     // Prepare for anonymous comment submission (no approval required).
96     $this->drupalLogin($this->adminUser);
97     user_role_change_permissions(RoleInterface::ANONYMOUS_ID, [
98       'access comments' => TRUE,
99       'post comments' => TRUE,
100       'skip comment approval' => TRUE,
101     ]);
102     $this->drupalLogout();
103
104     // Post comment #3 as anonymous.
105     $this->drupalGet('comment/reply/node/' . $this->node->id() . '/comment');
106     $anonymous_comment = $this->postComment($this->node, $this->randomMachineName(), '', ['name' => $this->randomMachineName()]);
107     $comment_loaded = Comment::load($anonymous_comment->id());
108
109     // Checks the new values of node comment statistics with comment #3.
110     // The node needs to be reloaded with the cache reset.
111     $node_storage->resetCache([$this->node->id()]);
112     $node = $node_storage->load($this->node->id());
113     $this->assertEqual($node->get('comment')->last_comment_name, $comment_loaded->getAuthorName(), 'The value of node last_comment_name is the name of the anonymous user.');
114     $this->assertEqual($node->get('comment')->last_comment_uid, 0, 'The value of node last_comment_uid is zero.');
115     $this->assertEqual($node->get('comment')->comment_count, 2, 'The value of node comment_count is 2.');
116   }
117
118 }