Skip to content

Commit f196fef

Browse files
swissspidyclaude
andcommitted
Fix failing granular cache flush tests and PHPCS warnings
The post, term and user scenarios seeded the object cache with plain arrays for entities that do not exist. clean_post_cache() then returned early for the missing post, and get_term() / clean_user_cache() fatally errored when they found an array where they expected an object. Prime the caches for the post, term and user that every install creates (ID 1) instead. For the term, cleaning a category rebuilds the term hierarchy and re-caches the term from the database, so the scenario seeds a stale name and checks that the stale entry is gone. Also align assignments as required by PHPCS. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01UWTd5pwpi2xQfDA6mUYkkx
1 parent 8d31feb commit f196fef

2 files changed

Lines changed: 38 additions & 21 deletions

File tree

‎features/cache-flush-granular.feature‎

Lines changed: 32 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,16 @@ Feature: Granular cache flushing operations
77
"""
88
<?php
99
$cache_post = function(){
10-
wp_cache_set( 123, array( 'ID' => 123, 'post_title' => 'Test' ), 'posts' );
11-
wp_cache_set( 123, array( 'key' => 'value' ), 'post_meta' );
10+
// Prime the caches for the "Hello world!" post created by the install.
11+
get_post( 1 );
12+
get_post_meta( 1 );
13+
if ( false === wp_cache_get( 1, 'posts' ) || false === wp_cache_get( 1, 'post_meta' ) ) {
14+
WP_CLI::error( 'Cache was not primed.' );
15+
}
1216
};
1317
$verify_cache_cleared = function(){
14-
$post = wp_cache_get( 123, 'posts' );
15-
$meta = wp_cache_get( 123, 'post_meta' );
18+
$post = wp_cache_get( 1, 'posts' );
19+
$meta = wp_cache_get( 1, 'post_meta' );
1620
if ( false !== $post || false !== $meta ) {
1721
WP_CLI::error( 'Cache was not properly cleared.' );
1822
}
@@ -21,10 +25,10 @@ Feature: Granular cache flushing operations
2125
WP_CLI::add_hook( 'after_invoke:cache flush-post', $verify_cache_cleared );
2226
"""
2327

24-
When I run `wp cache flush-post 123`
28+
When I run `wp cache flush-post 1`
2529
Then STDOUT should contain:
2630
"""
27-
Success: Post cache for ID 123 cleared.
31+
Success: Post cache for ID 1 cleared.
2832
"""
2933

3034
@skip-object-cache
@@ -34,24 +38,33 @@ Feature: Granular cache flushing operations
3438
"""
3539
<?php
3640
$cache_term = function(){
37-
wp_cache_set( 5, array( 'term_id' => 5 ), 'terms' );
38-
wp_cache_set( 5, array(), 'term_meta' );
41+
// Prime the caches for the "Uncategorized" term created by the install,
42+
// with a stale name so that a refreshed cache entry can be told apart.
43+
$term = get_term( 1 )->data;
44+
$term->name = 'Stale';
45+
wp_cache_set( 1, $term, 'terms' );
46+
get_term_meta( 1 );
47+
if ( false === wp_cache_get( 1, 'term_meta' ) ) {
48+
WP_CLI::error( 'Cache was not primed.' );
49+
}
3950
};
4051
$verify_cache_cleared = function(){
41-
$term = wp_cache_get( 5, 'terms' );
42-
$meta = wp_cache_get( 5, 'term_meta' );
43-
if ( false !== $term || false !== $meta ) {
52+
// Cleaning a category rebuilds the term hierarchy, which can re-cache
53+
// the term from the database, so only check that the stale entry is gone.
54+
$term = wp_cache_get( 1, 'terms' );
55+
$meta = wp_cache_get( 1, 'term_meta' );
56+
if ( ( false !== $term && 'Stale' === $term->name ) || false !== $meta ) {
4457
WP_CLI::error( 'Cache was not properly cleared.' );
4558
}
4659
};
4760
WP_CLI::add_hook( 'before_invoke:cache flush-term', $cache_term );
4861
WP_CLI::add_hook( 'after_invoke:cache flush-term', $verify_cache_cleared );
4962
"""
5063

51-
When I run `wp cache flush-term 5`
64+
When I run `wp cache flush-term 1`
5265
Then STDOUT should contain:
5366
"""
54-
Success: Term cache for ID 5 cleared.
67+
Success: Term cache for ID 1 cleared.
5568
"""
5669

