Make WordPress Core


Ignore:
Timestamp:
09/25/2015 01:46:36 PM (9 years ago)
Author:
boonebgorges
Message:

Bust term query cache when modifying term meta.

The 'last_changed' incrementor is used to invalidate the get_terms() query
cache. Since get_terms() queries may reference 'meta_query', changing term
metadata could change the results of the queries. So we invalidate the cache
on add, delete, and update.

See #10142.

File:
1 edited

Legend:

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

    r34529 r34538  
    145145        }
    146146    }
     147
     148    public function test_adding_term_meta_should_bust_get_terms_cache() {
     149        $terms = $this->factory->term->create_many( 2, array( 'taxonomy' => 'wptests_tax' ) );
     150
     151        add_term_meta( $terms[0], 'foo', 'bar' );
     152
     153        // Prime cache.
     154        $found = get_terms( 'wptests_tax', array(
     155            'hide_empty' => false,
     156            'fields' => 'ids',
     157            'meta_query' => array(
     158                array(
     159                    'key' => 'foo',
     160                    'value' => 'bar',
     161                ),
     162            ),
     163        ) );
     164
     165        $this->assertEqualSets( array( $terms[0] ), $found );
     166
     167        add_term_meta( $terms[1], 'foo', 'bar' );
     168
     169        $found = get_terms( 'wptests_tax', array(
     170            'hide_empty' => false,
     171            'fields' => 'ids',
     172            'meta_query' => array(
     173                array(
     174                    'key' => 'foo',
     175                    'value' => 'bar',
     176                ),
     177            ),
     178        ) );
     179
     180        $this->assertEqualSets( array( $terms[0], $terms[1] ), $found );
     181    }
     182
     183    public function test_updating_term_meta_should_bust_get_terms_cache() {
     184        $terms = $this->factory->term->create_many( 2, array( 'taxonomy' => 'wptests_tax' ) );
     185
     186        add_term_meta( $terms[0], 'foo', 'bar' );
     187        add_term_meta( $terms[1], 'foo', 'baz' );
     188
     189        // Prime cache.
     190        $found = get_terms( 'wptests_tax', array(
     191            'hide_empty' => false,
     192            'fields' => 'ids',
     193            'meta_query' => array(
     194                array(
     195                    'key' => 'foo',
     196                    'value' => 'bar',
     197                ),
     198            ),
     199        ) );
     200
     201        $this->assertEqualSets( array( $terms[0] ), $found );
     202
     203        update_term_meta( $terms[1], 'foo', 'bar' );
     204
     205        $found = get_terms( 'wptests_tax', array(
     206            'hide_empty' => false,
     207            'fields' => 'ids',
     208            'meta_query' => array(
     209                array(
     210                    'key' => 'foo',
     211                    'value' => 'bar',
     212                ),
     213            ),
     214        ) );
     215
     216        $this->assertEqualSets( array( $terms[0], $terms[1] ), $found );
     217    }
     218
     219    public function test_deleting_term_meta_should_bust_get_terms_cache() {
     220        $terms = $this->factory->term->create_many( 2, array( 'taxonomy' => 'wptests_tax' ) );
     221
     222        add_term_meta( $terms[0], 'foo', 'bar' );
     223        add_term_meta( $terms[1], 'foo', 'bar' );
     224
     225        // Prime cache.
     226        $found = get_terms( 'wptests_tax', array(
     227            'hide_empty' => false,
     228            'fields' => 'ids',
     229            'meta_query' => array(
     230                array(
     231                    'key' => 'foo',
     232                    'value' => 'bar',
     233                ),
     234            ),
     235        ) );
     236
     237        $this->assertEqualSets( array( $terms[0], $terms[1] ), $found );
     238
     239        delete_term_meta( $terms[1], 'foo', 'bar' );
     240
     241        $found = get_terms( 'wptests_tax', array(
     242            'hide_empty' => false,
     243            'fields' => 'ids',
     244            'meta_query' => array(
     245                array(
     246                    'key' => 'foo',
     247                    'value' => 'bar',
     248                ),
     249            ),
     250        ) );
     251
     252        $this->assertEqualSets( array( $terms[0] ), $found );
     253    }
    147254}
Note: See TracChangeset for help on using the changeset viewer.