comment.test 96 KB

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