| | 1 | <?php |
| | 2 | |
| | 3 | /** |
| | 4 | * Tests for public methods of WP_Date_Query. |
| | 5 | * |
| | 6 | * See query/dateQuery.php for tests that require WP_Query. |
| | 7 | * |
| | 8 | * @group datequery |
| | 9 | * @group date |
| | 10 | */ |
| | 11 | class Tests_WP_Date_Query extends WP_UnitTestCase { |
| | 12 | public function test_construct_date_query_empty() { |
| | 13 | $q = new WP_Date_Query( array() ); |
| | 14 | $this->assertSame( 'AND', $q->relation ); |
| | 15 | $this->assertSame( 'post_date', $q->column ); |
| | 16 | $this->assertSame( '=', $q->compare ); |
| | 17 | $this->assertSame( array(), $q->queries ); |
| | 18 | } |
| | 19 | |
| | 20 | public function test_construct_date_query_non_array() { |
| | 21 | $q = new WP_Date_Query( 'foo' ); |
| | 22 | $this->assertSame( 'AND', $q->relation ); |
| | 23 | $this->assertSame( 'post_date', $q->column ); |
| | 24 | $this->assertSame( '=', $q->compare ); |
| | 25 | $this->assertSame( array(), $q->queries ); |
| | 26 | } |
| | 27 | |
| | 28 | public function test_construct_relation_or_lowercase() { |
| | 29 | $q = new WP_Date_Query( array( |
| | 30 | 'relation' => 'or', |
| | 31 | ) ); |
| | 32 | |
| | 33 | $this->assertSame( 'OR', $q->relation ); |
| | 34 | } |
| | 35 | |
| | 36 | public function test_construct_relation_invalid() { |
| | 37 | $q = new WP_Date_Query( array( |
| | 38 | 'relation' => 'foo', |
| | 39 | ) ); |
| | 40 | |
| | 41 | $this->assertSame( 'AND', $q->relation ); |
| | 42 | } |
| | 43 | |
| | 44 | public function test_construct_query_not_an_array_of_arrays() { |
| | 45 | $q = new WP_Date_Query( array( |
| | 46 | 'before' => array( |
| | 47 | 'year' => 2008, |
| | 48 | 'month' => 6, |
| | 49 | ), |
| | 50 | ) ); |
| | 51 | |
| | 52 | $expected = array( |
| | 53 | 0 => array( |
| | 54 | 'before' => array( |
| | 55 | 'year' => 2008, |
| | 56 | 'month' => 6, |
| | 57 | ), |
| | 58 | ), |
| | 59 | ); |
| | 60 | |
| | 61 | $this->assertSame( $expected, $q->queries ); |
| | 62 | } |
| | 63 | |
| | 64 | public function test_construct_query_contains_non_arrays() { |
| | 65 | $q = new WP_Date_Query( array( |
| | 66 | 'foo', |
| | 67 | 'bar', |
| | 68 | array( |
| | 69 | 'before' => array( |
| | 70 | 'year' => 2008, |
| | 71 | 'month' => 6, |
| | 72 | ), |
| | 73 | ), |
| | 74 | ) ); |
| | 75 | |
| | 76 | // Note: WP_Date_Query does not reset indexes |
| | 77 | $expected = array( |
| | 78 | 2 => array( |
| | 79 | 'before' => array( |
| | 80 | 'year' => 2008, |
| | 81 | 'month' => 6, |
| | 82 | ), |
| | 83 | ), |
| | 84 | ); |
| | 85 | |
| | 86 | $this->assertSame( $expected, $q->queries ); |
| | 87 | } |
| | 88 | |
| | 89 | public function test_get_compare_empty() { |
| | 90 | $q = new WP_Date_Query( array() ); |
| | 91 | $this->assertSame( '=', $q->get_compare( array() ) ); |
| | 92 | } |
| | 93 | |
| | 94 | public function test_get_compare_equals() { |
| | 95 | $q = new WP_Date_Query( array() ); |
| | 96 | |
| | 97 | $found = $q->get_compare( array( |
| | 98 | 'compare' => '=', |
| | 99 | ) ); |
| | 100 | $this->assertSame( '=', $found ); |
| | 101 | } |
| | 102 | |
| | 103 | public function test_get_compare_not_equals() { |
| | 104 | $q = new WP_Date_Query( array() ); |
| | 105 | |
| | 106 | $found = $q->get_compare( array( |
| | 107 | 'compare' => '!=', |
| | 108 | ) ); |
| | 109 | $this->assertSame( '!=', $found ); |
| | 110 | } |
| | 111 | |
| | 112 | public function test_get_compare_greater_than() { |
| | 113 | $q = new WP_Date_Query( array() ); |
| | 114 | |
| | 115 | $found = $q->get_compare( array( |
| | 116 | 'compare' => '>', |
| | 117 | ) ); |
| | 118 | $this->assertSame( '>', $found ); |
| | 119 | } |
| | 120 | |
| | 121 | public function test_get_compare_greater_than_or_equal_to() { |
| | 122 | $q = new WP_Date_Query( array() ); |
| | 123 | |
| | 124 | $found = $q->get_compare( array( |
| | 125 | 'compare' => '>=', |
| | 126 | ) ); |
| | 127 | $this->assertSame( '>=', $found ); |
| | 128 | } |
| | 129 | |
| | 130 | public function test_get_compare_less_than() { |
| | 131 | $q = new WP_Date_Query( array() ); |
| | 132 | |
| | 133 | $found = $q->get_compare( array( |
| | 134 | 'compare' => '<', |
| | 135 | ) ); |
| | 136 | $this->assertSame( '<', $found ); |
| | 137 | } |
| | 138 | |
| | 139 | public function test_get_compare_less_than_or_equal_to() { |
| | 140 | $q = new WP_Date_Query( array() ); |
| | 141 | |
| | 142 | $found = $q->get_compare( array( |
| | 143 | 'compare' => '<=', |
| | 144 | ) ); |
| | 145 | $this->assertSame( '<=', $found ); |
| | 146 | } |
| | 147 | |
| | 148 | public function test_get_compare_in() { |
| | 149 | $q = new WP_Date_Query( array() ); |
| | 150 | |
| | 151 | $found = $q->get_compare( array( |
| | 152 | 'compare' => 'IN', |
| | 153 | ) ); |
| | 154 | $this->assertSame( 'IN', $found ); |
| | 155 | } |
| | 156 | |
| | 157 | public function test_get_compare_not_in() { |
| | 158 | $q = new WP_Date_Query( array() ); |
| | 159 | |
| | 160 | $found = $q->get_compare( array( |
| | 161 | 'compare' => 'NOT IN', |
| | 162 | ) ); |
| | 163 | $this->assertSame( 'NOT IN', $found ); |
| | 164 | } |
| | 165 | |
| | 166 | public function test_get_compare_between() { |
| | 167 | $q = new WP_Date_Query( array() ); |
| | 168 | |
| | 169 | $found = $q->get_compare( array( |
| | 170 | 'compare' => 'BETWEEN', |
| | 171 | ) ); |
| | 172 | $this->assertSame( 'BETWEEN', $found ); |
| | 173 | } |
| | 174 | |
| | 175 | public function test_get_compare_not_between() { |
| | 176 | $q = new WP_Date_Query( array() ); |
| | 177 | |
| | 178 | $found = $q->get_compare( array( |
| | 179 | 'compare' => 'BETWEEN', |
| | 180 | ) ); |
| | 181 | $this->assertSame( 'BETWEEN', $found ); |
| | 182 | } |
| | 183 | |
| | 184 | public function test_validate_column_post_date() { |
| | 185 | $q = new WP_Date_Query( array() ); |
| | 186 | |
| | 187 | $this->assertSame( 'post_date', $q->validate_column( 'post_date' ) ); |
| | 188 | } |
| | 189 | |
| | 190 | public function test_validate_column_post_date_gmt() { |
| | 191 | $q = new WP_Date_Query( array() ); |
| | 192 | |
| | 193 | $this->assertSame( 'post_date_gmt', $q->validate_column( 'post_date_gmt' ) ); |
| | 194 | } |
| | 195 | |
| | 196 | public function test_validate_column_post_modified() { |
| | 197 | $q = new WP_Date_Query( array() ); |
| | 198 | |
| | 199 | $this->assertSame( 'post_modified', $q->validate_column( 'post_modified' ) ); |
| | 200 | } |
| | 201 | |
| | 202 | public function test_validate_column_post_modified_gmt() { |
| | 203 | $q = new WP_Date_Query( array() ); |
| | 204 | |
| | 205 | $this->assertSame( 'post_modified_gmt', $q->validate_column( 'post_modified_gmt' ) ); |
| | 206 | } |
| | 207 | |
| | 208 | public function test_validate_column_comment_date() { |
| | 209 | $q = new WP_Date_Query( array() ); |
| | 210 | |
| | 211 | $this->assertSame( 'comment_date', $q->validate_column( 'comment_date' ) ); |
| | 212 | } |
| | 213 | |
| | 214 | public function test_validate_column_comment_date_gmt() { |
| | 215 | $q = new WP_Date_Query( array() ); |
| | 216 | |
| | 217 | $this->assertSame( 'comment_date_gmt', $q->validate_column( 'comment_date_gmt' ) ); |
| | 218 | } |
| | 219 | |
| | 220 | public function test_validate_column_invalid() { |
| | 221 | $q = new WP_Date_Query( array() ); |
| | 222 | |
| | 223 | $this->assertSame( 'post_date', $q->validate_column( 'foo' ) ); |
| | 224 | } |
| | 225 | |
| | 226 | public function test_build_value_value_null() { |
| | 227 | $q = new WP_Date_Query( array() ); |
| | 228 | |
| | 229 | $this->assertFalse( $q->build_value( 'foo', null ) ); |
| | 230 | } |
| | 231 | |
| | 232 | public function test_build_value_compare_in() { |
| | 233 | $q = new WP_Date_Query( array() ); |
| | 234 | |
| | 235 | // Single integer |
| | 236 | $found = $q->build_value( 'IN', 4 ); |
| | 237 | $this->assertSame( '(4)', $found ); |
| | 238 | |
| | 239 | // Single non-integer |
| | 240 | $found = $q->build_value( 'IN', 'foo' ); |
| | 241 | $this->assertSame( '(0)', $found ); |
| | 242 | |
| | 243 | // Array of integers |
| | 244 | $found = $q->build_value( 'IN', array( 1, 4, 7 ) ); |
| | 245 | $this->assertSame( '(1,4,7)', $found ); |
| | 246 | |
| | 247 | // Array containing non-integers |
| | 248 | $found = $q->build_value( 'IN', array( 1, 'foo', 7 ) ); |
| | 249 | $this->assertSame( '(1,0,7)', $found ); |
| | 250 | } |
| | 251 | |
| | 252 | public function test_build_value_compare_not_in() { |
| | 253 | $q = new WP_Date_Query( array() ); |
| | 254 | |
| | 255 | // Single integer |
| | 256 | $found = $q->build_value( 'NOT IN', 4 ); |
| | 257 | $this->assertSame( '(4)', $found ); |
| | 258 | |
| | 259 | // Single non-integer |
| | 260 | $found = $q->build_value( 'NOT IN', 'foo' ); |
| | 261 | $this->assertSame( '(0)', $found ); |
| | 262 | |
| | 263 | // Array of integers |
| | 264 | $found = $q->build_value( 'NOT IN', array( 1, 4, 7 ) ); |
| | 265 | $this->assertSame( '(1,4,7)', $found ); |
| | 266 | |
| | 267 | // Array containing non-integers |
| | 268 | $found = $q->build_value( 'NOT IN', array( 1, 'foo', 7 ) ); |
| | 269 | $this->assertSame( '(1,0,7)', $found ); |
| | 270 | } |
| | 271 | |
| | 272 | public function test_build_value_compare_between_single_integer() { |
| | 273 | $q = new WP_Date_Query( array() ); |
| | 274 | |
| | 275 | $found = $q->build_value( 'BETWEEN', 4 ); |
| | 276 | $this->assertSame( '4 AND 4', $found ); |
| | 277 | } |
| | 278 | |
| | 279 | /** |
| | 280 | * @todo This is a bug. Should return false when non-numeric |
| | 281 | */ |
| | 282 | public function test_build_value_compare_between_single_non_numeric() { |
| | 283 | $q = new WP_Date_Query( array() ); |
| | 284 | |
| | 285 | $found = $q->build_value( 'BETWEEN', 'foo' ); |
| | 286 | $this->assertSame( '0 AND 0', $found ); |
| | 287 | } |
| | 288 | |
| | 289 | /** |
| | 290 | * @todo This is a bug |
| | 291 | */ |
| | 292 | public function test_build_value_compare_between_array_with_other_than_two_items() { |
| | 293 | $q = new WP_Date_Query( array() ); |
| | 294 | |
| | 295 | $found = $q->build_value( 'BETWEEN', array( 2, 3, 4 ) ); |
| | 296 | |
| | 297 | // array_map( 'intval', array( |
| | 298 | // array( 2, 3, 4 ), |
| | 299 | // array( 2, 3, 4 ), |
| | 300 | // ) ); |
| | 301 | $this->assertSame( '1 AND 1', $found ); |
| | 302 | } |
| | 303 | |
| | 304 | /** |
| | 305 | * @todo This is a bug |
| | 306 | */ |
| | 307 | public function test_build_value_compare_between_incorrect_array_key() { |
| | 308 | $q = new WP_Date_Query( array() ); |
| | 309 | |
| | 310 | $found = $q->build_value( 'BETWEEN', array( |
| | 311 | 2 => 4, |
| | 312 | 3 => 5, |
| | 313 | ) ); |
| | 314 | |
| | 315 | // array_map( 'intval', array( |
| | 316 | // array( 2 => 4, 3 => 5 ), |
| | 317 | // array( 2 => 4, 3 => 5 ), |
| | 318 | // ) ); |
| | 319 | $this->assertSame( '1 AND 1', $found ); |
| | 320 | } |
| | 321 | |
| | 322 | public function test_build_value_compare_between_array_contains_non_numeric() { |
| | 323 | $q = new WP_Date_Query( array() ); |
| | 324 | |
| | 325 | $found = $q->build_value( 'BETWEEN', array( 2, 'foo' ) ); |
| | 326 | $this->assertSame( '2 AND 0', $found ); |
| | 327 | } |
| | 328 | |
| | 329 | public function test_build_value_compare_between() { |
| | 330 | $q = new WP_Date_Query( array() ); |
| | 331 | |
| | 332 | $found = $q->build_value( 'BETWEEN', array( 2, 3 ) ); |
| | 333 | $this->assertSame( '2 AND 3', $found ); |
| | 334 | } |
| | 335 | |
| | 336 | public function test_build_value_compare_not_between_single_integer() { |
| | 337 | $q = new WP_Date_Query( array() ); |
| | 338 | |
| | 339 | $found = $q->build_value( 'NOT BETWEEN', 4 ); |
| | 340 | $this->assertSame( '4 AND 4', $found ); |
| | 341 | } |
| | 342 | |
| | 343 | public function test_build_value_compare_not_between_single_non_numeric() { |
| | 344 | $q = new WP_Date_Query( array() ); |
| | 345 | |
| | 346 | $found = $q->build_value( 'NOT BETWEEN', 'foo' ); |
| | 347 | $this->assertSame( '0 AND 0', $found ); |
| | 348 | } |
| | 349 | |
| | 350 | /** |
| | 351 | * @todo This is a bug |
| | 352 | */ |
| | 353 | public function test_build_value_compare_not_between_array_with_other_than_two_items() { |
| | 354 | $q = new WP_Date_Query( array() ); |
| | 355 | |
| | 356 | $found = $q->build_value( 'NOT BETWEEN', array( 2, 3, 4 ) ); |
| | 357 | |
| | 358 | // array_map( 'intval', array( |
| | 359 | // array( 2, 3, 4 ), |
| | 360 | // array( 2, 3, 4 ), |
| | 361 | // ) ); |
| | 362 | $this->assertSame( '1 AND 1', $found ); |
| | 363 | } |
| | 364 | |
| | 365 | /** |
| | 366 | * @todo This is a bug |
| | 367 | */ |
| | 368 | public function test_build_value_compare_not_between_incorrect_array_key() { |
| | 369 | $q = new WP_Date_Query( array() ); |
| | 370 | |
| | 371 | $found = $q->build_value( 'NOT BETWEEN', array( |
| | 372 | 2 => 4, |
| | 373 | 3 => 5, |
| | 374 | ) ); |
| | 375 | |
| | 376 | // array_map( 'intval', array( |
| | 377 | // array( 2 => 4, 3 => 5 ), |
| | 378 | // array( 2 => 4, 3 => 5 ), |
| | 379 | // ) ); |
| | 380 | $this->assertSame( '1 AND 1', $found ); |
| | 381 | } |
| | 382 | |
| | 383 | public function test_build_value_compare_not_between_array_contains_non_numeric() { |
| | 384 | $q = new WP_Date_Query( array() ); |
| | 385 | |
| | 386 | $found = $q->build_value( 'NOT BETWEEN', array( 2, 'foo' ) ); |
| | 387 | $this->assertSame( '2 AND 0', $found ); |
| | 388 | } |
| | 389 | |
| | 390 | public function test_build_value_compare_not_between() { |
| | 391 | $q = new WP_Date_Query( array() ); |
| | 392 | |
| | 393 | $found = $q->build_value( 'NOT BETWEEN', array( 2, 3 ) ); |
| | 394 | $this->assertSame( '2 AND 3', $found ); |
| | 395 | } |
| | 396 | |
| | 397 | public function test_build_value_compare_default_value_integer() { |
| | 398 | $q = new WP_Date_Query( array() ); |
| | 399 | |
| | 400 | $found = $q->build_value( 'foo', 5 ); |
| | 401 | $this->assertSame( 5, $found ); |
| | 402 | } |
| | 403 | |
| | 404 | /** |
| | 405 | * @todo This is probably a bug - ought to return false |
| | 406 | */ |
| | 407 | public function test_build_value_compare_default_value_non_numeric() { |
| | 408 | $q = new WP_Date_Query( array() ); |
| | 409 | |
| | 410 | $found = $q->build_value( 'foo', 'foo' ); |
| | 411 | $this->assertSame( 0, $found ); |
| | 412 | } |
| | 413 | |
| | 414 | public function test_build_mysql_datetime_datetime_non_array() { |
| | 415 | $q = new WP_Date_Query( array() ); |
| | 416 | |
| | 417 | // This might be a fragile test if it takes longer than 1 second to run |
| | 418 | $found = $q->build_mysql_datetime( 'foo' ); |
| | 419 | $expected = gmdate( 'Y-m-d H:i:s', strtotime( current_time( 'timestamp' ) ) ); |
| | 420 | $this->assertSame( $found, $expected ); |
| | 421 | } |
| | 422 | |
| | 423 | public function test_build_mysql_datetime_default_to_max_true() { |
| | 424 | $q = new WP_Date_Query( array() ); |
| | 425 | |
| | 426 | $found = $q->build_mysql_datetime( array( |
| | 427 | 'year' => 2011, |
| | 428 | ), true ); |
| | 429 | $this->assertSame( '2011-12-31 23:59:59', $found ); |
| | 430 | } |
| | 431 | |
| | 432 | public function test_build_mysql_datetime_default_to_max_false() { |
| | 433 | $q = new WP_Date_Query( array() ); |
| | 434 | |
| | 435 | $found = $q->build_mysql_datetime( array( |
| | 436 | 'year' => 2011, |
| | 437 | ), false ); |
| | 438 | $this->assertSame( '2011-01-01 00:00:00', $found ); |
| | 439 | } |
| | 440 | |
| | 441 | public function test_build_mysql_datetime_default_to_max_default_to_false() { |
| | 442 | $q = new WP_Date_Query( array() ); |
| | 443 | |
| | 444 | $found = $q->build_mysql_datetime( array( |
| | 445 | 'year' => 2011, |
| | 446 | ), false ); |
| | 447 | $this->assertSame( '2011-01-01 00:00:00', $found ); |
| | 448 | } |
| | 449 | |
| | 450 | public function test_build_time_query_insufficient_time_values() { |
| | 451 | $q = new WP_Date_Query( array() ); |
| | 452 | |
| | 453 | $this->assertFalse( $q->build_time_query( 'post_date', '=' ) ); |
| | 454 | } |
| | 455 | |
| | 456 | public function test_build_time_query_compare_in() { |
| | 457 | $q = new WP_Date_Query( array() ); |
| | 458 | |
| | 459 | // Just hour |
| | 460 | $found = $q->build_time_query( 'post_date', 'IN', array( 1, 2 ) ); |
| | 461 | $this->assertSame( 'HOUR( post_date ) IN (1,2)', $found ); |
| | 462 | |
| | 463 | // Skip minute |
| | 464 | $found = $q->build_time_query( 'post_date', 'IN', array( 1, 2 ), null, 6 ); |
| | 465 | $this->assertSame( 'HOUR( post_date ) IN (1,2) AND SECOND( post_date ) IN (6)', $found ); |
| | 466 | |
| | 467 | // All three |
| | 468 | $found = $q->build_time_query( 'post_date', 'IN', array( 1, 2 ), array( 3, 4, 5 ), 6 ); |
| | 469 | $this->assertSame( 'HOUR( post_date ) IN (1,2) AND MINUTE( post_date ) IN (3,4,5) AND SECOND( post_date ) IN (6)', $found ); |
| | 470 | } |
| | 471 | |
| | 472 | public function test_build_time_query_compare_not_in() { |
| | 473 | $q = new WP_Date_Query( array() ); |
| | 474 | |
| | 475 | // Just hour |
| | 476 | $found = $q->build_time_query( 'post_date', 'NOT IN', array( 1, 2 ) ); |
| | 477 | $this->assertSame( 'HOUR( post_date ) NOT IN (1,2)', $found ); |
| | 478 | |
| | 479 | // Skip minute |
| | 480 | $found = $q->build_time_query( 'post_date', 'NOT IN', array( 1, 2 ), null, 6 ); |
| | 481 | $this->assertSame( 'HOUR( post_date ) NOT IN (1,2) AND SECOND( post_date ) NOT IN (6)', $found ); |
| | 482 | |
| | 483 | // All three |
| | 484 | $found = $q->build_time_query( 'post_date', 'NOT IN', array( 1, 2 ), array( 3, 4, 5 ), 6 ); |
| | 485 | $this->assertSame( 'HOUR( post_date ) NOT IN (1,2) AND MINUTE( post_date ) NOT IN (3,4,5) AND SECOND( post_date ) NOT IN (6)', $found ); |
| | 486 | } |
| | 487 | |
| | 488 | public function test_build_time_query_compare_between() { |
| | 489 | $q = new WP_Date_Query( array() ); |
| | 490 | |
| | 491 | // Just hour |
| | 492 | $found = $q->build_time_query( 'post_date', 'BETWEEN', array( 1, 2 ) ); |
| | 493 | $this->assertSame( 'HOUR( post_date ) BETWEEN 1 AND 2', $found ); |
| | 494 | |
| | 495 | // Skip minute |
| | 496 | $found = $q->build_time_query( 'post_date', 'BETWEEN', array( 1, 2 ), null, array( 6, 7 ) ); |
| | 497 | $this->assertSame( 'HOUR( post_date ) BETWEEN 1 AND 2 AND SECOND( post_date ) BETWEEN 6 AND 7', $found ); |
| | 498 | |
| | 499 | // All three |
| | 500 | $found = $q->build_time_query( 'post_date', 'BETWEEN', array( 1, 2 ), array( 3, 4 ), array( 6, 7 ) ); |
| | 501 | $this->assertSame( 'HOUR( post_date ) BETWEEN 1 AND 2 AND MINUTE( post_date ) BETWEEN 3 AND 4 AND SECOND( post_date ) BETWEEN 6 AND 7', $found ); |
| | 502 | } |
| | 503 | |
| | 504 | public function test_build_time_query_compare_not_between() { |
| | 505 | $q = new WP_Date_Query( array() ); |
| | 506 | |
| | 507 | // Just hour |
| | 508 | $found = $q->build_time_query( 'post_date', 'NOT BETWEEN', array( 1, 2 ) ); |
| | 509 | $this->assertSame( 'HOUR( post_date ) NOT BETWEEN 1 AND 2', $found ); |
| | 510 | |
| | 511 | // Skip minute |
| | 512 | $found = $q->build_time_query( 'post_date', 'NOT BETWEEN', array( 1, 2 ), null, array( 6, 7 ) ); |
| | 513 | $this->assertSame( 'HOUR( post_date ) NOT BETWEEN 1 AND 2 AND SECOND( post_date ) NOT BETWEEN 6 AND 7', $found ); |
| | 514 | |
| | 515 | // All three |
| | 516 | $found = $q->build_time_query( 'post_date', 'NOT BETWEEN', array( 1, 2 ), array( 3, 4 ), array( 6, 7 ) ); |
| | 517 | $this->assertSame( 'HOUR( post_date ) NOT BETWEEN 1 AND 2 AND MINUTE( post_date ) NOT BETWEEN 3 AND 4 AND SECOND( post_date ) NOT BETWEEN 6 AND 7', $found ); |
| | 518 | } |
| | 519 | |
| | 520 | public function test_build_time_query_hour_only() { |
| | 521 | $q = new WP_Date_Query( array() ); |
| | 522 | |
| | 523 | $found = $q->build_time_query( 'post_date', '=', 5 ); |
| | 524 | $this->assertSame( 'HOUR( post_date ) = 5', $found ); |
| | 525 | } |
| | 526 | |
| | 527 | public function test_build_time_query_minute_only() { |
| | 528 | $q = new WP_Date_Query( array() ); |
| | 529 | |
| | 530 | $found = $q->build_time_query( 'post_date', '=', null, 5 ); |
| | 531 | $this->assertSame( 'MINUTE( post_date ) = 5', $found ); |
| | 532 | } |
| | 533 | |
| | 534 | public function test_build_time_query_second_only() { |
| | 535 | $q = new WP_Date_Query( array() ); |
| | 536 | |
| | 537 | $found = $q->build_time_query( 'post_date', '=', null, null, 5 ); |
| | 538 | $this->assertSame( 'SECOND( post_date ) = 5', $found ); |
| | 539 | } |
| | 540 | |
| | 541 | public function test_build_time_query_hour_and_second() { |
| | 542 | $q = new WP_Date_Query( array() ); |
| | 543 | |
| | 544 | $found = $q->build_time_query( 'post_date', '=', 5, null, 5 ); |
| | 545 | $this->assertFalse( $found ); |
| | 546 | } |
| | 547 | |
| | 548 | public function test_build_time_query_hour_minute() { |
| | 549 | $q = new WP_Date_Query( array() ); |
| | 550 | |
| | 551 | $found = $q->build_time_query( 'post_date', '=', 5, 15 ); |
| | 552 | |
| | 553 | // $compare value is floating point - use regex to account for |
| | 554 | // varying precision on different PHP installations |
| | 555 | $this->assertRegExp( "/DATE_FORMAT\( post_date, '%H\.%i' \) = 5\.150*/", $found ); |
| | 556 | } |
| | 557 | |
| | 558 | public function test_build_time_query_hour_minute_second() { |
| | 559 | $q = new WP_Date_Query( array() ); |
| | 560 | |
| | 561 | $found = $q->build_time_query( 'post_date', '=', 5, 15, 35 ); |
| | 562 | |
| | 563 | // $compare value is floating point - use regex to account for |
| | 564 | // varying precision on different PHP installations |
| | 565 | $this->assertRegExp( "/DATE_FORMAT\( post_date, '%H\.%i%s' \) = 5\.15350*/", $found ); |
| | 566 | } |
| | 567 | |
| | 568 | public function test_build_time_query_minute_second() { |
| | 569 | $q = new WP_Date_Query( array() ); |
| | 570 | |
| | 571 | $found = $q->build_time_query( 'post_date', '=', null, 15, 35 ); |
| | 572 | |
| | 573 | // $compare value is floating point - use regex to account for |
| | 574 | // varying precision on different PHP installations |
| | 575 | $this->assertRegExp( "/DATE_FORMAT\( post_date, '0\.%i%s' \) = 0\.15350*/", $found ); |
| | 576 | } |
| | 577 | } |