Tests_Term_getTerms::test_orderby_include()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 23
Code Lines 15

Duplication

Lines 20
Ratio 86.96 %

Importance

Changes 0
Metric Value
cc 1
eloc 15
nc 1
nop 0
dl 20
loc 23
rs 9.0856
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * @group taxonomy
5
 */
6
class Tests_Term_getTerms extends WP_UnitTestCase
7
{
8
    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...
9
    {
10
        parent::setUp();
11
12
        _clean_term_filters();
13
        wp_cache_delete('last_changed', 'terms');
14
    }
15
16
    /**
17
     * @ticket 37568
18
     */
19
    public function test_meta_query_args_only() 
20
    {
21
        register_taxonomy('wptests_tax', 'post', array( 'hierarchical' => true ));
22
23
        $term1 = self::factory()->term->create(array( 'taxonomy' => 'wptests_tax' ));
24
        $term2 = self::factory()->term->create(array( 'taxonomy' => 'wptests_tax' ));
25
26
        $post = self::factory()->post->create(array( 'post_type' => 'post' ));
27
28
        update_term_meta($term1, 'somekey', 'thevalue');
29
30
        wp_set_post_terms($post, array( $term1, $term2 ), 'wptests_tax');
31
32
        $found = get_terms(
33
            array(
34
            'meta_key' => 'somekey',
0 ignored issues
show
introduced by
Detected usage of meta_key, possible slow query.
Loading history...
35
            'meta_value' => 'thevalue',
0 ignored issues
show
introduced by
Detected usage of meta_value, possible slow query.
Loading history...
36
            ) 
37
        );
38
39
        $this->assertEqualSets(array( $term1 ), wp_list_pluck($found, 'term_id'));
40
    }
41
42
    /**
43
     * @ticket 35495
44
     */
45 View Code Duplication
    public function test_should_accept_an_args_array_containing_taxonomy_for_first_parameter() 
46
    {
47
        register_taxonomy('wptests_tax', 'post');
48
        $term = self::factory()->term->create(array( 'taxonomy' => 'wptests_tax' ));
49
50
        $found = get_terms(
51
            array(
52
            'taxonomy' => 'wptests_tax',
53
            'hide_empty' => false,
54
            'fields' => 'ids',
55
            'update_term_meta_cache' => false,
56
            ) 
57
        );
58
59
        $this->assertEqualSets(array( $term ), $found);
60
    }
61
62
    /**
63
     * @ticket 35495
64
     * @ticket 35381
65
     */
66 View Code Duplication
    public function test_legacy_params_as_query_string_should_be_properly_parsed() 
67
    {
68
        register_taxonomy('wptests_tax', 'post');
69
        $term = self::factory()->term->create(array( 'taxonomy' => 'wptests_tax' ));
70
71
        $found = get_terms('wptests_tax', 'hide_empty=0&fields=ids&update_term_meta_cache=0');
72
73
        $this->assertEqualSets(array( $term ), $found);
74
    }
75
76
    /**
77
     * @ticket 35495
78
     * @ticket 35381
79
     */
80 View Code Duplication
    public function test_new_params_as_query_string_should_be_properly_parsed() 
81
    {
82
        register_taxonomy('wptests_tax', 'post');
83
        $term = self::factory()->term->create(array( 'taxonomy' => 'wptests_tax' ));
84
85
        $found = get_terms('taxonomy=wptests_tax&hide_empty=0&fields=ids&update_term_meta_cache=0');
86
87
        $this->assertEqualSets(array( $term ), $found);
88
    }
89
90
    /**
91
     * @ticket 35495
92
     */
93
    public function test_excluding_taxonomy_arg_should_return_terms_from_all_taxonomies() 
94
    {
95
        register_taxonomy('wptests_tax1', 'post');
96
        register_taxonomy('wptests_tax2', 'post');
97
        $t1 = self::factory()->term->create(array( 'taxonomy' => 'wptests_tax1' ));
98
        $t2 = self::factory()->term->create(array( 'taxonomy' => 'wptests_tax2' ));
99
100
        $found = get_terms(
101
            array(
102
            'hide_empty' => false,
103
            'fields' => 'ids',
104
            'update_term_meta_cache' => false,
105
            ) 
106
        );
107
108
        // There may be other terms lying around, so don't do an exact match.
109
        $this->assertContains($t1, $found);
110
        $this->assertContains($t2, $found);
111
    }
112
113
    /**
114
     * @ticket 23326
115
     */
116
    public function test_get_terms_cache() 
117
    {
118
        global $wpdb;
119
120
        $this->set_up_three_posts_and_tags();
121
122
        $num_queries = $wpdb->num_queries;
123
124
        // last_changed and num_queries should bump
125
        $terms = get_terms('post_tag', array( 'update_term_meta_cache' => false ));
126
        $this->assertEquals(3, count($terms));
127
        $time1 = wp_cache_get('last_changed', 'terms');
128
        $this->assertNotEmpty($time1);
129
        $this->assertEquals($num_queries + 1, $wpdb->num_queries);
130
131
        $num_queries = $wpdb->num_queries;
132
133
        // Again. last_changed and num_queries should remain the same.
134
        $terms = get_terms('post_tag', array( 'update_term_meta_cache' => false ));
135
        $this->assertEquals(3, count($terms));
136
        $this->assertEquals($time1, wp_cache_get('last_changed', 'terms'));
137
        $this->assertEquals($num_queries, $wpdb->num_queries);
138
    }
139
140
    /**
141
     * @ticket 23326
142
     */
143
    public function test_get_terms_cache_should_be_missed_when_passing_number() 
144
    {
145
        global $wpdb;
146
147
        $this->set_up_three_posts_and_tags();
148
149
        // Prime cache
150
        $terms = get_terms('post_tag');
151
        $time1 = wp_cache_get('last_changed', 'terms');
152
        $num_queries = $wpdb->num_queries;
153
154
        // num_queries should bump, last_changed should remain the same.
155
        $terms = get_terms('post_tag', array( 'number' => 2 ));
156
        $this->assertEquals(2, count($terms));
157
        $this->assertEquals($time1, wp_cache_get('last_changed', 'terms'));
158
        $this->assertEquals($num_queries + 1, $wpdb->num_queries);
159
160
        $num_queries = $wpdb->num_queries;
161
162
        // Again. last_changed and num_queries should remain the same.
163
        $terms = get_terms('post_tag', array( 'number' => 2 ));
164
        $this->assertEquals(2, count($terms));
165
        $this->assertEquals($time1, wp_cache_get('last_changed', 'terms'));
166
        $this->assertEquals($num_queries, $wpdb->num_queries);
167
    }
168
169
    /**
170
     * @ticket 23326
171
     */
172
    public function test_wp_delete_term_should_invalidate_cache() 
173
    {
174
        global $wpdb;
175
176
        $this->set_up_three_posts_and_tags();
177
178
        // Prime cache
179
        $terms = get_terms('post_tag');
180
        $time1 = wp_cache_get('last_changed', 'terms');
181
        $num_queries = $wpdb->num_queries;
182
183
        // Force last_changed to bump.
184
        wp_delete_term($terms[0]->term_id, 'post_tag');
185
186
        $num_queries = $wpdb->num_queries;
187
        $this->assertNotEquals($time1, $time2 = wp_cache_get('last_changed', 'terms'));
188
189
        // last_changed and num_queries should bump after a term is deleted.
190
        $terms = get_terms('post_tag');
191
        $this->assertEquals(2, count($terms));
192
        $this->assertEquals($time2, wp_cache_get('last_changed', 'terms'));
193
        $this->assertEquals($num_queries + 1, $wpdb->num_queries);
194
195
        $num_queries = $wpdb->num_queries;
196
197
        // Again. last_changed and num_queries should remain the same.
198
        $terms = get_terms('post_tag');
199
        $this->assertEquals(2, count($terms));
200
        $this->assertEquals($time2, wp_cache_get('last_changed', 'terms'));
201
        $this->assertEquals($num_queries, $wpdb->num_queries);
202
203
        // @todo Repeat with term insert and update.
204
    }
205
206
    /**
207
     * @ticket 23506
208
     */
209
    function test_get_terms_should_allow_arbitrary_indexed_taxonomies_array() 
210
    {
211
        $term_id = self::factory()->tag->create();
212
        $terms = get_terms(array( '111' => 'post_tag' ), array( 'hide_empty' => false ));
213
        $this->assertEquals($term_id, reset($terms)->term_id);
214
    }
215
216
    /**
217
     * @ticket 13661
218
     */
219
    function test_get_terms_fields() 
220
    {
221
        $term_id1 = self::factory()->tag->create(array( 'slug' => 'woo', 'name' => 'WOO!' ));
222
        $term_id2 = self::factory()->tag->create(array( 'slug' => 'hoo', 'name' => 'HOO!', 'parent' => $term_id1 ));
223
224
        $terms_id_parent = get_terms('post_tag', array( 'hide_empty' => false, 'fields' => 'id=>parent' ));
225
        $this->assertEquals(
226
            array(
227
            $term_id1 => 0,
228
            $term_id2 => $term_id1
229
            ), $terms_id_parent 
230
        );
231
232
        $terms_ids = get_terms('post_tag', array( 'hide_empty' => false, 'fields' => 'ids' ));
233
        $this->assertEqualSets(array( $term_id1, $term_id2 ), $terms_ids);
234
235
        $terms_name = get_terms('post_tag', array( 'hide_empty' => false, 'fields' => 'names' ));
236
        $this->assertEqualSets(array( 'WOO!', 'HOO!' ), $terms_name);
237
238
        $terms_id_name = get_terms('post_tag', array( 'hide_empty' => false, 'fields' => 'id=>name' ));
239
        $this->assertEquals(
240
            array(
241
            $term_id1 => 'WOO!',
242
            $term_id2 => 'HOO!',
243
            ), $terms_id_name 
244
        );
245
246
        $terms_id_slug = get_terms('post_tag', array( 'hide_empty' => false, 'fields' => 'id=>slug' ));
247
        $this->assertEquals(
248
            array(
249
            $term_id1 => 'woo',
250
            $term_id2 => 'hoo'
251
            ), $terms_id_slug 
252
        );
253
    }
254
255
    /**
256
     * @ticket 11823
257
      */
258
    function test_get_terms_include_exclude() 
259
    {
260
        global $wpdb;
261
262
        $term_id1 = self::factory()->tag->create();
263
        $term_id2 = self::factory()->tag->create();
264
        $inc_terms = get_terms(
265
            'post_tag', array(
266
            'include' => array( $term_id1, $term_id2 ),
267
            'hide_empty' => false
268
            ) 
269
        );
270
        $this->assertEquals(array( $term_id1, $term_id2 ), wp_list_pluck($inc_terms, 'term_id'));
271
272
        $exc_terms = get_terms(
273
            'post_tag', array(
274
            'exclude' => array( $term_id1, $term_id2 ),
275
            'hide_empty' => false
276
            ) 
277
        );
278
        $this->assertEquals(array(), wp_list_pluck($exc_terms, 'term_id'));
279
280
        // These should not generate query errors.
281
        get_terms('post_tag', array( 'exclude' => array( 0 ), 'hide_empty' => false ));
282
        $this->assertEmpty($wpdb->last_error);
283
284
        get_terms('post_tag', array( 'exclude' => array( 'unexpected-string' ), 'hide_empty' => false ));
285
        $this->assertEmpty($wpdb->last_error);
286
287
        get_terms('post_tag', array( 'include' => array( 'unexpected-string' ), 'hide_empty' => false ));
288
        $this->assertEmpty($wpdb->last_error);
289
    }
290
291
    /**
292
     * @ticket 30275
293
     */
294
    public function test_exclude_with_hierarchical_true_for_non_hierarchical_taxonomy() 
295
    {
296
        register_taxonomy('wptests_tax', 'post');
297
298
        $terms = self::factory()->term->create_many(
299
            2, array(
300
            'taxonomy' => 'wptests_tax',
301
            ) 
302
        );
303
304
        $found = get_terms(
305
            'wptests_tax', array(
306
            'taxonomy' => 'wptests_tax',
307
            'hide_empty' => false,
308
            'exclude_tree' => array( $terms[0] ),
309
            'hierarchical' => true,
310
            ) 
311
        );
312
313
        $this->assertEquals(array( $terms[1] ), wp_list_pluck($found, 'term_id'));
314
315
        _unregister_taxonomy('wptests_tax');
316
    }
317
318
    /**
319
     * @ticket 25710
320
     */
321 View Code Duplication
    function test_get_terms_exclude_tree() 
322
    {
323
324
        $term_id_uncategorized = get_option('default_category');
325
326
        $term_id1 = self::factory()->category->create();
327
        $term_id11 = self::factory()->category->create(array( 'parent' => $term_id1 ));
328
        $term_id2 = self::factory()->category->create();
329
        $term_id22 = self::factory()->category->create(array( 'parent' => $term_id2 ));
330
331
        $terms = get_terms(
332
            'category', array(
333
            'exclude' => $term_id_uncategorized,
334
            'fields' => 'ids',
335
            'hide_empty' => false,
336
            ) 
337
        );
338
        $this->assertEquals(array( $term_id1, $term_id11, $term_id2, $term_id22 ), $terms);
339
340
        $terms = get_terms(
341
            'category', array(
342
            'fields' => 'ids',
343
            'exclude_tree' => "$term_id1,$term_id_uncategorized",
344
            'hide_empty' => false,
345
            ) 
346
        );
347
348
        $this->assertEquals(array( $term_id2, $term_id22 ), $terms);
349
350
    }
351
352
    /**
353
     * @ticket 13992
354
     */
355 View Code Duplication
    function test_get_terms_search() 
356
    {
357
        $term_id1 = self::factory()->tag->create(array( 'slug' => 'burrito' ));
358
        $term_id2 = self::factory()->tag->create(array( 'name' => 'Wilbur' ));
359
        $term_id3 = self::factory()->tag->create(array( 'name' => 'Foo' ));
360
361
        $terms = get_terms('post_tag', array( 'hide_empty' => false, 'search' => 'bur', 'fields' => 'ids' ));
362
        $this->assertEqualSets(array( $term_id1, $term_id2 ), $terms);
363
    }
364
365
    /**
366
     * @ticket 8214
367
     */
368
    function test_get_terms_like() 
369
    {
370
        $term_id1 = self::factory()->tag->create(array( 'name' => 'burrito', 'description' => 'This is a burrito.' ));
371
        $term_id2 = self::factory()->tag->create(array( 'name' => 'taco', 'description' => 'Burning man.' ));
372
373
        $terms = get_terms('post_tag', array( 'hide_empty' => false, 'name__like' => 'bur', 'fields' => 'ids' ));
374
        $this->assertEqualSets(array( $term_id1 ), $terms);
375
376
        $terms2 = get_terms('post_tag', array( 'hide_empty' => false, 'description__like' => 'bur', 'fields' => 'ids' ));
377
        $this->assertEqualSets(array( $term_id1, $term_id2 ), $terms2);
378
379
        $terms3 = get_terms('post_tag', array( 'hide_empty' => false, 'name__like' => 'Bur', 'fields' => 'ids' ));
380
        $this->assertEqualSets(array( $term_id1 ), $terms3);
381
382
        $terms4 = get_terms('post_tag', array( 'hide_empty' => false, 'description__like' => 'Bur', 'fields' => 'ids' ));
383
        $this->assertEqualSets(array( $term_id1, $term_id2 ), $terms4);
384
385
        $terms5 = get_terms('post_tag', array( 'hide_empty' => false, 'name__like' => 'ENCHILADA', 'fields' => 'ids' ));
386
        $this->assertEmpty($terms5);
387
388
        $terms6 = get_terms('post_tag', array( 'hide_empty' => false, 'description__like' => 'ENCHILADA', 'fields' => 'ids' ));
389
        $this->assertEmpty($terms6);
390
391
        $terms7 = get_terms('post_tag', array( 'hide_empty' => false, 'name__like' => 'o', 'fields' => 'ids' ));
392
        $this->assertEqualSets(array( $term_id1, $term_id2 ), $terms7);
393
394
        $terms8 = get_terms('post_tag', array( 'hide_empty' => false, 'description__like' => '.', 'fields' => 'ids' ));
395
        $this->assertEqualSets(array( $term_id1, $term_id2 ), $terms8);
396
    }
397
398
    /**
399
     * @ticket 26903
400
     */
401
    function test_get_terms_parent_zero() 
402
    {
403
        $tax = 'food';
404
        register_taxonomy($tax, 'post', array( 'hierarchical' => true ));
405
406
        $cheese = self::factory()->term->create(array( 'name' => 'Cheese', 'taxonomy' => $tax ));
407
408
        $cheddar = self::factory()->term->create(array( 'name' => 'Cheddar', 'parent' => $cheese, 'taxonomy' => $tax ));
409
410
        $post_ids = self::factory()->post->create_many(2);
411
        foreach ( $post_ids as $id ) {
412
            wp_set_post_terms($id, $cheddar, $tax);
413
        }
414
        $term = get_term($cheddar, $tax);
415
        $this->assertEquals(2, $term->count);
416
417
        $brie = self::factory()->term->create(array( 'name' => 'Brie', 'parent' => $cheese, 'taxonomy' => $tax ));
418
        $post_id = self::factory()->post->create();
419
        wp_set_post_terms($post_id, $brie, $tax);
420
        $term = get_term($brie, $tax);
421
        $this->assertEquals(1, $term->count);
422
423
        $crackers = self::factory()->term->create(array( 'name' => 'Crackers', 'taxonomy' => $tax ));
424
425
        $butter = self::factory()->term->create(array( 'name' => 'Butter', 'parent' => $crackers, 'taxonomy' => $tax ));
426
        $post_ids = self::factory()->post->create_many(1);
427
        foreach ( $post_ids as $id ) {
428
            wp_set_post_terms($id, $butter, $tax);
429
        }
430
        $term = get_term($butter, $tax);
431
        $this->assertEquals(1, $term->count);
432
433
        $multigrain = self::factory()->term->create(array( 'name' => 'Multigrain', 'parent' => $crackers, 'taxonomy' => $tax ));
434
        $post_ids = self::factory()->post->create_many(1);
435
        foreach ( $post_ids as $id ) {
436
            wp_set_post_terms($id, $multigrain, $tax);
437
        }
438
        $term = get_term($multigrain, $tax);
439
        $this->assertEquals(1, $term->count);
440
441
        $fruit = self::factory()->term->create(array( 'name' => 'Fruit', 'taxonomy' => $tax ));
442
        $cranberries = self::factory()->term->create(array( 'name' => 'Cranberries', 'parent' => $fruit, 'taxonomy' => $tax ));
443
444
        $terms = get_terms($tax, array( 'parent' => 0, 'cache_domain' => $tax ));
445
        $this->assertEquals(2, count($terms));
446
        $this->assertEquals(wp_list_pluck($terms, 'name'), array( 'Cheese', 'Crackers' ));
447
    }
448
449
    /**
450
     * @ticket 26903
451
     */
452
    function test_get_terms_grandparent_zero() 
453
    {
454
        $tax = 'food';
455
        register_taxonomy($tax, 'post', array( 'hierarchical' => true ));
456
457
        $cheese = self::factory()->term->create(array( 'name' => 'Cheese', 'taxonomy' => $tax ));
458
        $cheddar = self::factory()->term->create(array( 'name' => 'Cheddar', 'parent' => $cheese, 'taxonomy' => $tax ));
459
        $spread = self::factory()->term->create(array( 'name' => 'Spread', 'parent' => $cheddar, 'taxonomy' => $tax ));
460
        $post_id = self::factory()->post->create();
461
        wp_set_post_terms($post_id, $spread, $tax);
462
        $term = get_term($spread, $tax);
463
        $this->assertEquals(1, $term->count);
464
465
        $terms = get_terms($tax, array( 'parent' => 0, 'cache_domain' => $tax ));
466
        $this->assertEquals(1, count($terms));
467
        $this->assertEquals(array( 'Cheese' ), wp_list_pluck($terms, 'name'));
468
469
        _unregister_taxonomy($tax);
470
    }
471
472
    /**
473
     * @ticket 26903
474
     */
475
    function test_get_terms_seven_levels_deep() 
476
    {
477
        $tax = 'deep';
478
        register_taxonomy($tax, 'post', array( 'hierarchical' => true ));
479
        $parent = 0;
480
        $t = array();
481
        foreach ( range(1, 7) as $depth ) {
482
            $t[$depth] = self::factory()->term->create(array( 'name' => 'term' . $depth, 'taxonomy' => $tax, 'parent' => $parent ));
0 ignored issues
show
introduced by
Array keys should be surrounded by spaces unless they contain a string or an integer.
Loading history...
483
            $parent = $t[$depth];
0 ignored issues
show
introduced by
Array keys should be surrounded by spaces unless they contain a string or an integer.
Loading history...
484
        }
485
        $post_id = self::factory()->post->create();
486
        wp_set_post_terms($post_id, $t[7], $tax);
487
        $term = get_term($t[7], $tax);
488
        $this->assertEquals(1, $term->count);
489
490
        $terms = get_terms($tax, array( 'parent' => 0, 'cache_domain' => $tax ));
491
        $this->assertEquals(1, count($terms));
492
        $this->assertEquals(array( 'term1' ), wp_list_pluck($terms, 'name'));
493
494
        _unregister_taxonomy($tax);
495
    }
496
497
    /**
498
     * @ticket 27123
499
     */
500 View Code Duplication
    function test_get_terms_child_of() 
501
    {
502
        $parent = self::factory()->category->create();
503
        $child = self::factory()->category->create(array( 'parent' => $parent ));
504
505
        $terms = get_terms('category', array( 'child_of' => $parent, 'hide_empty' => false ));
506
        $this->assertEquals(1, count($terms));
507
    }
508
509
    /**
510
     * @ticket 31118
511
     */
512 View Code Duplication
    public function test_child_of_should_skip_query_when_specified_parent_is_not_found_in_hierarchy_cache() 
513
    {
514
        global $wpdb;
515
516
        register_taxonomy('wptests_tax', 'post', array( 'hierarchical' => true, ));
517
518
        $terms = self::factory()->term->create_many(3, array( 'taxonomy' => 'wptests_tax' ));
519
520
        $num_queries = $wpdb->num_queries;
521
522
        $found = get_terms(
523
            'wptests_tax', array(
524
            'hide_empty' => false,
525
            'child_of' => $terms[0],
526
            ) 
527
        );
528
529
        $this->assertEmpty($found);
530
        $this->assertSame($num_queries, $wpdb->num_queries);
531
    }
532
533
    /**
534
     * @ticket 31118
535
     */
536 View Code Duplication
    public function test_child_of_should_respect_multiple_taxonomies() 
537
    {
538
        register_taxonomy('wptests_tax1', 'post', array( 'hierarchical' => true ));
539
        register_taxonomy('wptests_tax2', 'post', array( 'hierarchical' => true ));
540
541
        $t1 = self::factory()->term->create(array( 'taxonomy' => 'wptests_tax1' ));
542
        $t2 = self::factory()->term->create(array( 'taxonomy' => 'wptests_tax2' ));
543
        $t3 = self::factory()->term->create(array( 'taxonomy' => 'wptests_tax2', 'parent' => $t2 ));
544
545
        $found = get_terms(
546
            array( 'wptests_tax1', 'wptests_tax2' ), array(
547
            'fields' => 'ids',
548
            'hide_empty' => false,
549
            'child_of' => $t2,
550
            ) 
551
        );
552
553
        $this->assertEqualSets(array( $t3 ), $found);
554
    }
555
556
    /**
557
     * @ticket 27123
558
     */
559
    function test_get_term_children_recursion() 
560
    {
561
        // Assume there is a way to insert a term with the parent pointing to itself
562
        // See: https://core.trac.wordpress.org/changeset/15806
563
        remove_filter('wp_update_term_parent', 'wp_check_term_hierarchy_for_loops', 10);
564
565
        $term = wp_insert_term('Test', 'category');
566
        $term = wp_update_term($term['term_id'], 'category', array( 'parent' => $term['term_id'] ));
567
        $term = get_term($term['term_id'], 'category');
568
569
        $this->assertEquals($term->term_id, $term->parent);
570
        $this->assertInternalType('array', get_term_children($term->term_id, 'category'));
571
572
        add_filter('wp_update_term_parent', 'wp_check_term_hierarchy_for_loops', 10, 3);
573
    }
574
575
    /**
576
     * @covers ::_get_term_children
577
     * @ticket 24461
578
     */
579
    public function test__get_term_children_handles_cycles() 
580
    {
581
        remove_filter('wp_update_term_parent', 'wp_check_term_hierarchy_for_loops', 10);
582
583
        $c1 = self::factory()->category->create();
584
        $c2 = self::factory()->category->create(array( 'parent' => $c1 ));
585
        $c3 = self::factory()->category->create(array( 'parent' => $c2 ));
586
        wp_update_term($c1, 'category', array( 'parent' => $c3 ));
587
588
        add_filter('wp_update_term_parent', 'wp_check_term_hierarchy_for_loops', 10, 3);
589
590
        $result = _get_term_children($c1, array( $c1, $c2, $c3 ), 'category');
591
592
        $this->assertEqualSets(array( $c2, $c3 ), $result);
593
    }
594
595
    /**
596
     * @covers ::_get_term_children
597
     * @ticket 24461
598
     */
599
    public function test__get_term_children_handles_cycles_when_terms_argument_contains_objects() 
600
    {
601
        remove_filter('wp_update_term_parent', 'wp_check_term_hierarchy_for_loops', 10);
602
603
        $c1 = self::factory()->category->create_and_get();
604
        $c2 = self::factory()->category->create_and_get(array( 'parent' => $c1->term_id ));
605
        $c3 = self::factory()->category->create_and_get(array( 'parent' => $c2->term_id ));
606
        wp_update_term($c1->term_id, 'category', array( 'parent' => $c3->term_id ));
607
608
        add_filter('wp_update_term_parent', 'wp_check_term_hierarchy_for_loops', 10, 3);
609
610
        $result = _get_term_children($c1->term_id, array( $c1, $c2, $c3 ), 'category');
611
612
        $this->assertEqualSets(array( $c2, $c3 ), $result);
613
    }
614
615 View Code Duplication
    public function test_get_terms_by_slug() 
616
    {
617
        $t1 = self::factory()->tag->create(array( 'slug' => 'foo' ));
618
        $t2 = self::factory()->tag->create(array( 'slug' => 'bar' ));
619
620
        $found = get_terms(
621
            'post_tag', array(
622
            'hide_empty' => false,
623
            'fields' => 'ids',
624
            'slug' => 'foo',
625
            ) 
626
        );
627
628
        $this->assertEquals(array( $t1 ), $found);
629
    }
630
631
    /**
632
     * @ticket 23636
633
     */
634 View Code Duplication
    public function test_get_terms_by_multiple_slugs() 
635
    {
636
        $t1 = self::factory()->tag->create(array( 'slug' => 'foo' ));
637
        $t2 = self::factory()->tag->create(array( 'slug' => 'bar' ));
638
        $t3 = self::factory()->tag->create(array( 'slug' => 'barry' ));
639
640
        $found = get_terms(
641
            'post_tag', array(
642
            'hide_empty' => false,
643
            'fields' => 'ids',
644
            'slug' => array( 'foo', 'barry' )
645
            ) 
646
        );
647
648
        $this->assertEquals(array( $t1, $t3 ), $found);
649
    }
650
651
    /**
652
     * @ticket 30611
653
     */
654 View Code Duplication
    public function test_get_terms_by_name() 
655
    {
656
        $t1 = self::factory()->tag->create(array( 'name' => 'Foo' ));
657
        $t2 = self::factory()->tag->create(array( 'name' => 'Bar' ));
658
659
        $found = get_terms(
660
            'post_tag', array(
661
            'hide_empty' => false,
662
            'fields' => 'ids',
663
            'name' => 'Foo',
664
            ) 
665
        );
666
667
        $this->assertEquals(array( $t1 ), $found);
668
    }
669
670
    /**
671
     * @ticket 30611
672
     */
673 View Code Duplication
    public function test_get_terms_by_multiple_names() 
674
    {
675
        $t1 = self::factory()->tag->create(array( 'name' => 'Foo' ));
676
        $t2 = self::factory()->tag->create(array( 'name' => 'Bar' ));
677
        $t3 = self::factory()->tag->create(array( 'name' => 'Barry' ));
678
679
        $found = get_terms(
680
            'post_tag', array(
681
            'hide_empty' => false,
682
            'fields' => 'ids',
683
            'name' => array( 'Foo', 'Barry' )
684
            ) 
685
        );
686
687
        $this->assertEqualSets(array( $t3, $t1 ), $found);
688
    }
689
690
    /**
691
     * @ticket 32248
692
     */
693
    public function test_name_should_match_encoded_html_entities() 
694
    {
695
        register_taxonomy('wptests_tax', 'post');
696
697
        $t = self::factory()->term->create(
698
            array(
699
            'taxonomy' => 'wptests_tax',
700
            'name' => 'Foo & Bar',
701
            'slug' => 'foo-and-bar',
702
            ) 
703
        );
704
705
        $found = get_terms(
706
            'wptests_tax', array(
707
            'hide_empty' => false,
708
            'fields' => 'ids',
709
            'name' => 'Foo & Bar',
710
            ) 
711
        );
712
        $this->assertEqualSets(array( $t ), $found);
713
714
        // array format.
715
        $found = get_terms(
716
            'wptests_tax', array(
717
            'hide_empty' => false,
718
            'fields' => 'ids',
719
            'name' => array( 'Foo & Bar' ),
720
            ) 
721
        );
722
        $this->assertEqualSets(array( $t ), $found);
723
    }
724
725
    /**
726
     * @ticket 35493
727
     */
728
    public function test_name_should_not_double_escape_apostrophes() 
729
    {
730
        register_taxonomy('wptests_tax', 'post');
731
732
        $name = "Foo'Bar";
733
734
        $t = self::factory()->term->create(
735
            array(
736
            'taxonomy' => 'wptests_tax',
737
            'name' => $name,
738
            ) 
739
        );
740
741
        $term = get_term($t, 'wptests_tax');
742
743
        $this->assertSame($name, $term->name);
744
745
        $found = get_terms(
746
            'wptests_tax', array(
747
            'hide_empty' => false,
748
            'fields' => 'ids',
749
            'name' => $name,
750
            ) 
751
        );
752
753
        $this->assertEqualSets(array( $t ), $found);
754
    }
755
756
    /**
757
     * @ticket 29839
758
     */
759
    public function test_childless_should_return_all_terms_for_flat_hierarchy() 
760
    {
761
        // If run on a flat hierarchy it should return everything.
762
        $flat_tax = 'countries';
763
        register_taxonomy($flat_tax, 'post', array( 'hierarchical' => false ));
764
        $australia = self::factory()->term->create(array( 'name' => 'Australia', 'taxonomy' => $flat_tax ));
765
        $china     = self::factory()->term->create(array( 'name' => 'China',     'taxonomy' => $flat_tax ));
766
        $tanzania  =  self::factory()->term->create(array( 'name' => 'Tanzania',  'taxonomy' => $flat_tax ));
0 ignored issues
show
introduced by
Expected 1 space after "="; 2 found
Loading history...
767
768
        $terms = get_terms(
769
            $flat_tax, array(
770
            'childless' => true,
771
            'hide_empty' => false,
772
            'fields' => 'ids',
773
            ) 
774
        );
775
776
        $expected = array( $australia, $china, $tanzania );
777
        $this->assertEqualSets($expected, $terms);
778
    }
779
780
781
    /**
782
     * @ticket 29839
783
     */
784
    public function test_childless_hierarchical_taxonomy() 
785
    {
786
        $tax = 'location';
787
        register_taxonomy($tax, 'post', array( 'hierarchical' => true ));
788
        /*
789
        Canada
790
         Ontario
791
          Ottawa
792
        Nepean
793
          Toronto
794
         Quebec
795
          Montreal
796
         PEI
797
        */
798
        // Level 1
799
        $canada = self::factory()->term->create(array( 'name' => 'Canada', 'taxonomy' => $tax ));
800
801
        // Level 2
802
        $ontario = self::factory()->term->create(array( 'name' => 'Ontario', 'parent' => $canada, 'taxonomy' => $tax ));
803
        $quebec  = self::factory()->term->create(array( 'name' => 'Quebec', 'parent' => $canada, 'taxonomy' => $tax ));
804
        $pei     = self::factory()->term->create(array( 'name' => 'PEI', 'parent' => $canada, 'taxonomy' => $tax ));
805
806
        // Level 3
807
        $toronto  = self::factory()->term->create(array( 'name' => 'Toronto', 'parent' => $ontario, 'taxonomy' => $tax ));
808
        $ottawa   = self::factory()->term->create(array( 'name' => 'Ottawa', 'parent' => $ontario, 'taxonomy' => $tax ));
809
        $montreal = self::factory()->term->create(array( 'name' => 'Montreal', 'parent' => $quebec, 'taxonomy' => $tax ));
810
811
        // Level 4
812
        $nepean = self::factory()->term->create(array( 'name' => 'Nepean', 'parent' => $ottawa, 'taxonomy' => $tax ));
813
814
        $terms = get_terms(
815
            $tax, array(
816
            'childless' => true,
817
            'hide_empty' => false,
818
            'fields' => 'ids',
819
            ) 
820
        );
821
822
        $this->assertEqualSets(array( $montreal, $nepean, $toronto, $pei ), $terms);
823
    }
824
825
    /**
826
     * @ticket 29839
827
     */
828
    public function test_childless_hierarchical_taxonomy_used_with_child_of() 
829
    {
830
        $tax = 'location';
831
        register_taxonomy($tax, 'post', array( 'hierarchical' => true ));
832
833
        // Level 1
834
        $canada = self::factory()->term->create(array( 'name' => 'Canada', 'taxonomy' => $tax ));
835
836
        // Level 2
837
        $ontario = self::factory()->term->create(array( 'name' => 'Ontario', 'parent' => $canada, 'taxonomy' => $tax ));
838
        $quebec  = self::factory()->term->create(array( 'name' => 'Quebec', 'parent' => $canada, 'taxonomy' => $tax ));
839
840
        // Level 3
841
        $laval   = self::factory()->term->create(array( 'name' => 'Laval', 'parent' => $quebec, 'taxonomy' => $tax ));
842
        $montreal = self::factory()->term->create(array( 'name' => 'Montreal', 'parent' => $quebec, 'taxonomy' => $tax ));
843
844
        // Level 4
845
        $dorval = self::factory()->term->create(array( 'name' => 'Dorval', 'parent' => $montreal, 'taxonomy' => $tax ));
846
847
        $terms = get_terms(
848
            $tax, array(
849
            'childless' => true,
850
            'child_of' => $quebec,
851
            'hide_empty' => false,
852
            'fields' => 'ids',
853
            ) 
854
        );
855
856
        $this->assertEqualSets(array( $laval ), $terms);
857
    }
858
859
    /**
860
     * @ticket 29839
861
     */
862
    public function test_childless_should_enforce_childless_status_for_all_queried_taxonomies() 
863
    {
864
        register_taxonomy('wptests_tax1', 'post', array( 'hierarchical' => true ));
865
        register_taxonomy('wptests_tax2', 'post', array( 'hierarchical' => true ));
866
867
        $t1 = self::factory()->term->create(array( 'taxonomy' => 'wptests_tax1' ));
868
        $t2 = self::factory()->term->create(array( 'taxonomy' => 'wptests_tax1', 'parent' => $t1 ));
869
        $t3 = self::factory()->term->create(array( 'taxonomy' => 'wptests_tax2' ));
870
        $t4 = self::factory()->term->create(array( 'taxonomy' => 'wptests_tax2', 'parent' => $t3 ));
871
872
        $found = get_terms(
873
            array( 'wptests_tax1', 'wptests_tax2' ), array(
874
            'fields' => 'ids',
875
            'hide_empty' => false,
876
            'childless' => true,
877
            ) 
878
        );
879
880
        $this->assertEqualSets(array( $t2, $t4 ), $found);
881
    }
882
883 View Code Duplication
    public function test_get_terms_hierarchical_tax_hide_empty_false_fields_ids() 
884
    {
885
        // Set up a clean taxonomy.
886
        $tax = 'hierarchical_fields';
887
        register_taxonomy($tax, 'post', array( 'hierarchical' => true ));
888
889
        $terms = $this->create_hierarchical_terms_and_posts();
890
891
        $found = get_terms(
892
            $tax, array(
893
            'hide_empty' => false,
894
            'fields' => 'ids',
895
            ) 
896
        );
897
898
        $expected = array(
899
         $terms['parent1'],
900
         $terms['parent2'],
901
         $terms['child1'],
902
         $terms['child2'],
903
         $terms['grandchild1'],
904
        );
905
906
        _unregister_taxonomy('hierarchical_fields');
907
908
        $this->assertEqualSets($expected, $found);
909
    }
910
911
    public function test_get_terms_hierarchical_tax_hide_empty_true_fields_ids() 
912
    {
913
        // Set up a clean taxonomy.
914
        $tax = 'hierarchical_fields';
915
        register_taxonomy($tax, 'post', array( 'hierarchical' => true ));
916
917
        $terms = $this->create_hierarchical_terms_and_posts();
918
919
        $found = get_terms(
920
            $tax, array(
921
            'hide_empty' => true,
922
            'fields' => 'ids',
923
            ) 
924
        );
925
926
        $expected = array(
927
         $terms['parent1'],
928
         $terms['parent2'],
929
         $terms['child1'],
930
        );
931
932
        _unregister_taxonomy('hierarchical_fields');
933
934
        $this->assertEqualSets($expected, $found);
935
    }
936
937 View Code Duplication
    public function test_get_terms_hierarchical_tax_hide_empty_true_fields_ids_hierarchical_false() 
938
    {
939
        // Set up a clean taxonomy.
940
        $tax = 'hierarchical_fields';
941
        register_taxonomy($tax, 'post', array( 'hierarchical' => true ));
942
943
        $terms = $this->create_hierarchical_terms_and_posts();
944
945
        $found = get_terms(
946
            $tax, array(
947
            'hide_empty' => true,
948
            'fields' => 'ids',
949
            'hierarchical' => false,
950
            ) 
951
        );
952
953
        $expected = array(
954
         $terms['parent2'],
955
         $terms['child1'],
956
        );
957
958
        _unregister_taxonomy('hierarchical_fields');
959
960
        $this->assertEqualSets($expected, $found);
961
    }
962
963
    public function test_get_terms_hierarchical_tax_hide_empty_false_fields_names() 
964
    {
965
        // Set up a clean taxonomy.
966
        $tax = 'hierarchical_fields';
967
        register_taxonomy($tax, 'post', array( 'hierarchical' => true ));
968
969
        $terms = $this->create_hierarchical_terms_and_posts();
970
971
        $found = get_terms(
972
            $tax, array(
973
            'hide_empty' => false,
974
            'fields' => 'names',
975
            ) 
976
        );
977
978
        $expected = array(
979
         'Parent 1',
980
         'Parent 2',
981
         'Child 1',
982
         'Child 2',
983
         'Grandchild 1',
984
        );
985
986
        _unregister_taxonomy('hierarchical_fields');
987
988
        $this->assertEqualSets($expected, $found);
989
    }
990
991 View Code Duplication
    public function test_get_terms_hierarchical_tax_hide_empty_true_fields_names() 
992
    {
993
        // Set up a clean taxonomy.
994
        $tax = 'hierarchical_fields';
995
        register_taxonomy($tax, 'post', array( 'hierarchical' => true ));
996
997
        $terms = $this->create_hierarchical_terms_and_posts();
998
999
        $found = get_terms(
1000
            $tax, array(
1001
            'hide_empty' => true,
1002
            'fields' => 'names',
1003
            ) 
1004
        );
1005
1006
        $expected = array(
1007
         'Parent 1',
1008
         'Parent 2',
1009
         'Child 1',
1010
        );
1011
1012
        _unregister_taxonomy('hierarchical_fields');
1013
1014
        $this->assertEqualSets($expected, $found);
1015
    }
1016
1017 View Code Duplication
    public function test_get_terms_hierarchical_tax_hide_empty_true_fields_names_hierarchical_false() 
1018
    {
1019
        // Set up a clean taxonomy.
1020
        $tax = 'hierarchical_fields';
1021
        register_taxonomy($tax, 'post', array( 'hierarchical' => true ));
1022
1023
        $terms = $this->create_hierarchical_terms_and_posts();
1024
1025
        $found = get_terms(
1026
            $tax, array(
1027
            'hide_empty' => true,
1028
            'fields' => 'names',
1029
            'hierarchical' => false,
1030
            ) 
1031
        );
1032
1033
        $expected = array(
1034
         'Parent 2',
1035
         'Child 1',
1036
        );
1037
1038
        _unregister_taxonomy('hierarchical_fields');
1039
1040
        $this->assertEqualSets($expected, $found);
1041
    }
1042
1043 View Code Duplication
    public function test_get_terms_hierarchical_tax_hide_empty_false_fields_count() 
1044
    {
1045
        // Set up a clean taxonomy.
1046
        $tax = 'hierarchical_fields';
1047
        register_taxonomy($tax, 'post', array( 'hierarchical' => true ));
1048
1049
        $terms = $this->create_hierarchical_terms_and_posts();
1050
1051
        $found = get_terms(
1052
            $tax, array(
1053
            'hide_empty' => false,
1054
            'fields' => 'count',
1055
            ) 
1056
        );
1057
1058
        _unregister_taxonomy('hierarchical_fields');
1059
1060
        $this->assertEquals(5, $found);
1061
    }
1062
1063 View Code Duplication
    public function test_get_terms_hierarchical_tax_hide_empty_true_fields_count() 
1064
    {
1065
        // Set up a clean taxonomy.
1066
        $tax = 'hierarchical_fields';
1067
        register_taxonomy($tax, 'post', array( 'hierarchical' => true ));
1068
1069
        $terms = $this->create_hierarchical_terms_and_posts();
1070
1071
        $found = get_terms(
1072
            $tax, array(
1073
            'hide_empty' => true,
1074
            'fields' => 'count',
1075
            ) 
1076
        );
1077
1078
        _unregister_taxonomy('hierarchical_fields');
1079
1080
        // When using 'fields=count', 'hierarchical' is forced to false.
1081
        $this->assertEquals(2, $found);
1082
    }
1083
1084 View Code Duplication
    public function test_get_terms_hierarchical_tax_hide_empty_true_fields_count_hierarchical_false() 
1085
    {
1086
        // Set up a clean taxonomy.
1087
        $tax = 'hierarchical_fields';
1088
        register_taxonomy($tax, 'post', array( 'hierarchical' => true ));
1089
1090
        $terms = $this->create_hierarchical_terms_and_posts();
1091
1092
        $found = get_terms(
1093
            $tax, array(
1094
            'hide_empty' => true,
1095
            'fields' => 'count',
1096
            'hierarchical' => false,
1097
            ) 
1098
        );
1099
1100
        _unregister_taxonomy('hierarchical_fields');
1101
1102
        $this->assertEquals(2, $found);
1103
    }
1104
1105
    public function test_get_terms_hierarchical_tax_hide_empty_false_fields_idparent() 
1106
    {
1107
        // Set up a clean taxonomy.
1108
        $tax = 'hierarchical_fields';
1109
        register_taxonomy($tax, 'post', array( 'hierarchical' => true ));
1110
1111
        $terms = $this->create_hierarchical_terms_and_posts();
1112
1113
        $found = get_terms(
1114
            $tax, array(
1115
            'hide_empty' => false,
1116
            'fields' => 'id=>parent',
1117
            ) 
1118
        );
1119
1120
        $expected = array(
1121
         $terms['parent1'] => 0,
1122
         $terms['parent2'] => 0,
1123
         $terms['child1'] => $terms['parent1'],
1124
         $terms['child2'] => $terms['parent1'],
1125
         $terms['grandchild1'] => $terms['child1'],
1126
        );
1127
1128
        _unregister_taxonomy('hierarchical_fields');
1129
1130
        $this->assertEqualSetsWithIndex($expected, $found);
1131
    }
1132
1133 View Code Duplication
    public function test_get_terms_hierarchical_tax_hide_empty_true_fields_idparent() 
1134
    {
1135
        // Set up a clean taxonomy.
1136
        $tax = 'hierarchical_fields';
1137
        register_taxonomy($tax, 'post', array( 'hierarchical' => true ));
1138
1139
        $terms = $this->create_hierarchical_terms_and_posts();
1140
1141
        $found = get_terms(
1142
            $tax, array(
1143
            'hide_empty' => true,
1144
            'fields' => 'id=>parent',
1145
            ) 
1146
        );
1147
1148
        $expected = array(
1149
         $terms['parent1'] => 0,
1150
         $terms['parent2'] => 0,
1151
         $terms['child1'] => $terms['parent1'],
1152
        );
1153
1154
        _unregister_taxonomy('hierarchical_fields');
1155
1156
        $this->assertEqualSetsWithIndex($expected, $found);
1157
    }
1158
1159
    public function test_get_terms_hierarchical_tax_hide_empty_true_fields_idparent_hierarchical_false() 
1160
    {
1161
        // Set up a clean taxonomy.
1162
        $tax = 'hierarchical_fields';
1163
        register_taxonomy($tax, 'post', array( 'hierarchical' => true ));
1164
1165
        $terms = $this->create_hierarchical_terms_and_posts();
1166
1167
        $found = get_terms(
1168
            $tax, array(
1169
            'hide_empty' => true,
1170
            'fields' => 'id=>parent',
1171
            'hierarchical' => false,
1172
            ) 
1173
        );
1174
1175
        $expected = array(
1176
         $terms['parent2'] => 0,
1177
         $terms['child1'] => $terms['parent1'],
1178
        );
1179
1180
        _unregister_taxonomy('hierarchical_fields');
1181
1182
        $this->assertEqualSetsWithIndex($expected, $found);
1183
    }
1184
1185 View Code Duplication
    public function test_get_terms_hierarchical_tax_hide_empty_false_fields_idslug() 
1186
    {
1187
        // Set up a clean taxonomy.
1188
        $tax = 'hierarchical_fields';
1189
        register_taxonomy($tax, 'post', array( 'hierarchical' => true ));
1190
1191
        $terms = $this->create_hierarchical_terms_and_posts();
1192
1193
        $found = get_terms(
1194
            $tax, array(
1195
            'hide_empty' => false,
1196
            'fields' => 'id=>slug',
1197
            ) 
1198
        );
1199
1200
        $expected = array(
1201
         $terms['parent1'] => 'parent-1',
1202
         $terms['parent2'] => 'parent-2',
1203
         $terms['child1'] => 'child-1',
1204
         $terms['child2'] => 'child-2',
1205
         $terms['grandchild1'] => 'grandchild-1',
1206
        );
1207
1208
        _unregister_taxonomy('hierarchical_fields');
1209
1210
        $this->assertEqualSetsWithIndex($expected, $found);
1211
    }
1212
1213
    /**
1214
     * @ticket 29859
1215
     */
1216
    public function test_get_terms_hierarchical_tax_hide_empty_true_fields_idslug() 
1217
    {
1218
        // Set up a clean taxonomy.
1219
        $tax = 'hierarchical_fields';
1220
        register_taxonomy($tax, 'post', array( 'hierarchical' => true ));
1221
1222
        $terms = $this->create_hierarchical_terms_and_posts();
1223
1224
        $found = get_terms(
1225
            $tax, array(
1226
            'hide_empty' => true,
1227
            'fields' => 'id=>slug',
1228
            ) 
1229
        );
1230
1231
        $expected = array(
1232
         $terms['parent1'] => 'parent-1',
1233
         $terms['parent2'] => 'parent-2',
1234
         $terms['child1'] => 'child-1',
1235
        );
1236
1237
        _unregister_taxonomy('hierarchical_fields');
1238
1239
        $this->assertEqualSetsWithIndex($expected, $found);
1240
    }
1241
1242
    public function test_get_terms_hierarchical_tax_hide_empty_true_fields_idslug_hierarchical_false() 
1243
    {
1244
        // Set up a clean taxonomy.
1245
        $tax = 'hierarchical_fields';
1246
        register_taxonomy($tax, 'post', array( 'hierarchical' => true ));
1247
1248
        $terms = $this->create_hierarchical_terms_and_posts();
1249
1250
        $found = get_terms(
1251
            $tax, array(
1252
            'hide_empty' => true,
1253
            'fields' => 'id=>slug',
1254
            'hierarchical' => false,
1255
            ) 
1256
        );
1257
1258
        $expected = array(
1259
         $terms['parent2'] => 'parent-2',
1260
         $terms['child1'] => 'child-1',
1261
        );
1262
1263
        _unregister_taxonomy('hierarchical_fields');
1264
1265
        $this->assertEqualSetsWithIndex($expected, $found);
1266
    }
1267
1268 View Code Duplication
    public function test_get_terms_hierarchical_tax_hide_empty_false_fields_idname() 
1269
    {
1270
        // Set up a clean taxonomy.
1271
        $tax = 'hierarchical_fields';
1272
        register_taxonomy($tax, 'post', array( 'hierarchical' => true ));
1273
1274
        $terms = $this->create_hierarchical_terms_and_posts();
1275
1276
        $found = get_terms(
1277
            $tax, array(
1278
            'hide_empty' => false,
1279
            'fields' => 'id=>name',
1280
            ) 
1281
        );
1282
1283
        $expected = array(
1284
         $terms['parent1'] => 'Parent 1',
1285
         $terms['parent2'] => 'Parent 2',
1286
         $terms['child1'] => 'Child 1',
1287
         $terms['child2'] => 'Child 2',
1288
         $terms['grandchild1'] => 'Grandchild 1',
1289
        );
1290
1291
        _unregister_taxonomy('hierarchical_fields');
1292
1293
        $this->assertEqualSetsWithIndex($expected, $found);
1294
    }
1295
1296
    /**
1297
     * @ticket 29859
1298
     */
1299
    public function test_get_terms_hierarchical_tax_hide_empty_true_fields_idname() 
1300
    {
1301
        // Set up a clean taxonomy.
1302
        $tax = 'hierarchical_fields';
1303
        register_taxonomy($tax, 'post', array( 'hierarchical' => true ));
1304
1305
        $terms = $this->create_hierarchical_terms_and_posts();
1306
1307
        $found = get_terms(
1308
            $tax, array(
1309
            'hide_empty' => true,
1310
            'fields' => 'id=>name',
1311
            ) 
1312
        );
1313
1314
        $expected = array(
1315
         $terms['parent1'] => 'Parent 1',
1316
         $terms['parent2'] => 'Parent 2',
1317
         $terms['child1'] => 'Child 1',
1318
        );
1319
1320
        _unregister_taxonomy('hierarchical_fields');
1321
1322
        $this->assertEqualSetsWithIndex($expected, $found);
1323
    }
1324
1325
    public function test_get_terms_hierarchical_tax_hide_empty_true_fields_idname_hierarchical_false() 
1326
    {
1327
        // Set up a clean taxonomy.
1328
        $tax = 'hierarchical_fields';
1329
        register_taxonomy($tax, 'post', array( 'hierarchical' => true ));
1330
1331
        $terms = $this->create_hierarchical_terms_and_posts();
1332
1333
        $found = get_terms(
1334
            $tax, array(
1335
            'hide_empty' => true,
1336
            'fields' => 'id=>name',
1337
            'hierarchical' => false,
1338
            ) 
1339
        );
1340
1341
        $expected = array(
1342
         $terms['parent2'] => 'Parent 2',
1343
         $terms['child1'] => 'Child 1',
1344
        );
1345
1346
        _unregister_taxonomy('hierarchical_fields');
1347
1348
        $this->assertEqualSetsWithIndex($expected, $found);
1349
    }
1350
1351
    /**
1352
     * @ticket 31118
1353
     */
1354
    public function test_hierarchical_should_recurse_properly_for_all_taxonomies() 
1355
    {
1356
        register_taxonomy('wptests_tax1', 'post', array( 'hierarchical' => true ));
1357
        register_taxonomy('wptests_tax2', 'post', array( 'hierarchical' => true ));
1358
1359
        $t1 = self::factory()->term->create(array( 'taxonomy' => 'wptests_tax1' ));
1360
        $t2 = self::factory()->term->create(array( 'taxonomy' => 'wptests_tax1', 'parent' => $t1 ));
1361
        $t3 = self::factory()->term->create(array( 'taxonomy' => 'wptests_tax1', 'parent' => $t2 ));
1362
1363
        $t4 = self::factory()->term->create(array( 'taxonomy' => 'wptests_tax2' ));
1364
        $t5 = self::factory()->term->create(array( 'taxonomy' => 'wptests_tax2', 'parent' => $t4 ));
1365
        $t6 = self::factory()->term->create(array( 'taxonomy' => 'wptests_tax2', 'parent' => $t5 ));
1366
1367
        $p = self::factory()->post->create();
1368
1369
        wp_set_object_terms($p, $t3, 'wptests_tax1');
1370
        wp_set_object_terms($p, $t6, 'wptests_tax2');
1371
1372
        $found = get_terms(
1373
            array( 'wptests_tax1', 'wptests_tax2' ), array(
1374
            'hierarchical' => true,
1375
            'hide_empty' => true,
1376
            'fields' => 'ids',
1377
            ) 
1378
        );
1379
1380
        /*
1381
         * Should contain all terms, since they all have non-empty descendants.
1382
         * To make the case clearer, test taxonomies separately.
1383
         */
1384
1385
        // First taxonomy.
1386
        $this->assertContains($t1, $found);
1387
        $this->assertContains($t2, $found);
1388
        $this->assertContains($t3, $found);
1389
1390
        // Second taxonomy.
1391
        $this->assertContains($t4, $found);
1392
        $this->assertContains($t5, $found);
1393
        $this->assertContains($t6, $found);
1394
    }
1395
1396
    /**
1397
     * @ticket 23261
1398
     */
1399 View Code Duplication
    public function test_orderby_include() 
1400
    {
1401
        $tax = 'wptests_tax';
1402
        register_taxonomy($tax, 'post');
1403
1404
        $t1 = self::factory()->term->create(array( 'taxonomy' => $tax ));
1405
        $t2 = self::factory()->term->create(array( 'taxonomy' => $tax ));
1406
        $t3 = self::factory()->term->create(array( 'taxonomy' => $tax ));
1407
        $t4 = self::factory()->term->create(array( 'taxonomy' => $tax ));
1408
1409
        $found = get_terms(
1410
            $tax, array(
1411
            'fields' => 'ids',
1412
            'include' => array( $t4, $t1, $t2 ),
1413
            'orderby' => 'include',
1414
            'hide_empty' => false,
1415
            ) 
1416
        );
1417
1418
        _unregister_taxonomy('wptests_tax');
1419
1420
        $this->assertEquals(array( $t4, $t1, $t2 ), $found);
1421
    }
1422
1423
    /**
1424
     * @ticket 31364
1425
     */
1426
    public function test_orderby_description() 
1427
    {
1428
        $tax = 'wptests_tax';
1429
        register_taxonomy($tax, 'post');
1430
1431
        $t1 = self::factory()->term->create(array( 'taxonomy' => $tax, 'description' => 'fff' ));
1432
        $t2 = self::factory()->term->create(array( 'taxonomy' => $tax, 'description' => 'aaa' ));
1433
        $t3 = self::factory()->term->create(array( 'taxonomy' => $tax, 'description' => 'zzz' ));
1434
        $t4 = self::factory()->term->create(array( 'taxonomy' => $tax, 'description' => 'jjj' ));
1435
1436
        $found = get_terms(
1437
            $tax, array(
1438
            'fields' => 'ids',
1439
            'orderby' => 'description',
1440
            'hide_empty' => false,
1441
            ) 
1442
        );
1443
1444
        _unregister_taxonomy('wptests_tax');
1445
1446
        $this->assertEquals(array( $t2, $t1, $t4, $t3 ), $found);
1447
    }
1448
1449
    /**
1450
     * @ticket 33726
1451
     */
1452 View Code Duplication
    public function test_orderby_term_id() 
1453
    {
1454
        register_taxonomy('wptests_tax', 'post');
1455
        $t1 = self::factory()->term->create(
1456
            array(
1457
            'taxonomy' => 'wptests_tax',
1458
            'name' => 'AAA',
1459
            ) 
1460
        );
1461
        $t2 = self::factory()->term->create(
1462
            array(
1463
            'taxonomy' => 'wptests_tax',
1464
            'name' => 'ZZZ',
1465
            ) 
1466
        );
1467
        $t3 = self::factory()->term->create(
1468
            array(
1469
            'taxonomy' => 'wptests_tax',
1470
            'name' => 'JJJ',
1471
            ) 
1472
        );
1473
1474
        $found = get_terms(
1475
            'wptests_tax', array(
1476
            'orderby' => 'term_id',
1477
            'hide_empty' => false,
1478
            'fields' => 'ids',
1479
            ) 
1480
        );
1481
1482
        $this->assertEquals(array( $t1, $t2, $t3 ), $found);
1483
    }
1484
1485
    /**
1486
     * @ticket 34996
1487
     */
1488 View Code Duplication
    public function test_orderby_meta_value() 
1489
    {
1490
        register_taxonomy('wptests_tax', 'post');
1491
        $terms = self::factory()->term->create_many(3, array( 'taxonomy' => 'wptests_tax' ));
1492
        add_term_meta($terms[0], 'foo', 'zzz');
1493
        add_term_meta($terms[0], 'fee', 'ber');
1494
        add_term_meta($terms[1], 'foo', 'aaa');
1495
        add_term_meta($terms[1], 'fee', 'ber');
1496
        add_term_meta($terms[2], 'foo', 'jjj');
1497
        add_term_meta($terms[2], 'fee', 'ber');
1498
1499
        // Matches the first meta query clause.
1500
        $found = get_terms(
1501
            'wptests_tax', array(
1502
            'hide_empty' => false,
1503
            'meta_query' => array(
0 ignored issues
show
introduced by
Detected usage of meta_query, possible slow query.
Loading history...
1504
            'relation' => 'AND',
1505
            array(
1506
            'key' => 'foo',
1507
            'compare' => 'EXISTS',
1508
            ),
1509
            array(
1510
            'key' => 'fee',
1511
            'compare' => 'EXISTS',
1512
            ),
1513
            ),
1514
            'orderby' => 'meta_value',
1515
            'order' => 'ASC',
1516
            'fields' => 'ids',
1517
            ) 
1518
        );
1519
1520
        $this->assertEqualSets(array( $terms[1], $terms[2], $terms[0] ), $found);
1521
    }
1522
1523
    /**
1524
     * @ticket 34996
1525
     */
1526 View Code Duplication
    public function test_orderby_meta_value_num() 
1527
    {
1528
        register_taxonomy('wptests_tax', 'post');
1529
        $terms = self::factory()->term->create_many(3, array( 'taxonomy' => 'wptests_tax' ));
1530
        add_term_meta($terms[0], 'foo', '999');
1531
        add_term_meta($terms[0], 'fee', 'ber');
1532
        add_term_meta($terms[1], 'foo', '111');
1533
        add_term_meta($terms[1], 'fee', 'ber');
1534
        add_term_meta($terms[2], 'foo', '555');
1535
        add_term_meta($terms[2], 'fee', 'ber');
1536
1537
        // Matches the first meta query clause.
1538
        $found = get_terms(
1539
            'wptests_tax', array(
1540
            'hide_empty' => false,
1541
            'meta_query' => array(
0 ignored issues
show
introduced by
Detected usage of meta_query, possible slow query.
Loading history...
1542
            'relation' => 'AND',
1543
            array(
1544
            'key' => 'foo',
1545
            'compare' => 'EXISTS',
1546
            ),
1547
            array(
1548
            'key' => 'fee',
1549
            'compare' => 'EXISTS',
1550
            ),
1551
            ),
1552
            'orderby' => 'meta_value_num',
1553
            'order' => 'ASC',
1554
            'fields' => 'ids',
1555
            ) 
1556
        );
1557
1558
        $this->assertEqualSets(array( $terms[1], $terms[2], $terms[0] ), $found);
1559
    }
1560
1561
    /**
1562
     * @ticket 34996
1563
     */
1564 View Code Duplication
    public function test_orderby_meta_value_with_meta_key() 
1565
    {
1566
        register_taxonomy('wptests_tax', 'post');
1567
        $terms = self::factory()->term->create_many(3, array( 'taxonomy' => 'wptests_tax' ));
1568
        add_term_meta($terms[0], 'foo', 'bar');
1569
        add_term_meta($terms[0], 'fee', 'zzz');
1570
        add_term_meta($terms[0], 'faa', 'jjj');
1571
        add_term_meta($terms[1], 'foo', 'bar');
1572
        add_term_meta($terms[1], 'fee', 'aaa');
1573
        add_term_meta($terms[1], 'faa', 'aaa');
1574
        add_term_meta($terms[2], 'foo', 'bar');
1575
        add_term_meta($terms[2], 'fee', 'jjj');
1576
        add_term_meta($terms[2], 'faa', 'zzz');
1577
1578
        $found = get_terms(
1579
            'wptests_tax', array(
1580
            'hide_empty' => false,
1581
            'meta_key' => 'fee',
0 ignored issues
show
introduced by
Detected usage of meta_key, possible slow query.
Loading history...
1582
            'orderby' => 'meta_value',
1583
            'order' => 'ASC',
1584
            'fields' => 'ids',
1585
            ) 
1586
        );
1587
1588
        $this->assertEqualSets(array( $terms[1], $terms[2], $terms[0] ), $found);
1589
1590
        $found = get_terms(
1591
            'wptests_tax', array(
1592
            'hide_empty' => false,
1593
            'meta_query' => array(
0 ignored issues
show
introduced by
Detected usage of meta_query, possible slow query.
Loading history...
1594
            array(
1595
            'key' => 'foo',
1596
            'compare' => 'EXISTS',
1597
            ),
1598
            ),
1599
            'meta_key' => 'fee',
0 ignored issues
show
introduced by
Detected usage of meta_key, possible slow query.
Loading history...
1600
            'orderby' => 'meta_value',
1601
            'order' => 'ASC',
1602
            'fields' => 'ids',
1603
            ) 
1604
        );
1605
1606
        $this->assertEqualSets(array( $terms[1], $terms[2], $terms[0] ), $found);
1607
1608
        // Matches the first meta query clause.
1609
        $found = get_terms(
1610
            'wptests_tax', array(
1611
            'hide_empty' => false,
1612
            'meta_query' => array(
0 ignored issues
show
introduced by
Detected usage of meta_query, possible slow query.
Loading history...
1613
            'relation' => 'AND',
1614
            array(
1615
            'key' => 'foo',
1616
            'compare' => 'EXISTS',
1617
            ),
1618
            array(
1619
            'key' => 'fee',
1620
            'compare' => 'EXISTS',
1621
            ),
1622
            ),
1623
            'meta_key' => 'fee',
0 ignored issues
show
introduced by
Detected usage of meta_key, possible slow query.
Loading history...
1624
            'orderby' => 'meta_value',
1625
            'order' => 'ASC',
1626
            'fields' => 'ids',
1627
            ) 
1628
        );
1629
1630
        $this->assertEqualSets(array( $terms[1], $terms[2], $terms[0] ), $found);
1631
1632
        // Matches the meta query clause corresponding to the 'meta_key' param.
1633
        $found = get_terms(
1634
            'wptests_tax', array(
1635
            'hide_empty' => false,
1636
            'meta_query' => array(
0 ignored issues
show
introduced by
Detected usage of meta_query, possible slow query.
Loading history...
1637
            'relation' => 'AND',
1638
            array(
1639
            'key' => 'foo',
1640
            'compare' => 'EXISTS',
1641
            ),
1642
            array(
1643
            'key' => 'fee',
1644
            'compare' => 'EXISTS',
1645
            ),
1646
            ),
1647
            'meta_key' => 'faa',
0 ignored issues
show
introduced by
Detected usage of meta_key, possible slow query.
Loading history...
1648
            'orderby' => 'meta_value',
1649
            'order' => 'ASC',
1650
            'fields' => 'ids',
1651
            ) 
1652
        );
1653
1654
        $this->assertEqualSets(array( $terms[1], $terms[0], $terms[2] ), $found);
1655
    }
1656
1657
    /**
1658
     * @ticket 34996
1659
     */
1660 View Code Duplication
    public function test_orderby_meta_value_num_with_meta_key() 
1661
    {
1662
        register_taxonomy('wptests_tax', 'post');
1663
        $terms = self::factory()->term->create_many(3, array( 'taxonomy' => 'wptests_tax' ));
1664
        add_term_meta($terms[0], 'foo', 'bar');
1665
        add_term_meta($terms[0], 'fee', '999');
1666
        add_term_meta($terms[0], 'faa', '555');
1667
        add_term_meta($terms[1], 'foo', 'bar');
1668
        add_term_meta($terms[1], 'fee', '111');
1669
        add_term_meta($terms[1], 'faa', '111');
1670
        add_term_meta($terms[2], 'foo', 'bar');
1671
        add_term_meta($terms[2], 'fee', '555');
1672
        add_term_meta($terms[2], 'faa', '999');
1673
1674
        $found = get_terms(
1675
            'wptests_tax', array(
1676
            'hide_empty' => false,
1677
            'meta_key' => 'fee',
0 ignored issues
show
introduced by
Detected usage of meta_key, possible slow query.
Loading history...
1678
            'orderby' => 'meta_value',
1679
            'order' => 'ASC',
1680
            'fields' => 'ids',
1681
            ) 
1682
        );
1683
1684
        $this->assertEqualSets(array( $terms[1], $terms[2], $terms[0] ), $found);
1685
1686
        $found = get_terms(
1687
            'wptests_tax', array(
1688
            'hide_empty' => false,
1689
            'meta_query' => array(
0 ignored issues
show
introduced by
Detected usage of meta_query, possible slow query.
Loading history...
1690
            array(
1691
            'key' => 'foo',
1692
            'compare' => 'EXISTS',
1693
            ),
1694
            ),
1695
            'meta_key' => 'fee',
0 ignored issues
show
introduced by
Detected usage of meta_key, possible slow query.
Loading history...
1696
            'orderby' => 'meta_value',
1697
            'order' => 'ASC',
1698
            'fields' => 'ids',
1699
            ) 
1700
        );
1701
1702
        $this->assertEqualSets(array( $terms[1], $terms[2], $terms[0] ), $found);
1703
1704
        $found = get_terms(
1705
            'wptests_tax', array(
1706
            'hide_empty' => false,
1707
            'meta_query' => array(
0 ignored issues
show
introduced by
Detected usage of meta_query, possible slow query.
Loading history...
1708
            'relation' => 'AND',
1709
            array(
1710
            'key' => 'foo',
1711
            'compare' => 'EXISTS',
1712
            ),
1713
            array(
1714
            'key' => 'fee',
1715
            'compare' => 'EXISTS',
1716
            ),
1717
            ),
1718
            'meta_key' => 'fee',
0 ignored issues
show
introduced by
Detected usage of meta_key, possible slow query.
Loading history...
1719
            'orderby' => 'meta_value',
1720
            'order' => 'ASC',
1721
            'fields' => 'ids',
1722
            ) 
1723
        );
1724
1725
        $this->assertEqualSets(array( $terms[1], $terms[2], $terms[0] ), $found);
1726
1727
        $found = get_terms(
1728
            'wptests_tax', array(
1729
            'hide_empty' => false,
1730
            'meta_query' => array(
0 ignored issues
show
introduced by
Detected usage of meta_query, possible slow query.
Loading history...
1731
            'relation' => 'AND',
1732
            array(
1733
            'key' => 'foo',
1734
            'compare' => 'EXISTS',
1735
            ),
1736
            array(
1737
            'key' => 'fee',
1738
            'compare' => 'EXISTS',
1739
            ),
1740
            ),
1741
            'meta_key' => 'faa',
0 ignored issues
show
introduced by
Detected usage of meta_key, possible slow query.
Loading history...
1742
            'orderby' => 'meta_value',
1743
            'order' => 'ASC',
1744
            'fields' => 'ids',
1745
            ) 
1746
        );
1747
1748
        $this->assertEqualSets(array( $terms[1], $terms[0], $terms[2] ), $found);
1749
    }
1750
1751
    /**
1752
     * @ticket 34996
1753
     */
1754
    public function test_orderby_clause_key() 
1755
    {
1756
        register_taxonomy('wptests_tax', 'post');
1757
        $terms = self::factory()->term->create_many(3, array( 'taxonomy' => 'wptests_tax' ));
1758
        add_term_meta($terms[0], 'foo', 'zzz');
1759
        add_term_meta($terms[0], 'fee', 'jjj');
1760
        add_term_meta($terms[1], 'foo', 'aaa');
1761
        add_term_meta($terms[1], 'fee', 'aaa');
1762
        add_term_meta($terms[2], 'foo', 'jjj');
1763
        add_term_meta($terms[2], 'fee', 'zzz');
1764
1765
        $found = get_terms(
1766
            'wptests_tax', array(
1767
            'hide_empty' => false,
1768
            'meta_query' => array(
0 ignored issues
show
introduced by
Detected usage of meta_query, possible slow query.
Loading history...
1769
            'relation' => 'AND',
1770
            'foo_key' => array(
1771
            'key' => 'foo',
1772
            'compare' => 'EXISTS',
1773
            ),
1774
            'fee_key' => array(
1775
            'key' => 'fee',
1776
            'compare' => 'EXISTS',
1777
            ),
1778
            ),
1779
            'orderby' => 'foo_key',
1780
            'order' => 'ASC',
1781
            'fields' => 'ids',
1782
            ) 
1783
        );
1784
1785
        $this->assertEqualSets(array( $terms[1], $terms[2], $terms[0] ), $found);
1786
1787
        $found = get_terms(
1788
            'wptests_tax', array(
1789
            'hide_empty' => false,
1790
            'meta_query' => array(
0 ignored issues
show
introduced by
Detected usage of meta_query, possible slow query.
Loading history...
1791
            'relation' => 'AND',
1792
            'foo_key' => array(
1793
            'key' => 'foo',
1794
            'compare' => 'EXISTS',
1795
            ),
1796
            'fee_key' => array(
1797
            'key' => 'fee',
1798
            'compare' => 'EXISTS',
1799
            ),
1800
            ),
1801
            'orderby' => 'fee_key',
1802
            'order' => 'ASC',
1803
            'fields' => 'ids',
1804
            ) 
1805
        );
1806
1807
        $this->assertEqualSets(array( $terms[1], $terms[0], $terms[2] ), $found);
1808
1809
        $expected = get_terms(
1810
            'wptests_tax', array(
1811
            'hide_empty' => false,
1812
            'meta_query' => array(
0 ignored issues
show
introduced by
Detected usage of meta_query, possible slow query.
Loading history...
1813
            'relation' => 'AND',
1814
            'foo_key' => array(
1815
            'key' => 'foo',
1816
            'compare' => 'EXISTS',
1817
            ),
1818
            'fee_key' => array(
1819
            'key' => 'fee',
1820
            'compare' => 'EXISTS',
1821
            ),
1822
            ),
1823
            'fields' => 'ids',
1824
            ) 
1825
        );
1826
1827
        $found = get_terms(
1828
            'wptests_tax', array(
1829
            'hide_empty' => false,
1830
            'meta_query' => array(
0 ignored issues
show
introduced by
Detected usage of meta_query, possible slow query.
Loading history...
1831
            'relation' => 'AND',
1832
            'foo_key' => array(
1833
            'key' => 'foo',
1834
            'compare' => 'EXISTS',
1835
            ),
1836
            'fee_key' => array(
1837
            'key' => 'fee',
1838
            'compare' => 'EXISTS',
1839
            ),
1840
            ),
1841
            'orderby' => 'faa_key',
1842
            'fields' => 'ids',
1843
            ) 
1844
        );
1845
1846
        $this->assertEqualSets($expected, $found);
1847
    }
1848
1849 View Code Duplication
    public function test_hierarchical_false_with_parent() 
1850
    {
1851
        $initial_terms = $this->create_hierarchical_terms();
1852
1853
        // Case where hierarchical is false
1854
        $terms = get_terms(
1855
            'category', array(
1856
            'hierarchical' => false,
1857
            'parent' => $initial_terms['one_term']['term_id']
1858
            ) 
1859
        );
1860
1861
        // Verify that there are no children
1862
        $this->assertEquals(0, count($terms));
1863
    }
1864
1865
    /**
1866
     * @ticket 29185
1867
     */
1868
    public function test_hierarchical_true_with_parent() 
1869
    {
1870
        $initial_terms = $this->create_hierarchical_terms();
1871
1872
        // Case where hierarchical is true
1873
        $terms = get_terms(
1874
            'category', array(
1875
            'hierarchical' => true,
1876
            'parent' => $initial_terms['one_term']['term_id']
1877
            ) 
1878
        );
1879
1880
        // Verify that the children with non-empty descendants are returned
1881
        $expected = array(
1882
         $initial_terms['two_term']['term_id'],
1883
         $initial_terms['five_term']['term_id'],
1884
        );
1885
        $actual = wp_list_pluck($terms, 'term_id');
1886
        $this->assertEqualSets($expected, $actual);
1887
    }
1888
1889
    public function test_hierarchical_false_with_child_of_and_direct_child() 
1890
    {
1891
        $initial_terms = $this->create_hierarchical_terms();
1892
        $post_id = self::factory()->post->create();
1893
        wp_set_post_terms(
1894
            $post_id,
1895
            array( $initial_terms['seven_term']['term_id'] ),
1896
            'category'
1897
        );
1898
1899
        // Case where hierarchical is false
1900
        $terms = get_terms(
1901
            'category', array(
1902
            'hierarchical' => false,
1903
            'child_of' => $initial_terms['one_term']['term_id']
1904
            ) 
1905
        );
1906
1907
        $expected = array(
1908
         $initial_terms['seven_term']['term_id'],
1909
        );
1910
1911
        $actual = wp_list_pluck($terms, 'term_id');
1912
        $this->assertEqualSets($expected, $actual);
1913
    }
1914
1915 View Code Duplication
    public function test_hierarchical_false_with_child_of_should_not_return_grandchildren() 
1916
    {
1917
        $initial_terms = $this->create_hierarchical_terms();
1918
1919
        // Case where hierarchical is false
1920
        $terms = get_terms(
1921
            'category', array(
1922
            'hierarchical' => false,
1923
            'child_of' => $initial_terms['one_term']['term_id']
1924
            ) 
1925
        );
1926
1927
        // Verify that there are no children
1928
        $this->assertEquals(0, count($terms));
1929
    }
1930
1931 View Code Duplication
    public function test_hierarchical_true_with_child_of_should_return_grandchildren() 
1932
    {
1933
        $initial_terms = $this->create_hierarchical_terms();
1934
1935
        // Case where hierarchical is true
1936
        $terms = get_terms(
1937
            'category', array(
1938
            'hierarchical' => true,
1939
            'child_of' => $initial_terms['one_term']['term_id']
1940
            ) 
1941
        );
1942
1943
        $expected = array(
1944
         $initial_terms['two_term']['term_id'],
1945
         $initial_terms['three_term']['term_id'],
1946
         $initial_terms['five_term']['term_id'],
1947
         $initial_terms['six_term']['term_id'],
1948
        );
1949
        $actual = wp_list_pluck($terms, 'term_id');
1950
        $this->assertEqualSets($expected, $actual);
1951
    }
1952
1953 View Code Duplication
    public function test_parent_should_override_child_of() 
1954
    {
1955
        $initial_terms = $this->create_hierarchical_terms();
1956
1957
        $terms = get_terms(
1958
            'category', array(
1959
            'hide_empty' => false,
1960
            'child_of' => $initial_terms['one_term']['term_id'],
1961
            'parent' => $initial_terms['one_term']['term_id']
1962
            ) 
1963
        );
1964
1965
        // Verify that parent takes precedence over child_of and returns only direct children.
1966
        $expected = array(
1967
         $initial_terms['two_term']['term_id'],
1968
         $initial_terms['five_term']['term_id'],
1969
         $initial_terms['seven_term']['term_id']
1970
        );
1971
        $actual = wp_list_pluck($terms, 'term_id');
1972
        $this->assertEqualSets($expected, $actual);
1973
    }
1974
1975
    /**
1976
     * @ticket 31118
1977
     */
1978 View Code Duplication
    public function test_parent_should_skip_query_when_specified_parent_is_not_found_in_hierarchy_cache() 
1979
    {
1980
        global $wpdb;
1981
1982
        register_taxonomy('wptests_tax', 'post', array( 'hierarchical' => true, ));
1983
1984
        $terms = self::factory()->term->create_many(3, array( 'taxonomy' => 'wptests_tax' ));
1985
1986
        $num_queries = $wpdb->num_queries;
1987
1988
        $found = get_terms(
1989
            'wptests_tax', array(
1990
            'hide_empty' => false,
1991
            'parent' => $terms[0],
1992
            ) 
1993
        );
1994
1995
        $this->assertEmpty($found);
1996
        $this->assertSame($num_queries, $wpdb->num_queries);
1997
    }
1998
1999
    /**
2000
     * @ticket 31118
2001
     */
2002 View Code Duplication
    public function test_parent_should_respect_multiple_taxonomies() 
2003
    {
2004
        register_taxonomy('wptests_tax1', 'post', array( 'hierarchical' => true ));
2005
        register_taxonomy('wptests_tax2', 'post', array( 'hierarchical' => true ));
2006
2007
        $t1 = self::factory()->term->create(array( 'taxonomy' => 'wptests_tax1' ));
2008
        $t2 = self::factory()->term->create(array( 'taxonomy' => 'wptests_tax2' ));
2009
        $t3 = self::factory()->term->create(array( 'taxonomy' => 'wptests_tax2', 'parent' => $t2 ));
2010
2011
        $found = get_terms(
2012
            array( 'wptests_tax1', 'wptests_tax2' ), array(
2013
            'fields' => 'ids',
2014
            'hide_empty' => false,
2015
            'parent' => $t2,
2016
            ) 
2017
        );
2018
2019
        $this->assertEqualSets(array( $t3 ), $found);
2020
    }
2021
2022
    public function test_hierarchical_false_parent_should_override_child_of() 
2023
    {
2024
        $initial_terms = $this->create_hierarchical_terms();
2025
2026
        // Case where hierarchical is false
2027
        $terms = get_terms(
2028
            'category', array(
2029
            'hierarchical' => false,
2030
            'child_of' => $initial_terms['one_term']['term_id'],
2031
            'parent' => $initial_terms['one_term']['term_id']
2032
            ) 
2033
        );
2034
2035
        // hierarchical=false means that descendants are not fetched.
2036
        $this->assertEquals(0, count($terms));
2037
    }
2038
2039
    /**
2040
     * @ticket 29185
2041
     */
2042 View Code Duplication
    public function test_hierarchical_true_parent_overrides_child_of() 
2043
    {
2044
        $initial_terms = $this->create_hierarchical_terms();
2045
2046
        // Case where hierarchical is true
2047
        $terms = get_terms(
2048
            'category', array(
2049
            'hierarchical' => true,
2050
            'child_of' => $initial_terms['one_term']['term_id'],
2051
            'parent' => $initial_terms['one_term']['term_id'],
2052
            ) 
2053
        );
2054
2055
        // Verify that parent takes precedence over child_of
2056
        $expected = array(
2057
         $initial_terms['two_term']['term_id'],
2058
         $initial_terms['five_term']['term_id'],
2059
        );
2060
        $actual = wp_list_pluck($terms, 'term_id');
2061
        $this->assertEqualSets($expected, $actual);
2062
    }
2063
2064
    public function test_pad_counts() 
2065
    {
2066
        register_taxonomy('wptests_tax_1', 'post', array( 'hierarchical' => true ));
2067
2068
        $posts = self::factory()->post->create_many(3);
2069
2070
        $t1 = self::factory()->term->create(
2071
            array(
2072
            'taxonomy' => 'wptests_tax_1',
2073
            ) 
2074
        );
2075
        $t2 = self::factory()->term->create(
2076
            array(
2077
            'taxonomy' => 'wptests_tax_1',
2078
            'parent' => $t1,
2079
            ) 
2080
        );
2081
        $t3 = self::factory()->term->create(
2082
            array(
2083
            'taxonomy' => 'wptests_tax_1',
2084
            'parent' => $t2,
2085
            ) 
2086
        );
2087
2088
        wp_set_object_terms($posts[0], array( $t1 ), 'wptests_tax_1');
2089
        wp_set_object_terms($posts[1], array( $t2 ), 'wptests_tax_1');
2090
        wp_set_object_terms($posts[2], array( $t3 ), 'wptests_tax_1');
2091
2092
        $found = get_terms(
2093
            'wptests_tax_1', array(
2094
            'pad_counts' => true,
2095
            ) 
2096
        );
2097
2098
        $this->assertEqualSets(array( $t1, $t2, $t3 ), wp_list_pluck($found, 'term_id'));
2099
2100 View Code Duplication
        foreach ( $found as $f ) {
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...
2101
            if ($t1 == $f->term_id ) {
2102
                $this->assertSame(3, $f->count);
2103
            } elseif ($t2 == $f->term_id ) {
2104
                $this->assertSame(2, $f->count);
2105
            } else {
2106
                $this->assertSame(1, $f->count);
2107
            }
2108
        }
2109
    }
2110
2111
    /**
2112
     * @ticket 20635
2113
     */
2114
    public function test_pad_counts_should_not_recurse_infinitely_when_term_hierarchy_has_a_loop() 
2115
    {
2116
        remove_filter('wp_update_term_parent', 'wp_check_term_hierarchy_for_loops', 10);
2117
2118
        $c1 = self::factory()->category->create();
2119
        $c2 = self::factory()->category->create(array( 'parent' => $c1 ));
2120
        $c3 = self::factory()->category->create(array( 'parent' => $c2 ));
2121
        wp_update_term($c1, 'category', array( 'parent' => $c3 ));
2122
2123
        add_filter('wp_update_term_parent', 'wp_check_term_hierarchy_for_loops', 10, 3);
2124
2125
        $posts = self::factory()->post->create_many(3);
2126
        wp_set_post_terms($posts[0], $c1, 'category');
2127
        wp_set_post_terms($posts[1], $c2, 'category');
2128
        wp_set_post_terms($posts[2], $c3, 'category');
2129
2130
        $terms = get_terms(
2131
            'category', array(
2132
            'pad_counts' => true,
2133
            ) 
2134
        );
2135
2136
        $this->assertEqualSets(array( $c1, $c2, $c3 ), wp_list_pluck($terms, 'term_id'));
2137
2138
        foreach ( $terms as $term ) {
2139
            $this->assertSame(3, $term->count);
2140
        }
2141
    }
2142
2143
    /**
2144
     * @ticket 31118
2145
     */
2146
    public function test_pad_counts_should_work_when_first_taxonomy_is_nonhierarchical_and_second_taxonomy_is_hierarchical() 
2147
    {
2148
        register_taxonomy('wptests_tax1', 'post', array( 'hierarchical' => false ));
2149
        register_taxonomy('wptests_tax2', 'post', array( 'hierarchical' => true ));
2150
2151
        $t1 = self::factory()->term->create(array( 'taxonomy' => 'wptests_tax1' ));
2152
        $t2 = self::factory()->term->create(array( 'taxonomy' => 'wptests_tax2' ));
2153
        $t3 = self::factory()->term->create(array( 'taxonomy' => 'wptests_tax2', 'parent' => $t2 ));
2154
2155
        $posts = self::factory()->post->create_many(3);
2156
        wp_set_object_terms($posts[0], $t1, 'wptests_tax1');
2157
        wp_set_object_terms($posts[1], $t2, 'wptests_tax2');
2158
        wp_set_object_terms($posts[2], $t3, 'wptests_tax2');
2159
2160
        $found = get_terms(
2161
            array( 'wptests_tax1', 'wptests_tax2' ), array(
2162
            'pad_counts' => true,
2163
            ) 
2164
        );
2165
2166
        $this->assertEqualSets(array( $t1, $t2, $t3 ), wp_list_pluck($found, 'term_id'));
2167
2168 View Code Duplication
        foreach ( $found as $f ) {
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...
2169
            if ($t1 == $f->term_id ) {
2170
                $this->assertEquals(1, $f->count);
2171
            } elseif ($t2 == $f->term_id ) {
2172
                $this->assertEquals(2, $f->count);
2173
            } else {
2174
                $this->assertEquals(1, $f->count);
2175
            }
2176
        }
2177
    }
2178
2179
    /**
2180
     * @ticket 10142
2181
     */
2182 View Code Duplication
    public function test_termmeta_cache_should_be_primed_by_default() 
2183
    {
2184
        global $wpdb;
2185
2186
        register_taxonomy('wptests_tax', 'post');
2187
        $terms = self::factory()->term->create_many(3, array( 'taxonomy' => 'wptests_tax' ));
2188
        add_term_meta($terms[0], 'foo', 'bar');
2189
        add_term_meta($terms[1], 'foo', 'bar');
2190
        add_term_meta($terms[2], 'foo', 'bar');
2191
2192
        $found = get_terms(
2193
            'wptests_tax', array(
2194
            'hide_empty' => false,
2195
            'include' => $terms,
2196
            ) 
2197
        );
2198
2199
        $num_queries = $wpdb->num_queries;
2200
2201
        foreach ( $terms as $t ) {
2202
            $this->assertSame('bar', get_term_meta($t, 'foo', true));
2203
        }
2204
2205
        $this->assertSame($num_queries, $wpdb->num_queries);
2206
    }
2207
2208
    /**
2209
     * @ticket 10142
2210
     */
2211 View Code Duplication
    public function test_termmeta_cache_should_not_be_primed_when_update_term_meta_cache_is_false() 
2212
    {
2213
        global $wpdb;
2214
2215
        register_taxonomy('wptests_tax', 'post');
2216
        $terms = self::factory()->term->create_many(3, array( 'taxonomy' => 'wptests_tax' ));
2217
        add_term_meta($terms[0], 'foo', 'bar');
2218
        add_term_meta($terms[1], 'foo', 'bar');
2219
        add_term_meta($terms[2], 'foo', 'bar');
2220
2221
        $found = get_terms(
2222
            'wptests_tax', array(
2223
            'hide_empty' => false,
2224
            'include' => $terms,
2225
            'update_term_meta_cache' => false,
2226
            ) 
2227
        );
2228
2229
        $num_queries = $wpdb->num_queries;
2230
2231
        foreach ( $terms as $t ) {
2232
            $this->assertSame('bar', get_term_meta($t, 'foo', true));
2233
        }
2234
2235
        $this->assertSame($num_queries + 3, $wpdb->num_queries);
2236
    }
2237
2238
    /**
2239
     * @ticket 10142
2240
     */
2241
    public function test_meta_query() 
2242
    {
2243
        register_taxonomy('wptests_tax', 'post');
2244
        $terms = self::factory()->term->create_many(5, array( 'taxonomy' => 'wptests_tax' ));
2245
        add_term_meta($terms[0], 'foo', 'bar');
2246
        add_term_meta($terms[1], 'foo', 'bar');
2247
        add_term_meta($terms[2], 'foo', 'baz');
2248
        add_term_meta($terms[3], 'foob', 'ar');
2249
2250
        $found = get_terms(
2251
            'wptests_tax', array(
2252
            'hide_empty' => false,
2253
            'meta_query' => array(
0 ignored issues
show
introduced by
Detected usage of meta_query, possible slow query.
Loading history...
2254
            array(
2255
            'key' => 'foo',
2256
            'value' => 'bar',
2257
            ),
2258
            ),
2259
            'fields' => 'ids',
2260
            ) 
2261
        );
2262
2263
        $this->assertEqualSets(array( $terms[0], $terms[1] ), $found);
2264
    }
2265
2266
    /**
2267
     * @ticket 35137
2268
     */
2269
    public function test_meta_query_should_not_return_duplicates() 
2270
    {
2271
        register_taxonomy('wptests_tax', 'post');
2272
        $terms = self::factory()->term->create_many(1, array( 'taxonomy' => 'wptests_tax' ));
2273
        add_term_meta($terms[0], 'foo', 'bar');
2274
        add_term_meta($terms[0], 'foo', 'ber');
2275
2276
        $found = get_terms(
2277
            'wptests_tax', array(
2278
            'hide_empty' => false,
2279
            'meta_query' => array(
0 ignored issues
show
introduced by
Detected usage of meta_query, possible slow query.
Loading history...
2280
            array(
2281
            'key' => 'foo',
2282
            'value' => 'bur',
2283
            'compare' => '!=',
2284
            ),
2285
            ),
2286
            'fields' => 'ids',
2287
            ) 
2288
        );
2289
2290
        $this->assertEqualSets(array( $terms[0] ), $found);
2291
    }
2292
2293
    /**
2294
     * @ticket 14162
2295
     */
2296
    public function test_should_return_wp_term_objects() 
2297
    {
2298
        register_taxonomy('wptests_tax', 'post');
2299
        $terms = self::factory()->term->create_many(2, array( 'taxonomy' => 'wptests_tax' ));
2300
2301
        $found = get_terms(
2302
            'wptests_tax', array(
2303
            'hide_empty' => false,
2304
            'fields' => 'all',
2305
            ) 
2306
        );
2307
2308
        $this->assertNotEmpty($found);
2309
2310
        foreach ( $found as $term ) {
2311
            $this->assertInstanceOf('WP_Term', $term);
2312
        }
2313
    }
2314
2315
    /**
2316
     * @ticket 14162
2317
     * @ticket 34282
2318
     */
2319
    public function test_should_return_wp_term_objects_when_pulling_from_the_cache() 
2320
    {
2321
        global $wpdb;
2322
2323
        register_taxonomy('wptests_tax', 'post');
2324
        $terms = self::factory()->term->create_many(2, array( 'taxonomy' => 'wptests_tax' ));
2325
2326
        // Prime the cache.
2327
        get_terms(
2328
            'wptests_tax', array(
2329
            'hide_empty' => false,
2330
            'fields' => 'all',
2331
            ) 
2332
        );
2333
2334
        $num_queries = $wpdb->num_queries;
2335
2336
        $found = get_terms(
2337
            'wptests_tax', array(
2338
            'hide_empty' => false,
2339
            'fields' => 'all',
2340
            ) 
2341
        );
2342
2343
        $this->assertSame($num_queries, $wpdb->num_queries);
2344
2345
        $this->assertNotEmpty($found);
2346
2347
        foreach ( $found as $term ) {
2348
            $this->assertInstanceOf('WP_Term', $term);
2349
        }
2350
    }
2351
2352
    /**
2353
     * @ticket 14162
2354
     */
2355 View Code Duplication
    public function test_should_prime_individual_term_cache_when_fields_is_all() 
2356
    {
2357
        global $wpdb;
2358
2359
        register_taxonomy('wptests_tax', 'post');
2360
        $terms = self::factory()->term->create_many(2, array( 'taxonomy' => 'wptests_tax' ));
2361
2362
        $found = get_terms(
2363
            'wptests_tax', array(
2364
            'hide_empty' => false,
2365
            'fields' => 'all',
2366
            ) 
2367
        );
2368
2369
        $num_queries = $wpdb->num_queries;
2370
        $term0 = get_term($terms[0]);
2371
        $this->assertSame($num_queries, $wpdb->num_queries);
2372
2373
    }
2374
2375
    /**
2376
     * @ticket 35382
2377
     */
2378
    public function test_indexes_should_not_be_reset_when_number_of_matched_terms_is_greater_than_number() 
2379
    {
2380
        register_taxonomy('wptests_tax', 'post', array( 'hierarchical' => true ));
2381
        $terms = self::factory()->term->create_many(3, array( 'taxonomy' => 'wptests_tax' ));
2382
2383
        $found = get_terms(
2384
            'wptests_tax', array(
2385
            'hide_empty' => false,
2386
            'fields' => 'id=>parent',
2387
            'number' => 2,
2388
            'orderby' => 'id',
2389
            'order' => 'ASC',
2390
            'hierarchical' => true,
2391
            ) 
2392
        );
2393
2394
        $this->assertSame(array( $terms[0], $terms[1] ), array_keys($found));
2395
    }
2396
2397
    /**
2398
     * @ticket 35935
2399
     */
2400
    public function test_hierarchical_offset_0_with_number_greater_than_total_available_count() 
2401
    {
2402
        register_taxonomy('wptests_tax', 'post', array( 'hierarchical' => true ));
2403
2404
        $terms = self::factory()->term->create_many(2, array( 'taxonomy' => 'wptests_tax' ));
2405
2406
        $found = get_terms(
2407
            'wptests_tax', array(
2408
            'number'     => 3,
2409
            'offset'     => 0,
2410
            'hide_empty' => false,
2411
            'fields'     => 'ids',
2412
            ) 
2413
        );
2414
        $this->assertEqualSets($terms, $found);
2415
    }
2416
2417
    /**
2418
     * @ticket 35935
2419
     */
2420 View Code Duplication
    public function test_hierarchical_offset_plus_number() 
2421
    {
2422
        register_taxonomy('wptests_tax', 'post', array( 'hierarchical' => true ));
2423
2424
        $terms = self::factory()->term->create_many(3, array( 'taxonomy' => 'wptests_tax' ));
2425
2426
        $found = get_terms(
2427
            'wptests_tax', array(
2428
            'number'     => 1,
2429
            'offset'     => 1,
2430
            'hide_empty' => false,
2431
            'fields'     => 'ids',
2432
            'orderby'    => 'term_id',
2433
            'order'      => 'ASC',
2434
            ) 
2435
        );
2436
        $this->assertEqualSets(array( $terms[1] ), $found);
2437
    }
2438
2439
    /**
2440
     * @ticket 35935
2441
     */
2442 View Code Duplication
    public function test_hierarchical_offset_plus_number_exceeds_available_count() 
2443
    {
2444
        register_taxonomy('wptests_tax', 'post', array( 'hierarchical' => true ));
2445
2446
        $terms = self::factory()->term->create_many(2, array( 'taxonomy' => 'wptests_tax' ));
2447
2448
        $found = get_terms(
2449
            'wptests_tax', array(
2450
            'number'     => 2,
2451
            'offset'     => 1,
2452
            'hide_empty' => false,
2453
            'fields'     => 'ids',
2454
            'orderby'    => 'term_id',
2455
            'order'      => 'ASC',
2456
            ) 
2457
        );
2458
        $this->assertEqualSets(array( $terms[1] ), $found);
2459
    }
2460
2461
    /**
2462
     * @ticket 35935
2463
     */
2464 View Code Duplication
    public function test_hierarchical_offset_exceeds_available_count() 
2465
    {
2466
        register_taxonomy('wptests_tax', 'post', array( 'hierarchical' => true ));
2467
2468
        $terms = self::factory()->term->create_many(2, array( 'taxonomy' => 'wptests_tax' ));
2469
2470
        $found = get_terms(
2471
            'wptests_tax', array(
2472
            'number'     => 100,
2473
            'offset'     => 3,
2474
            'hide_empty' => false,
2475
            'fields'     => 'ids',
2476
            'orderby'    => 'term_id',
2477
            'order'      => 'ASC',
2478
            ) 
2479
        );
2480
        $this->assertEqualSets(array(), $found);
2481
    }
2482
2483
    /**
2484
     * @ticket 36992
2485
     * @ticket 35381
2486
     */
2487
    public function test_count_should_not_pass_through_main_get_terms_filter() 
2488
    {
2489
        add_filter('get_terms', array( __CLASS__, 'maybe_filter_count' ));
2490
2491
        $found = get_terms(
2492
            array(
2493
            'hide_empty' => 0,
2494
            'fields' => 'count',
2495
            ) 
2496
        );
2497
2498
        remove_filter('get_terms', array( __CLASS__, 'maybe_filter_count' ));
2499
2500
        $this->assertNotEquals('foo', $found);
2501
    }
2502
2503
    public static function maybe_filter_count() 
2504
    {
2505
        return 'foo';
2506
    }
2507
2508
    protected function create_hierarchical_terms_and_posts() 
2509
    {
2510
        $terms = array();
2511
2512
        $terms['parent1'] = self::factory()->term->create(array( 'slug' => 'parent-1', 'name' => 'Parent 1', 'taxonomy' => 'hierarchical_fields' ));
2513
        $terms['parent2'] = self::factory()->term->create(array( 'slug' => 'parent-2', 'name' => 'Parent 2', 'taxonomy' => 'hierarchical_fields' ));
2514
        $terms['child1'] = self::factory()->term->create(array( 'slug' => 'child-1', 'name' => 'Child 1', 'taxonomy' => 'hierarchical_fields', 'parent' => $terms['parent1'] ));
2515
        $terms['child2'] = self::factory()->term->create(array( 'slug' => 'child-2', 'name' => 'Child 2', 'taxonomy' => 'hierarchical_fields', 'parent' => $terms['parent1'] ));
2516
        $terms['grandchild1'] = self::factory()->term->create(array( 'slug' => 'grandchild-1', 'name' => 'Grandchild 1', 'taxonomy' => 'hierarchical_fields', 'parent' => $terms['child1'] ));
2517
2518
        $post_id = self::factory()->post->create();
2519
        wp_set_post_terms($post_id, $terms['parent2'], 'hierarchical_fields', true);
2520
        wp_set_post_terms($post_id, $terms['child1'], 'hierarchical_fields', true);
2521
2522
        return $terms;
2523
    }
2524
2525
    protected function create_hierarchical_terms() 
2526
    {
2527
        // Set up the following hierarchy:
2528
        // - One
2529
        //   - Two
2530
        //     - Three (1)
2531
        //     - Four
2532
        //   - Five
2533
        //     - Six (1)
2534
        //   - Seven
2535
        $one_term = wp_insert_term(
2536
            'One',
2537
            'category'
2538
        );
2539
        $two_term = wp_insert_term(
2540
            'Two',
2541
            'category',
2542
            array(
2543
            'parent' => $one_term['term_id']
2544
            )
2545
        );
2546
        $three_term = wp_insert_term(
2547
            'Three',
2548
            'category',
2549
            array(
2550
            'parent' => $two_term['term_id']
2551
            )
2552
        );
2553
        $four_term = wp_insert_term(
2554
            'Four',
2555
            'category',
2556
            array(
2557
            'parent' => $two_term['term_id']
2558
            )
2559
        );
2560
        $five_term = wp_insert_term(
2561
            'Five',
2562
            'category',
2563
            array(
2564
            'parent' => $one_term['term_id']
2565
            )
2566
        );
2567
        $six_term = wp_insert_term(
2568
            'Six',
2569
            'category',
2570
            array(
2571
            'parent' => $five_term['term_id']
2572
            )
2573
        );
2574
        $seven_term = wp_insert_term(
2575
            'Seven',
2576
            'category',
2577
            array(
2578
            'parent' => $one_term['term_id']
2579
            )
2580
        );
2581
2582
        // Ensure child terms are not empty
2583
        $first_post_id = self::factory()->post->create();
2584
        $second_post_id = self::factory()->post->create();
2585
        wp_set_post_terms($first_post_id, array( $three_term['term_id'] ), 'category');
2586
        wp_set_post_terms($second_post_id, array( $six_term['term_id'] ), 'category');
2587
2588
        return array(
2589
         'one_term' => $one_term,
2590
         'two_term' => $two_term,
2591
         'three_term' => $three_term,
2592
         'four_term' => $four_term,
2593
         'five_term' => $five_term,
2594
         'six_term' => $six_term,
2595
         'seven_term' => $seven_term
2596
        );
2597
    }
2598
2599
    protected function set_up_three_posts_and_tags() 
2600
    {
2601
        $posts = self::factory()->post->create_many(3, array( 'post_type' => 'post' ));
2602
        foreach ( $posts as $i => $post ) {
2603
            wp_set_object_terms($post, "term_{$i}", 'post_tag');
2604
        }
2605
2606
        wp_cache_delete('last_changed', 'terms');
2607
    }
2608
}
2609