Projet

Général

Profil

Paste
Télécharger (92,6 ko) Statistiques
| Branche: | Révision:

root / drupal7 / modules / comment / comment.test @ 76597ebf

1
<?php
2

    
3
/**
4
 * @file
5
 * Tests for comment.module.
6
 */
7

    
8
class CommentHelperCase extends DrupalWebTestCase {
9
  protected $admin_user;
10
  protected $web_user;
11
  protected $node;
12

    
13
  function setUp() {
14
    parent::setUp('comment', 'search');
15
    // Create users and test node.
16
    $this->admin_user = $this->drupalCreateUser(array('administer content types', 'administer comments', 'administer blocks'));
17
    $this->web_user = $this->drupalCreateUser(array('access comments', 'post comments', 'create article content', 'edit own comments'));
18
    $this->node = $this->drupalCreateNode(array('type' => 'article', 'promote' => 1, 'uid' => $this->web_user->uid));
19
  }
20

    
21
  /**
22
   * Post comment.
23
   *
24
   * @param $node
25
   *   Node to post comment on.
26
   * @param $comment
27
   *   Comment body.
28
   * @param $subject
29
   *   Comment subject.
30
   * @param $contact
31
   *   Set to NULL for no contact info, TRUE to ignore success checking, and
32
   *   array of values to set contact info.
33
   */
34
  function postComment($node, $comment, $subject = '', $contact = NULL) {
35
    $langcode = LANGUAGE_NONE;
36
    $edit = array();
37
    $edit['comment_body[' . $langcode . '][0][value]'] = $comment;
38

    
39
    $preview_mode = variable_get('comment_preview_article', DRUPAL_OPTIONAL);
40
    $subject_mode = variable_get('comment_subject_field_article', 1);
41

    
42
    // Must get the page before we test for fields.
43
    if ($node !== NULL) {
44
      $this->drupalGet('comment/reply/' . $node->nid);
45
    }
46

    
47
    if ($subject_mode == TRUE) {
48
      $edit['subject'] = $subject;
49
    }
50
    else {
51
      $this->assertNoFieldByName('subject', '', 'Subject field not found.');
52
    }
53

    
54
    if ($contact !== NULL && is_array($contact)) {
55
      $edit += $contact;
56
    }
57
    switch ($preview_mode) {
58
      case DRUPAL_REQUIRED:
59
        // Preview required so no save button should be found.
60
        $this->assertNoFieldByName('op', t('Save'), 'Save button not found.');
61
        $this->drupalPost(NULL, $edit, t('Preview'));
62
        // Don't break here so that we can test post-preview field presence and
63
        // function below.
64
      case DRUPAL_OPTIONAL:
65
        $this->assertFieldByName('op', t('Preview'), 'Preview button found.');
66
        $this->assertFieldByName('op', t('Save'), 'Save button found.');
67
        $this->drupalPost(NULL, $edit, t('Save'));
68
        break;
69

    
70
      case DRUPAL_DISABLED:
71
        $this->assertNoFieldByName('op', t('Preview'), 'Preview button not found.');
72
        $this->assertFieldByName('op', t('Save'), 'Save button found.');
73
        $this->drupalPost(NULL, $edit, t('Save'));
74
        break;
75
    }
76
    $match = array();
77
    // Get comment ID
78
    preg_match('/#comment-([0-9]+)/', $this->getURL(), $match);
79

    
80
    // Get comment.
81
    if ($contact !== TRUE) { // If true then attempting to find error message.
82
      if ($subject) {
83
        $this->assertText($subject, 'Comment subject posted.');
84
      }
85
      $this->assertText($comment, 'Comment body posted.');
86
      $this->assertTrue((!empty($match) && !empty($match[1])), 'Comment id found.');
87
    }
88

    
89
    if (isset($match[1])) {
90
      return (object) array('id' => $match[1], 'subject' => $subject, 'comment' => $comment);
91
    }
92
  }
93

    
94
  /**
95
   * Checks current page for specified comment.
96
   *
97
   * @param object $comment Comment object.
98
   * @param boolean $reply The comment is a reply to another comment.
99
   * @return boolean Comment found.
100
   */
101
  function commentExists($comment, $reply = FALSE) {
102
    if ($comment && is_object($comment)) {
103
      $regex = '/' . ($reply ? '<div class="indented">(.*?)' : '');
104
      $regex .= '<a id="comment-' . $comment->id . '"(.*?)'; // Comment anchor.
105
      $regex .= '<div(.*?)'; // Begin in comment div.
106
      $regex .= $comment->subject . '(.*?)'; // Match subject.
107
      $regex .= $comment->comment . '(.*?)'; // Match comment.
108
      $regex .= '/s';
109

    
110
      return (boolean)preg_match($regex, $this->drupalGetContent());
111
    }
112
    else {
113
      return FALSE;
114
    }
115
  }
116

    
117
  /**
118
   * Delete comment.
119
   *
120
   * @param object $comment
121
   *   Comment to delete.
122
   */
123
  function deleteComment($comment) {
124
    $this->drupalPost('comment/' . $comment->id . '/delete', array(), t('Delete'));
125
    $this->assertText(t('The comment and all its replies have been deleted.'), 'Comment deleted.');
126
  }
127

    
128
  /**
129
   * Set comment subject setting.
130
   *
131
   * @param boolean $enabled
132
   *   Subject value.
133
   */
134
  function setCommentSubject($enabled) {
135
    $this->setCommentSettings('comment_subject_field', ($enabled ? '1' : '0'), 'Comment subject ' . ($enabled ? 'enabled' : 'disabled') . '.');
136
  }
137

    
138
  /**
139
   * Set comment preview setting.
140
   *
141
   * @param int $mode
142
   *   Preview value.
143
   */
144
  function setCommentPreview($mode) {
145
    switch ($mode) {
146
      case DRUPAL_DISABLED:
147
        $mode_text = 'disabled';
148
        break;
149

    
150
      case DRUPAL_OPTIONAL:
151
        $mode_text = 'optional';
152
        break;
153

    
154
      case DRUPAL_REQUIRED:
155
        $mode_text = 'required';
156
        break;
157
    }
158
    $this->setCommentSettings('comment_preview', $mode, format_string('Comment preview @mode_text.', array('@mode_text' => $mode_text)));
159
  }
160

    
161
  /**
162
   * Set comment form location setting.
163
   *
164
   * @param boolean $enabled
165
   *   Form value.
166
   */
167
  function setCommentForm($enabled) {
168
    $this->setCommentSettings('comment_form_location', ($enabled ? COMMENT_FORM_BELOW : COMMENT_FORM_SEPARATE_PAGE), 'Comment controls ' . ($enabled ? 'enabled' : 'disabled') . '.');
169
  }
170

    
171
  /**
172
   * Set comment anonymous level setting.
173
   *
174
   * @param integer $level
175
   *   Anonymous level.
176
   */
177
  function setCommentAnonymous($level) {
178
    $this->setCommentSettings('comment_anonymous', $level, format_string('Anonymous commenting set to level @level.', array('@level' => $level)));
179
  }
180

    
181
  /**
182
   * Set the default number of comments per page.
183
   *
184
   * @param integer $comments
185
   *   Comments per page value.
186
   */
187
  function setCommentsPerPage($number) {
188
    $this->setCommentSettings('comment_default_per_page', $number, format_string('Number of comments per page set to @number.', array('@number' => $number)));
189
  }
190

    
191
  /**
192
   * Set comment setting for article content type.
193
   *
194
   * @param string $name
195
   *   Name of variable.
196
   * @param string $value
197
   *   Value of variable.
198
   * @param string $message
199
   *   Status message to display.
200
   */
201
  function setCommentSettings($name, $value, $message) {
202
    variable_set($name . '_article', $value);
203
    // Display status message.
204
    $this->pass($message);
205
  }
206

    
207
  /**
208
   * Check for contact info.
209
   *
210
   * @return boolean Contact info is available.
211
   */
212
  function commentContactInfoAvailable() {
213
    return preg_match('/(input).*?(name="name").*?(input).*?(name="mail").*?(input).*?(name="homepage")/s', $this->drupalGetContent());
214
  }
215

    
216
  /**
217
   * Perform the specified operation on the specified comment.
218
   *
219
   * @param object $comment
220
   *   Comment to perform operation on.
221
   * @param string $operation
222
   *   Operation to perform.
223
   * @param boolean $aproval
224
   *   Operation is found on approval page.
225
   */
226
  function performCommentOperation($comment, $operation, $approval = FALSE) {
227
    $edit = array();
228
    $edit['operation'] = $operation;
229
    $edit['comments[' . $comment->id . ']'] = TRUE;
230
    $this->drupalPost('admin/content/comment' . ($approval ? '/approval' : ''), $edit, t('Update'));
231

    
232
    if ($operation == 'delete') {
233
      $this->drupalPost(NULL, array(), t('Delete comments'));
234
      $this->assertRaw(format_plural(1, 'Deleted 1 comment.', 'Deleted @count comments.'), format_string('Operation @operation was performed on comment.', array('@operation' => $operation)));
235
    }
236
    else {
237
      $this->assertText(t('The update has been performed.'), format_string('Operation @operation was performed on comment.', array('@operation' => $operation)));
238
    }
239
  }
240

    
241
  /**
242
   * Get the comment ID for an unapproved comment.
243
   *
244
   * @param string $subject
245
   *   Comment subject to find.
246
   * @return integer
247
   *   Comment id.
248
   */
249
  function getUnapprovedComment($subject) {
250
    $this->drupalGet('admin/content/comment/approval');
251
    preg_match('/href="(.*?)#comment-([^"]+)"(.*?)>(' . $subject . ')/', $this->drupalGetContent(), $match);
252

    
253
    return $match[2];
254
  }
255
}
256

    
257
class CommentInterfaceTest extends CommentHelperCase {
258
  public static function getInfo() {
259
    return array(
260
      'name' => 'Comment interface',
261
      'description' => 'Test comment user interfaces.',
262
      'group' => 'Comment',
263
    );
264
  }
265

    
266
  /**
267
   * Test comment interface.
268
   */
269
  function testCommentInterface() {
270
    $langcode = LANGUAGE_NONE;
271
    // Set comments to have subject and preview disabled.
272
    $this->drupalLogin($this->admin_user);
273
    $this->setCommentPreview(DRUPAL_DISABLED);
274
    $this->setCommentForm(TRUE);
275
    $this->setCommentSubject(FALSE);
276
    $this->setCommentSettings('comment_default_mode', COMMENT_MODE_THREADED, 'Comment paging changed.');
277
    $this->drupalLogout();
278

    
279
    // Post comment #1 without subject or preview.
280
    $this->drupalLogin($this->web_user);
281
    $comment_text = $this->randomName();
282
    $comment = $this->postComment($this->node, $comment_text);
283
    $comment_loaded = comment_load($comment->id);
284
    $this->assertTrue($this->commentExists($comment), 'Comment found.');
285

    
286
    // Set comments to have subject and preview to required.
287
    $this->drupalLogout();
288
    $this->drupalLogin($this->admin_user);
289
    $this->setCommentSubject(TRUE);
290
    $this->setCommentPreview(DRUPAL_REQUIRED);
291
    $this->drupalLogout();
292

    
293
    // Create comment #2 that allows subject and requires preview.
294
    $this->drupalLogin($this->web_user);
295
    $subject_text = $this->randomName();
296
    $comment_text = $this->randomName();
297
    $comment = $this->postComment($this->node, $comment_text, $subject_text, TRUE);
298
    $comment_loaded = comment_load($comment->id);
299
    $this->assertTrue($this->commentExists($comment), 'Comment found.');
300

    
301
    // Check comment display.
302
    $this->drupalGet('node/' . $this->node->nid . '/' . $comment->id);
303
    $this->assertText($subject_text, 'Individual comment subject found.');
304
    $this->assertText($comment_text, 'Individual comment body found.');
305

    
306
    // Set comments to have subject and preview to optional.
307
    $this->drupalLogout();
308
    $this->drupalLogin($this->admin_user);
309
    $this->setCommentSubject(TRUE);
310
    $this->setCommentPreview(DRUPAL_OPTIONAL);
311

    
312
    // Test changing the comment author to "Anonymous".
313
    $this->drupalGet('comment/' . $comment->id . '/edit');
314
    $comment = $this->postComment(NULL, $comment->comment, $comment->subject, array('name' => ''));
315
    $comment_loaded = comment_load($comment->id);
316
    $this->assertTrue(empty($comment_loaded->name) && $comment_loaded->uid == 0, 'Comment author successfully changed to anonymous.');
317

    
318
    // Test changing the comment author to an unverified user.
319
    $random_name = $this->randomName();
320
    $this->drupalGet('comment/' . $comment->id . '/edit');
321
    $comment = $this->postComment(NULL, $comment->comment, $comment->subject, array('name' => $random_name));
322
    $this->drupalGet('node/' . $this->node->nid);
323
    $this->assertText($random_name . ' (' . t('not verified') . ')', 'Comment author successfully changed to an unverified user.');
324

    
325
    // Test changing the comment author to a verified user.
326
    $this->drupalGet('comment/' . $comment->id . '/edit');
327
    $comment = $this->postComment(NULL, $comment->comment, $comment->subject, array('name' => $this->web_user->name));
328
    $comment_loaded = comment_load($comment->id);
329
    $this->assertTrue($comment_loaded->name == $this->web_user->name && $comment_loaded->uid == $this->web_user->uid, 'Comment author successfully changed to a registered user.');
330

    
331
    $this->drupalLogout();
332

    
333
    // Reply to comment #2 creating comment #3 with optional preview and no
334
    // subject though field enabled.
335
    $this->drupalLogin($this->web_user);
336
    $this->drupalGet('comment/reply/' . $this->node->nid . '/' . $comment->id);
337
    $this->assertText($subject_text, 'Individual comment-reply subject found.');
338
    $this->assertText($comment_text, 'Individual comment-reply body found.');
339
    $reply = $this->postComment(NULL, $this->randomName(), '', TRUE);
340
    $reply_loaded = comment_load($reply->id);
341
    $this->assertTrue($this->commentExists($reply, TRUE), 'Reply found.');
342
    $this->assertEqual($comment->id, $reply_loaded->pid, 'Pid of a reply to a comment is set correctly.');
343
    $this->assertEqual(rtrim($comment_loaded->thread, '/') . '.00/', $reply_loaded->thread, 'Thread of reply grows correctly.');
344

    
345
    // Second reply to comment #3 creating comment #4.
346
    $this->drupalGet('comment/reply/' . $this->node->nid . '/' . $comment->id);
347
    $this->assertText($subject_text, 'Individual comment-reply subject found.');
348
    $this->assertText($comment_text, 'Individual comment-reply body found.');
349
    $reply = $this->postComment(NULL, $this->randomName(), $this->randomName(), TRUE);
350
    $reply_loaded = comment_load($reply->id);
351
    $this->assertTrue($this->commentExists($reply, TRUE), 'Second reply found.');
352
    $this->assertEqual(rtrim($comment_loaded->thread, '/') . '.01/', $reply_loaded->thread, 'Thread of second reply grows correctly.');
353

    
354
    // Edit reply.
355
    $this->drupalGet('comment/' . $reply->id . '/edit');
356
    $reply = $this->postComment(NULL, $this->randomName(), $this->randomName(), TRUE);
357
    $this->assertTrue($this->commentExists($reply, TRUE), 'Modified reply found.');
358

    
359
    // Correct link count
360
    $this->drupalGet('node');
361
    $this->assertRaw('4 comments', 'Link to the 4 comments exist.');
362

    
363
    // Confirm a new comment is posted to the correct page.
364
    $this->setCommentsPerPage(2);
365
    $comment_new_page = $this->postComment($this->node, $this->randomName(), $this->randomName(), TRUE);
366
    $this->assertTrue($this->commentExists($comment_new_page), 'Page one exists. %s');
367
    $this->drupalGet('node/' . $this->node->nid, array('query' => array('page' => 1)));
368
    $this->assertTrue($this->commentExists($reply, TRUE), 'Page two exists. %s');
369
    $this->setCommentsPerPage(50);
370

    
371
    // Attempt to post to node with comments disabled.
372
    $this->node = $this->drupalCreateNode(array('type' => 'article', 'promote' => 1, 'comment' => COMMENT_NODE_HIDDEN));
373
    $this->assertTrue($this->node, 'Article node created.');
374
    $this->drupalGet('comment/reply/' . $this->node->nid);
375
    $this->assertText('This discussion is closed', 'Posting to node with comments disabled');
376
    $this->assertNoField('edit-comment', 'Comment body field found.');
377

    
378
    // Attempt to post to node with read-only comments.
379
    $this->node = $this->drupalCreateNode(array('type' => 'article', 'promote' => 1, 'comment' => COMMENT_NODE_CLOSED));
380
    $this->assertTrue($this->node, 'Article node created.');
381
    $this->drupalGet('comment/reply/' . $this->node->nid);
382
    $this->assertText('This discussion is closed', 'Posting to node with comments read-only');
383
    $this->assertNoField('edit-comment', 'Comment body field found.');
384

    
385
    // Attempt to post to node with comments enabled (check field names etc).
386
    $this->node = $this->drupalCreateNode(array('type' => 'article', 'promote' => 1, 'comment' => COMMENT_NODE_OPEN));
387
    $this->assertTrue($this->node, 'Article node created.');
388
    $this->drupalGet('comment/reply/' . $this->node->nid);
389
    $this->assertNoText('This discussion is closed', 'Posting to node with comments enabled');
390
    $this->assertField('edit-comment-body-' . $langcode . '-0-value', 'Comment body field found.');
391

    
392
    // Delete comment and make sure that reply is also removed.
393
    $this->drupalLogout();
394
    $this->drupalLogin($this->admin_user);
395
    $this->deleteComment($comment);
396
    $this->deleteComment($comment_new_page);
397

    
398
    $this->drupalGet('node/' . $this->node->nid);
399
    $this->assertFalse($this->commentExists($comment), 'Comment not found.');
400
    $this->assertFalse($this->commentExists($reply, TRUE), 'Reply not found.');
401

    
402
    // Enabled comment form on node page.
403
    $this->drupalLogin($this->admin_user);
404
    $this->setCommentForm(TRUE);
405
    $this->drupalLogout();
406

    
407
    // Submit comment through node form.
408
    $this->drupalLogin($this->web_user);
409
    $this->drupalGet('node/' . $this->node->nid);
410
    $form_comment = $this->postComment(NULL, $this->randomName(), $this->randomName(), TRUE);
411
    $this->assertTrue($this->commentExists($form_comment), 'Form comment found.');
412

    
413
    // Disable comment form on node page.
414
    $this->drupalLogout();
415
    $this->drupalLogin($this->admin_user);
416
    $this->setCommentForm(FALSE);
417
  }
418

    
419
  /**
420
   * Tests new comment marker.
421
   */
422
  public function testCommentNewCommentsIndicator() {
423
    // Test if the right links are displayed when no comment is present for the
424
    // node.
425
    $this->drupalLogin($this->admin_user);
426
    $this->node = $this->drupalCreateNode(array('type' => 'article', 'promote' => 1, 'comment' => COMMENT_NODE_OPEN));
427
    $this->drupalGet('node');
428
    $this->assertNoLink(t('@count comments', array('@count' => 0)));
429
    $this->assertNoLink(t('@count new comments', array('@count' => 0)));
430
    $this->assertLink(t('Read more'));
431
    $count = $this->xpath('//div[@id=:id]/div[@class=:class]/ul/li', array(':id' => 'node-' . $this->node->nid, ':class' => 'link-wrapper'));
432
    $this->assertTrue(count($count) == 1, 'One child found');
433

    
434
    // Create a new comment. This helper function may be run with different
435
    // comment settings so use comment_save() to avoid complex setup.
436
    $comment = (object) array(
437
      'cid' => NULL,
438
      'nid' => $this->node->nid,
439
      'node_type' => $this->node->type,
440
      'pid' => 0,
441
      'uid' => $this->loggedInUser->uid,
442
      'status' => COMMENT_PUBLISHED,
443
      'subject' => $this->randomName(),
444
      'hostname' => ip_address(),
445
      'language' => LANGUAGE_NONE,
446
      'comment_body' => array(LANGUAGE_NONE => array($this->randomName())),
447
    );
448
    comment_save($comment);
449
    $this->drupalLogout();
450

    
451
    // Log in with 'web user' and check comment links.
452
    $this->drupalLogin($this->web_user);
453
    $this->drupalGet('node');
454
    $this->assertLink(t('1 new comment'));
455
    $this->clickLink(t('1 new comment'));
456
    $this->assertRaw('<a id="new"></a>', 'Found "new" marker.');
457
    $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.');
458

    
459
    // Test if "new comment" link is correctly removed.
460
    $this->drupalGet('node');
461
    $this->assertLink(t('1 comment'));
462
    $this->assertLink(t('Read more'));
463
    $this->assertNoLink(t('1 new comment'));
464
    $this->assertNoLink(t('@count new comments', array('@count' => 0)));
465
    $count = $this->xpath('//div[@id=:id]/div[@class=:class]/ul/li', array(':id' => 'node-' . $this->node->nid, ':class' => 'link-wrapper'));
466
    $this->assertTrue(count($count) == 2, print_r($count, TRUE));
467
  }
468

    
469
  /**
470
   * Tests CSS classes on comments.
471
   */
472
  function testCommentClasses() {
473
    // Create all permutations for comments, users, and nodes.
474
    $parameters = array(
475
      'node_uid' => array(0, $this->web_user->uid),
476
      'comment_uid' => array(0, $this->web_user->uid, $this->admin_user->uid),
477
      'comment_status' => array(COMMENT_PUBLISHED, COMMENT_NOT_PUBLISHED),
478
      'user' => array('anonymous', 'authenticated', 'admin'),
479
    );
480
    $permutations = $this->generatePermutations($parameters);
481

    
482
    foreach ($permutations as $case) {
483
      // Create a new node.
484
      $node = $this->drupalCreateNode(array('type' => 'article', 'uid' => $case['node_uid']));
485

    
486
      // Add a comment.
487
      $comment = (object) array(
488
        'cid' => NULL,
489
        'nid' => $node->nid,
490
        'pid' => 0,
491
        'uid' => $case['comment_uid'],
492
        'status' => $case['comment_status'],
493
        'subject' => $this->randomName(),
494
        'language' => LANGUAGE_NONE,
495
        'comment_body' => array(LANGUAGE_NONE => array($this->randomName())),
496
      );
497
      comment_save($comment);
498

    
499
      // Adjust the current/viewing user.
500
      switch ($case['user']) {
501
        case 'anonymous':
502
          $this->drupalLogout();
503
          $case['user_uid'] = 0;
504
          break;
505

    
506
        case 'authenticated':
507
          $this->drupalLogin($this->web_user);
508
          $case['user_uid'] = $this->web_user->uid;
509
          break;
510

    
511
        case 'admin':
512
          $this->drupalLogin($this->admin_user);
513
          $case['user_uid'] = $this->admin_user->uid;
514
          break;
515
      }
516
      // Request the node with the comment.
517
      $this->drupalGet('node/' . $node->nid);
518

    
519
      // Verify classes if the comment is visible for the current user.
520
      if ($case['comment_status'] == COMMENT_PUBLISHED || $case['user'] == 'admin') {
521
        // Verify the comment-by-anonymous class.
522
        $comments = $this->xpath('//*[contains(@class, "comment-by-anonymous")]');
523
        if ($case['comment_uid'] == 0) {
524
          $this->assertTrue(count($comments) == 1, 'comment-by-anonymous class found.');
525
        }
526
        else {
527
          $this->assertFalse(count($comments), 'comment-by-anonymous class not found.');
528
        }
529

    
530
        // Verify the comment-by-node-author class.
531
        $comments = $this->xpath('//*[contains(@class, "comment-by-node-author")]');
532
        if ($case['comment_uid'] > 0 && $case['comment_uid'] == $case['node_uid']) {
533
          $this->assertTrue(count($comments) == 1, 'comment-by-node-author class found.');
534
        }
535
        else {
536
          $this->assertFalse(count($comments), 'comment-by-node-author class not found.');
537
        }
538

    
539
        // Verify the comment-by-viewer class.
540
        $comments = $this->xpath('//*[contains(@class, "comment-by-viewer")]');
541
        if ($case['comment_uid'] > 0 && $case['comment_uid'] == $case['user_uid']) {
542
          $this->assertTrue(count($comments) == 1, 'comment-by-viewer class found.');
543
        }
544
        else {
545
          $this->assertFalse(count($comments), 'comment-by-viewer class not found.');
546
        }
547
      }
548

    
549
      // Verify the comment-unpublished class.
550
      $comments = $this->xpath('//*[contains(@class, "comment-unpublished")]');
551
      if ($case['comment_status'] == COMMENT_NOT_PUBLISHED && $case['user'] == 'admin') {
552
        $this->assertTrue(count($comments) == 1, 'comment-unpublished class found.');
553
      }
554
      else {
555
        $this->assertFalse(count($comments), 'comment-unpublished class not found.');
556
      }
557

    
558
      // Verify the comment-new class.
559
      if ($case['comment_status'] == COMMENT_PUBLISHED || $case['user'] == 'admin') {
560
        $comments = $this->xpath('//*[contains(@class, "comment-new")]');
561
        if ($case['user'] != 'anonymous') {
562
          $this->assertTrue(count($comments) == 1, 'comment-new class found.');
563

    
564
          // Request the node again. The comment-new class should disappear.
565
          $this->drupalGet('node/' . $node->nid);
566
          $comments = $this->xpath('//*[contains(@class, "comment-new")]');
567
          $this->assertFalse(count($comments), 'comment-new class not found.');
568
        }
569
        else {
570
          $this->assertFalse(count($comments), 'comment-new class not found.');
571
        }
572
      }
573
    }
574
  }
575

    
576
  /**
577
   * Tests the node comment statistics.
578
   */
579
  function testCommentNodeCommentStatistics() {
580
    $langcode = LANGUAGE_NONE;
581
    // Set comments to have subject and preview disabled.
582
    $this->drupalLogin($this->admin_user);
583
    $this->setCommentPreview(DRUPAL_DISABLED);
584
    $this->setCommentForm(TRUE);
585
    $this->setCommentSubject(FALSE);
586
    $this->setCommentSettings('comment_default_mode', COMMENT_MODE_THREADED, 'Comment paging changed.');
587
    $this->drupalLogout();
588

    
589
    // Creates a second user to post comments.
590
    $this->web_user2 = $this->drupalCreateUser(array('access comments', 'post comments', 'create article content', 'edit own comments'));
591

    
592
    // Checks the initial values of node comment statistics with no comment.
593
    $node = node_load($this->node->nid);
594
    $this->assertEqual($node->last_comment_timestamp, $this->node->created, 'The initial value of node last_comment_timestamp is the node created date.');
595
    $this->assertEqual($node->last_comment_name, NULL, 'The initial value of node last_comment_name is NULL.');
596
    $this->assertEqual($node->last_comment_uid, $this->web_user->uid, 'The initial value of node last_comment_uid is the node uid.');
597
    $this->assertEqual($node->comment_count, 0, 'The initial value of node comment_count is zero.');
598

    
599
    // Post comment #1 as web_user2.
600
    $this->drupalLogin($this->web_user2);
601
    $comment_text = $this->randomName();
602
    $comment = $this->postComment($this->node, $comment_text);
603
    $comment_loaded = comment_load($comment->id);
604

    
605
    // Checks the new values of node comment statistics with comment #1.
606
    // The node needs to be reloaded with a node_load_multiple cache reset.
607
    $node = node_load($this->node->nid, NULL, TRUE);
608
    $this->assertEqual($node->last_comment_name, NULL, 'The value of node last_comment_name is NULL.');
609
    $this->assertEqual($node->last_comment_uid, $this->web_user2->uid, 'The value of node last_comment_uid is the comment #1 uid.');
610
    $this->assertEqual($node->comment_count, 1, 'The value of node comment_count is 1.');
611

    
612
    // Prepare for anonymous comment submission (comment approval enabled).
613
    variable_set('user_register', USER_REGISTER_VISITORS);
614
    $this->drupalLogin($this->admin_user);
615
    user_role_change_permissions(DRUPAL_ANONYMOUS_RID, array(
616
      'access comments' => TRUE,
617
      'post comments' => TRUE,
618
      'skip comment approval' => FALSE,
619
    ));
620
    // Ensure that the poster can leave some contact info.
621
    $this->setCommentAnonymous('1');
622
    $this->drupalLogout();
623

    
624
    // Post comment #2 as anonymous (comment approval enabled).
625
    $this->drupalGet('comment/reply/' . $this->node->nid);
626
    $anonymous_comment = $this->postComment($this->node, $this->randomName(), '', TRUE);
627
    $comment_unpublished_loaded = comment_load($anonymous_comment->id);
628

    
629
    // Checks the new values of node comment statistics with comment #2 and
630
    // ensure they haven't changed since the comment has not been moderated.
631
    // The node needs to be reloaded with a node_load_multiple cache reset.
632
    $node = node_load($this->node->nid, NULL, TRUE);
633
    $this->assertEqual($node->last_comment_name, NULL, 'The value of node last_comment_name is still NULL.');
634
    $this->assertEqual($node->last_comment_uid, $this->web_user2->uid, 'The value of node last_comment_uid is still the comment #1 uid.');
635
    $this->assertEqual($node->comment_count, 1, 'The value of node comment_count is still 1.');
636

    
637
    // Prepare for anonymous comment submission (no approval required).
638
    $this->drupalLogin($this->admin_user);
639
    user_role_change_permissions(DRUPAL_ANONYMOUS_RID, array(
640
      'access comments' => TRUE,
641
      'post comments' => TRUE,
642
      'skip comment approval' => TRUE,
643
    ));
644
    $this->drupalLogout();
645

    
646
    // Post comment #3 as anonymous.
647
    $this->drupalGet('comment/reply/' . $this->node->nid);
648
    $anonymous_comment = $this->postComment($this->node, $this->randomName(), '', array('name' => $this->randomName()));
649
    $comment_loaded = comment_load($anonymous_comment->id);
650

    
651
    // Checks the new values of node comment statistics with comment #3.
652
    // The node needs to be reloaded with a node_load_multiple cache reset.
653
    $node = node_load($this->node->nid, NULL, TRUE);
654
    $this->assertEqual($node->last_comment_name, $comment_loaded->name, 'The value of node last_comment_name is the name of the anonymous user.');
655
    $this->assertEqual($node->last_comment_uid, 0, 'The value of node last_comment_uid is zero.');
656
    $this->assertEqual($node->comment_count, 2, 'The value of node comment_count is 2.');
657
  }
658

    
659
  /**
660
   * Tests comment links.
661
   *
662
   * The output of comment links depends on various environment conditions:
663
   * - Various Comment module configuration settings, user registration
664
   *   settings, and user access permissions.
665
   * - Whether the user is authenticated or not, and whether any comments exist.
666
   *
667
   * To account for all possible cases, this test creates permutations of all
668
   * possible conditions and tests the expected appearance of comment links in
669
   * each environment.
670
   */
671
  function testCommentLinks() {
672
    // Bartik theme alters comment links, so use a different theme.
673
    theme_enable(array('garland'));
674
    variable_set('theme_default', 'garland');
675

    
676
    // Remove additional user permissions from $this->web_user added by setUp(),
677
    // since this test is limited to anonymous and authenticated roles only.
678
    user_role_delete(key($this->web_user->roles));
679

    
680
    // Matrix of possible environmental conditions and configuration settings.
681
    // See setEnvironment() for details.
682
    $conditions = array(
683
      'authenticated'   => array(FALSE, TRUE),
684
      'comment count'   => array(FALSE, TRUE),
685
      'access comments' => array(0, 1),
686
      'post comments'   => array(0, 1),
687
      'form'            => array(COMMENT_FORM_BELOW, COMMENT_FORM_SEPARATE_PAGE),
688
      // USER_REGISTER_VISITORS_ADMINISTRATIVE_APPROVAL is irrelevant for this
689
      // test; there is only a difference between open and closed registration.
690
      'user_register'   => array(USER_REGISTER_VISITORS, USER_REGISTER_ADMINISTRATORS_ONLY),
691
      // @todo Complete test coverage for:
692
      //'comments'        => array(COMMENT_NODE_OPEN, COMMENT_NODE_CLOSED, COMMENT_NODE_HIDDEN),
693
      //// COMMENT_ANONYMOUS_MUST_CONTACT is irrelevant for this test.
694
      //'contact '        => array(COMMENT_ANONYMOUS_MAY_CONTACT, COMMENT_ANONYMOUS_MAYNOT_CONTACT),
695
    );
696

    
697
    $environments = $this->generatePermutations($conditions);
698
    foreach ($environments as $info) {
699
      $this->assertCommentLinks($info);
700
    }
701
  }
702

    
703
  /**
704
   * Re-configures the environment, module settings, and user permissions.
705
   *
706
   * @param $info
707
   *   An associative array describing the environment to setup:
708
   *   - Environment conditions:
709
   *     - authenticated: Boolean whether to test with $this->web_user or
710
   *       anonymous.
711
   *     - comment count: Boolean whether to test with a new/unread comment on
712
   *       $this->node or no comments.
713
   *   - Configuration settings:
714
   *     - form: COMMENT_FORM_BELOW or COMMENT_FORM_SEPARATE_PAGE.
715
   *     - user_register: USER_REGISTER_ADMINISTRATORS_ONLY or
716
   *       USER_REGISTER_VISITORS.
717
   *     - contact: COMMENT_ANONYMOUS_MAY_CONTACT or
718
   *       COMMENT_ANONYMOUS_MAYNOT_CONTACT.
719
   *     - comments: COMMENT_NODE_OPEN, COMMENT_NODE_CLOSED, or
720
   *       COMMENT_NODE_HIDDEN.
721
   *   - User permissions:
722
   *     These are granted or revoked for the user, according to the
723
   *     'authenticated' flag above. Pass 0 or 1 as parameter values. See
724
   *     user_role_change_permissions().
725
   *     - access comments
726
   *     - post comments
727
   *     - skip comment approval
728
   *     - edit own comments
729
   */
730
  function setEnvironment(array $info) {
731
    static $current;
732

    
733
    // Apply defaults to initial environment.
734
    if (!isset($current)) {
735
      $current = array(
736
        'authenticated' => FALSE,
737
        'comment count' => FALSE,
738
        'form' => COMMENT_FORM_BELOW,
739
        'user_register' => USER_REGISTER_VISITORS,
740
        'contact' => COMMENT_ANONYMOUS_MAY_CONTACT,
741
        'comments' => COMMENT_NODE_OPEN,
742
        'access comments' => 0,
743
        'post comments' => 0,
744
        // Enabled by default, because it's irrelevant for this test.
745
        'skip comment approval' => 1,
746
        'edit own comments' => 0,
747
      );
748
    }
749
    // Complete new environment with current environment.
750
    $info = array_merge($current, $info);
751

    
752
    // Change environment conditions.
753
    if ($current['authenticated'] != $info['authenticated']) {
754
      if ($this->loggedInUser) {
755
        $this->drupalLogout();
756
      }
757
      else {
758
        $this->drupalLogin($this->web_user);
759
      }
760
    }
761
    if ($current['comment count'] != $info['comment count']) {
762
      if ($info['comment count']) {
763
        // Create a comment via CRUD API functionality, since
764
        // $this->postComment() relies on actual user permissions.
765
        $comment = (object) array(
766
          'cid' => NULL,
767
          'nid' => $this->node->nid,
768
          'node_type' => $this->node->type,
769
          'pid' => 0,
770
          'uid' => 0,
771
          'status' => COMMENT_PUBLISHED,
772
          'subject' => $this->randomName(),
773
          'hostname' => ip_address(),
774
          'language' => LANGUAGE_NONE,
775
          'comment_body' => array(LANGUAGE_NONE => array($this->randomName())),
776
        );
777
        comment_save($comment);
778
        $this->comment = $comment;
779

    
780
        // comment_num_new() relies on node_last_viewed(), so ensure that no one
781
        // has seen the node of this comment.
782
        db_delete('history')->condition('nid', $this->node->nid)->execute();
783
      }
784
      else {
785
        $cids = db_query("SELECT cid FROM {comment}")->fetchCol();
786
        comment_delete_multiple($cids);
787
        unset($this->comment);
788
      }
789
    }
790

    
791
    // Change comment settings.
792
    variable_set('comment_form_location_' . $this->node->type, $info['form']);
793
    variable_set('comment_anonymous_' . $this->node->type, $info['contact']);
794
    if ($this->node->comment != $info['comments']) {
795
      $this->node->comment = $info['comments'];
796
      node_save($this->node);
797
    }
798

    
799
    // Change user settings.
800
    variable_set('user_register', $info['user_register']);
801

    
802
    // Change user permissions.
803
    $rid = ($this->loggedInUser ? DRUPAL_AUTHENTICATED_RID : DRUPAL_ANONYMOUS_RID);
804
    $perms = array_intersect_key($info, array('access comments' => 1, 'post comments' => 1, 'skip comment approval' => 1, 'edit own comments' => 1));
805
    user_role_change_permissions($rid, $perms);
806

    
807
    // Output verbose debugging information.
808
    // @see DrupalTestCase::error()
809
    $t_form = array(
810
      COMMENT_FORM_BELOW => 'below',
811
      COMMENT_FORM_SEPARATE_PAGE => 'separate page',
812
    );
813
    $t_contact = array(
814
      COMMENT_ANONYMOUS_MAY_CONTACT => 'optional',
815
      COMMENT_ANONYMOUS_MAYNOT_CONTACT => 'disabled',
816
      COMMENT_ANONYMOUS_MUST_CONTACT => 'required',
817
    );
818
    $t_comments = array(
819
      COMMENT_NODE_OPEN => 'open',
820
      COMMENT_NODE_CLOSED => 'closed',
821
      COMMENT_NODE_HIDDEN => 'hidden',
822
    );
823
    $verbose = $info;
824
    $verbose['form'] = $t_form[$info['form']];
825
    $verbose['contact'] = $t_contact[$info['contact']];
826
    $verbose['comments'] = $t_comments[$info['comments']];
827
    $message = t('Changed environment:<pre>@verbose</pre>', array(
828
      '@verbose' => var_export($verbose, TRUE),
829
    ));
830
    $this->assert('debug', $message, 'Debug');
831

    
832
    // Update current environment.
833
    $current = $info;
834

    
835
    return $info;
836
  }
837

    
838
  /**
839
   * Asserts that comment links appear according to the passed environment setup.
840
   *
841
   * @param $info
842
   *   An associative array describing the environment to pass to
843
   *   setEnvironment().
844
   */
845
  function assertCommentLinks(array $info) {
846
    $info = $this->setEnvironment($info);
847

    
848
    $nid = $this->node->nid;
849

    
850
    foreach (array('', "node/$nid") as $path) {
851
      $this->drupalGet($path);
852

    
853
      // User is allowed to view comments.
854
      if ($info['access comments']) {
855
        if ($path == '') {
856
          // In teaser view, a link containing the comment count is always
857
          // expected.
858
          if ($info['comment count']) {
859
            $this->assertLink(t('1 comment'));
860

    
861
            // For logged in users, a link containing the amount of new/unread
862
            // comments is expected.
863
            // See important note about comment_num_new() below.
864
            if ($this->loggedInUser && isset($this->comment) && !isset($this->comment->seen)) {
865
              $this->assertLink(t('1 new comment'));
866
              $this->comment->seen = TRUE;
867
            }
868
          }
869
        }
870
      }
871
      else {
872
        $this->assertNoLink(t('1 comment'));
873
        $this->assertNoLink(t('1 new comment'));
874
      }
875
      // comment_num_new() is based on node views, so comments are marked as
876
      // read when a node is viewed, regardless of whether we have access to
877
      // comments.
878
      if ($path == "node/$nid" && $this->loggedInUser && isset($this->comment)) {
879
        $this->comment->seen = TRUE;
880
      }
881

    
882
      // User is not allowed to post comments.
883
      if (!$info['post comments']) {
884
        $this->assertNoLink('Add new comment');
885

    
886
        // Anonymous users should see a note to log in or register in case
887
        // authenticated users are allowed to post comments.
888
        // @see theme_comment_post_forbidden()
889
        if (!$this->loggedInUser) {
890
          if (user_access('post comments', $this->web_user)) {
891
            // The note depends on whether users are actually able to register.
892
            if ($info['user_register']) {
893
              $this->assertText('Log in or register to post comments');
894
            }
895
            else {
896
              $this->assertText('Log in to post comments');
897
            }
898
          }
899
          else {
900
            $this->assertNoText('Log in or register to post comments');
901
            $this->assertNoText('Log in to post comments');
902
          }
903
        }
904
      }
905
      // User is allowed to post comments.
906
      else {
907
        $this->assertNoText('Log in or register to post comments');
908

    
909
        // "Add new comment" is always expected, except when there are no
910
        // comments or if the user cannot see them.
911
        if ($path == "node/$nid" && $info['form'] == COMMENT_FORM_BELOW && (!$info['comment count'] || !$info['access comments'])) {
912
          $this->assertNoLink('Add new comment');
913
        }
914
        else {
915
          $this->assertLink('Add new comment');
916
        }
917

    
918
        // Also verify that the comment form appears according to the configured
919
        // location.
920
        if ($path == "node/$nid") {
921
          $elements = $this->xpath('//form[@id=:id]', array(':id' => 'comment-form'));
922
          if ($info['form'] == COMMENT_FORM_BELOW) {
923
            $this->assertTrue(count($elements), 'Comment form found below.');
924
          }
925
          else {
926
            $this->assertFalse(count($elements), 'Comment form not found below.');
927
          }
928
        }
929
      }
930
    }
931
  }
932
}
933

    
934
/**
935
 * Test previewing comments.
936
 */
