Index: modules/comment/comment.admin.inc =================================================================== RCS file: /cvs/drupal/drupal/modules/comment/comment.admin.inc,v retrieving revision 1.23 diff -u -p -r1.23 comment.admin.inc --- modules/comment/comment.admin.inc 6 Jun 2009 10:27:42 -0000 1.23 +++ modules/comment/comment.admin.inc 11 Jun 2009 16:19:00 -0000 @@ -71,6 +71,7 @@ function comment_admin_overview($type = $query->join('node', 'n', 'n.nid = c.nid'); $query->addField('u', 'name', 'registered_name'); $query->addField('n', 'title', 'node_title'); + $query->addField('n', 'type', 'node_type'); $result = $query ->fields('c', array('subject', 'nid', 'cid', 'comment', 'timestamp', 'status', 'name', 'homepage')) ->fields('u', array('uid')) @@ -85,8 +86,10 @@ function comment_admin_overview($type = $destination = drupal_get_destination(); foreach ($result as $comment) { + $node = (object) array('type' => $comment->node_type); + $page = comment_get_display_page($comment->cid, $node); $options[$comment->cid] = array( - 'subject' => l($comment->subject, 'node/' . $comment->nid, array('attributes' => array('title' => truncate_utf8($comment->comment, 128)), 'fragment' => 'comment-' . $comment->cid)), + 'subject' => l($comment->subject, 'node/' . $comment->nid, array('attributes' => array('title' => truncate_utf8($comment->comment, 128)), 'query' => array('page' => $page), 'fragment' => 'comment-' . $comment->cid)), 'author' => theme('username', $comment), 'posted_in' => l($comment->node_title, 'node/' . $comment->nid), 'time' => format_date($comment->timestamp, 'small'), Index: modules/comment/comment.module =================================================================== RCS file: /cvs/drupal/drupal/modules/comment/comment.module,v retrieving revision 1.722 diff -u -p -r1.722 comment.module --- modules/comment/comment.module 11 Jun 2009 15:17:15 -0000 1.722 +++ modules/comment/comment.module 11 Jun 2009 16:19:01 -0000 @@ -331,6 +331,7 @@ function comment_get_recent($number = 10 $query->innerJoin('node', 'n', 'n.nid = c.nid'); return $query ->fields('c', array('nid', 'subject', 'cid', 'timestamp')) + ->fields('n', array('type')) ->condition('c.nid', $nids, 'IN') ->condition('c.status', COMMENT_PUBLISHED) ->condition('n.status', 1) @@ -404,7 +405,9 @@ function theme_comment_block() { $items = array(); $number = variable_get('comment_block_count', 10); foreach (comment_get_recent($number) as $comment) { - $items[] = l($comment->subject, 'node/' . $comment->nid, array('fragment' => 'comment-' . $comment->cid)) . '
' . t('@time ago', array('@time' => format_interval(REQUEST_TIME - $comment->timestamp))); + $node = (object) array('type' => $comment->type); + $page = comment_get_display_page($comment->cid, $node); + $items[] = l($comment->subject, 'node/' . $comment->nid, array('query' => array('page' => $page), 'fragment' => 'comment-' . $comment->cid)) . '
' . t('@time ago', array('@time' => format_interval(REQUEST_TIME - $comment->timestamp))); } if ($items) { @@ -1348,6 +1351,52 @@ function comment_num_new($nid, $timestam } /** + * Get the display ordinal for a comment, starting from 0. + * + * @param $cid + * The comment ID. + * @return + * The display ordinal for the comment. + */ +function comment_get_display_ordinal($cid, $node) { + // Count how many comments (c) are before $cid (d) in display order. This is + // the 0-based display ordinal. + $query = db_select('comment', 'c'); + $query->innerJoin('comment', 'd', 'd.nid = c.nid'); + $query->addExpression('COUNT(*)', 'count'); + $query->condition('d.cid', $cid); + if (!user_access('administer comments')) { + $query->condition('c.status', COMMENT_PUBLISHED); + } + $mode = _comment_get_display_setting('mode', $node); + + if ($mode == COMMENT_MODE_FLAT_EXPANDED || $mode == COMMENT_MODE_FLAT_COLLAPSED) { + $query->condition('c.cid', 'd.cid', '<'); + } + else { + $query->addExpression('SUBSTRING(c.thread, 1, (LENGTH(c.thread) -1)) < SUBSTRING(d.thread, 1, (LENGTH(d.thread) -1))'); + } + + return $query->execute()->fetchField(); +} + +/** + * Return the page number for a comment. + * + * @param $cid + * The comment ID. + * @param $node + * The node the comment is attached to. + * @return + * The page number. + */ +function comment_get_display_page($cid, $node) { + $ordinal = comment_get_display_ordinal($cid, $node); + $comments_per_page = _comment_get_display_setting('comments_per_page', $node); + return floor($ordinal/$comments_per_page); +} + +/** * Validate comment data. * * @param $edit @@ -1803,9 +1852,8 @@ function comment_form_submit($form, &$fo if ($cid = comment_save($form_state['values'])) { $node = node_load($form_state['values']['nid']); // Add 1 to existing $node->comment count to include new comment. - $comment_count = $node->comment_count + 1; - $page = comment_new_page_count($comment_count, 1, $node); - $form_state['redirect'] = array('node/' . $node->nid, $page, "comment-$cid"); + $page = comment_get_display_page($cid, $node); + $form_state['redirect'] = array('node/' . $node->nid, "page=$page", "comment-$cid"); return; } } Index: modules/comment/comment.test =================================================================== RCS file: /cvs/drupal/drupal/modules/comment/comment.test,v retrieving revision 1.31 diff -u -p -r1.31 comment.test --- modules/comment/comment.test 3 Jun 2009 06:52:29 -0000 1.31 +++ modules/comment/comment.test 11 Jun 2009 16:19:02 -0000 @@ -123,7 +123,9 @@ class CommentHelperCase extends DrupalWe * Form value. */ function setCommentForm($enabled) { - $this->setCommentSettings('comment_form_location', ($enabled ? '1' : '3'), 'Comment controls ' . ($enabled ? 'enabled' : 'disabled') . '.'); + $this->setCommentSettings('comment_form_location', + ($enabled ? COMMENT_FORM_BELOW : COMMENT_FORM_SEPARATE_PAGE), + 'Comment controls ' . ($enabled ? 'enabled' : 'disabled') . '.'); } /** @@ -143,7 +145,7 @@ class CommentHelperCase extends DrupalWe * Comments per page value. */ function setCommentsPerPage($number) { - $this->setCommentSettings('comment_default_per_page_article', $number, 'Number of comments per page set to ' . $number . '.'); + $this->setCommentSettings('comment_default_per_page', $number, 'Number of comments per page set to ' . $number . '.'); } /** @@ -244,7 +246,9 @@ class CommentInterfaceTest extends Comme // Set comments to not have subject. $this->drupalLogin($this->admin_user); $this->setCommentPreview(TRUE); + $this->setCommentForm(TRUE); $this->setCommentSubject(FALSE); + $this->setCommentSettings('comment_default_mode', COMMENT_MODE_THREADED_EXPANDED, t('Comment paging changed.')); $this->drupalLogout(); // Post comment without subject. @@ -300,11 +304,11 @@ class CommentInterfaceTest extends Comme $this->drupalGet('node'); $this->assertRaw('3 comments', t('Link to the 3 comments exist.')); - // Pager + // Confirm a new comment is posted to the correct page. $this->setCommentsPerPage(2); $comment_new_page = $this->postComment($this->node, $this->randomName(), $this->randomName()); - $this->drupalGet('node/' . $this->node->nid); - $this->assertTrue($this->commentExists($comment) && $this->commentExists($comment_new_page), t('Page one exists. %s')); + //$this->drupalGet('node/' . $this->node->nid); + $this->assertTrue($this->commentExists($comment_new_page), t('Page one exists. %s')); $this->drupalGet('node/' . $this->node->nid, array('query' => 'page=1')); $this->assertTrue($this->commentExists($reply, TRUE), t('Page two exists. %s')); $this->setCommentsPerPage(50); @@ -454,6 +458,109 @@ class CommentAnonymous extends CommentHe } } +class CommentFormTest extends CommentHelperCase { + public static function getInfo() { + return array( + 'name' => t('Comment form settings'), + 'description' => t('Test comment form settings and comment form visibility'), + 'group' => t('Comment') + ); + } + + function testFormVisibility() { + $content_types = node_type_get_types(); + $this->drupalLogin($this->admin_user); + foreach ($content_types as $type) { + $content_type = $type->type; + variable_set('comment_preview_' . $content_type, 0); + variable_set('comment_subject_field_' . $content_type, 1); + variable_set('comment_form_location_' . $content_type, 1); + + $node = $this->drupalCreateNode(array('type' => $content_type)); + $this->drupalGet('node/' . $node->nid); + $this->assertField('edit-comment', t('Comment form appears on the node page.')); + + variable_set('comment_form_location_' . $content_type, 0); + $this->drupalGet('node/' . $node->nid); + $this->assertRaw(t('Add new comment'), t('Link to comment form appears on the node page.')); + $this->assertNoField('edit-comment', t('Comment form does not appear on the node page.')); + } + $this->drupalLogout(); + } +} + +/** + * Verify pagination of comments + */ +class CommentPagerTest extends CommentHelperCase { + + public static function getInfo() { + return array( + 'name' => t('Comment paging settings'), + 'description' => t('Test paging of comments and their settings.'), + 'group' => t('Comment'), + ); + } + + function testCommentPaging() { + $this->drupalLogin($this->admin_user); + $this->setCommentForm(TRUE); + $this->setCommentSubject(TRUE); + $this->setCommentPreview(FALSE); + $node = $this->drupalCreateNode(array('type' => 'article', 'promote' => 1)); + $comments = array(); + $comments[] = $this->postComment($node, $this->randomName(), $this->randomName(), FALSE, TRUE); + $comments[] = $this->postComment($node, $this->randomName(), $this->randomName(), FALSE, TRUE); + $comments[] = $this->postComment($node, $this->randomName(), $this->randomName(), FALSE, TRUE); + + $this->setCommentSettings('comment_default_mode', COMMENT_MODE_FLAT_EXPANDED, t('Comment paging changed.')); + $this->setCommentsPerPage(1); + $this->drupalGet('node/' . $node->nid); + $this->assertRaw(t('next'), t('Paging links found.')); + $this->assertTrue($this->commentExists($comments[0]), t('Comment 1 appears on page 1.')); + $this->assertFalse($this->commentExists($comments[1]), t('Comment 2 does not appear on page 1.')); + $this->assertFalse($this->commentExists($comments[2]), t('Comment 3 does not appear on page 1.')); + + $this->drupalGet('node/' . $node->nid, array('query' => 'page=1')); + $this->assertTrue($this->commentExists($comments[1]), t('Comment 2 appears on page 2.')); + $this->assertFalse($this->commentExists($comments[0]), t('Comment 1 does not appear on page 2.')); + $this->assertFalse($this->commentExists($comments[2]), t('Comment 3 does not appear on page 2.')); + + $this->drupalGet('node/' . $node->nid, array('query' => 'page=2')); + $this->assertTrue($this->commentExists($comments[2]), t('Comment 3 appears on page 3.')); + $this->assertFalse($this->commentExists($comments[0]), t('Comment 1 does not appear on page 3.')); + $this->assertFalse($this->commentExists($comments[1]), t('Comment 2 does not appear on page 3.')); + + // Post a reply to the oldest comment and test again. + $replies = array(); + $oldest_comment = reset($comments); + $this->drupalGet('comment/reply/' . $node->nid . '/' . $oldest_comment->id); + $reply = $this->postComment(null, $this->randomName(), $this->randomName(), FALSE, TRUE); + + $this->setCommentsPerPage(2); + // We are still in flat view - the replies should not be on the first, + // even though they are replies to the oldest comment + $this->drupalGet('node/' . $node->nid, array('query' => 'page=0')); + $this->assertFalse($this->commentExists($reply, TRUE), t('In flat mode, reply does not appear on page 1.')); + + // If we switch to threaded mode, the replies on the oldest comment + // should be bumped to the first page and comment 6 should be bumped + // to the second page + $this->setCommentSettings('comment_default_mode', COMMENT_MODE_THREADED_EXPANDED, t('Switched to threaded mode.')); + $this->drupalGet('node/' . $node->nid, array('query' => 'page=0')); + $this->assertTrue($this->commentExists($reply, TRUE), t('In threaded mode, reply appears on page 1.')); + $this->assertFalse($this->commentExists($comments[1]), t('In threaded mode, comment 2 has been bumped off of page 1.')); + + // If (# replies > # comments per page) in threaded expanded view, + // the overage should be bumped + $reply2 = $this->postComment(NULL, $this->randomName(), $this->randomName(), FALSE, TRUE); + $this->drupalGet('node/' . $node->nid, array('query' => 'page=0')); + $this->assertFalse($this->commentExists($reply2, TRUE), t('In threaded mode where # replies > # comments per page, the newest reply does not appear on page 1.')); + + $this->drupalLogout(); + } +} + class CommentApprovalTest extends CommentHelperCase { public static function getInfo() { return array(