Tests_WP_Date_Query::setUp()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 4
nc 1
nop 0
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
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
{
13
    public $q;
14
15
    public function setUp() 
0 ignored issues
show
Coding Style introduced by
The function name setUp is in camel caps, but expected set_up instead as per the coding standard.
Loading history...
16
    {
17
        parent::setUp();
18
        unset($this->q);
19
        $this->q = new WP_Date_Query(array( 'm' => 2 ));
20
    }
21
22 View Code Duplication
    public function test_construct_date_query_empty() 
23
    {
24
        $q = new WP_Date_Query(array());
25
        $this->assertSame('AND', $q->relation);
26
        $this->assertSame('post_date', $q->column);
27
        $this->assertSame('=', $q->compare);
28
        $this->assertSame(array(), $q->queries);
29
    }
30
31 View Code Duplication
    public function test_construct_date_query_non_array() 
32
    {
33
        $q = new WP_Date_Query('foo');
34
        $this->assertSame('AND', $q->relation);
35
        $this->assertSame('post_date', $q->column);
36
        $this->assertSame('=', $q->compare);
37
        $this->assertSame(array(), $q->queries);
38
    }
39
40
    public function test_construct_relation_or_lowercase() 
41
    {
42
        $q = new WP_Date_Query(
43
            array(
44
            'relation' => 'or',
45
            ) 
46
        );
47
48
        $this->assertSame('OR', $q->relation);
49
    }
50
51
    public function test_construct_relation_invalid() 
52
    {
53
        $q = new WP_Date_Query(
54
            array(
55
            'relation' => 'foo',
56
            ) 
57
        );
58
59
        $this->assertSame('AND', $q->relation);
60
    }
61
62
    public function test_construct_query_not_an_array_of_arrays() 
63
    {
64
        $q = new WP_Date_Query(
65
            array(
66
            'before' => array(
67
            'year' => 2008,
68
            'month' => 6,
69
            ),
70
            ) 
71
        );
72
73
        $expected = array(
74
         0 => array(
75
          'before' => array(
76
        'year' => 2008,
77
        'month' => 6,
78
          ),
79
          'column' => 'post_date',
80
          'compare' => '=',
81
          'relation' => 'AND',
82
         ),
83
         'column' => 'post_date',
84
         'compare' => '=',
85
         'relation' => 'AND',
86
        );
87
88
        $this->assertSame($expected, $q->queries);
89
    }
90
91
    public function test_construct_query_contains_non_arrays() 
92
    {
93
        $q = new WP_Date_Query(
94
            array(
95
            'foo',
96
            'bar',
97
            array(
98
            'before' => array(
99
            'year' => 2008,
100
            'month' => 6,
101
            ),
102
            ),
103
            ) 
104
        );
105
106
        $expected = array(
107
         array(
108
          'before' => array(
109
        'year' => 2008,
110
        'month' => 6,
111
          ),
112
          'column' => 'post_date',
113
          'compare' => '=',
114
          'relation' => 'AND',
115
         ),
116
         'column' => 'post_date',
117
         'compare' => '=',
118
         'relation' => 'AND',
119
        );
120
121
        $this->assertEquals($expected, $q->queries);
122
    }
123
124
    public function test_get_compare_empty() 
125
    {
126
        $q = new WP_Date_Query(array());
127
        $this->assertSame('=', $q->get_compare(array()));
128
    }
129
130 View Code Duplication
    public function test_get_compare_equals() 
131
    {
132
        $q = new WP_Date_Query(array());
133
134
        $found = $q->get_compare(
135
            array(
136
            'compare' => '=',
137
            ) 
138
        );
139
        $this->assertSame('=', $found);
140
    }
141
142 View Code Duplication
    public function test_get_compare_not_equals() 
143
    {
144
        $q = new WP_Date_Query(array());
145
146
        $found = $q->get_compare(
147
            array(
148
            'compare' => '!=',
149
            ) 
150
        );
151
        $this->assertSame('!=', $found);
152
    }
153
154 View Code Duplication
    public function test_get_compare_greater_than() 
155
    {
156
        $q = new WP_Date_Query(array());
157
158
        $found = $q->get_compare(
159
            array(
160
            'compare' => '>',
161
            ) 
162
        );
163
        $this->assertSame('>', $found);
164
    }
165
166 View Code Duplication
    public function test_get_compare_greater_than_or_equal_to() 
167
    {
168
        $q = new WP_Date_Query(array());
169
170
        $found = $q->get_compare(
171
            array(
172
            'compare' => '>=',
173
            ) 
174
        );
175
        $this->assertSame('>=', $found);
176
    }
177
178 View Code Duplication
    public function test_get_compare_less_than() 
179
    {
180
        $q = new WP_Date_Query(array());
181
182
        $found = $q->get_compare(
183
            array(
184
            'compare' => '<',
185
            ) 
186
        );
187
        $this->assertSame('<', $found);
188
    }
189
190 View Code Duplication
    public function test_get_compare_less_than_or_equal_to() 
191
    {
192
        $q = new WP_Date_Query(array());
193
194
        $found = $q->get_compare(
195
            array(
196
            'compare' => '<=',
197
            ) 
198
        );
199
        $this->assertSame('<=', $found);
200
    }
201
202 View Code Duplication
    public function test_get_compare_in() 
203
    {
204
        $q = new WP_Date_Query(array());
205
206
        $found = $q->get_compare(
207
            array(
208
            'compare' => 'IN',
209
            ) 
210
        );
211
        $this->assertSame('IN', $found);
212
    }
213
214 View Code Duplication
    public function test_get_compare_not_in() 
215
    {
216
        $q = new WP_Date_Query(array());
217
218
        $found = $q->get_compare(
219
            array(
220
            'compare' => 'NOT IN',
221
            ) 
222
        );
223
        $this->assertSame('NOT IN', $found);
224
    }
225
226 View Code Duplication
    public function test_get_compare_between() 
227
    {
228
        $q = new WP_Date_Query(array());
229
230
        $found = $q->get_compare(
231
            array(
232
            'compare' => 'BETWEEN',
233
            ) 
234
        );
235
        $this->assertSame('BETWEEN', $found);
236
    }
237
238 View Code Duplication
    public function test_get_compare_not_between() 
239
    {
240
        $q = new WP_Date_Query(array());
241
242
        $found = $q->get_compare(
243
            array(
244
            'compare' => 'BETWEEN',
245
            ) 
246
        );
247
        $this->assertSame('BETWEEN', $found);
248
    }
249
250 View Code Duplication
    public function test_validate_column_post_date() 
251
    {
252
        global $wpdb;
253
        $q = new WP_Date_Query(array());
254
255
        $this->assertSame($wpdb->posts . '.post_date', $q->validate_column('post_date'));
256
    }
257
258
    public function test_validate_column_post_date_gmt() 
259
    {
260
        global $wpdb;
261
        $q = new WP_Date_Query(array());
262
263
        $this->assertSame($wpdb->posts . '.post_date_gmt', $q->validate_column('post_date_gmt'));
264
    }
265
266
    public function test_validate_column_post_modified() 
267
    {
268
        global $wpdb;
269
        $q = new WP_Date_Query(array());
270
271
        $this->assertSame($wpdb->posts . '.post_modified', $q->validate_column('post_modified'));
272
    }
273
274
    public function test_validate_column_post_modified_gmt() 
275
    {
276
        global $wpdb;
277
        $q = new WP_Date_Query(array());
278
279
        $this->assertSame($wpdb->posts . '.post_modified_gmt', $q->validate_column('post_modified_gmt'));
280
    }
281
282
    public function test_validate_column_comment_date() 
283
    {
284
        global $wpdb;
285
        $q = new WP_Date_Query(array());
286
287
        $this->assertSame($wpdb->comments . '.comment_date', $q->validate_column('comment_date'));
288
    }
289
290
    public function test_validate_column_comment_date_gmt() 
291
    {
292
        global $wpdb;
293
        $q = new WP_Date_Query(array());
294
295
        $this->assertSame($wpdb->comments . '.comment_date_gmt', $q->validate_column('comment_date_gmt'));
296
    }
297
298 View Code Duplication
    public function test_validate_column_invalid() 
299
    {
300
        global $wpdb;
301
        $q = new WP_Date_Query(array());
302
303
        $this->assertSame($wpdb->posts . '.post_date', $q->validate_column('foo'));
304
    }
305
306
    /**
307
     * @ticket 25775
308
     */
309
    public function test_validate_column_with_date_query_valid_columns_filter() 
310
    {
311
        $q = new WP_Date_Query(array());
312
313
        add_filter('date_query_valid_columns', array( $this, 'date_query_valid_columns_callback' ));
314
315
        $this->assertSame('my_custom_column', $q->validate_column('my_custom_column'));
316
317
        remove_filter('date_query_valid_columns', array( $this, 'date_query_valid_columns_callback' ));
318
    }
319
320
    /**
321
     * @ticket 25775
322
     */
323
    public function test_validate_column_prefixed_column_name() 
324
    {
325
        $q = new WP_Date_Query(array());
326
327
        $this->assertSame('foo.bar', $q->validate_column('foo.bar'));
328
    }
329
330
    /**
331
     * @ticket 25775
332
     */
333
    public function test_validate_column_prefixed_column_name_with_illegal_characters() 
334
    {
335
        $q = new WP_Date_Query(array());
336
337
        $this->assertSame('foo.bar', $q->validate_column('f"\'oo\/.b;:()ar'));
338
    }
339
340
    public function test_build_value_value_null() 
341
    {
342
        global $wpdb;
343
        $q = new WP_Date_Query(array());
344
345
        $this->assertFalse($q->build_value('foo', null));
346
    }
347
348
    /**
349
     * @ticket 29801
350
     */
351 View Code Duplication
    public function test_build_value_compare_in() 
352
    {
353
        $q = new WP_Date_Query(array());
354
355
        // Single integer
356
        $found = $q->build_value('IN', 4);
357
        $this->assertSame('(4)', $found);
358
359
        // Single non-integer
360
        $found = $q->build_value('IN', 'foo');
361
        $this->assertFalse($found);
362
363
        // Array of integers
364
        $found = $q->build_value('IN', array( 1, 4, 7 ));
365
        $this->assertSame('(1,4,7)', $found);
366
367
        // Array containing non-integers
368
        $found = $q->build_value('IN', array( 1, 'foo', 7 ));
369
        $this->assertSame('(1,7)', $found);
370
    }
371
372
    /**
373
     * @ticket 29801
374
     */
375 View Code Duplication
    public function test_build_value_compare_not_in() 
376
    {
377
        $q = new WP_Date_Query(array());
378
379
        // Single integer
380
        $found = $q->build_value('NOT IN', 4);
381
        $this->assertSame('(4)', $found);
382
383
        // Single non-integer
384
        $found = $q->build_value('NOT IN', 'foo');
385
        $this->assertFalse($found);
386
387
        // Array of integers
388
        $found = $q->build_value('NOT IN', array( 1, 4, 7 ));
389
        $this->assertSame('(1,4,7)', $found);
390
391
        // Array containing non-integers
392
        $found = $q->build_value('NOT IN', array( 1, 'foo', 7 ));
393
        $this->assertSame('(1,7)', $found);
394
    }
395
396
    public function test_build_value_compare_between_single_integer() 
397
    {
398
        $q = new WP_Date_Query(array());
399
400
        $found = $q->build_value('BETWEEN', 4);
401
        $this->assertSame('4 AND 4', $found);
402
    }
403
404
    /**
405
     * @ticket 29801
406
     */
407
    public function test_build_value_compare_between_single_non_numeric() 
408
    {
409
        $q = new WP_Date_Query(array());
410
411
        $found = $q->build_value('BETWEEN', 'foo');
412
        $this->assertFalse($found);
413
    }
414
415
    /**
416
     * @ticket 29801
417
     */
418 View Code Duplication
    public function test_build_value_compare_between_array_with_other_than_two_items() 
419
    {
420
        $q = new WP_Date_Query(array());
421
422
        $found = $q->build_value('BETWEEN', array( 2, 3, 4 ));
423
        $this->assertFalse($found);
424
    }
425
426
    /**
427
     * @ticket 29801
428
     */
429 View Code Duplication
    public function test_build_value_compare_between_incorrect_array_key() 
430
    {
431
        $q = new WP_Date_Query(array());
432
433
        $found = $q->build_value(
434
            'BETWEEN', array(
435
            2 => 4,
436
            3 => 5,
437
            ) 
438
        );
439
440
        $this->assertSame('4 AND 5', $found);
441
    }
442
443
    /**
444
     * @ticket 29801
445
     */
446 View Code Duplication
    public function test_build_value_compare_between_array_contains_non_numeric() 
447
    {
448
        $q = new WP_Date_Query(array());
449
450
        $found = $q->build_value('BETWEEN', array( 2, 'foo' ));
451
        $this->assertFalse($found);
452
    }
453
454 View Code Duplication
    public function test_build_value_compare_between() 
455
    {
456
        $q = new WP_Date_Query(array());
457
458
        $found = $q->build_value('BETWEEN', array( 2, 3 ));
459
        $this->assertSame('2 AND 3', $found);
460
    }
461
462
    public function test_build_value_compare_not_between_single_integer() 
463
    {
464
        $q = new WP_Date_Query(array());
465
466
        $found = $q->build_value('NOT BETWEEN', 4);
467
        $this->assertSame('4 AND 4', $found);
468
    }
469
470
    /**
471
     * @ticket 29801
472
     */
473
    public function test_build_value_compare_not_between_single_non_numeric() 
474
    {
475
        $q = new WP_Date_Query(array());
476
477
        $found = $q->build_value('NOT BETWEEN', 'foo');
478
        $this->assertFalse($found);
479
    }
480
481
    /**
482
     * @ticket 29801
483
     */
484 View Code Duplication
    public function test_build_value_compare_not_between_array_with_other_than_two_items() 
485
    {
486
        $q = new WP_Date_Query(array());
487
488
        $found = $q->build_value('NOT BETWEEN', array( 2, 3, 4 ));
489
        $this->assertFalse($found);
490
    }
491
492
    /**
493
     * @ticket 29801
494
     */
495 View Code Duplication
    public function test_build_value_compare_not_between_incorrect_array_key() 
496
    {
497
        $q = new WP_Date_Query(array());
498
499
        $found = $q->build_value(
500
            'NOT BETWEEN', array(
501
            2 => 4,
502
            3 => 5,
503
            ) 
504
        );
505
506
        $this->assertSame('4 AND 5', $found);
507
    }
508
509
    /**
510
     * @ticket 29801
511
     */
512 View Code Duplication
    public function test_build_value_compare_not_between_array_contains_non_numeric() 
513
    {
514
        $q = new WP_Date_Query(array());
515
516
        $found = $q->build_value('NOT BETWEEN', array( 2, 'foo' ));
517
        $this->assertFalse($found);
518
    }
519
520 View Code Duplication
    public function test_build_value_compare_not_between() 
521
    {
522
        $q = new WP_Date_Query(array());
523
524
        $found = $q->build_value('NOT BETWEEN', array( 2, 3 ));
525
        $this->assertSame('2 AND 3', $found);
526
    }
527
528
    public function test_build_value_compare_default_value_integer() 
529
    {
530
        $q = new WP_Date_Query(array());
531
532
        $found = $q->build_value('foo', 5);
533
        $this->assertSame(5, $found);
534
    }
535
536
    /**
537
     * @ticket 29801
538
     */
539
    public function test_build_value_compare_default_value_non_numeric() 
540
    {
541
        $q = new WP_Date_Query(array());
542
543
        $found = $q->build_value('foo', 'foo');
544
        $this->assertFalse($found);
545
    }
546
547
    public function test_build_mysql_datetime_datetime_non_array() 
548
    {
549
        $q = new WP_Date_Query(array());
550
551
        // This might be a fragile test if it takes longer than 1 second to run
552
        $found = $q->build_mysql_datetime('foo');
553
        $expected = gmdate('Y-m-d H:i:s', false);
554
        $this->assertSame($expected, $found);
555
    }
556
557 View Code Duplication
    public function test_build_mysql_datetime_default_to_max_true() 
558
    {
559
        $q = new WP_Date_Query(array());
560
561
        $found = $q->build_mysql_datetime(
562
            array(
563
            'year' => 2011,
564
            ), true 
565
        );
566
        $this->assertSame('2011-12-31 23:59:59', $found);
567
    }
568
569 View Code Duplication
    public function test_build_mysql_datetime_default_to_max_false() 
570
    {
571
        $q = new WP_Date_Query(array());
572
573
        $found = $q->build_mysql_datetime(
574
            array(
575
            'year' => 2011,
576
            ), false 
577
        );
578
        $this->assertSame('2011-01-01 00:00:00', $found);
579
    }
580
581 View Code Duplication
    public function test_build_mysql_datetime_default_to_max_default_to_false() 
582
    {
583
        $q = new WP_Date_Query(array());
584
585
        $found = $q->build_mysql_datetime(
586
            array(
587
            'year' => 2011,
588
            ), false 
589
        );
590
        $this->assertSame('2011-01-01 00:00:00', $found);
591
    }
592
593
    public function test_build_time_query_insufficient_time_values() 
594
    {
595
        $q = new WP_Date_Query(array());
596
597
        $this->assertFalse($q->build_time_query('post_date', '='));
598
    }
599
600
    /**
601
     * @ticket 34228
602
     */
603 View Code Duplication
    public function test_build_time_query_should_not_discard_hour_0() 
604
    {
605
        $q = new WP_Date_Query(array());
606
607
        $found = $q->build_time_query('post_date', '=', 0, 10);
608
609
        $this->assertContains('%H', $found);
610
    }
611
612 View Code Duplication
    public function test_build_time_query_compare_in() 
613
    {
614
        $q = new WP_Date_Query(array());
615
616
        // Just hour
617
        $found = $q->build_time_query('post_date', 'IN', array( 1, 2 ));
618
        $this->assertSame('HOUR( post_date ) IN (1,2)', $found);
619
620
        // Skip minute
621
        $found = $q->build_time_query('post_date', 'IN', array( 1, 2 ), null, 6);
622
        $this->assertSame('HOUR( post_date ) IN (1,2) AND SECOND( post_date ) IN (6)', $found);
623
624
        // All three
625
        $found = $q->build_time_query('post_date', 'IN', array( 1, 2 ), array( 3, 4, 5 ), 6);
626
        $this->assertSame('HOUR( post_date ) IN (1,2) AND MINUTE( post_date ) IN (3,4,5) AND SECOND( post_date ) IN (6)', $found);
627
    }
628
629 View Code Duplication
    public function test_build_time_query_compare_not_in() 
630
    {
631
        $q = new WP_Date_Query(array());
632
633
        // Just hour
634
        $found = $q->build_time_query('post_date', 'NOT IN', array( 1, 2 ));
635
        $this->assertSame('HOUR( post_date ) NOT IN (1,2)', $found);
636
637
        // Skip minute
638
        $found = $q->build_time_query('post_date', 'NOT IN', array( 1, 2 ), null, 6);
639
        $this->assertSame('HOUR( post_date ) NOT IN (1,2) AND SECOND( post_date ) NOT IN (6)', $found);
640
641
        // All three
642
        $found = $q->build_time_query('post_date', 'NOT IN', array( 1, 2 ), array( 3, 4, 5 ), 6);
643
        $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);
644
    }
