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
729 lines (619 loc) · 17.4 KB
/
query.php
File metadata and controls
729 lines (619 loc) · 17.4 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
<?php
/**
* @group query
* @group post
*/
class Tests_Post_Query extends WP_UnitTestCase {
/**
* @group taxonomy
*/
function test_category__and_var() {
$q = new WP_Query();
$term_id = self::factory()->category->create(
array(
'slug' => 'woo',
'name' => 'WOO!',
)
);
$term_id2 = self::factory()->category->create(
array(
'slug' => 'hoo',
'name' => 'HOO!',
)
);
$post_id = self::factory()->post->create();
wp_set_post_categories( $post_id, $term_id );
$posts = $q->query( array( 'category__and' => array( $term_id ) ) );
$this->assertEmpty( $q->get( 'category__and' ) );
$this->assertCount( 0, $q->get( 'category__and' ) );
$this->assertNotEmpty( $q->get( 'category__in' ) );
$this->assertCount( 1, $q->get( 'category__in' ) );
$this->assertNotEmpty( $posts );
$this->assertEquals( array( $post_id ), wp_list_pluck( $posts, 'ID' ) );
$posts2 = $q->query( array( 'category__and' => array( $term_id, $term_id2 ) ) );
$this->assertNotEmpty( $q->get( 'category__and' ) );
$this->assertCount( 2, $q->get( 'category__and' ) );
$this->assertEmpty( $q->get( 'category__in' ) );
$this->assertCount( 0, $q->get( 'category__in' ) );
$this->assertEmpty( $posts2 );
}
/**
* @ticket 28099
* @group taxonomy
*/
function test_empty_category__in() {
$cat_id = self::factory()->category->create();
$post_id = self::factory()->post->create();
wp_set_post_categories( $post_id, $cat_id );
$q1 = get_posts( array( 'category__in' => array( $cat_id ) ) );
$this->assertNotEmpty( $q1 );
$q2 = get_posts( array( 'category__in' => array() ) );
$this->assertNotEmpty( $q2 );
$tag = wp_insert_term( 'woo', 'post_tag' );
$tag_id = $tag['term_id'];
$slug = get_tag( $tag_id )->slug;
wp_set_post_tags( $post_id, $slug );
$q3 = get_posts( array( 'tag__in' => array( $tag_id ) ) );
$this->assertNotEmpty( $q3 );
$q4 = get_posts( array( 'tag__in' => array() ) );
$this->assertNotEmpty( $q4 );
$q5 = get_posts( array( 'tag_slug__in' => array( $slug ) ) );
$this->assertNotEmpty( $q5 );
$q6 = get_posts( array( 'tag_slug__in' => array() ) );
$this->assertNotEmpty( $q6 );
}
/**
* @ticket 22448
*/
function test_the_posts_filter() {
// Create posts and clear their caches.
$post_ids = self::factory()->post->create_many( 4 );
foreach ( $post_ids as $post_id ) {
clean_post_cache( $post_id );
}
add_filter( 'the_posts', array( $this, 'the_posts_filter' ) );
$query = new WP_Query(
array(
'post_type' => 'post',
'posts_per_page' => 3,
)
);
// Fourth post added in filter
$this->assertEquals( 4, count( $query->posts ) );
$this->assertEquals( 4, $query->post_count );
foreach ( $query->posts as $post ) {
// posts are WP_Post objects
$this->assertTrue( is_a( $post, 'WP_Post' ) );
// filters are raw
$this->assertEquals( 'raw', $post->filter );
// custom data added in the_posts filter is preserved
$this->assertEquals( array( $post->ID, 'custom data' ), $post->custom_data );
}
remove_filter( 'the_posts', array( $this, 'the_posts_filter' ) );
}
/**
* Use with the_posts filter, appends a post and adds some custom data.
*/
function the_posts_filter( $posts ) {
$posts[] = clone $posts[0];
// Add some custom data to each post.
foreach ( $posts as $key => $post ) {
$posts[ $key ]->custom_data = array( $post->ID, 'custom data' );
}
return $posts;
}
function test_post__in_ordering() {
$post_id1 = self::factory()->post->create(
array(
'post_type' => 'page',
'menu_order' => rand( 1, 100 ),
)
);
$post_id2 = self::factory()->post->create(
array(
'post_type' => 'page',
'menu_order' => rand( 1, 100 ),
)
);
$post_id3 = self::factory()->post->create(
array(
'post_type' => 'page',
'post_parent' => $post_id2,
'menu_order' => rand( 1, 100 ),
)
);
$post_id4 = self::factory()->post->create(
array(
'post_type' => 'page',
'post_parent' => $post_id2,
'menu_order' => rand( 1, 100 ),
)
);
$post_id5 = self::factory()->post->create(
array(
'post_type' => 'page',
'menu_order' => rand( 1, 100 ),
)
);
$ordered = array( $post_id2, $post_id4, $post_id3, $post_id1, $post_id5 );
$q = new WP_Query(
array(
'post_type' => 'any',
'post__in' => $ordered,
'orderby' => 'post__in',
)
);
$this->assertSame( $ordered, wp_list_pluck( $q->posts, 'ID' ) );
}
/**
* @ticket 38034
*/
public function test_orderby_post__in_array() {
$posts = self::factory()->post->create_many( 4 );
$ordered = array( $posts[2], $posts[0], $posts[3] );
$q = new WP_Query(
array(
'post_type' => 'any',
'post__in' => $ordered,
'orderby' => array( 'post__in' => 'ASC' ),
)
);
$this->assertSame( $ordered, wp_list_pluck( $q->posts, 'ID' ) );
}
/**
* @ticket 38034
*/
public function test_orderby_post__in_array_with_implied_order() {
$posts = self::factory()->post->create_many( 4 );
$ordered = array( $posts[2], $posts[0], $posts[3] );
$q = new WP_Query(
array(
'post_type' => 'any',
'post__in' => $ordered,
'orderby' => 'post__in',
)
);
$this->assertSame( $ordered, wp_list_pluck( $q->posts, 'ID' ) );
}
function test_post__in_attachment_ordering() {
$post_id = self::factory()->post->create();
$att_ids = array();
$file = DIR_TESTDATA . '/images/canola.jpg';
$att_ids[1] = self::factory()->attachment->create_object(
$file,
$post_id,
array(
'post_mime_type' => 'image/jpeg',
'menu_order' => rand( 1, 100 ),
)
);
$att_ids[2] = self::factory()->attachment->create_object(
$file,
$post_id,
array(
'post_mime_type' => 'image/jpeg',
'menu_order' => rand( 1, 100 ),
)
);
$att_ids[3] = self::factory()->attachment->create_object(
$file,
$post_id,
array(
'post_mime_type' => 'image/jpeg',
'menu_order' => rand( 1, 100 ),
)
);
$att_ids[4] = self::factory()->attachment->create_object(
$file,
$post_id,
array(
'post_mime_type' => 'image/jpeg',
'menu_order' => rand( 1, 100 ),
)
);
$att_ids[5] = self::factory()->attachment->create_object(
$file,
$post_id,
array(
'post_mime_type' => 'image/jpeg',
'menu_order' => rand( 1, 100 ),
)
);
$ordered = array( $att_ids[5], $att_ids[1], $att_ids[4], $att_ids[3], $att_ids[2] );
$attached = new WP_Query(
array(
'post__in' => $ordered,
'post_type' => 'attachment',
'post_parent' => $post_id,
'post_mime_type' => 'image',
'post_status' => 'inherit',
'posts_per_page' => '-1',
'orderby' => 'post__in',
)
);
$this->assertSame( $ordered, wp_list_pluck( $attached->posts, 'ID' ) );
}
/**
* @ticket 36515
*/
public function test_post_name__in_ordering() {
$post_id1 = self::factory()->post->create(
array(
'post_name' => 'id-1',
'post_type' => 'page',
)
);
$post_id2 = self::factory()->post->create(
array(
'post_name' => 'id-2',
'post_type' => 'page',
)
);
$post_id3 = self::factory()->post->create(
array(
'post_name' => 'id-3',
'post_type' => 'page',
'post_parent' => $post_id2,
)
);
$ordered = array( 'id-2', 'id-3', 'id-1' );
$q = new WP_Query(
array(
'post_type' => 'any',
'post_name__in' => $ordered,
'orderby' => 'post_name__in',
)
);
$this->assertSame( $ordered, wp_list_pluck( $q->posts, 'post_name' ) );
}
function test_post_status() {
$statuses1 = get_post_stati();
$this->assertContains( 'auto-draft', $statuses1 );
$statuses2 = get_post_stati( array( 'exclude_from_search' => true ) );
$this->assertContains( 'auto-draft', $statuses2 );
$statuses3 = get_post_stati( array( 'exclude_from_search' => false ) );
$this->assertNotContains( 'auto-draft', $statuses3 );
$q1 = new WP_Query( array( 'post_status' => 'any' ) );
$this->assertContains( "post_status <> 'auto-draft'", $q1->request );
$q2 = new WP_Query( array( 'post_status' => 'any, auto-draft' ) );
$this->assertNotContains( "post_status <> 'auto-draft'", $q2->request );
$q3 = new WP_Query( array( 'post_status' => array( 'any', 'auto-draft' ) ) );
$this->assertNotContains( "post_status <> 'auto-draft'", $q3->request );
}
/**
* @ticket 17065
*/
function test_orderby_array() {
global $wpdb;
$q1 = new WP_Query(
array(
'orderby' => array(
'type' => 'DESC',
'name' => 'ASC',
),
)
);
$this->assertContains(
"ORDER BY $wpdb->posts.post_type DESC, $wpdb->posts.post_name ASC",
$q1->request
);
$q2 = new WP_Query( array( 'orderby' => array() ) );
$this->assertNotContains( 'ORDER BY', $q2->request );
$this->assertNotContains( 'ORDER', $q2->request );
$q3 = new WP_Query( array( 'post_type' => 'post' ) );
$this->assertContains(
"ORDER BY $wpdb->posts.post_date DESC",
$q3->request
);
$q4 = new WP_Query( array( 'post_type' => 'post' ) );
$this->assertContains(
"ORDER BY $wpdb->posts.post_date DESC",
$q4->request
);
}
/**
* @ticket 17065
*/
function test_order() {
global $wpdb;
$q1 = new WP_Query(
array(
'orderby' => array(
'post_type' => 'foo',
),
)
);
$this->assertContains(
"ORDER BY $wpdb->posts.post_type DESC",
$q1->request
);
$q2 = new WP_Query(
array(
'orderby' => 'title',
'order' => 'foo',
)
);
$this->assertContains(
"ORDER BY $wpdb->posts.post_title DESC",
$q2->request
);
$q3 = new WP_Query(
array(
'order' => 'asc',
)
);
$this->assertContains(
"ORDER BY $wpdb->posts.post_date ASC",
$q3->request
);
}
/**
* @ticket 29629
*/
function test_orderby() {
// 'rand' is a valid value
$q = new WP_Query( array( 'orderby' => 'rand' ) );
$this->assertContains( 'ORDER BY RAND()', $q->request );
$this->assertNotContains( 'ASC', $q->request );
$this->assertNotContains( 'DESC', $q->request );
// This isn't allowed
$q2 = new WP_Query( array( 'order' => 'rand' ) );
$this->assertContains( 'ORDER BY', $q2->request );
$this->assertNotContains( 'RAND()', $q2->request );
$this->assertContains( 'DESC', $q2->request );
// 'none' is a valid value
$q3 = new WP_Query( array( 'orderby' => 'none' ) );
$this->assertNotContains( 'ORDER BY', $q3->request );
$this->assertNotContains( 'DESC', $q3->request );
$this->assertNotContains( 'ASC', $q3->request );
// false is a valid value
$q4 = new WP_Query( array( 'orderby' => false ) );
$this->assertNotContains( 'ORDER BY', $q4->request );
$this->assertNotContains( 'DESC', $q4->request );
$this->assertNotContains( 'ASC', $q4->request );
// empty array() is a valid value
$q5 = new WP_Query( array( 'orderby' => array() ) );
$this->assertNotContains( 'ORDER BY', $q5->request );
$this->assertNotContains( 'DESC', $q5->request );
$this->assertNotContains( 'ASC', $q5->request );
}
/**
* @ticket 35692
*/
public function test_orderby_rand_with_seed() {
$q = new WP_Query(
array(
'orderby' => 'RAND(5)',
)
);
$this->assertContains( 'ORDER BY RAND(5)', $q->request );
}
/**
* @ticket 35692
*/
public function test_orderby_rand_should_ignore_invalid_seed() {
$q = new WP_Query(
array(
'orderby' => 'RAND(foo)',
)
);
$this->assertNotContains( 'ORDER BY RAND', $q->request );
}
/**
* @ticket 35692
*/
public function test_orderby_rand_with_seed_should_be_case_insensitive() {
$q = new WP_Query(
array(
'orderby' => 'rand(5)',
)
);
$this->assertContains( 'ORDER BY RAND(5)', $q->request );
}
/**
* Tests the post_name__in attribute of WP_Query.
*
* @ticket 33065
*/
public function test_post_name__in() {
$q = new WP_Query();
$post_ids[0] = self::factory()->post->create(
array(
'post_title' => 'woo',
'post_date' => '2015-07-23 00:00:00',
)
);
$post_ids[1] = self::factory()->post->create(
array(
'post_title' => 'hoo',
'post_date' => '2015-07-23 00:00:00',
)
);
$post_ids[2] = self::factory()->post->create(
array(
'post_title' => 'test',
'post_date' => '2015-07-23 00:00:00',
)
);
$post_ids[3] = self::factory()->post->create(
array(
'post_title' => 'me',
'post_date' => '2015-07-23 00:00:00',
)
);
$requested = array( $post_ids[0], $post_ids[3] );
$q->query(
array(
'post_name__in' => array( 'woo', 'me' ),
'fields' => 'ids',
)
);
$actual_posts = $q->get_posts();
$this->assertEqualSets( $requested, $actual_posts );
$requested = array( $post_ids[1], $post_ids[2] );
$q->query(
array(
'post_name__in' => array( 'hoo', 'test' ),
'fields' => 'ids',
)
);
$actual_posts = $q->get_posts();
$this->assertEqualSets( $requested, $actual_posts );
}
/**
* @ticket 36687
*/
public function test_posts_pre_query_filter_should_bypass_database_query() {
global $wpdb;
add_filter( 'posts_pre_query', array( __CLASS__, 'filter_posts_pre_query' ) );
$num_queries = $wpdb->num_queries;
$q = new WP_Query(
array(
'fields' => 'ids',
'no_found_rows' => true,
)
);
remove_filter( 'posts_pre_query', array( __CLASS__, 'filter_posts_pre_query' ) );
$this->assertSame( $num_queries, $wpdb->num_queries );
$this->assertSame( array( 12345 ), $q->posts );
}
public static function filter_posts_pre_query( $posts ) {
return array( 12345 );
}
/**
* @ticket 36687
*/
public function test_posts_pre_query_filter_should_respect_set_found_posts() {
global $wpdb;
$this->post_id = self::factory()->post->create();
// Prevent the DB query
add_filter( 'posts_request', '__return_empty_string' );
add_filter( 'found_posts_query', '__return_empty_string' );
// Add the post and found_posts
add_filter( 'the_posts', array( $this, 'filter_the_posts' ) );
add_filter( 'found_posts', array( $this, 'filter_found_posts' ) );
$q = new WP_Query( array( 'suppress_filters' => false ) );
remove_filter( 'posts_request', '__return_empty_string' );
remove_filter( 'found_posts_query', '__return_empty_string' );
remove_filter( 'the_posts', array( $this, 'filter_the_posts' ) );
remove_filter( 'found_posts', array( $this, 'filter_found_posts' ) );
$this->assertSame( array( $this->post_id ), wp_list_pluck( $q->posts, 'ID' ) );
$this->assertSame( 1, $q->found_posts );
}
public function filter_the_posts() {
return array( get_post( $this->post_id ) );
}
public function filter_found_posts( $posts ) {
return 1;
}
/**
* @ticket 36687
*/
public function test_set_found_posts_fields_ids() {
register_post_type( 'wptests_pt' );
$posts = self::factory()->post->create_many( 2, array( 'post_type' => 'wptests_pt' ) );
foreach ( $posts as $p ) {
clean_post_cache( $p );
}
$q = new WP_Query(
array(
'post_type' => 'wptests_pt',
'posts_per_page' => 1,
'fields' => 'ids',
)
);
$this->assertEquals( 2, $q->found_posts );
$this->assertEquals( 2, $q->max_num_pages );
}
/**
* @ticket 36687
*/
public function test_set_found_posts_fields_idparent() {
register_post_type( 'wptests_pt' );
$posts = self::factory()->post->create_many( 2, array( 'post_type' => 'wptests_pt' ) );
foreach ( $posts as $p ) {
clean_post_cache( $p );
}
$q = new WP_Query(
array(
'post_type' => 'wptests_pt',
'posts_per_page' => 1,
'fields' => 'id=>parent',
)
);
$this->assertEquals( 2, $q->found_posts );
$this->assertEquals( 2, $q->max_num_pages );
}
/**
* @ticket 36687
*/
public function test_set_found_posts_fields_split_the_query() {
register_post_type( 'wptests_pt' );
$posts = self::factory()->post->create_many( 2, array( 'post_type' => 'wptests_pt' ) );
foreach ( $posts as $p ) {
clean_post_cache( $p );
}
add_filter( 'split_the_query', '__return_true' );
$q = new WP_Query(
array(
'post_type' => 'wptests_pt',
'posts_per_page' => 1,
)
);
remove_filter( 'split_the_query', '__return_true' );
$this->assertEquals( 2, $q->found_posts );
$this->assertEquals( 2, $q->max_num_pages );
}
/**
* @ticket 36687
*/
public function test_set_found_posts_fields_not_split_the_query() {
register_post_type( 'wptests_pt' );
$posts = self::factory()->post->create_many( 2, array( 'post_type' => 'wptests_pt' ) );
foreach ( $posts as $p ) {
clean_post_cache( $p );
}
// ! $split_the_query
add_filter( 'split_the_query', '__return_false' );
$q = new WP_Query(
array(
'post_type' => 'wptests_pt',
'posts_per_page' => 1,
)
);
remove_filter( 'split_the_query', '__return_false' );
$this->assertEquals( 2, $q->found_posts );
$this->assertEquals( 2, $q->max_num_pages );
}
public function set_found_posts_provider() {
// count return 0 for null, but 1 for other data you may not expect
return array(
array( null, 0 ),
array( '', 1 ),
array( "To life, to life, l'chaim", 1 ),
array( false, 1 ),
);
}
/**
* @ticket 42860
*
* @dataProvider set_found_posts_provider
*/
public function test_set_found_posts_not_posts_as_an_array( $posts, $expected ) {
if ( version_compare( PHP_VERSION, '5.3', '<' ) ) {
$this->markTestSkipped( 'ReflectionMethod::setAccessible is only available in PHP 5.3+' );
return;
}
$q = new WP_Query(
array(
'post_type' => 'wptests_pt',
'posts_per_page' => 1,
)
);
$q->posts = $posts;
$methd = new ReflectionMethod( 'WP_Query', 'set_found_posts' );
$methd->setAccessible( true );
$methd->invoke( $q, array( 'no_found_rows' => false ), array() );
$this->assertEquals( $expected, $q->found_posts );
}
}