937
class CommentPreviewTest extends CommentHelperCase {
938
  public static function getInfo() {
939
    return array(
940
      'name' => 'Comment preview',
941
      'description' => 'Test comment preview.',
942
      'group' => 'Comment',
943
    );
944
  }
945

    
946
  /**
947
   * Test comment preview.
948
   */
949
  function testCommentPreview() {
950
    $langcode = LANGUAGE_NONE;
951

    
952
    // As admin user, configure comment settings.
953
    $this->drupalLogin($this->admin_user);
954
    $this->setCommentPreview(DRUPAL_OPTIONAL);
955
    $this->setCommentForm(TRUE);
956
    $this->setCommentSubject(TRUE);
957
    $this->setCommentSettings('comment_default_mode', COMMENT_MODE_THREADED, 'Comment paging changed.');
958
    $this->drupalLogout();
959

    
960
    // Login as web user and add a signature and a user picture.
961
    $this->drupalLogin($this->web_user);
962
    variable_set('user_signatures', 1);
963
    variable_set('user_pictures', 1);
964
    $test_signature = $this->randomName();
965
    $edit['signature[value]'] = '<a href="http://example.com/">' . $test_signature. '</a>';
966
    $edit['signature[format]'] = 'filtered_html';
967
    $image = current($this->drupalGetTestFiles('image'));
968
    $edit['files[picture_upload]'] = drupal_realpath($image->uri);
969
    $this->drupalPost('user/' . $this->web_user->uid . '/edit', $edit, t('Save'));
970

    
971
    // As the web user, fill in the comment form and preview the comment.
972
    $edit = array();
973
    $edit['subject'] = $this->randomName(8);
974
    $edit['comment_body[' . $langcode . '][0][value]'] = $this->randomName(16);
975
    $this->drupalPost('node/' . $this->node->nid, $edit, t('Preview'));
976

    
977
    // Check that the preview is displaying the title and body.
978
    $this->assertTitle(t('Preview comment | Drupal'), 'Page title is "Preview comment".');
979
    $this->assertText($edit['subject'], 'Subject displayed.');
980
    $this->assertText($edit['comment_body[' . $langcode . '][0][value]'], 'Comment displayed.');
981

    
982
    // Check that the title and body fields are displayed with the correct values.
983
    $this->assertFieldByName('subject', $edit['subject'], 'Subject field displayed.');
984
    $this->assertFieldByName('comment_body[' . $langcode . '][0][value]', $edit['comment_body[' . $langcode . '][0][value]'], 'Comment field displayed.');
985

    
986
    // Check that the signature is displaying with the correct text format.
987
    $this->assertLink($test_signature);
988

    
989
    // Check that the user picture is displayed.
990
    $this->assertFieldByXPath("//div[contains(@class, 'comment-preview')]//div[contains(@class, 'user-picture')]//img", NULL, 'User picture displayed.');
991
  }
992

    
993
  /**
994
   * Test comment edit, preview, and save.
995
   */
996
  function testCommentEditPreviewSave() {
997
    $langcode = LANGUAGE_NONE;
998
    $web_user = $this->drupalCreateUser(array('access comments', 'post comments', 'skip comment approval'));
999
    $this->drupalLogin($this->admin_user);
1000
    $this->setCommentPreview(DRUPAL_OPTIONAL);
1001
    $this->setCommentForm(TRUE);
1002
    $this->setCommentSubject(TRUE);
1003
    $this->setCommentSettings('comment_default_mode', COMMENT_MODE_THREADED, 'Comment paging changed.');
1004

    
1005
    $edit = array();
1006
    $edit['subject'] = $this->randomName(8);
1007
    $edit['comment_body[' . $langcode . '][0][value]'] = $this->randomName(16);
1008
    $edit['name'] = $web_user->name;
1009
    $edit['date'] = '2008-03-02 17:23 +0300';
1010
    $raw_date = strtotime($edit['date']);
1011
    $expected_text_date = format_date($raw_date);
1012
    $expected_form_date = format_date($raw_date, 'custom', 'Y-m-d H:i O');
1013
    $comment = $this->postComment($this->node, $edit['subject'], $edit['comment_body[' . $langcode . '][0][value]'], TRUE);
1014
    $this->drupalPost('comment/' . $comment->id . '/edit', $edit, t('Preview'));
1015

    
1016
    // Check that the preview is displaying the subject, comment, author and date correctly.
1017
    $this->assertTitle(t('Preview comment | Drupal'), 'Page title is "Preview comment".');
1018
    $this->assertText($edit['subject'], 'Subject displayed.');
1019
    $this->assertText($edit['comment_body[' . $langcode . '][0][value]'], 'Comment displayed.');
1020
    $this->assertText($edit['name'], 'Author displayed.');
1021
    $this->assertText($expected_text_date, 'Date displayed.');
1022

    
1023
    // Check that the subject, comment, author and date fields are displayed with the correct values.
1024
    $this->assertFieldByName('subject', $edit['subject'], 'Subject field displayed.');
1025
    $this->assertFieldByName('comment_body[' . $langcode . '][0][value]', $edit['comment_body[' . $langcode . '][0][value]'], 'Comment field displayed.');
1026
    $this->assertFieldByName('name', $edit['name'], 'Author field displayed.');
1027
    $this->assertFieldByName('date', $edit['date'], 'Date field displayed.');
1028

    
1029
    // Check that saving a comment produces a success message.
1030
    $this->drupalPost('comment/' . $comment->id . '/edit', $edit, t('Save'));
1031
    $this->assertText(t('Your comment has been posted.'), 'Comment posted.');
1032

    
1033
    // Check that the comment fields are correct after loading the saved comment.
1034
    $this->drupalGet('comment/' . $comment->id . '/edit');
1035
    $this->assertFieldByName('subject', $edit['subject'], 'Subject field displayed.');
1036
    $this->assertFieldByName('comment_body[' . $langcode . '][0][value]', $edit['comment_body[' . $langcode . '][0][value]'], 'Comment field displayed.');
1037
    $this->assertFieldByName('name', $edit['name'], 'Author field displayed.');
1038
    $this->assertFieldByName('date', $expected_form_date, 'Date field displayed.');
1039

    
1040
    // Submit the form using the displayed values.
1041
    $displayed = array();
1042
    $displayed['subject'] = (string) current($this->xpath("//input[@id='edit-subject']/@value"));
1043
    $displayed['comment_body[' . $langcode . '][0][value]'] = (string) current($this->xpath("//textarea[@id='edit-comment-body-" . $langcode . "-0-value']"));
1044
    $displayed['name'] = (string) current($this->xpath("//input[@id='edit-name']/@value"));
1045
    $displayed['date'] = (string) current($this->xpath("//input[@id='edit-date']/@value"));
1046
    $this->drupalPost('comment/' . $comment->id . '/edit', $displayed, t('Save'));
1047

    
1048
    // Check that the saved comment is still correct.
1049
    $comment_loaded = comment_load($comment->id);
1050
    $this->assertEqual($comment_loaded->subject, $edit['subject'], 'Subject loaded.');
1051
    $this->assertEqual($comment_loaded->comment_body[$langcode][0]['value'], $edit['comment_body[' . $langcode . '][0][value]'], 'Comment body loaded.');
1052
    $this->assertEqual($comment_loaded->name, $edit['name'], 'Name loaded.');
1053
    $this->assertEqual($comment_loaded->created, $raw_date, 'Date loaded.');
1054

    
1055
  }
1056

    
1057
}
1058

    
1059
class CommentAnonymous extends CommentHelperCase {
1060
  public static function getInfo() {
1061
    return array(
1062
      'name' => 'Anonymous comments',
1063
      'description' => 'Test anonymous comments.',
1064
      'group' => 'Comment',
1065
    );
1066
  }
1067

    
1068
  function setUp() {
1069
    parent::setUp();
1070
    variable_set('user_register', USER_REGISTER_VISITORS);
1071
  }
1072

    
1073
  /**
1074
   * Test anonymous comment functionality.
1075
   */
1076
  function testAnonymous() {
1077
    $this->drupalLogin($this->admin_user);
1078
    // Enabled anonymous user comments.
1079
    user_role_change_permissions(DRUPAL_ANONYMOUS_RID, array(
1080
      'access comments' => TRUE,
1081
      'post comments' => TRUE,
1082
      'skip comment approval' => TRUE,
1083
    ));
1084
    $this->setCommentAnonymous('0'); // Ensure that doesn't require contact info.
1085
    $this->drupalLogout();
1086

    
1087
    // Post anonymous comment without contact info.
1088
    $anonymous_comment1 = $this->postComment($this->node, $this->randomName(), $this->randomName());
1089
    $this->assertTrue($this->commentExists($anonymous_comment1), 'Anonymous comment without contact info found.');
1090

    
1091
    // Allow contact info.
1092
    $this->drupalLogin($this->admin_user);
1093
    $this->setCommentAnonymous('1');
1094

    
1095
    // Attempt to edit anonymous comment.
1096
    $this->drupalGet('comment/' . $anonymous_comment1->id . '/edit');
1097
    $edited_comment = $this->postComment(NULL, $this->randomName(), $this->randomName());
1098
    $this->assertTrue($this->commentExists($edited_comment, FALSE), 'Modified reply found.');
1099
    $this->drupalLogout();
1100

    
1101
    // Post anonymous comment with contact info (optional).
1102
    $this->drupalGet('comment/reply/' . $this->node->nid);
1103
    $this->assertTrue($this->commentContactInfoAvailable(), 'Contact information available.');
1104

    
1105
    $anonymous_comment2 = $this->postComment($this->node, $this->randomName(), $this->randomName());
1106
    $this->assertTrue($this->commentExists($anonymous_comment2), 'Anonymous comment with contact info (optional) found.');
1107

    
1108
    // Ensure anonymous users cannot post in the name of registered users.
1109
    $langcode = LANGUAGE_NONE;
1110
    $edit = array(
1111
      'name' => $this->admin_user->name,
1112
      'mail' => $this->randomName() . '@example.com',
1113
      'subject' => $this->randomName(),
1114
      "comment_body[$langcode][0][value]" => $this->randomName(),
1115
    );
1116
    $this->drupalPost('comment/reply/' . $this->node->nid, $edit, t('Save'));
1117
    $this->assertText(t('The name you used belongs to a registered user.'));
1118

    
1119
    // Require contact info.
1120
    $this->drupalLogin($this->admin_user);
1121
    $this->setCommentAnonymous('2');
1122
    $this->drupalLogout();
1123

    
1124
    // Try to post comment with contact info (required).
1125
    $this->drupalGet('comment/reply/' . $this->node->nid);
1126
    $this->assertTrue($this->commentContactInfoAvailable(), 'Contact information available.');
1127

    
1128
    $anonymous_comment3 = $this->postComment($this->node, $this->randomName(), $this->randomName(), TRUE);
1129
    // Name should have 'Anonymous' for value by default.
1130
    $this->assertText(t('E-mail field is required.'), 'E-mail required.');
1131
    $this->assertFalse($this->commentExists($anonymous_comment3), 'Anonymous comment with contact info (required) not found.');
1132

    
1133
    // Post comment with contact info (required).
1134
    $author_name = $this->randomName();
1135
    $author_mail = $this->randomName() . '@example.com';
1136
    $anonymous_comment3 = $this->postComment($this->node, $this->randomName(), $this->randomName(), array('name' => $author_name, 'mail' => $author_mail));
1137
    $this->assertTrue($this->commentExists($anonymous_comment3), 'Anonymous comment with contact info (required) found.');
1138

    
1139
    // Make sure the user data appears correctly when editing the comment.
1140
    $this->drupalLogin($this->admin_user);
1141
    $this->drupalGet('comment/' . $anonymous_comment3->id . '/edit');
1142
    $this->assertRaw($author_name, "The anonymous user's name is correct when editing the comment.");
1143
    $this->assertRaw($author_mail, "The anonymous user's e-mail address is correct when editing the comment.");
1144

    
1145
    // Unpublish comment.
1146
    $this->performCommentOperation($anonymous_comment3, 'unpublish');
1147

    
1148
    $this->drupalGet('admin/content/comment/approval');
1149
    $this->assertRaw('comments[' . $anonymous_comment3->id . ']', 'Comment was unpublished.');
1150

    
1151
    // Publish comment.
1152
    $this->performCommentOperation($anonymous_comment3, 'publish', TRUE);
1153

    
1154
    $this->drupalGet('admin/content/comment');
1155
    $this->assertRaw('comments[' . $anonymous_comment3->id . ']', 'Comment was published.');
1156

    
1157
    // Delete comment.
1158
    $this->performCommentOperation($anonymous_comment3, 'delete');
1159

    
1160
    $this->drupalGet('admin/content/comment');
1161
    $this->assertNoRaw('comments[' . $anonymous_comment3->id . ']', 'Comment was deleted.');
1162
    $this->drupalLogout();
1163

    
1164
    // Reset.
1165
    user_role_change_permissions(DRUPAL_ANONYMOUS_RID, array(
1166
      'access comments' => FALSE,
1167
      'post comments' => FALSE,
1168
      'skip comment approval' => FALSE,
1169
    ));
1170

    
1171
    // Attempt to view comments while disallowed.
1172
    // NOTE: if authenticated user has permission to post comments, then a
1173
    // "Login or register to post comments" type link may be shown.
1174
    $this->drupalGet('node/' . $this->node->nid);
1175
    $this->assertNoPattern('@<h2[^>]*>Comments</h2>@', 'Comments were not displayed.');
1176
    $this->assertNoLink('Add new comment', 'Link to add comment was found.');
1177

    
1178
    // Attempt to view node-comment form while disallowed.
1179
    $this->drupalGet('comment/reply/' . $this->node->nid);
1180
    $this->assertText('You are not authorized to post comments', 'Error attempting to post comment.');
1181
    $this->assertNoFieldByName('subject', '', 'Subject field not found.');
1182
    $this->assertNoFieldByName("comment_body[$langcode][0][value]", '', 'Comment field not found.');
1183

    
1184
    user_role_change_permissions(DRUPAL_ANONYMOUS_RID, array(
1185
      'access comments' => TRUE,
1186
      'post comments' => FALSE,
1187
      'skip comment approval' => FALSE,
1188
    ));
1189
    $this->drupalGet('node/' . $this->node->nid);
1190
    $this->assertPattern('@<h2[^>]*>Comments</h2>@', 'Comments were displayed.');
1191
    $this->assertLink('Log in', 1, 'Link to log in was found.');
1192
    $this->assertLink('register', 1, 'Link to register was found.');
1193

    
1194
    user_role_change_permissions(DRUPAL_ANONYMOUS_RID, array(
1195
      'access comments' => FALSE,
1196
      'post comments' => TRUE,
1197
      'skip comment approval' => TRUE,
1198
    ));
1199
    $this->drupalGet('node/' . $this->node->nid);
1200
    $this->assertNoPattern('@<h2[^>]*>Comments</h2>@', 'Comments were not displayed.');
1201
    $this->assertFieldByName('subject', '', 'Subject field found.');
1202
    $this->assertFieldByName("comment_body[$langcode][0][value]", '', 'Comment field found.');
1203

    
1204
    $this->drupalGet('comment/reply/' . $this->node->nid . '/' . $anonymous_comment3->id);
1205
    $this->assertText('You are not authorized to view comments', 'Error attempting to post reply.');
1206
    $this->assertNoText($author_name, 'Comment not displayed.');
1207
  }
1208
}
1209

    
1210
/**
1211
 * Verify pagination of comments.
1212
 */
