annotate core/modules/comment/tests/src/Functional/CommentStatisticsTest.php @ 19:fa3358dc1485 tip

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