CommentStatisticsTest.php 4.75 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
<?php

/**
 * @file
 * Contains Drupal\comment\Tests\CommentStatisticsTest.
 */

namespace Drupal\comment\Tests;

/**
 * Tests the comment module administrative and end-user-facing interfaces.
 */
class CommentStatisticsTest extends CommentTestBase {

  public static function getInfo() {
    return array(
      'name' => 'Comment statistics',
      'description' => 'Test comment statistics on nodes.',
      'group' => 'Comment',
    );
  }

23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
  function setUp() {
    parent::setUp();

    // Create a second user to post comments.
    $this->web_user2 = $this->drupalCreateUser(array(
      'post comments',
      'create article content',
      'edit own comments',
      'post comments',
      'skip comment approval',
      'access comments',
      'access content',
    ));
  }

38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
  /**
   * Tests the node comment statistics.
   */
  function testCommentNodeCommentStatistics() {
    // Set comments to have subject and preview disabled.
    $this->drupalLogin($this->admin_user);
    $this->setCommentPreview(DRUPAL_DISABLED);
    $this->setCommentForm(TRUE);
    $this->setCommentSubject(FALSE);
    $this->setCommentSettings('comment_default_mode', COMMENT_MODE_THREADED, 'Comment paging changed.');
    $this->drupalLogout();

    // Checks the initial values of node comment statistics with no comment.
    $node = node_load($this->node->nid);
    $this->assertEqual($node->last_comment_timestamp, $this->node->created, 'The initial value of node last_comment_timestamp is the node created date.');
    $this->assertEqual($node->last_comment_name, NULL, 'The initial value of node last_comment_name is NULL.');
54
    $this->assertEqual($node->last_comment_uid, $this->web_user->id(), 'The initial value of node last_comment_uid is the node uid.');
55 56 57 58 59
    $this->assertEqual($node->comment_count, 0, 'The initial value of node comment_count is zero.');

    // Post comment #1 as web_user2.
    $this->drupalLogin($this->web_user2);
    $comment_text = $this->randomName();
60
    $this->postComment($this->node, $comment_text);
61 62 63 64 65

    // Checks the new values of node comment statistics with comment #1.
    // The node needs to be reloaded with a node_load_multiple cache reset.
    $node = node_load($this->node->nid, TRUE);
    $this->assertEqual($node->last_comment_name, NULL, 'The value of node last_comment_name is NULL.');
66
    $this->assertEqual($node->last_comment_uid, $this->web_user2->id(), 'The value of node last_comment_uid is the comment #1 uid.');
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81
    $this->assertEqual($node->comment_count, 1, 'The value of node comment_count is 1.');

    // Prepare for anonymous comment submission (comment approval enabled).
    $this->drupalLogin($this->admin_user);
    user_role_change_permissions(DRUPAL_ANONYMOUS_RID, array(
      'access comments' => TRUE,
      'post comments' => TRUE,
      'skip comment approval' => FALSE,
    ));
    // Ensure that the poster can leave some contact info.
    $this->setCommentAnonymous('1');
    $this->drupalLogout();

    // Post comment #2 as anonymous (comment approval enabled).
    $this->drupalGet('comment/reply/' . $this->node->nid);
82
    $this->postComment($this->node, $this->randomName(), '', TRUE);
83 84 85 86 87 88

    // Checks the new values of node comment statistics with comment #2 and
    // ensure they haven't changed since the comment has not been moderated.
    // The node needs to be reloaded with a node_load_multiple cache reset.
    $node = node_load($this->node->nid, TRUE);
    $this->assertEqual($node->last_comment_name, NULL, 'The value of node last_comment_name is still NULL.');
89
    $this->assertEqual($node->last_comment_uid, $this->web_user2->id(), 'The value of node last_comment_uid is still the comment #1 uid.');
90 91 92 93 94 95 96 97 98 99 100 101 102
    $this->assertEqual($node->comment_count, 1, 'The value of node comment_count is still 1.');

    // Prepare for anonymous comment submission (no approval required).
    $this->drupalLogin($this->admin_user);
    user_role_change_permissions(DRUPAL_ANONYMOUS_RID, array(
      'access comments' => TRUE,
      'post comments' => TRUE,
      'skip comment approval' => TRUE,
    ));
    $this->drupalLogout();

    // Post comment #3 as anonymous.
    $this->drupalGet('comment/reply/' . $this->node->nid);
103
    $comment_loaded = $this->postComment($this->node, $this->randomName(), '', array('name' => $this->randomName()));
104 105 106 107

    // Checks the new values of node comment statistics with comment #3.
    // The node needs to be reloaded with a node_load_multiple cache reset.
    $node = node_load($this->node->nid, TRUE);
108
    $this->assertEqual($node->last_comment_name, $comment_loaded->name->value, 'The value of node last_comment_name is the name of the anonymous user.');
109 110 111 112 113
    $this->assertEqual($node->last_comment_uid, 0, 'The value of node last_comment_uid is zero.');
    $this->assertEqual($node->comment_count, 2, 'The value of node comment_count is 2.');
  }

}