Make WordPress Core

Changeset 34131


Ignore:
Timestamp:
09/14/2015 10:03:23 PM (8 years ago)
Author:
wonderboymusic
Message:

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.

Location:
trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/comment-functions.php

    r34130 r34131  
    19171917    clean_post_cache( $post );
    19181918
     1919    wp_cache_delete( "comments-{$post_id}", 'counts' );
     1920
    19191921    /**
    19201922     * Fires immediately after a post's comment count is updated in the database.
     
    23252327    foreach ( (array) $ids as $id ) {
    23262328        wp_cache_delete( $id, 'comment' );
    2327         wp_cache_delete( "comment-{$id}", 'counts' );
    23282329    }
    23292330
  • trunk/tests/phpunit/tests/comment/wpCountComments.php

    r33823 r34131  
    8888        $this->assertEquals( 0, $count->total_comments );
    8989    }
     90
     91    public function test_wp_count_comments_cache() {
     92        $post_id = $this->factory->post->create( array(
     93            'post_status' => 'publish'
     94        ) );
     95        $comment_id = $this->factory->comment->create( array(
     96            'comment_approved' => '1',
     97            'comment_post_ID' => $post_id
     98        ) );
     99
     100        $count1 = wp_count_comments( $post_id );
     101
     102        $this->assertEquals( 1, $count1->approved );
     103        $this->assertEquals( 0, $count1->moderated );
     104        $this->assertEquals( 0, $count1->spam );
     105        $this->assertEquals( 0, $count1->trash );
     106        $this->assertEquals( 0, $count1->{'post-trashed'} );
     107        $this->assertEquals( 1, $count1->total_comments );
     108
     109        wp_spam_comment( $comment_id );
     110        $count2 = wp_count_comments( $post_id );
     111
     112        $this->assertEquals( 0, $count2->approved );
     113        $this->assertEquals( 0, $count2->moderated );
     114        $this->assertEquals( 1, $count2->spam );
     115        $this->assertEquals( 0, $count2->trash );
     116        $this->assertEquals( 0, $count2->{'post-trashed'} );
     117        $this->assertEquals( 1, $count2->total_comments );
     118
     119        wp_trash_comment( $comment_id );
     120        $count3 = wp_count_comments( $post_id );
     121
     122        $this->assertEquals( 0, $count3->approved );
     123        $this->assertEquals( 0, $count3->moderated );
     124        $this->assertEquals( 0, $count3->spam );
     125        $this->assertEquals( 1, $count3->trash );
     126        $this->assertEquals( 0, $count3->{'post-trashed'} );
     127        $this->assertEquals( 0, $count3->total_comments );
     128       
     129        wp_untrash_comment( $comment_id );
     130        $count4 = wp_count_comments( $post_id );
     131
     132        $this->assertEquals( 0, $count4->approved );
     133        $this->assertEquals( 0, $count4->moderated );
     134        $this->assertEquals( 1, $count4->spam );
     135        $this->assertEquals( 0, $count4->trash );
     136        $this->assertEquals( 0, $count4->{'post-trashed'} );
     137        $this->assertEquals( 1, $count4->total_comments );
     138    }
    90139}
Note: See TracChangeset for help on using the changeset viewer.