Cache queries in get_page_by_path().

Props spacedmonkey.
Fixes #36711.

git-svn-id: https://develop.svn.wordpress.org/trunk@37479 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Boone Gorges
2016-05-21 17:26:55 +00:00
parent 70fece5c7e
commit 1a39a00dfd
2 changed files with 140 additions and 0 deletions

View File

@@ -123,4 +123,123 @@ class Tests_Post_GetPageByPath extends WP_UnitTestCase {
$this->assertNull( $found );
}
/**
* @ticket 36711
*/
public function test_should_hit_cache() {
global $wpdb;
$page = self::factory()->post->create( array(
'post_type' => 'page',
'post_name' => 'foo',
) );
// Prime cache.
$found = get_page_by_path( 'foo' );
$this->assertSame( $page, $found->ID );
$num_queries = $wpdb->num_queries;
$found = get_page_by_path( 'foo' );
$this->assertSame( $page, $found->ID );
$this->assertSame( $num_queries, $wpdb->num_queries );
}
/**
* @ticket 36711
*/
public function test_bad_path_should_be_cached() {
global $wpdb;
// Prime cache.
$found = get_page_by_path( 'foo' );
$this->assertNull( $found );
$num_queries = $wpdb->num_queries;
$found = get_page_by_path( 'foo' );
$this->assertNull( $found );
$this->assertSame( $num_queries, $wpdb->num_queries );
}
/**
* @ticket 36711
*/
public function test_bad_path_served_from_cache_should_not_fall_back_on_current_post() {
global $wpdb, $post;
// Fake the global.
$post = self::factory()->post->create_and_get();
// Prime cache.
$found = get_page_by_path( 'foo' );
$this->assertNull( $found );
$num_queries = $wpdb->num_queries;
$found = get_page_by_path( 'foo' );
$this->assertNull( $found );
$this->assertSame( $num_queries, $wpdb->num_queries );
unset( $post );
}
/**
* @ticket 36711
*/
public function test_cache_should_not_match_post_in_different_post_type_with_same_path() {
global $wpdb;
register_post_type( 'wptests_pt' );
$p1 = self::factory()->post->create( array(
'post_type' => 'page',
'post_name' => 'foo',
) );
$p2 = self::factory()->post->create( array(
'post_type' => 'wptests_pt',
'post_name' => 'foo',
) );
// Prime cache for the page.
$found = get_page_by_path( 'foo' );
$this->assertSame( $p1, $found->ID );
$num_queries = $wpdb->num_queries;
$found = get_page_by_path( 'foo', OBJECT, 'wptests_pt' );
$this->assertSame( $p2, $found->ID );
$num_queries++;
$this->assertSame( $num_queries, $wpdb->num_queries );
}
/**
* @ticket 36711
*/
public function test_cache_should_be_invalidated_when_post_name_is_edited() {
global $wpdb;
$page = self::factory()->post->create( array(
'post_type' => 'page',
'post_name' => 'foo',
) );
// Prime cache.
$found = get_page_by_path( 'foo' );
$this->assertSame( $page, $found->ID );
wp_update_post( array(
'ID' => $page,
'post_name' => 'bar',
) );
$num_queries = $wpdb->num_queries;
$found = get_page_by_path( 'bar' );
$this->assertSame( $page, $found->ID );
$num_queries++;
$this->assertSame( $num_queries, $wpdb->num_queries );
}
}