1213
class CommentPagerTest extends CommentHelperCase {
1214

    
1215
  public static function getInfo() {
1216
    return array(
1217
      'name' => 'Comment paging settings',
1218
      'description' => 'Test paging of comments and their settings.',
1219
      'group' => 'Comment',
1220
    );
1221
  }
1222

    
1223
  /**
1224
   * Confirm comment paging works correctly with flat and threaded comments.
1225
   */
1226
  function testCommentPaging() {
1227
    $this->drupalLogin($this->admin_user);
1228

    
1229
    // Set comment variables.
1230
    $this->setCommentForm(TRUE);
1231
    $this->setCommentSubject(TRUE);
1232
    $this->setCommentPreview(DRUPAL_DISABLED);
1233

    
1234
    // Create a node and three comments.
1235
    $node = $this->drupalCreateNode(array('type' => 'article', 'promote' => 1));
1236
    $comments = array();
1237
    $comments[] = $this->postComment($node, $this->randomName(), $this->randomName(), TRUE);
1238
    $comments[] = $this->postComment($node, $this->randomName(), $this->randomName(), TRUE);
1239
    $comments[] = $this->postComment($node, $this->randomName(), $this->randomName(), TRUE);
1240

    
1241
    $this->setCommentSettings('comment_default_mode', COMMENT_MODE_FLAT, 'Comment paging changed.');
1242

    
1243
    // Set comments to one per page so that we are able to test paging without
1244
    // needing to insert large numbers of comments.
1245
    $this->setCommentsPerPage(1);
1246

    
1247
    // Check the first page of the node, and confirm the correct comments are
1248
    // shown.
1249
    $this->drupalGet('node/' . $node->nid);
1250
    $this->assertRaw(t('next'), 'Paging links found.');
1251
    $this->assertTrue($this->commentExists($comments[0]), 'Comment 1 appears on page 1.');
1252
    $this->assertFalse($this->commentExists($comments[1]), 'Comment 2 does not appear on page 1.');
1253
    $this->assertFalse($this->commentExists($comments[2]), 'Comment 3 does not appear on page 1.');
1254

    
1255
    // Check the second page.
1256
    $this->drupalGet('node/' . $node->nid, array('query' => array('page' => 1)));
1257
    $this->assertTrue($this->commentExists($comments[1]), 'Comment 2 appears on page 2.');
1258
    $this->assertFalse($this->commentExists($comments[0]), 'Comment 1 does not appear on page 2.');
1259
    $this->assertFalse($this->commentExists($comments[2]), 'Comment 3 does not appear on page 2.');
1260

    
1261
    // Check the third page.
1262
    $this->drupalGet('node/' . $node->nid, array('query' => array('page' => 2)));
1263
    $this->assertTrue($this->commentExists($comments[2]), 'Comment 3 appears on page 3.');
1264
    $this->assertFalse($this->commentExists($comments[0]), 'Comment 1 does not appear on page 3.');
1265
    $this->assertFalse($this->commentExists($comments[1]), 'Comment 2 does not appear on page 3.');
1266

    
1267
    // Post a reply to the oldest comment and test again.
1268
    $replies = array();
1269
    $oldest_comment = reset($comments);
1270
    $this->drupalGet('comment/reply/' . $node->nid . '/' . $oldest_comment->id);
1271
    $reply = $this->postComment(NULL, $this->randomName(), $this->randomName(), TRUE);
1272

    
1273
    $this->setCommentsPerPage(2);
1274
    // We are still in flat view - the replies should not be on the first page,
1275
    // even though they are replies to the oldest comment.
1276
    $this->drupalGet('node/' . $node->nid, array('query' => array('page' => 0)));
1277
    $this->assertFalse($this->commentExists($reply, TRUE), 'In flat mode, reply does not appear on page 1.');
1278

    
1279
    // If we switch to threaded mode, the replies on the oldest comment
1280
    // should be bumped to the first page and comment 6 should be bumped
1281
    // to the second page.
1282
    $this->setCommentSettings('comment_default_mode', COMMENT_MODE_THREADED, 'Switched to threaded mode.');
1283
    $this->drupalGet('node/' . $node->nid, array('query' => array('page' => 0)));
1284
    $this->assertTrue($this->commentExists($reply, TRUE), 'In threaded mode, reply appears on page 1.');
1285
    $this->assertFalse($this->commentExists($comments[1]), 'In threaded mode, comment 2 has been bumped off of page 1.');
1286

    
1287
    // If (# replies > # comments per page) in threaded expanded view,
1288
    // the overage should be bumped.
1289
    $reply2 = $this->postComment(NULL, $this->randomName(), $this->randomName(), TRUE);
1290
    $this->drupalGet('node/' . $node->nid, array('query' => array('page' => 0)));
1291
    $this->assertFalse($this->commentExists($reply2, TRUE), 'In threaded mode where # replies > # comments per page, the newest reply does not appear on page 1.');
1292

    
1293
    $this->drupalLogout();
1294
  }
1295

    
1296
  /**
1297
   * Test comment ordering and threading.
1298
   */
1299
  function testCommentOrderingThreading() {
1300
    $this->drupalLogin($this->admin_user);
1301

    
1302
    // Set comment variables.
1303
    $this->setCommentForm(TRUE);
1304
    $this->setCommentSubject(TRUE);
1305
    $this->setCommentPreview(DRUPAL_DISABLED);
1306

    
1307
    // Display all the comments on the same page.
1308
    $this->setCommentsPerPage(1000);
1309

    
1310
    // Create a node and three comments.
1311
    $node = $this->drupalCreateNode(array('type' => 'article', 'promote' => 1));
1312
    $comments = array();
1313
    $comments[] = $this->postComment($node, $this->randomName(), $this->randomName(), TRUE);
1314
    $comments[] = $this->postComment($node, $this->randomName(), $this->randomName(), TRUE);
1315
    $comments[] = $this->postComment($node, $this->randomName(), $this->randomName(), TRUE);
1316

    
1317
    // Post a reply to the second comment.
1318
    $this->drupalGet('comment/reply/' . $node->nid . '/' . $comments[1]->id);
1319
    $comments[] = $this->postComment(NULL, $this->randomName(), $this->randomName(), TRUE);
1320

    
1321
    // Post a reply to the first comment.
1322
    $this->drupalGet('comment/reply/' . $node->nid . '/' . $comments[0]->id);
1323
    $comments[] = $this->postComment(NULL, $this->randomName(), $this->randomName(), TRUE);
1324

    
1325
    // Post a reply to the last comment.
1326
    $this->drupalGet('comment/reply/' . $node->nid . '/' . $comments[2]->id);
1327
    $comments[] = $this->postComment(NULL, $this->randomName(), $this->randomName(), TRUE);
1328

    
1329
    // Post a reply to the second comment.
1330
    $this->drupalGet('comment/reply/' . $node->nid . '/' . $comments[3]->id);
1331
    $comments[] = $this->postComment(NULL, $this->randomName(), $this->randomName(), TRUE);
1332

    
1333
    // At this point, the comment tree is:
1334
    // - 0
1335
    //   - 4
1336
    // - 1
1337
    //   - 3
1338
    //     - 6
1339
    // - 2
1340
    //   - 5
1341

    
1342
    $this->setCommentSettings('comment_default_mode', COMMENT_MODE_FLAT, 'Comment paging changed.');
1343

    
1344
    $expected_order = array(
1345
      0,
1346
      1,
1347
      2,
1348
      3,
1349
      4,
1350
      5,
1351
      6,
1352
    );
1353
    $this->drupalGet('node/' . $node->nid);
1354
    $this->assertCommentOrder($comments, $expected_order);
1355

    
1356
    $this->setCommentSettings('comment_default_mode', COMMENT_MODE_THREADED, 'Switched to threaded mode.');
1357

    
1358
    $expected_order = array(
1359
      0,
1360
      4,
1361
      1,
1362
      3,
1363
      6,
1364
      2,
1365
      5,
1366
    );
1367
    $this->drupalGet('node/' . $node->nid);
1368
    $this->assertCommentOrder($comments, $expected_order);
1369
  }
1370

    
1371
  /**
1372
   * Helper function: assert that the comments are displayed in the correct order.
1373
   *
1374
   * @param $comments
1375
   *   And array of comments.
1376
   * @param $expected_order
1377
   *   An array of keys from $comments describing the expected order.
1378
   */
1379
  function assertCommentOrder(array $comments, array $expected_order) {
1380
    $expected_cids = array();
1381

    
1382
    // First, rekey the expected order by cid.
1383
    foreach ($expected_order as $key) {
1384
      $expected_cids[] = $comments[$key]->id;
1385
    }
1386

    
1387
    $comment_anchors = $this->xpath('//a[starts-with(@id,"comment-")]');
1388
    $result_order = array();
1389
    foreach ($comment_anchors as $anchor) {
1390
      $result_order[] = substr($anchor['id'], 8);
1391
    }
1392

    
1393
    return $this->assertIdentical($expected_cids, $result_order, format_string('Comment order: expected @expected, returned @returned.', array('@expected' => implode(',', $expected_cids), '@returned' => implode(',', $result_order))));
1394
  }
1395

    
1396
  /**
1397
   * Test comment_new_page_count().
1398
   */
1399
  function testCommentNewPageIndicator() {
1400
    $this->drupalLogin($this->admin_user);
1401

    
1402
    // Set comment variables.
1403
    $this->setCommentForm(TRUE);
1404
    $this->setCommentSubject(TRUE);
1405
    $this->setCommentPreview(DRUPAL_DISABLED);
1406

    
1407
    // Set comments to one per page so that we are able to test paging without
1408
    // needing to insert large numbers of comments.
1409
    $this->setCommentsPerPage(1);
1410

    
1411
    // Create a node and three comments.
1412
    $node = $this->drupalCreateNode(array('type' => 'article', 'promote' => 1));
1413
    $comments = array();
1414
    $comments[] = $this->postComment($node, $this->randomName(), $this->randomName(), TRUE);
1415
    $comments[] = $this->postComment($node, $this->randomName(), $this->randomName(), TRUE);
1416
    $comments[] = $this->postComment($node, $this->randomName(), $this->randomName(), TRUE);
1417

    
1418
    // Post a reply to the second comment.
1419
    $this->drupalGet('comment/reply/' . $node->nid . '/' . $comments[1]->id);
1420
    $comments[] = $this->postComment(NULL, $this->randomName(), $this->randomName(), TRUE);
1421

    
1422
    // Post a reply to the first comment.
1423
    $this->drupalGet('comment/reply/' . $node->nid . '/' . $comments[0]->id);
1424
    $comments[] = $this->postComment(NULL, $this->randomName(), $this->randomName(), TRUE);
1425

    
1426
    // Post a reply to the last comment.
1427
    $this->drupalGet('comment/reply/' . $node->nid . '/' . $comments[2]->id);
1428
    $comments[] = $this->postComment(NULL, $this->randomName(), $this->randomName(), TRUE);
1429

    
1430
    // At this point, the comment tree is:
1431
    // - 0
1432
    //   - 4
1433
    // - 1
1434
    //   - 3
1435
    // - 2
1436
    //   - 5
1437

    
1438
    $this->setCommentSettings('comment_default_mode', COMMENT_MODE_FLAT, 'Comment paging changed.');
1439

    
1440
    $expected_pages = array(
1441
      1 => 5, // Page of comment 5
1442
      2 => 4, // Page of comment 4
1443
      3 => 3, // Page of comment 3
1444
      4 => 2, // Page of comment 2
1445
      5 => 1, // Page of comment 1
1446
      6 => 0, // Page of comment 0
1447
    );
1448

    
1449
    $node = node_load($node->nid);
1450
    foreach ($expected_pages as $new_replies => $expected_page) {
1451
      $returned = comment_new_page_count($node->comment_count, $new_replies, $node);
1452
      $returned_page = is_array($returned) ? $returned['page'] : 0;
1453
      $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)));
