From 545da32217e4c6a7f2e840b74e9403310152c56c Mon Sep 17 00:00:00 2001 From: Scott Taylor Date: Mon, 14 Sep 2015 22:03:23 +0000 Subject: [PATCH] The "counts" cache for comments by post id is never invalidated. Neither `wp_update_comment_count()` nor `wp_update_comment_count_now()` touch the cache. Adds unit test. See #33875. git-svn-id: https://develop.svn.wordpress.org/trunk@34131 602fd350-edb4-49c9-b593-d223f7449a82 --- src/wp-includes/comment-functions.php | 3 +- .../phpunit/tests/comment/wpCountComments.php | 49 +++++++++++++++++++ 2 files changed, 51 insertions(+), 1 deletion(-) diff --git a/src/wp-includes/comment-functions.php b/src/wp-includes/comment-functions.php index c71673579f..b1127fa9d1 100644 --- a/src/wp-includes/comment-functions.php +++ b/src/wp-includes/comment-functions.php @@ -1916,6 +1916,8 @@ function wp_update_comment_count_now($post_id) { clean_post_cache( $post ); + wp_cache_delete( "comments-{$post_id}", 'counts' ); + /** * Fires immediately after a post's comment count is updated in the database. * @@ -2324,7 +2326,6 @@ function xmlrpc_pingback_error( $ixr_error ) { function clean_comment_cache($ids) { foreach ( (array) $ids as $id ) { wp_cache_delete( $id, 'comment' ); - wp_cache_delete( "comment-{$id}", 'counts' ); } wp_cache_set( 'last_changed', microtime(), 'comment' ); diff --git a/tests/phpunit/tests/comment/wpCountComments.php b/tests/phpunit/tests/comment/wpCountComments.php index 066d046d9a..a80f16933d 100644 --- a/tests/phpunit/tests/comment/wpCountComments.php +++ b/tests/phpunit/tests/comment/wpCountComments.php @@ -87,4 +87,53 @@ class Tests_WP_Count_Comments extends WP_UnitTestCase { $this->assertEquals( 1, $count->{'post-trashed'} ); $this->assertEquals( 0, $count->total_comments ); } + + public function test_wp_count_comments_cache() { + $post_id = $this->factory->post->create( array( + 'post_status' => 'publish' + ) ); + $comment_id = $this->factory->comment->create( array( + 'comment_approved' => '1', + 'comment_post_ID' => $post_id + ) ); + + $count1 = wp_count_comments( $post_id ); + + $this->assertEquals( 1, $count1->approved ); + $this->assertEquals( 0, $count1->moderated ); + $this->assertEquals( 0, $count1->spam ); + $this->assertEquals( 0, $count1->trash ); + $this->assertEquals( 0, $count1->{'post-trashed'} ); + $this->assertEquals( 1, $count1->total_comments ); + + wp_spam_comment( $comment_id ); + $count2 = wp_count_comments( $post_id ); + + $this->assertEquals( 0, $count2->approved ); + $this->assertEquals( 0, $count2->moderated ); + $this->assertEquals( 1, $count2->spam ); + $this->assertEquals( 0, $count2->trash ); + $this->assertEquals( 0, $count2->{'post-trashed'} ); + $this->assertEquals( 1, $count2->total_comments ); + + wp_trash_comment( $comment_id ); + $count3 = wp_count_comments( $post_id ); + + $this->assertEquals( 0, $count3->approved ); + $this->assertEquals( 0, $count3->moderated ); + $this->assertEquals( 0, $count3->spam ); + $this->assertEquals( 1, $count3->trash ); + $this->assertEquals( 0, $count3->{'post-trashed'} ); + $this->assertEquals( 0, $count3->total_comments ); + + wp_untrash_comment( $comment_id ); + $count4 = wp_count_comments( $post_id ); + + $this->assertEquals( 0, $count4->approved ); + $this->assertEquals( 0, $count4->moderated ); + $this->assertEquals( 1, $count4->spam ); + $this->assertEquals( 0, $count4->trash ); + $this->assertEquals( 0, $count4->{'post-trashed'} ); + $this->assertEquals( 1, $count4->total_comments ); + } }