Chris@0: webUser2 = $this->drupalCreateUser([ Chris@0: 'post comments', Chris@0: 'create article content', Chris@0: 'edit own comments', Chris@0: 'post comments', Chris@0: 'skip comment approval', Chris@0: 'access comments', Chris@0: 'access content', Chris@0: ]); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Tests the node comment statistics. Chris@0: */ Chris@0: public function testCommentNodeCommentStatistics() { Chris@0: $node_storage = $this->container->get('entity.manager')->getStorage('node'); Chris@0: // Set comments to have subject and preview disabled. Chris@0: $this->drupalLogin($this->adminUser); Chris@0: $this->setCommentPreview(DRUPAL_DISABLED); Chris@0: $this->setCommentForm(TRUE); Chris@0: $this->setCommentSubject(FALSE); Chris@0: $this->setCommentSettings('default_mode', CommentManagerInterface::COMMENT_MODE_THREADED, 'Comment paging changed.'); Chris@0: $this->drupalLogout(); Chris@0: Chris@0: // Checks the initial values of node comment statistics with no comment. Chris@0: $node = $node_storage->load($this->node->id()); Chris@0: $this->assertEqual($node->get('comment')->last_comment_timestamp, $this->node->getCreatedTime(), 'The initial value of node last_comment_timestamp is the node created date.'); Chris@0: $this->assertEqual($node->get('comment')->last_comment_name, NULL, 'The initial value of node last_comment_name is NULL.'); Chris@0: $this->assertEqual($node->get('comment')->last_comment_uid, $this->webUser->id(), 'The initial value of node last_comment_uid is the node uid.'); Chris@0: $this->assertEqual($node->get('comment')->comment_count, 0, 'The initial value of node comment_count is zero.'); Chris@0: Chris@0: // Post comment #1 as web_user2. Chris@0: $this->drupalLogin($this->webUser2); Chris@0: $comment_text = $this->randomMachineName(); Chris@0: $this->postComment($this->node, $comment_text); Chris@0: Chris@0: // Checks the new values of node comment statistics with comment #1. Chris@0: // The node cache needs to be reset before reload. Chris@0: $node_storage->resetCache([$this->node->id()]); Chris@0: $node = $node_storage->load($this->node->id()); Chris@0: $this->assertEqual($node->get('comment')->last_comment_name, NULL, 'The value of node last_comment_name is NULL.'); Chris@0: $this->assertEqual($node->get('comment')->last_comment_uid, $this->webUser2->id(), 'The value of node last_comment_uid is the comment #1 uid.'); Chris@0: $this->assertEqual($node->get('comment')->comment_count, 1, 'The value of node comment_count is 1.'); Chris@0: Chris@0: // Prepare for anonymous comment submission (comment approval enabled). Chris@0: $this->drupalLogin($this->adminUser); Chris@0: user_role_change_permissions(RoleInterface::ANONYMOUS_ID, [ Chris@0: 'access comments' => TRUE, Chris@0: 'post comments' => TRUE, Chris@0: 'skip comment approval' => FALSE, Chris@0: ]); Chris@0: // Ensure that the poster can leave some contact info. Chris@0: $this->setCommentAnonymous('1'); Chris@0: $this->drupalLogout(); Chris@0: Chris@0: // Post comment #2 as anonymous (comment approval enabled). Chris@0: $this->drupalGet('comment/reply/node/' . $this->node->id() . '/comment'); Chris@0: $anonymous_comment = $this->postComment($this->node, $this->randomMachineName(), '', TRUE); Chris@0: Chris@0: // Checks the new values of node comment statistics with comment #2 and Chris@0: // ensure they haven't changed since the comment has not been moderated. Chris@0: // The node needs to be reloaded with the cache reset. Chris@0: $node_storage->resetCache([$this->node->id()]); Chris@0: $node = $node_storage->load($this->node->id()); Chris@0: $this->assertEqual($node->get('comment')->last_comment_name, NULL, 'The value of node last_comment_name is still NULL.'); Chris@0: $this->assertEqual($node->get('comment')->last_comment_uid, $this->webUser2->id(), 'The value of node last_comment_uid is still the comment #1 uid.'); Chris@0: $this->assertEqual($node->get('comment')->comment_count, 1, 'The value of node comment_count is still 1.'); Chris@0: Chris@0: // Prepare for anonymous comment submission (no approval required). Chris@0: $this->drupalLogin($this->adminUser); Chris@0: user_role_change_permissions(RoleInterface::ANONYMOUS_ID, [ Chris@0: 'access comments' => TRUE, Chris@0: 'post comments' => TRUE, Chris@0: 'skip comment approval' => TRUE, Chris@0: ]); Chris@0: $this->drupalLogout(); Chris@0: Chris@0: // Post comment #3 as anonymous. Chris@0: $this->drupalGet('comment/reply/node/' . $this->node->id() . '/comment'); Chris@0: $anonymous_comment = $this->postComment($this->node, $this->randomMachineName(), '', ['name' => $this->randomMachineName()]); Chris@0: $comment_loaded = Comment::load($anonymous_comment->id()); Chris@0: Chris@0: // Checks the new values of node comment statistics with comment #3. Chris@0: // The node needs to be reloaded with the cache reset. Chris@0: $node_storage->resetCache([$this->node->id()]); Chris@0: $node = $node_storage->load($this->node->id()); Chris@0: $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.'); Chris@0: $this->assertEqual($node->get('comment')->last_comment_uid, 0, 'The value of node last_comment_uid is zero.'); Chris@0: $this->assertEqual($node->get('comment')->comment_count, 2, 'The value of node comment_count is 2.'); Chris@0: } Chris@0: Chris@0: }