Make WordPress Core


Ignore:
Timestamp:
02/06/2014 01:58:01 AM (11 years ago)
Author:
wonderboymusic
Message:

Regenerate the term hierarchy cache ({taxonomy}_children) when it is out of sync with the passed taxonomy's last_changed value.

Introduces taxonomy_hierarchy_is_fresh(), which is only called in _get_term_hierarchy(). The taxonomy's last_changed value is checked against the value of wp_cache_get( 'hierarchy_last_changed', $taxonomy ).

Adds a unit test - Tests_Term:test_hierachy_invalidation().

See [27101], which makes this type of cache invalidation possible.
Fixes #14485.

File:
1 edited

Legend:

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

    r27101 r27102  
    586586        $this->assertNotEquals( $last_changed2, $last_changed3 );
    587587    }
    588    
     588
    589589    /**
    590590     * @ticket 22526
     
    635635        $this->assertNotEquals( $term->name, reset( $cats2 )->name );
    636636    }
     637
     638    function test_hierachy_invalidation() {
     639        $tax = 'burrito';
     640        register_taxonomy( $tax, 'post', array( 'hierarchical' => true ) );
     641        $this->assertTrue( get_taxonomy( $tax )->hierarchical );
     642
     643        $step = 1;
     644        $parent_id = 0;
     645        $children = 0;
     646
     647        foreach ( range( 1, 99 ) as $i ) {
     648            switch ( $step ) {
     649            case 1:
     650                $parent = wp_insert_term( 'Parent' . $i, $tax );
     651                $parent_id = $parent['term_id'];
     652                break;
     653            case 2:
     654                $parent = wp_insert_term( 'Child' . $i, $tax, array( 'parent' => $parent_id ) );
     655                $parent_id = $parent['term_id'];
     656                $children++;
     657                break;
     658            case 3:
     659                wp_insert_term( 'Grandchild' . $i, $tax, array( 'parent' => $parent_id ) );
     660                $parent_id = 0;
     661                $children++;
     662                break;
     663            }
     664
     665            $terms = get_terms( $tax, array( 'hide_empty' => false ) );
     666            $this->assertEquals( $i, count( $terms ) );
     667            if ( 1 < $i ) {
     668                $hierarchy = _get_term_hierarchy( $tax );
     669                $this->assertNotEmpty( $hierarchy );
     670                $this->assertEquals( $children, count( $hierarchy, COUNT_RECURSIVE ) - count( $hierarchy ) );
     671            }
     672
     673            if ( $i % 3 === 0 ) {
     674                $step = 1;
     675            } else {
     676                $step++;
     677            }
     678        }
     679
     680        _unregister_taxonomy( $tax );
     681    }
    637682}
Note: See TracChangeset for help on using the changeset viewer.