Make WordPress Core


Ignore:
Timestamp:
06/23/2020 05:22:39 AM (3 years ago)
Author:
whyisjake
Message:

Comments: Ensure the proper comment count and pages for unapproved comments.

Previiously, unapproved comments can alter the comment count, returning incorrect page numbers.

Fixes #8973.

Props GregMulhauser, dd32, ryan, mrmist, hakre, solarissmoke, billerickson, ericlewis, SergeyBiryukov, chriscct7, dossy, lukecavanagh, renggo888, jdorner, matjack1, pento, audrasjb, imath, davidbaumwald, whyisjake.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/tests/phpunit/tests/comment/getPageOfComment.php

    r47122 r48133  
    440440        $this->assertEquals( 2, get_page_of_comment( $c3 ) );
    441441    }
     442
     443    /**
     444     * @ticket 8973
     445     */
     446    public function test_page_number_when_unapproved_comments_are_included_for_current_commenter() {
     447        $post         = self::factory()->post->create();
     448        $comment_args = array(
     449            'comment_post_ID'      => $post,
     450            'comment_approved'     => 0,
     451            'comment_author_email' => 'foo@bar.test',
     452            'comment_author'       => 'Foo',
     453            'comment_author_url'   => 'https://bar.test',
     454        );
     455
     456        for ( $i = 1; $i < 4; $i++ ) {
     457            self::factory()->comment->create(
     458                array_merge(
     459                    $comment_args,
     460                    array(
     461                        'comment_date_gmt' => gmdate( 'Y-m-d H:i:s', time() - ( $i * 1000 ) ),
     462                    )
     463                )
     464            );
     465        }
     466
     467        $new_unapproved = self::factory()->comment->create(
     468            $comment_args
     469        );
     470
     471        add_filter( 'wp_get_current_commenter', array( $this, 'get_current_commenter' ) );
     472
     473        $page     = get_page_of_comment( $new_unapproved, array( 'per_page' => 3 ) );
     474        $comments = get_comments(
     475            array(
     476                'number'             => 3,
     477                'paged'              => $page,
     478                'post_id'            => $post,
     479                'status'             => 'approve',
     480                'include_unapproved' => array( 'foo@bar.test' ),
     481                'orderby'            => 'comment_date_gmt',
     482                'order'              => 'ASC',
     483            )
     484        );
     485
     486        remove_filter( 'wp_get_current_commenter', array( $this, 'get_current_commenter' ) );
     487
     488        $this->assertContains( $new_unapproved, wp_list_pluck( $comments, 'comment_ID' ) );
     489    }
     490
     491    /**
     492     * @ticket 8973
     493     */
     494    public function test_page_number_when_unapproved_comments_are_included_for_current_user() {
     495        $current_user = get_current_user_id();
     496        $post         = self::factory()->post->create();
     497        $user         = self::factory()->user->create_and_get();
     498        $comment_args = array(
     499            'comment_post_ID'      => $post,
     500            'comment_approved'     => 0,
     501            'comment_author_email' => $user->user_email,
     502            'comment_author'       => $user->display_name,
     503            'comment_author_url'   => $user->user_url,
     504            'user_id'              => $user->ID,
     505        );
     506
     507        for ( $i = 1; $i < 4; $i++ ) {
     508            self::factory()->comment->create(
     509                array_merge(
     510                    $comment_args,
     511                    array(
     512                        'comment_date_gmt' => gmdate( 'Y-m-d H:i:s', time() - ( $i * 1000 ) ),
     513                    )
     514                )
     515            );
     516        }
     517
     518        $new_unapproved = self::factory()->comment->create(
     519            $comment_args
     520        );
     521
     522        wp_set_current_user( $user->ID );
     523
     524        $page     = get_page_of_comment( $new_unapproved, array( 'per_page' => 3 ) );
     525        $comments = get_comments(
     526            array(
     527                'number'             => 3,
     528                'paged'              => $page,
     529                'post_id'            => $post,
     530                'status'             => 'approve',
     531                'include_unapproved' => array( $user->ID ),
     532                'orderby'            => 'comment_date_gmt',
     533                'order'              => 'ASC',
     534            )
     535        );
     536
     537        $this->assertContains( $new_unapproved, wp_list_pluck( $comments, 'comment_ID' ) );
     538
     539        wp_set_current_user( $current_user );
     540    }
     541
     542    public function get_current_commenter() {
     543        return array(
     544            'comment_author_email' => 'foo@bar.test',
     545            'comment_author'       => 'Foo',
     546            'comment_author_url'   => 'https://bar.test',
     547        );
     548    }
    442549}
Note: See TracChangeset for help on using the changeset viewer.