Skip navigation
Help

comment.test

  1. drupal
    1. 7 drupal/modules/comment/comment.test

Tests for comment.module.

Classes

NameDescription
CommentActionsTestCaseTest actions provided by the comment module.
CommentAnonymous
CommentApprovalTest
CommentBlockFunctionalTestFunctional tests for the comment module blocks.
CommentContentRebuildTest to make sure comment content is rebuilt.
CommentFieldsTestTest fields on comments.
CommentHelperCase
CommentInterfaceTest
CommentNodeAccessTestTests comments with node access.
CommentPagerTestVerify pagination of comments.
CommentPreviewTestTest previewing comments.
CommentRSSUnitTestUnit tests for comment module integration with RSS feeds.
CommentTokenReplaceTestCaseTest comment token replacement in strings.

File

drupal/modules/comment/comment.test
View source
  1. <?php
  2. /**
  3. * @file
  4. * Tests for comment.module.
  5. */
  6. class CommentHelperCase extends DrupalWebTestCase {
  7. protected $admin_user;
  8. protected $web_user;
  9. protected $node;
  10. function setUp() {
  11. parent::setUp('comment', 'search');
  12. // Create users and test node.
  13. $this->admin_user = $this->drupalCreateUser(array('administer content types', 'administer comments', 'administer blocks'));
  14. $this->web_user = $this->drupalCreateUser(array('access comments', 'post comments', 'create article content', 'edit own comments'));
  15. $this->node = $this->drupalCreateNode(array('type' => 'article', 'promote' => 1, 'uid' => $this->web_user->uid));
  16. }
  17. /**
  18. * Post comment.
  19. *
  20. * @param $node
  21. * Node to post comment on.
  22. * @param $comment
  23. * Comment body.
  24. * @param $subject
  25. * Comment subject.
  26. * @param $contact
  27. * Set to NULL for no contact info, TRUE to ignore success checking, and
  28. * array of values to set contact info.
  29. */
  30. function postComment($node, $comment, $subject = '', $contact = NULL) {
  31. $langcode = LANGUAGE_NONE;
  32. $edit = array();
  33. $edit['comment_body[' . $langcode . '][0][value]'] = $comment;
  34. $preview_mode = variable_get('comment_preview_article', DRUPAL_OPTIONAL);
  35. $subject_mode = variable_get('comment_subject_field_article', 1);
  36. // Must get the page before we test for fields.
  37. if ($node !== NULL) {
  38. $this->drupalGet('comment/reply/' . $node->nid);
  39. }
  40. if ($subject_mode == TRUE) {
  41. $edit['subject'] = $subject;
  42. }
  43. else {
  44. $this->assertNoFieldByName('subject', '', t('Subject field not found.'));
  45. }
  46. if ($contact !== NULL && is_array($contact)) {
  47. $edit += $contact;
  48. }
  49. switch ($preview_mode) {
  50. case DRUPAL_REQUIRED:
  51. // Preview required so no save button should be found.
  52. $this->assertNoFieldByName('op', t('Save'), t('Save button not found.'));
  53. $this->drupalPost(NULL, $edit, t('Preview'));
  54. // Don't break here so that we can test post-preview field presence and
  55. // function below.
  56. case DRUPAL_OPTIONAL:
  57. $this->assertFieldByName('op', t('Preview'), t('Preview button found.'));
  58. $this->assertFieldByName('op', t('Save'), t('Save button found.'));
  59. $this->drupalPost(NULL, $edit, t('Save'));
  60. break;
  61. case DRUPAL_DISABLED:
  62. $this->assertNoFieldByName('op', t('Preview'), t('Preview button not found.'));
  63. $this->assertFieldByName('op', t('Save'), t('Save button found.'));
  64. $this->drupalPost(NULL, $edit, t('Save'));
  65. break;
  66. }
  67. $match = array();
  68. // Get comment ID
  69. preg_match('/#comment-([0-9]+)/', $this->getURL(), $match);
  70. // Get comment.
  71. if ($contact !== TRUE) { // If true then attempting to find error message.
  72. if ($subject) {
  73. $this->assertText($subject, 'Comment subject posted.');
  74. }
  75. $this->assertText($comment, 'Comment body posted.');
  76. $this->assertTrue((!empty($match) && !empty($match[1])), t('Comment id found.'));
  77. }
  78. if (isset($match[1])) {
  79. return (object) array('id' => $match[1], 'subject' => $subject, 'comment' => $comment);
  80. }
  81. }
  82. /**
  83. * Checks current page for specified comment.
  84. *
  85. * @param object $comment Comment object.
  86. * @param boolean $reply The comment is a reply to another comment.
  87. * @return boolean Comment found.
  88. */
  89. function commentExists($comment, $reply = FALSE) {
  90. if ($comment && is_object($comment)) {
  91. $regex = '/' . ($reply ? '<div class="indented">(.*?)' : '');
  92. $regex .= '<a id="comment-' . $comment->id . '"(.*?)'; // Comment anchor.
  93. $regex .= '<div(.*?)'; // Begin in comment div.
  94. $regex .= $comment->subject . '(.*?)'; // Match subject.
  95. $regex .= $comment->comment . '(.*?)'; // Match comment.
  96. $regex .= '/s';
  97. return (boolean)preg_match($regex, $this->drupalGetContent());
  98. }
  99. else {
  100. return FALSE;
  101. }
  102. }
  103. /**
  104. * Delete comment.
  105. *
  106. * @param object $comment
  107. * Comment to delete.
  108. */
  109. function deleteComment($comment) {
  110. $this->drupalPost('comment/' . $comment->id . '/delete', array(), t('Delete'));
  111. $this->assertText(t('The comment and all its replies have been deleted.'), t('Comment deleted.'));
  112. }
  113. /**
  114. * Set comment subject setting.
  115. *
  116. * @param boolean $enabled
  117. * Subject value.
  118. */
  119. function setCommentSubject($enabled) {
  120. $this->setCommentSettings('comment_subject_field', ($enabled ? '1' : '0'), 'Comment subject ' . ($enabled ? 'enabled' : 'disabled') . '.');
  121. }
  122. /**
  123. * Set comment preview setting.
  124. *
  125. * @param int $mode
  126. * Preview value.
  127. */
  128. function setCommentPreview($mode) {
  129. switch ($mode) {
  130. case DRUPAL_DISABLED:
  131. $mode_text = 'disabled';
  132. break;
  133. case DRUPAL_OPTIONAL:
  134. $mode_text = 'optional';
  135. break;
  136. case DRUPAL_REQUIRED:
  137. $mode_text = 'required';
  138. break;
  139. }
  140. $this->setCommentSettings('comment_preview', $mode, 'Comment preview ' . $mode_text . '.');
  141. }
  142. /**
  143. * Set comment form location setting.
  144. *
  145. * @param boolean $enabled
  146. * Form value.
  147. */
  148. function setCommentForm($enabled) {
  149. $this->setCommentSettings('comment_form_location', ($enabled ? COMMENT_FORM_BELOW : COMMENT_FORM_SEPARATE_PAGE), 'Comment controls ' . ($enabled ? 'enabled' : 'disabled') . '.');
  150. }
  151. /**
  152. * Set comment anonymous level setting.
  153. *
  154. * @param integer $level
  155. * Anonymous level.
  156. */
  157. function setCommentAnonymous($level) {
  158. $this->setCommentSettings('comment_anonymous', $level, 'Anonymous commenting set to level ' . $level . '.');
  159. }
  160. /**
  161. * Set the default number of comments per page.
  162. *
  163. * @param integer $comments
  164. * Comments per page value.
  165. */
  166. function setCommentsPerPage($number) {
  167. $this->setCommentSettings('comment_default_per_page', $number, 'Number of comments per page set to ' . $number . '.');
  168. }
  169. /**
  170. * Set comment setting for article content type.
  171. *
  172. * @param string $name
  173. * Name of variable.
  174. * @param string $value
  175. * Value of variable.
  176. * @param string $message
  177. * Status message to display.
  178. */
  179. function setCommentSettings($name, $value, $message) {
  180. variable_set($name . '_article', $value);
  181. $this->assertTrue(TRUE, t($message)); // Display status message.
  182. }
  183. /**
  184. * Check for contact info.
  185. *
  186. * @return boolean Contact info is available.
  187. */
  188. function commentContactInfoAvailable() {
  189. return preg_match('/(input).*?(name="name").*?(input).*?(name="mail").*?(input).*?(name="homepage")/s', $this->drupalGetContent());
  190. }
  191. /**
  192. * Perform the specified operation on the specified comment.
  193. *
  194. * @param object $comment
  195. * Comment to perform operation on.
  196. * @param string $operation
  197. * Operation to perform.
  198. * @param boolean $aproval
  199. * Operation is found on approval page.
  200. */
  201. function performCommentOperation($comment, $operation, $approval = FALSE) {
  202. $edit = array();
  203. $edit['operation'] = $operation;
  204. $edit['comments[' . $comment->id . ']'] = TRUE;
  205. $this->drupalPost('admin/content/comment' . ($approval ? '/approval' : ''), $edit, t('Update'));
  206. if ($operation == 'delete') {
  207. $this->drupalPost(NULL, array(), t('Delete comments'));
  208. $this->assertRaw(format_plural(1, 'Deleted 1 comment.', 'Deleted @count comments.'), t('Operation "' . $operation . '" was performed on comment.'));
  209. }
  210. else {
  211. $this->assertText(t('The update has been performed.'), t('Operation "' . $operation . '" was performed on comment.'));
  212. }
  213. }
  214. /**
  215. * Get the comment ID for an unapproved comment.
  216. *
  217. * @param string $subject
  218. * Comment subject to find.
  219. * @return integer
  220. * Comment id.
  221. */
  222. function getUnapprovedComment($subject) {
  223. $this->drupalGet('admin/content/comment/approval');
  224. preg_match('/href="(.*?)#comment-([^"]+)"(.*?)>(' . $subject . ')/', $this->drupalGetContent(), $match);
  225. return $match[2];
  226. }
  227. /**
  228. * Tests new comment marker.
  229. */
  230. public function testCommentNewCommentsIndicator() {
  231. // Test if the right links are displayed when no comment is present for the
  232. // node.
  233. $this->drupalLogin($this->admin_user);
  234. $this->node = $this->drupalCreateNode(array('type' => 'article', 'promote' => 1, 'comment' => COMMENT_NODE_OPEN));
  235. $this->drupalGet('node');
  236. $this->assertNoLink(t('@count comments', array('@count' => 0)));
  237. $this->assertNoLink(t('@count new comments', array('@count' => 0)));
  238. $this->assertLink(t('Read more'));
  239. $count = $this->xpath('//div[@id=:id]/div[@class=:class]/ul/li', array(':id' => 'node-' . $this->node->nid, ':class' => 'link-wrapper'));
  240. $this->assertTrue(count($count) == 1, t('One child found'));
  241. // Create a new comment. This helper function may be run with different
  242. // comment settings so use comment_save() to avoid complex setup.
  243. $comment = (object) array(
  244. 'cid' => NULL,
  245. 'nid' => $this->node->nid,
  246. 'node_type' => $this->node->type,
  247. 'pid' => 0,
  248. 'uid' => $this->loggedInUser->uid,
  249. 'status' => COMMENT_PUBLISHED,
  250. 'subject' => $this->randomName(),
  251. 'hostname' => ip_address(),
  252. 'language' => LANGUAGE_NONE,
  253. 'comment_body' => array(LANGUAGE_NONE => array($this->randomName())),
  254. );
  255. comment_save($comment);
  256. $this->drupalLogout();
  257. // Log in with 'web user' and check comment links.
  258. $this->drupalLogin($this->web_user);
  259. $this->drupalGet('node');
  260. $this->assertLink(t('1 new comment'));
  261. $this->clickLink(t('1 new comment'));
  262. $this->assertRaw('<a id="new"></a>', t('Found "new" marker.'));
  263. $this->assertTrue($this->xpath('//a[@id=:new]/following-sibling::a[1][@id=:comment_id]', array(':new' => 'new', ':comment_id' => 'comment-1')), t('The "new" anchor is positioned at the right comment.'));
  264. // Test if "new comment" link is correctly removed.
  265. $this->drupalGet('node');
  266. $this->assertLink(t('1 comment'));
  267. $this->assertLink(t('Read more'));
  268. $this->assertNoLink(t('1 new comment'));
  269. $this->assertNoLink(t('@count new comments', array('@count' => 0)));
  270. $count = $this->xpath('//div[@id=:id]/div[@class=:class]/ul/li', array(':id' => 'node-' . $this->node->nid, ':class' => 'link-wrapper'));
  271. $this->assertTrue(count($count) == 2, print_r($count, TRUE));
  272. }
  273. }
  274. class CommentInterfaceTest extends CommentHelperCase {
  275. public static function getInfo() {
  276. return array(
  277. 'name' => 'Comment interface',
  278. 'description' => 'Test comment user interfaces.',
  279. 'group' => 'Comment',
  280. );
  281. }
  282. /**
  283. * Test comment interface.
  284. */
  285. function testCommentInterface() {
  286. $langcode = LANGUAGE_NONE;
  287. // Set comments to have subject and preview disabled.
  288. $this->drupalLogin($this->admin_user);
  289. $this->setCommentPreview(DRUPAL_DISABLED);
  290. $this->setCommentForm(TRUE);
  291. $this->setCommentSubject(FALSE);
  292. $this->setCommentSettings('comment_default_mode', COMMENT_MODE_THREADED, t('Comment paging changed.'));
  293. $this->drupalLogout();
  294. // Post comment #1 without subject or preview.
  295. $this->drupalLogin($this->web_user);
  296. $comment_text = $this->randomName();
  297. $comment = $this->postComment($this->node, $comment_text);
  298. $comment_loaded = comment_load($comment->id);
  299. $this->assertTrue($this->commentExists($comment), t('Comment found.'));
  300. // Set comments to have subject and preview to required.
  301. $this->drupalLogout();
  302. $this->drupalLogin($this->admin_user);
  303. $this->setCommentSubject(TRUE);
  304. $this->setCommentPreview(DRUPAL_REQUIRED);
  305. $this->drupalLogout();
  306. // Create comment #2 that allows subject and requires preview.
  307. $this->drupalLogin($this->web_user);
  308. $subject_text = $this->randomName();
  309. $comment_text = $this->randomName();
  310. $comment = $this->postComment($this->node, $comment_text, $subject_text, TRUE);
  311. $comment_loaded = comment_load($comment->id);
  312. $this->assertTrue($this->commentExists($comment), t('Comment found.'));
  313. // Check comment display.
  314. $this->drupalGet('node/' . $this->node->nid . '/' . $comment->id);
  315. $this->assertText($subject_text, t('Individual comment subject found.'));
  316. $this->assertText($comment_text, t('Individual comment body found.'));
  317. // Set comments to have subject and preview to optional.
  318. $this->drupalLogout();
  319. $this->drupalLogin($this->admin_user);
  320. $this->setCommentSubject(TRUE);
  321. $this->setCommentPreview(DRUPAL_OPTIONAL);
  322. // Test changing the comment author to "Anonymous".
  323. $this->drupalGet('comment/' . $comment->id . '/edit');
  324. $comment = $this->postComment(NULL, $comment->comment, $comment->subject, array('name' => ''));
  325. $comment_loaded = comment_load($comment->id);
  326. $this->assertTrue(empty($comment_loaded->name) && $comment_loaded->uid == 0, t('Comment author successfully changed to anonymous.'));
  327. // Test changing the comment author to an unverified user.
  328. $random_name = $this->randomName();
  329. $this->drupalGet('comment/' . $comment->id . '/edit');
  330. $comment = $this->postComment(NULL, $comment->comment, $comment->subject, array('name' => $random_name));
  331. $this->drupalGet('node/' . $this->node->nid);
  332. $this->assertText($random_name . ' (' . t('not verified') . ')', t('Comment author successfully changed to an unverified user.'));
  333. // Test changing the comment author to a verified user.
  334. $this->drupalGet('comment/' . $comment->id . '/edit');
  335. $comment = $this->postComment(NULL, $comment->comment, $comment->subject, array('name' => $this->web_user->name));
  336. $comment_loaded = comment_load($comment->id);
  337. $this->assertTrue($comment_loaded->name == $this->web_user->name && $comment_loaded->uid == $this->web_user->uid, t('Comment author successfully changed to a registered user.'));
  338. $this->drupalLogout();
  339. // Reply to comment #2 creating comment #3 with optional preview and no
  340. // subject though field enabled.
  341. $this->drupalLogin($this->web_user);
  342. $this->drupalGet('comment/reply/' . $this->node->nid . '/' . $comment->id);
  343. $this->assertText($subject_text, t('Individual comment-reply subject found.'));
  344. $this->assertText($comment_text, t('Individual comment-reply body found.'));
  345. $reply = $this->postComment(NULL, $this->randomName(), '', TRUE);
  346. $reply_loaded = comment_load($reply->id);
  347. $this->assertTrue($this->commentExists($reply, TRUE), t('Reply found.'));
  348. $this->assertEqual($comment->id, $reply_loaded->pid, t('Pid of a reply to a comment is set correctly.'));
  349. $this->assertEqual(rtrim($comment_loaded->thread, '/') . '.00/', $reply_loaded->thread, t('Thread of reply grows correctly.'));
  350. // Second reply to comment #3 creating comment #4.
  351. $this->drupalGet('comment/reply/' . $this->node->nid . '/' . $comment->id);
  352. $this->assertText($subject_text, t('Individual comment-reply subject found.'));
  353. $this->assertText($comment_text, t('Individual comment-reply body found.'));
  354. $reply = $this->postComment(NULL, $this->randomName(), $this->randomName(), TRUE);
  355. $reply_loaded = comment_load($reply->id);
  356. $this->assertTrue($this->commentExists($reply, TRUE), t('Second reply found.'));
  357. $this->assertEqual(rtrim($comment_loaded->thread, '/') . '.01/', $reply_loaded->thread, t('Thread of second reply grows correctly.'));
  358. // Edit reply.
  359. $this->drupalGet('comment/' . $reply->id . '/edit');
  360. $reply = $this->postComment(NULL, $this->randomName(), $this->randomName(), TRUE);
  361. $this->assertTrue($this->commentExists($reply, TRUE), t('Modified reply found.'));
  362. // Correct link count
  363. $this->drupalGet('node');
  364. $this->assertRaw('4 comments', t('Link to the 4 comments exist.'));
  365. // Confirm a new comment is posted to the correct page.
  366. $this->setCommentsPerPage(2);
  367. $comment_new_page = $this->postComment($this->node, $this->randomName(), $this->randomName(), TRUE);
  368. $this->assertTrue($this->commentExists($comment_new_page), t('Page one exists. %s'));
  369. $this->drupalGet('node/' . $this->node->nid, array('query' => array('page' => 1)));
  370. $this->assertTrue($this->commentExists($reply, TRUE), t('Page two exists. %s'));
  371. $this->setCommentsPerPage(50);
  372. // Attempt to post to node with comments disabled.
  373. $this->node = $this->drupalCreateNode(array('type' => 'article', 'promote' => 1, 'comment' => COMMENT_NODE_HIDDEN));
  374. $this->assertTrue($this->node, t('Article node created.'));
  375. $this->drupalGet('comment/reply/' . $this->node->nid);
  376. $this->assertText('This discussion is closed', t('Posting to node with comments disabled'));
  377. $this->assertNoField('edit-comment', t('Comment body field found.'));
  378. // Attempt to post to node with read-only comments.
  379. $this->node = $this->drupalCreateNode(array('type' => 'article', 'promote' => 1, 'comment' => COMMENT_NODE_CLOSED));
  380. $this->assertTrue($this->node, t('Article node created.'));
  381. $this->drupalGet('comment/reply/' . $this->node->nid);
  382. $this->assertText('This discussion is closed', t('Posting to node with comments read-only'));
  383. $this->assertNoField('edit-comment', t('Comment body field found.'));
  384. // Attempt to post to node with comments enabled (check field names etc).
  385. $this->node = $this->drupalCreateNode(array('type' => 'article', 'promote' => 1, 'comment' => COMMENT_NODE_OPEN));
  386. $this->assertTrue($this->node, t('Article node created.'));
  387. $this->drupalGet('comment/reply/' . $this->node->nid);
  388. $this->assertNoText('This discussion is closed', t('Posting to node with comments enabled'));
  389. $this->assertField('edit-comment-body-' . $langcode . '-0-value', t('Comment body field found.'));
  390. // Delete comment and make sure that reply is also removed.
  391. $this->drupalLogout();
  392. $this->drupalLogin($this->admin_user);
  393. $this->deleteComment($comment);
  394. $this->deleteComment($comment_new_page);
  395. $this->drupalGet('node/' . $this->node->nid);
  396. $this->assertFalse($this->commentExists($comment), t('Comment not found.'));
  397. $this->assertFalse($this->commentExists($reply, TRUE), t('Reply not found.'));
  398. // Enabled comment form on node page.
  399. $this->drupalLogin($this->admin_user);
  400. $this->setCommentForm(TRUE);
  401. $this->drupalLogout();
  402. // Submit comment through node form.
  403. $this->drupalLogin($this->web_user);
  404. $this->drupalGet('node/' . $this->node->nid);
  405. $form_comment = $this->postComment(NULL, $this->randomName(), $this->randomName(), TRUE);
  406. $this->assertTrue($this->commentExists($form_comment), t('Form comment found.'));
  407. // Disable comment form on node page.
  408. $this->drupalLogout();
  409. $this->drupalLogin($this->admin_user);
  410. $this->setCommentForm(FALSE);
  411. }
  412. /**
  413. * Tests the node comment statistics.
  414. */
  415. function testCommentNodeCommentStatistics() {
  416. $langcode = LANGUAGE_NONE;
  417. // Set comments to have subject and preview disabled.
  418. $this->drupalLogin($this->admin_user);
  419. $this->setCommentPreview(DRUPAL_DISABLED);
  420. $this->setCommentForm(TRUE);
  421. $this->setCommentSubject(FALSE);
  422. $this->setCommentSettings('comment_default_mode', COMMENT_MODE_THREADED, t('Comment paging changed.'));
  423. $this->drupalLogout();
  424. // Creates a second user to post comments.
  425. $this->web_user2 = $this->drupalCreateUser(array('access comments', 'post comments', 'create article content', 'edit own comments'));
  426. // Checks the initial values of node comment statistics with no comment.
  427. $node = node_load($this->node->nid);
  428. $this->assertEqual($node->last_comment_timestamp, $this->node->created, t('The initial value of node last_comment_timestamp is the node created date.'));
  429. $this->assertEqual($node->last_comment_name, NULL, t('The initial value of node last_comment_name is NULL.'));
  430. $this->assertEqual($node->last_comment_uid, $this->web_user->uid, t('The initial value of node last_comment_uid is the node uid.'));
  431. $this->assertEqual($node->comment_count, 0, t('The initial value of node comment_count is zero.'));
  432. // Post comment #1 as web_user2.
  433. $this->drupalLogin($this->web_user2);
  434. $comment_text = $this->randomName();
  435. $comment = $this->postComment($this->node, $comment_text);
  436. $comment_loaded = comment_load($comment->id);
  437. // Checks the new values of node comment statistics with comment #1.
  438. // The node needs to be reloaded with a node_load_multiple cache reset.
  439. $node = node_load($this->node->nid, NULL, TRUE);
  440. $this->assertEqual($node->last_comment_name, NULL, t('The value of node last_comment_name is NULL.'));
  441. $this->assertEqual($node->last_comment_uid, $this->web_user2->uid, t('The value of node last_comment_uid is the comment #1 uid.'));
  442. $this->assertEqual($node->comment_count, 1, t('The value of node comment_count is 1.'));
  443. // Prepare for anonymous comment submission (comment approval enabled).
  444. variable_set('user_register', USER_REGISTER_VISITORS);
  445. $this->drupalLogin($this->admin_user);
  446. user_role_change_permissions(DRUPAL_ANONYMOUS_RID, array(
  447. 'access comments' => TRUE,
  448. 'post comments' => TRUE,
  449. 'skip comment approval' => FALSE,
  450. ));
  451. // Ensure that the poster can leave some contact info.
  452. $this->setCommentAnonymous('1');
  453. $this->drupalLogout();
  454. // Post comment #2 as anonymous (comment approval enabled).
  455. $this->drupalGet('comment/reply/' . $this->node->nid);
  456. $anonymous_comment = $this->postComment($this->node, $this->randomName(), '', TRUE);
  457. $comment_unpublished_loaded = comment_load($anonymous_comment->id);
  458. // Checks the new values of node comment statistics with comment #2 and
  459. // ensure they haven't changed since the comment has not been moderated.
  460. // The node needs to be reloaded with a node_load_multiple cache reset.
  461. $node = node_load($this->node->nid, NULL, TRUE);
  462. $this->assertEqual($node->last_comment_name, NULL, t('The value of node last_comment_name is still NULL.'));
  463. $this->assertEqual($node->last_comment_uid, $this->web_user2->uid, t('The value of node last_comment_uid is still the comment #1 uid.'));
  464. $this->assertEqual($node->comment_count, 1, t('The value of node comment_count is still 1.'));
  465. // Prepare for anonymous comment submission (no approval required).
  466. $this->drupalLogin($this->admin_user);
  467. user_role_change_permissions(DRUPAL_ANONYMOUS_RID, array(
  468. 'access comments' => TRUE,
  469. 'post comments' => TRUE,
  470. 'skip comment approval' => TRUE,
  471. ));
  472. $this->drupalLogout();
  473. // Post comment #3 as anonymous.
  474. $this->drupalGet('comment/reply/' . $this->node->nid);
  475. $anonymous_comment = $this->postComment($this->node, $this->randomName(), '', array('name' => $this->randomName()));
  476. $comment_loaded = comment_load($anonymous_comment->id);
  477. // Checks the new values of node comment statistics with comment #3.
  478. // The node needs to be reloaded with a node_load_multiple cache reset.
  479. $node = node_load($this->node->nid, NULL, TRUE);
  480. $this->assertEqual($node->last_comment_name, $comment_loaded->name, t('The value of node last_comment_name is the name of the anonymous user.'));
  481. $this->assertEqual($node->last_comment_uid, 0, t('The value of node last_comment_uid is zero.'));
  482. $this->assertEqual($node->comment_count, 2, t('The value of node comment_count is 2.'));
  483. }
  484. /**
  485. * Tests comment links.
  486. *
  487. * The output of comment links depends on various environment conditions:
  488. * - Various Comment module configuration settings, user registration
  489. * settings, and user access permissions.
  490. * - Whether the user is authenticated or not, and whether any comments exist.
  491. *
  492. * To account for all possible cases, this test creates permutations of all
  493. * possible conditions and tests the expected appearance of comment links in
  494. * each environment.
  495. */
  496. function testCommentLinks() {
  497. // Bartik theme alters comment links, so use a different theme.
  498. theme_enable(array('garland'));
  499. variable_set('theme_default', 'garland');
  500. // Remove additional user permissions from $this->web_user added by setUp(),
  501. // since this test is limited to anonymous and authenticated roles only.
  502. user_role_delete(key($this->web_user->roles));
  503. // Matrix of possible environmental conditions and configuration settings.
  504. // See setEnvironment() for details.
  505. $conditions = array(
  506. 'authenticated' => array(FALSE, TRUE),
  507. 'comment count' => array(FALSE, TRUE),
  508. 'access comments' => array(0, 1),
  509. 'post comments' => array(0, 1),
  510. 'form' => array(COMMENT_FORM_BELOW, COMMENT_FORM_SEPARATE_PAGE),
  511. // USER_REGISTER_VISITORS_ADMINISTRATIVE_APPROVAL is irrelevant for this
  512. // test; there is only a difference between open and closed registration.
  513. 'user_register' => array(USER_REGISTER_VISITORS, USER_REGISTER_ADMINISTRATORS_ONLY),
  514. // @todo Complete test coverage for:
  515. //'comments' => array(COMMENT_NODE_OPEN, COMMENT_NODE_CLOSED, COMMENT_NODE_HIDDEN),
  516. //// COMMENT_ANONYMOUS_MUST_CONTACT is irrelevant for this test.
  517. //'contact ' => array(COMMENT_ANONYMOUS_MAY_CONTACT, COMMENT_ANONYMOUS_MAYNOT_CONTACT),
  518. );
  519. $environments = $this->generatePermutations($conditions);
  520. foreach ($environments as $info) {
  521. $this->assertCommentLinks($info);
  522. }
  523. }
  524. /**
  525. * Re-configures the environment, module settings, and user permissions.
  526. *
  527. * @param $info
  528. * An associative array describing the environment to setup:
  529. * - Environment conditions:
  530. * - authenticated: Boolean whether to test with $this->web_user or
  531. * anonymous.
  532. * - comment count: Boolean whether to test with a new/unread comment on
  533. * $this->node or no comments.
  534. * - Configuration settings:
  535. * - form: COMMENT_FORM_BELOW or COMMENT_FORM_SEPARATE_PAGE.
  536. * - user_register: USER_REGISTER_ADMINISTRATORS_ONLY or
  537. * USER_REGISTER_VISITORS.
  538. * - contact: COMMENT_ANONYMOUS_MAY_CONTACT or
  539. * COMMENT_ANONYMOUS_MAYNOT_CONTACT.
  540. * - comments: COMMENT_NODE_OPEN, COMMENT_NODE_CLOSED, or
  541. * COMMENT_NODE_HIDDEN.
  542. * - User permissions:
  543. * These are granted or revoked for the user, according to the
  544. * 'authenticated' flag above. Pass 0 or 1 as parameter values. See
  545. * user_role_change_permissions().
  546. * - access comments
  547. * - post comments
  548. * - skip comment approval
  549. * - edit own comments
  550. */
  551. function setEnvironment(array $info) {
  552. static $current;
  553. // Apply defaults to initial environment.
  554. if (!isset($current)) {
  555. $current = array(
  556. 'authenticated' => FALSE,
  557. 'comment count' => FALSE,
  558. 'form' => COMMENT_FORM_BELOW,
  559. 'user_register' => USER_REGISTER_VISITORS,
  560. 'contact' => COMMENT_ANONYMOUS_MAY_CONTACT,
  561. 'comments' => COMMENT_NODE_OPEN,
  562. 'access comments' => 0,
  563. 'post comments' => 0,
  564. // Enabled by default, because it's irrelevant for this test.
  565. 'skip comment approval' => 1,
  566. 'edit own comments' => 0,
  567. );
  568. }
  569. // Complete new environment with current environment.
  570. $info = array_merge($current, $info);
  571. // Change environment conditions.
  572. if ($current['authenticated'] != $info['authenticated']) {
  573. if ($this->loggedInUser) {
  574. $this->drupalLogout();
  575. }
  576. else {
  577. $this->drupalLogin($this->web_user);
  578. }
  579. }
  580. if ($current['comment count'] != $info['comment count']) {
  581. if ($info['comment count']) {
  582. // Create a comment via CRUD API functionality, since
  583. // $this->postComment() relies on actual user permissions.
  584. $comment = (object) array(
  585. 'cid' => NULL,
  586. 'nid' => $this->node->nid,
  587. 'node_type' => $this->node->type,
  588. 'pid' => 0,
  589. 'uid' => 0,
  590. 'status' => COMMENT_PUBLISHED,
  591. 'subject' => $this->randomName(),
  592. 'hostname' => ip_address(),
  593. 'language' => LANGUAGE_NONE,
  594. 'comment_body' => array(LANGUAGE_NONE => array($this->randomName())),
  595. );
  596. comment_save($comment);
  597. $this->comment = $comment;
  598. // comment_num_new() relies on node_last_viewed(), so ensure that no one
  599. // has seen the node of this comment.
  600. db_delete('history')->condition('nid', $this->node->nid)->execute();
  601. }
  602. else {
  603. $cids = db_query("SELECT cid FROM {comment}")->fetchCol();
  604. comment_delete_multiple($cids);
  605. unset($this->comment);
  606. }
  607. }
  608. // Change comment settings.
  609. variable_set('comment_form_location_' . $this->node->type, $info['form']);
  610. variable_set('comment_anonymous_' . $this->node->type, $info['contact']);
  611. if ($this->node->comment != $info['comments']) {
  612. $this->node->comment = $info['comments'];
  613. node_save($this->node);
  614. }
  615. // Change user settings.
  616. variable_set('user_register', $info['user_register']);
  617. // Change user permissions.
  618. $rid = ($this->loggedInUser ? DRUPAL_AUTHENTICATED_RID : DRUPAL_ANONYMOUS_RID);
  619. $perms = array_intersect_key($info, array('access comments' => 1, 'post comments' => 1, 'skip comment approval' => 1, 'edit own comments' => 1));
  620. user_role_change_permissions($rid, $perms);
  621. // Output verbose debugging information.
  622. // @see DrupalTestCase::error()
  623. $t_form = array(
  624. COMMENT_FORM_BELOW => 'below',
  625. COMMENT_FORM_SEPARATE_PAGE => 'separate page',
  626. );
  627. $t_contact = array(
  628. COMMENT_ANONYMOUS_MAY_CONTACT => 'optional',
  629. COMMENT_ANONYMOUS_MAYNOT_CONTACT => 'disabled',
  630. COMMENT_ANONYMOUS_MUST_CONTACT => 'required',
  631. );
  632. $t_comments = array(
  633. COMMENT_NODE_OPEN => 'open',
  634. COMMENT_NODE_CLOSED => 'closed',
  635. COMMENT_NODE_HIDDEN => 'hidden',
  636. );
  637. $verbose = $info;
  638. $verbose['form'] = $t_form[$info['form']];
  639. $verbose['contact'] = $t_contact[$info['contact']];
  640. $verbose['comments'] = $t_comments[$info['comments']];
  641. $message = t('Changed environment:<pre>@verbose</pre>', array(
  642. '@verbose' => var_export($verbose, TRUE),
  643. ));
  644. $this->assert('debug', $message, 'Debug');
  645. // Update current environment.
  646. $current = $info;
  647. return $info;
  648. }
  649. /**
  650. * Asserts that comment links appear according to the passed environment setup.
  651. *
  652. * @param $info
  653. * An associative array describing the environment to pass to
  654. * setEnvironment().
  655. */
  656. function assertCommentLinks(array $info) {
  657. $info = $this->setEnvironment($info);
  658. $nid = $this->node->nid;
  659. foreach (array('', "node/$nid") as $path) {
  660. $this->drupalGet($path);
  661. // User is allowed to view comments.
  662. if ($info['access comments']) {
  663. if ($path == '') {
  664. // In teaser view, a link containing the comment count is always
  665. // expected.
  666. if ($info['comment count']) {
  667. $this->assertLink(t('1 comment'));
  668. // For logged in users, a link containing the amount of new/unread
  669. // comments is expected.
  670. // See important note about comment_num_new() below.
  671. if ($this->loggedInUser && isset($this->comment) && !isset($this->comment->seen)) {
  672. $this->assertLink(t('1 new comment'));
  673. $this->comment->seen = TRUE;
  674. }
  675. }
  676. }
  677. }
  678. else {
  679. $this->assertNoLink(t('1 comment'));
  680. $this->assertNoLink(t('1 new comment'));
  681. }
  682. // comment_num_new() is based on node views, so comments are marked as
  683. // read when a node is viewed, regardless of whether we have access to
  684. // comments.
  685. if ($path == "node/$nid" && $this->loggedInUser && isset($this->comment)) {
  686. $this->comment->seen = TRUE;
  687. }
  688. // User is not allowed to post comments.
  689. if (!$info['post comments']) {
  690. $this->assertNoLink('Add new comment');
  691. // Anonymous users should see a note to log in or register in case
  692. // authenticated users are allowed to post comments.
  693. // @see theme_comment_post_forbidden()
  694. if (!$this->loggedInUser) {
  695. if (user_access('post comments', $this->web_user)) {
  696. // The note depends on whether users are actually able to register.
  697. if ($info['user_register']) {
  698. $this->assertText('Log in or register to post comments');
  699. }
  700. else {
  701. $this->assertText('Log in to post comments');
  702. }
  703. }
  704. else {
  705. $this->assertNoText('Log in or register to post comments');
  706. $this->assertNoText('Log in to post comments');
  707. }
  708. }
  709. }
  710. // User is allowed to post comments.
  711. else {
  712. $this->assertNoText('Log in or register to post comments');
  713. // "Add new comment" is always expected, except when there are no
  714. // comments or if the user cannot see them.
  715. if ($path == "node/$nid" && $info['form'] == COMMENT_FORM_BELOW && (!$info['comment count'] || !$info['access comments'])) {
  716. $this->assertNoLink('Add new comment');
  717. }
  718. else {
  719. $this->assertLink('Add new comment');
  720. }
  721. // Also verify that the comment form appears according to the configured
  722. // location.
  723. if ($path == "node/$nid") {
  724. $elements = $this->xpath('//form[@id=:id]', array(':id' => 'comment-form'));
  725. if ($info['form'] == COMMENT_FORM_BELOW) {
  726. $this->assertTrue(count($elements), t('Comment form found below.'));
  727. }
  728. else {
  729. $this->assertFalse(count($elements), t('Comment form not found below.'));
  730. }
  731. }
  732. }
  733. }
  734. }
  735. }
  736. /**
  737. * Test previewing comments.
  738. */
  739. class CommentPreviewTest extends CommentHelperCase {
  740. public static function getInfo() {
  741. return array(
  742. 'name' => 'Comment preview',
  743. 'description' => 'Test comment preview.',
  744. 'group' => 'Comment',
  745. );
  746. }
  747. /**
  748. * Test comment preview.
  749. */
  750. function testCommentPreview() {
  751. $langcode = LANGUAGE_NONE;
  752. // As admin user, configure comment settings.
  753. $this->drupalLogin($this->admin_user);
  754. $this->setCommentPreview(DRUPAL_OPTIONAL);
  755. $this->setCommentForm(TRUE);
  756. $this->setCommentSubject(TRUE);
  757. $this->setCommentSettings('comment_default_mode', COMMENT_MODE_THREADED, t('Comment paging changed.'));
  758. $this->drupalLogout();
  759. // As web user, fill in node creation form and preview node.
  760. $this->drupalLogin($this->web_user);
  761. $edit = array();
  762. $edit['subject'] = $this->randomName(8);
  763. $edit['comment_body[' . $langcode . '][0][value]'] = $this->randomName(16);
  764. $this->drupalPost('node/' . $this->node->nid, $edit, t('Preview'));
  765. // Check that the preview is displaying the title and body.
  766. $this->assertTitle(t('Preview comment | Drupal'), t('Page title is "Preview comment".'));
  767. $this->assertText($edit['subject'], t('Subject displayed.'));
  768. $this->assertText($edit['comment_body[' . $langcode . '][0][value]'], t('Comment displayed.'));
  769. // Check that the title and body fields are displayed with the correct values.
  770. $this->assertFieldByName('subject', $edit['subject'], t('Subject field displayed.'));
  771. $this->assertFieldByName('comment_body[' . $langcode . '][0][value]', $edit['comment_body[' . $langcode . '][0][value]'], t('Comment field displayed.'));
  772. }
  773. /**
  774. * Test comment edit, preview, and save.
  775. */
  776. function testCommentEditPreviewSave() {
  777. $langcode = LANGUAGE_NONE;
  778. $web_user = $this->drupalCreateUser(array('access comments', 'post comments', 'skip comment approval'));
  779. $this->drupalLogin($this->admin_user);
  780. $this->setCommentPreview(DRUPAL_OPTIONAL);
  781. $this->setCommentForm(TRUE);
  782. $this->setCommentSubject(TRUE);
  783. $this->setCommentSettings('comment_default_mode', COMMENT_MODE_THREADED, t('Comment paging changed.'));
  784. $edit = array();
  785. $edit['subject'] = $this->randomName(8);
  786. $edit['comment_body[' . $langcode . '][0][value]'] = $this->randomName(16);
  787. $edit['name'] = $web_user->name;
  788. $edit['date'] = '2008-03-02 17:23 +0300';
  789. $raw_date = strtotime($edit['date']);
  790. $expected_text_date = format_date($raw_date);
  791. $expected_form_date = format_date($raw_date, 'custom', 'Y-m-d H:i O');
  792. $comment = $this->postComment($this->node, $edit['subject'], $edit['comment_body[' . $langcode . '][0][value]'], TRUE);
  793. $this->drupalPost('comment/' . $comment->id . '/edit', $edit, t('Preview'));
  794. // Check that the preview is displaying the subject, comment, author and date correctly.
  795. $this->assertTitle(t('Preview comment | Drupal'), t('Page title is "Preview comment".'));
  796. $this->assertText($edit['subject'], t('Subject displayed.'));
  797. $this->assertText($edit['comment_body[' . $langcode . '][0][value]'], t('Comment displayed.'));
  798. $this->assertText($edit['name'], t('Author displayed.'));
  799. $this->assertText($expected_text_date, t('Date displayed.'));
  800. // Check that the subject, comment, author and date fields are displayed with the correct values.
  801. $this->assertFieldByName('subject', $edit['subject'], t('Subject field displayed.'));
  802. $this->assertFieldByName('comment_body[' . $langcode . '][0][value]', $edit['comment_body[' . $langcode . '][0][value]'], t('Comment field displayed.'));
  803. $this->assertFieldByName('name', $edit['name'], t('Author field displayed.'));
  804. $this->assertFieldByName('date', $edit['date'], t('Date field displayed.'));
  805. // Check that saving a comment produces a success message.
  806. $this->drupalPost('comment/' . $comment->id . '/edit', $edit, t('Save'));
  807. $this->assertText(t('Your comment has been posted.'), t('Comment posted.'));
  808. // Check that the comment fields are correct after loading the saved comment.
  809. $this->drupalGet('comment/' . $comment->id . '/edit');
  810. $this->assertFieldByName('subject', $edit['subject'], t('Subject field displayed.'));
  811. $this->assertFieldByName('comment_body[' . $langcode . '][0][value]', $edit['comment_body[' . $langcode . '][0][value]'], t('Comment field displayed.'));
  812. $this->assertFieldByName('name', $edit['name'], t('Author field displayed.'));
  813. $this->assertFieldByName('date', $expected_form_date, t('Date field displayed.'));
  814. // Submit the form using the displayed values.
  815. $displayed = array();
  816. $displayed['subject'] = (string) current($this->xpath("//input[@id='edit-subject']/@value"));
  817. $displayed['comment_body[' . $langcode . '][0][value]'] = (string) current($this->xpath("//textarea[@id='edit-comment-body-" . $langcode . "-0-value']"));
  818. $displayed['name'] = (string) current($this->xpath("//input[@id='edit-name']/@value"));
  819. $displayed['date'] = (string) current($this->xpath("//input[@id='edit-date']/@value"));
  820. $this->drupalPost('comment/' . $comment->id . '/edit', $displayed, t('Save'));
  821. // Check that the saved comment is still correct.
  822. $comment_loaded = comment_load($comment->id);
  823. $this->assertEqual($comment_loaded->subject, $edit['subject'], t('Subject loaded.'));
  824. $this->assertEqual($comment_loaded->comment_body[$langcode][0]['value'], $edit['comment_body[' . $langcode . '][0][value]'], t('Comment body loaded.'));
  825. $this->assertEqual($comment_loaded->name, $edit['name'], t('Name loaded.'));
  826. $this->assertEqual($comment_loaded->created, $raw_date, t('Date loaded.'));
  827. }
  828. }
  829. class CommentAnonymous extends CommentHelperCase {
  830. public static function getInfo() {
  831. return array(
  832. 'name' => 'Anonymous comments',
  833. 'description' => 'Test anonymous comments.',
  834. 'group' => 'Comment',
  835. );
  836. }
  837. function setUp() {
  838. parent::setUp();
  839. variable_set('user_register', USER_REGISTER_VISITORS);
  840. }
  841. /**
  842. * Test anonymous comment functionality.
  843. */
  844. function testAnonymous() {
  845. $this->drupalLogin($this->admin_user);
  846. // Enabled anonymous user comments.
  847. user_role_change_permissions(DRUPAL_ANONYMOUS_RID, array(
  848. 'access comments' => TRUE,
  849. 'post comments' => TRUE,
  850. 'skip comment approval' => TRUE,
  851. ));
  852. $this->setCommentAnonymous('0'); // Ensure that doesn't require contact info.
  853. $this->drupalLogout();
  854. // Post anonymous comment without contact info.
  855. $anonymous_comment1 = $this->postComment($this->node, $this->randomName(), $this->randomName());
  856. $this->assertTrue($this->commentExists($anonymous_comment1), t('Anonymous comment without contact info found.'));
  857. // Allow contact info.
  858. $this->drupalLogin($this->admin_user);
  859. $this->setCommentAnonymous('1');
  860. // Attempt to edit anonymous comment.
  861. $this->drupalGet('comment/' . $anonymous_comment1->id . '/edit');
  862. $edited_comment = $this->postComment(NULL, $this->randomName(), $this->randomName());
  863. $this->assertTrue($this->commentExists($edited_comment, FALSE), t('Modified reply found.'));
  864. $this->drupalLogout();
  865. // Post anonymous comment with contact info (optional).
  866. $this->drupalGet('comment/reply/' . $this->node->nid);
  867. $this->assertTrue($this->commentContactInfoAvailable(), t('Contact information available.'));
  868. $anonymous_comment2 = $this->postComment($this->node, $this->randomName(), $this->randomName());
  869. $this->assertTrue($this->commentExists($anonymous_comment2), t('Anonymous comment with contact info (optional) found.'));
  870. // Ensure anonymous users cannot post in the name of registered users.
  871. $langcode = LANGUAGE_NONE;
  872. $edit = array(
  873. 'name' => $this->admin_user->name,
  874. 'mail' => $this->randomName() . '@example.com',
  875. 'subject' => $this->randomName(),
  876. "comment_body[$langcode][0][value]" => $this->randomName(),
  877. );
  878. $this->drupalPost('comment/reply/' . $this->node->nid, $edit, t('Save'));
  879. $this->assertText(t('The name you used belongs to a registered user.'));
  880. // Require contact info.
  881. $this->drupalLogin($this->admin_user);
  882. $this->setCommentAnonymous('2');
  883. $this->drupalLogout();
  884. // Try to post comment with contact info (required).
  885. $this->drupalGet('comment/reply/' . $this->node->nid);
  886. $this->assertTrue($this->commentContactInfoAvailable(), t('Contact information available.'));
  887. $anonymous_comment3 = $this->postComment($this->node, $this->randomName(), $this->randomName(), TRUE);
  888. $this->assertText(t('E-mail field is required.'), t('E-mail required.')); // Name should have 'Anonymous' for value by default.
  889. $this->assertFalse($this->commentExists($anonymous_comment3), t('Anonymous comment with contact info (required) not found.'));
  890. // Post comment with contact info (required).
  891. $author_name = $this->randomName();
  892. $author_mail = $this->randomName() . '@example.com';
  893. $anonymous_comment3 = $this->postComment($this->node, $this->randomName(), $this->randomName(), array('name' => $author_name, 'mail' => $author_mail));
  894. $this->assertTrue($this->commentExists($anonymous_comment3), t('Anonymous comment with contact info (required) found.'));
  895. // Make sure the user data appears correctly when editing the comment.
  896. $this->drupalLogin($this->admin_user);
  897. $this->drupalGet('comment/' . $anonymous_comment3->id . '/edit');
  898. $this->assertRaw($author_name, t("The anonymous user's name is correct when editing the comment."));
  899. $this->assertRaw($author_mail, t("The anonymous user's e-mail address is correct when editing the comment."));
  900. // Unpublish comment.
  901. $this->performCommentOperation($anonymous_comment3, 'unpublish');
  902. $this->drupalGet('admin/content/comment/approval');
  903. $this->assertRaw('comments[' . $anonymous_comment3->id . ']', t('Comment was unpublished.'));
  904. // Publish comment.
  905. $this->performCommentOperation($anonymous_comment3, 'publish', TRUE);
  906. $this->drupalGet('admin/content/comment');
  907. $this->assertRaw('comments[' . $anonymous_comment3->id . ']', t('Comment was published.'));
  908. // Delete comment.
  909. $this->performCommentOperation($anonymous_comment3, 'delete');
  910. $this->drupalGet('admin/content/comment');
  911. $this->assertNoRaw('comments[' . $anonymous_comment3->id . ']', t('Comment was deleted.'));
  912. $this->drupalLogout();
  913. // Reset.
  914. user_role_change_permissions(DRUPAL_ANONYMOUS_RID, array(
  915. 'access comments' => FALSE,
  916. 'post comments' => FALSE,
  917. 'skip comment approval' => FALSE,
  918. ));
  919. // Attempt to view comments while disallowed.
  920. // NOTE: if authenticated user has permission to post comments, then a
  921. // "Login or register to post comments" type link may be shown.
  922. $this->drupalGet('node/' . $this->node->nid);
  923. $this->assertNoPattern('@<h2[^>]*>Comments</h2>@', t('Comments were not displayed.'));
  924. $this->assertNoLink('Add new comment', t('Link to add comment was found.'));
  925. // Attempt to view node-comment form while disallowed.
  926. $this->drupalGet('comment/reply/' . $this->node->nid);
  927. $this->assertText('You are not authorized to post comments', t('Error attempting to post comment.'));
  928. $this->assertNoFieldByName('subject', '', t('Subject field not found.'));
  929. $this->assertNoFieldByName("comment_body[$langcode][0][value]", '', t('Comment field not found.'));
  930. user_role_change_permissions(DRUPAL_ANONYMOUS_RID, array(
  931. 'access comments' => TRUE,
  932. 'post comments' => FALSE,
  933. 'skip comment approval' => FALSE,
  934. ));
  935. $this->drupalGet('node/' . $this->node->nid);
  936. $this->assertPattern('@<h2[^>]*>Comments</h2>@', t('Comments were displayed.'));
  937. $this->assertLink('Log in', 1, t('Link to log in was found.'));
  938. $this->assertLink('register', 1, t('Link to register was found.'));
  939. user_role_change_permissions(DRUPAL_ANONYMOUS_RID, array(
  940. 'access comments' => FALSE,
  941. 'post comments' => TRUE,
  942. 'skip comment approval' => TRUE,
  943. ));
  944. $this->drupalGet('node/' . $this->node->nid);
  945. $this->assertNoPattern('@<h2[^>]*>Comments</h2>@', t('Comments were not displayed.'));
  946. $this->assertFieldByName('subject', '', t('Subject field found.'));
  947. $this->assertFieldByName("comment_body[$langcode][0][value]", '', t('Comment field found.'));
  948. $this->drupalGet('comment/reply/' . $this->node->nid . '/' . $anonymous_comment3->id);
  949. $this->assertText('You are not authorized to view comments', t('Error attempting to post reply.'));
  950. $this->assertNoText($author_name, t('Comment not displayed.'));
  951. }
  952. }
  953. /**
  954. * Verify pagination of comments.
  955. */
  956. class CommentPagerTest extends CommentHelperCase {
  957. public static function getInfo() {
  958. return array(
  959. 'name' => 'Comment paging settings',
  960. 'description' => 'Test paging of comments and their settings.',
  961. 'group' => 'Comment',
  962. );
  963. }
  964. /**
  965. * Confirm comment paging works correctly with flat and threaded comments.
  966. */
  967. function testCommentPaging() {
  968. $this->drupalLogin($this->admin_user);
  969. // Set comment variables.
  970. $this->setCommentForm(TRUE);
  971. $this->setCommentSubject(TRUE);
  972. $this->setCommentPreview(DRUPAL_DISABLED);
  973. // Create a node and three comments.
  974. $node = $this->drupalCreateNode(array('type' => 'article', 'promote' => 1));
  975. $comments = array();
  976. $comments[] = $this->postComment($node, $this->randomName(), $this->randomName(), TRUE);
  977. $comments[] = $this->postComment($node, $this->randomName(), $this->randomName(), TRUE);
  978. $comments[] = $this->postComment($node, $this->randomName(), $this->randomName(), TRUE);
  979. $this->setCommentSettings('comment_default_mode', COMMENT_MODE_FLAT, t('Comment paging changed.'));
  980. // Set comments to one per page so that we are able to test paging without
  981. // needing to insert large numbers of comments.
  982. $this->setCommentsPerPage(1);
  983. // Check the first page of the node, and confirm the correct comments are
  984. // shown.
  985. $this->drupalGet('node/' . $node->nid);
  986. $this->assertRaw(t('next'), t('Paging links found.'));
  987. $this->assertTrue($this->commentExists($comments[0]), t('Comment 1 appears on page 1.'));
  988. $this->assertFalse($this->commentExists($comments[1]), t('Comment 2 does not appear on page 1.'));
  989. $this->assertFalse($this->commentExists($comments[2]), t('Comment 3 does not appear on page 1.'));
  990. // Check the second page.
  991. $this->drupalGet('node/' . $node->nid, array('query' => array('page' => 1)));
  992. $this->assertTrue($this->commentExists($comments[1]), t('Comment 2 appears on page 2.'));
  993. $this->assertFalse($this->commentExists($comments[0]), t('Comment 1 does not appear on page 2.'));
  994. $this->assertFalse($this->commentExists($comments[2]), t('Comment 3 does not appear on page 2.'));
  995. // Check the third page.
  996. $this->drupalGet('node/' . $node->nid, array('query' => array('page' => 2)));
  997. $this->assertTrue($this->commentExists($comments[2]), t('Comment 3 appears on page 3.'));
  998. $this->assertFalse($this->commentExists($comments[0]), t('Comment 1 does not appear on page 3.'));
  999. $this->assertFalse($this->commentExists($comments[1]), t('Comment 2 does not appear on page 3.'));
  1000. // Post a reply to the oldest comment and test again.
  1001. $replies = array();
  1002. $oldest_comment = reset($comments);
  1003. $this->drupalGet('comment/reply/' . $node->nid . '/' . $oldest_comment->id);
  1004. $reply = $this->postComment(NULL, $this->randomName(), $this->randomName(), TRUE);
  1005. $this->setCommentsPerPage(2);
  1006. // We are still in flat view - the replies should not be on the first page,
  1007. // even though they are replies to the oldest comment.
  1008. $this->drupalGet('node/' . $node->nid, array('query' => array('page' => 0)));
  1009. $this->assertFalse($this->commentExists($reply, TRUE), t('In flat mode, reply does not appear on page 1.'));
  1010. // If we switch to threaded mode, the replies on the oldest comment
  1011. // should be bumped to the first page and comment 6 should be bumped
  1012. // to the second page.
  1013. $this->setCommentSettings('comment_default_mode', COMMENT_MODE_THREADED, t('Switched to threaded mode.'));
  1014. $this->drupalGet('node/' . $node->nid, array('query' => array('page' => 0)));
  1015. $this->assertTrue($this->commentExists($reply, TRUE), t('In threaded mode, reply appears on page 1.'));
  1016. $this->assertFalse($this->commentExists($comments[1]), t('In threaded mode, comment 2 has been bumped off of page 1.'));
  1017. // If (# replies > # comments per page) in threaded expanded view,
  1018. // the overage should be bumped.
  1019. $reply2 = $this->postComment(NULL, $this->randomName(), $this->randomName(), TRUE);
  1020. $this->drupalGet('node/' . $node->nid, array('query' => array('page' => 0)));
  1021. $this->assertFalse($this->commentExists($reply2, TRUE), t('In threaded mode where # replies > # comments per page, the newest reply does not appear on page 1.'));
  1022. $this->drupalLogout();
  1023. }
  1024. /**
  1025. * Test comment ordering and threading.
  1026. */
  1027. function testCommentOrderingThreading() {
  1028. $this->drupalLogin($this->admin_user);
  1029. // Set comment variables.
  1030. $this->setCommentForm(TRUE);
  1031. $this->setCommentSubject(TRUE);
  1032. $this->setCommentPreview(DRUPAL_DISABLED);
  1033. // Display all the comments on the same page.
  1034. $this->setCommentsPerPage(1000);
  1035. // Create a node and three comments.
  1036. $node = $this->drupalCreateNode(array('type' => 'article', 'promote' => 1));
  1037. $comments = array();
  1038. $comments[] = $this->postComment($node, $this->randomName(), $this->randomName(), TRUE);
  1039. $comments[] = $this->postComment($node, $this->randomName(), $this->randomName(), TRUE);
  1040. $comments[] = $this->postComment($node, $this->randomName(), $this->randomName(), TRUE);
  1041. // Post a reply to the second comment.
  1042. $this->drupalGet('comment/reply/' . $node->nid . '/' . $comments[1]->id);
  1043. $comments[] = $this->postComment(NULL, $this->randomName(), $this->randomName(), TRUE);
  1044. // Post a reply to the first comment.
  1045. $this->drupalGet('comment/reply/' . $node->nid . '/' . $comments[0]->id);
  1046. $comments[] = $this->postComment(NULL, $this->randomName(), $this->randomName(), TRUE);
  1047. // Post a reply to the last comment.
  1048. $this->drupalGet('comment/reply/' . $node->nid . '/' . $comments[2]->id);
  1049. $comments[] = $this->postComment(NULL, $this->randomName(), $this->randomName(), TRUE);
  1050. // Post a reply to the second comment.
  1051. $this->drupalGet('comment/reply/' . $node->nid . '/' . $comments[3]->id);
  1052. $comments[] = $this->postComment(NULL, $this->randomName(), $this->randomName(), TRUE);
  1053. // At this point, the comment tree is:
  1054. // - 0
  1055. // - 4
  1056. // - 1
  1057. // - 3
  1058. // - 6
  1059. // - 2
  1060. // - 5
  1061. $this->setCommentSettings('comment_default_mode', COMMENT_MODE_FLAT, t('Comment paging changed.'));
  1062. $expected_order = array(
  1063. 0,
  1064. 1,
  1065. 2,
  1066. 3,
  1067. 4,
  1068. 5,
  1069. 6,
  1070. );
  1071. $this->drupalGet('node/' . $node->nid);
  1072. $this->assertCommentOrder($comments, $expected_order);
  1073. $this->setCommentSettings('comment_default_mode', COMMENT_MODE_THREADED, t('Switched to threaded mode.'));
  1074. $expected_order = array(
  1075. 0,
  1076. 4,
  1077. 1,
  1078. 3,
  1079. 6,
  1080. 2,
  1081. 5,
  1082. );
  1083. $this->drupalGet('node/' . $node->nid);
  1084. $this->assertCommentOrder($comments, $expected_order);
  1085. }
  1086. /**
  1087. * Helper function: assert that the comments are displayed in the correct order.
  1088. *
  1089. * @param $comments
  1090. * And array of comments.
  1091. * @param $expected_order
  1092. * An array of keys from $comments describing the expected order.
  1093. */
  1094. function assertCommentOrder(array $comments, array $expected_order) {
  1095. $expected_cids = array();
  1096. // First, rekey the expected order by cid.
  1097. foreach ($expected_order as $key) {
  1098. $expected_cids[] = $comments[$key]->id;
  1099. }
  1100. $comment_anchors = $this->xpath('//a[starts-with(@id,"comment-")]');
  1101. $result_order = array();
  1102. foreach ($comment_anchors as $anchor) {
  1103. $result_order[] = substr($anchor['id'], 8);
  1104. }
  1105. return $this->assertIdentical($expected_cids, $result_order, t('Comment order: expected @expected, returned @returned.', array('@expected' => implode(',', $expected_cids), '@returned' => implode(',', $result_order))));
  1106. }
  1107. /**
  1108. * Test comment_new_page_count().
  1109. */
  1110. function testCommentNewPageIndicator() {
  1111. $this->drupalLogin($this->admin_user);
  1112. // Set comment variables.
  1113. $this->setCommentForm(TRUE);
  1114. $this->setCommentSubject(TRUE);
  1115. $this->setCommentPreview(DRUPAL_DISABLED);
  1116. // Set comments to one per page so that we are able to test paging without
  1117. // needing to insert large numbers of comments.
  1118. $this->setCommentsPerPage(1);
  1119. // Create a node and three comments.
  1120. $node = $this->drupalCreateNode(array('type' => 'article', 'promote' => 1));
  1121. $comments = array();
  1122. $comments[] = $this->postComment($node, $this->randomName(), $this->randomName(), TRUE);
  1123. $comments[] = $this->postComment($node, $this->randomName(), $this->randomName(), TRUE);
  1124. $comments[] = $this->postComment($node, $this->randomName(), $this->randomName(), TRUE);
  1125. // Post a reply to the second comment.
  1126. $this->drupalGet('comment/reply/' . $node->nid . '/' . $comments[1]->id);
  1127. $comments[] = $this->postComment(NULL, $this->randomName(), $this->randomName(), TRUE);
  1128. // Post a reply to the first comment.
  1129. $this->drupalGet('comment/reply/' . $node->nid . '/' . $comments[0]->id);
  1130. $comments[] = $this->postComment(NULL, $this->randomName(), $this->randomName(), TRUE);
  1131. // Post a reply to the last comment.
  1132. $this->drupalGet('comment/reply/' . $node->nid . '/' . $comments[2]->id);
  1133. $comments[] = $this->postComment(NULL, $this->randomName(), $this->randomName(), TRUE);
  1134. // At this point, the comment tree is:
  1135. // - 0
  1136. // - 4
  1137. // - 1
  1138. // - 3
  1139. // - 2
  1140. // - 5
  1141. $this->setCommentSettings('comment_default_mode', COMMENT_MODE_FLAT, t('Comment paging changed.'));
  1142. $expected_pages = array(
  1143. 1 => 5, // Page of comment 5
  1144. 2 => 4, // Page of comment 4
  1145. 3 => 3, // Page of comment 3
  1146. 4 => 2, // Page of comment 2
  1147. 5 => 1, // Page of comment 1
  1148. 6 => 0, // Page of comment 0
  1149. );
  1150. $node = node_load($node->nid);
  1151. foreach ($expected_pages as $new_replies => $expected_page) {
  1152. $returned = comment_new_page_count($node->comment_count, $new_replies, $node);
  1153. $returned_page = is_array($returned) ? $returned['page'] : 0;
  1154. $this->assertIdentical($expected_page, $returned_page, t('Flat mode, @new replies: expected page @expected, returned page @returned.', array('@new' => $new_replies, '@expected' => $expected_page, '@returned' => $returned_page)));
  1155. }
  1156. $this->setCommentSettings('comment_default_mode', COMMENT_MODE_THREADED, t('Switched to threaded mode.'));
  1157. $expected_pages = array(
  1158. 1 => 5, // Page of comment 5
  1159. 2 => 1, // Page of comment 4
  1160. 3 => 1, // Page of comment 4
  1161. 4 => 1, // Page of comment 4
  1162. 5 => 1, // Page of comment 4
  1163. 6 => 0, // Page of comment 0
  1164. );
  1165. $node = node_load($node->nid);
  1166. foreach ($expected_pages as $new_replies => $expected_page) {
  1167. $returned = comment_new_page_count($node->comment_count, $new_replies, $node);
  1168. $returned_page = is_array($returned) ? $returned['page'] : 0;
  1169. $this->assertEqual($expected_page, $returned_page, t('Threaded mode, @new replies: expected page @expected, returned page @returned.', array('@new' => $new_replies, '@expected' => $expected_page, '@returned' => $returned_page)));
  1170. }
  1171. }
  1172. }
  1173. /**
  1174. * Tests comments with node access.
  1175. *
  1176. * See http://drupal.org/node/886752 -- verify there is no PostgreSQL error when
  1177. * viewing a node with threaded comments (a comment and a reply), if a node
  1178. * access module is in use.
  1179. */
  1180. class CommentNodeAccessTest extends CommentHelperCase {
  1181. public static function getInfo() {
  1182. return array(
  1183. 'name' => 'Comment node access',
  1184. 'description' => 'Test comment viewing with node access.',
  1185. 'group' => 'Comment',
  1186. );
  1187. }
  1188. function setUp() {
  1189. DrupalWebTestCase::setUp('comment', 'search', 'node_access_test');
  1190. node_access_rebuild();
  1191. // Create users and test node.
  1192. $this->admin_user = $this->drupalCreateUser(array('administer content types', 'administer comments', 'administer blocks'));
  1193. $this->web_user = $this->drupalCreateUser(array('access comments', 'post comments', 'create article content', 'edit own comments', 'node test view'));
  1194. $this->node = $this->drupalCreateNode(array('type' => 'article', 'promote' => 1, 'uid' => $this->web_user->uid));
  1195. }
  1196. /**
  1197. * Test that threaded comments can be viewed.
  1198. */
  1199. function testThreadedCommentView() {
  1200. $langcode = LANGUAGE_NONE;
  1201. // Set comments to have subject required and preview disabled.
  1202. $this->drupalLogin($this->admin_user);
  1203. $this->setCommentPreview(DRUPAL_DISABLED);
  1204. $this->setCommentForm(TRUE);
  1205. $this->setCommentSubject(TRUE);
  1206. $this->setCommentSettings('comment_default_mode', COMMENT_MODE_THREADED, t('Comment paging changed.'));
  1207. $this->drupalLogout();
  1208. // Post comment.
  1209. $this->drupalLogin($this->web_user);
  1210. $comment_text = $this->randomName();
  1211. $comment_subject = $this->randomName();
  1212. $comment = $this->postComment($this->node, $comment_text, $comment_subject);
  1213. $comment_loaded = comment_load($comment->id);
  1214. $this->assertTrue($this->commentExists($comment), t('Comment found.'));
  1215. // Check comment display.
  1216. $this->drupalGet('node/' . $this->node->nid . '/' . $comment->id);
  1217. $this->assertText($comment_subject, t('Individual comment subject found.'));
  1218. $this->assertText($comment_text, t('Individual comment body found.'));
  1219. // Reply to comment, creating second comment.
  1220. $this->drupalGet('comment/reply/' . $this->node->nid . '/' . $comment->id);
  1221. $reply_text = $this->randomName();
  1222. $reply_subject = $this->randomName();
  1223. $reply = $this->postComment(NULL, $reply_text, $reply_subject, TRUE);
  1224. $reply_loaded = comment_load($reply->id);
  1225. $this->assertTrue($this->commentExists($reply, TRUE), t('Reply found.'));
  1226. // Go to the node page and verify comment and reply are visible.
  1227. $this->drupalGet('node/' . $this->node->nid);
  1228. $this->assertText($comment_text);
  1229. $this->assertText($comment_subject);
  1230. $this->assertText($reply_text);
  1231. $this->assertText($reply_subject);
  1232. }
  1233. }
  1234. class CommentApprovalTest extends CommentHelperCase {
  1235. public static function getInfo() {
  1236. return array(
  1237. 'name' => 'Comment approval',
  1238. 'description' => 'Test comment approval functionality.',
  1239. 'group' => 'Comment',
  1240. );
  1241. }
  1242. /**
  1243. * Test comment approval functionality through admin/content/comment.
  1244. */
  1245. function testApprovalAdminInterface() {
  1246. // Set anonymous comments to require approval.
  1247. user_role_change_permissions(DRUPAL_ANONYMOUS_RID, array(
  1248. 'access comments' => TRUE,
  1249. 'post comments' => TRUE,
  1250. 'skip comment approval' => FALSE,
  1251. ));
  1252. $this->drupalLogin($this->admin_user);
  1253. $this->setCommentAnonymous('0'); // Ensure that doesn't require contact info.
  1254. // Test that the comments page loads correctly when there are no comments
  1255. $this->drupalGet('admin/content/comment');
  1256. $this->assertText(t('No comments available.'));
  1257. $this->drupalLogout();
  1258. // Post anonymous comment without contact info.
  1259. $subject = $this->randomName();
  1260. $body = $this->randomName();
  1261. $this->postComment($this->node, $body, $subject, TRUE); // Set $contact to true so that it won't check for id and message.
  1262. $this->assertText(t('Your comment has been queued for review by site administrators and will be published after approval.'), t('Comment requires approval.'));
  1263. // Get unapproved comment id.
  1264. $this->drupalLogin($this->admin_user);
  1265. $anonymous_comment4 = $this->getUnapprovedComment($subject);
  1266. $anonymous_comment4 = (object) array('id' => $anonymous_comment4, 'subject' => $subject, 'comment' => $body);
  1267. $this->drupalLogout();
  1268. $this->assertFalse($this->commentExists($anonymous_comment4), t('Anonymous comment was not published.'));
  1269. // Approve comment.
  1270. $this->drupalLogin($this->admin_user);
  1271. $this->performCommentOperation($anonymous_comment4, 'publish', TRUE);
  1272. $this->drupalLogout();
  1273. $this->drupalGet('node/' . $this->node->nid);
  1274. $this->assertTrue($this->commentExists($anonymous_comment4), t('Anonymous comment visible.'));
  1275. // Post 2 anonymous comments without contact info.
  1276. $comments[] = $this->postComment($this->node, $this->randomName(), $this->randomName(), TRUE);
  1277. $comments[] = $this->postComment($this->node, $this->randomName(), $this->randomName(), TRUE);
  1278. // Publish multiple comments in one operation.
  1279. $this->drupalLogin($this->admin_user);
  1280. $this->drupalGet('admin/content/comment/approval');
  1281. $this->assertText(t('Unapproved comments (@count)', array('@count' => 2)), t('Two unapproved comments waiting for approval.'));
  1282. $edit = array(
  1283. "comments[{$comments[0]->id}]" => 1,
  1284. "comments[{$comments[1]->id}]" => 1,
  1285. );
  1286. $this->drupalPost(NULL, $edit, t('Update'));
  1287. $this->assertText(t('Unapproved comments (@count)', array('@count' => 0)), t('All comments were approved.'));
  1288. // Delete multiple comments in one operation.
  1289. $edit = array(
  1290. 'operation' => 'delete',
  1291. "comments[{$comments[0]->id}]" => 1,
  1292. "comments[{$comments[1]->id}]" => 1,
  1293. "comments[{$anonymous_comment4->id}]" => 1,
  1294. );
  1295. $this->drupalPost(NULL, $edit, t('Update'));
  1296. $this->assertText(t('Are you sure you want to delete these comments and all their children?'), t('Confirmation required.'));
  1297. $this->drupalPost(NULL, $edit, t('Delete comments'));
  1298. $this->assertText(t('No comments available.'), t('All comments were deleted.'));
  1299. }
  1300. /**
  1301. * Test comment approval functionality through node interface.
  1302. */
  1303. function testApprovalNodeInterface() {
  1304. // Set anonymous comments to require approval.
  1305. user_role_change_permissions(DRUPAL_ANONYMOUS_RID, array(
  1306. 'access comments' => TRUE,
  1307. 'post comments' => TRUE,
  1308. 'skip comment approval' => FALSE,
  1309. ));
  1310. $this->drupalLogin($this->admin_user);
  1311. $this->setCommentAnonymous('0'); // Ensure that doesn't require contact info.
  1312. $this->drupalLogout();
  1313. // Post anonymous comment without contact info.
  1314. $subject = $this->randomName();
  1315. $body = $this->randomName();
  1316. $this->postComment($this->node, $body, $subject, TRUE); // Set $contact to true so that it won't check for id and message.
  1317. $this->assertText(t('Your comment has been queued for review by site administrators and will be published after approval.'), t('Comment requires approval.'));
  1318. // Get unapproved comment id.
  1319. $this->drupalLogin($this->admin_user);
  1320. $anonymous_comment4 = $this->getUnapprovedComment($subject);
  1321. $anonymous_comment4 = (object) array('id' => $anonymous_comment4, 'subject' => $subject, 'comment' => $body);
  1322. $this->drupalLogout();
  1323. $this->assertFalse($this->commentExists($anonymous_comment4), t('Anonymous comment was not published.'));
  1324. // Approve comment.
  1325. $this->drupalLogin($this->admin_user);
  1326. $this->drupalGet('comment/1/approve');
  1327. $this->assertResponse(403, t('Forged comment approval was denied.'));
  1328. $this->drupalGet('comment/1/approve', array('query' => array('token' => 'forged')));
  1329. $this->assertResponse(403, t('Forged comment approval was denied.'));
  1330. $this->drupalGet('node/' . $this->node->nid);
  1331. $this->clickLink(t('approve'));
  1332. $this->drupalLogout();
  1333. $this->drupalGet('node/' . $this->node->nid);
  1334. $this->assertTrue($this->commentExists($anonymous_comment4), t('Anonymous comment visible.'));
  1335. }
  1336. }
  1337. /**
  1338. * Functional tests for the comment module blocks.
  1339. */
  1340. class CommentBlockFunctionalTest extends CommentHelperCase {
  1341. public static function getInfo() {
  1342. return array(
  1343. 'name' => 'Comment blocks',
  1344. 'description' => 'Test comment block functionality.',
  1345. 'group' => 'Comment',
  1346. );
  1347. }
  1348. /**
  1349. * Test the recent comments block.
  1350. */
  1351. function testRecentCommentBlock() {
  1352. $this->drupalLogin($this->admin_user);
  1353. // Set the block to a region to confirm block is available.
  1354. $edit = array(
  1355. 'blocks[comment_recent][region]' => 'sidebar_first',
  1356. );
  1357. $this->drupalPost('admin/structure/block', $edit, t('Save blocks'));
  1358. $this->assertText(t('The block settings have been updated.'), t('Block saved to first sidebar region.'));
  1359. // Set block title and variables.
  1360. $block = array(
  1361. 'title' => $this->randomName(),
  1362. 'comment_block_count' => 2,
  1363. );
  1364. $this->drupalPost('admin/structure/block/manage/comment/recent/configure', $block, t('Save block'));
  1365. $this->assertText(t('The block configuration has been saved.'), t('Block saved.'));
  1366. // Add some test comments, one without a subject.
  1367. $comment1 = $this->postComment($this->node, $this->randomName(), $this->randomName());
  1368. $comment2 = $this->postComment($this->node, $this->randomName(), $this->randomName());
  1369. $comment3 = $this->postComment($this->node, $this->randomName());
  1370. // Test that a user without the 'access comments' permission cannot see the
  1371. // block.
  1372. $this->drupalLogout();
  1373. user_role_revoke_permissions(DRUPAL_ANONYMOUS_RID, array('access comments'));
  1374. $this->drupalGet('');
  1375. $this->assertNoText($block['title'], t('Block was not found.'));
  1376. user_role_grant_permissions(DRUPAL_ANONYMOUS_RID, array('access comments'));
  1377. // Test that a user with the 'access comments' permission can see the
  1378. // block.
  1379. $this->drupalLogin($this->web_user);
  1380. $this->drupalGet('');
  1381. $this->assertText($block['title'], t('Block was found.'));
  1382. // Test the only the 2 latest comments are shown and in the proper order.
  1383. $this->assertNoText($comment1->subject, t('Comment not found in block.'));
  1384. $this->assertText($comment2->subject, t('Comment found in block.'));
  1385. $this->assertText($comment3->comment, t('Comment found in block.'));
  1386. $this->assertTrue(strpos($this->drupalGetContent(), $comment3->comment) < strpos($this->drupalGetContent(), $comment2->subject), t('Comments were ordered correctly in block.'));
  1387. // Set the number of recent comments to show to 10.
  1388. $this->drupalLogout();
  1389. $this->drupalLogin($this->admin_user);
  1390. $block = array(
  1391. 'comment_block_count' => 10,
  1392. );
  1393. $this->drupalPost('admin/structure/block/manage/comment/recent/configure', $block, t('Save block'));
  1394. $this->assertText(t('The block configuration has been saved.'), t('Block saved.'));
  1395. // Post an additional comment.
  1396. $comment4 = $this->postComment($this->node, $this->randomName(), $this->randomName());
  1397. // Test that all four comments are shown.
  1398. $this->assertText($comment1->subject, t('Comment found in block.'));
  1399. $this->assertText($comment2->subject, t('Comment found in block.'));
  1400. $this->assertText($comment3->comment, t('Comment found in block.'));
  1401. $this->assertText($comment4->subject, t('Comment found in block.'));
  1402. // Test that links to comments work when comments are across pages.
  1403. $this->setCommentsPerPage(1);
  1404. $this->drupalGet('');
  1405. $this->clickLink($comment1->subject);
  1406. $this->assertText($comment1->subject, t('Comment link goes to correct page.'));
  1407. $this->drupalGet('');
  1408. $this->clickLink($comment2->subject);
  1409. $this->assertText($comment2->subject, t('Comment link goes to correct page.'));
  1410. $this->clickLink($comment4->subject);
  1411. $this->assertText($comment4->subject, t('Comment link goes to correct page.'));
  1412. // Check that when viewing a comment page from a link to the comment, that
  1413. // rel="canonical" is added to the head of the document.
  1414. $this->assertRaw('<link rel="canonical"', t('Canonical URL was found in the HTML head'));
  1415. }
  1416. }
  1417. /**
  1418. * Unit tests for comment module integration with RSS feeds.
  1419. */
  1420. class CommentRSSUnitTest extends CommentHelperCase {
  1421. public static function getInfo() {
  1422. return array(
  1423. 'name' => 'Comment RSS',
  1424. 'description' => 'Test comments as part of an RSS feed.',
  1425. 'group' => 'Comment',
  1426. );
  1427. }
  1428. /**
  1429. * Test comments as part of an RSS feed.
  1430. */
  1431. function testCommentRSS() {
  1432. // Find comment in RSS feed.
  1433. $this->drupalLogin($this->web_user);
  1434. $comment = $this->postComment($this->node, $this->randomName(), $this->randomName());
  1435. $this->drupalGet('rss.xml');
  1436. $raw = '<comments>' . url('node/' . $this->node->nid, array('fragment' => 'comments', 'absolute' => TRUE)) . '</comments>';
  1437. $this->assertRaw($raw, t('Comments as part of RSS feed.'));
  1438. // Hide comments from RSS feed and check presence.
  1439. $this->node->comment = COMMENT_NODE_HIDDEN;
  1440. node_save($this->node);
  1441. $this->drupalGet('rss.xml');
  1442. $this->assertNoRaw($raw, t('Hidden comments is not a part of RSS feed.'));
  1443. }
  1444. }
  1445. /**
  1446. * Test to make sure comment content is rebuilt.
  1447. */
  1448. class CommentContentRebuild extends CommentHelperCase {
  1449. public static function getInfo() {
  1450. return array(
  1451. 'name' => 'Comment Rebuild',
  1452. 'description' => 'Test to make sure the comment content is rebuilt.',
  1453. 'group' => 'Comment',
  1454. );
  1455. }
  1456. /**
  1457. * Test to ensure that the comment's content array is rebuilt for every
  1458. * call to comment_view().
  1459. */
  1460. function testCommentRebuild() {
  1461. // Update the comment settings so preview isn't required.
  1462. $this->drupalLogin($this->admin_user);
  1463. $this->setCommentSubject(TRUE);
  1464. $this->setCommentPreview(DRUPAL_OPTIONAL);
  1465. $this->drupalLogout();
  1466. // Log in as the web user and add the comment.
  1467. $this->drupalLogin($this->web_user);
  1468. $subject_text = $this->randomName();
  1469. $comment_text = $this->randomName();
  1470. $comment = $this->postComment($this->node, $comment_text, $subject_text, TRUE);
  1471. $comment_loaded = comment_load($comment->id);
  1472. $this->assertTrue($this->commentExists($comment), t('Comment found.'));
  1473. // Add the property to the content array and then see if it still exists on build.
  1474. $comment_loaded->content['test_property'] = array('#value' => $this->randomString());
  1475. $built_content = comment_view($comment_loaded, $this->node);
  1476. // This means that the content was rebuilt as the added test property no longer exists.
  1477. $this->assertFalse(isset($built_content['test_property']), t('Comment content was emptied before being built.'));
  1478. }
  1479. }
  1480. /**
  1481. * Test comment token replacement in strings.
  1482. */
  1483. class CommentTokenReplaceTestCase extends CommentHelperCase {
  1484. public static function getInfo() {
  1485. return array(
  1486. 'name' => 'Comment token replacement',
  1487. 'description' => 'Generates text using placeholders for dummy content to check comment token replacement.',
  1488. 'group' => 'Comment',
  1489. );
  1490. }
  1491. /**
  1492. * Creates a comment, then tests the tokens generated from it.
  1493. */
  1494. function testCommentTokenReplacement() {
  1495. global $language;
  1496. $url_options = array(
  1497. 'absolute' => TRUE,
  1498. 'language' => $language,
  1499. );
  1500. $this->drupalLogin($this->admin_user);
  1501. // Set comment variables.
  1502. $this->setCommentSubject(TRUE);
  1503. // Create a node and a comment.
  1504. $node = $this->drupalCreateNode(array('type' => 'article'));
  1505. $parent_comment = $this->postComment($node, $this->randomName(), $this->randomName(), TRUE);
  1506. // Post a reply to the comment.
  1507. $this->drupalGet('comment/reply/' . $node->nid . '/' . $parent_comment->id);
  1508. $child_comment = $this->postComment(NULL, $this->randomName(), $this->randomName());
  1509. $comment = comment_load($child_comment->id);
  1510. $comment->homepage = 'http://example.org/';
  1511. // Add HTML to ensure that sanitation of some fields tested directly.
  1512. $comment->subject = '<blink>Blinking Comment</blink>';
  1513. $instance = field_info_instance('comment', 'body', 'comment_body');
  1514. // Generate and test sanitized tokens.
  1515. $tests = array();
  1516. $tests['[comment:cid]'] = $comment->cid;
  1517. $tests['[comment:hostname]'] = check_plain($comment->hostname);
  1518. $tests['[comment:name]'] = filter_xss($comment->name);
  1519. $tests['[comment:mail]'] = check_plain($this->admin_user->mail);
  1520. $tests['[comment:homepage]'] = check_url($comment->homepage);
  1521. $tests['[comment:title]'] = filter_xss($comment->subject);
  1522. $tests['[comment:body]'] = _text_sanitize($instance, LANGUAGE_NONE, $comment->comment_body[LANGUAGE_NONE][0], 'value');
  1523. $tests['[comment:url]'] = url('comment/' . $comment->cid, $url_options + array('fragment' => 'comment-' . $comment->cid));
  1524. $tests['[comment:edit-url]'] = url('comment/' . $comment->cid . '/edit', $url_options);
  1525. $tests['[comment:created:since]'] = format_interval(REQUEST_TIME - $comment->created, 2, $language->language);
  1526. $tests['[comment:changed:since]'] = format_interval(REQUEST_TIME - $comment->changed, 2, $language->language);
  1527. $tests['[comment:parent:cid]'] = $comment->pid;
  1528. $tests['[comment:parent:title]'] = check_plain($parent_comment->subject);
  1529. $tests['[comment:node:nid]'] = $comment->nid;
  1530. $tests['[comment:node:title]'] = check_plain($node->title);
  1531. $tests['[comment:author:uid]'] = $comment->uid;
  1532. $tests['[comment:author:name]'] = check_plain($this->admin_user->name);
  1533. // Test to make sure that we generated something for each token.
  1534. $this->assertFalse(in_array(0, array_map('strlen', $tests)), t('No empty tokens generated.'));
  1535. foreach ($tests as $input => $expected) {
  1536. $output = token_replace($input, array('comment' => $comment), array('language' => $language));
  1537. $this->assertEqual($output, $expected, t('Sanitized comment token %token replaced.', array('%token' => $input)));
  1538. }
  1539. // Generate and test unsanitized tokens.
  1540. $tests['[comment:hostname]'] = $comment->hostname;
  1541. $tests['[comment:name]'] = $comment->name;
  1542. $tests['[comment:mail]'] = $this->admin_user->mail;
  1543. $tests['[comment:homepage]'] = $comment->homepage;
  1544. $tests['[comment:title]'] = $comment->subject;
  1545. $tests['[comment:body]'] = $comment->comment_body[LANGUAGE_NONE][0]['value'];
  1546. $tests['[comment:parent:title]'] = $parent_comment->subject;
  1547. $tests['[comment:node:title]'] = $node->title;
  1548. $tests['[comment:author:name]'] = $this->admin_user->name;
  1549. foreach ($tests as $input => $expected) {
  1550. $output = token_replace($input, array('comment' => $comment), array('language' => $language, 'sanitize' => FALSE));
  1551. $this->assertEqual($output, $expected, t('Unsanitized comment token %token replaced.', array('%token' => $input)));
  1552. }
  1553. // Load node so comment_count gets computed.
  1554. $node = node_load($node->nid);
  1555. // Generate comment tokens for the node (it has 2 comments, both new).
  1556. $tests = array();
  1557. $tests['[node:comment-count]'] = 2;
  1558. $tests['[node:comment-count-new]'] = 2;
  1559. foreach ($tests as $input => $expected) {
  1560. $output = token_replace($input, array('node' => $node), array('language' => $language));
  1561. $this->assertEqual($output, $expected, t('Node comment token %token replaced.', array('%token' => $input)));
  1562. }
  1563. }
  1564. }
  1565. /**
  1566. * Test actions provided by the comment module.
  1567. */
  1568. class CommentActionsTestCase extends CommentHelperCase {
  1569. public static function getInfo() {
  1570. return array(
  1571. 'name' => 'Comment actions',
  1572. 'description' => 'Test actions provided by the comment module.',
  1573. 'group' => 'Comment',
  1574. );
  1575. }
  1576. /**
  1577. * Test comment publish and unpublish actions.
  1578. */
  1579. function testCommentPublishUnpublishActions() {
  1580. $this->drupalLogin($this->web_user);
  1581. $comment_text = $this->randomName();
  1582. $subject = $this->randomName();
  1583. $comment = $this->postComment($this->node, $comment_text, $subject);
  1584. $comment = comment_load($comment->id);
  1585. // Unpublish a comment (direct form: doesn't actually save the comment).
  1586. comment_unpublish_action($comment);
  1587. $this->assertEqual($comment->status, COMMENT_NOT_PUBLISHED, t('Comment was unpublished'));
  1588. $this->assertWatchdogMessage('Unpublished comment %subject.', array('%subject' => $subject), t('Found watchdog message'));
  1589. $this->clearWatchdog();
  1590. // Unpublish a comment (indirect form: modify the comment in the database).
  1591. comment_unpublish_action(NULL, array('cid' => $comment->cid));
  1592. $this->assertEqual(comment_load($comment->cid)->status, COMMENT_NOT_PUBLISHED, t('Comment was unpublished'));
  1593. $this->assertWatchdogMessage('Unpublished comment %subject.', array('%subject' => $subject), t('Found watchdog message'));
  1594. // Publish a comment (direct form: doesn't actually save the comment).
  1595. comment_publish_action($comment);
  1596. $this->assertEqual($comment->status, COMMENT_PUBLISHED, t('Comment was published'));
  1597. $this->assertWatchdogMessage('Published comment %subject.', array('%subject' => $subject), t('Found watchdog message'));
  1598. $this->clearWatchdog();
  1599. // Publish a comment (indirect form: modify the comment in the database).
  1600. comment_publish_action(NULL, array('cid' => $comment->cid));
  1601. $this->assertEqual(comment_load($comment->cid)->status, COMMENT_PUBLISHED, t('Comment was published'));
  1602. $this->assertWatchdogMessage('Published comment %subject.', array('%subject' => $subject), t('Found watchdog message'));
  1603. $this->clearWatchdog();
  1604. }
  1605. /**
  1606. * Verify that a watchdog message has been entered.
  1607. *
  1608. * @param $watchdog_message
  1609. * The watchdog message.
  1610. * @param $variables
  1611. * The array of variables passed to watchdog().
  1612. * @param $message
  1613. * The assertion message.
  1614. */
  1615. function assertWatchdogMessage($watchdog_message, $variables, $message) {
  1616. $status = (bool) db_query_range("SELECT 1 FROM {watchdog} WHERE message = :message AND variables = :variables", 0, 1, array(':message' => $watchdog_message, ':variables' => serialize($variables)))->fetchField();
  1617. return $this->assert($status, $message);
  1618. }
  1619. /**
  1620. * Helper function: clear the watchdog.
  1621. */
  1622. function clearWatchdog() {
  1623. db_truncate('watchdog')->execute();
  1624. }
  1625. }
  1626. /**
  1627. * Test fields on comments.
  1628. */
  1629. class CommentFieldsTest extends CommentHelperCase {
  1630. public static function getInfo() {
  1631. return array(
  1632. 'name' => 'Comment fields',
  1633. 'description' => 'Tests fields on comments.',
  1634. 'group' => 'Comment',
  1635. );
  1636. }
  1637. /**
  1638. * Tests that the default 'comment_body' field is correctly added.
  1639. */
  1640. function testCommentDefaultFields() {
  1641. // Do not make assumptions on default node types created by the test
  1642. // install profile, and create our own.
  1643. $this->drupalCreateContentType(array('type' => 'test_node_type'));
  1644. // Check that the 'comment_body' field is present on all comment bundles.
  1645. $instances = field_info_instances('comment');
  1646. foreach (node_type_get_types() as $type_name => $info) {
  1647. $this->assertTrue(isset($instances['comment_node_' . $type_name]['comment_body']), t('The comment_body field is present for comments on type @type', array('@type' => $type_name)));
  1648. // Delete the instance along the way.
  1649. field_delete_instance($instances['comment_node_' . $type_name]['comment_body']);
  1650. }
  1651. // Check that the 'comment_body' field is deleted.
  1652. $field = field_info_field('comment_body');
  1653. $this->assertTrue(empty($field), t('The comment_body field was deleted'));
  1654. // Create a new content type.
  1655. $type_name = 'test_node_type_2';
  1656. $this->drupalCreateContentType(array('type' => $type_name));
  1657. // Check that the 'comment_body' field exists and has an instance on the
  1658. // new comment bundle.
  1659. $field = field_info_field('comment_body');
  1660. $this->assertTrue($field, t('The comment_body field exists'));
  1661. $instances = field_info_instances('comment');
  1662. $this->assertTrue(isset($instances['comment_node_' . $type_name]['comment_body']), t('The comment_body field is present for comments on type @type', array('@type' => $type_name)));
  1663. }
  1664. /**
  1665. * Test that comment module works when enabled after a content module.
  1666. */
  1667. function testCommentEnable() {
  1668. // Create a user to do module administration.
  1669. $this->admin_user = $this->drupalCreateUser(array('access administration pages', 'administer modules'));
  1670. $this->drupalLogin($this->admin_user);
  1671. // Disable the comment module.
  1672. $edit = array();
  1673. $edit['modules[Core][comment][enable]'] = FALSE;
  1674. $this->drupalPost('admin/modules', $edit, t('Save configuration'));
  1675. $this->resetAll();
  1676. $this->assertFalse(module_exists('comment'), t('Comment module disabled.'));
  1677. // Enable core content type modules (blog, book, and poll).
  1678. $edit = array();
  1679. $edit['modules[Core][blog][enable]'] = 'blog';
  1680. $edit['modules[Core][book][enable]'] = 'book';
  1681. $edit['modules[Core][poll][enable]'] = 'poll';
  1682. $this->drupalPost('admin/modules', $edit, t('Save configuration'));
  1683. $this->resetAll();
  1684. // Now enable the comment module.
  1685. $edit = array();
  1686. $edit['modules[Core][comment][enable]'] = 'comment';
  1687. $this->drupalPost('admin/modules', $edit, t('Save configuration'));
  1688. $this->resetAll();
  1689. $this->assertTrue(module_exists('comment'), t('Comment module enabled.'));
  1690. // Create nodes of each type.
  1691. $blog_node = $this->drupalCreateNode(array('type' => 'blog'));
  1692. $book_node = $this->drupalCreateNode(array('type' => 'book'));
  1693. $poll_node = $this->drupalCreateNode(array('type' => 'poll', 'active' => 1, 'runtime' => 0, 'choice' => array(array('chtext' => ''))));
  1694. $this->drupalLogout();
  1695. // Try to post a comment on each node. A failure will be triggered if the
  1696. // comment body is missing on one of these forms, due to postComment()
  1697. // asserting that the body is actually posted correctly.
  1698. $this->web_user = $this->drupalCreateUser(array('access content', 'access comments', 'post comments', 'skip comment approval'));
  1699. $this->drupalLogin($this->web_user);
  1700. $this->postComment($blog_node, $this->randomName(), $this->randomName());
  1701. $this->postComment($book_node, $this->randomName(), $this->randomName());
  1702. $this->postComment($poll_node, $this->randomName(), $this->randomName());
  1703. }
  1704. /**
  1705. * Test that comment module works correctly with plain text format.
  1706. */
  1707. function testCommentFormat() {
  1708. // Disable text processing for comments.
  1709. $this->drupalLogin($this->admin_user);
  1710. $edit = array('instance[settings][text_processing]' => 0);
  1711. $this->drupalPost('admin/structure/types/manage/article/comment/fields/comment_body', $edit, t('Save settings'));
  1712. // Post a comment without an explicit subject.
  1713. $this->drupalLogin($this->web_user);
  1714. $edit = array('comment_body[und][0][value]' => $this->randomName(8));
  1715. $this->drupalPost('node/' . $this->node->nid, $edit, t('Save'));
  1716. }
  1717. }