Make WordPress Core

Ticket #33871: 33871.diff

File 33871.diff, 3.7 KB (added by boonebgorges, 9 years ago)
  • src/wp-admin/includes/comment.php

    diff --git src/wp-admin/includes/comment.php src/wp-admin/includes/comment.php
    index dfb3922..a7cdecd 100644
     
    99/**
    1010 * Determine if a comment exists based on author and date.
    1111 *
     12 * For best performance, use `$timezone = 'gmt'`, which queries a field that is properly indexed. The default value
     13 * for `$timezone` is 'blog' for legacy reasons.
     14 *
    1215 * @since 2.0.0
     16 * @since 4.4.0 Added the `$timezone` parameter.
    1317 *
    1418 * @global wpdb $wpdb WordPress database abstraction object.
    1519 *
    16  * @param string $comment_author Author of the comment
    17  * @param string $comment_date Date of the comment
     20 * @param string $comment_author Author of the comment.
     21 * @param string $comment_date   Date of the comment.
     22 * @param string $timezone       Timezone. Accepts 'blog' or 'gmt'. Default 'blog'.
     23 *
    1824 * @return mixed Comment post ID on success.
    1925 */
    20 function comment_exists($comment_author, $comment_date) {
     26function comment_exists( $comment_author, $comment_date, $timezone = 'blog' ) {
    2127        global $wpdb;
    2228
     29        $date_field = 'gmt' === $timezone ? 'comment_date_gmt' : 'comment_date';
    2330        return $wpdb->get_var( $wpdb->prepare("SELECT comment_post_ID FROM $wpdb->comments
    24                         WHERE comment_author = %s AND comment_date = %s",
     31                        WHERE comment_author = %s AND $date_field = %s",
    2532                        stripslashes( $comment_author ),
    2633                        stripslashes( $comment_date )
    2734        ) );
    function comment_footer_die( $msg ) { 
    181188        echo "<div class='wrap'><p>$msg</p></div>";
    182189        include( ABSPATH . 'wp-admin/admin-footer.php' );
    183190        die;
    184 }
    185  No newline at end of file
     191}
  • tests/phpunit/tests/admin/includesComment.php

    diff --git tests/phpunit/tests/admin/includesComment.php tests/phpunit/tests/admin/includesComment.php
    index 5b7b01b..fe9b86c 100644
    class Tests_Admin_IncludesComment extends WP_UnitTestCase { 
    2323                $this->assertNull( comment_exists( 1, '2004-01-02 12:00:00' ) );
    2424                $this->assertEquals( $p1, comment_exists( 1, '2014-05-06 12:00:00' ) );
    2525        }
     26
     27        /**
     28         * @ticket 33871
     29         */
     30        public function test_default_value_of_timezone_should_be_blog() {
     31                $p = $this->factory->post->create();
     32                $c = $this->factory->comment->create( array(
     33                        'comment_author' => 1,
     34                        'comment_post_ID' => $p,
     35                        'comment_date' => '2014-05-06 12:00:00',
     36                        'comment_date_gmt' => '2014-05-06 07:00:00',
     37                ) );
     38
     39                $this->assertEquals( $p, comment_exists( 1, '2014-05-06 12:00:00' ) );
     40        }
     41
     42        /**
     43         * @ticket 33871
     44         */
     45        public function test_should_respect_timezone_blog() {
     46                $p = $this->factory->post->create();
     47                $c = $this->factory->comment->create( array(
     48                        'comment_author' => 1,
     49                        'comment_post_ID' => $p,
     50                        'comment_date' => '2014-05-06 12:00:00',
     51                        'comment_date_gmt' => '2014-05-06 07:00:00',
     52                ) );
     53
     54                $this->assertEquals( $p, comment_exists( 1, '2014-05-06 12:00:00', 'blog' ) );
     55        }
     56
     57        /**
     58         * @ticket 33871
     59         */
     60        public function test_should_respect_timezone_gmt() {
     61                $p = $this->factory->post->create();
     62                $c = $this->factory->comment->create( array(
     63                        'comment_author' => 1,
     64                        'comment_post_ID' => $p,
     65                        'comment_date' => '2014-05-06 12:00:00',
     66                        'comment_date_gmt' => '2014-05-06 07:00:00',
     67                ) );
     68
     69                $this->assertEquals( $p, comment_exists( 1, '2014-05-06 07:00:00', 'gmt' ) );
     70        }
     71
     72        /**
     73         * @ticket 33871
     74         */
     75        public function test_invalid_timezone_should_fall_back_on_blog() {
     76                $p = $this->factory->post->create();
     77                $c = $this->factory->comment->create( array(
     78                        'comment_author' => 1,
     79                        'comment_post_ID' => $p,
     80                        'comment_date' => '2014-05-06 12:00:00',
     81                        'comment_date_gmt' => '2014-05-06 07:00:00',
     82                ) );
     83
     84                $this->assertEquals( $p, comment_exists( 1, '2014-05-06 12:00:00', 'not_a_valid_value' ) );
     85        }
    2686}