/[drupal]/drupal/modules/comment/comment.test
ViewVC logotype

Contents of /drupal/modules/comment/comment.test

Parent Directory Parent Directory | Revision Log Revision Log | View Revision Graph Revision Graph


Revision 1.52 - (show annotations) (download) (as text)
Tue Oct 20 17:33:42 2009 UTC (5 weeks, 2 days ago) by webchick
Branch: MAIN
CVS Tags: DRUPAL-7-0-UNSTABLE-10
Changes since 1.51: +5 -4 lines
File MIME type: text/x-php
#493030 follow-up by sun: Clean-ups to RDF module.
1 <?php
2 // $Id: comment.test,v 1.51 2009/10/19 18:28:15 dries Exp $
3
4 class CommentHelperCase extends DrupalWebTestCase {
5 protected $admin_user;
6 protected $web_user;
7 protected $node;
8
9 function setUp() {
10 parent::setUp('comment', 'search');
11 // Create users and test node.
12 $this->admin_user = $this->drupalCreateUser(array('administer content types', 'administer comments', 'administer blocks'));
13 $this->web_user = $this->drupalCreateUser(array('access comments', 'post comments', 'create article content'));
14 $this->node = $this->drupalCreateNode(array('type' => 'article', 'promote' => 1, 'uid' => $this->web_user->uid));
15 }
16
17 /**
18 * Post comment.
19 *
20 * @param object $node Node to post comment on.
21 * @param string $comment Comment body.
22 * @param string $subject Comment subject.
23 * @param mixed $contact Set to NULL for no contact info, TRUE to ignore success checking, and array of values to set contact info.
24 */
25 function postComment($node, $comment, $subject = '', $contact = NULL) {
26 $edit = array();
27 $edit['comment'] = $comment;
28
29 $preview_mode = variable_get('comment_preview_article', DRUPAL_OPTIONAL);
30 $subject_mode = variable_get('comment_subject_field_article', 1);
31
32 // Must get the page before we test for fields.
33 if ($node !== NULL) {
34 $this->drupalGet('comment/reply/' . $node->nid);
35 }
36
37 if ($subject_mode == TRUE) {
38 $edit['subject'] = $subject;
39 }
40 else {
41 $this->assertNoFieldByName('subject', '', t('Subject field not found.'));
42 }
43
44 if ($contact !== NULL && is_array($contact)) {
45 $edit += $contact;
46 }
47 switch ($preview_mode) {
48 case DRUPAL_REQUIRED:
49 // Preview required so no save button should be found.
50 $this->assertNoFieldByName('op', t('Save'), t('Save button not found.'));
51 $this->drupalPost(NULL, $edit, t('Preview'));
52 // Don't break here so that we can test post-preview field presence and
53 // function below.
54 case DRUPAL_OPTIONAL:
55 $this->assertFieldByName('op', t('Preview'), t('Preview button found.'));
56 $this->assertFieldByName('op', t('Save'), t('Save button found.'));
57 $this->drupalPost(NULL, $edit, t('Save'));
58 break;
59
60 case DRUPAL_DISABLED:
61 $this->assertNoFieldByName('op', t('Preview'), t('Preview button not found.'));
62 $this->assertFieldByName('op', t('Save'), t('Save button found.'));
63 $this->drupalPost(NULL, $edit, t('Save'));
64 break;
65 }
66 $match = array();
67 // Get comment ID
68 preg_match('/#comment-([0-9]+)/', $this->getURL(), $match);
69
70 // Get comment.
71 if ($contact !== TRUE) { // If true then attempting to find error message.
72 if ($subject) {
73 $this->assertText($subject, 'Comment subject posted.');
74 }
75 $this->assertText($comment, 'Comment body posted.');
76 $this->assertTrue((!empty($match) && !empty($match[1])), t('Comment id found.'));
77 }
78
79 if (isset($match[1])) {
80 return (object) array('id' => $match[1], 'subject' => $subject, 'comment' => $comment);
81 }
82 }
83
84 /**
85 * Checks current page for specified comment.
86 *
87 * @param object $comment Comment object.
88 * @param boolean $reply The comment is a reply to another comment.
89 * @return boolean Comment found.
90 */
91 function commentExists($comment, $reply = FALSE) {
92 if ($comment && is_object($comment)) {
93 $regex = '/' . ($reply ? '<div class="indented">(.*?)' : '');
94 $regex .= '<a id="comment-' . $comment->id . '"(.*?)'; // Comment anchor.
95 $regex .= '<div(.*?)'; // Begin in comment div.
96 $regex .= $comment->subject . '(.*?)'; // Match subject.
97 $regex .= $comment->comment . '(.*?)'; // Match comment.
98 $regex .= '/s';
99
100 return (boolean)preg_match($regex, $this->drupalGetContent());
101 }
102 else {
103 return FALSE;
104 }
105 }
106
107 /**
108 * Delete comment.
109 *
110 * @param object $comment
111 * Comment to delete.
112 */
113 function deleteComment($comment) {
114 $this->drupalPost('comment/' . $comment->id . '/delete', array(), t('Delete'));
115 $this->assertText(t('The comment and all its replies have been deleted.'), t('Comment deleted.'));
116 }
117
118 /**
119 * Set comment subject setting.
120 *
121 * @param boolean $enabled
122 * Subject value.
123 */
124 function setCommentSubject($enabled) {
125 $this->setCommentSettings('comment_subject_field', ($enabled ? '1' : '0'), 'Comment subject ' . ($enabled ? 'enabled' : 'disabled') . '.');
126 }
127
128 /**
129 * Set comment preview setting.
130 *
131 * @param int $mode
132 * Preview value.
133 */
134 function setCommentPreview($mode) {
135 switch ($mode) {
136 case DRUPAL_DISABLED:
137 $mode_text = 'disabled';
138 break;
139
140 case DRUPAL_OPTIONAL:
141 $mode_text = 'optional';
142 break;
143
144 case DRUPAL_REQUIRED:
145 $mode_text = 'required';
146 break;
147 }
148 $this->setCommentSettings('comment_preview', $mode, 'Comment preview ' . $mode_text . '.');
149 }
150
151 /**
152 * Set comment form setting.
153 *
154 * @param boolean $enabled
155 * Form value.
156 */
157 function setCommentForm($enabled) {
158 $this->setCommentSettings('comment_form_location', ($enabled ? COMMENT_FORM_BELOW : COMMENT_FORM_SEPARATE_PAGE), 'Comment controls ' . ($enabled ? 'enabled' : 'disabled') . '.');
159 }
160
161 /**
162 * Set comment anonymous level setting.
163 *
164 * @param integer $level
165 * Anonymous level.
166 */
167 function setCommentAnonymous($level) {
168 $this->setCommentSettings('comment_anonymous', $level, 'Anonymous commenting set to level ' . $level . '.');
169 }
170
171 /**
172 * Set the default number of comments per page.
173 *
174 * @param integer $comments
175 * Comments per page value.
176 */
177 function setCommentsPerPage($number) {
178 $this->setCommentSettings('comment_default_per_page', $number, 'Number of comments per page set to ' . $number . '.');
179 }
180
181 /**
182 * Set comment setting for article content type.
183 *
184 * @param string $name
185 * Name of variable.
186 * @param string $value
187 * Value of variable.
188 * @param string $message
189 * Status message to display.
190 */
191 function setCommentSettings($name, $value, $message) {
192 variable_set($name . '_article', $value);
193 $this->assertTrue(TRUE, t($message)); // Display status message.
194 }
195
196 /**
197 * Check for contact info.
198 *
199 * @return boolean Contact info is available.
200 */
201 function commentContactInfoAvailable() {
202 return preg_match('/(input).*?(name="name").*?(input).*?(name="mail").*?(input).*?(name="homepage")/s', $this->drupalGetContent());
203 }
204
205 /**
206 * Perform the specified operation on the specified comment.
207 *
208 * @param object $comment
209 * Comment to perform operation on.
210 * @param string $operation
211 * Operation to perform.
212 * @param boolean $aproval
213 * Operation is found on approval page.
214 */
215 function performCommentOperation($comment, $operation, $approval = FALSE) {
216 $edit = array();
217 $edit['operation'] = $operation;
218 $edit['comments[' . $comment->id . ']'] = TRUE;
219 $this->drupalPost('admin/content/comment' . ($approval ? '/approval' : ''), $edit, t('Update'));
220
221 if ($operation == 'delete') {
222 $this->drupalPost(NULL, array(), t('Delete comments'));
223 $this->assertRaw(t('Deleted @count comments.', array('@count' => 1)), t('Operation "' . $operation . '" was performed on comment.'));
224 }
225 else {
226 $this->assertText(t('The update has been performed.'), t('Operation "' . $operation . '" was performed on comment.'));
227 }
228 }
229
230 /**
231 * Get the comment ID for an unapproved comment.
232 *
233 * @param string $subject
234 * Comment subject to find.
235 * @return integer
236 * Comment id.
237 */
238 function getUnapprovedComment($subject) {
239 $this->drupalGet('admin/content/comment/approval');
240 preg_match('/href="(.*?)#comment-([^"]+)"(.*?)>(' . $subject . ')/', $this->drupalGetContent(), $match);
241
242 return $match[2];
243 }
244 }
245
246 class CommentInterfaceTest extends CommentHelperCase {
247 public static function getInfo() {
248 return array(
249 'name' => 'Comment interface',
250 'description' => 'Test comment user interfaces.',
251 'group' => 'Comment',
252 );
253 }
254
255 /**
256 * Test comment interface.
257 */
258 function testCommentInterface() {
259 // Set comments to have subject and preview disabled.
260 $this->drupalLogin($this->admin_user);
261 $this->setCommentPreview(DRUPAL_DISABLED);
262 $this->setCommentForm(TRUE);
263 $this->setCommentSubject(FALSE);
264 $this->setCommentSettings('comment_default_mode', COMMENT_MODE_THREADED, t('Comment paging changed.'));
265 $this->drupalLogout();
266
267 // Post comment #1 without subject or preview.
268 $this->drupalLogin($this->web_user);
269 $comment_text = $this->randomName();
270 $comment = $this->postComment($this->node, $comment_text);
271 $comment_loaded = comment_load($comment->id);
272 $this->assertTrue($this->commentExists($comment), t('Comment found.'));
273
274 // Set comments to have subject and preview to required.
275 $this->drupalLogout();
276 $this->drupalLogin($this->admin_user);
277 $this->setCommentSubject(TRUE);
278 $this->setCommentPreview(DRUPAL_REQUIRED);
279 $this->drupalLogout();
280
281 // Create comment #2 that allows subject and requires preview.
282 $this->drupalLogin($this->web_user);
283 $subject_text = $this->randomName();
284 $comment_text = $this->randomName();
285 $comment = $this->postComment($this->node, $comment_text, $subject_text, TRUE);
286 $comment_loaded = comment_load($comment->id);
287 $this->assertTrue($this->commentExists($comment), t('Comment found.'));
288
289 // Check comment display.
290 $this->drupalGet('node/' . $this->node->nid . '/' . $comment->id);
291 $this->assertText($subject_text, t('Individual comment subject found.'));
292 $this->assertText($comment_text, t('Individual comment body found.'));
293
294 // Set comments to have subject and preview to optional.
295 $this->drupalLogout();
296 $this->drupalLogin($this->admin_user);
297 $this->setCommentSubject(TRUE);
298 $this->setCommentPreview(DRUPAL_OPTIONAL);
299 $this->drupalLogout();
300
301 // Reply to comment #2 creating comment #3 with optional preview and no
302 // subject though field enabled.
303 $this->drupalLogin($this->web_user);
304 $this->drupalGet('comment/reply/' . $this->node->nid . '/' . $comment->id);
305 $this->assertText($subject_text, t('Individual comment-reply subject found.'));
306 $this->assertText($comment_text, t('Individual comment-reply body found.'));
307 $reply = $this->postComment(NULL, $this->randomName(), '', TRUE);
308 $reply_loaded = comment_load($reply->id);
309 $this->assertTrue($this->commentExists($reply, TRUE), t('Reply found.'));
310 $this->assertEqual($comment->id, $reply_loaded->pid, t('Pid of a reply to a comment is set correctly.'));
311 $this->assertEqual(rtrim($comment_loaded->thread, '/') . '.00/', $reply_loaded->thread, t('Thread of reply grows correctly.'));
312
313 // Second reply to comment #3 creating comment #4.
314 $this->drupalGet('comment/reply/' . $this->node->nid . '/' . $comment->id);
315 $this->assertText($subject_text, t('Individual comment-reply subject found.'));
316 $this->assertText($comment_text, t('Individual comment-reply body found.'));
317 $reply = $this->postComment(NULL, $this->randomName(), $this->randomName(), TRUE);
318 $reply_loaded = comment_load($reply->id);
319 $this->assertTrue($this->commentExists($reply, TRUE), t('Second reply found.'));
320 $this->assertEqual(rtrim($comment_loaded->thread, '/') . '.01/', $reply_loaded->thread, t('Thread of second reply grows correctly.'));
321
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), t('Modified reply found.'));
326
327 // Correct link count
328 $this->drupalGet('node');
329 $this->assertRaw('4 comments', t('Link to the 4 comments exist.'));
330
331 // Confirm a new comment is posted to the correct page.
332 $this->setCommentsPerPage(2);
333 $comment_new_page = $this->postComment($this->node, $this->randomName(), $this->randomName(), TRUE);
334 $this->assertTrue($this->commentExists($comment_new_page), t('Page one exists. %s'));
335 $this->drupalGet('node/' . $this->node->nid, array('query' => array('page' => 1)));
336 $this->assertTrue($this->commentExists($reply, TRUE), t('Page two exists. %s'));
337 $this->setCommentsPerPage(50);
338
339 // Attempt to post to node with comments disabled.
340 $this->node = $this->drupalCreateNode(array('type' => 'article', 'promote' => 1, 'comment' => COMMENT_NODE_HIDDEN));
341 $this->assertTrue($this->node, t('Article node created.'));
342 $this->drupalGet('comment/reply/' . $this->node->nid);
343 $this->assertText('This discussion is closed', t('Posting to node with comments disabled'));
344 $this->assertNoField('edit-comment', t('Comment body field found.'));
345
346 // Attempt to post to node with read-only comments.
347 $this->node = $this->drupalCreateNode(array('type' => 'article', 'promote' => 1, 'comment' => COMMENT_NODE_CLOSED));
348 $this->assertTrue($this->node, t('Article node created.'));
349 $this->drupalGet('comment/reply/' . $this->node->nid);
350 $this->assertText('This discussion is closed', t('Posting to node with comments read-only'));
351 $this->assertNoField('edit-comment', t('Comment body field found.'));
352
353 // Attempt to post to node with comments enabled (check field names etc).
354 $this->node = $this->drupalCreateNode(array('type' => 'article', 'promote' => 1, 'comment' => COMMENT_NODE_OPEN));
355 $this->assertTrue($this->node, t('Article node created.'));
356 $this->drupalGet('comment/reply/' . $this->node->nid);
357 $this->assertNoText('This discussion is closed', t('Posting to node with comments enabled'));
358 $this->assertField('edit-comment', t('Comment body field found.'));
359
360 // Delete comment and make sure that reply is also removed.
361 $this->drupalLogout();
362 $this->drupalLogin($this->admin_user);
363 $this->deleteComment($comment);
364 $this->deleteComment($comment_new_page);
365
366 $this->drupalGet('node/' . $this->node->nid);
367 $this->assertFalse($this->commentExists($comment), t('Comment not found.'));
368 $this->assertFalse($this->commentExists($reply, TRUE), t('Reply not found.'));
369
370 // Enabled comment form on node page.
371 $this->drupalLogin($this->admin_user);
372 $this->setCommentForm(TRUE);
373 $this->drupalLogout();
374
375 // Submit comment through node form.
376 $this->drupalLogin($this->web_user);
377 $this->drupalGet('node/' . $this->node->nid);
378 $form_comment = $this->postComment(NULL, $this->randomName(), $this->randomName(), TRUE);
379 $this->assertTrue($this->commentExists($form_comment), t('Form comment found.'));
380
381 // Disable comment form on node page.
382 $this->drupalLogout();
383 $this->drupalLogin($this->admin_user);
384 $this->setCommentForm(FALSE);
385 }
386 }
387
388 /**
389 * Test previewing comments.
390 */
391 class CommentPreviewTest extends CommentHelperCase {
392 public static function getInfo() {
393 return array(
394 'name' => 'Comment preview',
395 'description' => 'Test comment preview.',
396 'group' => 'Comment',
397 );
398 }
399
400 /**
401 * Test comment preview.
402 */
403 function testCommentPreview() {
404 // As admin user, configure comment settings.
405 $this->drupalLogin($this->admin_user);
406 $this->setCommentPreview(TRUE);
407 $this->setCommentForm(TRUE);
408 $this->setCommentSubject(TRUE);
409 $this->setCommentSettings('comment_default_mode', COMMENT_MODE_THREADED, t('Comment paging changed.'));
410 $this->drupalLogout();
411
412 // As web user, fill in node creation form and preview node.
413 $this->drupalLogin($this->web_user);
414 $edit = array();
415 $edit['subject'] = $this->randomName(8);
416 $edit['comment'] = $this->randomName(16);
417 $this->drupalPost('node/' . $this->node->nid, $edit, t('Preview'));
418
419 // Check that the preview is displaying the title and body.
420 $this->assertTitle(t('Preview comment | Drupal'), t('Page title is "Preview comment".'));
421 $this->assertText($edit['subject'], t('Subject displayed.'));
422 $this->assertText($edit['comment'], t('Comment displayed.'));
423
424 // Check that the title and body fields are displayed with the correct values.
425 $this->assertFieldByName('subject', $edit['subject'], t('Subject field displayed.'));
426 $this->assertFieldByName('comment', $edit['comment'], t('Comment field displayed.'));
427 }
428
429 /**
430 * Test comment edit and preview.
431 */
432 function testCommentEditPreview() {
433 $web_user = $this->drupalCreateUser(array('access comments', 'post comments', 'post comments without approval'));
434 $this->drupalLogin($this->admin_user);
435 $this->setCommentPreview(TRUE);
436 $this->setCommentForm(TRUE);
437 $this->setCommentSubject(TRUE);
438 $this->setCommentSettings('comment_default_mode', COMMENT_MODE_THREADED, t('Comment paging changed.'));
439
440 $edit = array();
441 $edit['subject'] = $this->randomName(8);
442 $edit['comment'] = $this->randomName(16);
443 $edit['author'] = $web_user->name;
444 $edit['date'] = '2008-03-02 17:23 +0300';
445 $expected_date = format_date(strtotime($edit['date']));
446 $comment = $this->postComment($this->node, $edit['subject'], $edit['comment'], TRUE);
447 $this->drupalPost('comment/' . $comment->id . '/edit', $edit, t('Preview'));
448
449 // Check that the preview is displaying the subject, comment, author and date correctly.
450 $this->assertTitle(t('Preview comment | Drupal'), t('Page title is "Preview comment".'));
451 $this->assertText($edit['subject'], t('Subject displayed.'));
452 $this->assertText($edit['comment'], t('Comment displayed.'));
453 $this->assertText($edit['author'], t('Author displayed.'));
454 $this->assertText($expected_date, t('Date displayed.'));
455
456 // Check that the title and body fields are displayed with the correct values.
457 $this->assertFieldByName('subject', $edit['subject'], t('Subject field displayed.'));
458 $this->assertFieldByName('comment', $edit['comment'], t('Comment field displayed.'));
459 $this->assertFieldByName('author', $edit['author'], t('Author field displayed.'));
460 $this->assertFieldByName('date', $edit['date'], t('Date field displayed.'));
461 }
462
463 }
464
465 class CommentAnonymous extends CommentHelperCase {
466 public static function getInfo() {
467 return array(
468 'name' => 'Anonymous comments',
469 'description' => 'Test anonymous comments.',
470 'group' => 'Comment',
471 );
472 }
473
474 /**
475 * Test anonymous comment functionality.
476 */
477 function testAnonymous() {
478 $this->drupalLogin($this->admin_user);
479 // Enabled anonymous user comments.
480 user_role_change_permissions(DRUPAL_ANONYMOUS_RID, array(
481 'access comments' => TRUE,
482 'post comments' => TRUE,
483 'post comments without approval' => TRUE,
484 ));
485 $this->setCommentAnonymous('0'); // Ensure that doesn't require contact info.
486 $this->drupalLogout();
487
488 // Post anonymous comment without contact info.
489 $anonymous_comment1 = $this->postComment($this->node, $this->randomName(), $this->randomName());
490 $this->assertTrue($this->commentExists($anonymous_comment1), t('Anonymous comment without contact info found.'));
491
492 // Allow contact info.
493 $this->drupalLogin($this->admin_user);
494 $this->setCommentAnonymous('1');
495
496 // Attempt to edit anonymous comment.
497 $this->drupalGet('comment/' . $anonymous_comment1->id . '/edit');
498 $edited_comment = $this->postComment(NULL, $this->randomName(), $this->randomName());
499 $this->assertTrue($this->commentExists($edited_comment, FALSE), t('Modified reply found.'));
500 $this->drupalLogout();
501
502 // Post anonymous comment with contact info (optional).
503 $this->drupalGet('comment/reply/' . $this->node->nid);
504 $this->assertTrue($this->commentContactInfoAvailable(), t('Contact information available.'));
505
506 $anonymous_comment2 = $this->postComment($this->node, $this->randomName(), $this->randomName());
507 $this->assertTrue($this->commentExists($anonymous_comment2), t('Anonymous comment with contact info (optional) found.'));
508
509 // Require contact info.
510 $this->drupalLogin($this->admin_user);
511 $this->setCommentAnonymous('2');
512 $this->drupalLogout();
513
514 // Try to post comment with contact info (required).
515 $this->drupalGet('comment/reply/' . $this->node->nid);
516 $this->assertTrue($this->commentContactInfoAvailable(), t('Contact information available.'));
517
518 $anonymous_comment3 = $this->postComment($this->node, $this->randomName(), $this->randomName(), TRUE);
519 $this->assertText(t('E-mail field is required.'), t('E-mail required.')); // Name should have 'Anonymous' for value by default.
520 $this->assertFalse($this->commentExists($anonymous_comment3), t('Anonymous comment with contact info (required) not found.'));
521
522 // Post comment with contact info (required).
523 $anonymous_comment3 = $this->postComment($this->node, $this->randomName(), $this->randomName(), array('mail' => 'tester@simpletest.org'));
524 $this->assertTrue($this->commentExists($anonymous_comment3), t('Anonymous comment with contact info (required) found.'));
525
526 // Unpublish comment.
527 $this->drupalLogin($this->admin_user);
528 $this->performCommentOperation($anonymous_comment3, 'unpublish');
529
530 $this->drupalGet('admin/content/comment/approval');
531 $this->assertRaw('comments[' . $anonymous_comment3->id . ']', t('Comment was unpublished.'));
532
533 // Publish comment.
534 $this->performCommentOperation($anonymous_comment3, 'publish', TRUE);
535
536 $this->drupalGet('admin/content/comment');
537 $this->assertRaw('comments[' . $anonymous_comment3->id . ']', t('Comment was published.'));
538
539 // Delete comment.
540 $this->performCommentOperation($anonymous_comment3, 'delete');
541
542 $this->drupalGet('admin/content/comment');
543 $this->assertNoRaw('comments[' . $anonymous_comment3->id . ']', t('Comment was deleted.'));
544 $this->drupalLogout();
545
546 // Reset.
547 user_role_change_permissions(DRUPAL_ANONYMOUS_RID, array(
548 'access comments' => FALSE,
549 'post comments' => FALSE,
550 'post comments without approval' => FALSE,
551 ));
552
553 // Attempt to view comments while disallowed.
554 // NOTE: if authenticated user has permission to post comments, then a
555 // "Login or register to post comments" type link may be shown.
556 $this->drupalGet('node/' . $this->node->nid);
557 $this->assertNoPattern('/<div ([^>]*?)id="comments"([^>]*?)>/', t('Comments were not displayed.'));
558 $this->assertNoLink('Add new comment', t('Link to add comment was found.'));
559
560 // Attempt to view node-comment form while disallowed.
561 $this->drupalGet('comment/reply/' . $this->node->nid);
562 $this->assertText('You are not authorized to view comments', t('Error attempting to post comment.'));
563 $this->assertNoFieldByName('subject', '', t('Subject field not found.'));
564 $this->assertNoFieldByName('comment', '', t('Comment field not found.'));
565
566 user_role_change_permissions(DRUPAL_ANONYMOUS_RID, array(
567 'access comments' => TRUE,
568 'post comments' => FALSE,
569 'post comments without approval' => FALSE,
570 ));
571 $this->drupalGet('node/' . $this->node->nid);
572 $this->assertPattern('/<div ([^>]*?)id="comments"([^>]*?)>/', t('Comments were displayed.'));
573 $this->assertLink('Login', 1, t('Link to login was found.'));
574 $this->assertLink('register', 1, t('Link to register was found.'));
575 }
576 }
577
578 /**
579 * Verify pagination of comments.
580 */
581 class CommentPagerTest extends CommentHelperCase {
582
583 public static function getInfo() {
584 return array(
585 'name' => 'Comment paging settings',
586 'description' => 'Test paging of comments and their settings.',
587 'group' => 'Comment',
588 );
589 }
590
591 /**
592 * Confirm comment paging works correctly with flat and threaded comments.
593 */
594 function testCommentPaging() {
595 $this->drupalLogin($this->admin_user);
596
597 // Set comment variables.
598 $this->setCommentForm(TRUE);
599 $this->setCommentSubject(TRUE);
600 $this->setCommentPreview(FALSE);
601
602 // Create a node and three comments.
603 $node = $this->drupalCreateNode(array('type' => 'article', 'promote' => 1));
604 $comments = array();
605 $comments[] = $this->postComment($node, $this->randomName(), $this->randomName(), TRUE);
606 $comments[] = $this->postComment($node, $this->randomName(), $this->randomName(), TRUE);
607 $comments[] = $this->postComment($node, $this->randomName(), $this->randomName(), TRUE);
608
609 $this->setCommentSettings('comment_default_mode', COMMENT_MODE_FLAT, t('Comment paging changed.'));
610
611 // Set comments to one per page so that we are able to test paging without
612 // needing to insert large numbers of comments.
613 $this->setCommentsPerPage(1);
614
615 // Check the first page of the node, and confirm the correct comments are
616 // shown.
617 $this->drupalGet('node/' . $node->nid);
618 $this->assertRaw(t('next'), t('Paging links found.'));
619 $this->assertTrue($this->commentExists($comments[0]), t('Comment 1 appears on page 1.'));
620 $this->assertFalse($this->commentExists($comments[1]), t('Comment 2 does not appear on page 1.'));
621 $this->assertFalse($this->commentExists($comments[2]), t('Comment 3 does not appear on page 1.'));
622
623 // Check the second page.
624 $this->drupalGet('node/' . $node->nid, array('query' => array('page' => 1)));
625 $this->assertTrue($this->commentExists($comments[1]), t('Comment 2 appears on page 2.'));
626 $this->assertFalse($this->commentExists($comments[0]), t('Comment 1 does not appear on page 2.'));
627 $this->assertFalse($this->commentExists($comments[2]), t('Comment 3 does not appear on page 2.'));
628
629 // Check the third page.
630 $this->drupalGet('node/' . $node->nid, array('query' => array('page' => 2)));
631 $this->assertTrue($this->commentExists($comments[2]), t('Comment 3 appears on page 3.'));
632 $this->assertFalse($this->commentExists($comments[0]), t('Comment 1 does not appear on page 3.'));
633 $this->assertFalse($this->commentExists($comments[1]), t('Comment 2 does not appear on page 3.'));
634
635 // Post a reply to the oldest comment and test again.
636 $replies = array();
637 $oldest_comment = reset($comments);
638 $this->drupalGet('comment/reply/' . $node->nid . '/' . $oldest_comment->id);
639 $reply = $this->postComment(NULL, $this->randomName(), $this->randomName(), TRUE);
640
641 $this->setCommentsPerPage(2);
642 // We are still in flat view - the replies should not be on the first page,
643 // even though they are replies to the oldest comment.
644 $this->drupalGet('node/' . $node->nid, array('query' => array('page' => 0)));
645 $this->assertFalse($this->commentExists($reply, TRUE), t('In flat mode, reply does not appear on page 1.'));
646
647 // If we switch to threaded mode, the replies on the oldest comment
648 // should be bumped to the first page and comment 6 should be bumped
649 // to the second page.
650 $this->setCommentSettings('comment_default_mode', COMMENT_MODE_THREADED, t('Switched to threaded mode.'));
651 $this->drupalGet('node/' . $node->nid, array('query' => array('page' => 0)));
652 $this->assertTrue($this->commentExists($reply, TRUE), t('In threaded mode, reply appears on page 1.'));
653 $this->assertFalse($this->commentExists($comments[1]), t('In threaded mode, comment 2 has been bumped off of page 1.'));
654
655 // If (# replies > # comments per page) in threaded expanded view,
656 // the overage should be bumped.
657 $reply2 = $this->postComment(NULL, $this->randomName(), $this->randomName(), TRUE);
658 $this->drupalGet('node/' . $node->nid, array('query' => array('page' => 0)));
659 $this->assertFalse($this->commentExists($reply2, TRUE), t('In threaded mode where # replies > # comments per page, the newest reply does not appear on page 1.'));
660
661 $this->drupalLogout();
662 }
663 }
664
665 class CommentApprovalTest extends CommentHelperCase {
666 public static function getInfo() {
667 return array(
668 'name' => 'Comment approval',
669 'description' => 'Test comment approval functionality.',
670 'group' => 'Comment',
671 );
672 }
673
674 /**
675 * Test comment approval functionality through admin/content/comment.
676 */
677 function testApprovalAdminInterface() {
678 // Set anonymous comments to require approval.
679 user_role_change_permissions(DRUPAL_ANONYMOUS_RID, array(
680 'access comments' => TRUE,
681 'post comments' => TRUE,
682 'post comments without approval' => FALSE,
683 ));
684 $this->drupalLogin($this->admin_user);
685 $this->setCommentAnonymous('0'); // Ensure that doesn't require contact info.
686 $this->drupalLogout();
687
688 // Post anonymous comment without contact info.
689 $subject = $this->randomName();
690 $body = $this->randomName();
691 $this->postComment($this->node, $body, $subject, TRUE); // Set $contact to true so that it won't check for id and message.
692 $this->assertText(t('Your comment has been queued for review by site administrators and will be published after approval.'), t('Comment requires approval.'));
693
694 // Get unapproved comment id.
695 $this->drupalLogin($this->admin_user);
696 $anonymous_comment4 = $this->getUnapprovedComment($subject);
697 $anonymous_comment4 = (object) array('id' => $anonymous_comment4, 'subject' => $subject, 'comment' => $body);
698 $this->drupalLogout();
699
700 $this->assertFalse($this->commentExists($anonymous_comment4), t('Anonymous comment was not published.'));
701
702 // Approve comment.
703 $this->drupalLogin($this->admin_user);
704 $this->performCommentOperation($anonymous_comment4, 'publish', TRUE);
705 $this->drupalLogout();
706
707 $this->drupalGet('node/' . $this->node->nid);
708 $this->assertTrue($this->commentExists($anonymous_comment4), t('Anonymous comment visible.'));
709 }
710
711 /**
712 * Test comment approval functionality through node interface.
713 */
714 function testApprovalNodeInterface() {
715 // Set anonymous comments to require approval.
716 user_role_change_permissions(DRUPAL_ANONYMOUS_RID, array(
717 'access comments' => TRUE,
718 'post comments' => TRUE,
719 'post comments without approval' => FALSE,
720 ));
721 $this->drupalLogin($this->admin_user);
722 $this->setCommentAnonymous('0'); // Ensure that doesn't require contact info.
723 $this->drupalLogout();
724
725 // Post anonymous comment without contact info.
726 $subject = $this->randomName();
727 $body = $this->randomName();
728 $this->postComment($this->node, $body, $subject, TRUE); // Set $contact to true so that it won't check for id and message.
729 $this->assertText(t('Your comment has been queued for review by site administrators and will be published after approval.'), t('Comment requires approval.'));
730
731 // Get unapproved comment id.
732 $this->drupalLogin($this->admin_user);
733 $anonymous_comment4 = $this->getUnapprovedComment($subject);
734 $anonymous_comment4 = (object) array('id' => $anonymous_comment4, 'subject' => $subject, 'comment' => $body);
735 $this->drupalLogout();
736
737 $this->assertFalse($this->commentExists($anonymous_comment4), t('Anonymous comment was not published.'));
738
739 // Approve comment.
740 $this->drupalLogin($this->admin_user);
741 $this->drupalGet('node/' . $this->node->nid);
742 $this->clickLink(t('approve'));
743 $this->drupalLogout();
744
745 $this->drupalGet('node/' . $this->node->nid);
746 $this->assertTrue($this->commentExists($anonymous_comment4), t('Anonymous comment visible.'));
747 }
748 }
749
750 /**
751 * Functional tests for the comment module blocks.
752 */
753 class CommentBlockFunctionalTest extends CommentHelperCase {
754 public static function getInfo() {
755 return array(
756 'name' => 'Comment blocks',
757 'description' => 'Test comment block functionality.',
758 'group' => 'Comment',
759 );
760 }
761
762 /**
763 * Test the recent comments block.
764 */
765 function testRecentCommentBlock() {
766 $this->drupalLogin($this->admin_user);
767
768 // Set the block to a region to confirm block is available.
769 $edit = array(
770 'comment_recent[region]' => 'sidebar_first',
771 );
772 $this->drupalPost('admin/structure/block', $edit, t('Save blocks'));
773 $this->assertText(t('The block settings have been updated.'), t('Block saved to first sidebar region.'));
774
775 // Set block title and variables.
776 $block = array(
777 'title' => $this->randomName(),
778 'comment_block_count' => 2,
779 );
780 $this->drupalPost('admin/structure/block/manage/comment/recent/configure', $block, t('Save block'));
781 $this->assertText(t('The block configuration has been saved.'), t('Block saved.'));
782
783 // Add some test comments, one without a subject.
784 $comment1 = $this->postComment($this->node, $this->randomName(), $this->randomName());
785 $comment2 = $this->postComment($this->node, $this->randomName(), $this->randomName());
786 $comment3 = $this->postComment($this->node, $this->randomName());
787
788 // Test that a user without the 'access comments' permission can not see the block.
789 $this->drupalLogout();
790 $this->drupalGet('');
791 $this->assertNoText($block['title'], t('Block was not found.'));
792
793 $this->drupalLogin($this->web_user);
794 $this->drupalGet('');
795 $this->assertText($block['title'], t('Block was found.'));
796
797 // Test the only the 2 latest comments are shown and in the proper order.
798 $this->assertNoText($comment1->subject, t('Comment not found in block.'));
799 $this->assertText($comment2->subject, t('Comment found in block.'));
800 $this->assertText($comment3->comment, t('Comment found in block.'));
801 $this->assertTrue(strpos($this->drupalGetContent(), $comment3->comment) < strpos($this->drupalGetContent(), $comment2->subject), t('Comments were ordered correctly in block.'));
802
803 // Set the number of recent comments to show to 10.
804 $this->drupalLogout();
805 $this->drupalLogin($this->admin_user);
806 $block = array(
807 'comment_block_count' => 10,
808 );
809 $this->drupalPost('admin/structure/block/manage/comment/recent/configure', $block, t('Save block'));
810 $this->assertText(t('The block configuration has been saved.'), t('Block saved.'));
811
812 // Post an additional comment.
813 $comment4 = $this->postComment($this->node, $this->randomName(), $this->randomName());
814
815 // Test that all four comments are shown.
816 $this->assertText($comment1->subject, t('Comment found in block.'));
817 $this->assertText($comment2->subject, t('Comment found in block.'));
818 $this->assertText($comment3->comment, t('Comment found in block.'));
819 $this->assertText($comment4->subject, t('Comment found in block.'));
820
821 // Test that links to comments work when comments are across pages.
822 $this->setCommentsPerPage(1);
823 $this->drupalGet('');
824 $this->clickLink($comment1->subject);
825 $this->assertText($comment1->subject, t('Comment link goes to correct page.'));
826 $this->drupalGet('');
827 $this->clickLink($comment2->subject);
828 $this->assertText($comment2->subject, t('Comment link goes to correct page.'));
829 $this->clickLink($comment4->subject);
830 $this->assertText($comment4->subject, t('Comment link goes to correct page.'));
831 // Check that when viewing a comment page from a link to the comment, that
832 // rel="canonical" is added to the head of the document.
833 $this->assertRaw('<link rel="canonical"', t('Canonical URL was found in the HTML head'));
834 }
835 }
836
837 /**
838 * Unit tests for comment module integration with RSS feeds.
839 */
840 class CommentRSSUnitTest extends CommentHelperCase {
841 public static function getInfo() {
842 return array(
843 'name' => 'Comment RSS',
844 'description' => 'Test comments as part of an RSS feed.',
845 'group' => 'Comment',
846 );
847 }
848
849 /**
850 * Test comments as part of an RSS feed.
851 */
852 function testCommentRSS() {
853 // Find comment in RSS feed.
854 $this->drupalLogin($this->web_user);
855 $comment = $this->postComment($this->node, $this->randomName(), $this->randomName());
856 $this->drupalGet('rss.xml');
857 $raw = '<comments>' . url('node/' . $this->node->nid, array('fragment' => 'comments', 'absolute' => TRUE)) . '</comments>';
858 $this->assertRaw($raw, t('Comments as part of RSS feed.'));
859
860 // Hide comments from RSS feed and check presence.
861 $this->node->comment = COMMENT_NODE_HIDDEN;
862 node_save($this->node);
863 $this->drupalGet('rss.xml');
864 $this->assertNoRaw($raw, t('Hidden comments is not a part of RSS feed.'));
865 }
866 }
867
868 /**
869 * Test RDFa markup for comments.
870 */
871 class CommentRdfaTestCase extends CommentHelperCase {
872 public static function getInfo() {
873 return array(
874 'name' => 'RDFa comment markup',
875 'description' => 'Test RDFa markup in comments.',
876 'group' => 'RDF',
877 );
878 }
879
880 function setUp() {
881 parent::setUp('comment', 'rdf');
882
883 $this->admin_user = $this->drupalCreateUser(array('administer content types', 'administer comments', 'administer permissions', 'administer blocks'));
884 $this->web_user = $this->drupalCreateUser(array('access comments', 'post comments', 'create article content'));
885
886 $this->drupalLogin($this->web_user);
887 $this->node = $this->drupalCreateNode(array('type' => 'article', 'promote' => 1));
888 $this->drupalLogout();
889 }
890
891 function testAttributesInMarkup() {
892 // Set comments to not have subject.
893 $this->drupalLogin($this->admin_user);
894 $this->setCommentPreview(FALSE);
895 $this->setCommentForm(TRUE);
896 $this->setCommentSubject(TRUE);
897 $this->setCommentSettings('comment_default_mode', COMMENT_MODE_THREADED, t('Comment paging changed.'));
898 $this->drupalLogout();
899
900 // Post comment.
901 $this->drupalLogin($this->web_user);
902 $subject_text = 'foo';
903 $comment_text = 'bar';
904 $comment = $this->postComment($this->node, $comment_text, $subject_text, FALSE);
905 $this->drupalGet('node/' . $this->node->nid);
906
907 $comment_container = $this->xpath("//div[contains(@class, 'comment') and @typeof='sioct:Post']");
908 $this->assertFalse(empty($comment_container));
909 $comment_title = $this->xpath("//h3[@property='dc:title']");
910 $this->assertEqual((string)$comment_title[0]->a, 'foo');
911 $comment_date = $this->xpath("//div[@typeof='sioct:Post']//*[contains(@property, 'dc:date') and contains(@property, 'dc:created')]");
912 $this->assertFalse(empty($comment_date));
913 $comment_author = $this->xpath("//div[@typeof='sioct:Post']//*[contains(@property, 'foaf:name')]");
914 $this->assertEqual((string)$comment_author[0], $this->web_user->name);
915 }
916 }

  ViewVC Help
Powered by ViewVC 1.1.2