forked from WordPress/wordpress-develop
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathquery.php
More file actions
967 lines (796 loc) · 24.1 KB
/
query.php
File metadata and controls
967 lines (796 loc) · 24.1 KB
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
<?php
class Tests_Query extends WP_UnitTestCase {
public function set_up() {
parent::set_up();
$this->set_permalink_structure( '/%year%/%monthnum%/%day%/%postname%/' );
create_initial_taxonomies();
}
/**
* @ticket 24785
*/
public function test_nested_loop_reset_postdata() {
$post_id = self::factory()->post->create();
$nested_post_id = self::factory()->post->create();
$first_query = new WP_Query( array( 'post__in' => array( $post_id ) ) );
while ( $first_query->have_posts() ) {
$first_query->the_post();
$second_query = new WP_Query( array( 'post__in' => array( $nested_post_id ) ) );
while ( $second_query->have_posts() ) {
$second_query->the_post();
$this->assertSame( get_the_ID(), $nested_post_id );
}
$first_query->reset_postdata();
$this->assertSame( get_the_ID(), $post_id );
}
}
/**
* @ticket 16471
*/
public function test_default_query_var() {
$query = new WP_Query();
$this->assertSame( '', $query->get( 'nonexistent' ) );
$this->assertFalse( $query->get( 'nonexistent', false ) );
$this->assertTrue( $query->get( 'nonexistent', true ) );
}
/**
* @ticket 25380
*/
public function test_pre_posts_per_page() {
self::factory()->post->create_many( 10 );
add_action( 'pre_get_posts', array( $this, 'filter_posts_per_page' ) );
$this->go_to( get_feed_link() );
$this->assertSame( 30, get_query_var( 'posts_per_page' ) );
}
public function filter_posts_per_page( &$query ) {
$query->set( 'posts_per_rss', 30 );
}
/**
* @ticket 26627
*/
public function test_tag_queried_object() {
$slug = 'tag-slug-26627';
self::factory()->tag->create( array( 'slug' => $slug ) );
$tag = get_term_by( 'slug', $slug, 'post_tag' );
add_action( 'pre_get_posts', array( $this, 'tag_queried_object' ), 11 );
$this->go_to( get_term_link( $tag ) );
$this->assertQueryTrue( 'is_tag', 'is_archive' );
$this->assertNotEmpty( get_query_var( 'tag_id' ) );
$this->assertNotEmpty( get_query_var( 'tag' ) );
$this->assertEmpty( get_query_var( 'tax_query' ) );
$this->assertCount( 1, get_query_var( 'tag_slug__in' ) );
$this->assertEquals( get_queried_object(), $tag );
remove_action( 'pre_get_posts', array( $this, 'tag_queried_object' ), 11 );
}
public function tag_queried_object( &$query ) {
$tag = get_term_by( 'slug', 'tag-slug-26627', 'post_tag' );
$this->assertTrue( $query->is_tag() );
$this->assertTrue( $query->is_archive() );
$this->assertNotEmpty( $query->get( 'tag' ) );
$this->assertCount( 1, $query->get( 'tag_slug__in' ) );
$this->assertEquals( $query->get_queried_object(), $tag );
}
/**
* @ticket 31246
*/
public function test_get_queried_object_should_return_null_when_is_tax_is_true_but_the_taxonomy_args_have_been_removed_in_a_parse_query_callback() {
// Don't override the args provided below.
remove_action( 'pre_get_posts', array( $this, 'pre_get_posts_tax_category_tax_query' ) );
register_taxonomy( 'wptests_tax', 'post' );
$terms = self::factory()->term->create_many(
2,
array(
'taxonomy' => 'wptests_tax',
)
);
$posts = self::factory()->post->create_many( 2 );
wp_set_object_terms( $posts[0], array( $terms[0] ), 'wptests_tax' );
wp_set_object_terms( $posts[1], array( $terms[1] ), 'wptests_tax' );
add_action( 'parse_query', array( $this, 'filter_parse_query_to_remove_tax' ) );
$q = new WP_Query(
array(
'fields' => 'ids',
'wptests_tax' => $terms[1],
)
);
remove_action( 'parse_query', array( $this, 'filter_parse_query_to_remove_tax' ) );
$this->assertNull( $q->get_queried_object() );
}
public function filter_parse_query_to_remove_tax( $q ) {
unset( $q->query_vars['wptests_tax'] );
}
/**
* @ticket 37962
*/
public function test_get_queried_object_should_return_null_for_not_exists_tax_query() {
register_taxonomy( 'wptests_tax', 'post' );
$q = new WP_Query(
array(
'tax_query' => array(
array(
'taxonomy' => 'wptests_tax',
'operator' => 'NOT EXISTS',
),
),
)
);
$queried_object = $q->get_queried_object();
$this->assertNull( $queried_object );
}
public function test_orderby_space_separated() {
global $wpdb;
$q = new WP_Query(
array(
'orderby' => 'title date',
'order' => 'DESC',
)
);
$this->assertStringContainsString( "ORDER BY $wpdb->posts.post_title DESC, $wpdb->posts.post_date DESC", $q->request );
}
public function test_cat_querystring_single_term() {
$c1 = self::factory()->category->create(
array(
'name' => 'Test Category 1',
'slug' => 'test1',
)
);
$c2 = self::factory()->category->create(
array(
'name' => 'Test Category 2',
'slug' => 'test2',
)
);
$p1 = self::factory()->post->create();
$p2 = self::factory()->post->create();
$p3 = self::factory()->post->create();
wp_set_object_terms( $p1, $c1, 'category' );
wp_set_object_terms( $p2, array( $c1, $c2 ), 'category' );
wp_set_object_terms( $p3, $c2, 'category' );
$url = add_query_arg(
array(
'cat' => $c1,
),
'/'
);
$this->go_to( $url );
$matching_posts = wp_list_pluck( $GLOBALS['wp_query']->posts, 'ID' );
$this->assertSameSets( array( $p1, $p2 ), $matching_posts );
}
public function test_category_querystring_multiple_terms_comma_separated() {
$c1 = self::factory()->category->create(
array(
'name' => 'Test Category 1',
'slug' => 'test1',
)
);
$c2 = self::factory()->category->create(
array(
'name' => 'Test Category 2',
'slug' => 'test2',
)
);
$c3 = self::factory()->category->create(
array(
'name' => 'Test Category 3',
'slug' => 'test3',
)
);
$p1 = self::factory()->post->create();
$p2 = self::factory()->post->create();
$p3 = self::factory()->post->create();
$p4 = self::factory()->post->create();
wp_set_object_terms( $p1, $c1, 'category' );
wp_set_object_terms( $p2, array( $c1, $c2 ), 'category' );
wp_set_object_terms( $p3, $c2, 'category' );
wp_set_object_terms( $p4, $c3, 'category' );
$url = add_query_arg(
array(
'cat' => implode( ',', array( $c1, $c2 ) ),
),
'/'
);
$this->go_to( $url );
$matching_posts = wp_list_pluck( $GLOBALS['wp_query']->posts, 'ID' );
$this->assertSameSets( array( $p1, $p2, $p3 ), $matching_posts );
}
/**
* @ticket 33532
*/
public function test_category_querystring_multiple_terms_formatted_as_array() {
$c1 = self::factory()->category->create(
array(
'name' => 'Test Category 1',
'slug' => 'test1',
)
);
$c2 = self::factory()->category->create(
array(
'name' => 'Test Category 2',
'slug' => 'test2',
)
);
$c3 = self::factory()->category->create(
array(
'name' => 'Test Category 3',
'slug' => 'test3',
)
);
$p1 = self::factory()->post->create();
$p2 = self::factory()->post->create();
$p3 = self::factory()->post->create();
$p4 = self::factory()->post->create();
wp_set_object_terms( $p1, $c1, 'category' );
wp_set_object_terms( $p2, array( $c1, $c2 ), 'category' );
wp_set_object_terms( $p3, $c2, 'category' );
wp_set_object_terms( $p4, $c3, 'category' );
$url = add_query_arg(
array(
'cat' => array( $c1, $c2 ),
),
'/'
);
$this->go_to( $url );
$matching_posts = wp_list_pluck( $GLOBALS['wp_query']->posts, 'ID' );
$this->assertSameSets( array( $p1, $p2, $p3 ), $matching_posts );
}
public function test_tag_querystring_single_term() {
$t1 = self::factory()->tag->create_and_get(
array(
'name' => 'Test Tag 1',
'slug' => 'test1',
)
);
$t2 = self::factory()->tag->create_and_get(
array(
'name' => 'Test Tag 2',
'slug' => 'test2',
)
);
$p1 = self::factory()->post->create();
$p2 = self::factory()->post->create();
$p3 = self::factory()->post->create();
wp_set_object_terms( $p1, $t1->slug, 'post_tag' );
wp_set_object_terms( $p2, array( $t1->slug, $t2->slug ), 'post_tag' );
wp_set_object_terms( $p3, $t2->slug, 'post_tag' );
$url = add_query_arg(
array(
'tag' => $t1->slug,
),
'/'
);
$this->go_to( $url );
$matching_posts = wp_list_pluck( $GLOBALS['wp_query']->posts, 'ID' );
$this->assertSameSets( array( $p1, $p2 ), $matching_posts );
}
public function test_tag_querystring_multiple_terms_comma_separated() {
$c1 = self::factory()->tag->create_and_get(
array(
'name' => 'Test Tag 1',
'slug' => 'test1',
)
);
$c2 = self::factory()->tag->create_and_get(
array(
'name' => 'Test Tag 2',
'slug' => 'test2',
)
);
$c3 = self::factory()->tag->create_and_get(
array(
'name' => 'Test Tag 3',
'slug' => 'test3',
)
);
$p1 = self::factory()->post->create();
$p2 = self::factory()->post->create();
$p3 = self::factory()->post->create();
$p4 = self::factory()->post->create();
wp_set_object_terms( $p1, $c1->slug, 'post_tag' );
wp_set_object_terms( $p2, array( $c1->slug, $c2->slug ), 'post_tag' );
wp_set_object_terms( $p3, $c2->slug, 'post_tag' );
wp_set_object_terms( $p4, $c3->slug, 'post_tag' );
$url = add_query_arg(
array(
'tag' => implode( ',', array( $c1->slug, $c2->slug ) ),
),
'/'
);
$this->go_to( $url );
$matching_posts = wp_list_pluck( $GLOBALS['wp_query']->posts, 'ID' );
$this->assertSameSets( array( $p1, $p2, $p3 ), $matching_posts );
}
/**
* @ticket 33532
*/
public function test_tag_querystring_multiple_terms_formatted_as_array() {
$c1 = self::factory()->tag->create_and_get(
array(
'name' => 'Test Tag 1',
'slug' => 'test1',
)
);
$c2 = self::factory()->tag->create_and_get(
array(
'name' => 'Test Tag 2',
'slug' => 'test2',
)
);
$c3 = self::factory()->tag->create_and_get(
array(
'name' => 'Test Tag 3',
'slug' => 'test3',
)
);
$p1 = self::factory()->post->create();
$p2 = self::factory()->post->create();
$p3 = self::factory()->post->create();
$p4 = self::factory()->post->create();
wp_set_object_terms( $p1, $c1->slug, 'post_tag' );
wp_set_object_terms( $p2, array( $c1->slug, $c2->slug ), 'post_tag' );
wp_set_object_terms( $p3, $c2->slug, 'post_tag' );
wp_set_object_terms( $p4, $c3->slug, 'post_tag' );
$url = add_query_arg(
array(
'tag' => array( $c1->slug, $c2->slug ),
),
'/'
);
$this->go_to( $url );
$matching_posts = wp_list_pluck( $GLOBALS['wp_query']->posts, 'ID' );
$this->assertSameSets( array( $p1, $p2, $p3 ), $matching_posts );
}
public function test_custom_taxonomy_querystring_single_term() {
register_taxonomy( 'test_tax_cat', 'post' );
wp_insert_term( 'test1', 'test_tax_cat' );
wp_insert_term( 'test2', 'test_tax_cat' );
wp_insert_term( 'test3', 'test_tax_cat' );
$p1 = self::factory()->post->create();
$p2 = self::factory()->post->create();
$p3 = self::factory()->post->create();
wp_set_object_terms( $p1, 'test1', 'test_tax_cat' );
wp_set_object_terms( $p2, array( 'test1', 'test2' ), 'test_tax_cat' );
wp_set_object_terms( $p3, 'test2', 'test_tax_cat' );
$url = add_query_arg(
array(
'test_tax_cat' => 'test1',
),
'/'
);
$this->go_to( $url );
$this->assertSameSets( array( $p1, $p2 ), wp_list_pluck( $GLOBALS['wp_query']->posts, 'ID' ) );
}
public function test_custom_taxonomy_querystring_multiple_terms_comma_separated() {
register_taxonomy( 'test_tax_cat', 'post' );
wp_insert_term( 'test1', 'test_tax_cat' );
wp_insert_term( 'test2', 'test_tax_cat' );
wp_insert_term( 'test3', 'test_tax_cat' );
$p1 = self::factory()->post->create();
$p2 = self::factory()->post->create();
$p3 = self::factory()->post->create();
$p4 = self::factory()->post->create();
wp_set_object_terms( $p1, 'test1', 'test_tax_cat' );
wp_set_object_terms( $p2, array( 'test1', 'test2' ), 'test_tax_cat' );
wp_set_object_terms( $p3, 'test2', 'test_tax_cat' );
wp_set_object_terms( $p4, 'test3', 'test_tax_cat' );
$url = add_query_arg(
array(
'test_tax_cat' => 'test1,test2',
),
'/'
);
$this->go_to( $url );
$this->assertSameSets( array( $p1, $p2, $p3 ), wp_list_pluck( $GLOBALS['wp_query']->posts, 'ID' ) );
}
/**
* @ticket 32454
*/
public function test_custom_taxonomy_querystring_multiple_terms_formatted_as_array() {
register_taxonomy( 'test_tax_cat', 'post' );
wp_insert_term( 'test1', 'test_tax_cat' );
wp_insert_term( 'test2', 'test_tax_cat' );
wp_insert_term( 'test3', 'test_tax_cat' );
$p1 = self::factory()->post->create();
$p2 = self::factory()->post->create();
$p3 = self::factory()->post->create();
$p4 = self::factory()->post->create();
wp_set_object_terms( $p1, 'test1', 'test_tax_cat' );
wp_set_object_terms( $p2, array( 'test1', 'test2' ), 'test_tax_cat' );
wp_set_object_terms( $p3, 'test2', 'test_tax_cat' );
wp_set_object_terms( $p4, 'test3', 'test_tax_cat' );
$url = add_query_arg(
array(
'test_tax_cat' => array( 'test1', 'test2' ),
),
'/'
);
$this->go_to( $url );
$this->assertSameSets( array( $p1, $p2, $p3 ), wp_list_pluck( $GLOBALS['wp_query']->posts, 'ID' ) );
}
/**
* @ticket 31355
*/
public function test_pages_dont_404_when_queried_post_id_is_modified() {
$post_id = self::factory()->post->create(
array(
'post_title' => 'A Test Page',
'post_type' => 'page',
)
);
add_action( 'parse_query', array( $this, 'filter_parse_query_to_modify_queried_post_id' ) );
$url = get_permalink( $post_id );
$this->go_to( $url );
remove_action( 'parse_query', array( $this, 'filter_parse_query_to_modify_queried_post_id' ) );
$this->assertFalse( $GLOBALS['wp_query']->is_404() );
$this->assertSame( $post_id, $GLOBALS['wp_query']->post->ID );
}
/**
* @ticket 31355
*/
public function test_custom_hierarchical_post_types_404_when_queried_post_id_is_modified() {
global $wp_rewrite;
register_post_type(
'guide',
array(
'name' => 'Guide',
'public' => true,
'hierarchical' => true,
)
);
$wp_rewrite->flush_rules();
$post_id = self::factory()->post->create(
array(
'post_title' => 'A Test Guide',
'post_type' => 'guide',
)
);
add_action( 'parse_query', array( $this, 'filter_parse_query_to_modify_queried_post_id' ) );
$url = get_permalink( $post_id );
$this->go_to( $url );
remove_action( 'parse_query', array( $this, 'filter_parse_query_to_modify_queried_post_id' ) );
$this->assertFalse( $GLOBALS['wp_query']->is_404() );
$this->assertSame( $post_id, $GLOBALS['wp_query']->post->ID );
}
public function filter_parse_query_to_modify_queried_post_id( $query ) {
$post = get_queried_object();
}
/**
* @ticket 34060
*/
public function test_offset_0_should_override_page() {
$q = new WP_Query(
array(
'paged' => 2,
'posts_per_page' => 5,
'offset' => 0,
)
);
$this->assertStringContainsString( 'LIMIT 0, 5', $q->request );
}
/**
* @ticket 34060
*/
public function test_offset_should_be_ignored_when_not_set() {
$q = new WP_Query(
array(
'paged' => 2,
'posts_per_page' => 5,
)
);
$this->assertStringContainsString( 'LIMIT 5, 5', $q->request );
}
/**
* @ticket 34060
*/
public function test_offset_should_be_ignored_when_passed_a_non_numeric_value() {
$q = new WP_Query(
array(
'paged' => 2,
'posts_per_page' => 5,
'offset' => '',
)
);
$this->assertStringContainsString( 'LIMIT 5, 5', $q->request );
}
/**
* @ticket 35601
*/
public function test_comment_status() {
$p1 = self::factory()->post->create( array( 'comment_status' => 'open' ) );
$p2 = self::factory()->post->create( array( 'comment_status' => 'closed' ) );
$q = new WP_Query(
array(
'fields' => 'ids',
'comment_status' => 'closed',
)
);
$this->assertSame( array( $p2 ), $q->posts );
}
/**
* @ticket 35601
*/
public function test_ping_status() {
$p1 = self::factory()->post->create( array( 'ping_status' => 'open' ) );
$p2 = self::factory()->post->create( array( 'ping_status' => 'closed' ) );
$q = new WP_Query(
array(
'fields' => 'ids',
'ping_status' => 'closed',
)
);
$this->assertSame( array( $p2 ), $q->posts );
}
/**
* @ticket 35619
*/
public function test_get_queried_object_should_return_first_of_multiple_terms() {
register_taxonomy( 'tax1', 'post' );
register_taxonomy( 'tax2', 'post' );
$term1 = self::factory()->term->create(
array(
'taxonomy' => 'tax1',
'name' => 'term1',
)
);
$term2 = self::factory()->term->create(
array(
'taxonomy' => 'tax2',
'name' => 'term2',
)
);
$post_id = self::factory()->post->create();
wp_set_object_terms( $post_id, 'term1', 'tax1' );
wp_set_object_terms( $post_id, 'term2', 'tax2' );
$this->go_to( home_url( '?tax1=term1&tax2=term2' ) );
$queried_object = get_queried_object();
$this->assertSame( 'tax1', $queried_object->taxonomy );
$this->assertSame( 'term1', $queried_object->slug );
}
/**
* @ticket 35619
*/
public function test_query_vars_should_match_first_of_multiple_terms() {
register_taxonomy( 'tax1', 'post' );
register_taxonomy( 'tax2', 'post' );
$term1 = self::factory()->term->create(
array(
'taxonomy' => 'tax1',
'name' => 'term1',
)
);
$term2 = self::factory()->term->create(
array(
'taxonomy' => 'tax2',
'name' => 'term2',
)
);
$post_id = self::factory()->post->create();
wp_set_object_terms( $post_id, 'term1', 'tax1' );
wp_set_object_terms( $post_id, 'term2', 'tax2' );
$this->go_to( home_url( '?tax1=term1&tax2=term2' ) );
$queried_object = get_queried_object();
$this->assertSame( 'tax1', get_query_var( 'taxonomy' ) );
$this->assertSame( 'term1', get_query_var( 'term' ) );
}
/**
* @ticket 55100
*/
public function test_get_queried_object_should_work_for_author_name_before_get_posts() {
$user_id = self::factory()->user->create();
$user = get_user_by( 'ID', $user_id );
$post_id = self::factory()->post->create(
array(
'post_author' => $user_id,
)
);
$this->go_to( home_url( '?author=' . $user_id ) );
$this->assertInstanceOf( 'WP_User', get_queried_object() );
$this->assertSame( get_queried_object_id(), $user_id );
$this->go_to( home_url( '?author_name=' . $user->user_nicename ) );
$this->assertInstanceOf( 'WP_User', get_queried_object() );
$this->assertSame( get_queried_object_id(), $user_id );
}
/**
* Tests that the `posts_clauses` filter receives an array of clauses
* with the other `posts_*` filters applied, e.g. `posts_join_paged`.
*
* @ticket 55699
* @covers WP_Query::get_posts
*/
public function test_posts_clauses_filter_should_receive_filtered_clauses() {
add_filter(
'posts_join_paged',
static function () {
return '/* posts_join_paged */';
}
);
$filter = new MockAction();
add_filter( 'posts_clauses', array( $filter, 'filter' ), 10, 2 );
$this->go_to( '/' );
$filter_args = $filter->get_args();
$posts_clauses = $filter_args[0][0];
$this->assertArrayHasKey( 'join', $posts_clauses );
$this->assertSame( '/* posts_join_paged */', $posts_clauses['join'] );
}
/**
* Tests that the `posts_clauses_request` filter receives an array of clauses
* with the other `posts_*_request` filters applied, e.g. `posts_join_request`.
*
* @ticket 55699
* @covers WP_Query::get_posts
*/
public function test_posts_clauses_request_filter_should_receive_filtered_clauses() {
add_filter(
'posts_join_request',
static function () {
return '/* posts_join_request */';
}
);
$filter = new MockAction();
add_filter( 'posts_clauses_request', array( $filter, 'filter' ), 10, 2 );
$this->go_to( '/' );
$filter_args = $filter->get_args();
$posts_clauses_request = $filter_args[0][0];
$this->assertArrayHasKey( 'join', $posts_clauses_request );
$this->assertSame( '/* posts_join_request */', $posts_clauses_request['join'] );
}
/**
* Tests that is_post_type_archive() returns false for an undefined post type.
*
* @ticket 56287
*
* @covers ::is_post_type_archive
*/
public function test_is_post_type_archive_should_return_false_for_an_undefined_post_type() {
global $wp_query;
$post_type = '56287-post-type';
// Force the request to be a post type archive.
$wp_query->is_post_type_archive = true;
$wp_query->set( 'post_type', $post_type );
$this->assertFalse( is_post_type_archive( $post_type ) );
}
/**
* @ticket 29660
*/
public function test_query_singular_404_does_not_throw_warning() {
$q = new WP_Query(
array(
'pagename' => 'non-existent-page',
)
);
$this->assertSame( 0, $q->post_count );
$this->assertFalse( $q->is_single() );
$this->assertTrue( $q->is_singular() );
$this->assertFalse( $q->is_singular( 'page' ) );
$this->assertTrue( $q->is_page() );
$this->assertFalse( $q->is_page( 'non-existent-page' ) );
}
/**
* @ticket 29660
*/
public function test_query_single_404_does_not_throw_warning() {
$q = new WP_Query(
array(
'name' => 'non-existent-post',
)
);
$this->assertSame( 0, $q->post_count );
$this->assertFalse( $q->is_page() );
$this->assertTrue( $q->is_singular() );
$this->assertFalse( $q->is_singular( 'post' ) );
$this->assertTrue( $q->is_single() );
$this->assertFalse( $q->is_single( 'non-existent-post' ) );
}
/**
* @ticket 29660
*/
public function test_query_attachment_404_does_not_throw_warning() {
$q = new WP_Query(
array(
'attachment' => 'non-existent-attachment',
)
);
$this->assertSame( 0, $q->post_count );
$this->assertTrue( $q->is_singular() );
$this->assertFalse( $q->is_singular( 'attachment' ) );
$this->assertTrue( $q->is_attachment() );
$this->assertFalse( $q->is_attachment( 'non-existent-attachment' ) );
}
/**
* @ticket 29660
*/
public function test_query_author_404_does_not_throw_warning() {
$q = new WP_Query(
array(
'author_name' => 'non-existent-author',
)
);
$this->assertSame( 0, $q->post_count );
$this->assertTrue( $q->is_author() );
$this->assertFalse( $q->is_author( 'non-existent-author' ) );
}
/**
* @ticket 29660
*/
public function test_query_category_404_does_not_throw_warning() {
$q = new WP_Query(
array(
'category_name' => 'non-existent-category',
)
);
$this->assertSame( 0, $q->post_count );
$this->assertTrue( $q->is_category() );
$this->assertFalse( $q->is_tax() );
$this->assertFalse( $q->is_category( 'non-existent-category' ) );
}
/**
* @ticket 29660
*/
public function test_query_tag_404_does_not_throw_warning() {
$q = new WP_Query(
array(
'tag' => 'non-existent-tag',
)
);
$this->assertSame( 0, $q->post_count );
$this->assertTrue( $q->is_tag() );
$this->assertFalse( $q->is_tax() );
$this->assertFalse( $q->is_tag( 'non-existent-tag' ) );
}
/**
* Test if $before_loop is true before loop.
*
* @ticket 58211
*/
public function test_before_loop_value_set_true_before_the_loop() {
// Get a new query with 3 posts.
$query = $this->get_new_wp_query_with_posts( 3 );
$this->assertTrue( $query->before_loop );
}
/**
* Test $before_loop value is set to false when the loop starts.
*
* @ticket 58211
*
* @covers WP_Query::the_post
*/
public function test_before_loop_value_set_to_false_in_loop_with_post() {
// Get a new query with 2 posts.
$query = $this->get_new_wp_query_with_posts( 2 );
while ( $query->have_posts() ) {
// $before_loop should be set false as soon as the_post is called for the first time.
$query->the_post();
$this->assertFalse( $query->before_loop );
break;
}
}
/**
* Test $before_loop value is set to false when there is no post in the loop.
*
* @ticket 58211
*
* @covers WP_Query::have_posts
*/
public function test_before_loop_set_false_after_loop_with_no_post() {
// New query without any posts in the result.
$query = new WP_Query(
array(
'category_name' => 'non-existent-category',
)
);
// There will not be any posts, so the loop will never actually enter.
while ( $query->have_posts() ) {
$query->the_post();
}
// Still, this should be false as there are no results and entering the loop was attempted.
$this->assertFalse( $query->before_loop );
}
/**
* Get a new query with a given number of posts.
*
* @param int $no_of_posts Number of posts to be added in the query.
*/
public function get_new_wp_query_with_posts( $no_of_posts ) {
$post_ids = self::factory()->post->create_many( $no_of_posts );
$query = new WP_Query( array( 'post__in' => $post_ids ) );
return $query;
}
}