forked from WordPress/wordpress-develop
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathterm.php
More file actions
379 lines (312 loc) · 12.4 KB
/
term.php
File metadata and controls
379 lines (312 loc) · 12.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
<?php
/**
* @group taxonomy
* @group category
*/
class Tests_Term extends WP_UnitTestCase {
protected $taxonomy = 'category';
protected static $post_ids = array();
public static function wpSetUpBeforeClass( WP_UnitTest_Factory $factory ) {
self::$post_ids = $factory->post->create_many( 5 );
}
/**
* @ticket 29911
*/
public function test_wp_delete_term_should_invalidate_cache_for_child_terms() {
register_taxonomy(
'wptests_tax',
'post',
array(
'hierarchical' => true,
)
);
$parent = self::factory()->term->create(
array(
'taxonomy' => 'wptests_tax',
)
);
$child = self::factory()->term->create(
array(
'taxonomy' => 'wptests_tax',
'parent' => $parent,
'slug' => 'foo',
)
);
// Prime the cache.
$child_term = get_term( $child, 'wptests_tax' );
$this->assertSame( $parent, $child_term->parent );
wp_delete_term( $parent, 'wptests_tax' );
$child_term = get_term( $child, 'wptests_tax' );
$this->assertSame( 0, $child_term->parent );
}
/**
* @ticket 5381
*/
public function test_is_term_type() {
// Insert a term.
$term = 'term_new';
$t = wp_insert_term( $term, $this->taxonomy );
$this->assertIsArray( $t );
$term_obj = get_term_by( 'name', $term, $this->taxonomy );
$exists = term_exists( $term_obj->slug );
// Clean up.
$deleted = wp_delete_term( $t['term_id'], $this->taxonomy );
$this->assertEquals( $t['term_id'], $exists );
$this->assertTrue( $deleted );
}
/**
* @ticket 15919
*/
public function test_wp_count_terms() {
$count = wp_count_terms(
array(
'hide_empty' => true,
'taxonomy' => 'category',
)
);
// There are 5 posts, all Uncategorized.
$this->assertSame( '1', $count );
}
/**
* @ticket 36399
*/
public function test_wp_count_terms_legacy_interoperability() {
self::factory()->tag->create_many( 5 );
// Counts all terms (1 default category, 5 tags).
$count = wp_count_terms();
$this->assertSame( '6', $count );
// Counts only tags (5), with both current and legacy signature.
// Legacy usage should not trigger deprecated notice.
$count = wp_count_terms( array( 'taxonomy' => 'post_tag' ) );
$legacy_count = wp_count_terms( 'post_tag' );
$this->assertSame( '5', $count );
$this->assertSame( $count, $legacy_count );
}
/**
* @ticket 15475
*/
public function test_wp_add_remove_object_terms() {
$posts = self::$post_ids;
$tags = self::factory()->tag->create_many( 5 );
$tt = wp_add_object_terms( $posts[0], $tags[1], 'post_tag' );
$this->assertCount( 1, $tt );
$this->assertSame( array( $tags[1] ), wp_get_object_terms( $posts[0], 'post_tag', array( 'fields' => 'ids' ) ) );
$three_tags = array( $tags[0], $tags[1], $tags[2] );
$tt = wp_add_object_terms( $posts[1], $three_tags, 'post_tag' );
$this->assertCount( 3, $tt );
$this->assertSame( $three_tags, wp_get_object_terms( $posts[1], 'post_tag', array( 'fields' => 'ids' ) ) );
$this->assertTrue( wp_remove_object_terms( $posts[0], $tags[1], 'post_tag' ) );
$this->assertFalse( wp_remove_object_terms( $posts[0], $tags[0], 'post_tag' ) );
$this->assertInstanceOf( 'WP_Error', wp_remove_object_terms( $posts[0], $tags[1], 'non_existing_taxonomy' ) );
$this->assertTrue( wp_remove_object_terms( $posts[1], $three_tags, 'post_tag' ) );
$this->assertCount( 0, wp_get_object_terms( $posts[1], 'post_tag' ) );
foreach ( $tags as $term_id ) {
$this->assertTrue( wp_delete_term( $term_id, 'post_tag' ) );
}
foreach ( $posts as $post_id ) {
$this->assertInstanceOf( 'WP_Post', wp_delete_post( $post_id ) );
}
}
public function test_term_is_ancestor_of() {
$term = rand_str();
$term2 = rand_str();
$t = wp_insert_term( $term, 'category' );
$this->assertIsArray( $t );
$t2 = wp_insert_term( $term, 'category', array( 'parent' => $t['term_id'] ) );
$this->assertIsArray( $t2 );
$this->assertTrue( term_is_ancestor_of( $t['term_id'], $t2['term_id'], 'category' ) );
$this->assertFalse( term_is_ancestor_of( $t2['term_id'], $t['term_id'], 'category' ) );
$this->assertTrue( cat_is_ancestor_of( $t['term_id'], $t2['term_id'] ) );
$this->assertFalse( cat_is_ancestor_of( $t2['term_id'], $t['term_id'] ) );
wp_delete_term( $t['term_id'], 'category' );
wp_delete_term( $t2['term_id'], 'category' );
}
public function test_wp_insert_delete_category() {
$term = rand_str();
$this->assertNull( category_exists( $term ) );
$initial_count = wp_count_terms( array( 'taxonomy' => 'category' ) );
$t = wp_insert_category( array( 'cat_name' => $term ) );
$this->assertIsNumeric( $t );
$this->assertNotWPError( $t );
$this->assertGreaterThan( 0, $t );
$this->assertSame( (string) ( $initial_count + 1 ), wp_count_terms( array( 'taxonomy' => 'category' ) ) );
// Make sure the term exists.
$this->assertGreaterThan( 0, term_exists( $term ) );
$this->assertGreaterThan( 0, term_exists( $t ) );
// Now delete it.
$this->assertTrue( wp_delete_category( $t ) );
$this->assertNull( term_exists( $term ) );
$this->assertNull( term_exists( $t ) );
$this->assertSame( $initial_count, wp_count_terms( array( 'taxonomy' => 'category' ) ) );
}
/**
* @ticket 16550
*/
public function test_wp_set_post_categories() {
$post_id = self::$post_ids[0];
$post = get_post( $post_id );
$this->assertIsArray( $post->post_category );
$this->assertCount( 1, $post->post_category );
$this->assertEquals( get_option( 'default_category' ), $post->post_category[0] );
$term1 = wp_insert_term( 'Foo', 'category' );
$term2 = wp_insert_term( 'Bar', 'category' );
$term3 = wp_insert_term( 'Baz', 'category' );
wp_set_post_categories( $post_id, array( $term1['term_id'], $term2['term_id'] ) );
$this->assertCount( 2, $post->post_category );
$this->assertSame( array( $term2['term_id'], $term1['term_id'] ), $post->post_category );
wp_set_post_categories( $post_id, $term3['term_id'], true );
$this->assertSame( array( $term2['term_id'], $term3['term_id'], $term1['term_id'] ), $post->post_category );
$term4 = wp_insert_term( 'Burrito', 'category' );
wp_set_post_categories( $post_id, $term4['term_id'] );
$this->assertSame( array( $term4['term_id'] ), $post->post_category );
wp_set_post_categories( $post_id, array( $term1['term_id'], $term2['term_id'] ), true );
$this->assertSame( array( $term2['term_id'], $term4['term_id'], $term1['term_id'] ), $post->post_category );
wp_set_post_categories( $post_id, array(), true );
$this->assertCount( 1, $post->post_category );
$this->assertEquals( get_option( 'default_category' ), $post->post_category[0] );
wp_set_post_categories( $post_id, array() );
$this->assertCount( 1, $post->post_category );
$this->assertEquals( get_option( 'default_category' ), $post->post_category[0] );
}
/**
* @ticket 43516
*/
public function test_wp_set_post_categories_sets_default_category_for_custom_post_types() {
add_filter( 'default_category_post_types', array( $this, 'filter_default_category_post_types' ) );
register_post_type( 'cpt', array( 'taxonomies' => array( 'category' ) ) );
$post_id = self::factory()->post->create( array( 'post_type' => 'cpt' ) );
$post = get_post( $post_id );
$this->assertEquals( get_option( 'default_category' ), $post->post_category[0] );
$term = wp_insert_term( 'Foo', 'category' );
wp_set_post_categories( $post_id, $term['term_id'] );
$this->assertSame( $term['term_id'], $post->post_category[0] );
wp_set_post_categories( $post_id, array() );
$this->assertEquals( get_option( 'default_category' ), $post->post_category[0] );
remove_filter( 'default_category_post_types', array( $this, 'filter_default_category_post_types' ) );
}
public function filter_default_category_post_types( $post_types ) {
$post_types[] = 'cpt';
return $post_types;
}
/**
* @ticket 25852
*/
public function test_sanitize_term_field() {
$term = wp_insert_term( 'foo', $this->taxonomy );
$this->assertSame( 0, sanitize_term_field( 'parent', 0, $term['term_id'], $this->taxonomy, 'raw' ) );
$this->assertSame( 1, sanitize_term_field( 'parent', 1, $term['term_id'], $this->taxonomy, 'raw' ) );
$this->assertSame( 0, sanitize_term_field( 'parent', -1, $term['term_id'], $this->taxonomy, 'raw' ) );
$this->assertSame( 0, sanitize_term_field( 'parent', '', $term['term_id'], $this->taxonomy, 'raw' ) );
}
/**
* @ticket 53152
* @dataProvider data_wp_set_term_objects_finds_term_name_with_special_characters
*
* @param string $name A term name containing special characters.
*/
public function test_wp_set_term_objects_finds_term_name_with_special_characters( $name ) {
$post_id = self::$post_ids[0];
$expected = wp_set_object_terms( $post_id, $name, 'category', false );
$actual = wp_set_object_terms( $post_id, $name, 'category', false );
$this->assertEquals( $expected, $actual );
}
/**
* Data provider.
*
* @return array
*/
public function data_wp_set_term_objects_finds_term_name_with_special_characters() {
return array(
'ampersand' => array( 'name' => 'Foo & Bar' ),
'ndash and mdash' => array( 'name' => 'Foo – Bar' ),
'trademark' => array( 'name' => 'Foo Bar™' ),
'copyright' => array( 'name' => 'Foo Bar©' ),
'registered' => array( 'name' => 'Foo Bar®' ),
'degree' => array( 'name' => 'Foo ° Bar' ),
'forward slash' => array( 'name' => 'Fo/o Ba/r' ),
'back slash' => array( 'name' => 'F\oo \Bar' ),
'multiply' => array( 'name' => 'Foo × Bar' ),
'standalone diacritic' => array( 'name' => 'Foo Bāáǎàr' ),
'acute accents' => array( 'name' => 'ááa´aˊ' ),
'iexcel and iquest' => array( 'name' => '¡Foo ¿Bar' ),
'angle quotes' => array( 'name' => '‹Foo« »Bar›' ),
'curly quotes' => array( 'name' => '“F‘o„o‚ „ ‟ ‛B“a’r”' ),
'bullet' => array( 'name' => 'Foo • Bar' ),
'unencoded percent' => array( 'name' => 'Foo % Bar' ),
'encoded ampersand' => array( 'name' => 'Foo & Bar' ),
'encoded ndash and mdash' => array( 'name' => 'Foo — – Bar' ),
'encoded trademark' => array( 'name' => 'Foo Bar ™' ),
'encoded copyright' => array( 'name' => 'Foo Bar ©' ),
'encoded registered' => array( 'name' => 'Foo Bar ®' ),
'encoded bullet' => array( 'name' => 'Foo • Bar' ),
);
}
/**
* @ticket 19205
*/
public function test_orphan_category() {
$cat_id1 = self::factory()->category->create();
wp_delete_category( $cat_id1 );
$cat_id2 = self::factory()->category->create( array( 'parent' => $cat_id1 ) );
$this->assertWPError( $cat_id2 );
}
/**
* @ticket 58329
*
* @covers ::get_term
*
*/
public function test_get_term_sanitize_once() {
$cat_id1 = self::factory()->category->create();
$_term = get_term( $cat_id1, '', OBJECT, 'edit' );
$filter = new MockAction();
add_filter( 'edit_term_slug', array( $filter, 'filter' ) );
$term = get_term( $_term, '', OBJECT, 'edit' );
$this->assertSame( 0, $filter->get_call_count(), 'The term was filtered more than once' );
$this->assertSame( $_term, $term, 'Both terms should match' );
}
/**
* @ticket 58329
*
* @covers ::get_term
*
* @dataProvider data_get_term_filter
*
* @param string $filter How to sanitize term fields.
*/
public function test_get_term_should_set_term_filter_property_to_filter_argument( $filter ) {
$cat_id1 = self::factory()->category->create();
$term = get_term( $cat_id1, '', OBJECT, $filter );
$this->assertSame( $filter, $term->filter, "The term's 'filter' property should be set to '$filter'." );
}
/**
* @ticket 58329
*
* @covers ::get_term
*
* @dataProvider data_get_term_filter
*
* @param string $filter How to sanitize term fields.
*/
public function test_get_term_filtered( $filter ) {
$cat_id1 = self::factory()->category->create();
$cat = self::factory()->category->create_and_get();
add_filter(
'get_term',
static function () use ( $cat ) {
return $cat;
}
);
$term = get_term( $cat_id1, '', OBJECT, $filter );
$this->assertSame( $filter, $term->filter, "The term's 'filter' property should be set to '$filter'." );
$this->assertSame( $term, $cat, 'The returned term should match the filtered term' );
}
/**
* Data provider.
*
* @return array[]
*/
public function data_get_term_filter() {
return self::text_array_to_dataprovider( array( 'edit', 'db', 'display', 'attribute', 'js', 'rss', 'raw' ) );
}
}