Make WordPress Core

Changeset 28252


Ignore:
Timestamp:
05/04/2014 11:06:07 PM (10 years ago)
Author:
wonderboymusic
Message:

In WP_Date_Query::get_sql_for_subquery(), don't parse duplicate parameters - only parse one of w and week or month and monthnum.

Adds unit tests.

Props oso96_2000, ChriCo.
Fixes #25835.

Location:
trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/date.php

    r27648 r28252  
    257257        if ( isset( $query['month'] ) && $value = $this->build_value( $compare, $query['month'] ) )
    258258            $where_parts[] = "MONTH( $column ) $compare $value";
    259 
    260         // Legacy
    261         if ( isset( $query['monthnum'] ) && $value = $this->build_value( $compare, $query['monthnum'] ) )
     259        else if ( isset( $query['monthnum'] ) && $value = $this->build_value( $compare, $query['monthnum'] ) )
    262260            $where_parts[] = "MONTH( $column ) $compare $value";
    263261
    264262        if ( isset( $query['week'] ) && false !== ( $value = $this->build_value( $compare, $query['week'] ) ) )
    265263            $where_parts[] = _wp_mysql_week( $column ) . " $compare $value";
    266 
    267         // Legacy
    268         if ( isset( $query['w'] ) && false !== ( $value = $this->build_value( $compare, $query['w'] ) ) )
     264        else if ( isset( $query['w'] ) && false !== ( $value = $this->build_value( $compare, $query['w'] ) ) )
    269265            $where_parts[] = _wp_mysql_week( $column ) . " $compare $value";
    270266
  • trunk/tests/phpunit/tests/query/dateQuery.php

    r25139 r28252  
    556556        $this->assertEquals( $expected_dates, wp_list_pluck( $posts, 'post_date' ) );
    557557    }
     558
     559    public function test_date_params_monthnum_m_duplicate() {
     560        $posts = $this->_get_query_result( array(
     561            'date_query' => array(
     562                'month' => 5,
     563                'monthnum' => 9
     564            ),
     565        ) );
     566
     567        $expected_dates = array(
     568            '1972-05-24 14:53:45',
     569            '2003-05-27 22:45:07',
     570            '2004-05-22 12:34:12',
     571            '2007-05-16 17:32:22',
     572            '2025-05-20 10:13:01',
     573        );
     574
     575        $this->assertEquals( $expected_dates, wp_list_pluck( $posts, 'post_date' ) );
     576
     577        $this->assertContains( "AND ( ( MONTH( post_date ) = 5 ) ) AND", $this->q->request );
     578
     579        $this->assertNotContains( "AND ( ( MONTH( post_date ) = 5 AND MONTH( post_date ) = 9 ) ) AND", $this->q->request );
     580    }
     581
     582    public function test_date_params_week_w_duplicate() {
     583        $posts = $this->_get_query_result( array(
     584            'date_query' => array(
     585                'week' => 21,
     586                'w' => 22
     587            ),
     588        ) );
     589
     590        $expected_dates = array(
     591            '1972-05-24 14:53:45',
     592            '2004-05-22 12:34:12',
     593            '2025-05-20 10:13:01',
     594        );
     595
     596
     597        $this->assertEquals( $expected_dates, wp_list_pluck( $posts, 'post_date' ) );
     598
     599        $this->assertContains( "AND ( ( WEEK( post_date, 1 ) = 21 ) ) AND", $this->q->request );
     600
     601        $this->assertNotContains( "AND ( ( WEEK( post_date, 1 ) = 21 AND WEEK( post_date, 1 ) = 22 ) ) AND", $this->q->request );
     602    }
    558603}
Note: See TracChangeset for help on using the changeset viewer.