645
646 View Code Duplication
    public function test_build_time_query_compare_between() 
647
    {
648
        $q = new WP_Date_Query(array());
649
650
        // Just hour
651
        $found = $q->build_time_query('post_date', 'BETWEEN', array( 1, 2 ));
652
        $this->assertSame('HOUR( post_date ) BETWEEN 1 AND 2', $found);
653
654
        // Skip minute
655
        $found = $q->build_time_query('post_date', 'BETWEEN', array( 1, 2 ), null, array( 6, 7 ));
656
        $this->assertSame('HOUR( post_date ) BETWEEN 1 AND 2 AND SECOND( post_date ) BETWEEN 6 AND 7', $found);
657
658
        // All three
659
        $found = $q->build_time_query('post_date', 'BETWEEN', array( 1, 2 ), array( 3, 4 ), array( 6, 7 ));
660
        $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);
661
    }
662
663 View Code Duplication
    public function test_build_time_query_compare_not_between() 
664
    {
665
        $q = new WP_Date_Query(array());
666
667
        // Just hour
668
        $found = $q->build_time_query('post_date', 'NOT BETWEEN', array( 1, 2 ));
669
        $this->assertSame('HOUR( post_date ) NOT BETWEEN 1 AND 2', $found);
670
671
        // Skip minute
672
        $found = $q->build_time_query('post_date', 'NOT BETWEEN', array( 1, 2 ), null, array( 6, 7 ));
673
        $this->assertSame('HOUR( post_date ) NOT BETWEEN 1 AND 2 AND SECOND( post_date ) NOT BETWEEN 6 AND 7', $found);
674
675
        // All three
676
        $found = $q->build_time_query('post_date', 'NOT BETWEEN', array( 1, 2 ), array( 3, 4 ), array( 6, 7 ));
677
        $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);
