Streamline some get_terms() cache tests.

* Split large method into a number of smaller tests.
* Create fewer fixtures.

See #30017.

git-svn-id: https://develop.svn.wordpress.org/trunk@30113 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Boone Gorges 2014-10-30 04:15:03 +00:00
parent df5e0a00b8
commit f59f32104a

View File

@ -14,21 +14,16 @@ class Tests_Term_getTerms extends WP_UnitTestCase {
/**
* @ticket 23326
*/
function test_get_terms_cache() {
public function test_get_terms_cache() {
global $wpdb;
$posts = $this->factory->post->create_many( 15, array( 'post_type' => 'post' ) );
foreach ( $posts as $post )
wp_set_object_terms( $post, rand_str(), 'post_tag' );
wp_cache_delete( 'last_changed', 'terms' );
$this->assertFalse( wp_cache_get( 'last_changed', 'terms' ) );
$this->set_up_three_posts_and_tags();
$num_queries = $wpdb->num_queries;
// last_changed and num_queries should bump
$terms = get_terms( 'post_tag' );
$this->assertEquals( 15, count( $terms ) );
$this->assertEquals( 3, count( $terms ) );
$time1 = wp_cache_get( 'last_changed', 'terms' );
$this->assertNotEmpty( $time1 );
$this->assertEquals( $num_queries + 1, $wpdb->num_queries );
@ -37,36 +32,61 @@ class Tests_Term_getTerms extends WP_UnitTestCase {
// Again. last_changed and num_queries should remain the same.
$terms = get_terms( 'post_tag' );
$this->assertEquals( 15, count( $terms ) );
$this->assertEquals( 3, count( $terms ) );
$this->assertEquals( $time1, wp_cache_get( 'last_changed', 'terms' ) );
$this->assertEquals( $num_queries, $wpdb->num_queries );
}
/**
* @ticket 23326
*/
public function test_get_terms_cache_should_be_missed_when_passing_number() {
global $wpdb;
$this->set_up_three_posts_and_tags();
// Prime cache
$terms = get_terms( 'post_tag' );
$time1 = wp_cache_get( 'last_changed', 'terms' );
$num_queries = $wpdb->num_queries;
// Different query. num_queries should bump, last_changed should remain the same.
$terms = get_terms( 'post_tag', array( 'number' => 10 ) );
$this->assertEquals( 10, count( $terms ) );
// num_queries should bump, last_changed should remain the same.
$terms = get_terms( 'post_tag', array( 'number' => 2 ) );
$this->assertEquals( 2, count( $terms ) );
$this->assertEquals( $time1, wp_cache_get( 'last_changed', 'terms' ) );
$this->assertEquals( $num_queries + 1, $wpdb->num_queries );
$num_queries = $wpdb->num_queries;
// Again. last_changed and num_queries should remain the same.
$terms = get_terms( 'post_tag', array( 'number' => 10 ) );
$this->assertEquals( 10, count( $terms ) );
$terms = get_terms( 'post_tag', array( 'number' => 2 ) );
$this->assertEquals( 2, count( $terms ) );
$this->assertEquals( $time1, wp_cache_get( 'last_changed', 'terms' ) );
$this->assertEquals( $num_queries, $wpdb->num_queries );
}
// Force last_changed to bump
/**
* @ticket 23326
*/
public function test_wp_delete_term_should_invalidate_cache() {
global $wpdb;
$this->set_up_three_posts_and_tags();
// Prime cache
$terms = get_terms( 'post_tag' );
$time1 = wp_cache_get( 'last_changed', 'terms' );
$num_queries = $wpdb->num_queries;
// Force last_changed to bump.
wp_delete_term( $terms[0]->term_id, 'post_tag' );
$num_queries = $wpdb->num_queries;
$this->assertNotEquals( $time1, $time2 = wp_cache_get( 'last_changed', 'terms' ) );
// last_changed and num_queries should bump after a term is deleted
// last_changed and num_queries should bump after a term is deleted.
$terms = get_terms( 'post_tag' );
$this->assertEquals( 14, count( $terms ) );
$this->assertEquals( 2, count( $terms ) );
$this->assertEquals( $time2, wp_cache_get( 'last_changed', 'terms' ) );
$this->assertEquals( $num_queries + 1, $wpdb->num_queries );
@ -74,7 +94,7 @@ class Tests_Term_getTerms extends WP_UnitTestCase {
// Again. last_changed and num_queries should remain the same.
$terms = get_terms( 'post_tag' );
$this->assertEquals( 14, count( $terms ) );
$this->assertEquals( 2, count( $terms ) );
$this->assertEquals( $time2, wp_cache_get( 'last_changed', 'terms' ) );
$this->assertEquals( $num_queries, $wpdb->num_queries );
@ -1052,4 +1072,13 @@ class Tests_Term_getTerms extends WP_UnitTestCase {
'seven_term' => $seven_term
);
}
protected function set_up_three_posts_and_tags() {
$posts = $this->factory->post->create_many( 3, array( 'post_type' => 'post' ) );
foreach ( $posts as $post ) {
wp_set_object_terms( $post, rand_str(), 'post_tag' );
}
wp_cache_delete( 'last_changed', 'terms' );
}
}