From fafe0b8c2bc764b9e7277d7b6b66a308b1697d82 Mon Sep 17 00:00:00 2001 From: Peter Wilson <519727+peterwilsoncc@users.noreply.github.com> Date: Mon, 24 Oct 2022 15:36:43 +1100 Subject: [PATCH 1/4] Add message to each assertion. --- tests/phpunit/tests/theme/wpThemeJsonResolver.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/tests/phpunit/tests/theme/wpThemeJsonResolver.php b/tests/phpunit/tests/theme/wpThemeJsonResolver.php index 9e7e26cae2b76..133bfc2a2aa15 100644 --- a/tests/phpunit/tests/theme/wpThemeJsonResolver.php +++ b/tests/phpunit/tests/theme/wpThemeJsonResolver.php @@ -632,16 +632,16 @@ function test_get_user_data_from_wp_global_styles_does_not_use_uncached_queries( $this->assertSame( 0, $query_count, 'Unexpected SQL queries detected for the wp_global_style post type' ); $user_cpt = WP_Theme_JSON_Resolver::get_user_data_from_wp_global_styles( $theme ); - $this->assertEmpty( $user_cpt ); + $this->assertEmpty( $user_cpt, 'User CPT is expected to be empty.' ); $user_cpt = WP_Theme_JSON_Resolver::get_user_data_from_wp_global_styles( $theme, true ); - $this->assertNotEmpty( $user_cpt ); + $this->assertNotEmpty( $user_cpt, 'User CPT is expected not to be empty.' ); $query_count = count( $this->queries ); for ( $i = 0; $i < 3; $i ++ ) { $new_user_cpt = WP_Theme_JSON_Resolver::get_user_data_from_wp_global_styles( $theme ); WP_Theme_JSON_Resolver::clean_cached_data(); - $this->assertSameSets( $user_cpt, $new_user_cpt ); + $this->assertSameSets( $user_cpt, $new_user_cpt, "User CPTs do not match on run {$i}." ); } $query_count = count( $this->queries ) - $query_count; $this->assertSame( 0, $query_count, 'Unexpected SQL queries detected for the wp_global_style post type' ); From b75ab8172011c27c656501e036af1b1862f3b502 Mon Sep 17 00:00:00 2001 From: Peter Wilson <519727+peterwilsoncc@users.noreply.github.com> Date: Mon, 24 Oct 2022 14:49:45 +1100 Subject: [PATCH 2/4] Use low-level cache for get_user_data_from_wp_global_styles(). --- src/wp-includes/class-wp-theme-json-resolver.php | 13 ------------- 1 file changed, 13 deletions(-) diff --git a/src/wp-includes/class-wp-theme-json-resolver.php b/src/wp-includes/class-wp-theme-json-resolver.php index 18b997561cf05..ee153eb657abe 100644 --- a/src/wp-includes/class-wp-theme-json-resolver.php +++ b/src/wp-includes/class-wp-theme-json-resolver.php @@ -423,17 +423,6 @@ public static function get_user_data_from_wp_global_styles( $theme, $create_post ), ); - $cache_key = sprintf( 'wp_global_styles_%s', md5( serialize( $args ) ) ); - $post_id = (int) get_transient( $cache_key ); - // Special case: '-1' is a results not found. - if ( -1 === $post_id && ! $create_post ) { - return $user_cpt; - } - - if ( $post_id > 0 && in_array( get_post_status( $post_id ), (array) $post_status_filter, true ) ) { - return get_post( $post_id, ARRAY_A ); - } - $global_style_query = new WP_Query(); $recent_posts = $global_style_query->query( $args ); if ( count( $recent_posts ) === 1 ) { @@ -456,8 +445,6 @@ public static function get_user_data_from_wp_global_styles( $theme, $create_post $user_cpt = get_post( $cpt_post_id, ARRAY_A ); } } - $cache_expiration = $user_cpt ? DAY_IN_SECONDS : HOUR_IN_SECONDS; - set_transient( $cache_key, $user_cpt ? $user_cpt['ID'] : -1, $cache_expiration ); return $user_cpt; } From a60b5b963cbb2e51eb456ff7c0d5df2fe5b6b64c Mon Sep 17 00:00:00 2001 From: Peter Wilson <519727+peterwilsoncc@users.noreply.github.com> Date: Tue, 25 Oct 2022 09:59:02 +1100 Subject: [PATCH 3/4] Global styles require an admin account to create taxo. --- .../tests/theme/wpThemeJsonResolver.php | 20 ++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-) diff --git a/tests/phpunit/tests/theme/wpThemeJsonResolver.php b/tests/phpunit/tests/theme/wpThemeJsonResolver.php index 133bfc2a2aa15..e5aeab45b5f83 100644 --- a/tests/phpunit/tests/theme/wpThemeJsonResolver.php +++ b/tests/phpunit/tests/theme/wpThemeJsonResolver.php @@ -12,6 +12,13 @@ */ class Tests_Theme_wpThemeJsonResolver extends WP_UnitTestCase { + /** + * Administrator ID. + * + * @var int + */ + protected static $administrator_id; + /** * Theme root directory. * @@ -64,6 +71,13 @@ class Tests_Theme_wpThemeJsonResolver extends WP_UnitTestCase { public static function set_up_before_class() { parent::set_up_before_class(); + self::$administrator_id = self::factory()->user->create( + array( + 'role' => 'administrator', + 'user_email' => 'administrator@example.com', + ) + ); + static::$property_blocks_cache = new ReflectionProperty( WP_Theme_JSON_Resolver::class, 'blocks_cache' ); static::$property_blocks_cache->setAccessible( true ); static::$property_blocks_cache_orig_value = static::$property_blocks_cache->getValue(); @@ -620,6 +634,7 @@ function test_merges_child_theme_json_into_parent_theme_json() { * @covers WP_Theme_JSON_Resolver::get_user_data_from_wp_global_styles */ function test_get_user_data_from_wp_global_styles_does_not_use_uncached_queries() { + wp_set_current_user( self::$administrator_id ); $theme = wp_get_theme(); WP_Theme_JSON_Resolver::get_user_data_from_wp_global_styles( $theme ); add_filter( 'query', array( $this, 'filter_db_query' ) ); @@ -629,7 +644,7 @@ function test_get_user_data_from_wp_global_styles_does_not_use_uncached_queries( WP_Theme_JSON_Resolver::clean_cached_data(); } $query_count = count( $this->queries ) - $query_count; - $this->assertSame( 0, $query_count, 'Unexpected SQL queries detected for the wp_global_style post type' ); + $this->assertSame( 0, $query_count, 'Unexpected SQL queries detected for the wp_global_style post type prior to creation.' ); $user_cpt = WP_Theme_JSON_Resolver::get_user_data_from_wp_global_styles( $theme ); $this->assertEmpty( $user_cpt, 'User CPT is expected to be empty.' ); @@ -644,8 +659,7 @@ function test_get_user_data_from_wp_global_styles_does_not_use_uncached_queries( $this->assertSameSets( $user_cpt, $new_user_cpt, "User CPTs do not match on run {$i}." ); } $query_count = count( $this->queries ) - $query_count; - $this->assertSame( 0, $query_count, 'Unexpected SQL queries detected for the wp_global_style post type' ); - remove_filter( 'query', array( $this, 'filter_db_query' ) ); + $this->assertSame( 1, $query_count, 'Unexpected SQL queries detected for the wp_global_style post type after creation.' ); } /** From c1faaa750c365e3cabfe90be8507d7e2ff808b14 Mon Sep 17 00:00:00 2001 From: Peter Wilson <519727+peterwilsoncc@users.noreply.github.com> Date: Tue, 25 Oct 2022 09:59:17 +1100 Subject: [PATCH 4/4] Test for cached queries with logged out users. --- .../tests/theme/wpThemeJsonResolver.php | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/tests/phpunit/tests/theme/wpThemeJsonResolver.php b/tests/phpunit/tests/theme/wpThemeJsonResolver.php index e5aeab45b5f83..524fade2d8f85 100644 --- a/tests/phpunit/tests/theme/wpThemeJsonResolver.php +++ b/tests/phpunit/tests/theme/wpThemeJsonResolver.php @@ -662,6 +662,25 @@ function test_get_user_data_from_wp_global_styles_does_not_use_uncached_queries( $this->assertSame( 1, $query_count, 'Unexpected SQL queries detected for the wp_global_style post type after creation.' ); } + /** + * @covers WP_Theme_JSON_Resolver::get_user_data_from_wp_global_styles + */ + function test_get_user_data_from_wp_global_styles_does_not_use_uncached_queries_for_logged_out_users() { + $theme = wp_get_theme(); + WP_Theme_JSON_Resolver::get_user_data_from_wp_global_styles( $theme ); + add_filter( 'query', array( $this, 'filter_db_query' ) ); + $query_count = count( $this->queries ); + for ( $i = 0; $i < 3; $i++ ) { + WP_Theme_JSON_Resolver::get_user_data_from_wp_global_styles( $theme ); + WP_Theme_JSON_Resolver::clean_cached_data(); + } + $query_count = count( $this->queries ) - $query_count; + $this->assertSame( 0, $query_count, 'Unexpected SQL queries detected for the wp_global_style post type prior to creation.' ); + + $user_cpt = WP_Theme_JSON_Resolver::get_user_data_from_wp_global_styles( $theme ); + $this->assertEmpty( $user_cpt, 'User CPT is expected to be empty.' ); + } + /** * @ticket 55392 * @covers WP_Theme_JSON_Resolver::get_user_data_from_wp_global_styles