1454
    }
1455

    
1456
    $this->setCommentSettings('comment_default_mode', COMMENT_MODE_THREADED, 'Switched to threaded mode.');
1457

    
1458
    $expected_pages = array(
1459
      1 => 5, // Page of comment 5
1460
      2 => 1, // Page of comment 4
1461
      3 => 1, // Page of comment 4
1462
      4 => 1, // Page of comment 4
1463
      5 => 1, // Page of comment 4
1464
      6 => 0, // Page of comment 0
1465
    );
1466

    
1467
    $node = node_load($node->nid);
1468
    foreach ($expected_pages as $new_replies => $expected_page) {
1469
      $returned = comment_new_page_count($node->comment_count, $new_replies, $node);
1470
      $returned_page = is_array($returned) ? $returned['page'] : 0;
1471
      $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)));
1472
    }
1473
  }
1474
}
1475

    
1476
/**
1477
 * Tests comments with node access.
1478
 *
1479
 * See http://drupal.org/node/886752 -- verify there is no PostgreSQL error when
1480
 * viewing a node with threaded comments (a comment and a reply), if a node
1481
 * access module is in use.
1482
 */
1483
class CommentNodeAccessTest extends CommentHelperCase {
1484
  public static function getInfo() {
1485
    return array(
1486
      'name' => 'Comment node access',
1487
      'description' => 'Test comment viewing with node access.',
1488
      'group' => 'Comment',
1489
    );
1490
  }
1491

    
1492
  function setUp() {
1493
    DrupalWebTestCase::setUp('comment', 'search', 'node_access_test');
1494
    node_access_rebuild();
1495

    
1496
    // Create users and test node.
1497
    $this->admin_user = $this->drupalCreateUser(array('administer content types', 'administer comments', 'administer blocks'));
1498
    $this->web_user = $this->drupalCreateUser(array('access comments', 'post comments', 'create article content', 'edit own comments', 'node test view'));
1499
    $this->node = $this->drupalCreateNode(array('type' => 'article', 'promote' => 1, 'uid' => $this->web_user->uid));
1500
  }
1501

    
1502
  /**
1503
   * Test that threaded comments can be viewed.
1504
   */
1505
  function testThreadedCommentView() {
1506
    $langcode = LANGUAGE_NONE;
1507
    // Set comments to have subject required and preview disabled.
1508
    $this->drupalLogin($this->admin_user);
1509
    $this->setCommentPreview(DRUPAL_DISABLED);
1510
    $this->setCommentForm(TRUE);
1511
    $this->setCommentSubject(TRUE);
1512
    $this->setCommentSettings('comment_default_mode', COMMENT_MODE_THREADED, 'Comment paging changed.');
1513
    $this->drupalLogout();
1514

    
1515
    // Post comment.
1516
    $this->drupalLogin($this->web_user);
1517
    $comment_text = $this->randomName();
1518
    $comment_subject = $this->randomName();
1519
    $comment = $this->postComment($this->node, $comment_text, $comment_subject);
1520
    $comment_loaded = comment_load($comment->id);
1521
    $this->assertTrue($this->commentExists($comment), 'Comment found.');
1522

    
1523
    // Check comment display.
1524
    $this->drupalGet('node/' . $this->node->nid . '/' . $comment->id);
1525
    $this->assertText($comment_subject, 'Individual comment subject found.');
1526
    $this->assertText($comment_text, 'Individual comment body found.');
1527

    
1528
    // Reply to comment, creating second comment.
1529
    $this->drupalGet('comment/reply/' . $this->node->nid . '/' . $comment->id);
1530
    $reply_text = $this->randomName();
1531
    $reply_subject = $this->randomName();
1532
    $reply = $this->postComment(NULL, $reply_text, $reply_subject, TRUE);
1533
    $reply_loaded = comment_load($reply->id);
1534
    $this->assertTrue($this->commentExists($reply, TRUE), 'Reply found.');
1535

    
1536
    // Go to the node page and verify comment and reply are visible.
1537
    $this->drupalGet('node/' . $this->node->nid);
1538
    $this->assertText($comment_text);
1539
    $this->assertText($comment_subject);
1540
    $this->assertText($reply_text);
1541
    $this->assertText($reply_subject);
1542
  }
1543
}
1544

    
1545
class CommentApprovalTest extends CommentHelperCase {
1546
  public static function getInfo() {
1547
    return array(
1548
      'name' => 'Comment approval',
1549
      'description' => 'Test comment approval functionality.',
1550
      'group' => 'Comment',
1551
    );
1552
  }
1553

    
1554
  /**
1555
   * Test comment approval functionality through admin/content/comment.
1556
   */
1557
  function testApprovalAdminInterface() {
1558
    // Set anonymous comments to require approval.
1559
    user_role_change_permissions(DRUPAL_ANONYMOUS_RID, array(
1560
      'access comments' => TRUE,
1561
      'post comments' => TRUE,
1562
      'skip comment approval' => FALSE,
1563
    ));
1564
    $this->drupalLogin($this->admin_user);
1565
    $this->setCommentAnonymous('0'); // Ensure that doesn't require contact info.
1566

    
1567
    // Test that the comments page loads correctly when there are no comments
1568
    $this->drupalGet('admin/content/comment');
1569
    $this->assertText(t('No comments available.'));
1570

    
1571
    $this->drupalLogout();
1572

    
1573
    // Post anonymous comment without contact info.
1574
    $subject = $this->randomName();
1575
    $body = $this->randomName();
1576
    $this->postComment($this->node, $body, $subject, TRUE); // Set $contact to true so that it won't check for id and message.
1577
    $this->assertText(t('Your comment has been queued for review by site administrators and will be published after approval.'), 'Comment requires approval.');
1578

    
1579
    // Get unapproved comment id.
1580
    $this->drupalLogin($this->admin_user);
1581
    $anonymous_comment4 = $this->getUnapprovedComment($subject);
1582
    $anonymous_comment4 = (object) array('id' => $anonymous_comment4, 'subject' => $subject, 'comment' => $body);
1583
    $this->drupalLogout();
1584

    
1585
    $this->assertFalse($this->commentExists($anonymous_comment4), 'Anonymous comment was not published.');
1586

    
1587
    // Approve comment.
1588
    $this->drupalLogin($this->admin_user);
1589
    $this->performCommentOperation($anonymous_comment4, 'publish', TRUE);
1590
    $this->drupalLogout();
1591

    
1592
    $this->drupalGet('node/' . $this->node->nid);
1593
    $this->assertTrue($this->commentExists($anonymous_comment4), 'Anonymous comment visible.');
1594

    
1595
    // Post 2 anonymous comments without contact info.
1596
    $comments[] = $this->postComment($this->node, $this->randomName(), $this->randomName(), TRUE);
1597
    $comments[] = $this->postComment($this->node, $this->randomName(), $this->randomName(), TRUE);
1598

    
1599
    // Publish multiple comments in one operation.
1600
    $this->drupalLogin($this->admin_user);
1601
    $this->drupalGet('admin/content/comment/approval');
1602
    $this->assertText(t('Unapproved comments (@count)', array('@count' => 2)), 'Two unapproved comments waiting for approval.');
1603
    $edit = array(
1604
      "comments[{$comments[0]->id}]" => 1,
1605
      "comments[{$comments[1]->id}]" => 1,
1606
    );
1607
    $this->drupalPost(NULL, $edit, t('Update'));
1608
    $this->assertText(t('Unapproved comments (@count)', array('@count' => 0)), 'All comments were approved.');
1609

    
1610
    // Delete multiple comments in one operation.
1611
    $edit = array(
1612
      'operation' => 'delete',
1613
      "comments[{$comments[0]->id}]" => 1,
1614
      "comments[{$comments[1]->id}]" => 1,
1615
      "comments[{$anonymous_comment4->id}]" => 1,
1616
    );
1617
    $this->drupalPost(NULL, $edit, t('Update'));
1618
    $this->assertText(t('Are you sure you want to delete these comments and all their children?'), 'Confirmation required.');
1619
    $this->drupalPost(NULL, $edit, t('Delete comments'));
1620
    $this->assertText(t('No comments available.'), 'All comments were deleted.');
1621
  }
1622

    
1623
  /**
1624
   * Test comment approval functionality through node interface.
1625
   */
1626
  function testApprovalNodeInterface() {
1627
    // Set anonymous comments to require approval.
1628
    user_role_change_permissions(DRUPAL_ANONYMOUS_RID, array(
1629
      'access comments' => TRUE,
1630
      'post comments' => TRUE,
1631
      'skip comment approval' => FALSE,
1632
    ));
1633
    $this->drupalLogin($this->admin_user);
1634
    $this->setCommentAnonymous('0'); // Ensure that doesn't require contact info.
1635
    $this->drupalLogout();
1636

    
1637
    // Post anonymous comment without contact info.
1638
    $subject = $this->randomName();
1639
    $body = $this->randomName();
1640
    $this->postComment($this->node, $body, $subject, TRUE); // Set $contact to true so that it won't check for id and message.
1641
    $this->assertText(t('Your comment has been queued for review by site administrators and will be published after approval.'), 'Comment requires approval.');
1642

    
1643
    // Get unapproved comment id.
1644
    $this->drupalLogin($this->admin_user);
1645
    $anonymous_comment4 = $this->getUnapprovedComment($subject);
1646
    $anonymous_comment4 = (object) array('id' => $anonymous_comment4, 'subject' => $subject, 'comment' => $body);
1647
    $this->drupalLogout();
1648

    
1649
    $this->assertFalse($this->commentExists($anonymous_comment4), 'Anonymous comment was not published.');
1650

    
1651
    // Approve comment.
1652
    $this->drupalLogin($this->admin_user);
1653
    $this->drupalGet('comment/1/approve');
1654
    $this->assertResponse(403, 'Forged comment approval was denied.');
1655
    $this->drupalGet('comment/1/approve', array('query' => array('token' => 'forged')));
1656
    $this->assertResponse(403, 'Forged comment approval was denied.');
1657
    $this->drupalGet('node/' . $this->node->nid);
1658
    $this->clickLink(t('approve'));
1659
    $this->drupalLogout();
1660

    
1661
    $this->drupalGet('node/' . $this->node->nid);
1662
    $this->assertTrue($this->commentExists($anonymous_comment4), 'Anonymous comment visible.');
1663
  }
1664
}
1665

    
1666
/**
1667
 * Functional tests for the comment module blocks.
1668
 */
