comment.test 93 KB

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