WordPress.org

Make WordPress Core


Ignore:
Timestamp:
02/03/2014 07:41:40 PM (8 years ago)
Author:
wonderboymusic
Message:

Properly invalidate the cache for wp_count_posts() on insert, trash, or when transitioning post_status inside of _transition_post_status(). Introduces _count_posts_cache_key(). Adds unit tests.

Props mark8barnes, for bringing this to our attention in an initial patch.
Fixes #21879.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/tests/phpunit/tests/post.php

    r25975 r27081  
    830830        return $counts;
    831831    }
     832
     833    function test_wp_count_posts_insert_invalidation() {
     834        $post_ids = $this->factory->post->create_many( 10 );
     835        $initial_counts = wp_count_posts();
     836
     837        $key = array_rand( $post_ids );
     838        $_post = get_post( $post_ids[$key], ARRAY_A );
     839        $_post['post_status'] = 'draft';
     840        wp_insert_post( $_post );
     841        $post = get_post( $post_ids[$key] );
     842        $this->assertEquals( 'draft', $post->post_status );
     843        $this->assertNotEquals( 'publish', $post->post_status );
     844
     845        $after_draft_counts = wp_count_posts();
     846        $this->assertEquals( 1, $after_draft_counts->draft );
     847        $this->assertEquals( 9, $after_draft_counts->publish );
     848        $this->assertNotEquals( $initial_counts->publish, $after_draft_counts->publish );
     849    }
     850
     851    function test_wp_count_posts_trash_invalidation() {
     852        $post_ids = $this->factory->post->create_many( 10 );
     853        $initial_counts = wp_count_posts();
     854
     855        $key = array_rand( $post_ids );
     856
     857        wp_trash_post( $post_ids[$key] );
     858
     859        $post = get_post( $post_ids[$key] );
     860        $this->assertEquals( 'trash', $post->post_status );
     861        $this->assertNotEquals( 'publish', $post->post_status );
     862
     863        $after_trash_counts = wp_count_posts();
     864        $this->assertEquals( 1, $after_trash_counts->trash );
     865        $this->assertEquals( 9, $after_trash_counts->publish );
     866        $this->assertNotEquals( $initial_counts->publish, $after_trash_counts->publish );
     867    }
    832868}
Note: See TracChangeset for help on using the changeset viewer.