Make WordPress Core


Ignore:
Timestamp:
10/16/2014 08:11:13 PM (11 years ago)
Author:
boonebgorges
Message:

Throw notices when invalid date values are passed to WP_Date_Query.

_doing_it_wrong() notices are now generated when passing out-of-range values
(month=13) or invalid dates (2014-02-29).

Includes unit tests.

Props ChriCo.
Fixes #25834.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/tests/phpunit/tests/date/query.php

    r29923 r29925  
    1010 */
    1111class Tests_WP_Date_Query extends WP_UnitTestCase {
     12    public $q;
     13
     14    public function setUp() {
     15        parent::setUp();
     16        unset( $this->q );
     17        $this->q = new WP_Date_Query( array( 'm' => 2 ) );
     18    }
     19
    1220    public function test_construct_date_query_empty() {
    1321        $q = new WP_Date_Query( array() );
     
    583591        $this->assertRegExp( "/DATE_FORMAT\( post_date, '0\.%i%s' \) = 0\.15350*/", $found );
    584592    }
     593
     594    /**
     595     * @ticket 25834
     596     * @expectedIncorrectUsage WP_Date_Query
     597     */
     598    public function test_validate_date_query_before_after(){
     599        // Valid values.
     600        $valid_args = array(
     601            array(
     602                'month' => 2,
     603                'year'  => 2014,
     604            ),
     605            array(
     606                'day'  => 8,
     607                'year' => 2014,
     608            ),
     609        );
     610
     611        foreach ( $valid_args as $args ) {
     612            $this->assertTrue( $this->q->validate_date_values( array( 'before' => $args ) ) );
     613            $this->assertTrue( $this->q->validate_date_values( array( 'after' => $args ) ) );
     614        }
     615
     616        // Invalid values.
     617        $invalid_args = array(
     618            array(
     619                'month' => 13,
     620            ),
     621            array(
     622                'day' => 32,
     623            ),
     624            array(
     625                'minute' => 60,
     626            ),
     627            array(
     628                'second' => 60,
     629            ),
     630            array(
     631                'week' => 54,
     632            ),
     633        );
     634
     635        foreach ( $invalid_args as $args ) {
     636            $this->assertFalse( $this->q->validate_date_values( array( 'before' => $args ) ) );
     637            $this->assertFalse( $this->q->validate_date_values( array( 'after' => $args ) ) );
     638        }
     639    }
     640
     641    /**
     642     * @ticket 25834
     643     * @expectedIncorrectUsage WP_Date_Query
     644     */
     645    public function test_validate_date_query_before_after_with_month(){
     646        // Both are valid.
     647        $args = array(
     648            'before' => array(
     649                'month' => 2,
     650                'year'  => 2014,
     651            ),
     652            'month' => 10,
     653        );
     654        $this->assertTrue( $this->q->validate_date_values( $args ) );
     655
     656        // 'before' is invalid, 'month' is valid.
     657        $args = array(
     658            'before' => array(
     659                'month' => 13,
     660                'year'  => 2014,
     661            ),
     662            'month' => 10,
     663        );
     664        $this->assertFalse( $this->q->validate_date_values( $args ) );
     665
     666        // 'before' is valid, 'month' is invalid.
     667        $args = array(
     668            'before' => array(
     669                'month' => 10,
     670                'year'  => 2014,
     671            ),
     672            'month' => 14,
     673        );
     674        $this->assertFalse( $this->q->validate_date_values( $args ) );
     675
     676        // Both are invalid.
     677        $args = array(
     678            'before' => array(
     679                'month' => 14,
     680                'year'  => 2014,
     681            ),
     682            'month' => 14,
     683        );
     684        $this->assertFalse( $this->q->validate_date_values( $args ) );
     685    }
     686
     687    /**
     688     * @ticket 25834
     689     * @expectedIncorrectUsage WP_Date_Query
     690     */
     691    public function test_validate_date_values_week() {
     692        // Valid values.
     693        $weeks = range( 1, 53 );
     694        foreach ( $weeks as $week ) {
     695            $this->assertTrue( $this->q->validate_date_values( array( 'week' => $week ) ) );
     696        }
     697
     698        // Invalid values.
     699        $weeks = array( -1, 0, 54 );
     700        foreach ( $weeks as $week ) {
     701            $this->assertFalse( $this->q->validate_date_values( array( 'week' => $week ) ) );
     702        }
     703
     704        // Valid combinations.
     705        $weeks = array(
     706            array(
     707                'week' => 52,
     708                'year' => 2012,
     709            ),
     710            array(
     711                'week' => 53,
     712                'year' => 2009,
     713            ),
     714        );
     715
     716        foreach ( $weeks as $week_args ) {
     717            $this->assertTrue( $this->q->validate_date_values( $week_args ) );
     718        }
     719
     720        // Invalid combinations.
     721        $weeks = array(
     722            // 2012 has 52 weeks.
     723            array(
     724                'week' => 53,
     725                'year' => 2012,
     726            ),
     727
     728            // 2013 has 53 weeks.
     729            array(
     730                'week' => 54,
     731                'year' => 2009,
     732            )
     733        );
     734
     735        foreach ( $weeks as $week_args ) {
     736            $this->assertFalse( $this->q->validate_date_values( $week_args ) );
     737        }
     738    }
     739
     740    /**
     741     * @ticket 25834
     742     * @expectedIncorrectUsage WP_Date_Query
     743     */
     744    public function test_validate_date_values_month() {
     745        // Valid values.
     746        $months = range( 1, 12 );
     747        foreach ( $months as $month ) {
     748            $this->assertTrue( $this->q->validate_date_values( array( 'month' => $month ) ) );
     749        }
     750
     751        // Invalid values.
     752        $months = array( -1, 0, 13, 'string who wants to be a int' );
     753        foreach ( $months as $month ) {
     754            $this->assertFalse( $this->q->validate_date_values( array( 'month' => $month ) ) );
     755        }
     756    }
     757
     758    /**
     759     * @ticket 25834
     760     * @expectedIncorrectUsage WP_Date_Query
     761     */
     762    public function test_validate_date_values_day() {
     763        // Valid values.
     764        $days = range( 1, 31 );
     765        foreach ( $days as $day ) {
     766            $this->assertTrue( $this->q->validate_date_values( array( 'day' => $day ) ) );
     767        }
     768
     769        // Invalid values.
     770        $days = array( -1, 32 );
     771        foreach ( $days as $day ) {
     772            $this->assertFalse( $this->q->validate_date_values( array( 'day' => $day ) ) );
     773        }
     774
     775        // Valid combinations.
     776        $days = array(
     777            array(
     778                'day'   => 29,
     779                'month' => 2,
     780                'year'  => 2008,
     781            ),
     782            array(
     783                'day'   => 28,
     784                'month' => 2,
     785                'year'  => 2009,
     786            ),
     787        );
     788
     789        foreach ( $days as $args ) {
     790            $this->assertTrue( $this->q->validate_date_values( $args ) );
     791        }
     792
     793        // Invalid combinations.
     794        $days = array(
     795            // February 2008 has 29 days.
     796            array(
     797                'day'   => 30,
     798                'month' => 2,
     799                'year'  => 2008,
     800            ),
     801
     802            // February 2009 has 29 days.
     803            array(
     804                'day'   => 29,
     805                'month' => 2,
     806                'year'  => 2009,
     807            ),
     808        );
     809
     810        foreach ( $days as $args ) {
     811            $this->assertFalse( $this->q->validate_date_values( $args ) );
     812        }
     813    }
     814
     815    /**
     816     * @ticket 25834
     817     * @expectedIncorrectUsage WP_Date_Query
     818     */
     819    public function test_validate_date_values_hour() {
     820        // Valid values.
     821        $hours = range( 1, 23 );
     822        foreach ( $hours as $hour ) {
     823            $this->assertTrue( $this->q->validate_date_values( array( 'hour' => $hour ) ) );
     824        }
     825
     826        // Invalid values.
     827        $hours = array( -1, 24, 25, 'string who wants to be a int' );
     828        foreach ( $hours as $hour ) {
     829            $this->assertFalse( $this->q->validate_date_values( array( 'hour' => $hour ) ) );
     830        }
     831    }
     832
     833    /**
     834     * @ticket 25834
     835     * @expectedIncorrectUsage WP_Date_Query
     836     */
     837    public function test_validate_date_values_minute() {
     838        // Valid values.
     839        $minutes = range( 0, 59 );
     840        foreach ( $minutes as $minute ) {
     841            $this->assertTrue( $this->q->validate_date_values( array( 'minute' => $minute ) ) );
     842        }
     843
     844        // Invalid values.
     845        $minutes = array( -1, 60 );
     846        foreach ( $minutes as $minute ) {
     847            $this->assertFalse( $this->q->validate_date_values( array( 'minute' => $minute ) ) );
     848        }
     849    }
     850
     851    /**
     852     * @ticket 25834
     853     * @expectedIncorrectUsage WP_Date_Query
     854     */
     855    public function test_validate_date_values_second() {
     856        // Valid values.
     857        $seconds = range( 0, 59 );
     858        foreach ( $seconds as $second ) {
     859            $this->assertTrue( $this->q->validate_date_values( array( 'second' => $second ) ) );
     860        }
     861
     862        // Invalid values.
     863        $seconds = array( -1, 60 );
     864        foreach ( $seconds as $second ) {
     865            $this->assertFalse( $this->q->validate_date_values( array( 'second' => $second ) ) );
     866        }
     867
     868    }
     869
     870    /**
     871     * @ticket 25834
     872     * @expectedIncorrectUsage WP_Date_Query
     873     */
     874    public function test_validate_date_values_day_of_week() {
     875        // Valid values.
     876        $days_of_week = range( 1, 7 );
     877        foreach ( $days_of_week as $day_of_week ) {
     878            $this->assertTrue( $this->q->validate_date_values( array( 'dayofweek' => $day_of_week ) ) );
     879        }
     880
     881        // Invalid values.
     882        $days_of_week = array( -1, 0, 8 );
     883        foreach ( $days_of_week as $day_of_week ) {
     884            $this->assertFalse( $this->q->validate_date_values( array( 'dayofweek' => $day_of_week ) ) );
     885        }
     886    }
     887
     888    /**
     889     * @ticket 25834
     890     * @expectedIncorrectUsage WP_Date_Query
     891     */
     892    public function test_validate_date_values_day_of_year() {
     893        // Valid values.
     894        $days_of_year = range( 1, 366 );
     895        foreach ( $days_of_year as $day_of_year ) {
     896            $this->assertTrue( $this->q->validate_date_values( array( 'dayofyear' => $day_of_year ) ) );
     897        }
     898
     899        // Invalid values.
     900        $days_of_year = array( -1, 0, 367 );
     901        foreach ( $days_of_year as $day_of_year ) {
     902            $this->assertFalse( @$this->q->validate_date_values( array( 'dayofyear' => $day_of_year ) ) );
     903        }
     904    }
    585905}
Note: See TracChangeset for help on using the changeset viewer.