1 comment.test CommentInterfaceTest::testCommentInterface()

Tests the comment interface.

File

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

Class

CommentInterfaceTest

Code

function testCommentInterface() {
  $langcode = LANGUAGE_NONE;
  // Set comments to have auto generated title and preview disabled.
  $this->backdropLogin($this->admin_user);
  $this->setCommentPreview(BACKDROP_DISABLED);
  $this->setCommentForm(TRUE);
  $this->setCommentSubject(COMMENT_TITLE_AUTO);
  $this->setCommentSettings('comment_mode', COMMENT_MODE_THREADED, 'Comment paging changed.');
  $this->backdropLogout();

  // Create comment #1 without title or preview.
  $this->backdropLogin($this->web_user);
  $comment_text = $this->randomName();
  $comment = $this->postComment($this->node, $comment_text);
  $comment_loaded = comment_load($comment->id);
  $this->assertTrue($this->commentExists($comment), 'Comment found.');

  // Set comments to have custom title and preview to required.
  $this->backdropLogout();
  $this->backdropLogin($this->admin_user);
  $this->setCommentSubject(COMMENT_TITLE_CUSTOM);
  $this->setCommentPreview(BACKDROP_REQUIRED);
  $this->backdropLogout();

  // Create comment #2 that allows title and requires preview.
  $this->backdropLogin($this->web_user);
  $title_text = $this->randomName();
  $comment_text = $this->randomName();
  $comment = $this->postComment($this->node, $comment_text, $title_text, TRUE);
  $comment_loaded = comment_load($comment->id);
  $this->assertTrue($this->commentExists($comment), 'Comment found.');

  // Check comment display.
  $this->backdropGet('node/' . $this->node->nid . '/' . $comment->id);
  $this->assertText($title_text, 'Individual comment title found.');
  $this->assertText($comment_text, 'Individual comment body found.');

  // Set comments to have custom title and preview to optional.
  $this->backdropLogout();
  $this->backdropLogin($this->admin_user);
  $this->setCommentSubject(COMMENT_TITLE_CUSTOM);
  $this->setCommentPreview(BACKDROP_OPTIONAL);

  // Test changing the comment author to "Anonymous".
  $this->backdropGet('comment/' . $comment->id . '/edit');
  $comment = $this->postComment(NULL, $comment->comment, $comment->subject, array('name' => ''));
  $comment_loaded = comment_load($comment->id);
  $this->assertTrue(empty($comment_loaded->name) && $comment_loaded->uid == 0, 'Comment author successfully changed to anonymous.');

  // Test changing the comment author to an unverified user.
  $random_name = $this->randomName();
  $this->backdropGet('comment/' . $comment->id . '/edit');
  $comment = $this->postComment(NULL, $comment->comment, $comment->subject, array('name' => $random_name));
  $this->backdropGet('node/' . $this->node->nid);
  $this->assertText($random_name . ' (' . t('not verified') . ')', 'Comment author successfully changed to an unverified user.');

  // Test changing the comment author to a verified user.
  $this->backdropGet('comment/' . $comment->id . '/edit');
  $comment = $this->postComment(NULL, $comment->comment, $comment->subject, array('name' => $this->web_user->name));
  $comment_loaded = comment_load($comment->id);
  $this->assertTrue($comment_loaded->name == $this->web_user->name && $comment_loaded->uid == $this->web_user->uid, 'Comment author successfully changed to a registered user.');

  $this->backdropLogout();

  // Reply to comment #2 creating comment #3 with optional preview and no
  // title though field enabled.
  $this->backdropLogin($this->web_user);
  $this->backdropGet('comment/reply/' . $this->node->nid . '/' . $comment->id);
  $this->assertText($title_text, 'Individual comment-reply title found.');
  $this->assertText($comment_text, 'Individual comment-reply body found.');
  $this->assertUniqueText($comment_text, 'Comment-reply body found only once.');
  $reply = $this->postComment(NULL, $this->randomName(), '', TRUE);
  $reply_loaded = comment_load($reply->id);
  $this->assertTrue($this->commentExists($reply, TRUE), 'Reply found.');
  $this->assertEqual($comment->id, $reply_loaded->pid, 'Pid of a reply to a comment is set correctly.');
  $this->assertEqual(rtrim($comment_loaded->thread, '/') . '.00/', $reply_loaded->thread, 'Thread of reply grows correctly.');

  // Second reply to comment #2 creating comment #4.
  $this->backdropGet('comment/reply/' . $this->node->nid . '/' . $comment->id);
  $this->assertText($title_text, 'Individual comment-reply subject found.');
  $this->assertText($comment_text, 'Individual comment-reply body found.');
  $reply = $this->postComment(NULL, $this->randomName(), $this->randomName(), TRUE);
  $reply_loaded = comment_load($reply->id);
  $this->assertTrue($this->commentExists($reply, TRUE), 'Second reply found.');
  $this->assertEqual(rtrim($comment_loaded->thread, '/') . '.01/', $reply_loaded->thread, 'Thread of second reply grows correctly.');
  // Test rendered comment for title.
  $this->backdropGet('node/' . $this->node->nid);
  $this->assertText($title_text, 'Individual comment title found.');

  // Edit reply.
  $this->backdropLogin($this->web_user);
  $this->backdropGet('comment/' . $reply->id . '/edit');
  $reply = $this->postComment(NULL, $this->randomName(), $this->randomName(), TRUE);
  $this->assertTrue($this->commentExists($reply, TRUE), 'Modified reply found.');

  // Correct link count
  $this->backdropGet('node');
  $this->assertRaw('4 comments', 'Link to the 4 comments exist.');

  // Set comments to have hidden title.
  $this->backdropLogin($this->admin_user);
  $this->setCommentSubject(COMMENT_TITLE_HIDDEN);

  // Third reply to comment #2 creating comment #5 to test hidden titles.
  $this->backdropLogin($this->web_user);
  $this->backdropGet('comment/reply/' . $this->node->nid . '/' . $comment->id);
  $this->assertNoText($title_text, 'Individual comment-reply title not found.');
  $reply = $this->postComment(NULL, $this->randomName(), NULL, TRUE);
  $reply_loaded = comment_load($reply->id);

  // Test rendered comment for hidden title.
  $this->backdropGet('node/' . $this->node->nid);
  $this->assertNoText($title_text, 'Individual comment title not found.');

  // Restore comment title setting to custom titles.
  $this->backdropLogin($this->admin_user);
  $this->setCommentSubject(COMMENT_TITLE_CUSTOM);

  // Confirm a new comment is posted to the correct page.
  $this->setCommentsPerPage(2);
  $comment_new_page = $this->postComment($this->node, $this->randomName(), $this->randomName(), TRUE);
  $this->assertTrue($this->commentExists($comment_new_page), 'Page one exists. %s');
  $this->backdropGet('node/' . $this->node->nid, array('query' => array('page' => 2)));
  $this->assertTrue($this->commentExists($reply, TRUE), 'Page two exists. %s');
  $this->setCommentsPerPage(50);

  // Attempt to post to node with comments disabled.
  $this->node = $this->backdropCreateNode(array('type' => 'post', 'promote' => 1, 'comment' => COMMENT_NODE_HIDDEN));
  $this->assertTrue($this->node, 'Post node created.');
  $this->backdropGet('comment/reply/' . $this->node->nid);
  $this->assertText('This discussion is closed', 'Posting to node with comments disabled');
  $this->assertNoField('edit-comment', 'Comment body field found.');

  // Attempt to post to node with read-only comments.
  $this->node = $this->backdropCreateNode(array('type' => 'post', 'promote' => 1, 'comment' => COMMENT_NODE_CLOSED));
  $this->assertTrue($this->node, 'Post node created.');
  $this->backdropGet('comment/reply/' . $this->node->nid);
  $this->assertText('This discussion is closed', 'Posting to node with comments read-only');
  $this->assertNoField('edit-comment', 'Comment body field found.');

  // Attempt to post to node with comments enabled (check field names etc).
  $this->node = $this->backdropCreateNode(array('type' => 'post', 'promote' => 1, 'comment' => COMMENT_NODE_OPEN));
  $this->assertTrue($this->node, 'Post node created.');
  $this->backdropGet('comment/reply/' . $this->node->nid);
  $this->assertNoText('This discussion is closed', 'Posting to node with comments enabled');
  $this->assertField('edit-comment-body-' . $langcode . '-0-value', 'Comment body field found.');

  // Delete comment and make sure that reply is also removed.
  $this->backdropLogout();
  $this->backdropLogin($this->admin_user);
  $this->deleteComment($comment);
  $this->deleteComment($comment_new_page);

  $this->backdropGet('node/' . $this->node->nid);
  $this->assertFalse($this->commentExists($comment), 'Comment not found.');
  $this->assertFalse($this->commentExists($reply, TRUE), 'Reply not found.');

  // Enabled comment form on node page.
  $this->backdropLogin($this->admin_user);
  $this->setCommentForm(TRUE);
  $this->backdropLogout();

  // Submit comment through node form.
  $this->backdropLogin($this->web_user);
  $this->backdropGet('node/' . $this->node->nid);
  $form_comment = $this->postComment(NULL, $this->randomName(), $this->randomName(), TRUE);
  $this->assertTrue($this->commentExists($form_comment), 'Form comment found.');

  // Disable comment form on node page.
  $this->backdropLogout();
  $this->backdropLogin($this->admin_user);
  $this->setCommentForm(FALSE);

  // Check that moderator can manage individual comments, but not comment
  // settings.
  $this->backdropLogout();
  $this->backdropLogin($this->moderator_user);
  $this->backdropGet('admin/structure/types/manage/post');
  $this->assertNoText(t('Comment settings'));
  $this->backdropGet('admin/content/comment');
  $this->assertNoText(t('You are not authorized to access this page.'));
}