wordpress-develop/tests/phpunit/tests/post/updatePostCache.php
Peter Wilson b1b305b826 Posts, Post Types: Set post filter in update_post_cache().
Ensure the post cache is primed with raw sanitized data. This resolves an inconsistency between how posts retrieved via `get_post()` vs `WP_Query` are cached.

This prevents `sanitize_post( $post, 'raw' )` being run multiple times on a cached post. This can happen over 20 times per post on some page loads so avoiding this will provide a noticeable performance boost.

Props Cybr, SergeyBiryukov, peterwilsoncc, hellofromTonya, costdev.
Fixes #50567.



git-svn-id: https://develop.svn.wordpress.org/trunk@53042 602fd350-edb4-49c9-b593-d223f7449a82
2022-04-01 03:16:33 +00:00

142 lines
2.9 KiB
PHP

<?php
/**
* Test `wp_update_cache()`.
*
* @package WordPress
*/
/**
* Test class for `wp_update_cache()`.
*
* @group post
* @group query
*
* @covers ::update_post_cache
*/
class Tests_Post_UpdatePostCache extends WP_UnitTestCase {
/**
* Post IDs from the shared fixture.
*
* @var array
*/
public static $post_ids;
/**
* Set up test resources before the class.
*
* @param WP_UnitTest_Factory $factory The unit test factory.
*/
public static function wpSetupBeforeClass( WP_UnitTest_Factory $factory ) {
self::$post_ids = $factory->post->create_many( 1 );
}
/**
* Ensure that `update_post_cache()` returns `null` when
* `$posts` is empty.
*
* @ticket 50567
*/
public function test_should_return_null_with_an_empty_array() {
$posts = array();
$this->assertNull( update_post_cache( $posts ) );
}
/**
* Ensure filter = raw is always set via Query.
*
* @ticket 50567
*/
public function test_query_caches_post_filter() {
$post_id = self::$post_ids[0];
$this->go_to( '/' );
$cached_post = wp_cache_get( $post_id, 'posts' );
$this->assertIsObject(
$cached_post,
'The cached post is not an object'
);
$this->assertObjectHasAttribute(
'filter',
$cached_post,
'The cached post does not have a "filter" property'
);
$this->assertSame(
'raw',
$cached_post->filter,
'The filter is not set to "raw"'
);
}
/**
* Ensure filter = raw is always set via get_post.
*
* @ticket 50567
*/
public function test_get_post_caches_post_filter() {
$post_id = self::$post_ids[0];
get_post( $post_id );
$cached_post = wp_cache_get( $post_id, 'posts' );
$this->assertSame( 'raw', $cached_post->filter );
}
/**
* Ensure filter = raw is always set via get_post called with a different filter setting.
*
* @ticket 50567
*/
public function test_get_post_caches_post_filter_is_always_raw() {
$post_id = self::$post_ids[0];
get_post( $post_id, OBJECT, 'display' );
$cached_post = wp_cache_get( $post_id, 'posts' );
$this->assertIsObject(
$cached_post,
'The cached post is not an object'
);
$this->assertObjectHasAttribute(
'filter',
$cached_post,
'The cached post does not have a "filter" property'
);
$this->assertSame(
'raw',
$cached_post->filter,
'The filter is not set to "raw"'
);
}
/**
* Ensure filter = raw is always set via get_posts.
*
* @ticket 50567
*/
public function test_get_posts_caches_post_filter_is_always_raw() {
$post_id = self::$post_ids[0];
get_posts( array( 'includes' => $post_id ) );
$cached_post = wp_cache_get( $post_id, 'posts' );
$this->assertIsObject(
$cached_post,
'The cached post is not an object'
);
$this->assertObjectHasAttribute(
'filter',
$cached_post,
'The cached post does not have a "filter" property'
);
$this->assertSame(
'raw',
$cached_post->filter,
'The filter is not set to "raw"'
);
}
}