678
    }
679
680
    public function test_build_time_query_hour_only() 
681
    {
682
        $q = new WP_Date_Query(array());
683
684
        $found = $q->build_time_query('post_date', '=', 5);
685
        $this->assertSame('HOUR( post_date ) = 5', $found);
686
    }
687
688 View Code Duplication
    public function test_build_time_query_minute_only() 
689
    {
690
        $q = new WP_Date_Query(array());
691
692
        $found = $q->build_time_query('post_date', '=', null, 5);
693
        $this->assertSame('MINUTE( post_date ) = 5', $found);
694
    }
695
696 View Code Duplication
    public function test_build_time_query_second_only() 
697
    {
698
        $q = new WP_Date_Query(array());
699
700
        $found = $q->build_time_query('post_date', '=', null, null, 5);
701
        $this->assertSame('SECOND( post_date ) = 5', $found);
702
    }
703
704 View Code Duplication
    public function test_build_time_query_hour_and_second() 
705
    {
706
        $q = new WP_Date_Query(array());
707
708
        $found = $q->build_time_query('post_date', '=', 5, null, 5);
709
        $this->assertFalse($found);
710
    }
711
712 View Code Duplication
    public function test_build_time_query_hour_minute() 
713
    {
714
        $q = new WP_Date_Query(array());
715
716
        $found = $q->build_time_query('post_date', '=', 5, 15);
717
718
        // $compare value is floating point - use regex to account for
719
        // varying precision on different PHP installations
720
        $this->assertRegExp("/DATE_FORMAT\( post_date, '%H\.%i' \) = 5\.150*/", $found);
721
    }
