forked from WordPress/wordpress-develop
/
query.php
1136 lines (935 loc) · 28.7 KB
/
query.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
/**
* Tests for public methods of WP_Date_Query.
*
* See query/dateQuery.php for tests that require WP_Query.
*
* @group datequery
* @group date
*/
class Tests_WP_Date_Query extends WP_UnitTestCase {
public $q;
public function setUp(): void {
parent::setUp();
unset( $this->q );
$this->q = new WP_Date_Query( array( 'm' => 2 ) );
}
public function test_construct_date_query_empty() {
$q = new WP_Date_Query( array() );
$this->assertSame( 'AND', $q->relation );
$this->assertSame( 'post_date', $q->column );
$this->assertSame( '=', $q->compare );
$this->assertSame( array(), $q->queries );
}
public function test_construct_date_query_non_array() {
$q = new WP_Date_Query( 'foo' );
$this->assertSame( 'AND', $q->relation );
$this->assertSame( 'post_date', $q->column );
$this->assertSame( '=', $q->compare );
$this->assertSame( array(), $q->queries );
}
public function test_construct_relation_or_lowercase() {
$q = new WP_Date_Query(
array(
'relation' => 'or',
)
);
$this->assertSame( 'OR', $q->relation );
}
public function test_construct_relation_invalid() {
$q = new WP_Date_Query(
array(
'relation' => 'foo',
)
);
$this->assertSame( 'AND', $q->relation );
}
public function test_construct_query_not_an_array_of_arrays() {
$q = new WP_Date_Query(
array(
'before' => array(
'year' => 2008,
'month' => 6,
),
)
);
$expected = array(
0 => array(
'before' => array(
'year' => 2008,
'month' => 6,
),
'column' => 'post_date',
'compare' => '=',
'relation' => 'AND',
),
'column' => 'post_date',
'compare' => '=',
'relation' => 'AND',
);
$this->assertSame( $expected, $q->queries );
}
public function test_construct_query_contains_non_arrays() {
$q = new WP_Date_Query(
array(
'foo',
'bar',
array(
'before' => array(
'year' => 2008,
'month' => 6,
),
),
)
);
$expected = array(
array(
'before' => array(
'year' => 2008,
'month' => 6,
),
'column' => 'post_date',
'compare' => '=',
'relation' => 'AND',
),
'column' => 'post_date',
'compare' => '=',
'relation' => 'AND',
);
$this->assertEquals( $expected, $q->queries );
}
public function test_get_compare_empty() {
$q = new WP_Date_Query( array() );
$this->assertSame( '=', $q->get_compare( array() ) );
}
public function test_get_compare_equals() {
$q = new WP_Date_Query( array() );
$found = $q->get_compare(
array(
'compare' => '=',
)
);
$this->assertSame( '=', $found );
}
public function test_get_compare_not_equals() {
$q = new WP_Date_Query( array() );
$found = $q->get_compare(
array(
'compare' => '!=',
)
);
$this->assertSame( '!=', $found );
}
public function test_get_compare_greater_than() {
$q = new WP_Date_Query( array() );
$found = $q->get_compare(
array(
'compare' => '>',
)
);
$this->assertSame( '>', $found );
}
public function test_get_compare_greater_than_or_equal_to() {
$q = new WP_Date_Query( array() );
$found = $q->get_compare(
array(
'compare' => '>=',
)
);
$this->assertSame( '>=', $found );
}
public function test_get_compare_less_than() {
$q = new WP_Date_Query( array() );
$found = $q->get_compare(
array(
'compare' => '<',
)
);
$this->assertSame( '<', $found );
}
public function test_get_compare_less_than_or_equal_to() {
$q = new WP_Date_Query( array() );
$found = $q->get_compare(
array(
'compare' => '<=',
)
);
$this->assertSame( '<=', $found );
}
public function test_get_compare_in() {
$q = new WP_Date_Query( array() );
$found = $q->get_compare(
array(
'compare' => 'IN',
)
);
$this->assertSame( 'IN', $found );
}
public function test_get_compare_not_in() {
$q = new WP_Date_Query( array() );
$found = $q->get_compare(
array(
'compare' => 'NOT IN',
)
);
$this->assertSame( 'NOT IN', $found );
}
public function test_get_compare_between() {
$q = new WP_Date_Query( array() );
$found = $q->get_compare(
array(
'compare' => 'BETWEEN',
)
);
$this->assertSame( 'BETWEEN', $found );
}
public function test_get_compare_not_between() {
$q = new WP_Date_Query( array() );
$found = $q->get_compare(
array(
'compare' => 'BETWEEN',
)
);
$this->assertSame( 'BETWEEN', $found );
}
public function test_validate_column_post_date() {
global $wpdb;
$q = new WP_Date_Query( array() );
$this->assertSame( $wpdb->posts . '.post_date', $q->validate_column( 'post_date' ) );
}
public function test_validate_column_post_date_gmt() {
global $wpdb;
$q = new WP_Date_Query( array() );
$this->assertSame( $wpdb->posts . '.post_date_gmt', $q->validate_column( 'post_date_gmt' ) );
}
public function test_validate_column_post_modified() {
global $wpdb;
$q = new WP_Date_Query( array() );
$this->assertSame( $wpdb->posts . '.post_modified', $q->validate_column( 'post_modified' ) );
}
public function test_validate_column_post_modified_gmt() {
global $wpdb;
$q = new WP_Date_Query( array() );
$this->assertSame( $wpdb->posts . '.post_modified_gmt', $q->validate_column( 'post_modified_gmt' ) );
}
public function test_validate_column_comment_date() {
global $wpdb;
$q = new WP_Date_Query( array() );
$this->assertSame( $wpdb->comments . '.comment_date', $q->validate_column( 'comment_date' ) );
}
public function test_validate_column_comment_date_gmt() {
global $wpdb;
$q = new WP_Date_Query( array() );
$this->assertSame( $wpdb->comments . '.comment_date_gmt', $q->validate_column( 'comment_date_gmt' ) );
}
public function test_validate_column_invalid() {
global $wpdb;
$q = new WP_Date_Query( array() );
$this->assertSame( $wpdb->posts . '.post_date', $q->validate_column( 'foo' ) );
}
/**
* @ticket 25775
*/
public function test_validate_column_with_date_query_valid_columns_filter() {
$q = new WP_Date_Query( array() );
add_filter( 'date_query_valid_columns', array( $this, 'date_query_valid_columns_callback' ) );
$this->assertSame( 'my_custom_column', $q->validate_column( 'my_custom_column' ) );
remove_filter( 'date_query_valid_columns', array( $this, 'date_query_valid_columns_callback' ) );
}
/**
* @ticket 25775
*/
public function test_validate_column_prefixed_column_name() {
$q = new WP_Date_Query( array() );
$this->assertSame( 'foo.bar', $q->validate_column( 'foo.bar' ) );
}
/**
* @ticket 25775
*/
public function test_validate_column_prefixed_column_name_with_illegal_characters() {
$q = new WP_Date_Query( array() );
$this->assertSame( 'foo.bar', $q->validate_column( 'f"\'oo\/.b;:()ar' ) );
}
public function test_build_value_value_null() {
global $wpdb;
$q = new WP_Date_Query( array() );
$this->assertFalse( $q->build_value( 'foo', null ) );
}
/**
* @ticket 29801
*/
public function test_build_value_compare_in() {
$q = new WP_Date_Query( array() );
// Single integer.
$found = $q->build_value( 'IN', 4 );
$this->assertSame( '(4)', $found );
// Single non-integer.
$found = $q->build_value( 'IN', 'foo' );
$this->assertFalse( $found );
// Array of integers.
$found = $q->build_value( 'IN', array( 1, 4, 7 ) );
$this->assertSame( '(1,4,7)', $found );
// Array containing non-integers.
$found = $q->build_value( 'IN', array( 1, 'foo', 7 ) );
$this->assertSame( '(1,7)', $found );
}
/**
* @ticket 29801
*/
public function test_build_value_compare_not_in() {
$q = new WP_Date_Query( array() );
// Single integer.
$found = $q->build_value( 'NOT IN', 4 );
$this->assertSame( '(4)', $found );
// Single non-integer.
$found = $q->build_value( 'NOT IN', 'foo' );
$this->assertFalse( $found );
// Array of integers.
$found = $q->build_value( 'NOT IN', array( 1, 4, 7 ) );
$this->assertSame( '(1,4,7)', $found );
// Array containing non-integers.
$found = $q->build_value( 'NOT IN', array( 1, 'foo', 7 ) );
$this->assertSame( '(1,7)', $found );
}
public function test_build_value_compare_between_single_integer() {
$q = new WP_Date_Query( array() );
$found = $q->build_value( 'BETWEEN', 4 );
$this->assertSame( '4 AND 4', $found );
}
/**
* @ticket 29801
*/
public function test_build_value_compare_between_single_non_numeric() {
$q = new WP_Date_Query( array() );
$found = $q->build_value( 'BETWEEN', 'foo' );
$this->assertFalse( $found );
}
/**
* @ticket 29801
*/
public function test_build_value_compare_between_array_with_other_than_two_items() {
$q = new WP_Date_Query( array() );
$found = $q->build_value( 'BETWEEN', array( 2, 3, 4 ) );
$this->assertFalse( $found );
}
/**
* @ticket 29801
*/
public function test_build_value_compare_between_incorrect_array_key() {
$q = new WP_Date_Query( array() );
$found = $q->build_value(
'BETWEEN',
array(
2 => 4,
3 => 5,
)
);
$this->assertSame( '4 AND 5', $found );
}
/**
* @ticket 29801
*/
public function test_build_value_compare_between_array_contains_non_numeric() {
$q = new WP_Date_Query( array() );
$found = $q->build_value( 'BETWEEN', array( 2, 'foo' ) );
$this->assertFalse( $found );
}
public function test_build_value_compare_between() {
$q = new WP_Date_Query( array() );
$found = $q->build_value( 'BETWEEN', array( 2, 3 ) );
$this->assertSame( '2 AND 3', $found );
}
public function test_build_value_compare_not_between_single_integer() {
$q = new WP_Date_Query( array() );
$found = $q->build_value( 'NOT BETWEEN', 4 );
$this->assertSame( '4 AND 4', $found );
}
/**
* @ticket 29801
*/
public function test_build_value_compare_not_between_single_non_numeric() {
$q = new WP_Date_Query( array() );
$found = $q->build_value( 'NOT BETWEEN', 'foo' );
$this->assertFalse( $found );
}
/**
* @ticket 29801
*/
public function test_build_value_compare_not_between_array_with_other_than_two_items() {
$q = new WP_Date_Query( array() );
$found = $q->build_value( 'NOT BETWEEN', array( 2, 3, 4 ) );
$this->assertFalse( $found );
}
/**
* @ticket 29801
*/
public function test_build_value_compare_not_between_incorrect_array_key() {
$q = new WP_Date_Query( array() );
$found = $q->build_value(
'NOT BETWEEN',
array(
2 => 4,
3 => 5,
)
);
$this->assertSame( '4 AND 5', $found );
}
/**
* @ticket 29801
*/
public function test_build_value_compare_not_between_array_contains_non_numeric() {
$q = new WP_Date_Query( array() );
$found = $q->build_value( 'NOT BETWEEN', array( 2, 'foo' ) );
$this->assertFalse( $found );
}
public function test_build_value_compare_not_between() {
$q = new WP_Date_Query( array() );
$found = $q->build_value( 'NOT BETWEEN', array( 2, 3 ) );
$this->assertSame( '2 AND 3', $found );
}
public function test_build_value_compare_default_value_integer() {
$q = new WP_Date_Query( array() );
$found = $q->build_value( 'foo', 5 );
$this->assertSame( 5, $found );
}
/**
* @ticket 29801
*/
public function test_build_value_compare_default_value_non_numeric() {
$q = new WP_Date_Query( array() );
$found = $q->build_value( 'foo', 'foo' );
$this->assertFalse( $found );
}
public function test_build_mysql_datetime_datetime_non_array() {
$q = new WP_Date_Query( array() );
// This might be a fragile test if it takes longer than 1 second to run.
$found = $q->build_mysql_datetime( 'foo' );
$expected = gmdate( 'Y-m-d H:i:s', false );
$this->assertSame( $expected, $found );
}
/**
* @ticket 41782
*
* @dataProvider mysql_datetime_input_provider
*
* @param array|string $datetime Array or string date input.
* @param string $expected Expected built result.
* @param bool $default_to_max Flag to default missing values to max.
*/
public function test_build_mysql_datetime( $datetime, $expected, $default_to_max = false ) {
$q = new WP_Date_Query( array() );
$found = $q->build_mysql_datetime( $datetime, $default_to_max );
$message = "Expected {$expected}, got {$found}";
$this->assertEqualsWithDelta( strtotime( $expected ), strtotime( $found ), 10, $message );
}
public function mysql_datetime_input_provider() {
return array(
array( '2019-06-04T08:18:24+03:00', '2019-06-04 05:18:24' ),
array( '2019-06-04T05:18:24+00:00', '2019-06-04 05:18:24' ),
array( array(), current_time( 'Y' ) . '-01-01 00:00:00' ),
array( array( 'year' => 2011 ), '2011-12-31 23:59:59', true ),
array( array( 'year' => 2011 ), '2011-01-01 00:00:00' ),
array( '2011', '2011-01-01 00:00:00' ),
array( '2011-02', '2011-02-01 00:00:00' ),
array( '2011-02-03', '2011-02-03 00:00:00' ),
array( '2011-02-03 13:30', '2011-02-03 13:30:00' ),
array( '2011-02-03 13:30:35', '2011-02-03 13:30:35' ),
);
}
/**
* @ticket 41782
*
* @dataProvider mysql_datetime_input_provider_custom_timezone
*
* @param array|string $datetime Array or string date input.
* @param string $expected Expected built result.
* @param bool $default_to_max Flag to default missing values to max.
*/
public function test_build_mysql_datetime_with_custom_timezone( $datetime, $expected, $default_to_max = false ) {
update_option( 'timezone_string', 'Europe/Kiev' );
$q = new WP_Date_Query( array() );
$found = $q->build_mysql_datetime( $datetime, $default_to_max );
$message = "Expected {$expected}, got {$found}";
$this->assertEqualsWithDelta( strtotime( $expected ), strtotime( $found ), 10, $message );
}
public function mysql_datetime_input_provider_custom_timezone() {
return array(
array( '2019-06-04T08:18:24+03:00', '2019-06-04 08:18:24' ),
array( '2019-06-04T05:18:24+00:00', '2019-06-04 08:18:24' ),
);
}
/**
* @ticket 41782
*/
public function test_build_mysql_datetime_with_relative_date() {
update_option( 'timezone_string', 'Europe/Kiev' );
$q = new WP_Date_Query( array() );
$yesterday = new DateTimeImmutable( '-1 day', wp_timezone() );
$expected = $yesterday->format( 'Y-m-d H:i:s' );
$found = $q->build_mysql_datetime( '-1 day' );
$message = "Expected {$expected}, got {$found}";
$this->assertEqualsWithDelta( strtotime( $expected ), strtotime( $found ), 10, $message );
}
public function test_build_time_query_insufficient_time_values() {
$q = new WP_Date_Query( array() );
$this->assertFalse( $q->build_time_query( 'post_date', '=' ) );
}
/**
* @ticket 34228
*/
public function test_build_time_query_should_not_discard_hour_0() {
global $wpdb;
$q = new WP_Date_Query( array() );
$found = $q->build_time_query( 'post_date', '=', 0, 10 );
$this->assertStringContainsString( '%H', $wpdb->remove_placeholder_escape( $found ) );
}
public function test_build_time_query_compare_in() {
$q = new WP_Date_Query( array() );
// Just hour.
$found = $q->build_time_query( 'post_date', 'IN', array( 1, 2 ) );
$this->assertSame( 'HOUR( post_date ) IN (1,2)', $found );
// Skip minute.
$found = $q->build_time_query( 'post_date', 'IN', array( 1, 2 ), null, 6 );
$this->assertSame( 'HOUR( post_date ) IN (1,2) AND SECOND( post_date ) IN (6)', $found );
// All three.
$found = $q->build_time_query( 'post_date', 'IN', array( 1, 2 ), array( 3, 4, 5 ), 6 );
$this->assertSame( 'HOUR( post_date ) IN (1,2) AND MINUTE( post_date ) IN (3,4,5) AND SECOND( post_date ) IN (6)', $found );
}
public function test_build_time_query_compare_not_in() {
$q = new WP_Date_Query( array() );
// Just hour.
$found = $q->build_time_query( 'post_date', 'NOT IN', array( 1, 2 ) );
$this->assertSame( 'HOUR( post_date ) NOT IN (1,2)', $found );
// Skip minute.
$found = $q->build_time_query( 'post_date', 'NOT IN', array( 1, 2 ), null, 6 );
$this->assertSame( 'HOUR( post_date ) NOT IN (1,2) AND SECOND( post_date ) NOT IN (6)', $found );
// All three.
$found = $q->build_time_query( 'post_date', 'NOT IN', array( 1, 2 ), array( 3, 4, 5 ), 6 );
$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 );
}
public function test_build_time_query_compare_between() {
$q = new WP_Date_Query( array() );
// Just hour.
$found = $q->build_time_query( 'post_date', 'BETWEEN', array( 1, 2 ) );
$this->assertSame( 'HOUR( post_date ) BETWEEN 1 AND 2', $found );
// Skip minute.
$found = $q->build_time_query( 'post_date', 'BETWEEN', array( 1, 2 ), null, array( 6, 7 ) );
$this->assertSame( 'HOUR( post_date ) BETWEEN 1 AND 2 AND SECOND( post_date ) BETWEEN 6 AND 7', $found );
// All three.
$found = $q->build_time_query( 'post_date', 'BETWEEN', array( 1, 2 ), array( 3, 4 ), array( 6, 7 ) );
$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 );
}
public function test_build_time_query_compare_not_between() {
$q = new WP_Date_Query( array() );
// Just hour.
$found = $q->build_time_query( 'post_date', 'NOT BETWEEN', array( 1, 2 ) );
$this->assertSame( 'HOUR( post_date ) NOT BETWEEN 1 AND 2', $found );
// Skip minute.
$found = $q->build_time_query( 'post_date', 'NOT BETWEEN', array( 1, 2 ), null, array( 6, 7 ) );
$this->assertSame( 'HOUR( post_date ) NOT BETWEEN 1 AND 2 AND SECOND( post_date ) NOT BETWEEN 6 AND 7', $found );
// All three.
$found = $q->build_time_query( 'post_date', 'NOT BETWEEN', array( 1, 2 ), array( 3, 4 ), array( 6, 7 ) );
$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 );
}
public function test_build_time_query_hour_only() {
$q = new WP_Date_Query( array() );
$found = $q->build_time_query( 'post_date', '=', 5 );
$this->assertSame( 'HOUR( post_date ) = 5', $found );
}
public function test_build_time_query_minute_only() {
$q = new WP_Date_Query( array() );
$found = $q->build_time_query( 'post_date', '=', null, 5 );
$this->assertSame( 'MINUTE( post_date ) = 5', $found );
}
public function test_build_time_query_second_only() {
$q = new WP_Date_Query( array() );
$found = $q->build_time_query( 'post_date', '=', null, null, 5 );
$this->assertSame( 'SECOND( post_date ) = 5', $found );
}
public function test_build_time_query_hour_and_second() {
$q = new WP_Date_Query( array() );
$found = $q->build_time_query( 'post_date', '=', 5, null, 5 );
$this->assertFalse( $found );
}
public function test_build_time_query_hour_minute() {
global $wpdb;
$q = new WP_Date_Query( array() );
$found = $q->build_time_query( 'post_date', '=', 5, 15 );
// $compare value is floating point - use regex to account for
// varying precision on different PHP installations.
$this->assertRegExp( "/DATE_FORMAT\( post_date, '%H\.%i' \) = 5\.150*/", $wpdb->remove_placeholder_escape( $found ) );
}
public function test_build_time_query_hour_minute_second() {
global $wpdb;
$q = new WP_Date_Query( array() );
$found = $q->build_time_query( 'post_date', '=', 5, 15, 35 );
// $compare value is floating point - use regex to account for
// varying precision on different PHP installations.
$this->assertRegExp( "/DATE_FORMAT\( post_date, '%H\.%i%s' \) = 5\.15350*/", $wpdb->remove_placeholder_escape( $found ) );
}
public function test_build_time_query_minute_second() {
global $wpdb;
$q = new WP_Date_Query( array() );
$found = $q->build_time_query( 'post_date', '=', null, 15, 35 );
// $compare value is floating point - use regex to account for
// varying precision on different PHP installations.
$this->assertRegExp( "/DATE_FORMAT\( post_date, '0\.%i%s' \) = 0\.15350*/", $wpdb->remove_placeholder_escape( $found ) );
}
/**
* @ticket 25834
* @expectedIncorrectUsage WP_Date_Query
*/
public function test_validate_date_query_before_after() {
// Valid values.
$valid_args = array(
array(
'month' => 2,
'year' => 2014,
),
array(
'day' => 8,
'year' => 2014,
),
);
foreach ( $valid_args as $args ) {
$this->assertTrue( $this->q->validate_date_values( array( 'before' => $args ) ) );
$this->assertTrue( $this->q->validate_date_values( array( 'after' => $args ) ) );
}
// Invalid values.
$invalid_args = array(
array(
'month' => 13,
),
array(
'day' => 32,
),
array(
'minute' => 60,
),
array(
'second' => 60,
),
array(
'week' => 54,
),
);
foreach ( $invalid_args as $args ) {
$this->assertFalse( $this->q->validate_date_values( array( 'before' => $args ) ) );
$this->assertFalse( $this->q->validate_date_values( array( 'after' => $args ) ) );
}
}
/**
* @ticket 25834
* @expectedIncorrectUsage WP_Date_Query
*/
public function test_validate_date_query_before_after_with_month() {
// Both are valid.
$args = array(
'before' => array(
'month' => 2,
'year' => 2014,
),
'month' => 10,
);
$this->assertTrue( $this->q->validate_date_values( $args ) );
// 'before' is invalid, 'month' is valid.
$args = array(
'before' => array(
'month' => 13,
'year' => 2014,
),
'month' => 10,
);
$this->assertFalse( $this->q->validate_date_values( $args ) );
// 'before' is valid, 'month' is invalid.
$args = array(
'before' => array(
'month' => 10,
'year' => 2014,
),
'month' => 14,
);
$this->assertFalse( $this->q->validate_date_values( $args ) );
// Both are invalid.
$args = array(
'before' => array(
'month' => 14,
'year' => 2014,
),
'month' => 14,
);
$this->assertFalse( $this->q->validate_date_values( $args ) );
}
/**
* @ticket 25834
* @expectedIncorrectUsage WP_Date_Query
*/
public function test_validate_date_values_week() {
// Valid values.
$weeks = range( 1, 53 );
foreach ( $weeks as $week ) {
$this->assertTrue( $this->q->validate_date_values( array( 'week' => $week ) ) );
}
// Invalid values.
$weeks = array( -1, 0, 54 );
foreach ( $weeks as $week ) {
$this->assertFalse( $this->q->validate_date_values( array( 'week' => $week ) ) );
}
// Valid combinations.
$weeks = array(
array(
'week' => 52,
'year' => 2012,
),
array(
'week' => 53,
'year' => 2009,
),
);
foreach ( $weeks as $week_args ) {
$this->assertTrue( $this->q->validate_date_values( $week_args ) );
}
// Invalid combinations.
$weeks = array(
// 2012 has 52 weeks.
array(
'week' => 53,
'year' => 2012,
),
// 2013 has 53 weeks.
array(
'week' => 54,
'year' => 2009,
),
);
foreach ( $weeks as $week_args ) {
$this->assertFalse( $this->q->validate_date_values( $week_args ) );
}
}
/**
* @ticket 25834
* @expectedIncorrectUsage WP_Date_Query
*/
public function test_validate_date_values_month() {
// Valid values.
$months = range( 1, 12 );
foreach ( $months as $month ) {
$this->assertTrue( $this->q->validate_date_values( array( 'month' => $month ) ) );
}
// Invalid values.
$months = array( -1, 0, 13, 'string who wants to be a int' );
foreach ( $months as $month ) {
$this->assertFalse( $this->q->validate_date_values( array( 'month' => $month ) ) );
}
}
/**
* @ticket 25834
* @expectedIncorrectUsage WP_Date_Query
*/
public function test_validate_date_values_day() {
// Valid values.
$days = range( 1, 31 );
foreach ( $days as $day ) {
$this->assertTrue( $this->q->validate_date_values( array( 'day' => $day ) ) );
}
// Invalid values.
$days = array( -1, 32 );
foreach ( $days as $day ) {
$this->assertFalse( $this->q->validate_date_values( array( 'day' => $day ) ) );
}
// Valid combinations.
$days = array(
array(
'day' => 29,
'month' => 2,
'year' => 2008,
),
array(
'day' => 28,
'month' => 2,
'year' => 2009,
),
);
foreach ( $days as $args ) {
$this->assertTrue( $this->q->validate_date_values( $args ) );
}
// Invalid combinations.
$days = array(
// February 2008 has 29 days.
array(
'day' => 30,
'month' => 2,
'year' => 2008,
),
// February 2009 has 29 days.
array(
'day' => 29,
'month' => 2,
'year' => 2009,
),
);
foreach ( $days as $args ) {
$this->assertFalse( $this->q->validate_date_values( $args ) );
}
}
/**
* @ticket 25834
* @expectedIncorrectUsage WP_Date_Query
*/
public function test_validate_date_values_hour() {
// Valid values.
$hours = range( 0, 23 );
foreach ( $hours as $hour ) {
$this->assertTrue( $this->q->validate_date_values( array( 'hour' => $hour ) ) );
}
// Invalid values.
$hours = array( -1, 24, 25, 'string' );
foreach ( $hours as $hour ) {
$this->assertFalse( $this->q->validate_date_values( array( 'hour' => $hour ) ) );
}
}
/**
* @ticket 25834
* @expectedIncorrectUsage WP_Date_Query
*/
public function test_validate_date_values_minute() {
// Valid values.
$minutes = range( 0, 59 );
foreach ( $minutes as $minute ) {
$this->assertTrue( $this->q->validate_date_values( array( 'minute' => $minute ) ) );
}
// Invalid values.
$minutes = array( -1, 60 );
foreach ( $minutes as $minute ) {
$this->assertFalse( $this->q->validate_date_values( array( 'minute' => $minute ) ) );
}
}
/**
* @ticket 25834
* @expectedIncorrectUsage WP_Date_Query
*/
public function test_validate_date_values_second() {
// Valid values.
$seconds = range( 0, 59 );
foreach ( $seconds as $second ) {
$this->assertTrue( $this->q->validate_date_values( array( 'second' => $second ) ) );
}
// Invalid values.