<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head><meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>[34735] trunk: Ensure that comment permalinks reflect pagination.</title>
</head>
<body>

<style type="text/css"><!--
#msg dl.meta { border: 1px #006 solid; background: #369; padding: 6px; color: #fff; }
#msg dl.meta dt { float: left; width: 6em; font-weight: bold; }
#msg dt:after { content:':';}
#msg dl, #msg dt, #msg ul, #msg li, #header, #footer, #logmsg { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt;  }
#msg dl a { font-weight: bold}
#msg dl a:link    { color:#fc3; }
#msg dl a:active  { color:#ff0; }
#msg dl a:visited { color:#cc6; }
h3 { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; font-weight: bold; }
#msg pre { overflow: auto; background: #ffc; border: 1px #fa0 solid; padding: 6px; }
#logmsg { background: #ffc; border: 1px #fa0 solid; padding: 1em 1em 0 1em; }
#logmsg p, #logmsg pre, #logmsg blockquote { margin: 0 0 1em 0; }
#logmsg p, #logmsg li, #logmsg dt, #logmsg dd { line-height: 14pt; }
#logmsg h1, #logmsg h2, #logmsg h3, #logmsg h4, #logmsg h5, #logmsg h6 { margin: .5em 0; }
#logmsg h1:first-child, #logmsg h2:first-child, #logmsg h3:first-child, #logmsg h4:first-child, #logmsg h5:first-child, #logmsg h6:first-child { margin-top: 0; }
#logmsg ul, #logmsg ol { padding: 0; list-style-position: inside; margin: 0 0 0 1em; }
#logmsg ul { text-indent: -1em; padding-left: 1em; }#logmsg ol { text-indent: -1.5em; padding-left: 1.5em; }
#logmsg > ul, #logmsg > ol { margin: 0 0 1em 0; }
#logmsg pre { background: #eee; padding: 1em; }
#logmsg blockquote { border: 1px solid #fa0; border-left-width: 10px; padding: 1em 1em 0 1em; background: white;}
#logmsg dl { margin: 0; }
#logmsg dt { font-weight: bold; }
#logmsg dd { margin: 0; padding: 0 0 0.5em 0; }
#logmsg dd:before { content:'\00bb';}
#logmsg table { border-spacing: 0px; border-collapse: collapse; border-top: 4px solid #fa0; border-bottom: 1px solid #fa0; background: #fff; }
#logmsg table th { text-align: left; font-weight: normal; padding: 0.2em 0.5em; border-top: 1px dotted #fa0; }
#logmsg table td { text-align: right; border-top: 1px dotted #fa0; padding: 0.2em 0.5em; }
#logmsg table thead th { text-align: center; border-bottom: 1px solid #fa0; }
#logmsg table th.Corner { text-align: left; }
#logmsg hr { border: none 0; border-top: 2px dashed #fa0; height: 1px; }
#header, #footer { color: #fff; background: #636; border: 1px #300 solid; padding: 6px; }
#patch { width: 100%; }
#patch h4 {font-family: verdana,arial,helvetica,sans-serif;font-size:10pt;padding:8px;background:#369;color:#fff;margin:0;}
#patch .propset h4, #patch .binary h4 {margin:0;}
#patch pre {padding:0;line-height:1.2em;margin:0;}
#patch .diff {width:100%;background:#eee;padding: 0 0 10px 0;overflow:auto;}
#patch .propset .diff, #patch .binary .diff  {padding:10px 0;}
#patch span {display:block;padding:0 10px;}
#patch .modfile, #patch .addfile, #patch .delfile, #patch .propset, #patch .binary, #patch .copfile {border:1px solid #ccc;margin:10px 0;}
#patch ins {background:#dfd;text-decoration:none;display:block;padding:0 10px;}
#patch del {background:#fdd;text-decoration:none;display:block;padding:0 10px;}
#patch .lines, .info {color:#888;background:#fff;}
--></style>
<div id="msg">
<dl class="meta" style="font-size: 105%">
<dt style="float: left; width: 6em; font-weight: bold">Revision</dt> <dd><a style="font-weight: bold" href="https://core.trac.wordpress.org/changeset/34735">34735</a><script type="application/ld+json">{"@context":"http://schema.org","@type":"EmailMessage","description":"Review this Commit","action":{"@type":"ViewAction","url":"https://core.trac.wordpress.org/changeset/34735","name":"Review Commit"}}</script></dd>
<dt style="float: left; width: 6em; font-weight: bold">Author</dt> <dd>boonebgorges</dd>
<dt style="float: left; width: 6em; font-weight: bold">Date</dt> <dd>2015-10-01 05:12:39 +0000 (Thu, 01 Oct 2015)</dd>
</dl>

