mirror of
https://github.com/gosticks/wordpress-develop.git
synced 2026-07-04 01:00:04 +00:00
Taxonomy: Use WP_Term_Query in get_term_by().
`WP_Term_Query` already supports querying terms by 'slug', 'name', and 'term_taxonomy_id'. Its additional arguments allow us to generate nearly the same SQL queries as before. This change has one yuge benefit: the term queries are now cached. Add tests to increase coverage of `get_term_by()`. Props spacedmonkey, boonebgorges, johnjamesjacoby, pento, ocean90. Fixes #21760. git-svn-id: https://develop.svn.wordpress.org/trunk@38677 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
@@ -219,4 +219,175 @@ class Tests_Term_Cache extends WP_UnitTestCase {
|
||||
$this->assertSame( $p, $term->object_id );
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @ticket 21760
|
||||
*/
|
||||
function test_get_term_by_slug_cache() {
|
||||
global $wpdb;
|
||||
|
||||
$term_id = $this->factory->term->create( array( 'slug' => 'burrito', 'name' => 'Taco', 'taxonomy' => 'post_tag' ) );
|
||||
|
||||
clean_term_cache( $term_id, 'post_tag' );
|
||||
$num_queries = $wpdb->num_queries;
|
||||
|
||||
$term = get_term_by( 'slug', 'burrito', 'post_tag' );
|
||||
$num_queries++;
|
||||
$this->assertEquals( 'Taco', $term->name );
|
||||
$this->assertEquals( $num_queries, $wpdb->num_queries );
|
||||
|
||||
// This should now hit cache.
|
||||
$term = get_term_by( 'slug', 'burrito', 'post_tag' );
|
||||
$this->assertEquals( 'Taco', $term->name );
|
||||
$this->assertEquals( $num_queries, $wpdb->num_queries );
|
||||
|
||||
$this->assertEquals( get_term( $term_id, 'post_tag' ), $term );
|
||||
$this->assertEquals( $num_queries, $wpdb->num_queries );
|
||||
}
|
||||
|
||||
/**
|
||||
* @ticket 21760
|
||||
*/
|
||||
function test_get_term_by_slug_cache_update() {
|
||||
global $wpdb;
|
||||
|
||||
$term_id = $this->factory->term->create( array( 'slug' => 'burrito', 'name' => 'Taco', 'taxonomy' => 'post_tag' ) );
|
||||
|
||||
clean_term_cache( $term_id, 'post_tag' );
|
||||
$num_queries = $wpdb->num_queries;
|
||||
|
||||
$term = get_term_by( 'slug', 'burrito', 'post_tag' );
|
||||
$num_queries++;
|
||||
$this->assertEquals( 'Taco', $term->name );
|
||||
$this->assertEquals( $num_queries, $wpdb->num_queries );
|
||||
|
||||
// This should now hit cache.
|
||||
$term = get_term_by( 'slug', 'burrito', 'post_tag' );
|
||||
$this->assertEquals( 'Taco', $term->name );
|
||||
$this->assertEquals( $num_queries, $wpdb->num_queries );
|
||||
|
||||
// Update the tag which invalidates the cache.
|
||||
wp_update_term( $term_id, 'post_tag', array( 'name' => 'No Taco' ) );
|
||||
$num_queries = $wpdb->num_queries;
|
||||
|
||||
// This should not hit cache.
|
||||
$term = get_term_by( 'slug', 'burrito', 'post_tag' );
|
||||
$num_queries++;
|
||||
$this->assertEquals( 'No Taco', $term->name );
|
||||
$this->assertEquals( $num_queries, $wpdb->num_queries );
|
||||
}
|
||||
|
||||
/**
|
||||
* @ticket 21760
|
||||
*/
|
||||
function test_get_term_by_name_cache() {
|
||||
global $wpdb;
|
||||
|
||||
$term_id = $this->factory->term->create( array( 'name' => 'Burrito', 'slug' => 'noburrito', 'taxonomy' => 'post_tag' ) );
|
||||
|
||||
clean_term_cache( $term_id, 'post_tag' );
|
||||
$num_queries = $wpdb->num_queries;
|
||||
|
||||
get_term_by( 'name', 'Burrito', 'post_tag' );
|
||||
$num_queries++;
|
||||
$this->assertEquals( $num_queries, $wpdb->num_queries );
|
||||
|
||||
// This should now hit cache.
|
||||
$term = get_term_by( 'name', 'Burrito', 'post_tag' );
|
||||
$this->assertEquals( $num_queries, $wpdb->num_queries );
|
||||
|
||||
$this->assertEquals( get_term( $term_id, 'post_tag' ), $term );
|
||||
$this->assertEquals( $num_queries, $wpdb->num_queries );
|
||||
}
|
||||
|
||||
/**
|
||||
* @ticket 21760
|
||||
*/
|
||||
function test_get_term_by_name_cache_update() {
|
||||
global $wpdb;
|
||||
|
||||
$term_id = $this->factory->term->create( array( 'name' => 'Burrito', 'slug' => 'noburrito', 'taxonomy' => 'post_tag' ) );
|
||||
|
||||
clean_term_cache( $term_id, 'post_tag' );
|
||||
$num_queries = $wpdb->num_queries;
|
||||
|
||||
get_term_by( 'name', 'Burrito', 'post_tag' );
|
||||
$num_queries++;
|
||||
$this->assertEquals( $num_queries, $wpdb->num_queries );
|
||||
|
||||
// This should now hit cache.
|
||||
get_term_by( 'name', 'Burrito', 'post_tag' );
|
||||
$this->assertEquals( $num_queries, $wpdb->num_queries );
|
||||
|
||||
// Update the tag which invalidates the cache.
|
||||
wp_update_term( $term_id, 'post_tag', array( 'slug' => 'taco' ) );
|
||||
$num_queries = $wpdb->num_queries;
|
||||
|
||||
// This should not hit cache.
|
||||
get_term_by( 'name', 'burrito', 'post_tag' );
|
||||
$num_queries++;
|
||||
$this->assertEquals( $num_queries, $wpdb->num_queries );
|
||||
}
|
||||
|
||||
/**
|
||||
* @ticket 21760
|
||||
*/
|
||||
function test_invalidating_term_caches_should_fail_when_invalidation_is_suspended() {
|
||||
global $wpdb;
|
||||
|
||||
$term_id = $this->factory->term->create( array( 'name' => 'Burrito', 'taxonomy' => 'post_tag' ) );
|
||||
|
||||
clean_term_cache( $term_id, 'post_tag' );
|
||||
$num_queries = $wpdb->num_queries;
|
||||
$last_changed = wp_cache_get( 'last_changed', 'terms' );
|
||||
|
||||
$term1 = get_term_by( 'name', 'Burrito', 'post_tag' );
|
||||
$num_queries++;
|
||||
|
||||
// Verify the term is cached.
|
||||
$term2 = get_term_by( 'name', 'Burrito', 'post_tag' );
|
||||
$this->assertEquals( $num_queries, $wpdb->num_queries );
|
||||
$this->assertEquals( $term1, $term2 );
|
||||
|
||||
$suspend = wp_suspend_cache_invalidation();
|
||||
|
||||
// Update the tag.
|
||||
wp_update_term( $term_id, 'post_tag', array( 'name' => 'Taco' ) );
|
||||
$num_queries = $wpdb->num_queries;
|
||||
|
||||
// Verify that the cached term still matches the initial cached term.
|
||||
$term3 = get_term_by( 'name', 'Burrito', 'post_tag' );
|
||||
$this->assertEquals( $num_queries, $wpdb->num_queries );
|
||||
$this->assertEquals( $term1, $term3 );
|
||||
|
||||
// Verify that last changed has not been updated as part of an invalidation routine.
|
||||
$this->assertSame( $last_changed, wp_cache_get( 'last_changed', 'terms' ) );
|
||||
|
||||
// Clean up.
|
||||
wp_suspend_cache_invalidation( $suspend );
|
||||
}
|
||||
|
||||
/**
|
||||
* @ticket 21760
|
||||
*/
|
||||
public function test_get_term_by_does_not_prime_term_meta_cache() {
|
||||
global $wpdb;
|
||||
|
||||
$term_id = $this->factory->term->create( array( 'name' => 'Burrito', 'taxonomy' => 'post_tag' ) );
|
||||
add_term_meta( $term_id, 'foo', 'bar' );
|
||||
|
||||
clean_term_cache( $term_id, 'post_tag' );
|
||||
$num_queries = $wpdb->num_queries;
|
||||
|
||||
$term = get_term_by( 'name', 'Burrito', 'post_tag' );
|
||||
$num_queries++;
|
||||
$this->assertTrue( $term instanceof WP_Term );
|
||||
$this->assertSame( $term_id, $term->term_id );
|
||||
$this->assertEquals( $num_queries, $wpdb->num_queries );
|
||||
|
||||
$term_meta = get_term_meta( $term_id, 'foo', true );
|
||||
$num_queries++;
|
||||
$this->assertSame( $term_meta, 'bar' );
|
||||
$this->assertEquals( $num_queries, $wpdb->num_queries );
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user