comment.test 93 KB

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