1669
class CommentBlockFunctionalTest extends CommentHelperCase {
1670
  public static function getInfo() {
1671
    return array(
1672
      'name' => 'Comment blocks',
1673
      'description' => 'Test comment block functionality.',
1674
      'group' => 'Comment',
1675
    );
1676
  }
1677

    
1678
  /**
1679
   * Test the recent comments block.
1680
   */
1681
  function testRecentCommentBlock() {
1682
    $this->drupalLogin($this->admin_user);
1683

    
1684
    // Set the block to a region to confirm block is available.
1685
    $edit = array(
1686
      'blocks[comment_recent][region]' => 'sidebar_first',
1687
    );
1688
    $this->drupalPost('admin/structure/block', $edit, t('Save blocks'));
1689
    $this->assertText(t('The block settings have been updated.'), 'Block saved to first sidebar region.');
1690

    
1691
    // Set block title and variables.
1692
    $block = array(
1693
      'title' => $this->randomName(),
1694
      'comment_block_count' => 2,
1695
    );
1696
    $this->drupalPost('admin/structure/block/manage/comment/recent/configure', $block, t('Save block'));
1697
    $this->assertText(t('The block configuration has been saved.'), 'Block saved.');
1698

    
1699
    // Add some test comments, one without a subject.
1700
    $comment1 = $this->postComment($this->node, $this->randomName(), $this->randomName());
1701
    $comment2 = $this->postComment($this->node, $this->randomName(), $this->randomName());
1702
    $comment3 = $this->postComment($this->node, $this->randomName());
1703

    
1704
    // Test that a user without the 'access comments' permission cannot see the
1705
    // block.
1706
    $this->drupalLogout();
1707
    user_role_revoke_permissions(DRUPAL_ANONYMOUS_RID, array('access comments'));
1708
    $this->drupalGet('');
1709
    $this->assertNoText($block['title'], 'Block was not found.');
1710
    user_role_grant_permissions(DRUPAL_ANONYMOUS_RID, array('access comments'));
1711

    
1712
    // Test that a user with the 'access comments' permission can see the
1713
    // block.
1714
    $this->drupalLogin($this->web_user);
1715
    $this->drupalGet('');
1716
    $this->assertText($block['title'], 'Block was found.');
1717

    
1718
    // Test the only the 2 latest comments are shown and in the proper order.
1719
    $this->assertNoText($comment1->subject, 'Comment not found in block.');
1720
    $this->assertText($comment2->subject, 'Comment found in block.');
1721
    $this->assertText($comment3->comment, 'Comment found in block.');
1722
    $this->assertTrue(strpos($this->drupalGetContent(), $comment3->comment) < strpos($this->drupalGetContent(), $comment2->subject), 'Comments were ordered correctly in block.');
1723

    
1724
    // Set the number of recent comments to show to 10.
1725
    $this->drupalLogout();
1726
    $this->drupalLogin($this->admin_user);
1727
    $block = array(
1728
      'comment_block_count' => 10,
1729
    );
1730
    $this->drupalPost('admin/structure/block/manage/comment/recent/configure', $block, t('Save block'));
1731
    $this->assertText(t('The block configuration has been saved.'), 'Block saved.');
1732

    
1733
    // Post an additional comment.
1734
    $comment4 = $this->postComment($this->node, $this->randomName(), $this->randomName());
1735

    
1736
    // Test that all four comments are shown.
1737
    $this->assertText($comment1->subject, 'Comment found in block.');
1738
    $this->assertText($comment2->subject, 'Comment found in block.');
1739
    $this->assertText($comment3->comment, 'Comment found in block.');
1740
    $this->assertText($comment4->subject, 'Comment found in block.');
1741

    
1742
    // Test that links to comments work when comments are across pages.
1743
    $this->setCommentsPerPage(1);
1744
    $this->drupalGet('');
1745
    $this->clickLink($comment1->subject);
1746
    $this->assertText($comment1->subject, 'Comment link goes to correct page.');
1747
    $this->drupalGet('');
1748
    $this->clickLink($comment2->subject);
1749
    $this->assertText($comment2->subject, 'Comment link goes to correct page.');
1750
    $this->clickLink($comment4->subject);
1751
    $this->assertText($comment4->subject, 'Comment link goes to correct page.');
1752
    // Check that when viewing a comment page from a link to the comment, that
1753
    // rel="canonical" is added to the head of the document.
1754
    $this->assertRaw('<link rel="canonical"', 'Canonical URL was found in the HTML head');
1755
  }
1756
}
1757

    
1758
/**
1759
 * Unit tests for comment module integration with RSS feeds.
1760
 */