722
723 View Code Duplication
    public function test_build_time_query_hour_minute_second() 
724
    {
725
        $q = new WP_Date_Query(array());
726
727
        $found = $q->build_time_query('post_date', '=', 5, 15, 35);
728
729
        // $compare value is floating point - use regex to account for
730
        // varying precision on different PHP installations
731
        $this->assertRegExp("/DATE_FORMAT\( post_date, '%H\.%i%s' \) = 5\.15350*/", $found);
732
    }
733
734 View Code Duplication
    public function test_build_time_query_minute_second() 
735
    {
736
        $q = new WP_Date_Query(array());
737
738
        $found = $q->build_time_query('post_date', '=', null, 15, 35);
739
740
        // $compare value is floating point - use regex to account for
741
        // varying precision on different PHP installations
742
        $this->assertRegExp("/DATE_FORMAT\( post_date, '0\.%i%s' \) = 0\.15350*/", $found);
743
    }
744
745
    /**
746
     * @ticket 25834
747
     * @expectedIncorrectUsage WP_Date_Query
748
     */
749
    public function test_validate_date_query_before_after()
750
    {
751
        // Valid values.
752
        $valid_args = array(
753
         array(
754
          'month' => 2,
755
          'year'  => 2014,
756
         ),
757
         array(
758
          'day'  => 8,
759
          'year' => 2014,
760
         ),
761
        );
762
763 View Code Duplication
        foreach ( $valid_args as $args ) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
764
            $this->assertTrue($this->q->validate_date_values(array( 'before' => $args )));
765
            $this->assertTrue($this->q->validate_date_values(array( 'after' => $args )));
766
        }
