1 comment.test CommentInterfaceTest::testCommentClasses()

Tests CSS classes on comments.

File

core/modules/comment/tests/comment.test, line 599
Tests for the Comment module.

Class

CommentInterfaceTest

Code

function testCommentClasses() {
  // Create all permutations for comments, users, and nodes.
  $parameters = array(
    'node_uid' => array(0, $this->web_user->uid),
    'comment_uid' => array(0, $this->web_user->uid, $this->admin_user->uid),
    'comment_status' => array(COMMENT_PUBLISHED, COMMENT_NOT_PUBLISHED),
    'user' => array('anonymous', 'authenticated', 'admin'),
  );
  $permutations = $this->generatePermutations($parameters);

  foreach ($permutations as $case) {
    // Create a new node.
    $node = $this->backdropCreateNode(array('type' => 'post', 'uid' => $case['node_uid']));

    // Add a comment.
    $comment = entity_create('comment', array(
      'nid' => $node->nid,
      'uid' => $case['comment_uid'],
      'status' => $case['comment_status'],
      'subject' => $this->randomName(),
      'langcode' => LANGUAGE_NONE,
      'comment_body' => array(LANGUAGE_NONE => array($this->randomName())),
    ));
    comment_save($comment);

    // Adjust the current/viewing user.
    switch ($case['user']) {
      case 'anonymous':
        $this->backdropLogout();
        $case['user_uid'] = 0;
        break;

      case 'authenticated':
        $this->backdropLogin($this->web_user);
        $case['user_uid'] = $this->web_user->uid;
        break;

      case 'admin':
        $this->backdropLogin($this->admin_user);
        $case['user_uid'] = $this->admin_user->uid;
        break;
    }

    // Wait a few seconds to ensure that the "comment-new" flag is allowed to
    // trigger (more than 1 second from the time the comment was created).
    sleep(3);

    // Request the node with the comment.
    $this->backdropGet('node/' . $node->nid);

    // Verify classes if the comment is visible for the current user.
    if ($case['comment_status'] == COMMENT_PUBLISHED || $case['user'] == 'admin') {
      // Verify the comment-by-anonymous class.
      $comments = $this->xpath('//*[contains(@class, "comment-by-anonymous")]');
      if ($case['comment_uid'] == 0) {
        $this->assertTrue(count($comments) == 1, 'comment-by-anonymous class found.');
      }
      else {
        $this->assertFalse(count($comments), 'comment-by-anonymous class not found.');
      }

      // Verify the comment-by-node-author class.
      $comments = $this->xpath('//*[contains(@class, "comment-by-node-author")]');
      if ($case['comment_uid'] > 0 && $case['comment_uid'] == $case['node_uid']) {
        $this->assertTrue(count($comments) == 1, 'comment-by-node-author class found.');
      }
      else {
        $this->assertFalse(count($comments), 'comment-by-node-author class not found.');
      }

      // Verify the comment-by-viewer class.
      $comments = $this->xpath('//*[contains(@class, "comment-by-viewer")]');
      if ($case['comment_uid'] > 0 && $case['comment_uid'] == $case['user_uid']) {
        $this->assertTrue(count($comments) == 1, 'comment-by-viewer class found.');
      }
      else {
        $this->assertFalse(count($comments), 'comment-by-viewer class not found.');
      }
    }

    // Verify the comment-unpublished class.
    $comments = $this->xpath('//*[contains(@class, "comment-unpublished")]');
    if ($case['comment_status'] == COMMENT_NOT_PUBLISHED && $case['user'] == 'admin') {
      $this->assertTrue(count($comments) == 1, 'comment-unpublished class found.');
    }
    else {
      $this->assertFalse(count($comments), 'comment-unpublished class not found.');
    }

    // Verify the comment-new class.
    if ($case['comment_status'] == COMMENT_PUBLISHED || $case['user'] == 'admin') {
      $comments = $this->xpath('//*[contains(@class, "comment-new")]');
      if ($case['user'] != 'anonymous') {
        $this->assertTrue(count($comments) == 1, 'comment-new class found.');

        // node_tag_new() writes to the database after delivering the page,
        // wait to ensure the record is written before reloading the page.
        sleep(2);

        // Request the node again. The comment-new class should disappear.
        $this->backdropGet('node/' . $node->nid);
        $comments = $this->xpath('//*[contains(@class, "comment-new")]');
        $this->assertFalse(count($comments), 'comment-new class not found.');
      }
      else {
        $this->assertFalse(count($comments), 'comment-new class not found.');
      }
    }
  }
}