1761
class CommentRSSUnitTest extends CommentHelperCase {
1762
  public static function getInfo() {
1763
    return array(
1764
      'name' => 'Comment RSS',
1765
      'description' => 'Test comments as part of an RSS feed.',
1766
      'group' => 'Comment',
1767
    );
1768
  }
1769

    
1770
  /**
1771
   * Test comments as part of an RSS feed.
1772
   */
1773
  function testCommentRSS() {
1774
    // Find comment in RSS feed.
1775
    $this->drupalLogin($this->web_user);
1776
    $comment = $this->postComment($this->node, $this->randomName(), $this->randomName());
1777
    $this->drupalGet('rss.xml');
1778
    $raw = '<comments>' . url('node/' . $this->node->nid, array('fragment' => 'comments', 'absolute' => TRUE)) . '</comments>';
1779
    $this->assertRaw($raw, 'Comments as part of RSS feed.');
1780

    
1781
    // Hide comments from RSS feed and check presence.
1782
    $this->node->comment = COMMENT_NODE_HIDDEN;
1783
    node_save($this->node);
1784
    $this->drupalGet('rss.xml');
1785
    $this->assertNoRaw($raw, 'Hidden comments is not a part of RSS feed.');
1786
  }
1787
}
1788

    
1789

    
1790
/**
1791
 * Test to make sure comment content is rebuilt.
1792
 */