767
768
        // Invalid values.
769
        $invalid_args = array(
770
         array(
771
          'month' => 13,
772
         ),
773
         array(
774
          'day' => 32,
775
         ),
776
         array(
777
          'minute' => 60,
778
         ),
779
         array(
780
          'second' => 60,
781
         ),
782
         array(
783
          'week' => 54,
784
         ),
785
        );
786
787 View Code Duplication
        foreach ( $invalid_args as $args ) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
788
            $this->assertFalse($this->q->validate_date_values(array( 'before' => $args )));
789
            $this->assertFalse($this->q->validate_date_values(array( 'after' => $args )));
790
        }
791
    }
792
793
    /**
794
     * @ticket 25834
795
     * @expectedIncorrectUsage WP_Date_Query
796
     */
797
    public function test_validate_date_query_before_after_with_month()
798
    {
799
        // Both are valid.
800
        $args = array(
801
         'before' => array(
802
          'month' => 2,
803
          'year'  => 2014,
804
         ),
805
         'month' => 10,
806
        );
807
        $this->assertTrue($this->q->validate_date_values($args));
808
809
        // 'before' is invalid, 'month' is valid.
810
        $args = array(
811
         'before' => array(
812
          'month' => 13,
813
          'year'  => 2014,
814
         ),
815
         'month' => 10,
816
        );
817
        $this->assertFalse($this->q->validate_date_values($args));
818
819
        // 'before' is valid, 'month' is invalid.
820
        $args = array(
821
         'before' => array(
822
          'month' => 10,
823
          'year'  => 2014,
824
         ),
825
         'month' => 14,
826
        );
827
        $this->assertFalse($this->q->validate_date_values($args));
828
829
        // Both are invalid.
830
        $args = array(
831
         'before' => array(
832
          'month' => 14,
833
          'year'  => 2014,
834
         ),
835
         'month' => 14,
836
        );
837
        $this->assertFalse($this->q->validate_date_values($args));
838
    }
