From 2e051261f3c91c9f8fcd0f4e284c975989f8b769 Mon Sep 17 00:00:00 2001 From: Felix Arntz Date: Tue, 3 Oct 2017 18:20:37 +0000 Subject: [PATCH] Multisite: Add specific tests for `clean_blog_cache()` and `refresh_blog_details()`. See #40201. git-svn-id: https://develop.svn.wordpress.org/trunk@41715 602fd350-edb4-49c9-b593-d223f7449a82 --- tests/phpunit/tests/multisite/site.php | 127 +++++++++++++++++++++++++ 1 file changed, 127 insertions(+) diff --git a/tests/phpunit/tests/multisite/site.php b/tests/phpunit/tests/multisite/site.php index 7e3bb54571..6cbb5351b7 100644 --- a/tests/phpunit/tests/multisite/site.php +++ b/tests/phpunit/tests/multisite/site.php @@ -1057,6 +1057,133 @@ class Tests_Multisite_Site extends WP_UnitTestCase { public function test_is_main_site_different_network_random_site() { $this->assertFalse( is_main_site( self::$site_ids['make.wordpress.org/foo/'], self::$network_ids['make.wordpress.org/'] ) ); } + + /** + * @ticket 40201 + * @dataProvider data_get_site_caches + */ + public function test_clean_blog_cache( $key, $group ) { + $site = get_site( self::$site_ids['make.wordpress.org/'] ); + + $replacements = array( + '%blog_id%' => $site->blog_id, + '%domain%' => $site->domain, + '%path%' => $site->path, + '%domain_path_key%' => md5( $site->domain . $site->path ), + ); + + $key = str_replace( array_keys( $replacements ), array_values( $replacements ), $key ); + + if ( 'sites' === $group ) { // This needs to be actual data for get_site() lookups. + wp_cache_set( $key, (object) $site->to_array(), $group ); + } else { + wp_cache_set( $key, 'something', $group ); + } + + clean_blog_cache( $site ); + $this->assertFalse( wp_cache_get( $key, $group ) ); + } + + /** + * @ticket 40201 + */ + public function test_clean_blog_cache_resets_last_changed() { + $site = get_site( self::$site_ids['make.wordpress.org/'] ); + + wp_cache_delete( 'last_changed', 'sites' ); + + clean_blog_cache( $site ); + $this->assertNotFalse( wp_cache_get( 'last_changed', 'sites' ) ); + } + + /** + * @ticket 40201 + */ + public function test_clean_blog_cache_fires_action() { + $site = get_site( self::$site_ids['make.wordpress.org/'] ); + + $old_count = did_action( 'clean_site_cache' ); + + clean_blog_cache( $site ); + $this->assertEquals( $old_count + 1, did_action( 'clean_site_cache' ) ); + } + + /** + * @ticket 40201 + */ + public function test_clean_blog_cache_bails_on_suspend_cache_invalidation() { + $site = get_site( self::$site_ids['make.wordpress.org/'] ); + + $old_count = did_action( 'clean_site_cache' ); + + $suspend = wp_suspend_cache_invalidation(); + clean_blog_cache( $site ); + wp_suspend_cache_invalidation( $suspend ); + $this->assertEquals( $old_count, did_action( 'clean_site_cache' ) ); + } + + /** + * @ticket 40201 + * @dataProvider data_get_site_caches + */ + public function test_refresh_blog_details( $key, $group ) { + $site = get_site( self::$site_ids['make.wordpress.org/'] ); + + $replacements = array( + '%blog_id%' => $site->blog_id, + '%domain%' => $site->domain, + '%path%' => $site->path, + '%domain_path_key%' => md5( $site->domain . $site->path ), + ); + + $key = str_replace( array_keys( $replacements ), array_values( $replacements ), $key ); + + if ( 'sites' === $group ) { // This needs to be actual data for get_site() lookups. + wp_cache_set( $key, (object) $site->to_array(), $group ); + } else { + wp_cache_set( $key, 'something', $group ); + } + + refresh_blog_details( $site->blog_id ); + $this->assertFalse( wp_cache_get( $key, $group ) ); + } + + /** + * @ticket 40201 + */ + public function test_refresh_blog_details_works_with_deleted_site() { + $site_id = 12345; + + wp_cache_set( $site_id, 'something', 'site-details' ); + + refresh_blog_details( $site_id ); + $this->assertFalse( wp_cache_get( $site_id, 'site-details' ) ); + } + + /** + * @ticket 40201 + */ + public function test_refresh_blog_details_uses_current_site_as_default() { + $site_id = get_current_blog_id(); + + wp_cache_set( $site_id, 'something', 'site-details' ); + + refresh_blog_details(); + $this->assertFalse( wp_cache_get( $site_id, 'site-details' ) ); + } + + public function data_get_site_caches() { + return array( + array( '%blog_id%', 'sites' ), + array( '%blog_id%', 'site-details' ), + array( '%blog_id%', 'blog-details' ), + array( '%blog_id%' . 'short' , 'blog-details' ), + array( '%domain_path_key%', 'blog-lookup' ), + array( '%domain_path_key%', 'blog-id-cache' ), + array( 'current_blog_%domain%', 'site-options' ), + array( 'current_blog_%domain%%path%', 'site-options' ), + ); + } } endif;