1793
class CommentContentRebuild extends CommentHelperCase {
1794
  public static function getInfo() {
1795
    return array(
1796
      'name' => 'Comment Rebuild',
1797
      'description' => 'Test to make sure the comment content is rebuilt.',
1798
      'group' => 'Comment',
1799
    );
1800
  }
1801

    
1802
  /**
1803
   * Test to ensure that the comment's content array is rebuilt for every
1804
   * call to comment_view().
1805
   */
1806
  function testCommentRebuild() {
1807
    // Update the comment settings so preview isn't required.
1808
    $this->drupalLogin($this->admin_user);
1809
    $this->setCommentSubject(TRUE);
1810
    $this->setCommentPreview(DRUPAL_OPTIONAL);
1811
    $this->drupalLogout();
1812

    
1813
    // Log in as the web user and add the comment.
1814
    $this->drupalLogin($this->web_user);
1815
    $subject_text = $this->randomName();
1816
    $comment_text = $this->randomName();
1817
    $comment = $this->postComment($this->node, $comment_text, $subject_text, TRUE);
1818
    $comment_loaded = comment_load($comment->id);
1819
    $this->assertTrue($this->commentExists($comment), 'Comment found.');
1820

    
1821
    // Add the property to the content array and then see if it still exists on build.
1822
    $comment_loaded->content['test_property'] = array('#value' => $this->randomString());
1823
    $built_content = comment_view($comment_loaded, $this->node);
1824

    
1825
    // This means that the content was rebuilt as the added test property no longer exists.
1826
    $this->assertFalse(isset($built_content['test_property']), 'Comment content was emptied before being built.');
1827
  }
1828
}
1829

    
1830
/**
1831
 * Test comment token replacement in strings.
1832
 */
1833
class CommentTokenReplaceTestCase extends CommentHelperCase {
1834
  public static function getInfo() {
1835
    return array(
1836
      'name' => 'Comment token replacement',
1837
      'description' => 'Generates text using placeholders for dummy content to check comment token replacement.',
1838
      'group' => 'Comment',
1839
    );
1840
  }
1841

    
1842
  /**
1843
   * Creates a comment, then tests the tokens generated from it.
1844
   */
1845
  function testCommentTokenReplacement() {
1846
    global $language;
1847
    $url_options = array(
1848
      'absolute' => TRUE,
1849
      'language' => $language,
1850
    );
1851

    
1852
    $this->drupalLogin($this->admin_user);
1853

    
1854
    // Set comment variables.
1855
    $this->setCommentSubject(TRUE);
1856

    
1857
    // Create a node and a comment.
1858
    $node = $this->drupalCreateNode(array('type' => 'article'));
1859
    $parent_comment = $this->postComment($node, $this->randomName(), $this->randomName(), TRUE);
1860

    
1861
    // Post a reply to the comment.
1862
    $this->drupalGet('comment/reply/' . $node->nid . '/' . $parent_comment->id);
1863
    $child_comment = $this->postComment(NULL, $this->randomName(), $this->randomName());
1864
    $comment = comment_load($child_comment->id);
1865
    $comment->homepage = 'http://example.org/';
1866

    
1867
    // Add HTML to ensure that sanitation of some fields tested directly.
1868
    $comment->subject = '<blink>Blinking Comment</blink>';
1869
    $instance = field_info_instance('comment', 'body', 'comment_body');
1870

    
1871
    // Generate and test sanitized tokens.
1872
    $tests = array();
1873
    $tests['[comment:cid]'] = $comment->cid;
1874
    $tests['[comment:hostname]'] = check_plain($comment->hostname);
1875
    $tests['[comment:name]'] = filter_xss($comment->name);
1876
    $tests['[comment:mail]'] = check_plain($this->admin_user->mail);
1877
    $tests['[comment:homepage]'] = check_url($comment->homepage);
1878
    $tests['[comment:title]'] = filter_xss($comment->subject);
1879
    $tests['[comment:body]'] = _text_sanitize($instance, LANGUAGE_NONE, $comment->comment_body[LANGUAGE_NONE][0], 'value');
1880
    $tests['[comment:url]'] = url('comment/' . $comment->cid, $url_options + array('fragment' => 'comment-' . $comment->cid));
1881
    $tests['[comment:edit-url]'] = url('comment/' . $comment->cid . '/edit', $url_options);
1882
    $tests['[comment:created:since]'] = format_interval(REQUEST_TIME - $comment->created, 2, $language->language);
1883
    $tests['[comment:changed:since]'] = format_interval(REQUEST_TIME - $comment->changed, 2, $language->language);
1884
    $tests['[comment:parent:cid]'] = $comment->pid;
1885
    $tests['[comment:parent:title]'] = check_plain($parent_comment->subject);
1886
    $tests['[comment:node:nid]'] = $comment->nid;
1887
    $tests['[comment:node:title]'] = check_plain($node->title);
1888
    $tests['[comment:author:uid]'] = $comment->uid;
1889
    $tests['[comment:author:name]'] = check_plain($this->admin_user->name);
1890

    
1891
    // Test to make sure that we generated something for each token.
1892
    $this->assertFalse(in_array(0, array_map('strlen', $tests)), 'No empty tokens generated.');
1893

    
1894
    foreach ($tests as $input => $expected) {
1895
      $output = token_replace($input, array('comment' => $comment), array('language' => $language));
1896
      $this->assertEqual($output, $expected, format_string('Sanitized comment token %token replaced.', array('%token' => $input)));
1897
    }
1898

    
1899
    // Generate and test unsanitized tokens.
1900
    $tests['[comment:hostname]'] = $comment->hostname;
1901
    $tests['[comment:name]'] = $comment->name;
1902
    $tests['[comment:mail]'] = $this->admin_user->mail;
1903
    $tests['[comment:homepage]'] = $comment->homepage;
1904
    $tests['[comment:title]'] = $comment->subject;
1905
    $tests['[comment:body]'] = $comment->comment_body[LANGUAGE_NONE][0]['value'];
1906
    $tests['[comment:parent:title]'] = $parent_comment->subject;
1907
    $tests['[comment:node:title]'] = $node->title;
1908
    $tests['[comment:author:name]'] = $this->admin_user->name;
1909

    
1910
    foreach ($tests as $input => $expected) {
1911
      $output = token_replace($input, array('comment' => $comment), array('language' => $language, 'sanitize' => FALSE));
1912
      $this->assertEqual($output, $expected, format_string('Unsanitized comment token %token replaced.', array('%token' => $input)));
1913
    }
1914

    
1915
    // Load node so comment_count gets computed.
1916
    $node = node_load($node->nid);
1917

    
1918
    // Generate comment tokens for the node (it has 2 comments, both new).
1919
    $tests = array();
1920
    $tests['[node:comment-count]'] = 2;
1921
    $tests['[node:comment-count-new]'] = 2;
1922

    
1923
    foreach ($tests as $input => $expected) {
1924
      $output = token_replace($input, array('node' => $node), array('language' => $language));
1925
      $this->assertEqual($output, $expected, format_string('Node comment token %token replaced.', array('%token' => $input)));
1926
    }
1927
  }
1928
}
1929

    
1930
/**
1931
 * Test actions provided by the comment module.
1932
 */
