mirror of
https://github.com/gosticks/wordpress-develop.git
synced 2026-07-29 05:20:16 +00:00
Tests: Use the function get_num_queries across all unit tests.
Replace use of `$wpdb->num_queries` with a function call to `get_num_queries`. This improves readability and consistency between tests. Props SergeyBiryukov, peterwilsoncc, spacedmonkey. See #57841. git-svn-id: https://develop.svn.wordpress.org/trunk@55745 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
@@ -101,8 +101,6 @@ class Tests_Term_Cache extends WP_UnitTestCase {
|
||||
}
|
||||
|
||||
public function test_get_term_should_update_term_cache_when_passed_an_object() {
|
||||
global $wpdb;
|
||||
|
||||
register_taxonomy( 'wptests_tax', 'post' );
|
||||
$term = self::factory()->term->create(
|
||||
array(
|
||||
@@ -116,7 +114,7 @@ class Tests_Term_Cache extends WP_UnitTestCase {
|
||||
// Affirm that the cache is empty.
|
||||
$this->assertEmpty( wp_cache_get( $term, 'terms' ) );
|
||||
|
||||
$num_queries = $wpdb->num_queries;
|
||||
$num_queries = get_num_queries();
|
||||
|
||||
// get_term() will only be update the cache if the 'filter' prop is unset.
|
||||
unset( $term_object->filter );
|
||||
@@ -124,13 +122,11 @@ class Tests_Term_Cache extends WP_UnitTestCase {
|
||||
$term_object_2 = get_term( $term_object, 'wptests_tax' );
|
||||
|
||||
// No new queries should have fired.
|
||||
$this->assertSame( $num_queries, $wpdb->num_queries );
|
||||
$this->assertSame( $num_queries, get_num_queries() );
|
||||
$this->assertSame( $term_object, $term_object_2 );
|
||||
}
|
||||
|
||||
public function test_get_term_should_update_term_cache_when_passed_a_valid_term_identifier() {
|
||||
global $wpdb;
|
||||
|
||||
register_taxonomy( 'wptests_tax', 'post' );
|
||||
$term = self::factory()->term->create(
|
||||
array(
|
||||
@@ -143,23 +139,21 @@ class Tests_Term_Cache extends WP_UnitTestCase {
|
||||
// Affirm that the cache is empty.
|
||||
$this->assertEmpty( wp_cache_get( $term, 'terms' ) );
|
||||
|
||||
$num_queries = $wpdb->num_queries;
|
||||
$num_queries = get_num_queries();
|
||||
|
||||
// Prime cache.
|
||||
$term_object = get_term( $term, 'wptests_tax' );
|
||||
$this->assertNotEmpty( wp_cache_get( $term, 'terms' ) );
|
||||
$this->assertSame( $num_queries + 1, $wpdb->num_queries );
|
||||
$this->assertSame( $num_queries + 1, get_num_queries() );
|
||||
|
||||
$term_object_2 = get_term( $term, 'wptests_tax' );
|
||||
|
||||
// No new queries should have fired.
|
||||
$this->assertSame( $num_queries + 1, $wpdb->num_queries );
|
||||
$this->assertSame( $num_queries + 1, get_num_queries() );
|
||||
$this->assertEquals( $term_object, $term_object_2 );
|
||||
}
|
||||
|
||||
public function test_get_term_by_should_update_term_cache_when_passed_a_valid_term_identifier() {
|
||||
global $wpdb;
|
||||
|
||||
register_taxonomy( 'wptests_tax', 'post' );
|
||||
$term = self::factory()->term->create(
|
||||
array(
|
||||
@@ -172,17 +166,17 @@ class Tests_Term_Cache extends WP_UnitTestCase {
|
||||
// Affirm that the cache is empty.
|
||||
$this->assertEmpty( wp_cache_get( $term, 'terms' ) );
|
||||
|
||||
$num_queries = $wpdb->num_queries;
|
||||
$num_queries = get_num_queries();
|
||||
|
||||
// Prime cache.
|
||||
$term_object = get_term_by( 'id', $term, 'wptests_tax' );
|
||||
$this->assertNotEmpty( wp_cache_get( $term, 'terms' ) );
|
||||
$this->assertSame( $num_queries + 1, $wpdb->num_queries );
|
||||
$this->assertSame( $num_queries + 1, get_num_queries() );
|
||||
|
||||
$term_object_2 = get_term( $term, 'wptests_tax' );
|
||||
|
||||
// No new queries should have fired.
|
||||
$this->assertSame( $num_queries + 1, $wpdb->num_queries );
|
||||
$this->assertSame( $num_queries + 1, get_num_queries() );
|
||||
$this->assertEquals( $term_object, $term_object_2 );
|
||||
}
|
||||
|
||||
@@ -190,8 +184,6 @@ class Tests_Term_Cache extends WP_UnitTestCase {
|
||||
* @ticket 30749
|
||||
*/
|
||||
public function test_get_terms_should_update_cache_for_located_terms() {
|
||||
global $wpdb;
|
||||
|
||||
register_taxonomy( 'wptests_tax', 'post' );
|
||||
|
||||
$terms = self::factory()->term->create_many(
|
||||
@@ -208,13 +200,13 @@ class Tests_Term_Cache extends WP_UnitTestCase {
|
||||
)
|
||||
);
|
||||
|
||||
$num_queries = $wpdb->num_queries;
|
||||
$num_queries = get_num_queries();
|
||||
|
||||
foreach ( $terms as $term_id ) {
|
||||
get_term( $term_id, 'wptests_tax' );
|
||||
}
|
||||
|
||||
$this->assertSame( $num_queries, $wpdb->num_queries );
|
||||
$this->assertSame( $num_queries, get_num_queries() );
|
||||
|
||||
_unregister_taxonomy( 'wptests_tax' );
|
||||
}
|
||||
@@ -242,8 +234,6 @@ class Tests_Term_Cache extends WP_UnitTestCase {
|
||||
* @ticket 21760
|
||||
*/
|
||||
public function test_get_term_by_slug_cache() {
|
||||
global $wpdb;
|
||||
|
||||
$term_id = self::factory()->term->create(
|
||||
array(
|
||||
'slug' => 'burrito',
|
||||
@@ -253,28 +243,26 @@ class Tests_Term_Cache extends WP_UnitTestCase {
|
||||
);
|
||||
|
||||
clean_term_cache( $term_id, 'post_tag' );
|
||||
$num_queries = $wpdb->num_queries;
|
||||
$num_queries = get_num_queries();
|
||||
|
||||
$term = get_term_by( 'slug', 'burrito', 'post_tag' );
|
||||
$num_queries = $num_queries + 2;
|
||||
$this->assertSame( 'Taco', $term->name );
|
||||
$this->assertSame( $num_queries, $wpdb->num_queries );
|
||||
$this->assertSame( $num_queries, get_num_queries() );
|
||||
|
||||
// This should now hit cache.
|
||||
$term = get_term_by( 'slug', 'burrito', 'post_tag' );
|
||||
$this->assertSame( 'Taco', $term->name );
|
||||
$this->assertSame( $num_queries, $wpdb->num_queries );
|
||||
$this->assertSame( $num_queries, get_num_queries() );
|
||||
|
||||
$this->assertEquals( get_term( $term_id, 'post_tag' ), $term );
|
||||
$this->assertSame( $num_queries, $wpdb->num_queries );
|
||||
$this->assertSame( $num_queries, get_num_queries() );
|
||||
}
|
||||
|
||||
/**
|
||||
* @ticket 21760
|
||||
*/
|
||||
public function test_get_term_by_slug_cache_update() {
|
||||
global $wpdb;
|
||||
|
||||
$term_id = self::factory()->term->create(
|
||||
array(
|
||||
'slug' => 'burrito',
|
||||
@@ -284,35 +272,33 @@ class Tests_Term_Cache extends WP_UnitTestCase {
|
||||
);
|
||||
|
||||
clean_term_cache( $term_id, 'post_tag' );
|
||||
$num_queries = $wpdb->num_queries;
|
||||
$num_queries = get_num_queries();
|
||||
|
||||
$term = get_term_by( 'slug', 'burrito', 'post_tag' );
|
||||
$num_queries = $num_queries + 2;
|
||||
$this->assertSame( 'Taco', $term->name );
|
||||
$this->assertSame( $num_queries, $wpdb->num_queries );
|
||||
$this->assertSame( $num_queries, get_num_queries() );
|
||||
|
||||
// This should now hit cache.
|
||||
$term = get_term_by( 'slug', 'burrito', 'post_tag' );
|
||||
$this->assertSame( 'Taco', $term->name );
|
||||
$this->assertSame( $num_queries, $wpdb->num_queries );
|
||||
$this->assertSame( $num_queries, get_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;
|
||||
$num_queries = get_num_queries();
|
||||
|
||||
// This should not hit cache.
|
||||
$term = get_term_by( 'slug', 'burrito', 'post_tag' );
|
||||
$num_queries = $num_queries + 2;
|
||||
$this->assertSame( 'No Taco', $term->name );
|
||||
$this->assertSame( $num_queries, $wpdb->num_queries );
|
||||
$this->assertSame( $num_queries, get_num_queries() );
|
||||
}
|
||||
|
||||
/**
|
||||
* @ticket 21760
|
||||
*/
|
||||
public function test_get_term_by_name_cache() {
|
||||
global $wpdb;
|
||||
|
||||
$term_id = self::factory()->term->create(
|
||||
array(
|
||||
'name' => 'Burrito',
|
||||
@@ -322,26 +308,24 @@ class Tests_Term_Cache extends WP_UnitTestCase {
|
||||
);
|
||||
|
||||
clean_term_cache( $term_id, 'post_tag' );
|
||||
$num_queries = $wpdb->num_queries;
|
||||
$num_queries = get_num_queries();
|
||||
|
||||
get_term_by( 'name', 'Burrito', 'post_tag' );
|
||||
$num_queries = $num_queries + 2;
|
||||
$this->assertSame( $num_queries, $wpdb->num_queries );
|
||||
$this->assertSame( $num_queries, get_num_queries() );
|
||||
|
||||
// This should now hit cache.
|
||||
$term = get_term_by( 'name', 'Burrito', 'post_tag' );
|
||||
$this->assertSame( $num_queries, $wpdb->num_queries );
|
||||
$this->assertSame( $num_queries, get_num_queries() );
|
||||
|
||||
$this->assertEquals( get_term( $term_id, 'post_tag' ), $term );
|
||||
$this->assertSame( $num_queries, $wpdb->num_queries );
|
||||
$this->assertSame( $num_queries, get_num_queries() );
|
||||
}
|
||||
|
||||
/**
|
||||
* @ticket 21760
|
||||
*/
|
||||
public function test_get_term_by_name_cache_update() {
|
||||
global $wpdb;
|
||||
|
||||
$term_id = self::factory()->term->create(
|
||||
array(
|
||||
'name' => 'Burrito',
|
||||
@@ -351,32 +335,30 @@ class Tests_Term_Cache extends WP_UnitTestCase {
|
||||
);
|
||||
|
||||
clean_term_cache( $term_id, 'post_tag' );
|
||||
$num_queries = $wpdb->num_queries;
|
||||
$num_queries = get_num_queries();
|
||||
|
||||
get_term_by( 'name', 'Burrito', 'post_tag' );
|
||||
$num_queries = $num_queries + 2;
|
||||
$this->assertSame( $num_queries, $wpdb->num_queries );
|
||||
$this->assertSame( $num_queries, get_num_queries() );
|
||||
|
||||
// This should now hit cache.
|
||||
get_term_by( 'name', 'Burrito', 'post_tag' );
|
||||
$this->assertSame( $num_queries, $wpdb->num_queries );
|
||||
$this->assertSame( $num_queries, get_num_queries() );
|
||||
|
||||
// Update the tag which invalidates the cache.
|
||||
wp_update_term( $term_id, 'post_tag', array( 'slug' => 'taco' ) );
|
||||
$num_queries = $wpdb->num_queries;
|
||||
$num_queries = get_num_queries();
|
||||
|
||||
// This should not hit cache.
|
||||
get_term_by( 'name', 'burrito', 'post_tag' );
|
||||
$num_queries = $num_queries + 2;
|
||||
$this->assertSame( $num_queries, $wpdb->num_queries );
|
||||
$this->assertSame( $num_queries, get_num_queries() );
|
||||
}
|
||||
|
||||
/**
|
||||
* @ticket 21760
|
||||
*/
|
||||
public function test_invalidating_term_caches_should_fail_when_invalidation_is_suspended() {
|
||||
global $wpdb;
|
||||
|
||||
$term_id = self::factory()->term->create(
|
||||
array(
|
||||
'name' => 'Burrito',
|
||||
@@ -385,7 +367,7 @@ class Tests_Term_Cache extends WP_UnitTestCase {
|
||||
);
|
||||
|
||||
clean_term_cache( $term_id, 'post_tag' );
|
||||
$num_queries = $wpdb->num_queries;
|
||||
$num_queries = get_num_queries();
|
||||
$last_changed = wp_cache_get( 'last_changed', 'terms' );
|
||||
|
||||
$term1 = get_term_by( 'name', 'Burrito', 'post_tag' );
|
||||
@@ -393,18 +375,18 @@ class Tests_Term_Cache extends WP_UnitTestCase {
|
||||
|
||||
// Verify the term is cached.
|
||||
$term2 = get_term_by( 'name', 'Burrito', 'post_tag' );
|
||||
$this->assertSame( $num_queries, $wpdb->num_queries );
|
||||
$this->assertSame( $num_queries, get_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;
|
||||
$num_queries = get_num_queries();
|
||||
|
||||
// Verify that the cached term still matches the initial cached term.
|
||||
$term3 = get_term_by( 'name', 'Burrito', 'post_tag' );
|
||||
$this->assertSame( $num_queries, $wpdb->num_queries );
|
||||
$this->assertSame( $num_queries, get_num_queries() );
|
||||
$this->assertEquals( $term1, $term3 );
|
||||
|
||||
// Verify that last changed has not been updated as part of an invalidation routine.
|
||||
@@ -418,8 +400,6 @@ class Tests_Term_Cache extends WP_UnitTestCase {
|
||||
* @ticket 21760
|
||||
*/
|
||||
public function test_get_term_by_does_not_prime_term_meta_cache() {
|
||||
global $wpdb;
|
||||
|
||||
$term_id = self::factory()->term->create(
|
||||
array(
|
||||
'name' => 'Burrito',
|
||||
@@ -429,18 +409,18 @@ class Tests_Term_Cache extends WP_UnitTestCase {
|
||||
add_term_meta( $term_id, 'foo', 'bar' );
|
||||
|
||||
clean_term_cache( $term_id, 'post_tag' );
|
||||
$num_queries = $wpdb->num_queries;
|
||||
$num_queries = get_num_queries();
|
||||
|
||||
$term = get_term_by( 'name', 'Burrito', 'post_tag' );
|
||||
$num_queries = $num_queries + 2;
|
||||
$this->assertInstanceOf( 'WP_Term', $term );
|
||||
$this->assertSame( $term_id, $term->term_id );
|
||||
$this->assertSame( $num_queries, $wpdb->num_queries );
|
||||
$this->assertSame( $num_queries, get_num_queries() );
|
||||
|
||||
$term_meta = get_term_meta( $term_id, 'foo', true );
|
||||
$num_queries++;
|
||||
$this->assertSame( $term_meta, 'bar' );
|
||||
$this->assertSame( $num_queries, $wpdb->num_queries );
|
||||
$this->assertSame( $num_queries, get_num_queries() );
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user