comment.test 101 KB

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