| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * Tests to make sure querying posts based on various date parameters works as expected. |
| 5 | * |
| 6 | * @ticket 18694 |
| 7 | * |
| 8 | * @group datequery |
| 9 | * @group datequery-posts |
| 10 | */ |
| 11 | class Tests_Query_Date_Posts extends WP_UnitTestCase { |
| 12 | |
| 13 | public $q; |
| 14 | |
| 15 | public function setUp() { |
| 16 | parent::setUp(); |
| 17 | |
| 18 | // Be careful modifying this. Tests are coded to expect this exact sample data. |
| 19 | $post_dates = array( |
| 20 | '1972-05-24 14:53:45', |
| 21 | '1984-07-28 19:28:56', |
| 22 | '2003-05-27 22:45:07', |
| 23 | '2004-01-03 08:54:10', |
| 24 | '2004-05-22 12:34:12', |
| 25 | '2005-02-17 00:00:15', |
| 26 | '2005-12-31 23:59:20', |
| 27 | '2007-01-22 03:49:21', |
| 28 | '2007-05-16 17:32:22', |
| 29 | '2007-09-24 07:17:23', |
| 30 | '2008-03-29 09:04:25', |
| 31 | '2008-07-15 11:32:26', |
| 32 | '2008-12-10 13:06:27', |
| 33 | '2009-06-11 21:30:28', |
| 34 | '2009-12-18 10:42:29', |
| 35 | '2010-06-17 17:09:30', |
| 36 | '2011-02-23 12:12:31', |
| 37 | '2011-07-04 01:56:32', |
| 38 | '2011-12-12 16:39:33', |
| 39 | '2012-06-13 14:03:34', |
| 40 | '2025-04-20 10:13:00', |
| 41 | '2025-04-20 10:13:01', |
| 42 | '2025-05-20 10:13:01', |
| 43 | ); |
| 44 | |
| 45 | foreach ( $post_dates as $post_date ) { |
| 46 | $this->factory->post->create( array( 'post_date' => $post_date ) ); |
| 47 | } |
| 48 | |
| 49 | unset( $this->q ); |
| 50 | $this->q = new WP_Query(); |
| 51 | } |
| 52 | |
| 53 | public function _get_query_result( $args = array() ) { |
| 54 | $args = wp_parse_args( $args, array( |
| 55 | 'post_status' => 'any', // For the future post |
| 56 | 'posts_per_page' => '-1', // To make sure results are accurate |
| 57 | 'orderby' => 'ID', // Same order they were created |
| 58 | 'order' => 'ASC', |
| 59 | ) ); |
| 60 | |
| 61 | return $this->q->query( $args ); |
| 62 | } |
| 63 | |
| 64 | |
| 65 | /** |
| 66 | * Simple legacy date parameter tests |
| 67 | */ |
| 68 | |
| 69 | public function test_simple_year_expecting_results() { |
| 70 | $posts = $this->_get_query_result( array( |
| 71 | 'year' => 2008, |
| 72 | ) ); |
| 73 | |
| 74 | $expected_dates = array( |
| 75 | '2008-03-29 09:04:25', |
| 76 | '2008-07-15 11:32:26', |
| 77 | '2008-12-10 13:06:27', |
| 78 | ); |
| 79 | |
| 80 | $this->assertEquals( $expected_dates, wp_list_pluck( $posts, 'post_date' ) ); |
| 81 | } |
| 82 | |
| 83 | public function test_simple_year_expecting_noresults() { |
| 84 | $posts = $this->_get_query_result( array( |
| 85 | 'year' => 2000, |
| 86 | ) ); |
| 87 | |
| 88 | $this->assertCount( 0, $posts ); |
| 89 | } |
| 90 | |
| 91 | public function test_simple_year_non_valid_numbers() { |
| 92 | $test_values = array( |
| 93 | 'abc', |
| 94 | -1, |
| 95 | true, |
| 96 | false, |
| 97 | 0, |
| 98 | '0', |
| 99 | ); |
| 100 | |
| 101 | foreach ( $test_values as $test_value ) { |
| 102 | $posts = $this->_get_query_result( array( |
| 103 | 'year' => 2000, |
| 104 | ) ); |
| 105 | |
| 106 | $this->assertCount( 0, $posts, $test_value ); |
| 107 | } |
| 108 | } |
| 109 | |
| 110 | public function test_simple_m_with_year_expecting_results() { |
| 111 | $posts = $this->_get_query_result( array( |
| 112 | 'm' => '2007', |
| 113 | ) ); |
| 114 | |
| 115 | $expected_dates = array( |
| 116 | '2007-01-22 03:49:21', |
| 117 | '2007-05-16 17:32:22', |
| 118 | '2007-09-24 07:17:23', |
| 119 | ); |
| 120 | |
| 121 | $this->assertEquals( $expected_dates, wp_list_pluck( $posts, 'post_date' ) ); |
| 122 | } |
| 123 | |
| 124 | public function test_simple_m_with_year_expecting_noresults() { |
| 125 | $posts = $this->_get_query_result( array( |
| 126 | 'm' => '1999', |
| 127 | ) ); |
| 128 | |
| 129 | $this->assertCount( 0, $posts ); |
| 130 | } |
| 131 | |
| 132 | public function test_simple_m_with_yearmonth_expecting_results() { |
| 133 | $posts = $this->_get_query_result( array( |
| 134 | 'm' => '202504', |
| 135 | ) ); |
| 136 | |
| 137 | $expected_dates = array( |
| 138 | '2025-04-20 10:13:00', |
| 139 | '2025-04-20 10:13:01', |
| 140 | ); |
| 141 | |
| 142 | $this->assertEquals( $expected_dates, wp_list_pluck( $posts, 'post_date' ) ); |
| 143 | } |
| 144 | |
| 145 | public function test_simple_m_with_yearmonth_expecting_noresults() { |
| 146 | $posts = $this->_get_query_result( array( |
| 147 | 'm' => '202502', |
| 148 | ) ); |
| 149 | |
| 150 | $this->assertCount( 0, $posts ); |
| 151 | } |
| 152 | |
| 153 | public function test_simple_m_with_yearmonthday_expecting_results() { |
| 154 | $posts = $this->_get_query_result( array( |
| 155 | 'm' => '20250420', |
| 156 | ) ); |
| 157 | |
| 158 | $expected_dates = array( |
| 159 | '2025-04-20 10:13:00', |
| 160 | '2025-04-20 10:13:01', |
| 161 | ); |
| 162 | |
| 163 | $this->assertEquals( $expected_dates, wp_list_pluck( $posts, 'post_date' ) ); |
| 164 | } |
| 165 | |
| 166 | public function test_simple_m_with_yearmonthday_expecting_noresults() { |
| 167 | $posts = $this->_get_query_result( array( |
| 168 | 'm' => '20250419', |
| 169 | ) ); |
| 170 | |
| 171 | $this->assertCount( 0, $posts ); |
| 172 | } |
| 173 | |
| 174 | public function test_simple_m_with_yearmonthdayhour_expecting_results() { |
| 175 | $posts = $this->_get_query_result( array( |
| 176 | 'm' => '2025042010', |
| 177 | ) ); |
| 178 | |
| 179 | $expected_dates = array( |
| 180 | '2025-04-20 10:13:00', |
| 181 | '2025-04-20 10:13:01', |
| 182 | ); |
| 183 | |
| 184 | $this->assertEquals( $expected_dates, wp_list_pluck( $posts, 'post_date' ) ); |
| 185 | } |
| 186 | |
| 187 | public function test_simple_m_with_yearmonthdayhour_expecting_noresults() { |
| 188 | $posts = $this->_get_query_result( array( |
| 189 | 'm' => '2025042009', |
| 190 | ) ); |
| 191 | |
| 192 | $this->assertCount( 0, $posts ); |
| 193 | } |
| 194 | |
| 195 | public function test_simple_m_with_yearmonthdayhourminute_expecting_results() { |
| 196 | $posts = $this->_get_query_result( array( |
| 197 | 'm' => '202504201013', |
| 198 | ) ); |
| 199 | |
| 200 | $expected_dates = array( |
| 201 | '2025-04-20 10:13:00', |
| 202 | '2025-04-20 10:13:01', |
| 203 | ); |
| 204 | |
| 205 | $this->assertEquals( $expected_dates, wp_list_pluck( $posts, 'post_date' ) ); |
| 206 | } |
| 207 | |
| 208 | public function test_simple_m_with_yearmonthdayhourminute_expecting_noresults() { |
| 209 | $posts = $this->_get_query_result( array( |
| 210 | 'm' => '202504201012', |
| 211 | ) ); |
| 212 | |
| 213 | $this->assertCount( 0, $posts ); |
| 214 | } |
| 215 | |
| 216 | public function test_simple_m_with_yearmonthdayhourminutesecond_expecting_results() { |
| 217 | $posts = $this->_get_query_result( array( |
| 218 | 'm' => '20250420101301', |
| 219 | ) ); |
| 220 | |
| 221 | $expected_dates = array( |
| 222 | '2025-04-20 10:13:01', |
| 223 | ); |
| 224 | |
| 225 | $this->assertEquals( $expected_dates, wp_list_pluck( $posts, 'post_date' ) ); |
| 226 | } |
| 227 | |
| 228 | public function test_simple_m_with_yearmonthdayhourminutesecond_expecting_noresults() { |
| 229 | $posts = $this->_get_query_result( array( |
| 230 | 'm' => '20250420101302', |
| 231 | ) ); |
| 232 | |
| 233 | $this->assertCount( 0, $posts ); |
| 234 | } |
| 235 | |
| 236 | public function test_simple_m_with_yearmonthdayhourminutesecond_and_dashes_expecting_results() { |
| 237 | $posts = $this->_get_query_result( array( |
| 238 | 'm' => '2025-04-20 10:13:00', |
| 239 | ) ); |
| 240 | |
| 241 | $expected_dates = array( |
| 242 | '2025-04-20 10:13:00', |
| 243 | ); |
| 244 | |
| 245 | $this->assertEquals( $expected_dates, wp_list_pluck( $posts, 'post_date' ) ); |
| 246 | } |
| 247 | |
| 248 | public function test_simple_m_with_yearmonthdayhourminutesecond_and_dashesletters_expecting_results() { |
| 249 | $posts = $this->_get_query_result( array( |
| 250 | 'm' => 'alpha2025-04-20 10:13:00', |
| 251 | ) ); |
| 252 | |
| 253 | $expected_dates = array( |
| 254 | '2025-04-20 10:13:00', |
| 255 | ); |
| 256 | |
| 257 | $this->assertEquals( $expected_dates, wp_list_pluck( $posts, 'post_date' ) ); |
| 258 | } |
| 259 | |
| 260 | public function test_simple_monthnum_expecting_results() { |
| 261 | $posts = $this->_get_query_result( array( |
| 262 | 'monthnum' => 5, |
| 263 | ) ); |
| 264 | |
| 265 | $expected_dates = array( |
| 266 | '1972-05-24 14:53:45', |
| 267 | '2003-05-27 22:45:07', |
| 268 | '2004-05-22 12:34:12', |
| 269 | '2007-05-16 17:32:22', |
| 270 | '2025-05-20 10:13:01', |
| 271 | ); |
| 272 | |
| 273 | $this->assertEquals( $expected_dates, wp_list_pluck( $posts, 'post_date' ) ); |
| 274 | } |
| 275 | |
| 276 | public function test_simple_monthnum_expecting_noresults() { |
| 277 | $posts = $this->_get_query_result( array( |
| 278 | 'monthnum' => 8, |
| 279 | ) ); |
| 280 | |
| 281 | $this->assertCount( 0, $posts ); |
| 282 | } |
| 283 | |
| 284 | public function test_simple_w_as_in_week_expecting_results() { |
| 285 | $posts = $this->_get_query_result( array( |
| 286 | 'w' => 24, |
| 287 | ) ); |
| 288 | |
| 289 | $expected_dates = array( |
| 290 | '2009-06-11 21:30:28', |
| 291 | '2010-06-17 17:09:30', |
| 292 | '2012-06-13 14:03:34', |
| 293 | ); |
| 294 | |
| 295 | $this->assertEquals( $expected_dates, wp_list_pluck( $posts, 'post_date' ) ); |
| 296 | } |
| 297 | |
| 298 | public function test_simple_w_as_in_week_expecting_noresults() { |
| 299 | $posts = $this->_get_query_result( array( |
| 300 | 'w' => 2, |
| 301 | ) ); |
| 302 | |
| 303 | $this->assertCount( 0, $posts ); |
| 304 | } |
| 305 | |
| 306 | public function test_simple_day_expecting_results() { |
| 307 | $posts = $this->_get_query_result( array( |
| 308 | 'day' => 22, |
| 309 | ) ); |
| 310 | |
| 311 | $expected_dates = array( |
| 312 | '2004-05-22 12:34:12', |
| 313 | '2007-01-22 03:49:21', |
| 314 | ); |
| 315 | |
| 316 | $this->assertEquals( $expected_dates, wp_list_pluck( $posts, 'post_date' ) ); |
| 317 | } |
| 318 | |
| 319 | public function test_simple_day_expecting_noresults() { |
| 320 | $posts = $this->_get_query_result( array( |
| 321 | 'day' => 30, |
| 322 | ) ); |
| 323 | |
| 324 | $this->assertCount( 0, $posts ); |
| 325 | } |
| 326 | |
| 327 | public function test_simple_hour_expecting_results() { |
| 328 | $posts = $this->_get_query_result( array( |
| 329 | 'hour' => 21, |
| 330 | ) ); |
| 331 | |
| 332 | $expected_dates = array( |
| 333 | '2009-06-11 21:30:28', |
| 334 | ); |
| 335 | |
| 336 | $this->assertEquals( $expected_dates, wp_list_pluck( $posts, 'post_date' ) ); |
| 337 | } |
| 338 | |
| 339 | public function test_simple_hour_expecting_noresults() { |
| 340 | $posts = $this->_get_query_result( array( |
| 341 | 'hour' => 2, |
| 342 | ) ); |
| 343 | |
| 344 | $this->assertCount( 0, $posts ); |
| 345 | } |
| 346 | |
| 347 | public function test_simple_minute_expecting_results() { |
| 348 | $posts = $this->_get_query_result( array( |
| 349 | 'minute' => 32, |
| 350 | ) ); |
| 351 | |
| 352 | $expected_dates = array( |
| 353 | '2007-05-16 17:32:22', |
| 354 | '2008-07-15 11:32:26', |
| 355 | ); |
| 356 | |
| 357 | $this->assertEquals( $expected_dates, wp_list_pluck( $posts, 'post_date' ) ); |
| 358 | } |
| 359 | |
| 360 | public function test_simple_minute_expecting_noresults() { |
| 361 | $posts = $this->_get_query_result( array( |
| 362 | 'minute' => 1, |
| 363 | ) ); |
| 364 | |
| 365 | $this->assertCount( 0, $posts ); |
| 366 | } |
| 367 | |
| 368 | public function test_simple_second_expecting_results() { |
| 369 | $posts = $this->_get_query_result( array( |
| 370 | 'second' => 30, |
| 371 | ) ); |
| 372 | |
| 373 | $expected_dates = array( |
| 374 | '2010-06-17 17:09:30', |
| 375 | ); |
| 376 | |
| 377 | $this->assertEquals( $expected_dates, wp_list_pluck( $posts, 'post_date' ) ); |
| 378 | } |
| 379 | |
| 380 | public function test_simple_second_expecting_noresults() { |
| 381 | $posts = $this->_get_query_result( array( |
| 382 | 'second' => 50, |
| 383 | ) ); |
| 384 | |
| 385 | $this->assertCount( 0, $posts ); |
| 386 | } |
| 387 | |
| 388 | |
| 389 | /** |
| 390 | * WP_Date_Query tests |
| 391 | */ |
| 392 | |
| 393 | public function test_date_query_before_array() { |
| 394 | if ( ! class_exists( 'WP_Date_Query' ) ) |
| 395 | $this->markTestSkipped( 'WP_Date_Query is missing, skipping tests.' ); |
| 396 | |
| 397 | $posts = $this->_get_query_result( array( |
| 398 | 'date_query' => array( |
| 399 | array( |
| 400 | 'before' => array( |
| 401 | 'year' => 2008, |
| 402 | 'month' => 6, |
| 403 | ), |
| 404 | ), |
| 405 | ), |
| 406 | ) ); |
| 407 | |
| 408 | $expected_dates = array( |
| 409 | '1972-05-24 14:53:45', |
| 410 | '1984-07-28 19:28:56', |
| 411 | '2003-05-27 22:45:07', |
| 412 | '2004-01-03 08:54:10', |
| 413 | '2004-05-22 12:34:12', |
| 414 | '2005-02-17 00:00:15', |
| 415 | '2005-12-31 23:59:20', |
| 416 | '2007-01-22 03:49:21', |
| 417 | '2007-05-16 17:32:22', |
| 418 | '2007-09-24 07:17:23', |
| 419 | '2008-03-29 09:04:25', |
| 420 | ); |
| 421 | |
| 422 | $this->assertEquals( $expected_dates, wp_list_pluck( $posts, 'post_date' ) ); |
| 423 | } |
| 424 | |
| 425 | /** |
| 426 | * Specifically tests to make sure values are defaulting to |
| 427 | * their minimum values when being used with "before". |
| 428 | */ |
| 429 | public function test_date_query_before_array_test_defaulting() { |
| 430 | if ( ! class_exists( 'WP_Date_Query' ) ) |
| 431 | $this->markTestSkipped( 'WP_Date_Query is missing, skipping tests.' ); |
| 432 | |
| 433 | $posts = $this->_get_query_result( array( |
| 434 | 'date_query' => array( |
| 435 | array( |
| 436 | 'before' => array( |
| 437 | 'year' => 2008, |
| 438 | ), |
| 439 | ), |
| 440 | ), |
| 441 | ) ); |
| 442 | |
| 443 | $expected_dates = array( |
| 444 | '1972-05-24 14:53:45', |
| 445 | '1984-07-28 19:28:56', |
| 446 | '2003-05-27 22:45:07', |
| 447 | '2004-01-03 08:54:10', |
| 448 | '2004-05-22 12:34:12', |
| 449 | '2005-02-17 00:00:15', |
| 450 | '2005-12-31 23:59:20', |
| 451 | '2007-01-22 03:49:21', |
| 452 | '2007-05-16 17:32:22', |
| 453 | '2007-09-24 07:17:23', |
| 454 | ); |
| 455 | |
| 456 | $this->assertEquals( $expected_dates, wp_list_pluck( $posts, 'post_date' ) ); |
| 457 | } |
| 458 | |
| 459 | public function test_date_query_before_string() { |
| 460 | if ( ! class_exists( 'WP_Date_Query' ) ) |
| 461 | $this->markTestSkipped( 'WP_Date_Query is missing, skipping tests.' ); |
| 462 | |
| 463 | $posts = $this->_get_query_result( array( |
| 464 | 'date_query' => array( |
| 465 | array( |
| 466 | 'before' => 'May 4th, 2008', |
| 467 | ), |
| 468 | ), |
| 469 | ) ); |
| 470 | |
| 471 | $expected_dates = array( |
| 472 | '1972-05-24 14:53:45', |
| 473 | '1984-07-28 19:28:56', |
| 474 | '2003-05-27 22:45:07', |
| 475 | '2004-01-03 08:54:10', |
| 476 | '2004-05-22 12:34:12', |
| 477 | '2005-02-17 00:00:15', |
| 478 | '2005-12-31 23:59:20', |
| 479 | '2007-01-22 03:49:21', |
| 480 | '2007-05-16 17:32:22', |
| 481 | '2007-09-24 07:17:23', |
| 482 | '2008-03-29 09:04:25', |
| 483 | ); |
| 484 | |
| 485 | $this->assertEquals( $expected_dates, wp_list_pluck( $posts, 'post_date' ) ); |
| 486 | } |
| 487 | |
| 488 | public function test_date_query_after_array() { |
| 489 | if ( ! class_exists( 'WP_Date_Query' ) ) |
| 490 | $this->markTestSkipped( 'WP_Date_Query is missing, skipping tests.' ); |
| 491 | |
| 492 | $posts = $this->_get_query_result( array( |
| 493 | 'date_query' => array( |
| 494 | array( |
| 495 | 'after' => array( |
| 496 | 'year' => 2009, |
| 497 | 'month' => 12, |
| 498 | 'day' => 31, |
| 499 | ), |
| 500 | ), |
| 501 | ), |
| 502 | ) ); |
| 503 | |
| 504 | $expected_dates = array( |
| 505 | '2010-06-17 17:09:30', |
| 506 | '2011-02-23 12:12:31', |
| 507 | '2011-07-04 01:56:32', |
| 508 | '2011-12-12 16:39:33', |
| 509 | '2012-06-13 14:03:34', |
| 510 | '2025-04-20 10:13:00', |
| 511 | '2025-04-20 10:13:01', |
| 512 | '2025-05-20 10:13:01', |
| 513 | ); |
| 514 | |
| 515 | $this->assertEquals( $expected_dates, wp_list_pluck( $posts, 'post_date' ) ); |
| 516 | } |
| 517 | |
| 518 | /** |
| 519 | * Specifically tests to make sure values are defaulting to |
| 520 | * their maximum values when being used with "after". |
| 521 | */ |
| 522 | public function test_date_query_after_array_test_defaulting() { |
| 523 | if ( ! class_exists( 'WP_Date_Query' ) ) |
| 524 | $this->markTestSkipped( 'WP_Date_Query is missing, skipping tests.' ); |
| 525 | |
| 526 | $posts = $this->_get_query_result( array( |
| 527 | 'date_query' => array( |
| 528 | array( |
| 529 | 'after' => array( |
| 530 | 'year' => 2008, |
| 531 | ), |
| 532 | ), |
| 533 | ), |
| 534 | ) ); |
| 535 | |
| 536 | $expected_dates = array( |
| 537 | '2009-06-11 21:30:28', |
| 538 | '2009-12-18 10:42:29', |
| 539 | '2010-06-17 17:09:30', |
| 540 | '2011-02-23 12:12:31', |
| 541 | '2011-07-04 01:56:32', |
| 542 | '2011-12-12 16:39:33', |
| 543 | '2012-06-13 14:03:34', |
| 544 | '2025-04-20 10:13:00', |
| 545 | '2025-04-20 10:13:01', |
| 546 | '2025-05-20 10:13:01', |
| 547 | ); |
| 548 | |
| 549 | $this->assertEquals( $expected_dates, wp_list_pluck( $posts, 'post_date' ) ); |
| 550 | } |
| 551 | |
| 552 | public function test_date_query_after_string() { |
| 553 | if ( ! class_exists( 'WP_Date_Query' ) ) |
| 554 | $this->markTestSkipped( 'WP_Date_Query is missing, skipping tests.' ); |
| 555 | |
| 556 | $posts = $this->_get_query_result( array( |
| 557 | 'date_query' => array( |
| 558 | array( |
| 559 | 'after' => '2009-12-18 10:42:29', |
| 560 | ), |
| 561 | ), |
| 562 | ) ); |
| 563 | |
| 564 | $expected_dates = array( |
| 565 | '2010-06-17 17:09:30', |
| 566 | '2011-02-23 12:12:31', |
| 567 | '2011-07-04 01:56:32', |
| 568 | '2011-12-12 16:39:33', |
| 569 | '2012-06-13 14:03:34', |
| 570 | '2025-04-20 10:13:00', |
| 571 | '2025-04-20 10:13:01', |
| 572 | '2025-05-20 10:13:01', |
| 573 | ); |
| 574 | |
| 575 | $this->assertEquals( $expected_dates, wp_list_pluck( $posts, 'post_date' ) ); |
| 576 | } |
| 577 | |
| 578 | public function test_date_query_after_string_inclusive() { |
| 579 | if ( ! class_exists( 'WP_Date_Query' ) ) |
| 580 | $this->markTestSkipped( 'WP_Date_Query is missing, skipping tests.' ); |
| 581 | |
| 582 | $posts = $this->_get_query_result( array( |
| 583 | 'date_query' => array( |
| 584 | array( |
| 585 | 'after' => '2009-12-18 10:42:29', |
| 586 | 'inclusive' => true, |
| 587 | ), |
| 588 | ), |
| 589 | ) ); |
| 590 | |
| 591 | $expected_dates = array( |
| 592 | '2009-12-18 10:42:29', |
| 593 | '2010-06-17 17:09:30', |
| 594 | '2011-02-23 12:12:31', |
| 595 | '2011-07-04 01:56:32', |
| 596 | '2011-12-12 16:39:33', |
| 597 | '2012-06-13 14:03:34', |
| 598 | '2025-04-20 10:13:00', |
| 599 | '2025-04-20 10:13:01', |
| 600 | '2025-05-20 10:13:01', |
| 601 | ); |
| 602 | |
| 603 | $this->assertEquals( $expected_dates, wp_list_pluck( $posts, 'post_date' ) ); |
| 604 | } |
| 605 | |
| 606 | public function test_date_query_year_expecting_results() { |
| 607 | if ( ! class_exists( 'WP_Date_Query' ) ) |
| 608 | $this->markTestSkipped( 'WP_Date_Query is missing, skipping tests.' ); |
| 609 | |
| 610 | $posts = $this->_get_query_result( array( |
| 611 | 'date_query' => array( |
| 612 | array( |
| 613 | 'year' => 2009, |
| 614 | ), |
| 615 | ), |
| 616 | ) ); |
| 617 | |
| 618 | $expected_dates = array( |
| 619 | '2009-06-11 21:30:28', |
| 620 | '2009-12-18 10:42:29', |
| 621 | ); |
| 622 | |
| 623 | $this->assertEquals( $expected_dates, wp_list_pluck( $posts, 'post_date' ) ); |
| 624 | } |
| 625 | |
| 626 | public function test_date_query_year_expecting_noresults() { |
| 627 | if ( ! class_exists( 'WP_Date_Query' ) ) |
| 628 | $this->markTestSkipped( 'WP_Date_Query is missing, skipping tests.' ); |
| 629 | |
| 630 | $posts = $this->_get_query_result( array( |
| 631 | 'date_query' => array( |
| 632 | array( |
| 633 | 'year' => 2001, |
| 634 | ), |
| 635 | ), |
| 636 | ) ); |
| 637 | |
| 638 | $this->assertCount( 0, $posts ); |
| 639 | } |
| 640 | |
| 641 | public function test_date_query_month_expecting_results() { |
| 642 | if ( ! class_exists( 'WP_Date_Query' ) ) |
| 643 | $this->markTestSkipped( 'WP_Date_Query is missing, skipping tests.' ); |
| 644 | |
| 645 | $posts = $this->_get_query_result( array( |
| 646 | 'date_query' => array( |
| 647 | array( |
| 648 | 'month' => 12, |
| 649 | ), |
| 650 | ), |
| 651 | ) ); |
| 652 | |
| 653 | $expected_dates = array( |
| 654 | '2005-12-31 23:59:20', |
| 655 | '2008-12-10 13:06:27', |
| 656 | '2009-12-18 10:42:29', |
| 657 | '2011-12-12 16:39:33', |
| 658 | ); |
| 659 | |
| 660 | $this->assertEquals( $expected_dates, wp_list_pluck( $posts, 'post_date' ) ); |
| 661 | } |
| 662 | |
| 663 | public function test_date_query_month_expecting_noresults() { |
| 664 | if ( ! class_exists( 'WP_Date_Query' ) ) |
| 665 | $this->markTestSkipped( 'WP_Date_Query is missing, skipping tests.' ); |
| 666 | |
| 667 | $posts = $this->_get_query_result( array( |
| 668 | 'date_query' => array( |
| 669 | array( |
| 670 | 'month' => 8, |
| 671 | ), |
| 672 | ), |
| 673 | ) ); |
| 674 | |
| 675 | $this->assertCount( 0, $posts ); |
| 676 | } |
| 677 | |
| 678 | public function test_date_query_week_expecting_results() { |
| 679 | if ( ! class_exists( 'WP_Date_Query' ) ) |
| 680 | $this->markTestSkipped( 'WP_Date_Query is missing, skipping tests.' ); |
| 681 | |
| 682 | $posts = $this->_get_query_result( array( |
| 683 | 'date_query' => array( |
| 684 | array( |
| 685 | 'week' => 1, |
| 686 | ), |
| 687 | ), |
| 688 | ) ); |
| 689 | |
| 690 | $expected_dates = array( |
| 691 | '2004-01-03 08:54:10', |
| 692 | ); |
| 693 | |
| 694 | $this->assertEquals( $expected_dates, wp_list_pluck( $posts, 'post_date' ) ); |
| 695 | } |
| 696 | |
| 697 | public function test_date_query_week_expecting_noresults() { |
| 698 | if ( ! class_exists( 'WP_Date_Query' ) ) |
| 699 | $this->markTestSkipped( 'WP_Date_Query is missing, skipping tests.' ); |
| 700 | |
| 701 | $posts = $this->_get_query_result( array( |
| 702 | 'date_query' => array( |
| 703 | array( |
| 704 | 'week' => 10, |
| 705 | ), |
| 706 | ), |
| 707 | ) ); |
| 708 | |
| 709 | $this->assertCount( 0, $posts ); |
| 710 | } |
| 711 | |
| 712 | public function test_date_query_day_expecting_results() { |
| 713 | if ( ! class_exists( 'WP_Date_Query' ) ) |
| 714 | $this->markTestSkipped( 'WP_Date_Query is missing, skipping tests.' ); |
| 715 | |
| 716 | $posts = $this->_get_query_result( array( |
| 717 | 'date_query' => array( |
| 718 | array( |
| 719 | 'day' => 17, |
| 720 | ), |
| 721 | ), |
| 722 | ) ); |
| 723 | |
| 724 | $expected_dates = array( |
| 725 | '2005-02-17 00:00:15', |
| 726 | '2010-06-17 17:09:30', |
| 727 | ); |
| 728 | |
| 729 | $this->assertEquals( $expected_dates, wp_list_pluck( $posts, 'post_date' ) ); |
| 730 | } |
| 731 | |
| 732 | public function test_date_query_day_expecting_noresults() { |
| 733 | if ( ! class_exists( 'WP_Date_Query' ) ) |
| 734 | $this->markTestSkipped( 'WP_Date_Query is missing, skipping tests.' ); |
| 735 | |
| 736 | $posts = $this->_get_query_result( array( |
| 737 | 'date_query' => array( |
| 738 | array( |
| 739 | 'day' => 19, |
| 740 | ), |
| 741 | ), |
| 742 | ) ); |
| 743 | |
| 744 | $this->assertCount( 0, $posts ); |
| 745 | } |
| 746 | |
| 747 | public function test_date_query_dayofweek_expecting_results() { |
| 748 | if ( ! class_exists( 'WP_Date_Query' ) ) |
| 749 | $this->markTestSkipped( 'WP_Date_Query is missing, skipping tests.' ); |
| 750 | |
| 751 | $posts = $this->_get_query_result( array( |
| 752 | 'date_query' => array( |
| 753 | array( |
| 754 | 'dayofweek' => 7, |
| 755 | ), |
| 756 | ), |
| 757 | ) ); |
| 758 | |
| 759 | $expected_dates = array( |
| 760 | '1984-07-28 19:28:56', |
| 761 | '2004-01-03 08:54:10', |
| 762 | '2004-05-22 12:34:12', |
| 763 | '2005-12-31 23:59:20', |
| 764 | '2008-03-29 09:04:25', |
| 765 | ); |
| 766 | |
| 767 | $this->assertEquals( $expected_dates, wp_list_pluck( $posts, 'post_date' ) ); |
| 768 | } |
| 769 | |
| 770 | public function test_date_query_hour_expecting_results() { |
| 771 | if ( ! class_exists( 'WP_Date_Query' ) ) |
| 772 | $this->markTestSkipped( 'WP_Date_Query is missing, skipping tests.' ); |
| 773 | |
| 774 | $posts = $this->_get_query_result( array( |
| 775 | 'date_query' => array( |
| 776 | array( |
| 777 | 'hour' => 13, |
| 778 | ), |
| 779 | ), |
| 780 | ) ); |
| 781 | |
| 782 | $expected_dates = array( |
| 783 | '2008-12-10 13:06:27', |
| 784 | ); |
| 785 | |
| 786 | $this->assertEquals( $expected_dates, wp_list_pluck( $posts, 'post_date' ) ); |
| 787 | } |
| 788 | |
| 789 | public function test_date_query_hour_expecting_noresults() { |
| 790 | if ( ! class_exists( 'WP_Date_Query' ) ) |
| 791 | $this->markTestSkipped( 'WP_Date_Query is missing, skipping tests.' ); |
| 792 | |
| 793 | $posts = $this->_get_query_result( array( |
| 794 | 'date_query' => array( |
| 795 | array( |
| 796 | 'hour' => 2, |
| 797 | ), |
| 798 | ), |
| 799 | ) ); |
| 800 | |
| 801 | $this->assertCount( 0, $posts ); |
| 802 | } |
| 803 | |
| 804 | public function test_date_query_minute_expecting_results() { |
| 805 | if ( ! class_exists( 'WP_Date_Query' ) ) |
| 806 | $this->markTestSkipped( 'WP_Date_Query is missing, skipping tests.' ); |
| 807 | |
| 808 | $posts = $this->_get_query_result( array( |
| 809 | 'date_query' => array( |
| 810 | array( |
| 811 | 'minute' => 56, |
| 812 | ), |
| 813 | ), |
| 814 | ) ); |
| 815 | |
| 816 | $expected_dates = array( |
| 817 | '2011-07-04 01:56:32', |
| 818 | ); |
| 819 | |
| 820 | $this->assertEquals( $expected_dates, wp_list_pluck( $posts, 'post_date' ) ); |
| 821 | } |
| 822 | |
| 823 | public function test_date_query_minute_expecting_noresults() { |
| 824 | if ( ! class_exists( 'WP_Date_Query' ) ) |
| 825 | $this->markTestSkipped( 'WP_Date_Query is missing, skipping tests.' ); |
| 826 | |
| 827 | $posts = $this->_get_query_result( array( |
| 828 | 'date_query' => array( |
| 829 | array( |
| 830 | 'minute' => 2, |
| 831 | ), |
| 832 | ), |
| 833 | ) ); |
| 834 | |
| 835 | $this->assertCount( 0, $posts ); |
| 836 | } |
| 837 | |
| 838 | public function test_date_query_second_expecting_results() { |
| 839 | if ( ! class_exists( 'WP_Date_Query' ) ) |
| 840 | $this->markTestSkipped( 'WP_Date_Query is missing, skipping tests.' ); |
| 841 | |
| 842 | $posts = $this->_get_query_result( array( |
| 843 | 'date_query' => array( |
| 844 | array( |
| 845 | 'second' => 21, |
| 846 | ), |
| 847 | ), |
| 848 | ) ); |
| 849 | |
| 850 | $expected_dates = array( |
| 851 | '2007-01-22 03:49:21', |
| 852 | ); |
| 853 | |
| 854 | $this->assertEquals( $expected_dates, wp_list_pluck( $posts, 'post_date' ) ); |
| 855 | } |
| 856 | |
| 857 | public function test_date_query_second_expecting_noresults() { |
| 858 | if ( ! class_exists( 'WP_Date_Query' ) ) |
| 859 | $this->markTestSkipped( 'WP_Date_Query is missing, skipping tests.' ); |
| 860 | |
| 861 | $posts = $this->_get_query_result( array( |
| 862 | 'date_query' => array( |
| 863 | array( |
| 864 | 'second' => 1, |
| 865 | ), |
| 866 | ), |
| 867 | ) ); |
| 868 | |
| 869 | $this->assertCount( 0, $posts ); |
| 870 | } |
| 871 | |
| 872 | public function test_date_query_between_two_times() { |
| 873 | if ( ! class_exists( 'WP_Date_Query' ) ) |
| 874 | $this->markTestSkipped( 'WP_Date_Query is missing, skipping tests.' ); |
| 875 | |
| 876 | $posts = $this->_get_query_result( array( |
| 877 | 'date_query' => array( |
| 878 | array( |
| 879 | 'hour' => 9, |
| 880 | 'minute' => 0, |
| 881 | 'compare' => '>=', |
| 882 | ), |
| 883 | array( |
| 884 | 'hour' => '17', |
| 885 | 'minute' => '0', |
| 886 | 'compare' => '<=', |
| 887 | ), |
| 888 | ), |
| 889 | ) ); |
| 890 | |
| 891 | $expected_dates = array( |
| 892 | '1972-05-24 14:53:45', |
| 893 | '2004-05-22 12:34:12', |
| 894 | '2008-03-29 09:04:25', |
| 895 | '2008-07-15 11:32:26', |
| 896 | '2008-12-10 13:06:27', |
| 897 | '2009-12-18 10:42:29', |
| 898 | '2011-02-23 12:12:31', |
| 899 | '2011-12-12 16:39:33', |
| 900 | '2012-06-13 14:03:34', |
| 901 | '2025-04-20 10:13:00', |
| 902 | '2025-04-20 10:13:01', |
| 903 | '2025-05-20 10:13:01', |
| 904 | ); |
| 905 | |
| 906 | $this->assertEquals( $expected_dates, wp_list_pluck( $posts, 'post_date' ) ); |
| 907 | } |
| 908 | |
| 909 | public function test_date_query_relation_or() { |
| 910 | if ( ! class_exists( 'WP_Date_Query' ) ) |
| 911 | $this->markTestSkipped( 'WP_Date_Query is missing, skipping tests.' ); |
| 912 | |
| 913 | $posts = $this->_get_query_result( array( |
| 914 | 'date_query' => array( |
| 915 | array( |
| 916 | 'hour' => 14, |
| 917 | ), |
| 918 | array( |
| 919 | 'minute' => 34, |
| 920 | ), |
| 921 | 'relation' => 'OR', |
| 922 | ), |
| 923 | ) ); |
| 924 | |
| 925 | $expected_dates = array( |
| 926 | '1972-05-24 14:53:45', |
| 927 | '2004-05-22 12:34:12', |
| 928 | '2012-06-13 14:03:34', |
| 929 | ); |
| 930 | |
| 931 | $this->assertEquals( $expected_dates, wp_list_pluck( $posts, 'post_date' ) ); |
| 932 | } |
| 933 | |
| 934 | public function test_date_query_compare_greater_than_or_equal_to() { |
| 935 | if ( ! class_exists( 'WP_Date_Query' ) ) |
| 936 | $this->markTestSkipped( 'WP_Date_Query is missing, skipping tests.' ); |
| 937 | |
| 938 | $posts = $this->_get_query_result( array( |
| 939 | 'date_query' => array( |
| 940 | array( |
| 941 | 'hour' => 14, |
| 942 | 'minute' => 34, |
| 943 | ), |
| 944 | 'compare' => '>=', |
| 945 | ), |
| 946 | ) ); |
| 947 | |
| 948 | $expected_dates = array( |
| 949 | '1972-05-24 14:53:45', |
| 950 | '1984-07-28 19:28:56', |
| 951 | '2003-05-27 22:45:07', |
| 952 | '2005-12-31 23:59:20', |
| 953 | '2007-05-16 17:32:22', |
| 954 | '2009-06-11 21:30:28', |
| 955 | '2010-06-17 17:09:30', |
| 956 | '2011-12-12 16:39:33', |
| 957 | ); |
| 958 | |
| 959 | $this->assertEquals( $expected_dates, wp_list_pluck( $posts, 'post_date' ) ); |
| 960 | } |
| 961 | } |
| 962 | |
| 963 | /** |
| 964 | * Tests for things specifically related to comments. |
| 965 | * No need to do a full repeat of all of the post tests again |
| 966 | * since the query SQL is the same for both just with a different column. |
| 967 | * |
| 968 | * @group datequery |
| 969 | * @group datequery-comments |
| 970 | */ |
| 971 | class Tests_Query_Date_Comments extends WP_UnitTestCase { |
| 972 | |
| 973 | public function setUp() { |
| 974 | parent::setUp(); |
| 975 | |
| 976 | // Just some dummy posts to use as parents for comments |
| 977 | $posts = array(); |
| 978 | for ( $i = 1; $i <= 2; $i++ ) { |
| 979 | $posts[$i] = $this->factory->post->create(); |
| 980 | } |
| 981 | |
| 982 | // Be careful modifying this. Tests are coded to expect this exact sample data. |
| 983 | // Format is 'datetime' => 'post number (not ID)' |
| 984 | $comment_dates = array( |
| 985 | '2007-01-22 03:49:21' => 1, |
| 986 | '2007-05-16 17:32:22' => 1, |
| 987 | '2007-09-24 07:17:23' => 1, |
| 988 | '2008-03-29 09:04:25' => 1, |
| 989 | '2008-07-15 11:32:26' => 2, // This one should never be in the results |
| 990 | '2008-12-10 13:06:27' => 1, |
| 991 | '2009-06-11 21:30:28' => 1, |
| 992 | '2009-12-18 10:42:29' => 1, |
| 993 | ); |
| 994 | |
| 995 | foreach ( $comment_dates as $comment_date => $comment_parent ) { |
| 996 | $result = $this->factory->comment->create( array( |
| 997 | 'comment_date' => $comment_date, |
| 998 | 'comment_parent' => $posts[ $comment_parent ], |
| 999 | ) ); |
| 1000 | } |
| 1001 | } |
| 1002 | |
| 1003 | public function _get_query_result( $args = array() ) { |
| 1004 | $args = wp_parse_args( $args, array( |
| 1005 | 'post_id' => 1, |
| 1006 | 'orderby' => 'comment_ID', // Same order they were created |
| 1007 | 'order' => 'ASC', |
| 1008 | ) ); |
| 1009 | |
| 1010 | return get_comments( $args ); |
| 1011 | } |
| 1012 | |
| 1013 | public function test_year() { |
| 1014 | if ( ! class_exists( 'WP_Date_Query' ) ) |
| 1015 | $this->markTestSkipped( 'WP_Date_Query is missing, skipping tests.' ); |
| 1016 | |
| 1017 | $comments = $this->_get_query_result( array( |
| 1018 | 'date_query' => array( |
| 1019 | array( |
| 1020 | 'year' => 2008, |
| 1021 | ), |
| 1022 | ), |
| 1023 | ) ); |
| 1024 | |
| 1025 | $expected_dates = array( |
| 1026 | '2008-03-29 09:04:25', |
| 1027 | '2008-12-10 13:06:27', |
| 1028 | ); |
| 1029 | |
| 1030 | $this->assertEquals( $expected_dates, wp_list_pluck( $comments, 'comment_date' ) ); |
| 1031 | } |
| 1032 | } |
| 1033 | No newline at end of file |