Completed
Pull Request — master (#2)
by Stephen
13:19
created

Tests_Term_getTerms::setUp()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 4
nc 1
nop 0
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * @group taxonomy
5
 */
6
class Tests_Term_getTerms extends WP_UnitTestCase {
7
	function setUp() {
8
		parent::setUp();
9
10
		_clean_term_filters();
11
		wp_cache_delete( 'last_changed', 'terms' );
12
	}
13
14
	/**
15
	 * @ticket 37568
16
	 */
17
	public function test_meta_query_args_only() {
18
		register_taxonomy( 'wptests_tax', 'post', array( 'hierarchical' => true ) );
19
20
		$term1 = self::factory()->term->create( array( 'taxonomy' => 'wptests_tax' ) );
21
		$term2 = self::factory()->term->create( array( 'taxonomy' => 'wptests_tax' ) );
22
23
		$post = self::factory()->post->create( array( 'post_type' => 'post' ) );
24
25
		update_term_meta( $term1, 'somekey', 'thevalue' );
26
27
		wp_set_post_terms( $post, array( $term1, $term2 ), 'wptests_tax' );
28
29
		$found = get_terms( array(
30
			'meta_key' => 'somekey',
31
			'meta_value' => 'thevalue',
32
		) );
33
34
		$this->assertEqualSets( array( $term1 ), wp_list_pluck( $found, 'term_id' ) );
35
	}
36
37
	/**
38
	 * @ticket 35495
39
	 */
40
	public function test_should_accept_an_args_array_containing_taxonomy_for_first_parameter() {
41
		register_taxonomy( 'wptests_tax', 'post' );
42
		$term = self::factory()->term->create( array( 'taxonomy' => 'wptests_tax' ) );
43
44
		$found = get_terms( array(
45
			'taxonomy' => 'wptests_tax',
46
			'hide_empty' => false,
47
			'fields' => 'ids',
48
			'update_term_meta_cache' => false,
49
		) );
50
51
		$this->assertEqualSets( array( $term ), $found );
52
	}
53
54
	/**
55
	 * @ticket 35495
56
	 * @ticket 35381
57
	 */
58
	public function test_legacy_params_as_query_string_should_be_properly_parsed() {
59
		register_taxonomy( 'wptests_tax', 'post' );
60
		$term = self::factory()->term->create( array( 'taxonomy' => 'wptests_tax' ) );
61
62
		$found = get_terms( 'wptests_tax', 'hide_empty=0&fields=ids&update_term_meta_cache=0' );
63
64
		$this->assertEqualSets( array( $term ), $found );
65
	}
66
67
	/**
68
	 * @ticket 35495
69
	 * @ticket 35381
70
	 */
71
	public function test_new_params_as_query_string_should_be_properly_parsed() {
72
		register_taxonomy( 'wptests_tax', 'post' );
73
		$term = self::factory()->term->create( array( 'taxonomy' => 'wptests_tax' ) );
74
75
		$found = get_terms( 'taxonomy=wptests_tax&hide_empty=0&fields=ids&update_term_meta_cache=0' );
76
77
		$this->assertEqualSets( array( $term ), $found );
78
	}
79
80
	/**
81
	 * @ticket 35495
82
	 */
83
	public function test_excluding_taxonomy_arg_should_return_terms_from_all_taxonomies() {
84
		register_taxonomy( 'wptests_tax1', 'post' );
85
		register_taxonomy( 'wptests_tax2', 'post' );
86
		$t1 = self::factory()->term->create( array( 'taxonomy' => 'wptests_tax1' ) );
87
		$t2 = self::factory()->term->create( array( 'taxonomy' => 'wptests_tax2' ) );
88
89
		$found = get_terms( array(
90
			'hide_empty' => false,
91
			'fields' => 'ids',
92
			'update_term_meta_cache' => false,
93
		) );
94
95
		// There may be other terms lying around, so don't do an exact match.
96
		$this->assertContains( $t1, $found );
97
		$this->assertContains( $t2, $found );
98
	}
99
100
	/**
101
	 * @ticket 23326
102
	 */
103
	public function test_get_terms_cache() {
104
		global $wpdb;
105
106
		$this->set_up_three_posts_and_tags();
107
108
		$num_queries = $wpdb->num_queries;
109
110
		// last_changed and num_queries should bump
111
		$terms = get_terms( 'post_tag', array( 'update_term_meta_cache' => false ) );
112
		$this->assertEquals( 3, count( $terms ) );
113
		$time1 = wp_cache_get( 'last_changed', 'terms' );
114
		$this->assertNotEmpty( $time1 );
115
		$this->assertEquals( $num_queries + 1, $wpdb->num_queries );
116
117
		$num_queries = $wpdb->num_queries;
118
119
		// Again. last_changed and num_queries should remain the same.
120
		$terms = get_terms( 'post_tag', array( 'update_term_meta_cache' => false ) );
121
		$this->assertEquals( 3, count( $terms ) );
122
		$this->assertEquals( $time1, wp_cache_get( 'last_changed', 'terms' ) );
123
		$this->assertEquals( $num_queries, $wpdb->num_queries );
124
	}
125
126
	/**
127
	 * @ticket 23326
128
	 */
129
	public function test_get_terms_cache_should_be_missed_when_passing_number() {
130
		global $wpdb;
131
132
		$this->set_up_three_posts_and_tags();
133
134
		// Prime cache
135
		$terms = get_terms( 'post_tag' );
0 ignored issues
show
Unused Code introduced by
$terms is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
136
		$time1 = wp_cache_get( 'last_changed', 'terms' );
137
		$num_queries = $wpdb->num_queries;
138
139
		// num_queries should bump, last_changed should remain the same.
140
		$terms = get_terms( 'post_tag', array( 'number' => 2 ) );
141
		$this->assertEquals( 2, count( $terms ) );
142
		$this->assertEquals( $time1, wp_cache_get( 'last_changed', 'terms' ) );
143
		$this->assertEquals( $num_queries + 1, $wpdb->num_queries );
144
145
		$num_queries = $wpdb->num_queries;
146
147
		// Again. last_changed and num_queries should remain the same.
148
		$terms = get_terms( 'post_tag', array( 'number' => 2 ) );
149
		$this->assertEquals( 2, count( $terms ) );
150
		$this->assertEquals( $time1, wp_cache_get( 'last_changed', 'terms' ) );
151
		$this->assertEquals( $num_queries, $wpdb->num_queries );
152
	}
153
154
	/**
155
	 * @ticket 23326
156
	 */
157
	public function test_wp_delete_term_should_invalidate_cache() {
158
		global $wpdb;
159
160
		$this->set_up_three_posts_and_tags();
161
162
		// Prime cache
163
		$terms = get_terms( 'post_tag' );
164
		$time1 = wp_cache_get( 'last_changed', 'terms' );
165
		$num_queries = $wpdb->num_queries;
0 ignored issues
show
Unused Code introduced by
$num_queries is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
166
167
		// Force last_changed to bump.
168
		wp_delete_term( $terms[0]->term_id, 'post_tag' );
169
170
		$num_queries = $wpdb->num_queries;
171
		$this->assertNotEquals( $time1, $time2 = wp_cache_get( 'last_changed', 'terms' ) );
172
173
		// last_changed and num_queries should bump after a term is deleted.
174
		$terms = get_terms( 'post_tag' );
175
		$this->assertEquals( 2, count( $terms ) );
176
		$this->assertEquals( $time2, wp_cache_get( 'last_changed', 'terms' ) );
177
		$this->assertEquals( $num_queries + 1, $wpdb->num_queries );
178
179
		$num_queries = $wpdb->num_queries;
180
181
		// Again. last_changed and num_queries should remain the same.
182
		$terms = get_terms( 'post_tag' );
183
		$this->assertEquals( 2, count( $terms ) );
184
		$this->assertEquals( $time2, wp_cache_get( 'last_changed', 'terms' ) );
185
		$this->assertEquals( $num_queries, $wpdb->num_queries );
186
187
		// @todo Repeat with term insert and update.
188
	}
189
190
	/**
191
	 * @ticket 23506
192
	 */
193
	function test_get_terms_should_allow_arbitrary_indexed_taxonomies_array() {
194
		$term_id = self::factory()->tag->create();
195
		$terms = get_terms( array( '111' => 'post_tag' ), array( 'hide_empty' => false ) );
196
		$this->assertEquals( $term_id, reset( $terms )->term_id );
197
	}
198
199
	/**
200
	 * @ticket 13661
201
	 */
202
	function test_get_terms_fields() {
203
		$term_id1 = self::factory()->tag->create( array( 'slug' => 'woo', 'name' => 'WOO!' ) );
204
		$term_id2 = self::factory()->tag->create( array( 'slug' => 'hoo', 'name' => 'HOO!', 'parent' => $term_id1 ) );
205
206
		$terms_id_parent = get_terms( 'post_tag', array( 'hide_empty' => false, 'fields' => 'id=>parent' ) );
207
		$this->assertEquals( array(
208
			$term_id1 => 0,
209
			$term_id2 => $term_id1
210
		), $terms_id_parent );
211
212
		$terms_ids = get_terms( 'post_tag', array( 'hide_empty' => false, 'fields' => 'ids' ) );
213
		$this->assertEqualSets( array( $term_id1, $term_id2 ), $terms_ids );
214
215
		$terms_name = get_terms( 'post_tag', array( 'hide_empty' => false, 'fields' => 'names' ) );
216
		$this->assertEqualSets( array( 'WOO!', 'HOO!' ), $terms_name );
217
218
		$terms_id_name = get_terms( 'post_tag', array( 'hide_empty' => false, 'fields' => 'id=>name' ) );
219
		$this->assertEquals( array(
220
			$term_id1 => 'WOO!',
221
			$term_id2 => 'HOO!',
222
		), $terms_id_name );
223
224
		$terms_id_slug = get_terms( 'post_tag', array( 'hide_empty' => false, 'fields' => 'id=>slug' ) );
225
		$this->assertEquals( array(
226
			$term_id1 => 'woo',
227
			$term_id2 => 'hoo'
228
		), $terms_id_slug );
229
	}
230
231
 	/**
0 ignored issues
show
Coding Style introduced by
There is some trailing whitespace on this line which should be avoided as per coding-style.
Loading history...
232
	 * @ticket 11823
233
 	 */
234
	function test_get_terms_include_exclude() {
235
		global $wpdb;
236
237
		$term_id1 = self::factory()->tag->create();
238
		$term_id2 = self::factory()->tag->create();
239
		$inc_terms = get_terms( 'post_tag', array(
240
			'include' => array( $term_id1, $term_id2 ),
241
			'hide_empty' => false
242
		) );
243
		$this->assertEquals( array( $term_id1, $term_id2 ), wp_list_pluck( $inc_terms, 'term_id' ) );
244
245
		$exc_terms = get_terms( 'post_tag', array(
246
			'exclude' => array( $term_id1, $term_id2 ),
247
			'hide_empty' => false
248
		) );
249
		$this->assertEquals( array(), wp_list_pluck( $exc_terms, 'term_id' ) );
250
251
		// These should not generate query errors.
252
		get_terms( 'post_tag', array( 'exclude' => array( 0 ), 'hide_empty' => false ) );
253
		$this->assertEmpty( $wpdb->last_error );
254
255
		get_terms( 'post_tag', array( 'exclude' => array( 'unexpected-string' ), 'hide_empty' => false ) );
256
		$this->assertEmpty( $wpdb->last_error );
257
258
		get_terms( 'post_tag', array( 'include' => array( 'unexpected-string' ), 'hide_empty' => false ) );
259
		$this->assertEmpty( $wpdb->last_error );
260
	}
261
262
	/**
263
	 * @ticket 30275
264
	 */
265
	public function test_exclude_with_hierarchical_true_for_non_hierarchical_taxonomy() {
266
		register_taxonomy( 'wptests_tax', 'post' );
267
268
		$terms = self::factory()->term->create_many( 2, array(
269
			'taxonomy' => 'wptests_tax',
270
		) );
271
272
		$found = get_terms( 'wptests_tax', array(
273
			'taxonomy' => 'wptests_tax',
274
			'hide_empty' => false,
275
			'exclude_tree' => array( $terms[0] ),
276
			'hierarchical' => true,
277
		) );
278
279
		$this->assertEquals( array( $terms[1] ), wp_list_pluck( $found, 'term_id' ) );
280
281
		_unregister_taxonomy( 'wptests_tax' );
282
	}
283
284
	/**
285
	 * @ticket 25710
286
	 */
287
	function test_get_terms_exclude_tree() {
288
289
		$term_id_uncategorized = get_option( 'default_category' );
290
291
		$term_id1 = self::factory()->category->create();
292
		$term_id11 = self::factory()->category->create( array( 'parent' => $term_id1 ) );
293
		$term_id2 = self::factory()->category->create();
294
		$term_id22 = self::factory()->category->create( array( 'parent' => $term_id2 ) );
295
296
		$terms = get_terms( 'category', array(
297
			'exclude' => $term_id_uncategorized,
298
			'fields' => 'ids',
299
			'hide_empty' => false,
300
		) );
301
		$this->assertEquals( array( $term_id1, $term_id11, $term_id2, $term_id22 ), $terms );
302
303
		$terms = get_terms( 'category', array(
304
			'fields' => 'ids',
305
			'exclude_tree' => "$term_id1,$term_id_uncategorized",
306
			'hide_empty' => false,
307
		) );
308
309
		$this->assertEquals( array( $term_id2, $term_id22 ), $terms );
310
311
	}
312
313
	/**
314
	 * @ticket 13992
315
	 */
316
	function test_get_terms_search() {
317
		$term_id1 = self::factory()->tag->create( array( 'slug' => 'burrito' ) );
318
		$term_id2 = self::factory()->tag->create( array( 'name' => 'Wilbur' ) );
319
		$term_id3 = self::factory()->tag->create( array( 'name' => 'Foo' ) );
0 ignored issues
show
Unused Code introduced by
$term_id3 is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
320
321
		$terms = get_terms( 'post_tag', array( 'hide_empty' => false, 'search' => 'bur', 'fields' => 'ids' ) );
322
		$this->assertEqualSets( array( $term_id1, $term_id2 ), $terms );
323
	}
324
325
	/**
326
	 * @ticket 8214
327
	 */
328
	function test_get_terms_like() {
329
		$term_id1 = self::factory()->tag->create( array( 'name' => 'burrito', 'description' => 'This is a burrito.' ) );
330
		$term_id2 = self::factory()->tag->create( array( 'name' => 'taco', 'description' => 'Burning man.' ) );
331
332
		$terms = get_terms( 'post_tag', array( 'hide_empty' => false, 'name__like' => 'bur', 'fields' => 'ids' ) );
333
		$this->assertEqualSets( array( $term_id1 ), $terms );
334
335
		$terms2 = get_terms( 'post_tag', array( 'hide_empty' => false, 'description__like' => 'bur', 'fields' => 'ids' ) );
336
		$this->assertEqualSets( array( $term_id1, $term_id2 ), $terms2 );
337
338
		$terms3 = get_terms( 'post_tag', array( 'hide_empty' => false, 'name__like' => 'Bur', 'fields' => 'ids' ) );
339
		$this->assertEqualSets( array( $term_id1 ), $terms3 );
340
341
		$terms4 = get_terms( 'post_tag', array( 'hide_empty' => false, 'description__like' => 'Bur', 'fields' => 'ids' ) );
342
		$this->assertEqualSets( array( $term_id1, $term_id2 ), $terms4 );
343
344
		$terms5 = get_terms( 'post_tag', array( 'hide_empty' => false, 'name__like' => 'ENCHILADA', 'fields' => 'ids' ) );
345
		$this->assertEmpty( $terms5 );
346
347
		$terms6 = get_terms( 'post_tag', array( 'hide_empty' => false, 'description__like' => 'ENCHILADA', 'fields' => 'ids' ) );
348
		$this->assertEmpty( $terms6 );
349
350
		$terms7 = get_terms( 'post_tag', array( 'hide_empty' => false, 'name__like' => 'o', 'fields' => 'ids' ) );
351
		$this->assertEqualSets( array( $term_id1, $term_id2 ), $terms7 );
352
353
		$terms8 = get_terms( 'post_tag', array( 'hide_empty' => false, 'description__like' => '.', 'fields' => 'ids' ) );
354
		$this->assertEqualSets( array( $term_id1, $term_id2 ), $terms8 );
355
	}
356
357
	/**
358
	 * @ticket 26903
359
	 */
360
	function test_get_terms_parent_zero() {
361
		$tax = 'food';
362
		register_taxonomy( $tax, 'post', array( 'hierarchical' => true ) );
363
364
		$cheese = self::factory()->term->create( array( 'name' => 'Cheese', 'taxonomy' => $tax ) );
365
366
		$cheddar = self::factory()->term->create( array( 'name' => 'Cheddar', 'parent' => $cheese, 'taxonomy' => $tax ) );
367
368
		$post_ids = self::factory()->post->create_many( 2 );
369
		foreach ( $post_ids as $id ) {
370
			wp_set_post_terms( $id, $cheddar, $tax );
371
		}
372
		$term = get_term( $cheddar, $tax );
373
		$this->assertEquals( 2, $term->count );
374
375
		$brie = self::factory()->term->create( array( 'name' => 'Brie', 'parent' => $cheese, 'taxonomy' => $tax ) );
376
		$post_id = self::factory()->post->create();
377
		wp_set_post_terms( $post_id, $brie, $tax );
378
		$term = get_term( $brie, $tax );
379
		$this->assertEquals( 1, $term->count );
380
381
		$crackers = self::factory()->term->create( array( 'name' => 'Crackers', 'taxonomy' => $tax ) );
382
383
		$butter = self::factory()->term->create( array( 'name' => 'Butter', 'parent' => $crackers, 'taxonomy' => $tax ) );
384
		$post_ids = self::factory()->post->create_many( 1 );
385
		foreach ( $post_ids as $id ) {
386
			wp_set_post_terms( $id, $butter, $tax );
387
		}
388
		$term = get_term( $butter, $tax );
389
		$this->assertEquals( 1, $term->count );
390
391
		$multigrain = self::factory()->term->create( array( 'name' => 'Multigrain', 'parent' => $crackers, 'taxonomy' => $tax ) );
392
		$post_ids = self::factory()->post->create_many( 1 );
393
		foreach ( $post_ids as $id ) {
394
			wp_set_post_terms( $id, $multigrain, $tax );
395
		}
396
		$term = get_term( $multigrain, $tax );
397
		$this->assertEquals( 1, $term->count );
398
399
		$fruit = self::factory()->term->create( array( 'name' => 'Fruit', 'taxonomy' => $tax ) );
400
		$cranberries = self::factory()->term->create( array( 'name' => 'Cranberries', 'parent' => $fruit, 'taxonomy' => $tax ) );
0 ignored issues
show
Unused Code introduced by
$cranberries is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
401
402
		$terms = get_terms( $tax, array( 'parent' => 0, 'cache_domain' => $tax ) );
403
		$this->assertEquals( 2, count( $terms ) );
404
		$this->assertEquals( wp_list_pluck( $terms, 'name' ), array( 'Cheese', 'Crackers' ) );
405
	}
406
407
	/**
408
	 * @ticket 26903
409
	 */
410
	function test_get_terms_grandparent_zero() {
411
		$tax = 'food';
412
		register_taxonomy( $tax, 'post', array( 'hierarchical' => true ) );
413
414
		$cheese = self::factory()->term->create( array( 'name' => 'Cheese', 'taxonomy' => $tax ) );
415
		$cheddar = self::factory()->term->create( array( 'name' => 'Cheddar', 'parent' => $cheese, 'taxonomy' => $tax ) );
416
		$spread = self::factory()->term->create( array( 'name' => 'Spread', 'parent' => $cheddar, 'taxonomy' => $tax ) );
417
		$post_id = self::factory()->post->create();
418
		wp_set_post_terms( $post_id, $spread, $tax );
419
		$term = get_term( $spread, $tax );
420
		$this->assertEquals( 1, $term->count );
421
422
		$terms = get_terms( $tax, array( 'parent' => 0, 'cache_domain' => $tax ) );
423
		$this->assertEquals( 1, count( $terms ) );
424
		$this->assertEquals( array( 'Cheese' ), wp_list_pluck( $terms, 'name' ) );
425
426
		_unregister_taxonomy( $tax );
427
	}
428
429
	/**
430
	 * @ticket 26903
431
	 */
432
	function test_get_terms_seven_levels_deep() {
433
		$tax = 'deep';
434
		register_taxonomy( $tax, 'post', array( 'hierarchical' => true ) );
435
		$parent = 0;
436
		$t = array();
437
		foreach ( range( 1, 7 ) as $depth ) {
438
			$t[$depth] = self::factory()->term->create( array( 'name' => 'term' . $depth, 'taxonomy' => $tax, 'parent' => $parent ) );
439
			$parent = $t[$depth];
440
		}
441
		$post_id = self::factory()->post->create();
442
		wp_set_post_terms( $post_id, $t[7], $tax );
443
		$term = get_term( $t[7], $tax );
444
		$this->assertEquals( 1, $term->count );
445
446
		$terms = get_terms( $tax, array( 'parent' => 0, 'cache_domain' => $tax ) );
447
		$this->assertEquals( 1, count( $terms ) );
448
		$this->assertEquals( array( 'term1' ), wp_list_pluck( $terms, 'name' ) );
449
450
		_unregister_taxonomy( $tax );
451
	}
452
453
	/**
454
	 * @ticket 27123
455
	 */
456
	function test_get_terms_child_of() {
457
		$parent = self::factory()->category->create();
458
		$child = self::factory()->category->create( array( 'parent' => $parent ) );
0 ignored issues
show
Unused Code introduced by
$child is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
459
460
		$terms = get_terms( 'category', array( 'child_of' => $parent, 'hide_empty' => false ) );
461
		$this->assertEquals( 1, count( $terms ) );
462
	}
463
464
	/**
465
	 * @ticket 31118
466
	 */
467
	public function test_child_of_should_skip_query_when_specified_parent_is_not_found_in_hierarchy_cache() {
468
		global $wpdb;
469
470
		register_taxonomy( 'wptests_tax', 'post', array( 'hierarchical' => true, ) );
471
472
		$terms = self::factory()->term->create_many( 3, array( 'taxonomy' => 'wptests_tax' ) );
473
474
		$num_queries = $wpdb->num_queries;
475
476
		$found = get_terms( 'wptests_tax', array(
477
			'hide_empty' => false,
478
			'child_of' => $terms[0],
479
		) );
480
481
		$this->assertEmpty( $found );
482
		$this->assertSame( $num_queries, $wpdb->num_queries );
483
	}
484
485
	/**
486
	 * @ticket 31118
487
	 */
488
	public function test_child_of_should_respect_multiple_taxonomies() {
489
		register_taxonomy( 'wptests_tax1', 'post', array( 'hierarchical' => true ) );
490
		register_taxonomy( 'wptests_tax2', 'post', array( 'hierarchical' => true ) );
491
492
		$t1 = self::factory()->term->create( array( 'taxonomy' => 'wptests_tax1' ) );
0 ignored issues
show
Unused Code introduced by
$t1 is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
493
		$t2 = self::factory()->term->create( array( 'taxonomy' => 'wptests_tax2' ) );
494
		$t3 = self::factory()->term->create( array( 'taxonomy' => 'wptests_tax2', 'parent' => $t2 ) );
495
496
		$found = get_terms( array( 'wptests_tax1', 'wptests_tax2' ), array(
497
			'fields' => 'ids',
498
			'hide_empty' => false,
499
			'child_of' => $t2,
500
		) );
501
502
		$this->assertEqualSets( array( $t3 ), $found );
503
	}
504
505
	/**
506
	 * @ticket 27123
507
	 */
508
	function test_get_term_children_recursion() {
509
		// Assume there is a way to insert a term with the parent pointing to itself
510
		// See: https://core.trac.wordpress.org/changeset/15806
511
		remove_filter( 'wp_update_term_parent', 'wp_check_term_hierarchy_for_loops', 10 );
512
513
		$term = wp_insert_term( 'Test', 'category' );
514
		$term = wp_update_term( $term['term_id'], 'category', array( 'parent' => $term['term_id'] ) );
515
		$term = get_term( $term['term_id'], 'category' );
516
517
		$this->assertEquals( $term->term_id, $term->parent );
518
		$this->assertInternalType( 'array', get_term_children( $term->term_id, 'category' ) );
519
520
		add_filter( 'wp_update_term_parent', 'wp_check_term_hierarchy_for_loops', 10, 3 );
521
	}
522
523
	/**
524
	 * @covers ::_get_term_children
525
	 * @ticket 24461
526
	 */
527
	public function test__get_term_children_handles_cycles() {
528
		remove_filter( 'wp_update_term_parent', 'wp_check_term_hierarchy_for_loops', 10 );
529
530
		$c1 = self::factory()->category->create();
531
		$c2 = self::factory()->category->create( array( 'parent' => $c1 ) );
532
		$c3 = self::factory()->category->create( array( 'parent' => $c2 ) );
533
		wp_update_term( $c1, 'category', array( 'parent' => $c3 ) );
534
535
		add_filter( 'wp_update_term_parent', 'wp_check_term_hierarchy_for_loops', 10, 3 );
536
537
		$result = _get_term_children( $c1, array( $c1, $c2, $c3 ), 'category' );
538
539
		$this->assertEqualSets( array( $c2, $c3 ), $result );
540
	}
541
542
	/**
543
	 * @covers ::_get_term_children
544
	 * @ticket 24461
545
	 */
546
	public function test__get_term_children_handles_cycles_when_terms_argument_contains_objects() {
547
		remove_filter( 'wp_update_term_parent', 'wp_check_term_hierarchy_for_loops', 10 );
548
549
		$c1 = self::factory()->category->create_and_get();
550
		$c2 = self::factory()->category->create_and_get( array( 'parent' => $c1->term_id ) );
551
		$c3 = self::factory()->category->create_and_get( array( 'parent' => $c2->term_id ) );
552
		wp_update_term( $c1->term_id, 'category', array( 'parent' => $c3->term_id ) );
553
554
		add_filter( 'wp_update_term_parent', 'wp_check_term_hierarchy_for_loops', 10, 3 );
555
556
		$result = _get_term_children( $c1->term_id, array( $c1, $c2, $c3 ), 'category' );
557
558
		$this->assertEqualSets( array( $c2, $c3 ), $result );
559
	}
560
561
	public function test_get_terms_by_slug() {
562
		$t1 = self::factory()->tag->create( array( 'slug' => 'foo' ) );
563
		$t2 = self::factory()->tag->create( array( 'slug' => 'bar' ) );
0 ignored issues
show
Unused Code introduced by
$t2 is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
564
565
		$found = get_terms( 'post_tag', array(
566
			'hide_empty' => false,
567
			'fields' => 'ids',
568
			'slug' => 'foo',
569
		) );
570
571
		$this->assertEquals( array( $t1 ), $found );
572
	}
573
574
	/**
575
	 * @ticket 23636
576
	 */
577
	public function test_get_terms_by_multiple_slugs() {
578
		$t1 = self::factory()->tag->create( array( 'slug' => 'foo' ) );
579
		$t2 = self::factory()->tag->create( array( 'slug' => 'bar' ) );
0 ignored issues
show
Unused Code introduced by
$t2 is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
580
		$t3 = self::factory()->tag->create( array( 'slug' => 'barry' ) );
581
582
		$found = get_terms( 'post_tag', array(
583
			'hide_empty' => false,
584
			'fields' => 'ids',
585
			'slug' => array( 'foo', 'barry' )
586
		) );
587
588
		$this->assertEquals( array( $t1, $t3 ), $found );
589
	}
590
591
	/**
592
	 * @ticket 30611
593
	 */
594
	public function test_get_terms_by_name() {
595
		$t1 = self::factory()->tag->create( array( 'name' => 'Foo' ) );
596
		$t2 = self::factory()->tag->create( array( 'name' => 'Bar' ) );
0 ignored issues
show
Unused Code introduced by
$t2 is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
597
598
		$found = get_terms( 'post_tag', array(
599
			'hide_empty' => false,
600
			'fields' => 'ids',
601
			'name' => 'Foo',
602
		) );
603
604
		$this->assertEquals( array( $t1 ), $found );
605
	}
606
607
	/**
608
	 * @ticket 30611
609
	 */
610
	public function test_get_terms_by_multiple_names() {
611
		$t1 = self::factory()->tag->create( array( 'name' => 'Foo' ) );
612
		$t2 = self::factory()->tag->create( array( 'name' => 'Bar' ) );
0 ignored issues
show
Unused Code introduced by
$t2 is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
613
		$t3 = self::factory()->tag->create( array( 'name' => 'Barry' ) );
614
615
		$found = get_terms( 'post_tag', array(
616
			'hide_empty' => false,
617
			'fields' => 'ids',
618
			'name' => array( 'Foo', 'Barry' )
619
		) );
620
621
		$this->assertEqualSets( array( $t3, $t1 ), $found );
622
	}
623
624
	/**
625
	 * @ticket 32248
626
	 */
627
	public function test_name_should_match_encoded_html_entities() {
628
		register_taxonomy( 'wptests_tax', 'post' );
629
630
		$t = self::factory()->term->create( array(
631
			'taxonomy' => 'wptests_tax',
632
			'name' => 'Foo & Bar',
633
			'slug' => 'foo-and-bar',
634
		) );
635
636
		$found = get_terms( 'wptests_tax', array(
637
			'hide_empty' => false,
638
			'fields' => 'ids',
639
			'name' => 'Foo & Bar',
640
		) );
641
		$this->assertEqualSets( array( $t ), $found );
642
643
		// array format.
644
		$found = get_terms( 'wptests_tax', array(
645
			'hide_empty' => false,
646
			'fields' => 'ids',
647
			'name' => array( 'Foo & Bar' ),
648
		) );
649
		$this->assertEqualSets( array( $t ), $found );
650
	}
651
652
	/**
653
	 * @ticket 35493
654
	 */
655
	public function test_name_should_not_double_escape_apostrophes() {
656
		register_taxonomy( 'wptests_tax', 'post' );
657
658
		$name = "Foo'Bar";
659
660
		$t = self::factory()->term->create( array(
661
			'taxonomy' => 'wptests_tax',
662
			'name' => $name,
663
		) );
664
665
		$term = get_term( $t, 'wptests_tax' );
666
667
		$this->assertSame( $name, $term->name );
668
669
		$found = get_terms( 'wptests_tax', array(
670
			'hide_empty' => false,
671
			'fields' => 'ids',
672
			'name' => $name,
673
		) );
674
675
		$this->assertEqualSets( array( $t ), $found );
676
	}
677
678
	/**
679
	 * @ticket 29839
680
	 */
681
	public function test_childless_should_return_all_terms_for_flat_hierarchy() {
682
		// If run on a flat hierarchy it should return everything.
683
		$flat_tax = 'countries';
684
		register_taxonomy( $flat_tax, 'post', array( 'hierarchical' => false ) );
685
		$australia = self::factory()->term->create( array( 'name' => 'Australia', 'taxonomy' => $flat_tax ) );
686
		$china     = self::factory()->term->create( array( 'name' => 'China',     'taxonomy' => $flat_tax ) );
687
		$tanzania  =  self::factory()->term->create( array( 'name' => 'Tanzania',  'taxonomy' => $flat_tax ) );
688
689
		$terms = get_terms( $flat_tax, array(
690
			'childless' => true,
691
			'hide_empty' => false,
692
			'fields' => 'ids',
693
		) );
694
695
		$expected = array( $australia, $china, $tanzania );
696
		$this->assertEqualSets( $expected, $terms );
697
	}
698
699
700
	/**
701
	 * @ticket 29839
702
	 */
703
	public function test_childless_hierarchical_taxonomy() {
704
		$tax = 'location';
705
		register_taxonomy( $tax, 'post', array( 'hierarchical' => true ) );
706
		/*
707
		Canada
708
			Ontario
709
				Ottawa
710
					Nepean
711
				Toronto
712
			Quebec
713
				Montreal
714
			PEI
715
		*/
716
		// Level 1
717
		$canada = self::factory()->term->create( array( 'name' => 'Canada', 'taxonomy' => $tax ) );
718
719
		// Level 2
720
		$ontario = self::factory()->term->create( array( 'name' => 'Ontario', 'parent' => $canada, 'taxonomy' => $tax ) );
721
		$quebec  = self::factory()->term->create( array( 'name' => 'Quebec', 'parent' => $canada, 'taxonomy' => $tax ) );
722
		$pei     = self::factory()->term->create( array( 'name' => 'PEI', 'parent' => $canada, 'taxonomy' => $tax ) );
723
724
		// Level 3
725
		$toronto  = self::factory()->term->create( array( 'name' => 'Toronto', 'parent' => $ontario, 'taxonomy' => $tax ) );
726
		$ottawa   = self::factory()->term->create( array( 'name' => 'Ottawa', 'parent' => $ontario, 'taxonomy' => $tax ) );
727
		$montreal = self::factory()->term->create( array( 'name' => 'Montreal', 'parent' => $quebec, 'taxonomy' => $tax ) );
728
729
		// Level 4
730
		$nepean = self::factory()->term->create( array( 'name' => 'Nepean', 'parent' => $ottawa, 'taxonomy' => $tax ) );
731
732
		$terms = get_terms( $tax, array(
733
			'childless' => true,
734
			'hide_empty' => false,
735
			'fields' => 'ids',
736
		) );
737
738
		$this->assertEqualSets( array( $montreal, $nepean, $toronto, $pei ), $terms );
739
	}
740
741
	/**
742
	 * @ticket 29839
743
	 */
744
	public function test_childless_hierarchical_taxonomy_used_with_child_of() {
745
		$tax = 'location';
746
		register_taxonomy( $tax, 'post', array( 'hierarchical' => true ) );
747
748
		// Level 1
749
		$canada = self::factory()->term->create( array( 'name' => 'Canada', 'taxonomy' => $tax ) );
750
751
		// Level 2
752
		$ontario = self::factory()->term->create( array( 'name' => 'Ontario', 'parent' => $canada, 'taxonomy' => $tax ) );
0 ignored issues
show
Unused Code introduced by
$ontario is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
753
		$quebec  = self::factory()->term->create( array( 'name' => 'Quebec', 'parent' => $canada, 'taxonomy' => $tax ) );
754
755
		// Level 3
756
		$laval   = self::factory()->term->create( array( 'name' => 'Laval', 'parent' => $quebec, 'taxonomy' => $tax ) );
757
		$montreal = self::factory()->term->create( array( 'name' => 'Montreal', 'parent' => $quebec, 'taxonomy' => $tax ) );
758
759
		// Level 4
760
		$dorval = self::factory()->term->create( array( 'name' => 'Dorval', 'parent' => $montreal, 'taxonomy' => $tax ) );
0 ignored issues
show
Unused Code introduced by
$dorval is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
761
762
		$terms = get_terms( $tax, array(
763
			'childless' => true,
764
			'child_of' => $quebec,
765
			'hide_empty' => false,
766
			'fields' => 'ids',
767
		) );
768
769
		$this->assertEqualSets( array( $laval ), $terms );
770
	}
771
772
	/**
773
	 * @ticket 29839
774
	 */
775
	public function test_childless_should_enforce_childless_status_for_all_queried_taxonomies() {
776
		register_taxonomy( 'wptests_tax1', 'post', array( 'hierarchical' => true ) );
777
		register_taxonomy( 'wptests_tax2', 'post', array( 'hierarchical' => true ) );
778
779
		$t1 = self::factory()->term->create( array( 'taxonomy' => 'wptests_tax1' ) );
780
		$t2 = self::factory()->term->create( array( 'taxonomy' => 'wptests_tax1', 'parent' => $t1 ) );
781
		$t3 = self::factory()->term->create( array( 'taxonomy' => 'wptests_tax2' ) );
782
		$t4 = self::factory()->term->create( array( 'taxonomy' => 'wptests_tax2', 'parent' => $t3 ) );
783
784
		$found = get_terms( array( 'wptests_tax1', 'wptests_tax2' ), array(
785
			'fields' => 'ids',
786
			'hide_empty' => false,
787
			'childless' => true,
788
		) );
789
790
		$this->assertEqualSets( array( $t2, $t4 ), $found );
791
	}
792
793
	public function test_get_terms_hierarchical_tax_hide_empty_false_fields_ids() {
794
		// Set up a clean taxonomy.
795
		$tax = 'hierarchical_fields';
796
		register_taxonomy( $tax, 'post', array( 'hierarchical' => true ) );
797
798
		$terms = $this->create_hierarchical_terms_and_posts();
799
800
		$found = get_terms( $tax, array(
801
			'hide_empty' => false,
802
			'fields' => 'ids',
803
		) );
804
805
		$expected = array(
806
			$terms['parent1'],
807
			$terms['parent2'],
808
			$terms['child1'],
809
			$terms['child2'],
810
			$terms['grandchild1'],
811
		);
812
813
		_unregister_taxonomy( 'hierarchical_fields' );
814
815
		$this->assertEqualSets( $expected, $found );
816
	}
817
818
	public function test_get_terms_hierarchical_tax_hide_empty_true_fields_ids() {
819
		// Set up a clean taxonomy.
820
		$tax = 'hierarchical_fields';
821
		register_taxonomy( $tax, 'post', array( 'hierarchical' => true ) );
822
823
		$terms = $this->create_hierarchical_terms_and_posts();
824
825
		$found = get_terms( $tax, array(
826
			'hide_empty' => true,
827
			'fields' => 'ids',
828
		) );
829
830
		$expected = array(
831
			$terms['parent1'],
832
			$terms['parent2'],
833
			$terms['child1'],
834
		);
835
836
		_unregister_taxonomy( 'hierarchical_fields' );
837
838
		$this->assertEqualSets( $expected, $found );
839
	}
840
841
	public function test_get_terms_hierarchical_tax_hide_empty_true_fields_ids_hierarchical_false() {
842
		// Set up a clean taxonomy.
843
		$tax = 'hierarchical_fields';
844
		register_taxonomy( $tax, 'post', array( 'hierarchical' => true ) );
845
846
		$terms = $this->create_hierarchical_terms_and_posts();
847
848
		$found = get_terms( $tax, array(
849
			'hide_empty' => true,
850
			'fields' => 'ids',
851
			'hierarchical' => false,
852
		) );
853
854
		$expected = array(
855
			$terms['parent2'],
856
			$terms['child1'],
857
		);
858
859
		_unregister_taxonomy( 'hierarchical_fields' );
860
861
		$this->assertEqualSets( $expected, $found );
862
	}
863
864
	public function test_get_terms_hierarchical_tax_hide_empty_false_fields_names() {
865
		// Set up a clean taxonomy.
866
		$tax = 'hierarchical_fields';
867
		register_taxonomy( $tax, 'post', array( 'hierarchical' => true ) );
868
869
		$terms = $this->create_hierarchical_terms_and_posts();
0 ignored issues
show
Unused Code introduced by
$terms is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
870
871
		$found = get_terms( $tax, array(
872
			'hide_empty' => false,
873
			'fields' => 'names',
874
		) );
875
876
		$expected = array(
877
			'Parent 1',
878
			'Parent 2',
879
			'Child 1',
880
			'Child 2',
881
			'Grandchild 1',
882
		);
883
884
		_unregister_taxonomy( 'hierarchical_fields' );
885
886
		$this->assertEqualSets( $expected, $found );
887
	}
888
889
	public function test_get_terms_hierarchical_tax_hide_empty_true_fields_names() {
890
		// Set up a clean taxonomy.
891
		$tax = 'hierarchical_fields';
892
		register_taxonomy( $tax, 'post', array( 'hierarchical' => true ) );
893
894
		$terms = $this->create_hierarchical_terms_and_posts();
0 ignored issues
show
Unused Code introduced by
$terms is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
895
896
		$found = get_terms( $tax, array(
897
			'hide_empty' => true,
898
			'fields' => 'names',
899
		) );
900
901
		$expected = array(
902
			'Parent 1',
903
			'Parent 2',
904
			'Child 1',
905
		);
906
907
		_unregister_taxonomy( 'hierarchical_fields' );
908
909
		$this->assertEqualSets( $expected, $found );
910
	}
911
912
	public function test_get_terms_hierarchical_tax_hide_empty_true_fields_names_hierarchical_false() {
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();
0 ignored issues
show
Unused Code introduced by
$terms is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
918
919
		$found = get_terms( $tax, array(
920
			'hide_empty' => true,
921
			'fields' => 'names',
922
			'hierarchical' => false,
923
		) );
924
925
		$expected = array(
926
			'Parent 2',
927
			'Child 1',
928
		);
929
930
		_unregister_taxonomy( 'hierarchical_fields' );
931
932
		$this->assertEqualSets( $expected, $found );
933
	}
934
935
	public function test_get_terms_hierarchical_tax_hide_empty_false_fields_count() {
936
		// Set up a clean taxonomy.
937
		$tax = 'hierarchical_fields';
938
		register_taxonomy( $tax, 'post', array( 'hierarchical' => true ) );
939
940
		$terms = $this->create_hierarchical_terms_and_posts();
0 ignored issues
show
Unused Code introduced by
$terms is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
941
942
		$found = get_terms( $tax, array(
943
			'hide_empty' => false,
944
			'fields' => 'count',
945
		) );
946
947
		_unregister_taxonomy( 'hierarchical_fields' );
948
949
		$this->assertEquals( 5, $found );
950
	}
951
952
	public function test_get_terms_hierarchical_tax_hide_empty_true_fields_count() {
953
		// Set up a clean taxonomy.
954
		$tax = 'hierarchical_fields';
955
		register_taxonomy( $tax, 'post', array( 'hierarchical' => true ) );
956
957
		$terms = $this->create_hierarchical_terms_and_posts();
0 ignored issues
show
Unused Code introduced by
$terms is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
958
959
		$found = get_terms( $tax, array(
960
			'hide_empty' => true,
961
			'fields' => 'count',
962
		) );
963
964
		_unregister_taxonomy( 'hierarchical_fields' );
965
966
		// When using 'fields=count', 'hierarchical' is forced to false.
967
		$this->assertEquals( 2, $found );
968
	}
969
970
	public function test_get_terms_hierarchical_tax_hide_empty_true_fields_count_hierarchical_false() {
971
		// Set up a clean taxonomy.
972
		$tax = 'hierarchical_fields';
973
		register_taxonomy( $tax, 'post', array( 'hierarchical' => true ) );
974
975
		$terms = $this->create_hierarchical_terms_and_posts();
0 ignored issues
show
Unused Code introduced by
$terms is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
976
977
		$found = get_terms( $tax, array(
978
			'hide_empty' => true,
979
			'fields' => 'count',
980
			'hierarchical' => false,
981
		) );
982
983
		_unregister_taxonomy( 'hierarchical_fields' );
984
985
		$this->assertEquals( 2, $found );
986
	}
987
988
	public function test_get_terms_hierarchical_tax_hide_empty_false_fields_idparent() {
989
		// Set up a clean taxonomy.
990
		$tax = 'hierarchical_fields';
991
		register_taxonomy( $tax, 'post', array( 'hierarchical' => true ) );
992
993
		$terms = $this->create_hierarchical_terms_and_posts();
994
995
		$found = get_terms( $tax, array(
996
			'hide_empty' => false,
997
			'fields' => 'id=>parent',
998
		) );
999
1000
		$expected = array(
1001
			$terms['parent1'] => 0,
1002
			$terms['parent2'] => 0,
1003
			$terms['child1'] => $terms['parent1'],
1004
			$terms['child2'] => $terms['parent1'],
1005
			$terms['grandchild1'] => $terms['child1'],
1006
		);
1007
1008
		_unregister_taxonomy( 'hierarchical_fields' );
1009
1010
		$this->assertEqualSetsWithIndex( $expected, $found );
1011
	}
1012
1013
	public function test_get_terms_hierarchical_tax_hide_empty_true_fields_idparent() {
1014
		// Set up a clean taxonomy.
1015
		$tax = 'hierarchical_fields';
1016
		register_taxonomy( $tax, 'post', array( 'hierarchical' => true ) );
1017
1018
		$terms = $this->create_hierarchical_terms_and_posts();
1019
1020
		$found = get_terms( $tax, array(
1021
			'hide_empty' => true,
1022
			'fields' => 'id=>parent',
1023
		) );
1024
1025
		$expected = array(
1026
			$terms['parent1'] => 0,
1027
			$terms['parent2'] => 0,
1028
			$terms['child1'] => $terms['parent1'],
1029
		);
1030
1031
		_unregister_taxonomy( 'hierarchical_fields' );
1032
1033
		$this->assertEqualSetsWithIndex( $expected, $found );
1034
	}
1035
1036
	public function test_get_terms_hierarchical_tax_hide_empty_true_fields_idparent_hierarchical_false() {
1037
		// Set up a clean taxonomy.
1038
		$tax = 'hierarchical_fields';
1039
		register_taxonomy( $tax, 'post', array( 'hierarchical' => true ) );
1040
1041
		$terms = $this->create_hierarchical_terms_and_posts();
1042
1043
		$found = get_terms( $tax, array(
1044
			'hide_empty' => true,
1045
			'fields' => 'id=>parent',
1046
			'hierarchical' => false,
1047
		) );
1048
1049
		$expected = array(
1050
			$terms['parent2'] => 0,
1051
			$terms['child1'] => $terms['parent1'],
1052
		);
1053
1054
		_unregister_taxonomy( 'hierarchical_fields' );
1055
1056
		$this->assertEqualSetsWithIndex( $expected, $found );
1057
	}
1058
1059
	public function test_get_terms_hierarchical_tax_hide_empty_false_fields_idslug() {
1060
		// Set up a clean taxonomy.
1061
		$tax = 'hierarchical_fields';
1062
		register_taxonomy( $tax, 'post', array( 'hierarchical' => true ) );
1063
1064
		$terms = $this->create_hierarchical_terms_and_posts();
1065
1066
		$found = get_terms( $tax, array(
1067
			'hide_empty' => false,
1068
			'fields' => 'id=>slug',
1069
		) );
1070
1071
		$expected = array(
1072
			$terms['parent1'] => 'parent-1',
1073
			$terms['parent2'] => 'parent-2',
1074
			$terms['child1'] => 'child-1',
1075
			$terms['child2'] => 'child-2',
1076
			$terms['grandchild1'] => 'grandchild-1',
1077
		);
1078
1079
		_unregister_taxonomy( 'hierarchical_fields' );
1080
1081
		$this->assertEqualSetsWithIndex( $expected, $found );
1082
	}
1083
1084
	/**
1085
	 * @ticket 29859
1086
	 */
1087
	public function test_get_terms_hierarchical_tax_hide_empty_true_fields_idslug() {
1088
		// Set up a clean taxonomy.
1089
		$tax = 'hierarchical_fields';
1090
		register_taxonomy( $tax, 'post', array( 'hierarchical' => true ) );
1091
1092
		$terms = $this->create_hierarchical_terms_and_posts();
1093
1094
		$found = get_terms( $tax, array(
1095
			'hide_empty' => true,
1096
			'fields' => 'id=>slug',
1097
		) );
1098
1099
		$expected = array(
1100
			$terms['parent1'] => 'parent-1',
1101
			$terms['parent2'] => 'parent-2',
1102
			$terms['child1'] => 'child-1',
1103
		);
1104
1105
		_unregister_taxonomy( 'hierarchical_fields' );
1106
1107
		$this->assertEqualSetsWithIndex( $expected, $found );
1108
	}
1109
1110
	public function test_get_terms_hierarchical_tax_hide_empty_true_fields_idslug_hierarchical_false() {
1111
		// Set up a clean taxonomy.
1112
		$tax = 'hierarchical_fields';
1113
		register_taxonomy( $tax, 'post', array( 'hierarchical' => true ) );
1114
1115
		$terms = $this->create_hierarchical_terms_and_posts();
1116
1117
		$found = get_terms( $tax, array(
1118
			'hide_empty' => true,
1119
			'fields' => 'id=>slug',
1120
			'hierarchical' => false,
1121
		) );
1122
1123
		$expected = array(
1124
			$terms['parent2'] => 'parent-2',
1125
			$terms['child1'] => 'child-1',
1126
		);
1127
1128
		_unregister_taxonomy( 'hierarchical_fields' );
1129
1130
		$this->assertEqualSetsWithIndex( $expected, $found );
1131
	}
1132
1133
	public function test_get_terms_hierarchical_tax_hide_empty_false_fields_idname() {
1134
		// Set up a clean taxonomy.
1135
		$tax = 'hierarchical_fields';
1136
		register_taxonomy( $tax, 'post', array( 'hierarchical' => true ) );
1137
1138
		$terms = $this->create_hierarchical_terms_and_posts();
1139
1140
		$found = get_terms( $tax, array(
1141
			'hide_empty' => false,
1142
			'fields' => 'id=>name',
1143
		) );
1144
1145
		$expected = array(
1146
			$terms['parent1'] => 'Parent 1',
1147
			$terms['parent2'] => 'Parent 2',
1148
			$terms['child1'] => 'Child 1',
1149
			$terms['child2'] => 'Child 2',
1150
			$terms['grandchild1'] => 'Grandchild 1',
1151
		);
1152
1153
		_unregister_taxonomy( 'hierarchical_fields' );
1154
1155
		$this->assertEqualSetsWithIndex( $expected, $found );
1156
	}
1157
1158
	/**
1159
	 * @ticket 29859
1160
	 */
1161
	public function test_get_terms_hierarchical_tax_hide_empty_true_fields_idname() {
1162
		// Set up a clean taxonomy.
1163
		$tax = 'hierarchical_fields';
1164
		register_taxonomy( $tax, 'post', array( 'hierarchical' => true ) );
1165
1166
		$terms = $this->create_hierarchical_terms_and_posts();
1167
1168
		$found = get_terms( $tax, array(
1169
			'hide_empty' => true,
1170
			'fields' => 'id=>name',
1171
		) );
1172
1173
		$expected = array(
1174
			$terms['parent1'] => 'Parent 1',
1175
			$terms['parent2'] => 'Parent 2',
1176
			$terms['child1'] => 'Child 1',
1177
		);
1178
1179
		_unregister_taxonomy( 'hierarchical_fields' );
1180
1181
		$this->assertEqualSetsWithIndex( $expected, $found );
1182
	}
1183
1184
	public function test_get_terms_hierarchical_tax_hide_empty_true_fields_idname_hierarchical_false() {
1185
		// Set up a clean taxonomy.
1186
		$tax = 'hierarchical_fields';
1187
		register_taxonomy( $tax, 'post', array( 'hierarchical' => true ) );
1188
1189
		$terms = $this->create_hierarchical_terms_and_posts();
1190
1191
		$found = get_terms( $tax, array(
1192
			'hide_empty' => true,
1193
			'fields' => 'id=>name',
1194
			'hierarchical' => false,
1195
		) );
1196
1197
		$expected = array(
1198
			$terms['parent2'] => 'Parent 2',
1199
			$terms['child1'] => 'Child 1',
1200
		);
1201
1202
		_unregister_taxonomy( 'hierarchical_fields' );
1203
1204
		$this->assertEqualSetsWithIndex( $expected, $found );
1205
	}
1206
1207
	/**
1208
	 * @ticket 31118
1209
	 */
1210
	public function test_hierarchical_should_recurse_properly_for_all_taxonomies() {
1211
		register_taxonomy( 'wptests_tax1', 'post', array( 'hierarchical' => true ) );
1212
		register_taxonomy( 'wptests_tax2', 'post', array( 'hierarchical' => true ) );
1213
1214
		$t1 = self::factory()->term->create( array( 'taxonomy' => 'wptests_tax1' ) );
1215
		$t2 = self::factory()->term->create( array( 'taxonomy' => 'wptests_tax1', 'parent' => $t1 ) );
1216
		$t3 = self::factory()->term->create( array( 'taxonomy' => 'wptests_tax1', 'parent' => $t2 ) );
1217
1218
		$t4 = self::factory()->term->create( array( 'taxonomy' => 'wptests_tax2' ) );
1219
		$t5 = self::factory()->term->create( array( 'taxonomy' => 'wptests_tax2', 'parent' => $t4 ) );
1220
		$t6 = self::factory()->term->create( array( 'taxonomy' => 'wptests_tax2', 'parent' => $t5 ) );
1221
1222
		$p = self::factory()->post->create();
1223
1224
		wp_set_object_terms( $p, $t3, 'wptests_tax1' );
1225
		wp_set_object_terms( $p, $t6, 'wptests_tax2' );
1226
1227
		$found = get_terms( array( 'wptests_tax1', 'wptests_tax2' ), array(
1228
			'hierarchical' => true,
1229
			'hide_empty' => true,
1230
			'fields' => 'ids',
1231
		) );
1232
1233
		/*
1234
		 * Should contain all terms, since they all have non-empty descendants.
1235
		 * To make the case clearer, test taxonomies separately.
1236
		 */
1237
1238
		// First taxonomy.
1239
		$this->assertContains( $t1, $found );
1240
		$this->assertContains( $t2, $found );
1241
		$this->assertContains( $t3, $found );
1242
1243
		// Second taxonomy.
1244
		$this->assertContains( $t4, $found );
1245
		$this->assertContains( $t5, $found );
1246
		$this->assertContains( $t6, $found );
1247
	}
1248
1249
	/**
1250
	 * @ticket 23261
1251
	 */
1252
	public function test_orderby_include() {
1253
		$tax = 'wptests_tax';
1254
		register_taxonomy( $tax, 'post' );
1255
1256
		$t1 = self::factory()->term->create( array( 'taxonomy' => $tax ) );
1257
		$t2 = self::factory()->term->create( array( 'taxonomy' => $tax ) );
1258
		$t3 = self::factory()->term->create( array( 'taxonomy' => $tax ) );
0 ignored issues
show
Unused Code introduced by
$t3 is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
1259
		$t4 = self::factory()->term->create( array( 'taxonomy' => $tax ) );
1260
1261
		$found = get_terms( $tax, array(
1262
			'fields' => 'ids',
1263
			'include' => array( $t4, $t1, $t2 ),
1264
			'orderby' => 'include',
1265
			'hide_empty' => false,
1266
		) );
1267
1268
		_unregister_taxonomy( 'wptests_tax' );
1269
1270
		$this->assertEquals( array( $t4, $t1, $t2 ), $found );
1271
	}
1272
1273
	/**
1274
	 * @ticket 31364
1275
	 */
1276
	public function test_orderby_description() {
1277
		$tax = 'wptests_tax';
1278
		register_taxonomy( $tax, 'post' );
1279
1280
		$t1 = self::factory()->term->create( array( 'taxonomy' => $tax, 'description' => 'fff' ) );
1281
		$t2 = self::factory()->term->create( array( 'taxonomy' => $tax, 'description' => 'aaa' ) );
1282
		$t3 = self::factory()->term->create( array( 'taxonomy' => $tax, 'description' => 'zzz' ) );
1283
		$t4 = self::factory()->term->create( array( 'taxonomy' => $tax, 'description' => 'jjj' ) );
1284
1285
		$found = get_terms( $tax, array(
1286
			'fields' => 'ids',
1287
			'orderby' => 'description',
1288
			'hide_empty' => false,
1289
		) );
1290
1291
		_unregister_taxonomy( 'wptests_tax' );
1292
1293
		$this->assertEquals( array( $t2, $t1, $t4, $t3 ), $found );
1294
	}
1295
1296
	/**
1297
	 * @ticket 33726
1298
	 */
1299
	public function test_orderby_term_id() {
1300
		register_taxonomy( 'wptests_tax', 'post' );
1301
		$t1 = self::factory()->term->create( array(
1302
			'taxonomy' => 'wptests_tax',
1303
			'name' => 'AAA',
1304
		) );
1305
		$t2 = self::factory()->term->create( array(
1306
			'taxonomy' => 'wptests_tax',
1307
			'name' => 'ZZZ',
1308
		) );
1309
		$t3 = self::factory()->term->create( array(
1310
			'taxonomy' => 'wptests_tax',
1311
			'name' => 'JJJ',
1312
		) );
1313
1314
		$found = get_terms( 'wptests_tax', array(
1315
			'orderby' => 'term_id',
1316
			'hide_empty' => false,
1317
			'fields' => 'ids',
1318
		) );
1319
1320
		$this->assertEquals( array( $t1, $t2, $t3 ), $found );
1321
	}
1322
1323
	/**
1324
	 * @ticket 34996
1325
	 */
1326
	public function test_orderby_meta_value() {
1327
		register_taxonomy( 'wptests_tax', 'post' );
1328
		$terms = self::factory()->term->create_many( 3, array( 'taxonomy' => 'wptests_tax' ) );
1329
		add_term_meta( $terms[0], 'foo', 'zzz' );
1330
		add_term_meta( $terms[0], 'fee', 'ber' );
1331
		add_term_meta( $terms[1], 'foo', 'aaa' );
1332
		add_term_meta( $terms[1], 'fee', 'ber' );
1333
		add_term_meta( $terms[2], 'foo', 'jjj' );
1334
		add_term_meta( $terms[2], 'fee', 'ber' );
1335
1336
		// Matches the first meta query clause.
1337
		$found = get_terms( 'wptests_tax', array(
1338
			'hide_empty' => false,
1339
			'meta_query' => array(
1340
				'relation' => 'AND',
1341
				array(
1342
					'key' => 'foo',
1343
					'compare' => 'EXISTS',
1344
				),
1345
				array(
1346
					'key' => 'fee',
1347
					'compare' => 'EXISTS',
1348
				),
1349
			),
1350
			'orderby' => 'meta_value',
1351
			'order' => 'ASC',
1352
			'fields' => 'ids',
1353
		) );
1354
1355
		$this->assertEqualSets( array( $terms[1], $terms[2], $terms[0] ), $found );
1356
	}
1357
1358
	/**
1359
	 * @ticket 34996
1360
	 */
1361
	public function test_orderby_meta_value_num() {
1362
		register_taxonomy( 'wptests_tax', 'post' );
1363
		$terms = self::factory()->term->create_many( 3, array( 'taxonomy' => 'wptests_tax' ) );
1364
		add_term_meta( $terms[0], 'foo', '999' );
1365
		add_term_meta( $terms[0], 'fee', 'ber' );
1366
		add_term_meta( $terms[1], 'foo', '111' );
1367
		add_term_meta( $terms[1], 'fee', 'ber' );
1368
		add_term_meta( $terms[2], 'foo', '555' );
1369
		add_term_meta( $terms[2], 'fee', 'ber' );
1370
1371
		// Matches the first meta query clause.
1372
		$found = get_terms( 'wptests_tax', array(
1373
			'hide_empty' => false,
1374
			'meta_query' => array(
1375
				'relation' => 'AND',
1376
				array(
1377
					'key' => 'foo',
1378
					'compare' => 'EXISTS',
1379
				),
1380
				array(
1381
					'key' => 'fee',
1382
					'compare' => 'EXISTS',
1383
				),
1384
			),
1385
			'orderby' => 'meta_value_num',
1386
			'order' => 'ASC',
1387
			'fields' => 'ids',
1388
		) );
1389
1390
		$this->assertEqualSets( array( $terms[1], $terms[2], $terms[0] ), $found );
1391
	}
1392
1393
	/**
1394
	 * @ticket 34996
1395
	 */
1396
	public function test_orderby_meta_value_with_meta_key() {
1397
		register_taxonomy( 'wptests_tax', 'post' );
1398
		$terms = self::factory()->term->create_many( 3, array( 'taxonomy' => 'wptests_tax' ) );
1399
		add_term_meta( $terms[0], 'foo', 'bar' );
1400
		add_term_meta( $terms[0], 'fee', 'zzz' );
1401
		add_term_meta( $terms[0], 'faa', 'jjj' );
1402
		add_term_meta( $terms[1], 'foo', 'bar' );
1403
		add_term_meta( $terms[1], 'fee', 'aaa' );
1404
		add_term_meta( $terms[1], 'faa', 'aaa' );
1405
		add_term_meta( $terms[2], 'foo', 'bar' );
1406
		add_term_meta( $terms[2], 'fee', 'jjj' );
1407
		add_term_meta( $terms[2], 'faa', 'zzz' );
1408
1409
		$found = get_terms( 'wptests_tax', array(
1410
			'hide_empty' => false,
1411
			'meta_key' => 'fee',
1412
			'orderby' => 'meta_value',
1413
			'order' => 'ASC',
1414
			'fields' => 'ids',
1415
		) );
1416
1417
		$this->assertEqualSets( array( $terms[1], $terms[2], $terms[0] ), $found );
1418
1419
		$found = get_terms( 'wptests_tax', array(
1420
			'hide_empty' => false,
1421
			'meta_query' => array(
1422
				array(
1423
					'key' => 'foo',
1424
					'compare' => 'EXISTS',
1425
				),
1426
			),
1427
			'meta_key' => 'fee',
1428
			'orderby' => 'meta_value',
1429
			'order' => 'ASC',
1430
			'fields' => 'ids',
1431
		) );
1432
1433
		$this->assertEqualSets( array( $terms[1], $terms[2], $terms[0] ), $found );
1434
1435
		// Matches the first meta query clause.
1436
		$found = get_terms( 'wptests_tax', array(
1437
			'hide_empty' => false,
1438
			'meta_query' => array(
1439
				'relation' => 'AND',
1440
				array(
1441
					'key' => 'foo',
1442
					'compare' => 'EXISTS',
1443
				),
1444
				array(
1445
					'key' => 'fee',
1446
					'compare' => 'EXISTS',
1447
				),
1448
			),
1449
			'meta_key' => 'fee',
1450
			'orderby' => 'meta_value',
1451
			'order' => 'ASC',
1452
			'fields' => 'ids',
1453
		) );
1454
1455
		$this->assertEqualSets( array( $terms[1], $terms[2], $terms[0] ), $found );
1456
1457
		// Matches the meta query clause corresponding to the 'meta_key' param.
1458
		$found = get_terms( 'wptests_tax', array(
1459
			'hide_empty' => false,
1460
			'meta_query' => array(
1461
				'relation' => 'AND',
1462
				array(
1463
					'key' => 'foo',
1464
					'compare' => 'EXISTS',
1465
				),
1466
				array(
1467
					'key' => 'fee',
1468
					'compare' => 'EXISTS',
1469
				),
1470
			),
1471
			'meta_key' => 'faa',
1472
			'orderby' => 'meta_value',
1473
			'order' => 'ASC',
1474
			'fields' => 'ids',
1475
		) );
1476
1477
		$this->assertEqualSets( array( $terms[1], $terms[0], $terms[2] ), $found );
1478
	}
1479
1480
	/**
1481
	 * @ticket 34996
1482
	 */
1483
	public function test_orderby_meta_value_num_with_meta_key() {
1484
		register_taxonomy( 'wptests_tax', 'post' );
1485
		$terms = self::factory()->term->create_many( 3, array( 'taxonomy' => 'wptests_tax' ) );
1486
		add_term_meta( $terms[0], 'foo', 'bar' );
1487
		add_term_meta( $terms[0], 'fee', '999' );
1488
		add_term_meta( $terms[0], 'faa', '555' );
1489
		add_term_meta( $terms[1], 'foo', 'bar' );
1490
		add_term_meta( $terms[1], 'fee', '111' );
1491
		add_term_meta( $terms[1], 'faa', '111' );
1492
		add_term_meta( $terms[2], 'foo', 'bar' );
1493
		add_term_meta( $terms[2], 'fee', '555' );
1494
		add_term_meta( $terms[2], 'faa', '999' );
1495
1496
		$found = get_terms( 'wptests_tax', array(
1497
			'hide_empty' => false,
1498
			'meta_key' => 'fee',
1499
			'orderby' => 'meta_value',
1500
			'order' => 'ASC',
1501
			'fields' => 'ids',
1502
		) );
1503
1504
		$this->assertEqualSets( array( $terms[1], $terms[2], $terms[0] ), $found );
1505
1506
		$found = get_terms( 'wptests_tax', array(
1507
			'hide_empty' => false,
1508
			'meta_query' => array(
1509
				array(
1510
					'key' => 'foo',
1511
					'compare' => 'EXISTS',
1512
				),
1513
			),
1514
			'meta_key' => 'fee',
1515
			'orderby' => 'meta_value',
1516
			'order' => 'ASC',
1517
			'fields' => 'ids',
1518
		) );
1519
1520
		$this->assertEqualSets( array( $terms[1], $terms[2], $terms[0] ), $found );
1521
1522
		$found = get_terms( 'wptests_tax', array(
1523
			'hide_empty' => false,
1524
			'meta_query' => array(
1525
				'relation' => 'AND',
1526
				array(
1527
					'key' => 'foo',
1528
					'compare' => 'EXISTS',
1529
				),
1530
				array(
1531
					'key' => 'fee',
1532
					'compare' => 'EXISTS',
1533
				),
1534
			),
1535
			'meta_key' => 'fee',
1536
			'orderby' => 'meta_value',
1537
			'order' => 'ASC',
1538
			'fields' => 'ids',
1539
		) );
1540
1541
		$this->assertEqualSets( array( $terms[1], $terms[2], $terms[0] ), $found );
1542
1543
		$found = get_terms( 'wptests_tax', array(
1544
			'hide_empty' => false,
1545
			'meta_query' => array(
1546
				'relation' => 'AND',
1547
				array(
1548
					'key' => 'foo',
1549
					'compare' => 'EXISTS',
1550
				),
1551
				array(
1552
					'key' => 'fee',
1553
					'compare' => 'EXISTS',
1554
				),
1555
			),
1556
			'meta_key' => 'faa',
1557
			'orderby' => 'meta_value',
1558
			'order' => 'ASC',
1559
			'fields' => 'ids',
1560
		) );
1561
1562
		$this->assertEqualSets( array( $terms[1], $terms[0], $terms[2] ), $found );
1563
	}
1564
1565
	/**
1566
	 * @ticket 34996
1567
	 */
1568
	public function test_orderby_clause_key() {
1569
		register_taxonomy( 'wptests_tax', 'post' );
1570
		$terms = self::factory()->term->create_many( 3, array( 'taxonomy' => 'wptests_tax' ) );
1571
		add_term_meta( $terms[0], 'foo', 'zzz' );
1572
		add_term_meta( $terms[0], 'fee', 'jjj' );
1573
		add_term_meta( $terms[1], 'foo', 'aaa' );
1574
		add_term_meta( $terms[1], 'fee', 'aaa' );
1575
		add_term_meta( $terms[2], 'foo', 'jjj' );
1576
		add_term_meta( $terms[2], 'fee', 'zzz' );
1577
1578
		$found = get_terms( 'wptests_tax', array(
1579
			'hide_empty' => false,
1580
			'meta_query' => array(
1581
				'relation' => 'AND',
1582
				'foo_key' => array(
1583
					'key' => 'foo',
1584
					'compare' => 'EXISTS',
1585
				),
1586
				'fee_key' => array(
1587
					'key' => 'fee',
1588
					'compare' => 'EXISTS',
1589
				),
1590
			),
1591
			'orderby' => 'foo_key',
1592
			'order' => 'ASC',
1593
			'fields' => 'ids',
1594
		) );
1595
1596
		$this->assertEqualSets( array( $terms[1], $terms[2], $terms[0] ), $found );
1597
1598
		$found = get_terms( 'wptests_tax', array(
1599
			'hide_empty' => false,
1600
			'meta_query' => array(
1601
				'relation' => 'AND',
1602
				'foo_key' => array(
1603
					'key' => 'foo',
1604
					'compare' => 'EXISTS',
1605
				),
1606
				'fee_key' => array(
1607
					'key' => 'fee',
1608
					'compare' => 'EXISTS',
1609
				),
1610
			),
1611
			'orderby' => 'fee_key',
1612
			'order' => 'ASC',
1613
			'fields' => 'ids',
1614
		) );
1615
1616
		$this->assertEqualSets( array( $terms[1], $terms[0], $terms[2] ), $found );
1617
1618
		$expected = get_terms( 'wptests_tax', array(
1619
			'hide_empty' => false,
1620
			'meta_query' => array(
1621
				'relation' => 'AND',
1622
				'foo_key' => array(
1623
					'key' => 'foo',
1624
					'compare' => 'EXISTS',
1625
				),
1626
				'fee_key' => array(
1627
					'key' => 'fee',
1628
					'compare' => 'EXISTS',
1629
				),
1630
			),
1631
			'fields' => 'ids',
1632
		) );
1633
1634
		$found = get_terms( 'wptests_tax', array(
1635
			'hide_empty' => false,
1636
			'meta_query' => array(
1637
				'relation' => 'AND',
1638
				'foo_key' => array(
1639
					'key' => 'foo',
1640
					'compare' => 'EXISTS',
1641
				),
1642
				'fee_key' => array(
1643
					'key' => 'fee',
1644
					'compare' => 'EXISTS',
1645
				),
1646
			),
1647
			'orderby' => 'faa_key',
1648
			'fields' => 'ids',
1649
		) );
1650
1651
		$this->assertEqualSets( $expected, $found );
1652
	}
1653
1654
	public function test_hierarchical_false_with_parent() {
1655
		$initial_terms = $this->create_hierarchical_terms();
1656
1657
		// Case where hierarchical is false
1658
		$terms = get_terms( 'category', array(
1659
			'hierarchical' => false,
1660
			'parent' => $initial_terms['one_term']['term_id']
1661
		) );
1662
1663
		// Verify that there are no children
1664
		$this->assertEquals( 0, count( $terms ) );
1665
	}
1666
1667
	/**
1668
	 * @ticket 29185
1669
	 */
1670
	public function test_hierarchical_true_with_parent() {
1671
		$initial_terms = $this->create_hierarchical_terms();
1672
1673
		// Case where hierarchical is true
1674
		$terms = get_terms( 'category', array(
1675
			'hierarchical' => true,
1676
			'parent' => $initial_terms['one_term']['term_id']
1677
		) );
1678
1679
		// Verify that the children with non-empty descendants are returned
1680
		$expected = array(
1681
			$initial_terms['two_term']['term_id'],
1682
			$initial_terms['five_term']['term_id'],
1683
		);
1684
		$actual = wp_list_pluck( $terms, 'term_id' );
1685
		$this->assertEqualSets( $expected, $actual );
1686
	}
1687
1688
	public function test_hierarchical_false_with_child_of_and_direct_child() {
1689
		$initial_terms = $this->create_hierarchical_terms();
1690
		$post_id = self::factory()->post->create();
1691
		wp_set_post_terms(
1692
			$post_id,
1693
			array( $initial_terms['seven_term']['term_id'] ),
1694
			'category'
1695
		);
1696
1697
		// Case where hierarchical is false
1698
		$terms = get_terms( 'category', array(
1699
			'hierarchical' => false,
1700
			'child_of' => $initial_terms['one_term']['term_id']
1701
		) );
1702
1703
		$expected = array(
1704
			$initial_terms['seven_term']['term_id'],
1705
		);
1706
1707
		$actual = wp_list_pluck( $terms, 'term_id' );
1708
		$this->assertEqualSets( $expected, $actual );
1709
	}
1710
1711
	public function test_hierarchical_false_with_child_of_should_not_return_grandchildren() {
1712
		$initial_terms = $this->create_hierarchical_terms();
1713
1714
		// Case where hierarchical is false
1715
		$terms = get_terms( 'category', array(
1716
			'hierarchical' => false,
1717
			'child_of' => $initial_terms['one_term']['term_id']
1718
		) );
1719
1720
		// Verify that there are no children
1721
		$this->assertEquals( 0, count( $terms ) );
1722
	}
1723
1724
	public function test_hierarchical_true_with_child_of_should_return_grandchildren() {
1725
		$initial_terms = $this->create_hierarchical_terms();
1726
1727
		// Case where hierarchical is true
1728
		$terms = get_terms( 'category', array(
1729
			'hierarchical' => true,
1730
			'child_of' => $initial_terms['one_term']['term_id']
1731
		) );
1732
1733
		$expected = array(
1734
			$initial_terms['two_term']['term_id'],
1735
			$initial_terms['three_term']['term_id'],
1736
			$initial_terms['five_term']['term_id'],
1737
			$initial_terms['six_term']['term_id'],
1738
		);
1739
		$actual = wp_list_pluck( $terms, 'term_id' );
1740
		$this->assertEqualSets( $expected, $actual );
1741
	}
1742
1743
	public function test_parent_should_override_child_of() {
1744
		$initial_terms = $this->create_hierarchical_terms();
1745
1746
		$terms = get_terms( 'category', array(
1747
			'hide_empty' => false,
1748
			'child_of' => $initial_terms['one_term']['term_id'],
1749
			'parent' => $initial_terms['one_term']['term_id']
1750
		) );
1751
1752
		// Verify that parent takes precedence over child_of and returns only direct children.
1753
		$expected = array(
1754
			$initial_terms['two_term']['term_id'],
1755
			$initial_terms['five_term']['term_id'],
1756
			$initial_terms['seven_term']['term_id']
1757
		);
1758
		$actual = wp_list_pluck( $terms, 'term_id' );
1759
		$this->assertEqualSets( $expected, $actual );
1760
	}
1761
1762
	/**
1763
	 * @ticket 31118
1764
	 */
1765
	public function test_parent_should_skip_query_when_specified_parent_is_not_found_in_hierarchy_cache() {
1766
		global $wpdb;
1767
1768
		register_taxonomy( 'wptests_tax', 'post', array( 'hierarchical' => true, ) );
1769
1770
		$terms = self::factory()->term->create_many( 3, array( 'taxonomy' => 'wptests_tax' ) );
1771
1772
		$num_queries = $wpdb->num_queries;
1773
1774
		$found = get_terms( 'wptests_tax', array(
1775
			'hide_empty' => false,
1776
			'parent' => $terms[0],
1777
		) );
1778
1779
		$this->assertEmpty( $found );
1780
		$this->assertSame( $num_queries, $wpdb->num_queries );
1781
	}
1782
1783
	/**
1784
	 * @ticket 31118
1785
	 */
1786
	public function test_parent_should_respect_multiple_taxonomies() {
1787
		register_taxonomy( 'wptests_tax1', 'post', array( 'hierarchical' => true ) );
1788
		register_taxonomy( 'wptests_tax2', 'post', array( 'hierarchical' => true ) );
1789
1790
		$t1 = self::factory()->term->create( array( 'taxonomy' => 'wptests_tax1' ) );
0 ignored issues
show
Unused Code introduced by
$t1 is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
1791
		$t2 = self::factory()->term->create( array( 'taxonomy' => 'wptests_tax2' ) );
1792
		$t3 = self::factory()->term->create( array( 'taxonomy' => 'wptests_tax2', 'parent' => $t2 ) );
1793
1794
		$found = get_terms( array( 'wptests_tax1', 'wptests_tax2' ), array(
1795
			'fields' => 'ids',
1796
			'hide_empty' => false,
1797
			'parent' => $t2,
1798
		) );
1799
1800
		$this->assertEqualSets( array( $t3 ), $found );
1801
	}
1802
1803
	public function test_hierarchical_false_parent_should_override_child_of() {
1804
		$initial_terms = $this->create_hierarchical_terms();
1805
1806
		// Case where hierarchical is false
1807
		$terms = get_terms( 'category', array(
1808
			'hierarchical' => false,
1809
			'child_of' => $initial_terms['one_term']['term_id'],
1810
			'parent' => $initial_terms['one_term']['term_id']
1811
		) );
1812
1813
		// hierarchical=false means that descendants are not fetched.
1814
		$this->assertEquals( 0, count( $terms ) );
1815
	}
1816
1817
	/**
1818
	 * @ticket 29185
1819
	 */
1820
	public function test_hierarchical_true_parent_overrides_child_of() {
1821
		$initial_terms = $this->create_hierarchical_terms();
1822
1823
		// Case where hierarchical is true
1824
		$terms = get_terms( 'category', array(
1825
			'hierarchical' => true,
1826
			'child_of' => $initial_terms['one_term']['term_id'],
1827
			'parent' => $initial_terms['one_term']['term_id'],
1828
		) );
1829
1830
		// Verify that parent takes precedence over child_of
1831
		$expected = array(
1832
			$initial_terms['two_term']['term_id'],
1833
			$initial_terms['five_term']['term_id'],
1834
		);
1835
		$actual = wp_list_pluck( $terms, 'term_id' );
1836
		$this->assertEqualSets( $expected, $actual );
1837
	}
1838
1839
	public function test_pad_counts() {
1840
		register_taxonomy( 'wptests_tax_1', 'post', array( 'hierarchical' => true ) );
1841
1842
		$posts = self::factory()->post->create_many( 3 );
1843
1844
		$t1 = self::factory()->term->create( array(
1845
			'taxonomy' => 'wptests_tax_1',
1846
		) );
1847
		$t2 = self::factory()->term->create( array(
1848
			'taxonomy' => 'wptests_tax_1',
1849
			'parent' => $t1,
1850
		) );
1851
		$t3 = self::factory()->term->create( array(
1852
			'taxonomy' => 'wptests_tax_1',
1853
			'parent' => $t2,
1854
		) );
1855
1856
		wp_set_object_terms( $posts[0], array( $t1 ), 'wptests_tax_1' );
1857
		wp_set_object_terms( $posts[1], array( $t2 ), 'wptests_tax_1' );
1858
		wp_set_object_terms( $posts[2], array( $t3 ), 'wptests_tax_1' );
1859
1860
		$found = get_terms( 'wptests_tax_1', array(
1861
			'pad_counts' => true,
1862
		) );
1863
1864
		$this->assertEqualSets( array( $t1, $t2, $t3 ), wp_list_pluck( $found, 'term_id' ) );
1865
1866
		foreach ( $found as $f ) {
0 ignored issues
show
Bug introduced by
The expression $found of type array|integer|object<WP_Error> is not guaranteed to be traversable. How about adding an additional type check?

There are different options of fixing this problem.

  1. If you want to be on the safe side, you can add an additional type-check:

    $collection = json_decode($data, true);
    if ( ! is_array($collection)) {
        throw new \RuntimeException('$collection must be an array.');
    }
    
    foreach ($collection as $item) { /** ... */ }
    
  2. If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:

    /** @var array $collection */
    $collection = json_decode($data, true);
    
    foreach ($collection as $item) { /** .. */ }
    
  3. Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.

Loading history...
1867
			if ( $t1 == $f->term_id ) {
1868
				$this->assertSame( 3, $f->count );
1869
			} elseif ( $t2 == $f->term_id ) {
1870
				$this->assertSame( 2, $f->count );
1871
			} else {
1872
				$this->assertSame( 1, $f->count );
1873
			}
1874
		}
1875
	}
1876
1877
	/**
1878
	 * @ticket 20635
1879
	 */
1880
	public function test_pad_counts_should_not_recurse_infinitely_when_term_hierarchy_has_a_loop() {
1881
		remove_filter( 'wp_update_term_parent', 'wp_check_term_hierarchy_for_loops', 10 );
1882
1883
		$c1 = self::factory()->category->create();
1884
		$c2 = self::factory()->category->create( array( 'parent' => $c1 ) );
1885
		$c3 = self::factory()->category->create( array( 'parent' => $c2 ) );
1886
		wp_update_term( $c1, 'category', array( 'parent' => $c3 ) );
1887
1888
		add_filter( 'wp_update_term_parent', 'wp_check_term_hierarchy_for_loops', 10, 3 );
1889
1890
		$posts = self::factory()->post->create_many( 3 );
1891
		wp_set_post_terms( $posts[0], $c1, 'category' );
1892
		wp_set_post_terms( $posts[1], $c2, 'category' );
1893
		wp_set_post_terms( $posts[2], $c3, 'category' );
1894
1895
		$terms = get_terms( 'category', array(
1896
			'pad_counts' => true,
1897
		) );
1898
1899
		$this->assertEqualSets( array( $c1, $c2, $c3 ), wp_list_pluck( $terms, 'term_id' ) );
1900
1901
		foreach ( $terms as $term ) {
0 ignored issues
show
Bug introduced by
The expression $terms of type array|integer|object<WP_Error> is not guaranteed to be traversable. How about adding an additional type check?

There are different options of fixing this problem.

  1. If you want to be on the safe side, you can add an additional type-check:

    $collection = json_decode($data, true);
    if ( ! is_array($collection)) {
        throw new \RuntimeException('$collection must be an array.');
    }
    
    foreach ($collection as $item) { /** ... */ }
    
  2. If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:

    /** @var array $collection */
    $collection = json_decode($data, true);
    
    foreach ($collection as $item) { /** .. */ }
    
  3. Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.

Loading history...
1902
			$this->assertSame( 3, $term->count );
1903
		}
1904
	}
1905
1906
	/**
1907
	 * @ticket 31118
1908
	 */
1909
	public function test_pad_counts_should_work_when_first_taxonomy_is_nonhierarchical_and_second_taxonomy_is_hierarchical() {
1910
		register_taxonomy( 'wptests_tax1', 'post', array( 'hierarchical' => false ) );
1911
		register_taxonomy( 'wptests_tax2', 'post', array( 'hierarchical' => true ) );
1912
1913
		$t1 = self::factory()->term->create( array( 'taxonomy' => 'wptests_tax1' ) );
1914
		$t2 = self::factory()->term->create( array( 'taxonomy' => 'wptests_tax2' ) );
1915
		$t3 = self::factory()->term->create( array( 'taxonomy' => 'wptests_tax2', 'parent' => $t2 ) );
1916
1917
		$posts = self::factory()->post->create_many( 3 );
1918
		wp_set_object_terms( $posts[0], $t1, 'wptests_tax1' );
1919
		wp_set_object_terms( $posts[1], $t2, 'wptests_tax2' );
1920
		wp_set_object_terms( $posts[2], $t3, 'wptests_tax2' );
1921
1922
		$found = get_terms( array( 'wptests_tax1', 'wptests_tax2' ), array(
1923
			'pad_counts' => true,
1924
		) );
1925
1926
		$this->assertEqualSets( array( $t1, $t2, $t3 ), wp_list_pluck( $found, 'term_id' ) );
1927
1928
		foreach ( $found as $f ) {
0 ignored issues
show
Bug introduced by
The expression $found of type array|integer|object<WP_Error> is not guaranteed to be traversable. How about adding an additional type check?

There are different options of fixing this problem.

  1. If you want to be on the safe side, you can add an additional type-check:

    $collection = json_decode($data, true);
    if ( ! is_array($collection)) {
        throw new \RuntimeException('$collection must be an array.');
    }
    
    foreach ($collection as $item) { /** ... */ }
    
  2. If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:

    /** @var array $collection */
    $collection = json_decode($data, true);
    
    foreach ($collection as $item) { /** .. */ }
    
  3. Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.

Loading history...
1929
			if ( $t1 == $f->term_id ) {
1930
				$this->assertEquals( 1, $f->count );
1931
			} elseif ( $t2 == $f->term_id ) {
1932
				$this->assertEquals( 2, $f->count );
1933
			} else {
1934
				$this->assertEquals( 1, $f->count );
1935
			}
1936
		}
1937
	}
1938
1939
	/**
1940
	 * @ticket 10142
1941
	 */
1942
	public function test_termmeta_cache_should_be_primed_by_default() {
1943
		global $wpdb;
1944
1945
		register_taxonomy( 'wptests_tax', 'post' );
1946
		$terms = self::factory()->term->create_many( 3, array( 'taxonomy' => 'wptests_tax' ) );
1947
		add_term_meta( $terms[0], 'foo', 'bar' );
1948
		add_term_meta( $terms[1], 'foo', 'bar' );
1949
		add_term_meta( $terms[2], 'foo', 'bar' );
1950
1951
		$found = get_terms( 'wptests_tax', array(
0 ignored issues
show
Unused Code introduced by
$found is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
1952
			'hide_empty' => false,
1953
			'include' => $terms,
1954
		) );
1955
1956
		$num_queries = $wpdb->num_queries;
1957
1958
		foreach ( $terms as $t ) {
1959
			$this->assertSame( 'bar', get_term_meta( $t, 'foo', true ) );
1960
		}
1961
1962
		$this->assertSame( $num_queries, $wpdb->num_queries );
1963
	}
1964
1965
	/**
1966
	 * @ticket 10142
1967
	 */
1968
	public function test_termmeta_cache_should_not_be_primed_when_update_term_meta_cache_is_false() {
1969
		global $wpdb;
1970
1971
		register_taxonomy( 'wptests_tax', 'post' );
1972
		$terms = self::factory()->term->create_many( 3, array( 'taxonomy' => 'wptests_tax' ) );
1973
		add_term_meta( $terms[0], 'foo', 'bar' );
1974
		add_term_meta( $terms[1], 'foo', 'bar' );
1975
		add_term_meta( $terms[2], 'foo', 'bar' );
1976
1977
		$found = get_terms( 'wptests_tax', array(
0 ignored issues
show
Unused Code introduced by
$found is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
1978
			'hide_empty' => false,
1979
			'include' => $terms,
1980
			'update_term_meta_cache' => false,
1981
		) );
1982
1983
		$num_queries = $wpdb->num_queries;
1984
1985
		foreach ( $terms as $t ) {
1986
			$this->assertSame( 'bar', get_term_meta( $t, 'foo', true ) );
1987
		}
1988
1989
		$this->assertSame( $num_queries + 3, $wpdb->num_queries );
1990
	}
1991
1992
	/**
1993
	 * @ticket 10142
1994
	 */
1995
	public function test_meta_query() {
1996
		register_taxonomy( 'wptests_tax', 'post' );
1997
		$terms = self::factory()->term->create_many( 5, array( 'taxonomy' => 'wptests_tax' ) );
1998
		add_term_meta( $terms[0], 'foo', 'bar' );
1999
		add_term_meta( $terms[1], 'foo', 'bar' );
2000
		add_term_meta( $terms[2], 'foo', 'baz' );
2001
		add_term_meta( $terms[3], 'foob', 'ar' );
2002
2003
		$found = get_terms( 'wptests_tax', array(
2004
			'hide_empty' => false,
2005
			'meta_query' => array(
2006
				array(
2007
					'key' => 'foo',
2008
					'value' => 'bar',
2009
				),
2010
			),
2011
			'fields' => 'ids',
2012
		) );
2013
2014
		$this->assertEqualSets( array( $terms[0], $terms[1] ), $found );
2015
	}
2016
2017
	/**
2018
	 * @ticket 35137
2019
	 */
2020
	public function test_meta_query_should_not_return_duplicates() {
2021
		register_taxonomy( 'wptests_tax', 'post' );
2022
		$terms = self::factory()->term->create_many( 1, array( 'taxonomy' => 'wptests_tax' ) );
2023
		add_term_meta( $terms[0], 'foo', 'bar' );
2024
		add_term_meta( $terms[0], 'foo', 'ber' );
2025
2026
		$found = get_terms( 'wptests_tax', array(
2027
			'hide_empty' => false,
2028
			'meta_query' => array(
2029
				array(
2030
					'key' => 'foo',
2031
					'value' => 'bur',
2032
					'compare' => '!=',
2033
				),
2034
			),
2035
			'fields' => 'ids',
2036
		) );
2037
2038
		$this->assertEqualSets( array( $terms[0] ), $found );
2039
	}
2040
2041
	/**
2042
	 * @ticket 14162
2043
	 */
2044
	public function test_should_return_wp_term_objects() {
2045
		register_taxonomy( 'wptests_tax', 'post' );
2046
		$terms = self::factory()->term->create_many( 2, array( 'taxonomy' => 'wptests_tax' ) );
0 ignored issues
show
Unused Code introduced by
$terms is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
2047
2048
		$found = get_terms( 'wptests_tax', array(
2049
			'hide_empty' => false,
2050
			'fields' => 'all',
2051
		) );
2052
2053
		$this->assertNotEmpty( $found );
2054
2055
		foreach ( $found as $term ) {
0 ignored issues
show
Bug introduced by
The expression $found of type array|integer|object<WP_Error> is not guaranteed to be traversable. How about adding an additional type check?

There are different options of fixing this problem.

  1. If you want to be on the safe side, you can add an additional type-check:

    $collection = json_decode($data, true);
    if ( ! is_array($collection)) {
        throw new \RuntimeException('$collection must be an array.');
    }
    
    foreach ($collection as $item) { /** ... */ }
    
  2. If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:

    /** @var array $collection */
    $collection = json_decode($data, true);
    
    foreach ($collection as $item) { /** .. */ }
    
  3. Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.

Loading history...
2056
			$this->assertInstanceOf( 'WP_Term', $term );
2057
		}
2058
	}
2059
2060
	/**
2061
	 * @ticket 14162
2062
	 * @ticket 34282
2063
	 */
2064
	public function test_should_return_wp_term_objects_when_pulling_from_the_cache() {
2065
		global $wpdb;
2066
2067
		register_taxonomy( 'wptests_tax', 'post' );
2068
		$terms = self::factory()->term->create_many( 2, array( 'taxonomy' => 'wptests_tax' ) );
0 ignored issues
show
Unused Code introduced by
$terms is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
2069
2070
		// Prime the cache.
2071
		get_terms( 'wptests_tax', array(
2072
			'hide_empty' => false,
2073
			'fields' => 'all',
2074
		) );
2075
2076
		$num_queries = $wpdb->num_queries;
2077
2078
		$found = get_terms( 'wptests_tax', array(
2079
			'hide_empty' => false,
2080
			'fields' => 'all',
2081
		) );
2082
2083
		$this->assertSame( $num_queries, $wpdb->num_queries );
2084
2085
		$this->assertNotEmpty( $found );
2086
2087
		foreach ( $found as $term ) {
0 ignored issues
show
Bug introduced by
The expression $found of type array|integer|object<WP_Error> is not guaranteed to be traversable. How about adding an additional type check?

There are different options of fixing this problem.

  1. If you want to be on the safe side, you can add an additional type-check:

    $collection = json_decode($data, true);
    if ( ! is_array($collection)) {
        throw new \RuntimeException('$collection must be an array.');
    }
    
    foreach ($collection as $item) { /** ... */ }
    
  2. If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:

    /** @var array $collection */
    $collection = json_decode($data, true);
    
    foreach ($collection as $item) { /** .. */ }
    
  3. Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.

Loading history...
2088
			$this->assertInstanceOf( 'WP_Term', $term );
2089
		}
2090
	}
2091
2092
	/**
2093
	 * @ticket 14162
2094
	 */
2095
	public function test_should_prime_individual_term_cache_when_fields_is_all() {
2096
		global $wpdb;
2097
2098
		register_taxonomy( 'wptests_tax', 'post' );
2099
		$terms = self::factory()->term->create_many( 2, array( 'taxonomy' => 'wptests_tax' ) );
2100
2101
		$found = get_terms( 'wptests_tax', array(
0 ignored issues
show
Unused Code introduced by
$found is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
2102
			'hide_empty' => false,
2103
			'fields' => 'all',
2104
		) );
2105
2106
		$num_queries = $wpdb->num_queries;
2107
		$term0 = get_term( $terms[0] );
0 ignored issues
show
Unused Code introduced by
$term0 is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
2108
		$this->assertSame( $num_queries, $wpdb->num_queries );
2109
2110
	}
2111
2112
	/**
2113
	 * @ticket 35382
2114
	 */
2115
	public function test_indexes_should_not_be_reset_when_number_of_matched_terms_is_greater_than_number() {
2116
		register_taxonomy( 'wptests_tax', 'post', array( 'hierarchical' => true ) );
2117
		$terms = self::factory()->term->create_many( 3, array( 'taxonomy' => 'wptests_tax' ) );
2118
2119
		$found = get_terms( 'wptests_tax', array(
2120
			'hide_empty' => false,
2121
			'fields' => 'id=>parent',
2122
			'number' => 2,
2123
			'orderby' => 'id',
2124
			'order' => 'ASC',
2125
			'hierarchical' => true,
2126
		) );
2127
2128
		$this->assertSame( array( $terms[0], $terms[1] ), array_keys( $found ) );
2129
	}
2130
2131
	/**
2132
	 * @ticket 35935
2133
	 */
2134
	public function test_hierarchical_offset_0_with_number_greater_than_total_available_count() {
2135
		register_taxonomy( 'wptests_tax', 'post', array( 'hierarchical' => true ) );
2136
2137
		$terms = self::factory()->term->create_many( 2, array( 'taxonomy' => 'wptests_tax' ) );
2138
2139
		$found = get_terms( 'wptests_tax', array(
2140
			'number'     => 3,
2141
			'offset'     => 0,
2142
			'hide_empty' => false,
2143
			'fields'     => 'ids',
2144
		) );
2145
		$this->assertEqualSets( $terms, $found );
2146
	}
2147
2148
	/**
2149
	 * @ticket 35935
2150
	 */
2151
	public function test_hierarchical_offset_plus_number() {
2152
		register_taxonomy( 'wptests_tax', 'post', array( 'hierarchical' => true ) );
2153
2154
		$terms = self::factory()->term->create_many( 3, array( 'taxonomy' => 'wptests_tax' ) );
2155
2156
		$found = get_terms( 'wptests_tax', array(
2157
			'number'     => 1,
2158
			'offset'     => 1,
2159
			'hide_empty' => false,
2160
			'fields'     => 'ids',
2161
			'orderby'    => 'term_id',
2162
			'order'      => 'ASC',
2163
		) );
2164
		$this->assertEqualSets( array( $terms[1] ), $found );
2165
	}
2166
2167
	/**
2168
	 * @ticket 35935
2169
	 */
2170
	public function test_hierarchical_offset_plus_number_exceeds_available_count() {
2171
		register_taxonomy( 'wptests_tax', 'post', array( 'hierarchical' => true ) );
2172
2173
		$terms = self::factory()->term->create_many( 2, array( 'taxonomy' => 'wptests_tax' ) );
2174
2175
		$found = get_terms( 'wptests_tax', array(
2176
			'number'     => 2,
2177
			'offset'     => 1,
2178
			'hide_empty' => false,
2179
			'fields'     => 'ids',
2180
			'orderby'    => 'term_id',
2181
			'order'      => 'ASC',
2182
		) );
2183
		$this->assertEqualSets( array( $terms[1] ), $found );
2184
	}
2185
2186
	/**
2187
	 * @ticket 35935
2188
	 */
2189
	public function test_hierarchical_offset_exceeds_available_count() {
2190
		register_taxonomy( 'wptests_tax', 'post', array( 'hierarchical' => true ) );
2191
2192
		$terms = self::factory()->term->create_many( 2, array( 'taxonomy' => 'wptests_tax' ) );
0 ignored issues
show
Unused Code introduced by
$terms is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
2193
2194
		$found = get_terms( 'wptests_tax', array(
2195
			'number'     => 100,
2196
			'offset'     => 3,
2197
			'hide_empty' => false,
2198
			'fields'     => 'ids',
2199
			'orderby'    => 'term_id',
2200
			'order'      => 'ASC',
2201
		) );
2202
		$this->assertEqualSets( array(), $found );
2203
	}
2204
2205
	/**
2206
	 * @ticket 36992
2207
	 * @ticket 35381
2208
	 */
2209
	public function test_count_should_not_pass_through_main_get_terms_filter() {
2210
		add_filter( 'get_terms', array( __CLASS__, 'maybe_filter_count' ) );
2211
2212
		$found = get_terms( array(
2213
			'hide_empty' => 0,
2214
			'fields' => 'count',
2215
		) );
2216
2217
		remove_filter( 'get_terms', array( __CLASS__, 'maybe_filter_count' ) );
2218
2219
		$this->assertNotEquals( 'foo', $found );
2220
	}
2221
2222
	public static function maybe_filter_count() {
2223
		return 'foo';
2224
	}
2225
2226
	/**
2227
	 * @ticket 21760
2228
	 */
2229
	public function test_with_term_slug_equal_to_string_0() {
2230
		register_taxonomy( 'wptests_tax', 'post', array( 'hierarchical' => true ) );
2231
2232
		$term_id = self::factory()->term->create( array(
2233
			'name' => '0',
2234
			'slug' => '0',
2235
			'taxonomy' => 'wptests_tax',
2236
		) );
2237
2238
		$found = get_terms( array(
2239
			'taxonomy'   => 'wptests_tax',
2240
			'hide_empty' => 0,
2241
			'slug'       => '0',
2242
		) );
2243
2244
		$this->assertEqualSets( array( $term_id ), wp_list_pluck( $found, 'term_id' ) );
2245
	}
2246
2247
	/**
2248
	 * @ticket 21760
2249
	 */
2250
	public function test_with_multiple_term_slugs_one_equal_to_string_0() {
2251
		register_taxonomy( 'wptests_tax', 'post', array( 'hierarchical' => true ) );
2252
2253
		$term_id1 = self::factory()->term->create( array(
2254
			'name'     => '0',
2255
			'slug'     => '0',
2256
			'taxonomy' => 'wptests_tax',
2257
		) );
2258
2259
		$term_id2 = self::factory()->term->create( array(
2260
			'name'     => 'Test',
2261
			'slug'     => 'test',
2262
			'taxonomy' => 'wptests_tax',
2263
		) );
2264
2265
		$found = get_terms( array(
2266
			'taxonomy'   => 'wptests_tax',
2267
			'hide_empty' => 0,
2268
			'slug'       => array(
2269
				'0',
2270
				'test',
2271
			),
2272
		) );
2273
2274
		$this->assertEqualSets( array( $term_id1, $term_id2 ), wp_list_pluck( $found, 'term_id' ) );
2275
	}
2276
2277
	protected function create_hierarchical_terms_and_posts() {
2278
		$terms = array();
2279
2280
		$terms['parent1'] = self::factory()->term->create( array( 'slug' => 'parent-1', 'name' => 'Parent 1', 'taxonomy' => 'hierarchical_fields' ) );
2281
		$terms['parent2'] = self::factory()->term->create( array( 'slug' => 'parent-2', 'name' => 'Parent 2', 'taxonomy' => 'hierarchical_fields' ) );
2282
		$terms['child1'] = self::factory()->term->create( array( 'slug' => 'child-1', 'name' => 'Child 1', 'taxonomy' => 'hierarchical_fields', 'parent' => $terms['parent1'] ) );
2283
		$terms['child2'] = self::factory()->term->create( array( 'slug' => 'child-2', 'name' => 'Child 2', 'taxonomy' => 'hierarchical_fields', 'parent' => $terms['parent1'] ) );
2284
		$terms['grandchild1'] = self::factory()->term->create( array( 'slug' => 'grandchild-1', 'name' => 'Grandchild 1', 'taxonomy' => 'hierarchical_fields', 'parent' => $terms['child1'] ) );
2285
2286
		$post_id = self::factory()->post->create();
2287
		wp_set_post_terms( $post_id, $terms['parent2'], 'hierarchical_fields', true );
2288
		wp_set_post_terms( $post_id, $terms['child1'], 'hierarchical_fields', true );
2289
2290
		return $terms;
2291
	}
2292
2293
	protected function create_hierarchical_terms() {
2294
		// Set up the following hierarchy:
2295
		// - One
2296
		//   - Two
2297
		//     - Three (1)
2298
		//     - Four
2299
		//   - Five
2300
		//     - Six (1)
2301
		//   - Seven
2302
		$one_term = wp_insert_term(
2303
			'One',
2304
			'category'
2305
		);
2306
		$two_term = wp_insert_term(
2307
			'Two',
2308
			'category',
2309
			array(
2310
				'parent' => $one_term['term_id']
2311
			)
2312
		);
2313
		$three_term = wp_insert_term(
2314
			'Three',
2315
			'category',
2316
			array(
2317
				'parent' => $two_term['term_id']
2318
			)
2319
		);
2320
		$four_term = wp_insert_term(
2321
			'Four',
2322
			'category',
2323
			array(
2324
				'parent' => $two_term['term_id']
2325
			)
2326
		);
2327
		$five_term = wp_insert_term(
2328
			'Five',
2329
			'category',
2330
			array(
2331
				'parent' => $one_term['term_id']
2332
			)
2333
		);
2334
		$six_term = wp_insert_term(
2335
			'Six',
2336
			'category',
2337
			array(
2338
				'parent' => $five_term['term_id']
2339
			)
2340
		);
2341
		$seven_term = wp_insert_term(
2342
			'Seven',
2343
			'category',
2344
			array(
2345
				'parent' => $one_term['term_id']
2346
			)
2347
		);
2348
2349
		// Ensure child terms are not empty
2350
		$first_post_id = self::factory()->post->create();
2351
		$second_post_id = self::factory()->post->create();
2352
		wp_set_post_terms( $first_post_id, array( $three_term['term_id'] ), 'category' );
2353
		wp_set_post_terms( $second_post_id, array( $six_term['term_id'] ), 'category' );
2354
2355
		return array(
2356
			'one_term' => $one_term,
2357
			'two_term' => $two_term,
2358
			'three_term' => $three_term,
2359
			'four_term' => $four_term,
2360
			'five_term' => $five_term,
2361
			'six_term' => $six_term,
2362
			'seven_term' => $seven_term
2363
		);
2364
	}
2365
2366
	protected function set_up_three_posts_and_tags() {
2367
		$posts = self::factory()->post->create_many( 3, array( 'post_type' => 'post' ) );
2368
		foreach ( $posts as $i => $post ) {
2369
			wp_set_object_terms( $post, "term_{$i}", 'post_tag' );
2370
		}
2371
2372
		wp_cache_delete( 'last_changed', 'terms' );
2373
	}
2374
}
2375