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