<pre style='padding-left: 1em; margin: 2em 0; border-left: 2px solid #ccc; line-height: 1.25; font-size: 105%; font-family: sans-serif'>Ensure that comment permalinks reflect pagination.

After <a href="https://core.trac.wordpress.org/changeset/34561">[34561]</a>, `wp_list_comments()` no longer passed all of a post's comments
to `Walker_Comments`. As a result, calls to `get_comment_link()` occurring
inside the comment loop had insufficient context to determine the proper
'cpage' value to use when generating comment permalinks. This, in turn, caused
comment permalinks to behave erratically.

The current changeset addresses the problem as follows:

* `get_comment_link()` now accepts a 'cpage' parameter. When present, 'cpage' will be used to build the comment permalink - no automatic calculation will take place.
* When called within the main loop, `wp_list_comments()` calculates the proper 'cpage' value for comments in the loop, and passes it down to `get_comment_link()`.
* `cpage` and `comment-page-x` query vars are generally required in comment permalinks (see <a href="https://core.trac.wordpress.org/ticket/34068">#34068</a>), but an exception is made when 'default_comment_page=oldest': the bare post permalink will always be the same as `cpage=1`, so `cpage` is excluded in this case.

Props peterwilsoncc for assiduous spreadsheeting.
Fixes <a href="https://core.trac.wordpress.org/ticket/34073">#34073</a>.</pre>

<h3>Modified Paths</h3>
<ul>
<li><a href="#trunksrcwpincludescommenttemplatephp">trunk/src/wp-includes/comment-template.php</a></li>
<li><a href="#trunktestsphpunittestscommentcommentsTemplatephp">trunk/tests/phpunit/tests/comment/commentsTemplate.php</a></li>
</ul>

<h3>Added Paths</h3>
<ul>
<li><a href="#trunktestsphpunittestscommentgetCommentLinkphp">trunk/tests/phpunit/tests/comment/getCommentLink.php</a></li>
</ul>

</div>
<div id="patch">
<h3>Diff</h3>
<a id="trunksrcwpincludescommenttemplatephp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/src/wp-includes/comment-template.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/src/wp-includes/comment-template.php        2015-10-01 05:08:31 UTC (rev 34734)
+++ trunk/src/wp-includes/comment-template.php  2015-10-01 05:12:39 UTC (rev 34735)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -659,7 +659,7 @@
</span><span class="cx" style="display: block; padding: 0 10px">  * Retrieve the link to a given comment.
</span><span class="cx" style="display: block; padding: 0 10px">  *
</span><span class="cx" style="display: block; padding: 0 10px">  * @since 1.5.0
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- * @since 4.4.0 Added the ability for `$comment` to also accept a WP_Comment object.
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * @since 4.4.0 Added the ability for `$comment` to also accept a WP_Comment object. Added `$cpage` argument.
</ins><span class="cx" style="display: block; padding: 0 10px">  *
</span><span class="cx" style="display: block; padding: 0 10px">  * @see get_page_of_comment()
</span><span class="cx" style="display: block; padding: 0 10px">  *
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -670,10 +670,12 @@
</span><span class="cx" style="display: block; padding: 0 10px">  * @param array               $args {
</span><span class="cx" style="display: block; padding: 0 10px">  *     An array of optional arguments to override the defaults.
</span><span class="cx" style="display: block; padding: 0 10px">  *
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- *     @type string $type      Passed to {@see get_page_of_comment()}.
- *     @type int    $page      Current page of comments, for calculating comment pagination.
- *     @type int    $per_page  Per-page value for comment pagination.
- *     @type int    $max_depth Passed to {@see get_page_of_comment()}.
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ *     @type string     $type      Passed to {@see get_page_of_comment()}.
+ *     @type int        $page      Current page of comments, for calculating comment pagination.
+ *     @type int        $per_page  Per-page value for comment pagination.
+ *     @type int        $max_depth Passed to {@see get_page_of_comment()}.
+ *     @type int|string $cpage     Value to use for the comment's "comment-page" or "cpage" value. If provided, this
+ *                                 value overrides any value calculated from `$page` and `$per_page`.
</ins><span class="cx" style="display: block; padding: 0 10px">  * }
</span><span class="cx" style="display: block; padding: 0 10px">  * @return string The permalink to the given comment.
</span><span class="cx" style="display: block; padding: 0 10px">  */
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -687,42 +689,94 @@
</span><span class="cx" style="display: block; padding: 0 10px">                $args = array( 'page' => $args );
</span><span class="cx" style="display: block; padding: 0 10px">        }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-        $defaults = array( 'type' => 'all', 'page' => '', 'per_page' => '', 'max_depth' => '' );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $defaults = array(
+               'type'      => 'all',
+               'page'      => '',
+               'per_page'  => '',
+               'max_depth' => '',
+               'cpage'     => null,
+       );
</ins><span class="cx" style="display: block; padding: 0 10px">         $args = wp_parse_args( $args, $defaults );
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-        if ( '' === $args['per_page'] )
-               $args['per_page'] = get_option('comments_per_page');
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $link = get_permalink( $comment->comment_post_ID );
</ins><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-        if ( empty($args['per_page']) ) {
-               $args['per_page'] = 0;
-               $args['page'] = 0;
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ // The 'cpage' param takes precedence.
+       if ( ! is_null( $args['cpage'] ) ) {
+               $cpage = $args['cpage'];
+
+       // No 'cpage' is provided, so we calculate one.
+       } else {
+               if ( '' === $args['per_page'] ) {
+                       $args['per_page'] = get_option('comments_per_page');
+               }
+
+               if ( empty( $args['per_page'] ) ) {
+                       $args['per_page'] = 0;
+                       $args['page'] = 0;
+               }
+
+               $cpage = $args['page'];
+
+               if ( '' == $cpage ) {
+                       if ( ! empty( $in_comment_loop ) ) {
+                               $cpage = get_query_var( 'cpage' );
+                       } else {
+                               // Requires a database hit, so we only do it when we can't figure out from context.
+                               $cpage = get_page_of_comment( $comment->comment_ID, $args );
+                       }
+               }
+
+               // Drop the 'page' var if we're on the default page.
+               $comment_post = get_post( $comment->comment_post_ID );
+               if ( $args['per_page'] ) {
+                       $total_pages = ceil( $comment_post->comment_count / $args['per_page'] );
+               } else {
+                       $total_pages = 1;
+               }
+
+               /*
+                * If the default page displays the oldest comments, the permalinks for comments on the default page
+                * do not need a 'cpage' query var.
+                */
+               $default_comments_page = get_option( 'default_comments_page' );
+               if ( 'oldest' === get_option( 'default_comments_page' ) && 1 === $cpage ) {
+                       $cpage = '';
+               }
</ins><span class="cx" style="display: block; padding: 0 10px">         }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-        if ( $args['per_page'] ) {
-               if ( '' == $args['page'] )
-                       $args['page'] = ( !empty($in_comment_loop) ) ? get_query_var('cpage') : get_page_of_comment( $comment->comment_ID, $args );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ if ( $cpage ) {
+               if ( $wp_rewrite->using_permalinks() ) {
+                       if ( $cpage ) {
+                               $link = trailingslashit( $link ) . $wp_rewrite->comments_pagination_base . '-' . $cpage;
+                       }
</ins><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                if ( $wp_rewrite->using_permalinks() )
-                       $link = user_trailingslashit( trailingslashit( get_permalink( $comment->comment_post_ID ) ) . $wp_rewrite->comments_pagination_base . '-' . $args['page'], 'comment' );
-               else
-                       $link = add_query_arg( 'cpage', $args['page'], get_permalink( $comment->comment_post_ID ) );
-       } else {
-               $link = get_permalink( $comment->comment_post_ID );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                 $link = user_trailingslashit( $link, 'comment' );
+               } elseif ( $cpage ) {
+                       $link = add_query_arg( 'cpage', $cpage, $link );
+               }
+
</ins><span class="cx" style="display: block; padding: 0 10px">         }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+        if ( $wp_rewrite->using_permalinks() ) {
+               $link = user_trailingslashit( $link, 'comment' );
+       }
+
</ins><span class="cx" style="display: block; padding: 0 10px">         $link = $link . '#comment-' . $comment->comment_ID;
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+
</ins><span class="cx" style="display: block; padding: 0 10px">         /**
</span><span class="cx" style="display: block; padding: 0 10px">         * Filter the returned single comment permalink.
</span><span class="cx" style="display: block; padding: 0 10px">         *
</span><span class="cx" style="display: block; padding: 0 10px">         * @since 2.8.0
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         * @since 4.4.0 Added the `$cpage` parameter.
</ins><span class="cx" style="display: block; padding: 0 10px">          *
</span><span class="cx" style="display: block; padding: 0 10px">         * @see get_page_of_comment()
</span><span class="cx" style="display: block; padding: 0 10px">         *
</span><span class="cx" style="display: block; padding: 0 10px">         * @param string     $link    The comment permalink with '#comment-$id' appended.
</span><span class="cx" style="display: block; padding: 0 10px">         * @param WP_Comment $comment The current comment object.
</span><span class="cx" style="display: block; padding: 0 10px">         * @param array      $args    An array of arguments to override the defaults.
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         * @param int        $cpage   The calculated 'cpage' value.
</ins><span class="cx" style="display: block; padding: 0 10px">          */
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-        return apply_filters( 'get_comment_link', $link, $comment, $args );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ return apply_filters( 'get_comment_link', $link, $comment, $args, $cpage );
</ins><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px"> /**
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -1877,8 +1931,21 @@
</span><span class="cx" style="display: block; padding: 0 10px">                }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">                // Pagination is already handled by `WP_Comment_Query`, so we tell Walker not to bother.
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                if ( 1 < $wp_query->max_num_comment_pages ) {
-                       $r['page'] = 1;
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         if ( $wp_query->max_num_comment_pages ) {
+                       $default_comments_page = get_option( 'default_comments_page' );
+                       $cpage = get_query_var( 'cpage' );
+                       if ( 'newest' === $default_comments_page ) {
+                               $r['cpage'] = $cpage;
+
+                       // When first page shows oldest comments, post permalink is the same as the comment permalink.
+                       } elseif ( $cpage == 1 ) {
+                               $r['cpage'] = '';
+                       } else {
+                               $r['cpage'] = $cpage;
+                       }
+
+                       $r['page'] = 0;
+                       $r['per_page'] = 0;
</ins><span class="cx" style="display: block; padding: 0 10px">                 }
</span><span class="cx" style="display: block; padding: 0 10px">        }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span></span></pre></div>
<a id="trunktestsphpunittestscommentcommentsTemplatephp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/tests/phpunit/tests/comment/commentsTemplate.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/tests/phpunit/tests/comment/commentsTemplate.php    2015-10-01 05:08:31 UTC (rev 34734)
+++ trunk/tests/phpunit/tests/comment/commentsTemplate.php      2015-10-01 05:12:39 UTC (rev 34735)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -409,4 +409,159 @@
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">                $this->assertSame( array( $comment_1 ), $found_cids );
</span><span class="cx" style="display: block; padding: 0 10px">        }
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+
+       /**
+        * @ticket 34073
+        */
+       public function test_comment_permalinks_should_be_correct_when_using_default_display_callback_with_default_comment_page_oldest() {
+               $now = time();
+               $p = $this->factory->post->create();
+               $comment_1 = $this->factory->comment->create( array(
+                       'comment_post_ID' => $p,
+                       'comment_content' => '1',
+                       'comment_date_gmt' => date( 'Y-m-d H:i:s', $now - 100 ),
+               ) );
+               $comment_2 = $this->factory->comment->create( array(
+                       'comment_post_ID' => $p,
+                       'comment_content' => '2',
+                       'comment_date_gmt' => date( 'Y-m-d H:i:s', $now - 200 ),
+               ) );
+               $comment_3 = $this->factory->comment->create( array(
+                       'comment_post_ID' => $p,
+                       'comment_content' => '3',
+                       'comment_date_gmt' => date( 'Y-m-d H:i:s', $now - 300 ),
+               ) );
+               $comment_4 = $this->factory->comment->create( array(
+                       'comment_post_ID' => $p,
+                       'comment_content' => '4',
+                       'comment_date_gmt' => date( 'Y-m-d H:i:s', $now - 400 ),
+               ) );
+
+               update_option( 'comment_order', 'desc' );
+               update_option( 'default_comments_page', 'oldest' );
+
+               $link_p1 = add_query_arg( array(
+                       'comments_per_page' => 2,
+               ), get_permalink( $p ) );
+
+               $this->go_to( $link_p1 );
+
+               $found_p1 = get_echo( 'comments_template' );
+
+               // Find the comment permalinks.
+               preg_match_all( '|href="(.*?#comment-([0-9]+))|', $found_p1, $matches );
+
+               // This is the main post page, so we don't expect any cpage param.
+               foreach ( $matches[1] as $m ) {
+                       $this->assertNotContains( 'cpage', $m );
+               }
+
+               $link_p2 = add_query_arg( array(
+                       'cpage' => 2,
+                       'comments_per_page' => 2,
+               ), get_permalink( $p ) );
+
+               $this->go_to( $link_p2 );
+
+               $found_p2 = get_echo( 'comments_template' );
+
+               // Find the comment permalinks.
+               preg_match_all( '|href="(.*?#comment-([0-9]+))|', $found_p2, $matches );
+
+               // They should all be on page 2.
+               foreach ( $matches[1] as $m ) {
+                       $this->assertContains( 'cpage=2', $m );
+               }
+       }
+
+       /**
+        * @ticket 34073
+        */
+       public function test_comment_permalinks_should_be_correct_when_using_default_display_callback_with_default_comment_page_newest() {
+               $now = time();
+               $p = $this->factory->post->create();
+               $comment_1 = $this->factory->comment->create( array(
+                       'comment_post_ID' => $p,
+                       'comment_content' => '1',
+                       'comment_date_gmt' => date( 'Y-m-d H:i:s', $now - 100 ),
+               ) );
+               $comment_2 = $this->factory->comment->create( array(
+                       'comment_post_ID' => $p,
+                       'comment_content' => '2',
+                       'comment_date_gmt' => date( 'Y-m-d H:i:s', $now - 200 ),
+               ) );
+               $comment_3 = $this->factory->comment->create( array(
+                       'comment_post_ID' => $p,
+                       'comment_content' => '3',
+                       'comment_date_gmt' => date( 'Y-m-d H:i:s', $now - 300 ),
+               ) );
+               $comment_4 = $this->factory->comment->create( array(
+                       'comment_post_ID' => $p,
+                       'comment_content' => '4',
+                       'comment_date_gmt' => date( 'Y-m-d H:i:s', $now - 400 ),
+               ) );
+               $comment_5 = $this->factory->comment->create( array(
+                       'comment_post_ID' => $p,
+                       'comment_content' => '4',
+                       'comment_date_gmt' => date( 'Y-m-d H:i:s', $now - 500 ),
+               ) );
+               $comment_6 = $this->factory->comment->create( array(
+                       'comment_post_ID' => $p,
+                       'comment_content' => '4',
+                       'comment_date_gmt' => date( 'Y-m-d H:i:s', $now - 600 ),
+               ) );
+
+               update_option( 'comment_order', 'desc' );
+               update_option( 'default_comments_page', 'newest' );
+
+               $link_p0 = add_query_arg( array(
+                       'comments_per_page' => 2,
+               ), get_permalink( $p ) );
+
+               $this->go_to( $link_p0 );
+
+               $found_p0 = get_echo( 'comments_template' );
+
+               // Find the comment permalinks.
+               preg_match_all( '|href="(.*?#comment-([0-9]+))|', $found_p0, $matches );
+
+               foreach ( $matches[1] as $m ) {
+                       $this->assertContains( 'cpage=3', $m );
+               }
+
+               $link_p2 = add_query_arg( array(
+                       'cpage' => 2,
+                       'comments_per_page' => 2,
+               ), get_permalink( $p ) );
+
+               $this->go_to( $link_p2 );
+
+               $found_p2 = get_echo( 'comments_template' );
+
+               // Find the comment permalinks.
+               preg_match_all( '|href="(.*?#comment-([0-9]+))|', $found_p2, $matches );
+
+               // They should all be on page 2.
+               foreach ( $matches[1] as $m ) {
+                       $this->assertContains( 'cpage=2', $m );
+               }
+
+               // p1 is the last page (neat!).
+               $link_p1 = add_query_arg( array(
+                       'cpage' => 1,
+                       'comments_per_page' => 2,
+               ), get_permalink( $p ) );
+
+               $this->go_to( $link_p1 );
+
+               $found_p1 = get_echo( 'comments_template' );
+
+               // Find the comment permalinks.
+               preg_match_all( '|href="(.*?#comment-([0-9]+))|', $found_p1, $matches );
+
+               // They should all be on page 2.
+               foreach ( $matches[1] as $m ) {
+                       $this->assertContains( 'cpage=1', $m );
+               }
+       }
</ins><span class="cx" style="display: block; padding: 0 10px"> }
</span></span></pre></div>
<a id="trunktestsphpunittestscommentgetCommentLinkphp"></a>
<div class="addfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Added: trunk/tests/phpunit/tests/comment/getCommentLink.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/tests/phpunit/tests/comment/getCommentLink.php                              (rev 0)
+++ trunk/tests/phpunit/tests/comment/getCommentLink.php        2015-10-01 05:12:39 UTC (rev 34735)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -0,0 +1,118 @@
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+<?php
+
+/**
+ * @group comment
+ */
+class Tests_Comment_GetCommentLink extends WP_UnitTestCase {
+       protected $p;
+       protected $comments = array();
+
+       public function setUp() {
+               parent::setUp();
+
+               $now = time();
+               $this->p = $this->factory->post->create();
+               $this->comments[] = $this->factory->comment->create( array(
+                       'comment_post_ID' => $this->p,
+                       'comment_content' => '1',
+                       'comment_date_gmt' => date( 'Y-m-d H:i:s', $now - 100 ),
+               ) );
+               $this->comments[] = $this->factory->comment->create( array(
+                       'comment_post_ID' => $this->p,
+                       'comment_content' => '2',
+                       'comment_date_gmt' => date( 'Y-m-d H:i:s', $now - 200 ),
+               ) );
+               $this->comments[] = $this->factory->comment->create( array(
+                       'comment_post_ID' => $this->p,
+                       'comment_content' => '3',
+                       'comment_date_gmt' => date( 'Y-m-d H:i:s', $now - 300 ),
+               ) );
+               $this->comments[] = $this->factory->comment->create( array(
+                       'comment_post_ID' => $this->p,
+                       'comment_content' => '4',
+                       'comment_date_gmt' => date( 'Y-m-d H:i:s', $now - 400 ),
+               ) );
+               $this->comments[] = $this->factory->comment->create( array(
+                       'comment_post_ID' => $this->p,
+                       'comment_content' => '4',
+                       'comment_date_gmt' => date( 'Y-m-d H:i:s', $now - 500 ),
+               ) );
+               $this->comments[] = $this->factory->comment->create( array(
+                       'comment_post_ID' => $this->p,
+                       'comment_content' => '4',
+                       'comment_date_gmt' => date( 'Y-m-d H:i:s', $now - 600 ),
+               ) );
+       }
+
+       /**
+        * @ticket 34068
+        */
+       public function test_default_comments_page_newest_default_page_should_have_cpage() {
+               update_option( 'default_comments_page', 'newest' );
+               update_option( 'comments_per_page', 2 );
+
+               $found = get_comment_link( $this->comments[1] );
+
+               $this->assertContains( 'cpage=3', $found );
+       }
+
+       /**
+        * @ticket 34068
+        */
+       public function test_default_comments_page_newest_middle_page_should_have_cpage() {
+               update_option( 'default_comments_page', 'newest' );
+               update_option( 'comments_per_page', 2 );
+
+               $found = get_comment_link( $this->comments[3] );
+
+               $this->assertContains( 'cpage=2', $found );
+       }
+
+       /**
+        * @ticket 34068
+        */
+       public function test_default_comments_page_newest_last_page_should_have_cpage() {
+               update_option( 'default_comments_page', 'newest' );
+               update_option( 'comments_per_page', 2 );
+
+               $found = get_comment_link( $this->comments[5] );
+
+               $this->assertContains( 'cpage=1', $found );
+       }
+
+       /**
+        * @ticket 34068
+        */
+       public function test_default_comments_page_oldest_default_page_should_not_have_cpage() {
+               update_option( 'default_comments_page', 'oldest' );
+               update_option( 'comments_per_page', 2 );
+
+               $found = get_comment_link( $this->comments[5] );
+
+               $this->assertNotContains( 'cpage', $found );
+       }
+
+       /**
+        * @ticket 34068
+        */
+       public function test_default_comments_page_oldest_middle_page_should_have_cpage() {
+               update_option( 'default_comments_page', 'oldest' );
+               update_option( 'comments_per_page', 2 );
+
+               $found = get_comment_link( $this->comments[3] );
+
+               $this->assertContains( 'cpage=2', $found );
+       }
+
+       /**
+        * @ticket 34068
+        */
+       public function test_default_comments_page_oldest_last_page_should_have_cpage() {
+               update_option( 'default_comments_page', 'oldest' );
+               update_option( 'comments_per_page', 2 );
+
+               $found = get_comment_link( $this->comments[1] );
+
+               $this->assertContains( 'cpage=3', $found );
+       }
+}
</ins></span></pre>
</div>
</div>

</body>
</html>