Make WordPress Core

Ticket #35175: 35175.2.diff

File 35175.2.diff, 4.9 KB (added by boonebgorges, 9 years ago)
  • src/wp-includes/comment-template.php

    diff --git src/wp-includes/comment-template.php src/wp-includes/comment-template.php
    index ac84324..439ac39 100644
    function wp_list_comments( $args = array(), $comments = null ) { 
    18761876         */
    18771877        $r = apply_filters( 'wp_list_comments_args', $r );
    18781878
     1879        /*
     1880         * If 'page' or 'per_page' has been passed, and does not match what's in $wp_query,
     1881         * perform a separate comment query and allow Walker_Comment to paginate.
     1882         */
     1883        $default_comments_page = get_option( 'default_comments_page' );
     1884        if ( is_singular() && ( $r['page'] || $r['per_page'] ) ) {
     1885                $current_cpage = get_query_var( 'cpage' );
     1886                if ( ! $current_cpage ) {
     1887                        $current_cpage = 'newest' === $default_comments_page ? 1 : $wp_query->max_num_comment_pages;
     1888                }
     1889
     1890                $current_per_page = get_query_var( 'comments_per_page' );
     1891                if ( $r['page'] != $current_cpage || $r['per_page'] != $current_per_page ) {
     1892                        $comments = get_comments( array(
     1893                                'post_id' => get_queried_object_id(),
     1894                                'orderby' => 'comment_date_gmt',
     1895                                'order' => 'ASC',
     1896                                'status' => 'all',
     1897                        ) );
     1898                }
     1899        }
     1900
    18791901        // Figure out what comments we'll be looping through ($_comments)
    18801902        if ( null !== $comments ) {
    18811903                $comments = (array) $comments;
    function wp_list_comments( $args = array(), $comments = null ) { 
    19041926
    19051927                // Pagination is already handled by `WP_Comment_Query`, so we tell Walker not to bother.
    19061928                if ( $wp_query->max_num_comment_pages ) {
    1907                         $default_comments_page = get_option( 'default_comments_page' );
    19081929                        $cpage = get_query_var( 'cpage' );
    19091930                        if ( 'newest' === $default_comments_page ) {
    19101931                                $r['cpage'] = $cpage;
  • new file tests/phpunit/tests/comment/wpListComments.php

    diff --git tests/phpunit/tests/comment/wpListComments.php tests/phpunit/tests/comment/wpListComments.php
    new file mode 100644
    index 0000000..84f6e4c
    - +  
     1<?php
     2
     3/**
     4 * @group comment
     5 */
     6class Tests_Comment_WpListComments extends WP_UnitTestCase {
     7        /**
     8         * @ticket 35175
     9         */
     10        public function test_should_respect_page_param() {
     11                $p = self::factory()->post->create();
     12
     13                $comments = array();
     14                $now = time();
     15                for ( $i = 0; $i <= 5; $i++ ) {
     16                        $comments[] = self::factory()->comment->create( array(
     17                                'comment_post_ID' => $p,
     18                                'comment_date_gmt' => date( 'Y-m-d H:i:s', $now - $i ),
     19                                'comment_author' => 'Commenter ' . $i,
     20                        ) );
     21                }
     22
     23                update_option( 'page_comments', true );
     24                update_option( 'comments_per_page', 2 );
     25
     26                $this->go_to( get_permalink( $p ) );
     27
     28                // comments_template() populates $wp_query->comments
     29                get_echo( 'comments_template' );
     30
     31                $found = wp_list_comments( array(
     32                        'page' => 2,
     33                        'echo' => false,
     34                ) );
     35
     36                preg_match_all( '|id="comment\-([0-9]+)"|', $found, $matches );
     37
     38                $this->assertEqualSets( array( $comments[2], $comments[3] ), $matches[1] );
     39        }
     40
     41        /**
     42         * @ticket 35175
     43         */
     44        public function test_should_respect_per_page_param() {
     45                $p = self::factory()->post->create();
     46
     47                $comments = array();
     48                $now = time();
     49                for ( $i = 0; $i <= 5; $i++ ) {
     50                        $comments[] = self::factory()->comment->create( array(
     51                                'comment_post_ID' => $p,
     52                                'comment_date_gmt' => date( 'Y-m-d H:i:s', $now - $i ),
     53                                'comment_author' => 'Commenter ' . $i,
     54                        ) );
     55                }
     56
     57                update_option( 'page_comments', true );
     58                update_option( 'comments_per_page', 2 );
     59
     60                $this->go_to( get_permalink( $p ) );
     61
     62                // comments_template() populates $wp_query->comments
     63                get_echo( 'comments_template' );
     64
     65                $found = wp_list_comments( array(
     66                        'per_page' => 3,
     67                        'echo' => false,
     68                ) );
     69
     70                preg_match_all( '|id="comment\-([0-9]+)"|', $found, $matches );
     71
     72                $this->assertEqualSets( array( $comments[0], $comments[1], $comments[2] ), $matches[1] );
     73        }
     74
     75        /**
     76         * @ticket 35175
     77         */
     78        public function test_should_respect_reverse_top_level_param() {
     79                $p = self::factory()->post->create();
     80
     81                $comments = array();
     82                $now = time();
     83                for ( $i = 0; $i <= 5; $i++ ) {
     84                        $comments[] = self::factory()->comment->create( array(
     85                                'comment_post_ID' => $p,
     86                                'comment_date_gmt' => date( 'Y-m-d H:i:s', $now - $i ),
     87                                'comment_author' => 'Commenter ' . $i,
     88                        ) );
     89                }
     90
     91                update_option( 'page_comments', true );
     92                update_option( 'comments_per_page', 2 );
     93
     94                $this->go_to( get_permalink( $p ) );
     95
     96                // comments_template() populates $wp_query->comments
     97                get_echo( 'comments_template' );
     98
     99                $found1 = wp_list_comments( array(
     100                        'reverse_top_level' => true,
     101                        'echo' => false,
     102                ) );
     103                preg_match_all( '|id="comment\-([0-9]+)"|', $found1, $matches );
     104                $this->assertSame( array( $comments[0], $comments[1] ), array_map( 'intval', $matches[1] ) );
     105
     106                $found2 = wp_list_comments( array(
     107                        'reverse_top_level' => false,
     108                        'echo' => false,
     109                ) );
     110                preg_match_all( '|id="comment\-([0-9]+)"|', $found2, $matches );
     111                $this->assertSame( array( $comments[1], $comments[0] ), array_map( 'intval', $matches[1] ) );
     112        }
     113}