comment.test 94 KB

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