Make WordPress Core

Ticket #30749: 30749.2.patch

File 30749.2.patch, 3.8 KB (added by boonebgorges, 11 years ago)
  • src/wp-includes/taxonomy.php

    diff --git src/wp-includes/taxonomy.php src/wp-includes/taxonomy.php
    index f407621..1d93f69 100644
    function get_terms( $taxonomies, $args = '' ) { 
    19121912        }
    19131913
    19141914        $terms = $wpdb->get_results($query);
     1915        if ( 'all' == $_fields ) {
     1916                update_term_cache( $terms );
     1917        }
    19151918
    19161919        if ( empty($terms) ) {
    19171920                wp_cache_add( $cache_key, array(), 'terms', DAY_IN_SECONDS );
  • tests/phpunit/tests/term/cache.php

    diff --git tests/phpunit/tests/term/cache.php tests/phpunit/tests/term/cache.php
    index d424353..1a31df0 100644
    class Tests_Term_Cache extends WP_UnitTestCase { 
    9393
    9494                _unregister_taxonomy( $tax );
    9595        }
     96
     97        public function test_get_term_should_update_term_cache_when_passed_an_object() {
     98                global $wpdb;
     99
     100                register_taxonomy( 'wptests_tax', 'post' );
     101                $term = $this->factory->term->create( array(
     102                        'taxonomy' => 'wptests_tax',
     103                ) );
     104
     105                $term_object = get_term( $term, 'wptests_tax' );
     106                wp_cache_delete( $term, 'wptests_tax' );
     107
     108                // Affirm that the cache is empty.
     109                $this->assertEmpty( wp_cache_get( $term, 'wptests_tax' ) );
     110
     111                $num_queries = $wpdb->num_queries;
     112
     113                // get_term() will only be update the cache if the 'filter' prop is unset.
     114                unset( $term_object->filter );
     115
     116                $term_object_2 = get_term( $term_object, 'wptests_tax' );
     117
     118                // No new queries should have fired.
     119                $this->assertSame( $num_queries, $wpdb->num_queries );
     120                $this->assertEquals( $term_object, $term_object_2 );
     121        }
     122
     123        public function test_get_term_should_update_term_cache_when_passed_a_valid_term_identifier() {
     124                global $wpdb;
     125
     126                register_taxonomy( 'wptests_tax', 'post' );
     127                $term = $this->factory->term->create( array(
     128                        'taxonomy' => 'wptests_tax',
     129                ) );
     130
     131                wp_cache_delete( $term, 'wptests_tax' );
     132
     133                // Affirm that the cache is empty.
     134                $this->assertEmpty( wp_cache_get( $term, 'wptests_tax' ) );
     135
     136                $num_queries = $wpdb->num_queries;
     137
     138                // Prime cache.
     139                $term_object = get_term( $term, 'wptests_tax' );
     140                $this->assertNotEmpty( wp_cache_get( $term, 'wptests_tax' ) );
     141                $this->assertSame( $num_queries + 1, $wpdb->num_queries );
     142
     143                $term_object_2 = get_term( $term, 'wptests_tax' );
     144
     145                // No new queries should have fired.
     146                $this->assertSame( $num_queries + 1, $wpdb->num_queries );
     147                $this->assertEquals( $term_object, $term_object_2 );
     148        }
     149
     150        public function test_get_term_by_should_update_term_cache_when_passed_a_valid_term_identifier() {
     151                global $wpdb;
     152
     153                register_taxonomy( 'wptests_tax', 'post' );
     154                $term = $this->factory->term->create( array(
     155                        'taxonomy' => 'wptests_tax',
     156                ) );
     157
     158                wp_cache_delete( $term, 'wptests_tax' );
     159
     160                // Affirm that the cache is empty.
     161                $this->assertEmpty( wp_cache_get( $term, 'wptests_tax' ) );
     162
     163                $num_queries = $wpdb->num_queries;
     164
     165                // Prime cache.
     166                $term_object = get_term_by( 'id', $term, 'wptests_tax' );
     167                $this->assertNotEmpty( wp_cache_get( $term, 'wptests_tax' ) );
     168                $this->assertSame( $num_queries + 1, $wpdb->num_queries );
     169
     170                $term_object_2 = get_term( $term, 'wptests_tax' );
     171
     172                // No new queries should have fired.
     173                $this->assertSame( $num_queries + 1, $wpdb->num_queries );
     174                $this->assertEquals( $term_object, $term_object_2 );
     175        }
     176
     177        /**
     178         * @ticket 30749
     179         */
     180        public function test_get_terms_should_update_cache_for_located_terms() {
     181                global $wpdb;
     182
     183                register_taxonomy( 'wptests_tax', 'post' );
     184
     185                $terms = $this->factory->term->create_many( 5, array(
     186                        'taxonomy' => 'wptests_tax',
     187                ) );
     188
     189                $term_objects = get_terms( 'wptests_tax', array(
     190                        'hide_empty' => false,
     191                ) );
     192
     193                $num_queries = $wpdb->num_queries;
     194
     195                foreach ( $terms as $term_id ) {
     196                        get_term( $term_id, 'wptests_tax' );
     197                }
     198
     199                $this->assertSame( $num_queries, $wpdb->num_queries );
     200
     201                _unregister_taxonomy( 'wptests_tax' );
     202        }
    96203}