forked from WordPress/wordpress-develop
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcache.php
More file actions
222 lines (170 loc) · 6.06 KB
/
cache.php
File metadata and controls
222 lines (170 loc) · 6.06 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
<?php
/**
* @group taxonomy
*/
class Tests_Term_Cache extends WP_UnitTestCase {
function setUp() {
parent::setUp();
wp_cache_delete( 'last_changed', 'terms' );
}
/**
* @ticket 25711
*/
function test_category_children_cache() {
// Test with only one Parent => Child
$term_id1 = self::factory()->category->create();
$term_id1_child = self::factory()->category->create( array( 'parent' => $term_id1 ) );
$hierarchy = _get_term_hierarchy( 'category' );
$this->assertEquals( array( $term_id1 => array( $term_id1_child ) ), $hierarchy );
// Add another Parent => Child
$term_id2 = self::factory()->category->create();
$term_id2_child = self::factory()->category->create( array( 'parent' => $term_id2 ) );
$hierarchy = _get_term_hierarchy( 'category' );
$this->assertEquals( array( $term_id1 => array( $term_id1_child ), $term_id2 => array( $term_id2_child ) ), $hierarchy );
}
/**
* @ticket 22526
*/
function test_category_name_change() {
$term = self::factory()->category->create_and_get( array( 'name' => 'Foo' ) );
$post_id = self::factory()->post->create();
wp_set_post_categories( $post_id, $term->term_id );
$post = get_post( $post_id );
$cats1 = get_the_category( $post->ID );
$this->assertEquals( $term->name, reset( $cats1 )->name );
wp_update_term( $term->term_id, 'category', array( 'name' => 'Bar' ) );
$cats2 = get_the_category( $post->ID );
$this->assertNotEquals( $term->name, reset( $cats2 )->name );
}
/**
* @ticket 14485
*/
function test_hierachy_invalidation() {
$tax = 'burrito';
register_taxonomy( $tax, 'post', array( 'hierarchical' => true ) );
$this->assertTrue( get_taxonomy( $tax )->hierarchical );
$step = 1;
$parent_id = 0;
$children = 0;
foreach ( range( 1, 9 ) as $i ) {
switch ( $step ) {
case 1:
$parent = wp_insert_term( 'Parent' . $i, $tax );
$parent_id = $parent['term_id'];
break;
case 2:
$parent = wp_insert_term( 'Child' . $i, $tax, array( 'parent' => $parent_id ) );
$parent_id = $parent['term_id'];
$children++;
break;
case 3:
wp_insert_term( 'Grandchild' . $i, $tax, array( 'parent' => $parent_id ) );
$parent_id = 0;
$children++;
break;
}
$terms = get_terms( $tax, array( 'hide_empty' => false ) );
$this->assertEquals( $i, count( $terms ) );
if ( $i > 1 ) {
$hierarchy = _get_term_hierarchy( $tax );
$this->assertNotEmpty( $hierarchy );
$this->assertEquals( $children, count( $hierarchy, COUNT_RECURSIVE ) - count( $hierarchy ) );
}
if ( $i % 3 === 0 ) {
$step = 1;
} else {
$step++;
}
}
_unregister_taxonomy( $tax );
}
public function test_get_term_should_update_term_cache_when_passed_an_object() {
global $wpdb;
register_taxonomy( 'wptests_tax', 'post' );
$term = self::factory()->term->create( array(
'taxonomy' => 'wptests_tax',
) );
$term_object = get_term( $term, 'wptests_tax' );
wp_cache_delete( $term, 'terms' );
// Affirm that the cache is empty.
$this->assertEmpty( wp_cache_get( $term, 'terms' ) );
$num_queries = $wpdb->num_queries;
// get_term() will only be update the cache if the 'filter' prop is unset.
unset( $term_object->filter );
$term_object_2 = get_term( $term_object, 'wptests_tax' );
// No new queries should have fired.
$this->assertSame( $num_queries, $wpdb->num_queries );
$this->assertEquals( $term_object, $term_object_2 );
}
public function test_get_term_should_update_term_cache_when_passed_a_valid_term_identifier() {
global $wpdb;
register_taxonomy( 'wptests_tax', 'post' );
$term = self::factory()->term->create( array(
'taxonomy' => 'wptests_tax',
) );
wp_cache_delete( $term, 'terms' );
// Affirm that the cache is empty.
$this->assertEmpty( wp_cache_get( $term, 'terms' ) );
$num_queries = $wpdb->num_queries;
// Prime cache.
$term_object = get_term( $term, 'wptests_tax' );
$this->assertNotEmpty( wp_cache_get( $term, 'terms' ) );
$this->assertSame( $num_queries + 1, $wpdb->num_queries );
$term_object_2 = get_term( $term, 'wptests_tax' );
// No new queries should have fired.
$this->assertSame( $num_queries + 1, $wpdb->num_queries );
$this->assertEquals( $term_object, $term_object_2 );
}
public function test_get_term_by_should_update_term_cache_when_passed_a_valid_term_identifier() {
global $wpdb;
register_taxonomy( 'wptests_tax', 'post' );
$term = self::factory()->term->create( array(
'taxonomy' => 'wptests_tax',
) );
wp_cache_delete( $term, 'terms' );
// Affirm that the cache is empty.
$this->assertEmpty( wp_cache_get( $term, 'terms' ) );
$num_queries = $wpdb->num_queries;
// Prime cache.
$term_object = get_term_by( 'id', $term, 'wptests_tax' );
$this->assertNotEmpty( wp_cache_get( $term, 'terms' ) );
$this->assertSame( $num_queries + 1, $wpdb->num_queries );
$term_object_2 = get_term( $term, 'wptests_tax' );
// No new queries should have fired.
$this->assertSame( $num_queries + 1, $wpdb->num_queries );
$this->assertEquals( $term_object, $term_object_2 );
}
/**
* @ticket 30749
*/
public function test_get_terms_should_update_cache_for_located_terms() {
global $wpdb;
register_taxonomy( 'wptests_tax', 'post' );
$terms = self::factory()->term->create_many( 5, array(
'taxonomy' => 'wptests_tax',
) );
$term_objects = get_terms( 'wptests_tax', array(
'hide_empty' => false,
) );
$num_queries = $wpdb->num_queries;
foreach ( $terms as $term_id ) {
get_term( $term_id, 'wptests_tax' );
}
$this->assertSame( $num_queries, $wpdb->num_queries );
_unregister_taxonomy( 'wptests_tax' );
}
/**
* @ticket 35462
*/
public function test_term_objects_should_not_be_modified_by_update_term_cache() {
register_taxonomy( 'wptests_tax', 'post' );
$t = self::factory()->term->create( array( 'taxonomy' => 'wptests_tax' ) );
$p = self::factory()->post->create();
wp_set_object_terms( $p, $t, 'wptests_tax' );
$terms = wp_get_object_terms( $p, 'wptests_tax', array( 'fields' => 'all_with_object_id' ) );
update_term_cache( $terms );
foreach ( $terms as $term ) {
$this->assertSame( $p, $term->object_id );
}
}
}