Skip to content

Commit 66daf71

Browse files
committed
Fix input validation, capability detection, and test coverage
- Add proper input validation for numeric IDs to prevent silent fallthrough - Remove wp_using_ext_object_cache() requirement; rely only on wp_cache_supports( 'flush_group' ) - Check return values of wp_cache_flush_group() calls and error on failure - Add after_invoke hooks to verify cache is actually cleared - Add @require-wp-6-1 version-gated scenarios for group flush behavior - Add error test cases for invalid IDs
1 parent bbc0fdf commit 66daf71

2 files changed

Lines changed: 282 additions & 29 deletions

File tree

‎features/cache-flush-granular.feature‎

Lines changed: 231 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,15 @@ Feature: Granular cache flushing operations
1010
wp_cache_set( 'post_123', array( 'ID' => 123, 'post_title' => 'Test' ), 'posts' );
1111
wp_cache_set( 'meta_123', array( 'key' => 'value' ), 'post_meta' );
1212
};
13+
$verify_cache_cleared = function(){
14+
$post = wp_cache_get( 'post_123', 'posts' );
15+
$meta = wp_cache_get( 'meta_123', 'post_meta' );
16+
if ( false !== $post || false !== $meta ) {
17+
WP_CLI::error( 'Cache was not properly cleared.' );
18+
}
19+
};
1320
WP_CLI::add_hook( 'before_invoke:cache flush-post', $cache_post );
21+
WP_CLI::add_hook( 'after_invoke:cache flush-post', $verify_cache_cleared );
1422
"""
1523

1624
When I run `wp cache flush-post 123`
@@ -29,7 +37,15 @@ Feature: Granular cache flushing operations
2937
wp_cache_set( 'term_5', array( 'term_id' => 5 ), 'terms' );
3038
wp_cache_set( 'term_meta_5', array(), 'term_meta' );
3139
};
40+
$verify_cache_cleared = function(){
41+
$term = wp_cache_get( 'term_5', 'terms' );
42+
$meta = wp_cache_get( 'term_meta_5', 'term_meta' );
43+
if ( false !== $term || false !== $meta ) {
44+
WP_CLI::error( 'Cache was not properly cleared.' );
45+
}
46+
};
3247
WP_CLI::add_hook( 'before_invoke:cache flush-term', $cache_term );
48+
WP_CLI::add_hook( 'after_invoke:cache flush-term', $verify_cache_cleared );
3349
"""
3450

3551
When I run `wp cache flush-term 5`
@@ -48,7 +64,15 @@ Feature: Granular cache flushing operations
4864
wp_cache_set( 'comment_42', array(), 'comment' );
4965
wp_cache_set( 'comment_meta_42', array(), 'comment_meta' );
5066
};
67+
$verify_cache_cleared = function(){
68+
$comment = wp_cache_get( 'comment_42', 'comment' );
69+
$meta = wp_cache_get( 'comment_meta_42', 'comment_meta' );
70+
if ( false !== $comment || false !== $meta ) {
71+
WP_CLI::error( 'Cache was not properly cleared.' );
72+
}
73+
};
5174
WP_CLI::add_hook( 'before_invoke:cache flush-comment', $cache_comment );
75+
WP_CLI::add_hook( 'after_invoke:cache flush-comment', $verify_cache_cleared );
5276
"""
5377

5478
When I run `wp cache flush-comment 42`
@@ -67,7 +91,15 @@ Feature: Granular cache flushing operations
6791
wp_cache_set( 'user_1', array( 'ID' => 1 ), 'users' );
6892
wp_cache_set( 'user_meta_1', array(), 'user_meta' );
6993
};
94+
$verify_cache_cleared = function(){
95+
$user = wp_cache_get( 'user_1', 'users' );
96+
$meta = wp_cache_get( 'user_meta_1', 'user_meta' );
97+
if ( false !== $user || false !== $meta ) {
98+
WP_CLI::error( 'Cache was not properly cleared.' );
99+
}
100+
};
70101
WP_CLI::add_hook( 'before_invoke:cache flush-user', $cache_user );
102+
WP_CLI::add_hook( 'after_invoke:cache flush-user', $verify_cache_cleared );
71103
"""
72104