839
840
    /**
841
     * @ticket 25834
842
     * @expectedIncorrectUsage WP_Date_Query
843
     */
844
    public function test_validate_date_values_week() 
845
    {
846
        // Valid values.
847
        $weeks = range(1, 53);
848
        foreach ( $weeks as $week ) {
849
            $this->assertTrue($this->q->validate_date_values(array( 'week' => $week )));
850
        }
851
852
        // Invalid values.
853
        $weeks = array( -1, 0, 54 );
854
        foreach ( $weeks as $week ) {
855
            $this->assertFalse($this->q->validate_date_values(array( 'week' => $week )));
856
        }
857
858
        // Valid combinations.
859
        $weeks = array(
860
         array(
861
          'week' => 52,
862
          'year' => 2012,
863
         ),
864
         array(
865
          'week' => 53,
866
          'year' => 2009,
867
         ),
868
        );
869
870
        foreach ( $weeks as $week_args ) {
871
            $this->assertTrue($this->q->validate_date_values($week_args));
872
        }
873
874
        // Invalid combinations.
875
        $weeks = array(
876
         // 2012 has 52 weeks.
877
         array(
878
          'week' => 53,
879
          'year' => 2012,
880
         ),
881
882
         // 2013 has 53 weeks.
883
         array(
884
          'week' => 54,
885
          'year' => 2009,
886
         )
887
        );
888
889
        foreach ( $weeks as $week_args ) {
890
            $this->assertFalse($this->q->validate_date_values($week_args));
891
        }
892
    }
893
894
    /**
895
     * @ticket 25834
896
     * @expectedIncorrectUsage WP_Date_Query
897
     */
898 View Code Duplication
    public function test_validate_date_values_month() 