5770
@skip-object-cache
@@ -98,8 +111,12 @@ Feature: Granular cache flushing operations
98111
"""
99112
<?php
100113
$cache_user = function(){
101-
wp_cache_set( 1, array( 'ID' => 1 ), 'users' );
102-
wp_cache_set( 1, array(), 'user_meta' );
114+
// Prime the caches for the admin user created by the install.
115+
get_userdata( 1 );
116+
get_user_meta( 1 );
117+
if ( false === wp_cache_get( 1, 'users' ) || false === wp_cache_get( 1, 'user_meta' ) ) {
118+
WP_CLI::error( 'Cache was not primed.' );
119+
}
103120
};
104121
$verify_cache_cleared = function(){
105122
$user = wp_cache_get( 1, 'users' );

‎src/Cache_Command.php‎

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -646,7 +646,7 @@ public function flush_post( $args ) {
646646
if ( ! function_exists( 'wp_cache_supports' ) || ! wp_cache_supports( 'flush_group' ) ) {
647647
WP_CLI::error( 'Flushing all post caches requires WordPress 6.1+' );
648648
}
649-
$posts_flushed = wp_cache_flush_group( 'posts' );
649+
$posts_flushed = wp_cache_flush_group( 'posts' );
650650
$post_meta_flushed = wp_cache_flush_group( 'post_meta' );
651651
if ( ! $posts_flushed || ! $post_meta_flushed ) {
652652
WP_CLI::error( 'Failed to flush post caches.' );
@@ -682,8 +682,8 @@ public function flush_term( $args ) {
682682
if ( ! is_numeric( $args[0] ) || (int) $args[0] <= 0 ) {
683683
WP_CLI::error( 'Please provide a valid term ID.' );
684684
}
685-
$term_id = (int) $args[0];
686-
$term = get_term( $term_id );
685+
$term_id = (int) $args[0];
686+
$term = get_term( $term_id );
687687
$taxonomy = ( $term && ! is_wp_error( $term ) ) ? $term->taxonomy : '';
688688
clean_term_cache( $term_id, $taxonomy );
689689
wp_cache_delete( $term_id, 'term_meta' );
@@ -692,7 +692,7 @@ public function flush_term( $args ) {
692692
if ( ! function_exists( 'wp_cache_supports' ) || ! wp_cache_supports( 'flush_group' ) ) {
693693
WP_CLI::error( 'Flushing all term caches requires WordPress 6.1+' );
694694
}
695-
$terms_flushed = wp_cache_flush_group( 'terms' );
695+
$terms_flushed = wp_cache_flush_group( 'terms' );
696696
$term_meta_flushed = wp_cache_flush_group( 'term_meta' );
697697
if ( ! $terms_flushed || ! $term_meta_flushed ) {
698698
WP_CLI::error( 'Failed to flush term caches.' );
@@ -736,7 +736,7 @@ public function flush_comment( $args ) {
736736
if ( ! function_exists( 'wp_cache_supports' ) || ! wp_cache_supports( 'flush_group' ) ) {
737737
WP_CLI::error( 'Flushing all comment caches requires WordPress 6.1+' );
738738
}
739-
$comment_flushed = wp_cache_flush_group( 'comment' );
739+
$comment_flushed = wp_cache_flush_group( 'comment' );
740740
$comment_meta_flushed = wp_cache_flush_group( 'comment_meta' );
741741
if ( ! $comment_flushed || ! $comment_meta_flushed ) {
742742
WP_CLI::error( 'Failed to flush comment caches.' );
@@ -780,7 +780,7 @@ public function flush_user( $args ) {
780780
if ( ! function_exists( 'wp_cache_supports' ) || ! wp_cache_supports( 'flush_group' ) ) {
781781
WP_CLI::error( 'Flushing all user caches requires WordPress 6.1+' );
782782
}
783-
$users_flushed = wp_cache_flush_group( 'users' );
783+
$users_flushed = wp_cache_flush_group( 'users' );
784784
$user_meta_flushed = wp_cache_flush_group( 'user_meta' );
785785
if ( ! $users_flushed || ! $user_meta_flushed ) {
786786
WP_CLI::error( 'Failed to flush user caches.' );

0 commit comments

Comments
 (0)