WordPress.org

Make WordPress Core

Changeset 30113


Ignore:
Timestamp:
10/30/2014 04:15:03 AM (6 years ago)
Author:
boonebgorges
Message:

Streamline some get_terms() cache tests.

  • Split large method into a number of smaller tests.
  • Create fewer fixtures.

See #30017.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/tests/phpunit/tests/term/getTerms.php

    r30107 r30113  
    1515     * @ticket 23326
    1616     */
    17     function test_get_terms_cache() {
     17    public function test_get_terms_cache() {
    1818        global $wpdb;
    1919
    20         $posts = $this->factory->post->create_many( 15, array( 'post_type' => 'post' ) );
    21         foreach ( $posts as $post )
    22             wp_set_object_terms( $post, rand_str(), 'post_tag' );
    23         wp_cache_delete( 'last_changed', 'terms' );
    24 
    25         $this->assertFalse( wp_cache_get( 'last_changed', 'terms' ) );
     20        $this->set_up_three_posts_and_tags();
    2621
    2722        $num_queries = $wpdb->num_queries;
     
    2924        // last_changed and num_queries should bump
    3025        $terms = get_terms( 'post_tag' );
    31         $this->assertEquals( 15, count( $terms ) );
     26        $this->assertEquals( 3, count( $terms ) );
    3227        $time1 = wp_cache_get( 'last_changed', 'terms' );
    3328        $this->assertNotEmpty( $time1 );
     
    3833        // Again. last_changed and num_queries should remain the same.
    3934        $terms = get_terms( 'post_tag' );
    40         $this->assertEquals( 15, count( $terms ) );
     35        $this->assertEquals( 3, count( $terms ) );
    4136        $this->assertEquals( $time1, wp_cache_get( 'last_changed', 'terms' ) );
    4237        $this->assertEquals( $num_queries, $wpdb->num_queries );
    43 
     38    }
     39
     40    /**
     41     * @ticket 23326
     42     */
     43    public function test_get_terms_cache_should_be_missed_when_passing_number() {
     44        global $wpdb;
     45
     46        $this->set_up_three_posts_and_tags();
     47
     48        // Prime cache
     49        $terms = get_terms( 'post_tag' );
     50        $time1 = wp_cache_get( 'last_changed', 'terms' );
    4451        $num_queries = $wpdb->num_queries;
    4552
    46 
    47         // Different query. num_queries should bump, last_changed should remain the same.
    48         $terms = get_terms( 'post_tag', array( 'number' => 10 ) );
    49         $this->assertEquals( 10, count( $terms ) );
     53        // num_queries should bump, last_changed should remain the same.
     54        $terms = get_terms( 'post_tag', array( 'number' => 2 ) );
     55        $this->assertEquals( 2, count( $terms ) );
    5056        $this->assertEquals( $time1, wp_cache_get( 'last_changed', 'terms' ) );
    5157        $this->assertEquals( $num_queries + 1, $wpdb->num_queries );
     
    5460
    5561        // Again. last_changed and num_queries should remain the same.
    56         $terms = get_terms( 'post_tag', array( 'number' => 10 ) );
    57         $this->assertEquals( 10, count( $terms ) );
     62        $terms = get_terms( 'post_tag', array( 'number' => 2 ) );
     63        $this->assertEquals( 2, count( $terms ) );
    5864        $this->assertEquals( $time1, wp_cache_get( 'last_changed', 'terms' ) );
    5965        $this->assertEquals( $num_queries, $wpdb->num_queries );
    60 
    61         // Force last_changed to bump
     66    }
     67
     68    /**
     69     * @ticket 23326
     70     */
     71    public function test_wp_delete_term_should_invalidate_cache() {
     72        global $wpdb;
     73
     74        $this->set_up_three_posts_and_tags();
     75
     76        // Prime cache
     77        $terms = get_terms( 'post_tag' );
     78        $time1 = wp_cache_get( 'last_changed', 'terms' );
     79        $num_queries = $wpdb->num_queries;
     80
     81        // Force last_changed to bump.
    6282        wp_delete_term( $terms[0]->term_id, 'post_tag' );
    6383
     
    6585        $this->assertNotEquals( $time1, $time2 = wp_cache_get( 'last_changed', 'terms' ) );
    6686
    67         // last_changed and num_queries should bump after a term is deleted
     87        // last_changed and num_queries should bump after a term is deleted.
    6888        $terms = get_terms( 'post_tag' );
    69         $this->assertEquals( 14, count( $terms ) );
     89        $this->assertEquals( 2, count( $terms ) );
    7090        $this->assertEquals( $time2, wp_cache_get( 'last_changed', 'terms' ) );
    7191        $this->assertEquals( $num_queries + 1, $wpdb->num_queries );
     
    7595        // Again. last_changed and num_queries should remain the same.
    7696        $terms = get_terms( 'post_tag' );
    77         $this->assertEquals( 14, count( $terms ) );
     97        $this->assertEquals( 2, count( $terms ) );
    7898        $this->assertEquals( $time2, wp_cache_get( 'last_changed', 'terms' ) );
    7999        $this->assertEquals( $num_queries, $wpdb->num_queries );
     
    10531073        );
    10541074    }
     1075
     1076    protected function set_up_three_posts_and_tags() {
     1077        $posts = $this->factory->post->create_many( 3, array( 'post_type' => 'post' ) );
     1078        foreach ( $posts as $post ) {
     1079            wp_set_object_terms( $post, rand_str(), 'post_tag' );
     1080        }
     1081
     1082        wp_cache_delete( 'last_changed', 'terms' );
     1083    }
    10551084}
Note: See TracChangeset for help on using the changeset viewer.