Make WordPress Core


Ignore:
Timestamp:
10/13/2014 12:17:12 AM (12 years ago)
Author:
boonebgorges
Message:

Don't create dummy data for each date query test.

The integration tests for using date_query in WP_Query require the creation of
dummy post data, much of which is common between a number of tests. However,
newer tests do not require as much data, so to reduce overhead we create the
data explicitly only when we need it, instead of in the setUp() method.

See #29781.

File:
1 edited

Legend:

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

    r29793 r29885  
    1616        public function setUp() {
    1717                parent::setUp();
    18 
     18                unset( $this->q );
     19                $this->q = new WP_Query();
     20        }
     21
     22        public function _get_query_result( $args = array() ) {
     23                $args = wp_parse_args( $args, array(
     24                        'post_status'            => 'any', // For the future post
     25                        'posts_per_page'         => '-1',  // To make sure results are accurate
     26                        'orderby'                => 'ID',  // Same order they were created
     27                        'order'                  => 'ASC',
     28                        'update_post_meta_cache' => false,
     29                        'update_post_term_cache' => false,
     30                ) );
     31
     32                return $this->q->query( $args );
     33        }
     34
     35        public function test_date_query_before_array() {
     36                $this->create_posts();
     37
     38                $posts = $this->_get_query_result( array(
     39                        'date_query' => array(
     40                                array(
     41                                        'before' => array(
     42                                                'year' => 2008,
     43                                                'month' => 6,
     44                                        ),
     45                                ),
     46                        ),
     47                ) );
     48
     49                $expected_dates = array(
     50                        '1972-05-24 14:53:45',
     51                        '1984-07-28 19:28:56',
     52                        '2003-05-27 22:45:07',
     53                        '2004-01-03 08:54:10',
     54                        '2004-05-22 12:34:12',
     55                        '2005-02-17 00:00:15',
     56                        '2005-12-31 23:59:20',
     57                        '2007-01-22 03:49:21',
     58                        '2007-05-16 17:32:22',
     59                        '2007-09-24 07:17:23',
     60                        '2008-03-29 09:04:25',
     61                );
     62
     63                $this->assertEquals( $expected_dates, wp_list_pluck( $posts, 'post_date' ) );
     64        }
     65
     66        /**
     67         * Specifically tests to make sure values are defaulting to
     68         * their minimum values when being used with "before".
     69         */
     70        public function test_date_query_before_array_test_defaulting() {
     71                $this->create_posts();
     72
     73                $posts = $this->_get_query_result( array(
     74                        'date_query' => array(
     75                                array(
     76                                        'before' => array(
     77                                                'year' => 2008,
     78                                        ),
     79                                ),
     80                        ),
     81                ) );
     82
     83                $expected_dates = array(
     84                        '1972-05-24 14:53:45',
     85                        '1984-07-28 19:28:56',
     86                        '2003-05-27 22:45:07',
     87                        '2004-01-03 08:54:10',
     88                        '2004-05-22 12:34:12',
     89                        '2005-02-17 00:00:15',
     90                        '2005-12-31 23:59:20',
     91                        '2007-01-22 03:49:21',
     92                        '2007-05-16 17:32:22',
     93                        '2007-09-24 07:17:23',
     94                );
     95
     96                $this->assertEquals( $expected_dates, wp_list_pluck( $posts, 'post_date' ) );
     97        }
     98
     99        public function test_date_query_before_string() {
     100                $this->create_posts();
     101
     102                $posts = $this->_get_query_result( array(
     103                        'date_query' => array(
     104                                array(
     105                                        'before' => 'May 4th, 2008',
     106                                ),
     107                        ),
     108                ) );
     109
     110                $expected_dates = array(
     111                        '1972-05-24 14:53:45',
     112                        '1984-07-28 19:28:56',
     113                        '2003-05-27 22:45:07',
     114                        '2004-01-03 08:54:10',
     115                        '2004-05-22 12:34:12',
     116                        '2005-02-17 00:00:15',
     117                        '2005-12-31 23:59:20',
     118                        '2007-01-22 03:49:21',
     119                        '2007-05-16 17:32:22',
     120                        '2007-09-24 07:17:23',
     121                        '2008-03-29 09:04:25',
     122                );
     123
     124                $this->assertEquals( $expected_dates, wp_list_pluck( $posts, 'post_date' ) );
     125        }
     126
     127        public function test_date_query_after_array() {
     128                $this->create_posts();
     129
     130                $posts = $this->_get_query_result( array(
     131                        'date_query' => array(
     132                                array(
     133                                        'after' => array(
     134                                                'year'  => 2009,
     135                                                'month' => 12,
     136                                                'day'   => 31,
     137                                        ),
     138                                ),
     139                        ),
     140                ) );
     141
     142                $expected_dates = array(
     143                        '2010-06-17 17:09:30',
     144                        '2011-02-23 12:12:31',
     145                        '2011-07-04 01:56:32',
     146                        '2011-12-12 16:39:33',
     147                        '2012-06-13 14:03:34',
     148                        '2025-04-20 10:13:00',
     149                        '2025-04-20 10:13:01',
     150                        '2025-05-20 10:13:01',
     151                );
     152
     153                $this->assertEquals( $expected_dates, wp_list_pluck( $posts, 'post_date' ) );
     154        }
     155
     156        /**
     157         * Specifically tests to make sure values are defaulting to
     158         * their maximum values when being used with "after".
     159         */
     160        public function test_date_query_after_array_test_defaulting() {
     161                $this->create_posts();
     162
     163                $posts = $this->_get_query_result( array(
     164                        'date_query' => array(
     165                                array(
     166                                        'after' => array(
     167                                                'year' => 2008,
     168                                        ),
     169                                ),
     170                        ),
     171                ) );
     172
     173                $expected_dates = array(
     174                        '2009-06-11 21:30:28',
     175                        '2009-12-18 10:42:29',
     176                        '2010-06-17 17:09:30',
     177                        '2011-02-23 12:12:31',
     178                        '2011-07-04 01:56:32',
     179                        '2011-12-12 16:39:33',
     180                        '2012-06-13 14:03:34',
     181                        '2025-04-20 10:13:00',
     182                        '2025-04-20 10:13:01',
     183                        '2025-05-20 10:13:01',
     184                );
     185
     186                $this->assertEquals( $expected_dates, wp_list_pluck( $posts, 'post_date' ) );
     187        }
     188
     189        public function test_date_query_after_string() {
     190                $this->create_posts();
     191
     192                $posts = $this->_get_query_result( array(
     193                        'date_query' => array(
     194                                array(
     195                                        'after' => '2009-12-18 10:42:29',
     196                                ),
     197                        ),
     198                ) );
     199
     200                $expected_dates = array(
     201                        '2010-06-17 17:09:30',
     202                        '2011-02-23 12:12:31',
     203                        '2011-07-04 01:56:32',
     204                        '2011-12-12 16:39:33',
     205                        '2012-06-13 14:03:34',
     206                        '2025-04-20 10:13:00',
     207                        '2025-04-20 10:13:01',
     208                        '2025-05-20 10:13:01',
     209                );
     210
     211                $this->assertEquals( $expected_dates, wp_list_pluck( $posts, 'post_date' ) );
     212        }
     213
     214        public function test_date_query_after_string_inclusive() {
     215                $this->create_posts();
     216
     217                $posts = $this->_get_query_result( array(
     218                        'date_query' => array(
     219                                array(
     220                                        'after'     => '2009-12-18 10:42:29',
     221                                        'inclusive' => true,
     222                                ),
     223                        ),
     224                ) );
     225
     226                $expected_dates = array(
     227                        '2009-12-18 10:42:29',
     228                        '2010-06-17 17:09:30',
     229                        '2011-02-23 12:12:31',
     230                        '2011-07-04 01:56:32',
     231                        '2011-12-12 16:39:33',
     232                        '2012-06-13 14:03:34',
     233                        '2025-04-20 10:13:00',
     234                        '2025-04-20 10:13:01',
     235                        '2025-05-20 10:13:01',
     236                );
     237
     238                $this->assertEquals( $expected_dates, wp_list_pluck( $posts, 'post_date' ) );
     239        }
     240
     241        /**
     242         * @ticket 26653
     243         */
     244        public function test_date_query_inclusive_between_dates() {
     245                $this->create_posts();
     246
     247                $posts = $this->_get_query_result( array(
     248                        'date_query' => array(
     249                                'after' => array(
     250                                        'year' => 2007,
     251                                        'month' => 1
     252                                ),
     253                                'before' => array(
     254                                        'year' => 2008,
     255                                        'month' => 12
     256                                ),
     257                                'inclusive' => true
     258                        ),
     259                ) );
     260
     261
     262                $expected_dates = array(
     263                        '2007-01-22 03:49:21',
     264                        '2007-05-16 17:32:22',
     265                        '2007-09-24 07:17:23',
     266                        '2008-03-29 09:04:25',
     267                        '2008-07-15 11:32:26',
     268                        '2008-12-10 13:06:27',
     269                );
     270
     271                $this->assertEquals( $expected_dates, wp_list_pluck( $posts, 'post_date' ) );
     272        }
     273
     274        public function test_date_query_year_expecting_results() {
     275                $this->create_posts();
     276
     277                $posts = $this->_get_query_result( array(
     278                        'date_query' => array(
     279                                array(
     280                                        'year' => 2009,
     281                                ),
     282                        ),
     283                ) );
     284
     285                $expected_dates = array(
     286                        '2009-06-11 21:30:28',
     287                        '2009-12-18 10:42:29',
     288                );
     289
     290                $this->assertEquals( $expected_dates, wp_list_pluck( $posts, 'post_date' ) );
     291        }
     292
     293        public function test_date_query_year_expecting_noresults() {
     294                $this->create_posts();
     295
     296                $posts = $this->_get_query_result( array(
     297                        'date_query' => array(
     298                                array(
     299                                        'year' => 2001,
     300                                ),
     301                        ),
     302                ) );
     303
     304                $this->assertCount( 0, $posts );
     305        }
     306
     307        public function test_date_query_month_expecting_results() {
     308                $this->create_posts();
     309
     310                $posts = $this->_get_query_result( array(
     311                        'date_query' => array(
     312                                array(
     313                                        'month' => 12,
     314                                ),
     315                        ),
     316                ) );
     317
     318                $expected_dates = array(
     319                        '2005-12-31 23:59:20',
     320                        '2008-12-10 13:06:27',
     321                        '2009-12-18 10:42:29',
     322                        '2011-12-12 16:39:33',
     323                );
     324
     325                $this->assertEquals( $expected_dates, wp_list_pluck( $posts, 'post_date' ) );
     326        }
     327
     328        public function test_date_query_month_expecting_noresults() {
     329                $this->create_posts();
     330
     331                $posts = $this->_get_query_result( array(
     332                        'date_query' => array(
     333                                array(
     334                                        'month' => 8,
     335                                ),
     336                        ),
     337                ) );
     338
     339                $this->assertCount( 0, $posts );
     340        }
     341
     342        public function test_date_query_week_expecting_results() {
     343                $this->create_posts();
     344
     345                $posts = $this->_get_query_result( array(
     346                        'date_query' => array(
     347                                array(
     348                                        'week' => 1,
     349                                ),
     350                        ),
     351                ) );
     352
     353                $expected_dates = array(
     354                        '2004-01-03 08:54:10',
     355                );
     356
     357                $this->assertEquals( $expected_dates, wp_list_pluck( $posts, 'post_date' ) );
     358        }
     359
     360        public function test_date_query_week_expecting_noresults() {
     361                $this->create_posts();
     362
     363                $posts = $this->_get_query_result( array(
     364                        'date_query' => array(
     365                                array(
     366                                        'week' => 10,
     367                                ),
     368                        ),
     369                ) );
     370
     371                $this->assertCount( 0, $posts );
     372        }
     373
     374        public function test_date_query_day_expecting_results() {
     375                $this->create_posts();
     376
     377                $posts = $this->_get_query_result( array(
     378                        'date_query' => array(
     379                                array(
     380                                        'day' => 17,
     381                                ),
     382                        ),
     383                ) );
     384
     385                $expected_dates = array(
     386                        '2005-02-17 00:00:15',
     387                        '2010-06-17 17:09:30',
     388                );
     389
     390                $this->assertEquals( $expected_dates, wp_list_pluck( $posts, 'post_date' ) );
     391        }
     392
     393        public function test_date_query_day_expecting_noresults() {
     394                $this->create_posts();
     395
     396                $posts = $this->_get_query_result( array(
     397                        'date_query' => array(
     398                                array(
     399                                        'day' => 19,
     400                                ),
     401                        ),
     402                ) );
     403
     404                $this->assertCount( 0, $posts );
     405        }
     406
     407        public function test_date_query_dayofweek_expecting_results() {
     408                $this->create_posts();
     409
     410                $posts = $this->_get_query_result( array(
     411                        'date_query' => array(
     412                                array(
     413                                        'dayofweek' => 7,
     414                                ),
     415                        ),
     416                ) );
     417
     418                $expected_dates = array(
     419                        '1984-07-28 19:28:56',
     420                        '2004-01-03 08:54:10',
     421                        '2004-05-22 12:34:12',
     422                        '2005-12-31 23:59:20',
     423                        '2008-03-29 09:04:25',
     424                );
     425
     426                $this->assertEquals( $expected_dates, wp_list_pluck( $posts, 'post_date' ) );
     427        }
     428
     429        public function test_date_query_hour_expecting_results() {
     430                $this->create_posts();
     431
     432                $posts = $this->_get_query_result( array(
     433                        'date_query' => array(
     434                                array(
     435                                        'hour' => 13,
     436                                ),
     437                        ),
     438                ) );
     439
     440                $expected_dates = array(
     441                        '2008-12-10 13:06:27',
     442                );
     443
     444                $this->assertEquals( $expected_dates, wp_list_pluck( $posts, 'post_date' ) );
     445        }
     446
     447        public function test_date_query_hour_expecting_noresults() {
     448                $this->create_posts();
     449
     450                $posts = $this->_get_query_result( array(
     451                        'date_query' => array(
     452                                array(
     453                                        'hour' => 2,
     454                                ),
     455                        ),
     456                ) );
     457
     458                $this->assertCount( 0, $posts );
     459        }
     460
     461        public function test_date_query_minute_expecting_results() {
     462                $this->create_posts();
     463
     464                $posts = $this->_get_query_result( array(
     465                        'date_query' => array(
     466                                array(
     467                                        'minute' => 56,
     468                                ),
     469                        ),
     470                ) );
     471
     472                $expected_dates = array(
     473                        '2011-07-04 01:56:32',
     474                );
     475
     476                $this->assertEquals( $expected_dates, wp_list_pluck( $posts, 'post_date' ) );
     477        }
     478
     479        public function test_date_query_minute_expecting_noresults() {
     480                $this->create_posts();
     481
     482                $posts = $this->_get_query_result( array(
     483                        'date_query' => array(
     484                                array(
     485                                        'minute' => 2,
     486                                ),
     487                        ),
     488                ) );
     489
     490                $this->assertCount( 0, $posts );
     491        }
     492
     493        public function test_date_query_second_expecting_results() {
     494                $this->create_posts();
     495
     496                $posts = $this->_get_query_result( array(
     497                        'date_query' => array(
     498                                array(
     499                                        'second' => 21,
     500                                ),
     501                        ),
     502                ) );
     503
     504                $expected_dates = array(
     505                        '2007-01-22 03:49:21',
     506                );
     507
     508                $this->assertEquals( $expected_dates, wp_list_pluck( $posts, 'post_date' ) );
     509        }
     510
     511        public function test_date_query_second_expecting_noresults() {
     512                $this->create_posts();
     513
     514                $posts = $this->_get_query_result( array(
     515                        'date_query' => array(
     516                                array(
     517                                        'second' => 2,
     518                                ),
     519                        ),
     520                ) );
     521
     522                $this->assertCount( 0, $posts );
     523        }
     524
     525        public function test_date_query_between_two_times() {
     526                $this->create_posts();
     527
     528                $posts = $this->_get_query_result( array(
     529                        'date_query' => array(
     530                                array(
     531                                        'hour'    => 9,
     532                                        'minute'  => 0,
     533                                        'compare' => '>=',
     534                                ),
     535                                array(
     536                                        'hour'    => '17',
     537                                        'minute'  => '0',
     538                                        'compare' => '<=',
     539                                ),
     540                        ),
     541                ) );
     542
     543                $expected_dates = array(
     544                        '1972-05-24 14:53:45',
     545                        '2004-05-22 12:34:12',
     546                        '2008-03-29 09:04:25',
     547                        '2008-07-15 11:32:26',
     548                        '2008-12-10 13:06:27',
     549                        '2009-12-18 10:42:29',
     550                        '2011-02-23 12:12:31',
     551                        '2011-12-12 16:39:33',
     552                        '2012-06-13 14:03:34',
     553                        '2025-04-20 10:13:00',
     554                        '2025-04-20 10:13:01',
     555                        '2025-05-20 10:13:01',
     556                );
     557
     558                $this->assertEquals( $expected_dates, wp_list_pluck( $posts, 'post_date' ) );
     559        }
     560
     561        public function test_date_query_relation_or() {
     562                $this->create_posts();
     563
     564                $posts = $this->_get_query_result( array(
     565                        'date_query' => array(
     566                                array(
     567                                        'hour' => 14,
     568                                ),
     569                                array(
     570                                        'minute' => 34,
     571                                ),
     572                                'relation' => 'OR',
     573                        ),
     574                ) );
     575
     576                $expected_dates = array(
     577                        '1972-05-24 14:53:45',
     578                        '2004-05-22 12:34:12',
     579                        '2012-06-13 14:03:34',
     580                );
     581
     582                $this->assertEquals( $expected_dates, wp_list_pluck( $posts, 'post_date' ) );
     583        }
     584
     585        public function test_date_query_compare_greater_than_or_equal_to() {
     586                $this->create_posts();
     587
     588                $posts = $this->_get_query_result( array(
     589                        'date_query' => array(
     590                                array(
     591                                        'hour' => 14,
     592                                        'minute' => 34,
     593                                ),
     594                                'compare' => '>=',
     595                        ),
     596                ) );
     597
     598                $expected_dates = array(
     599                        '1972-05-24 14:53:45',
     600                        '1984-07-28 19:28:56',
     601                        '2003-05-27 22:45:07',
     602                        '2005-12-31 23:59:20',
     603                        '2007-05-16 17:32:22',
     604                        '2009-06-11 21:30:28',
     605                        '2010-06-17 17:09:30',
     606                        '2011-12-12 16:39:33',
     607                );
     608
     609                $this->assertEquals( $expected_dates, wp_list_pluck( $posts, 'post_date' ) );
     610        }
     611
     612        public function test_date_params_monthnum_m_duplicate() {
     613                $this->create_posts();
     614
     615                $posts = $this->_get_query_result( array(
     616                        'date_query' => array(
     617                                'month' => 5,
     618                                'monthnum' => 9
     619                        ),
     620                ) );
     621
     622                $expected_dates = array(
     623                        '1972-05-24 14:53:45',
     624                        '2003-05-27 22:45:07',
     625                        '2004-05-22 12:34:12',
     626                        '2007-05-16 17:32:22',
     627                        '2025-05-20 10:13:01',
     628                );
     629
     630                $this->assertEquals( $expected_dates, wp_list_pluck( $posts, 'post_date' ) );
     631
     632                $this->assertContains( "AND ( ( MONTH( post_date ) = 5 ) ) AND", $this->q->request );
     633
     634                $this->assertNotContains( "AND ( ( MONTH( post_date ) = 5 AND MONTH( post_date ) = 9 ) ) AND", $this->q->request );
     635        }
     636
     637        public function test_date_params_week_w_duplicate() {
     638                $this->create_posts();
     639
     640                $posts = $this->_get_query_result( array(
     641                        'date_query' => array(
     642                                'week' => 21,
     643                                'w' => 22
     644                        ),
     645                ) );
     646
     647                $expected_dates = array(
     648                        '1972-05-24 14:53:45',
     649                        '2004-05-22 12:34:12',
     650                        '2025-05-20 10:13:01',
     651                );
     652
     653
     654                $this->assertEquals( $expected_dates, wp_list_pluck( $posts, 'post_date' ) );
     655
     656                $this->assertContains( "AND ( ( WEEK( post_date, 1 ) = 21 ) ) AND", $this->q->request );
     657
     658                $this->assertNotContains( "AND ( ( WEEK( post_date, 1 ) = 21 AND WEEK( post_date, 1 ) = 22 ) ) AND", $this->q->request );
     659        }
     660
     661        /** Helpers **********************************************************/
     662
     663        protected function create_posts() {
    19664                // Be careful modifying this. Tests are coded to expect this exact sample data.
    20665                $post_dates = array(
     
    47692                        $this->factory->post->create( array( 'post_date' => $post_date ) );
    48693                }
    49 
    50                 unset( $this->q );
    51                 $this->q = new WP_Query();
    52         }
    53 
    54         public function _get_query_result( $args = array() ) {
    55                 $args = wp_parse_args( $args, array(
    56                         'post_status'            => 'any', // For the future post
    57                         'posts_per_page'         => '-1',  // To make sure results are accurate
    58                         'orderby'                => 'ID',  // Same order they were created
    59                         'order'                  => 'ASC',
    60                         'update_post_meta_cache' => false,
    61                         'update_post_term_cache' => false,
    62                 ) );
    63 
    64                 return $this->q->query( $args );
    65         }
    66 
    67         public function test_date_query_before_array() {
    68                 $posts = $this->_get_query_result( array(
    69                         'date_query' => array(
    70                                 array(
    71                                         'before' => array(
    72                                                 'year' => 2008,
    73                                                 'month' => 6,
    74                                         ),
    75                                 ),
    76                         ),
    77                 ) );
    78 
    79                 $expected_dates = array(
    80                         '1972-05-24 14:53:45',
    81                         '1984-07-28 19:28:56',
    82                         '2003-05-27 22:45:07',
    83                         '2004-01-03 08:54:10',
    84                         '2004-05-22 12:34:12',
    85                         '2005-02-17 00:00:15',
    86                         '2005-12-31 23:59:20',
    87                         '2007-01-22 03:49:21',
    88                         '2007-05-16 17:32:22',
    89                         '2007-09-24 07:17:23',
    90                         '2008-03-29 09:04:25',
    91                 );
    92 
    93                 $this->assertEquals( $expected_dates, wp_list_pluck( $posts, 'post_date' ) );
    94         }
    95 
    96         /**
    97          * Specifically tests to make sure values are defaulting to
    98          * their minimum values when being used with "before".
    99          */
    100         public function test_date_query_before_array_test_defaulting() {
    101                 $posts = $this->_get_query_result( array(
    102                         'date_query' => array(
    103                                 array(
    104                                         'before' => array(
    105                                                 'year' => 2008,
    106                                         ),
    107                                 ),
    108                         ),
    109                 ) );
    110 
    111                 $expected_dates = array(
    112                         '1972-05-24 14:53:45',
    113                         '1984-07-28 19:28:56',
    114                         '2003-05-27 22:45:07',
    115                         '2004-01-03 08:54:10',
    116                         '2004-05-22 12:34:12',
    117                         '2005-02-17 00:00:15',
    118                         '2005-12-31 23:59:20',
    119                         '2007-01-22 03:49:21',
    120                         '2007-05-16 17:32:22',
    121                         '2007-09-24 07:17:23',
    122                 );
    123 
    124                 $this->assertEquals( $expected_dates, wp_list_pluck( $posts, 'post_date' ) );
    125         }
    126 
    127         public function test_date_query_before_string() {
    128                 $posts = $this->_get_query_result( array(
    129                         'date_query' => array(
    130                                 array(
    131                                         'before' => 'May 4th, 2008',
    132                                 ),
    133                         ),
    134                 ) );
    135 
    136                 $expected_dates = array(
    137                         '1972-05-24 14:53:45',
    138                         '1984-07-28 19:28:56',
    139                         '2003-05-27 22:45:07',
    140                         '2004-01-03 08:54:10',
    141                         '2004-05-22 12:34:12',
    142                         '2005-02-17 00:00:15',
    143                         '2005-12-31 23:59:20',
    144                         '2007-01-22 03:49:21',
    145                         '2007-05-16 17:32:22',
    146                         '2007-09-24 07:17:23',
    147                         '2008-03-29 09:04:25',
    148                 );
    149 
    150                 $this->assertEquals( $expected_dates, wp_list_pluck( $posts, 'post_date' ) );
    151         }
    152 
    153         public function test_date_query_after_array() {
    154                 $posts = $this->_get_query_result( array(
    155                         'date_query' => array(
    156                                 array(
    157                                         'after' => array(
    158                                                 'year'  => 2009,
    159                                                 'month' => 12,
    160                                                 'day'   => 31,
    161                                         ),
    162                                 ),
    163                         ),
    164                 ) );
    165 
    166                 $expected_dates = array(
    167                         '2010-06-17 17:09:30',
    168                         '2011-02-23 12:12:31',
    169                         '2011-07-04 01:56:32',
    170                         '2011-12-12 16:39:33',
    171                         '2012-06-13 14:03:34',
    172                         '2025-04-20 10:13:00',
    173                         '2025-04-20 10:13:01',
    174                         '2025-05-20 10:13:01',
    175                 );
    176 
    177                 $this->assertEquals( $expected_dates, wp_list_pluck( $posts, 'post_date' ) );
    178         }
    179 
    180         /**
    181          * Specifically tests to make sure values are defaulting to
    182          * their maximum values when being used with "after".
    183          */
    184         public function test_date_query_after_array_test_defaulting() {
    185                 $posts = $this->_get_query_result( array(
    186                         'date_query' => array(
    187                                 array(
    188                                         'after' => array(
    189                                                 'year' => 2008,
    190                                         ),
    191                                 ),
    192                         ),
    193                 ) );
    194 
    195                 $expected_dates = array(
    196                         '2009-06-11 21:30:28',
    197                         '2009-12-18 10:42:29',
    198                         '2010-06-17 17:09:30',
    199                         '2011-02-23 12:12:31',
    200                         '2011-07-04 01:56:32',
    201                         '2011-12-12 16:39:33',
    202                         '2012-06-13 14:03:34',
    203                         '2025-04-20 10:13:00',
    204                         '2025-04-20 10:13:01',
    205                         '2025-05-20 10:13:01',
    206                 );
    207 
    208                 $this->assertEquals( $expected_dates, wp_list_pluck( $posts, 'post_date' ) );
    209         }
    210 
    211         public function test_date_query_after_string() {
    212                 $posts = $this->_get_query_result( array(
    213                         'date_query' => array(
    214                                 array(
    215                                         'after' => '2009-12-18 10:42:29',
    216                                 ),
    217                         ),
    218                 ) );
    219 
    220                 $expected_dates = array(
    221                         '2010-06-17 17:09:30',
    222                         '2011-02-23 12:12:31',
    223                         '2011-07-04 01:56:32',
    224                         '2011-12-12 16:39:33',
    225                         '2012-06-13 14:03:34',
    226                         '2025-04-20 10:13:00',
    227                         '2025-04-20 10:13:01',
    228                         '2025-05-20 10:13:01',
    229                 );
    230 
    231                 $this->assertEquals( $expected_dates, wp_list_pluck( $posts, 'post_date' ) );
    232         }
    233 
    234         public function test_date_query_after_string_inclusive() {
    235                 $posts = $this->_get_query_result( array(
    236                         'date_query' => array(
    237                                 array(
    238                                         'after'     => '2009-12-18 10:42:29',
    239                                         'inclusive' => true,
    240                                 ),
    241                         ),
    242                 ) );
    243 
    244                 $expected_dates = array(
    245                         '2009-12-18 10:42:29',
    246                         '2010-06-17 17:09:30',
    247                         '2011-02-23 12:12:31',
    248                         '2011-07-04 01:56:32',
    249                         '2011-12-12 16:39:33',
    250                         '2012-06-13 14:03:34',
    251                         '2025-04-20 10:13:00',
    252                         '2025-04-20 10:13:01',
    253                         '2025-05-20 10:13:01',
    254                 );
    255 
    256                 $this->assertEquals( $expected_dates, wp_list_pluck( $posts, 'post_date' ) );
    257         }
    258 
    259         /**
    260          * @ticket 26653
    261          */
    262         public function test_date_query_inclusive_between_dates() {
    263                 $posts = $this->_get_query_result( array(
    264                         'date_query' => array(
    265                                 'after' => array(
    266                                         'year' => 2007,
    267                                         'month' => 1
    268                                 ),
    269                                 'before' => array(
    270                                         'year' => 2008,
    271                                         'month' => 12
    272                                 ),
    273                                 'inclusive' => true
    274                         ),
    275                 ) );
    276 
    277 
    278                 $expected_dates = array(
    279                         '2007-01-22 03:49:21',
    280                         '2007-05-16 17:32:22',
    281                         '2007-09-24 07:17:23',
    282                         '2008-03-29 09:04:25',
    283                         '2008-07-15 11:32:26',
    284                         '2008-12-10 13:06:27',
    285                 );
    286 
    287                 $this->assertEquals( $expected_dates, wp_list_pluck( $posts, 'post_date' ) );
    288         }
    289 
    290         public function test_date_query_year_expecting_results() {
    291                 $posts = $this->_get_query_result( array(
    292                         'date_query' => array(
    293                                 array(
    294                                         'year' => 2009,
    295                                 ),
    296                         ),
    297                 ) );
    298 
    299                 $expected_dates = array(
    300                         '2009-06-11 21:30:28',
    301                         '2009-12-18 10:42:29',
    302                 );
    303 
    304                 $this->assertEquals( $expected_dates, wp_list_pluck( $posts, 'post_date' ) );
    305         }
    306 
    307         public function test_date_query_year_expecting_noresults() {
    308                 $posts = $this->_get_query_result( array(
    309                         'date_query' => array(
    310                                 array(
    311                                         'year' => 2001,
    312                                 ),
    313                         ),
    314                 ) );
    315 
    316                 $this->assertCount( 0, $posts );
    317         }
    318 
    319         public function test_date_query_month_expecting_results() {
    320                 $posts = $this->_get_query_result( array(
    321                         'date_query' => array(
    322                                 array(
    323                                         'month' => 12,
    324                                 ),
    325                         ),
    326                 ) );
    327 
    328                 $expected_dates = array(
    329                         '2005-12-31 23:59:20',
    330                         '2008-12-10 13:06:27',
    331                         '2009-12-18 10:42:29',
    332                         '2011-12-12 16:39:33',
    333                 );
    334 
    335                 $this->assertEquals( $expected_dates, wp_list_pluck( $posts, 'post_date' ) );
    336         }
    337 
    338         public function test_date_query_month_expecting_noresults() {
    339                 $posts = $this->_get_query_result( array(
    340                         'date_query' => array(
    341                                 array(
    342                                         'month' => 8,
    343                                 ),
    344                         ),
    345                 ) );
    346 
    347                 $this->assertCount( 0, $posts );
    348         }
    349 
    350         public function test_date_query_week_expecting_results() {
    351                 $posts = $this->_get_query_result( array(
    352                         'date_query' => array(
    353                                 array(
    354                                         'week' => 1,
    355                                 ),
    356                         ),
    357                 ) );
    358 
    359                 $expected_dates = array(
    360                         '2004-01-03 08:54:10',
    361                 );
    362 
    363                 $this->assertEquals( $expected_dates, wp_list_pluck( $posts, 'post_date' ) );
    364         }
    365 
    366         public function test_date_query_week_expecting_noresults() {
    367                 $posts = $this->_get_query_result( array(
    368                         'date_query' => array(
    369                                 array(
    370                                         'week' => 10,
    371                                 ),
    372                         ),
    373                 ) );
    374 
    375                 $this->assertCount( 0, $posts );
    376         }
    377 
    378         public function test_date_query_day_expecting_results() {
    379                 $posts = $this->_get_query_result( array(
    380                         'date_query' => array(
    381                                 array(
    382                                         'day' => 17,
    383                                 ),
    384                         ),
    385                 ) );
    386 
    387                 $expected_dates = array(
    388                         '2005-02-17 00:00:15',
    389                         '2010-06-17 17:09:30',
    390                 );
    391 
    392                 $this->assertEquals( $expected_dates, wp_list_pluck( $posts, 'post_date' ) );
    393         }
    394 
    395         public function test_date_query_day_expecting_noresults() {
    396                 $posts = $this->_get_query_result( array(
    397                         'date_query' => array(
    398                                 array(
    399                                         'day' => 19,
    400                                 ),
    401                         ),
    402                 ) );
    403 
    404                 $this->assertCount( 0, $posts );
    405         }
    406 
    407         public function test_date_query_dayofweek_expecting_results() {
    408                 $posts = $this->_get_query_result( array(
    409                         'date_query' => array(
    410                                 array(
    411                                         'dayofweek' => 7,
    412                                 ),
    413                         ),
    414                 ) );
    415 
    416                 $expected_dates = array(
    417                         '1984-07-28 19:28:56',
    418                         '2004-01-03 08:54:10',
    419                         '2004-05-22 12:34:12',
    420                         '2005-12-31 23:59:20',
    421                         '2008-03-29 09:04:25',
    422                 );
    423 
    424                 $this->assertEquals( $expected_dates, wp_list_pluck( $posts, 'post_date' ) );
    425         }
    426 
    427         public function test_date_query_hour_expecting_results() {
    428                 $posts = $this->_get_query_result( array(
    429                         'date_query' => array(
    430                                 array(
    431                                         'hour' => 13,
    432                                 ),
    433                         ),
    434                 ) );
    435 
    436                 $expected_dates = array(
    437                         '2008-12-10 13:06:27',
    438                 );
    439 
    440                 $this->assertEquals( $expected_dates, wp_list_pluck( $posts, 'post_date' ) );
    441         }
    442 
    443         public function test_date_query_hour_expecting_noresults() {
    444                 $posts = $this->_get_query_result( array(
    445                         'date_query' => array(
    446                                 array(
    447                                         'hour' => 2,
    448                                 ),
    449                         ),
    450                 ) );
    451 
    452                 $this->assertCount( 0, $posts );
    453         }
    454 
    455         public function test_date_query_minute_expecting_results() {
    456                 $posts = $this->_get_query_result( array(
    457                         'date_query' => array(
    458                                 array(
    459                                         'minute' => 56,
    460                                 ),
    461                         ),
    462                 ) );
    463 
    464                 $expected_dates = array(
    465                         '2011-07-04 01:56:32',
    466                 );
    467 
    468                 $this->assertEquals( $expected_dates, wp_list_pluck( $posts, 'post_date' ) );
    469         }
    470 
    471         public function test_date_query_minute_expecting_noresults() {
    472                 $posts = $this->_get_query_result( array(
    473                         'date_query' => array(
    474                                 array(
    475                                         'minute' => 2,
    476                                 ),
    477                         ),
    478                 ) );
    479 
    480                 $this->assertCount( 0, $posts );
    481         }
    482 
    483         public function test_date_query_second_expecting_results() {
    484                 $posts = $this->_get_query_result( array(
    485                         'date_query' => array(
    486                                 array(
    487                                         'second' => 21,
    488                                 ),
    489                         ),
    490                 ) );
    491 
    492                 $expected_dates = array(
    493                         '2007-01-22 03:49:21',
    494                 );
    495 
    496                 $this->assertEquals( $expected_dates, wp_list_pluck( $posts, 'post_date' ) );
    497         }
    498 
    499         public function test_date_query_second_expecting_noresults() {
    500                 $posts = $this->_get_query_result( array(
    501                         'date_query' => array(
    502                                 array(
    503                                         'second' => 2,
    504                                 ),
    505                         ),
    506                 ) );
    507 
    508                 $this->assertCount( 0, $posts );
    509         }
    510 
    511         public function test_date_query_between_two_times() {
    512                 $posts = $this->_get_query_result( array(
    513                         'date_query' => array(
    514                                 array(
    515                                         'hour'    => 9,
    516                                         'minute'  => 0,
    517                                         'compare' => '>=',
    518                                 ),
    519                                 array(
    520                                         'hour'    => '17',
    521                                         'minute'  => '0',
    522                                         'compare' => '<=',
    523                                 ),
    524                         ),
    525                 ) );
    526 
    527                 $expected_dates = array(
    528                         '1972-05-24 14:53:45',
    529                         '2004-05-22 12:34:12',
    530                         '2008-03-29 09:04:25',
    531                         '2008-07-15 11:32:26',
    532                         '2008-12-10 13:06:27',
    533                         '2009-12-18 10:42:29',
    534                         '2011-02-23 12:12:31',
    535                         '2011-12-12 16:39:33',
    536                         '2012-06-13 14:03:34',
    537                         '2025-04-20 10:13:00',
    538                         '2025-04-20 10:13:01',
    539                         '2025-05-20 10:13:01',
    540                 );
    541 
    542                 $this->assertEquals( $expected_dates, wp_list_pluck( $posts, 'post_date' ) );
    543         }
    544 
    545         public function test_date_query_relation_or() {
    546                 $posts = $this->_get_query_result( array(
    547                         'date_query' => array(
    548                                 array(
    549                                         'hour' => 14,
    550                                 ),
    551                                 array(
    552                                         'minute' => 34,
    553                                 ),
    554                                 'relation' => 'OR',
    555                         ),
    556                 ) );
    557 
    558                 $expected_dates = array(
    559                         '1972-05-24 14:53:45',
    560                         '2004-05-22 12:34:12',
    561                         '2012-06-13 14:03:34',
    562                 );
    563 
    564                 $this->assertEquals( $expected_dates, wp_list_pluck( $posts, 'post_date' ) );
    565         }
    566 
    567         public function test_date_query_compare_greater_than_or_equal_to() {
    568                 $posts = $this->_get_query_result( array(
    569                         'date_query' => array(
    570                                 array(
    571                                         'hour' => 14,
    572                                         'minute' => 34,
    573                                 ),
    574                                 'compare' => '>=',
    575                         ),
    576                 ) );
    577 
    578                 $expected_dates = array(
    579                         '1972-05-24 14:53:45',
    580                         '1984-07-28 19:28:56',
    581                         '2003-05-27 22:45:07',
    582                         '2005-12-31 23:59:20',
    583                         '2007-05-16 17:32:22',
    584                         '2009-06-11 21:30:28',
    585                         '2010-06-17 17:09:30',
    586                         '2011-12-12 16:39:33',
    587                 );
    588 
    589                 $this->assertEquals( $expected_dates, wp_list_pluck( $posts, 'post_date' ) );
    590         }
    591 
    592         public function test_date_params_monthnum_m_duplicate() {
    593                 $posts = $this->_get_query_result( array(
    594                         'date_query' => array(
    595                                 'month' => 5,
    596                                 'monthnum' => 9
    597                         ),
    598                 ) );
    599 
    600                 $expected_dates = array(
    601                         '1972-05-24 14:53:45',
    602                         '2003-05-27 22:45:07',
    603                         '2004-05-22 12:34:12',
    604                         '2007-05-16 17:32:22',
    605                         '2025-05-20 10:13:01',
    606                 );
    607 
    608                 $this->assertEquals( $expected_dates, wp_list_pluck( $posts, 'post_date' ) );
    609 
    610                 $this->assertContains( "AND ( ( MONTH( post_date ) = 5 ) ) AND", $this->q->request );
    611 
    612                 $this->assertNotContains( "AND ( ( MONTH( post_date ) = 5 AND MONTH( post_date ) = 9 ) ) AND", $this->q->request );
    613         }
    614 
    615         public function test_date_params_week_w_duplicate() {
    616                 $posts = $this->_get_query_result( array(
    617                         'date_query' => array(
    618                                 'week' => 21,
    619                                 'w' => 22
    620                         ),
    621                 ) );
    622 
    623                 $expected_dates = array(
    624                         '1972-05-24 14:53:45',
    625                         '2004-05-22 12:34:12',
    626                         '2025-05-20 10:13:01',
    627                 );
    628 
    629 
    630                 $this->assertEquals( $expected_dates, wp_list_pluck( $posts, 'post_date' ) );
    631 
    632                 $this->assertContains( "AND ( ( WEEK( post_date, 1 ) = 21 ) ) AND", $this->q->request );
    633 
    634                 $this->assertNotContains( "AND ( ( WEEK( post_date, 1 ) = 21 AND WEEK( post_date, 1 ) = 22 ) ) AND", $this->q->request );
    635694        }
    636695}
Note: See TracChangeset for help on using the changeset viewer.