forked from WordPress/wordpress-develop
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgetTermBy.php
More file actions
285 lines (237 loc) · 7.36 KB
/
getTermBy.php
File metadata and controls
285 lines (237 loc) · 7.36 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
<?php
/**
* @group taxonomy
*/
class Tests_Term_GetTermBy extends WP_UnitTestCase {
function test_get_term_by_slug() {
$term1 = wp_insert_term( 'Foo', 'category', array( 'slug' => 'foo' ) );
$term2 = get_term_by( 'slug', 'foo', 'category' );
$this->assertEquals( get_term( $term1['term_id'], 'category' ), $term2 );
}
function test_get_term_by_name() {
$term1 = wp_insert_term( 'Foo', 'category', array( 'slug' => 'foo' ) );
$term2 = get_term_by( 'name', 'Foo', 'category' );
$this->assertEquals( get_term( $term1['term_id'], 'category' ), $term2 );
}
function test_get_term_by_id() {
$term1 = wp_insert_term( 'Foo', 'category', array( 'slug' => 'foo' ) );
$term2 = get_term_by( 'id', $term1['term_id'], 'category' );
$this->assertEquals( get_term( $term1['term_id'], 'category' ), $term2 );
}
/**
* 'term_id' is an alias of 'id'.
*/
function test_get_term_by_term_id() {
$term1 = wp_insert_term( 'Foo', 'category', array( 'slug' => 'foo' ) );
$term2 = get_term_by( 'term_id', $term1['term_id'], 'category' );
$this->assertEquals( get_term( $term1['term_id'], 'category' ), $term2 );
}
/**
* @ticket 45163
*/
function test_get_term_by_uppercase_id() {
$term1 = wp_insert_term( 'Foo', 'category', array( 'slug' => 'foo' ) );
$term2 = get_term_by( 'ID', $term1['term_id'], 'category' );
$this->assertEquals( get_term( $term1['term_id'], 'category' ), $term2 );
}
/**
* @ticket 21651
*/
function test_get_term_by_tt_id() {
$term1 = wp_insert_term( 'Foo', 'category' );
$term2 = get_term_by( 'term_taxonomy_id', $term1['term_taxonomy_id'], 'category' );
$this->assertEquals( get_term( $term1['term_id'], 'category' ), $term2 );
}
function test_get_term_by_unknown() {
wp_insert_term( 'Foo', 'category', array( 'slug' => 'foo' ) );
$term2 = get_term_by( 'unknown', 'foo', 'category' );
$this->assertFalse( $term2 );
}
/**
* @ticket 33281
*/
function test_get_term_by_with_nonexistent_id_should_return_false() {
$term = get_term_by( 'id', 123456, 'category' );
$this->assertFalse( $term );
}
/**
* @ticket 16282
*/
public function test_get_term_by_slug_should_match_nonaccented_equivalents() {
register_taxonomy( 'wptests_tax', 'post' );
$slug = 'ńaș';
$t = self::factory()->term->create(
array(
'slug' => $slug,
'taxonomy' => 'wptests_tax',
)
);
$found = get_term_by( 'slug', 'nas', 'wptests_tax' );
$this->assertSame( $t, $found->term_id );
}
/**
* @ticket 30620
*/
public function test_taxonomy_should_be_ignored_if_matching_by_term_taxonomy_id() {
global $wpdb;
register_taxonomy( 'wptests_tax', 'post' );
$t = self::factory()->term->create( array( 'taxonomy' => 'wptests_tax' ) );
$term = get_term( $t, 'wptests_tax' );
$new_ttid = $term->term_taxonomy_id + 1;
// Offset just to be sure.
$wpdb->update(
$wpdb->term_taxonomy,
array( 'term_taxonomy_id' => $new_ttid ),
array( 'term_id' => $t )
);
$found = get_term_by( 'term_taxonomy_id', $new_ttid, 'foo' );
$this->assertSame( $t, $found->term_id );
}
/**
* @ticket 14162
*/
public function test_should_prime_term_cache() {
global $wpdb;
register_taxonomy( 'wptests_tax', 'post' );
$t = self::factory()->term->create(
array(
'taxonomy' => 'wptests_tax',
'slug' => 'foo',
)
);
clean_term_cache( $t, 'wptests_tax' );
$num_queries = $wpdb->num_queries;
$found = get_term_by( 'slug', 'foo', 'wptests_tax' );
$num_queries++;
$this->assertInstanceOf( 'WP_Term', $found );
$this->assertSame( $t, $found->term_id );
$this->assertSame( $num_queries, $wpdb->num_queries );
// Calls to `get_term()` should now hit cache.
$found2 = get_term( $t );
$this->assertSame( $t, $found->term_id );
$this->assertSame( $num_queries, $wpdb->num_queries );
}
/**
* @ticket 21760
*/
public function test_should_unslash_name() {
register_taxonomy( 'wptests_tax', 'post' );
$term_name = 'Foo " \o/';
$term_name_slashed = wp_slash( $term_name );
$t = self::factory()->term->create(
array(
'taxonomy' => 'wptests_tax',
'name' => $term_name_slashed,
)
);
$found = get_term_by( 'name', $term_name_slashed, 'wptests_tax' );
$this->assertInstanceOf( 'WP_Term', $found );
$this->assertSame( $t, $found->term_id );
$this->assertSame( $term_name, $found->name );
}
/**
* @ticket 21760
*/
public function test_should_sanitize_slug() {
register_taxonomy( 'wptests_tax', 'post' );
$t1 = self::factory()->term->create(
array(
'taxonomy' => 'wptests_tax',
'slug' => 'foo-foo',
)
);
// Whitespace should get replaced by a '-'.
$found1 = get_term_by( 'slug', 'foo foo', 'wptests_tax' );
$this->assertInstanceOf( 'WP_Term', $found1 );
$this->assertSame( $t1, $found1->term_id );
$t2 = self::factory()->term->create(
array(
'taxonomy' => 'wptests_tax',
'slug' => '%e4%bb%aa%e8%a1%a8%e7%9b%98',
)
);
// Slug should get urlencoded.
$found2 = get_term_by( 'slug', '仪表盘', 'wptests_tax' );
$this->assertInstanceOf( 'WP_Term', $found2 );
$this->assertSame( $t2, $found2->term_id );
}
/**
* @ticket 21760
*/
public function test_query_should_not_contain_order_by_clause() {
global $wpdb;
$term_id = $this->factory->term->create(
array(
'name' => 'burrito',
'taxonomy' => 'post_tag',
)
);
$found = get_term_by( 'name', 'burrito', 'post_tag' );
$this->assertSame( $term_id, $found->term_id );
$this->assertStringNotContainsString( 'ORDER BY', $wpdb->last_query );
}
/**
* @ticket 21760
*/
public function test_query_should_contain_limit_clause() {
global $wpdb;
$term_id = $this->factory->term->create(
array(
'name' => 'burrito',
'taxonomy' => 'post_tag',
)
);
$found = get_term_by( 'name', 'burrito', 'post_tag' );
$this->assertSame( $term_id, $found->term_id );
$this->assertStringContainsString( 'LIMIT 1', $wpdb->last_query );
}
/**
* @ticket 21760
*/
public function test_prevent_recursion_by_get_terms_filter() {
$action = new MockAction();
add_filter( 'get_terms', array( $action, 'filter' ) );
get_term_by( 'name', 'burrito', 'post_tag' );
remove_filter( 'get_terms', array( $action, 'filter' ) );
$this->assertSame( 0, $action->get_call_count() );
}
/**
* @ticket 21760
*/
public function test_get_term_by_name_with_string_0() {
register_taxonomy( 'wptests_tax', 'post', array( 'hierarchical' => true ) );
$term_id = $this->factory->term->create(
array(
'name' => '0',
'taxonomy' => 'wptests_tax',
)
);
$found = get_term_by( 'name', '0', 'wptests_tax' );
$this->assertSame( $term_id, $found->term_id );
}
/**
* @ticket 21760
*/
public function test_get_term_by_slug_with_string_0() {
register_taxonomy( 'wptests_tax', 'post', array( 'hierarchical' => true ) );
$term_id = $this->factory->term->create(
array(
'taxonomy' => 'wptests_tax',
'name' => '0',
'slug' => '0',
)
);
$found = get_term_by( 'slug', '0', 'wptests_tax' );
$this->assertSame( $term_id, $found->term_id );
}
/**
* @ticket 21760
*/
public function test_get_term_by_with_empty_string() {
register_taxonomy( 'wptests_tax', 'post', array( 'hierarchical' => true ) );
$found_by_slug = get_term_by( 'slug', '', 'wptests_tax' );
$found_by_name = get_term_by( 'name', '', 'wptests_tax' );
$this->assertFalse( $found_by_slug );
$this->assertFalse( $found_by_name );
}
}