73105
When I run `wp cache flush-user 1`
@@ -86,7 +118,15 @@ Feature: Granular cache flushing operations
86118
wp_cache_set( 'my_option', 'value', 'options' );
87119
wp_cache_set( 'alloptions', array( 'my_option' => 'value' ), 'options' );
88120
};
121+
$verify_cache_cleared = function(){
122+
$option = wp_cache_get( 'my_option', 'options' );
123+
$alloptions = wp_cache_get( 'alloptions', 'options' );
124+
if ( false !== $option || false !== $alloptions ) {
125+
WP_CLI::error( 'Cache was not properly cleared.' );
126+
}
127+
};
89128
WP_CLI::add_hook( 'before_invoke:cache flush-option', $cache_option );
129+
WP_CLI::add_hook( 'after_invoke:cache flush-option', $verify_cache_cleared );
90130
"""
91131

92132
When I run `wp cache flush-option my_option`
@@ -95,4 +135,195 @@ Feature: Granular cache flushing operations
95135
Success: Option cache for 'my_option' cleared.
96136
"""
97137

138+
@skip-object-cache
139+
Scenario: Invalid post ID fails gracefully
140+
Given a WP install
141+
142+
When I try `wp cache flush-post abc`
143+
Then STDERR should contain:
144+
"""
145+
Please provide a valid post ID.
146+
"""
147+
148+
@skip-object-cache
149+
Scenario: Invalid term ID fails gracefully
150+
Given a WP install
151+
152+
When I try `wp cache flush-term xyz`
153+
Then STDERR should contain:
154+
"""
155+
Please provide a valid term ID.
156+
"""
157+
158+
@skip-object-cache
159+
Scenario: Invalid comment ID fails gracefully
160+
Given a WP install
161+
162+
When I try `wp cache flush-comment notanumber`
163+
Then STDERR should contain:
164+
"""
165+
Please provide a valid comment ID.
166+
"""
167+
168+
@skip-object-cache
169+
Scenario: Invalid user ID fails gracefully
170+
Given a WP install
171+
172+
When I try `wp cache flush-user nope`
173+
Then STDERR should contain:
174+
"""
175+
Please provide a valid user ID.
176+
"""
177+
178+
@require-wp-6-1 @skip-object-cache
179+
Scenario: Flush all post caches on WordPress 6.1+
180+
Given a WP install
181+
And a wp-content/mu-plugins/test-harness.php file:
182+
"""
183+
<?php
184+
$cache_posts = function(){
185+
wp_cache_set( 'post_1', array( 'ID' => 1 ), 'posts' );
186+
wp_cache_set( 'post_2', array( 'ID' => 2 ), 'posts' );
187+
wp_cache_set( 'meta_1', array( 'key' => 'value' ), 'post_meta' );
188+
};
189+
$verify_group_cleared = function(){
190+
if ( function_exists( 'wp_cache_supports' ) && wp_cache_supports( 'flush_group' ) ) {
191+
$post1 = wp_cache_get( 'post_1', 'posts' );
192+
$post2 = wp_cache_get( 'post_2', 'posts' );
193+
$meta = wp_cache_get( 'meta_1', 'post_meta' );
194+
if ( false !== $post1 || false !== $post2 || false !== $meta ) {
195+
WP_CLI::error( 'Group cache was not properly cleared.' );
196+
}
197+
}
198+
};
199+
WP_CLI::add_hook( 'before_invoke:cache flush-post', $cache_posts );
200+
WP_CLI::add_hook( 'after_invoke:cache flush-post', $verify_group_cleared );
201+
"""
202+
203+
When I run `wp cache flush-post`
204+
Then STDOUT should contain:
205+
"""
206+
Success: Post caches cleared.
207+
"""
208+
209+
@require-wp-6-1 @skip-object-cache
210+
Scenario: Flush all term caches on WordPress 6.1+
211+
Given a WP install
212+
And a wp-content/mu-plugins/test-harness.php file:
213+
"""
214+
<?php
215+
$cache_terms = function(){
216+
wp_cache_set( 'term_1', array( 'term_id' => 1 ), 'terms' );
217+
wp_cache_set( 'term_2', array( 'term_id' => 2 ), 'terms' );
218+
wp_cache_set( 'meta_1', array(), 'term_meta' );
219+
};
220+
$verify_group_cleared = function(){
221+
if ( function_exists( 'wp_cache_supports' ) && wp_cache_supports( 'flush_group' ) ) {
222+
$term1 = wp_cache_get( 'term_1', 'terms' );
223+
$term2 = wp_cache_get( 'term_2', 'terms' );
224+
$meta = wp_cache_get( 'meta_1', 'term_meta' );
225+
if ( false !== $term1 || false !== $term2 || false !== $meta ) {
226+
WP_CLI::error( 'Group cache was not properly cleared.' );
227+
}
228+
}
229+
};
230+
WP_CLI::add_hook( 'before_invoke:cache flush-term', $cache_terms );
231+
WP_CLI::add_hook( 'after_invoke:cache flush-term', $verify_group_cleared );
232+
"""
233+
234+
When I run `wp cache flush-term`
235+
Then STDOUT should contain:
236+
"""
237+
Success: Term caches cleared.
238+
"""
239+
240+
@require-wp-6-1 @skip-object-cache
241+
Scenario: Flush all comment caches on WordPress 6.1+
242+
Given a WP install
243+
And a wp-content/mu-plugins/test-harness.php file:
244+
"""
245+
<?php
246+
$cache_comments = function(){
247+
wp_cache_set( 'comment_1', array(), 'comment' );
248+
wp_cache_set( 'comment_2', array(), 'comment' );
249+
wp_cache_set( 'meta_1', array(), 'comment_meta' );
250+
};
251+
$verify_group_cleared = function(){
252+
if ( function_exists( 'wp_cache_supports' ) && wp_cache_supports( 'flush_group' ) ) {
253+
$comment1 = wp_cache_get( 'comment_1', 'comment' );
254+
$comment2 = wp_cache_get( 'comment_2', 'comment' );
255+
$meta = wp_cache_get( 'meta_1', 'comment_meta' );
256+
if ( false !== $comment1 || false !== $comment2 || false !== $meta ) {
257+
WP_CLI::error( 'Group cache was not properly cleared.' );
258+
}
259+
}
260+
};
261+
WP_CLI::add_hook( 'before_invoke:cache flush-comment', $cache_comments );
262+
WP_CLI::add_hook( 'after_invoke:cache flush-comment', $verify_group_cleared );
263+
"""
264+
265+
When I run `wp cache flush-comment`
266+
Then STDOUT should contain:
267+
"""
268+
Success: Comment caches cleared.
269+
"""
270+
271+
@require-wp-6-1 @skip-object-cache
272+
Scenario: Flush all user caches on WordPress 6.1+
273+
Given a WP install
274+
And a wp-content/mu-plugins/test-harness.php file:
275+
"""
276+
<?php
277+
$cache_users = function(){
278+
wp_cache_set( 'user_1', array( 'ID' => 1 ), 'users' );
279+
wp_cache_set( 'user_2', array( 'ID' => 2 ), 'users' );
280+
wp_cache_set( 'meta_1', array(), 'user_meta' );
281+
};
282+
$verify_group_cleared = function(){
283+
if ( function_exists( 'wp_cache_supports' ) && wp_cache_supports( 'flush_group' ) ) {
284+
$user1 = wp_cache_get( 'user_1', 'users' );
285+
$user2 = wp_cache_get( 'user_2', 'users' );
286+
$meta = wp_cache_get( 'meta_1', 'user_meta' );
287+
if ( false !== $user1 || false !== $user2 || false !== $meta ) {
288+
WP_CLI::error( 'Group cache was not properly cleared.' );
289+
}
290+
}
291+
};
292+
WP_CLI::add_hook( 'before_invoke:cache flush-user', $cache_users );
293+
WP_CLI::add_hook( 'after_invoke:cache flush-user', $verify_group_cleared );
294+
"""
295+
296+
When I run `wp cache flush-user`
297+
Then STDOUT should contain:
298+
"""
299+
Success: User caches cleared.
300+
"""
301+
302+
@require-wp-6-1 @skip-object-cache
303+
Scenario: Flush all option caches on WordPress 6.1+
304+
Given a WP install
305+
And a wp-content/mu-plugins/test-harness.php file:
306+
"""
307+
<?php
308+
$cache_options = function(){
309+
wp_cache_set( 'option1', 'value1', 'options' );
310+
wp_cache_set( 'option2', 'value2', 'options' );
311+
};
312+
$verify_group_cleared = function(){
313+
if ( function_exists( 'wp_cache_supports' ) && wp_cache_supports( 'flush_group' ) ) {
314+
$option1 = wp_cache_get( 'option1', 'options' );
315+
$option2 = wp_cache_get( 'option2', 'options' );
316+
if ( false !== $option1 || false !== $option2 ) {
317+
WP_CLI::error( 'Group cache was not properly cleared.' );
318+
}
319+
}
320+
};
321+
WP_CLI::add_hook( 'before_invoke:cache flush-option', $cache_options );
322+
WP_CLI::add_hook( 'after_invoke:cache flush-option', $verify_group_cleared );
323+
"""
98324

325+
When I run `wp cache flush-option`
326+
Then STDOUT should contain:
327+
"""
328+
Success: Option caches cleared.
329+
"""

0 commit comments

Comments
 (0)