1933
class CommentActionsTestCase extends CommentHelperCase {
1934
  public static function getInfo() {
1935
    return array(
1936
      'name' => 'Comment actions',
1937
      'description' => 'Test actions provided by the comment module.',
1938
      'group' => 'Comment',
1939
    );
1940
  }
1941

    
1942
  /**
1943
   * Test comment publish and unpublish actions.
1944
   */
1945
  function testCommentPublishUnpublishActions() {
1946
    $this->drupalLogin($this->web_user);
1947
    $comment_text = $this->randomName();
1948
    $subject = $this->randomName();
1949
    $comment = $this->postComment($this->node, $comment_text, $subject);
1950
    $comment = comment_load($comment->id);
1951

    
1952
    // Unpublish a comment (direct form: doesn't actually save the comment).
1953
    comment_unpublish_action($comment);
1954
    $this->assertEqual($comment->status, COMMENT_NOT_PUBLISHED, 'Comment was unpublished');
1955
    $this->assertWatchdogMessage('Unpublished comment %subject.', array('%subject' => $subject), 'Found watchdog message');
1956
    $this->clearWatchdog();
1957

    
1958
    // Unpublish a comment (indirect form: modify the comment in the database).
1959
    comment_unpublish_action(NULL, array('cid' => $comment->cid));
1960
    $this->assertEqual(comment_load($comment->cid)->status, COMMENT_NOT_PUBLISHED, 'Comment was unpublished');
1961
    $this->assertWatchdogMessage('Unpublished comment %subject.', array('%subject' => $subject), 'Found watchdog message');
1962

    
1963
    // Publish a comment (direct form: doesn't actually save the comment).
1964
    comment_publish_action($comment);
1965
    $this->assertEqual($comment->status, COMMENT_PUBLISHED, 'Comment was published');
1966
    $this->assertWatchdogMessage('Published comment %subject.', array('%subject' => $subject), 'Found watchdog message');
1967
    $this->clearWatchdog();
1968

    
1969
    // Publish a comment (indirect form: modify the comment in the database).
1970
    comment_publish_action(NULL, array('cid' => $comment->cid));
1971
    $this->assertEqual(comment_load($comment->cid)->status, COMMENT_PUBLISHED, 'Comment was published');
1972
    $this->assertWatchdogMessage('Published comment %subject.', array('%subject' => $subject), 'Found watchdog message');
1973
    $this->clearWatchdog();
1974
  }
1975

    
1976
  /**
1977
   * Verify that a watchdog message has been entered.
1978
   *
1979
   * @param $watchdog_message
1980
   *   The watchdog message.
1981
   * @param $variables
1982
   *   The array of variables passed to watchdog().
1983
   * @param $message
1984
   *   The assertion message.
1985
   */
1986
  function assertWatchdogMessage($watchdog_message, $variables, $message) {
1987
    $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();
1988
    return $this->assert($status, format_string('@message', array('@message' => $message)));
1989
  }
1990

    
1991
  /**
1992
   * Helper function: clear the watchdog.
1993
   */
1994
  function clearWatchdog() {
1995
    db_truncate('watchdog')->execute();
1996
  }
1997
}
1998

    
1999
/**
2000
 * Test fields on comments.
2001
 */
2002
class CommentFieldsTest extends CommentHelperCase {
2003
  public static function getInfo() {
2004
    return array(
2005
      'name' => 'Comment fields',
2006
      'description' => 'Tests fields on comments.',
2007
      'group' => 'Comment',
2008
    );
2009
  }
2010

    
2011
  /**
2012
   * Tests that the default 'comment_body' field is correctly added.
2013
   */
2014
  function testCommentDefaultFields() {
2015
    // Do not make assumptions on default node types created by the test
2016
    // installation profile, and create our own.
2017
    $this->drupalCreateContentType(array('type' => 'test_node_type'));
2018

    
2019
    // Check that the 'comment_body' field is present on all comment bundles.
2020
    $instances = field_info_instances('comment');
2021
    foreach (node_type_get_types() as $type_name => $info) {
2022
      $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)));
2023

    
2024
      // Delete the instance along the way.
2025
      field_delete_instance($instances['comment_node_' . $type_name]['comment_body']);
2026
    }
2027

    
2028
    // Check that the 'comment_body' field is deleted.
2029
    $field = field_info_field('comment_body');
2030
    $this->assertTrue(empty($field), 'The comment_body field was deleted');
2031

    
2032
    // Create a new content type.
2033
    $type_name = 'test_node_type_2';
2034
    $this->drupalCreateContentType(array('type' => $type_name));
2035

    
2036
    // Check that the 'comment_body' field exists and has an instance on the
2037
    // new comment bundle.
2038
    $field = field_info_field('comment_body');
2039
    $this->assertTrue($field, 'The comment_body field exists');
2040
    $instances = field_info_instances('comment');
2041
    $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)));
2042
  }
2043

    
2044
  /**
2045
   * Test that comment module works when enabled after a content module.
2046
   */
2047
  function testCommentEnable() {
2048
    // Create a user to do module administration.
2049
    $this->admin_user = $this->drupalCreateUser(array('access administration pages', 'administer modules'));
2050
    $this->drupalLogin($this->admin_user);
2051

    
2052
    // Disable the comment module.
2053
    $edit = array();
2054
    $edit['modules[Core][comment][enable]'] = FALSE;
2055
    $this->drupalPost('admin/modules', $edit, t('Save configuration'));
2056
    $this->resetAll();
2057
    $this->assertFalse(module_exists('comment'), 'Comment module disabled.');
2058

    
2059
    // Enable core content type modules (blog, book, and poll).
2060
    $edit = array();
2061
    $edit['modules[Core][blog][enable]'] = 'blog';
2062
    $edit['modules[Core][book][enable]'] = 'book';
2063
    $edit['modules[Core][poll][enable]'] = 'poll';
2064
    $this->drupalPost('admin/modules', $edit, t('Save configuration'));
2065
    $this->resetAll();
2066

    
2067
    // Now enable the comment module.
2068
    $edit = array();
2069
    $edit['modules[Core][comment][enable]'] = 'comment';
2070
    $this->drupalPost('admin/modules', $edit, t('Save configuration'));
2071
    $this->resetAll();
2072
    $this->assertTrue(module_exists('comment'), 'Comment module enabled.');
2073

    
2074
    // Create nodes of each type.
2075
    $blog_node = $this->drupalCreateNode(array('type' => 'blog'));
2076
    $book_node = $this->drupalCreateNode(array('type' => 'book'));
2077
    $poll_node = $this->drupalCreateNode(array('type' => 'poll', 'active' => 1, 'runtime' => 0, 'choice' => array(array('chtext' => ''))));
2078

    
2079
    $this->drupalLogout();
2080

    
2081
    // Try to post a comment on each node. A failure will be triggered if the
2082
    // comment body is missing on one of these forms, due to postComment()
2083
    // asserting that the body is actually posted correctly.
2084
    $this->web_user = $this->drupalCreateUser(array('access content', 'access comments', 'post comments', 'skip comment approval'));
2085
    $this->drupalLogin($this->web_user);
2086
    $this->postComment($blog_node, $this->randomName(), $this->randomName());
2087
    $this->postComment($book_node, $this->randomName(), $this->randomName());
2088
    $this->postComment($poll_node, $this->randomName(), $this->randomName());
2089
  }
2090

    
2091
  /**
2092
   * Test that comment module works correctly with plain text format.
2093
   */
2094
  function testCommentFormat() {
2095
    // Disable text processing for comments.
2096
    $this->drupalLogin($this->admin_user);
2097
    $edit = array('instance[settings][text_processing]' => 0);
2098
    $this->drupalPost('admin/structure/types/manage/article/comment/fields/comment_body', $edit, t('Save settings'));
2099

    
2100
    // Post a comment without an explicit subject.
2101
    $this->drupalLogin($this->web_user);
2102
    $edit = array('comment_body[und][0][value]' => $this->randomName(8));
2103
    $this->drupalPost('node/' . $this->node->nid, $edit, t('Save'));
2104
  }
2105
}
2106

    
2107
/**
2108
 * Tests comment threading.
2109
 */
2110
class CommentThreadingTestCase extends CommentHelperCase {
2111
  public static function getInfo() {
2112
    return array(
2113
      'name' => 'Comment Threading',
2114
      'description' => 'Test to make sure the comment number increments properly.',
2115
      'group' => 'Comment',
2116
    );
2117
  }
2118

    
2119
  /**
2120
   * Tests the comment threading.
2121
   */
2122
  function testCommentThreading() {
2123
    $langcode = LANGUAGE_NONE;
2124
    // Set comments to have a subject with preview disabled.
2125
    $this->drupalLogin($this->admin_user);
2126
    $this->setCommentPreview(DRUPAL_DISABLED);
2127
    $this->setCommentForm(TRUE);
2128
    $this->setCommentSubject(TRUE);
2129
    $this->setCommentSettings('comment_default_mode', COMMENT_MODE_THREADED, 'Comment paging changed.');
2130
    $this->drupalLogout();
2131

    
2132
    // Create a node.
2133
    $this->drupalLogin($this->web_user);
2134
    $this->node = $this->drupalCreateNode(array('type' => 'article', 'promote' => 1, 'uid' => $this->web_user->uid));
2135

    
2136
    // Post comment #1.
2137
    $this->drupalLogin($this->web_user);
2138
    $subject_text = $this->randomName();
2139
    $comment_text = $this->randomName();
2140
    $comment = $this->postComment($this->node, $comment_text, $subject_text, TRUE);
2141
    $comment_loaded = comment_load($comment->id);
2142
    $this->assertTrue($this->commentExists($comment), 'Comment #1. Comment found.');
2143
    $this->assertEqual($comment_loaded->thread, '01/');
2144

    
2145
    // Reply to comment #1 creating comment #2.
2146
    $this->drupalLogin($this->web_user);
2147
    $this->drupalGet('comment/reply/' . $this->node->nid . '/' . $comment->id);
2148
    $reply = $this->postComment(NULL, $this->randomName(), '', TRUE);
2149
    $reply_loaded = comment_load($reply->id);
2150
    $this->assertTrue($this->commentExists($reply, TRUE), 'Comment #2. Reply found.');
2151
    $this->assertEqual($reply_loaded->thread, '01.00/');
2152

    
2153
    // Reply to comment #2 creating comment #3.
2154
    $this->drupalGet('comment/reply/' . $this->node->nid . '/' . $reply->id);
2155
    $reply = $this->postComment(NULL, $this->randomName(), $this->randomName(), TRUE);
2156
    $reply_loaded = comment_load($reply->id);
2157
    $this->assertTrue($this->commentExists($reply, TRUE), 'Comment #3. Second reply found.');
2158
    $this->assertEqual($reply_loaded->thread, '01.00.00/');
2159

    
2160
    // Reply to comment #1 creating comment #4.
2161
    $this->drupalLogin($this->web_user);
2162
    $this->drupalGet('comment/reply/' . $this->node->nid . '/' . $comment->id);
2163
    $reply = $this->postComment(NULL, $this->randomName(), '', TRUE);
2164
    $reply_loaded = comment_load($reply->id);
2165
    $this->assertTrue($this->commentExists($comment), 'Comment #4. Third reply found.');
2166
    $this->assertEqual($reply_loaded->thread, '01.01/');
2167

    
2168
    // Post comment #2 overall comment #5.
2169
    $this->drupalLogin($this->web_user);
2170
    $subject_text = $this->randomName();
2171
    $comment_text = $this->randomName();
2172
    $comment = $this->postComment($this->node, $comment_text, $subject_text, TRUE);
2173
    $comment_loaded = comment_load($comment->id);
2174
    $this->assertTrue($this->commentExists($comment), 'Comment #5. Second comment found.');
2175
    $this->assertEqual($comment_loaded->thread, '02/');
2176

    
2177
    // Reply to comment #5 creating comment #6.
2178
    $this->drupalLogin($this->web_user);
2179
    $this->drupalGet('comment/reply/' . $this->node->nid . '/' . $comment->id);
2180
    $reply = $this->postComment(NULL, $this->randomName(), '', TRUE);
2181
    $reply_loaded = comment_load($reply->id);
2182
    $this->assertTrue($this->commentExists($reply, TRUE), 'Comment #6. Reply found.');
2183
    $this->assertEqual($reply_loaded->thread, '02.00/');
2184

    
2185
    // Reply to comment #6 creating comment #7.
2186
    $this->drupalGet('comment/reply/' . $this->node->nid . '/' . $reply->id);
2187
    $reply = $this->postComment(NULL, $this->randomName(), $this->randomName(), TRUE);
2188
    $reply_loaded = comment_load($reply->id);
2189
    $this->assertTrue($this->commentExists($reply, TRUE), 'Comment #7. Second reply found.');
2190
    $this->assertEqual($reply_loaded->thread, '02.00.00/');
2191

    
2192
    // Reply to comment #5 creating comment #8.
2193
    $this->drupalLogin($this->web_user);
2194
    $this->drupalGet('comment/reply/' . $this->node->nid . '/' . $comment->id);
2195
    $reply = $this->postComment(NULL, $this->randomName(), '', TRUE);
2196
    $reply_loaded = comment_load($reply->id);
2197
    $this->assertTrue($this->commentExists($comment), 'Comment #8. Third reply found.');
2198
    $this->assertEqual($reply_loaded->thread, '02.01/');
2199
  }
2200
}
2201

    
2202
/**
2203
 * Tests that comments behave correctly when the node is changed.
2204
 */
2205
class CommentNodeChangesTestCase extends CommentHelperCase {
2206

    
2207
  public static function getInfo() {
2208
    return array(
2209
      'name' => 'Comment deletion on node changes',
2210
      'description' => 'Tests that comments behave correctly when the node is changed.',
2211
      'group' => 'Comment',
2212
    );
2213
  }
2214

    
2215
  /**
2216
   * Tests that comments are deleted with the node.
2217
   */
2218
  function testNodeDeletion() {
2219
    $this->drupalLogin($this->web_user);
2220
    $comment = $this->postComment($this->node, $this->randomName(), $this->randomName());
2221
    $this->assertTrue(comment_load($comment->id), 'The comment could be loaded.');
2222
    node_delete($this->node->nid);
2223
    $this->assertFalse(comment_load($comment->id), 'The comment could not be loaded after the node was deleted.');
2224
  }
2225
}