899
    {
900
        // Valid values.
901
        $months = range(1, 12);
902
        foreach ( $months as $month ) {
903
            $this->assertTrue($this->q->validate_date_values(array( 'month' => $month )));
904
        }
905
906
        // Invalid values.
907
        $months = array( -1, 0, 13, 'string who wants to be a int' );
908
        foreach ( $months as $month ) {
909
            $this->assertFalse($this->q->validate_date_values(array( 'month' => $month )));
910
        }
911
    }
912
913
    /**
914
     * @ticket 25834
915
     * @expectedIncorrectUsage WP_Date_Query
916
     */
917
    public function test_validate_date_values_day() 
918
    {
919
        // Valid values.
920
        $days = range(1, 31);
921
        foreach ( $days as $day ) {
922
            $this->assertTrue($this->q->validate_date_values(array( 'day' => $day )));
923
        }
924
925
        // Invalid values.
926
        $days = array( -1, 32 );
927
        foreach ( $days as $day ) {
928
            $this->assertFalse($this->q->validate_date_values(array( 'day' => $day )));
929
        }
930
931
        // Valid combinations.
932
        $days = array(
933
         array(
934
          'day'   => 29,
935
          'month' => 2,
936
          'year'  => 2008,
937
         ),
938
         array(
939
          'day'   => 28,
940
          'month' => 2,
941
          'year'  => 2009,
942
         ),
943
        );
944
945
        foreach ( $days as $args ) {
946
            $this->assertTrue($this->q->validate_date_values($args));
947
        }
948
949
        // Invalid combinations.
950
        $days = array(
951
         // February 2008 has 29 days.
952
         array(
953
          'day'   => 30,
954
          'month' => 2,
955
          'year'  => 2008,
956
         ),
957
958
         // February 2009 has 29 days.
959
         array(
960
          'day'   => 29,
961
          'month' => 2,
962
          'year'  => 2009,
963
         ),
964
        );
965
966
        foreach ( $days as $args ) {
967
            $this->assertFalse($this->q->validate_date_values($args));
968
        }
969
    }
970
971
    /**
972
     * @ticket 25834
973
     * @expectedIncorrectUsage WP_Date_Query
974
     */
975 View Code Duplication
    public function test_validate_date_values_hour() 
976
    {
977
        // Valid values.
978
        $hours = range(0, 23);
979
        foreach ( $hours as $hour ) {
980
            $this->assertTrue($this->q->validate_date_values(array( 'hour' => $hour )));
981
        }
982
983
        // Invalid values.
984
        $hours = array( -1, 24, 25, 'string' );
985
        foreach ( $hours as $hour ) {
986
            $this->assertFalse($this->q->validate_date_values(array( 'hour' => $hour )));
987
        }
988
    }
989
990
    /**
991
     * @ticket 25834
992
     * @expectedIncorrectUsage WP_Date_Query
993
     */
994 View Code Duplication
    public function test_validate_date_values_minute() 
995
    {
996
        // Valid values.
997
        $minutes = range(0, 59);
998
        foreach ( $minutes as $minute ) {
999
            $this->assertTrue($this->q->validate_date_values(array( 'minute' => $minute )));
1000
        }
1001
1002
        // Invalid values.
1003
        $minutes = array( -1, 60 );
1004
        foreach ( $minutes as $minute ) {
1005
            $this->assertFalse($this->q->validate_date_values(array( 'minute' => $minute )));
1006
        }
1007
    }
1008
1009
    /**
1010
     * @ticket 25834
1011
     * @expectedIncorrectUsage WP_Date_Query
1012
     */
1013 View Code Duplication
    public function test_validate_date_values_second() 
1014
    {
1015
        // Valid values.
1016
        $seconds = range(0, 59);
1017
        foreach ( $seconds as $second ) {
1018
            $this->assertTrue($this->q->validate_date_values(array( 'second' => $second )));
1019
        }
1020
1021
        // Invalid values.
1022
        $seconds = array( -1, 60 );
1023
        foreach ( $seconds as $second ) {
1024
            $this->assertFalse($this->q->validate_date_values(array( 'second' => $second )));
1025
        }
1026
1027
    }
1028
1029
    /**
1030
     * @ticket 25834
1031
     * @expectedIncorrectUsage WP_Date_Query
1032
     */
1033 View Code Duplication
    public function test_validate_date_values_day_of_week() 
1034
    {
1035
        // Valid values.
1036
        $days_of_week = range(1, 7);
1037
        foreach ( $days_of_week as $day_of_week ) {
1038
            $this->assertTrue($this->q->validate_date_values(array( 'dayofweek' => $day_of_week )));
1039
        }
1040
1041
        // Invalid values.
1042
        $days_of_week = array( -1, 0, 8 );
1043
        foreach ( $days_of_week as $day_of_week ) {
1044
            $this->assertFalse($this->q->validate_date_values(array( 'dayofweek' => $day_of_week )));
1045
        }
1046
    }
