comment.test 94 KB

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