1047
1048
    /**
1049
     * @ticket 28063
1050
     * @expectedIncorrectUsage WP_Date_Query
1051
     */
1052 View Code Duplication
    public function test_validate_date_values_day_of_week_iso() 
1053
    {
1054
        // Valid values.
1055
        $days_of_week = range(1, 7);
1056
        foreach ( $days_of_week as $day_of_week ) {
1057
            $this->assertTrue($this->q->validate_date_values(array( 'dayofweek_iso' => $day_of_week )));
1058
        }
1059
1060
        // Invalid values.
1061
        $days_of_week = array( -1, 0, 8 );
1062
        foreach ( $days_of_week as $day_of_week ) {
1063
            $this->assertFalse($this->q->validate_date_values(array( 'dayofweek_iso' => $day_of_week )));
1064
        }
1065
    }
1066
1067
    /**
1068
     * @ticket 25834
1069
     * @expectedIncorrectUsage WP_Date_Query
1070
     */
1071 View Code Duplication
    public function test_validate_date_values_day_of_year() 
1072
    {
1073
        // Valid values.
1074
        $days_of_year = range(1, 366);
1075
        foreach ( $days_of_year as $day_of_year ) {
1076
            $this->assertTrue($this->q->validate_date_values(array( 'dayofyear' => $day_of_year )));
1077
        }
1078
1079
        // Invalid values.
1080
        $days_of_year = array( -1, 0, 367 );
1081
        foreach ( $days_of_year as $day_of_year ) {
1082
            $this->assertFalse(@$this->q->validate_date_values(array( 'dayofyear' => $day_of_year )));
0 ignored issues
show
Coding Style introduced by
Silencing errors is discouraged
Loading history...
1083
        }
1084
    }
1085
1086
    /**
1087
     * @ticket 31001
1088
     */
1089 View Code Duplication
    public function test_validate_date_values_should_process_array_value_for_year() 
1090
    {
1091
        $p1 = self::factory()->post->create(array( 'post_date' => '2015-01-12 00:00:00' ));
1092
        $p2 = self::factory()->post->create(array( 'post_date' => '2013-01-12 00:00:00' ));
1093
1094
        $q = new WP_Query(
1095
            array(
1096
            'date_query' => array(
1097
            array(
1098
            'compare' => 'BETWEEN',
1099
            'year' => array( 2012, 2014 ),
1100
            ),
1101
            ),
1102
            'fields' => 'ids',
1103
            ) 
1104
        );
1105
1106
        $this->assertEquals(array( $p2 ), $q->posts);
1107
    }
1108
1109
    /**
1110
     * @ticket 31001
1111
     */
1112 View Code Duplication
    public function test_validate_date_values_should_process_array_value_for_day() 
1113
    {
1114
        $p1 = self::factory()->post->create(array( 'post_date' => '2015-01-12 00:00:00' ));
1115
        $p2 = self::factory()->post->create(array( 'post_date' => '2015-01-10 00:00:00' ));
1116
1117
        $q = new WP_Query(
1118
            array(
1119
            'date_query' => array(
1120
            array(
1121
            'compare' => 'BETWEEN',
1122
            'day' => array( 9, 11 ),
1123
            ),
1124
            ),
1125
            'fields' => 'ids',
1126
            ) 
1127
        );
1128
1129
        $this->assertEquals(array( $p2 ), $q->posts);
1130
    }
1131
1132
    /**
1133
     * @ticket 31001
1134
     * @expectedIncorrectUsage WP_Date_Query
1135
     */
1136 View Code Duplication
    public function test_validate_date_values_should_process_array_value_for_day_when_values_are_invalid() 
1137
    {
1138
        $p1 = self::factory()->post->create(array( 'post_date' => '2015-01-12 00:00:00' ));
1139
        $p2 = self::factory()->post->create(array( 'post_date' => '2015-01-10 00:00:00' ));
1140
1141
        $q = new WP_Query(
1142
            array(
1143
            'date_query' => array(
1144
            array(
1145
            'compare' => 'BETWEEN',
1146
            'day' => array( 9, 32 ),
1147
            ),
1148
            ),
1149
            'fields' => 'ids',
1150
            ) 
1151
        );
1152
1153
        // MySQL ignores the invalid clause.
1154
        $this->assertEquals(array( $p1, $p2 ), $q->posts);
1155
    }
1156
1157
    /**
1158
 * Helpers 
1159
**********************************************************/
1160
1161
    public function date_query_valid_columns_callback( $columns ) 
1162
    {
1163
        $columns[] = 'my_custom_column';
1164
        return $columns;
1165
    }
1166
}
1167