mirror of
https://github.com/gosticks/wordpress-develop.git
synced 2026-06-28 14:20:15 +00:00
Themes: Make caches for block patterns clearable.
In [56765], theme block pattern files were cached to a transient as a performance enhancement. However, transients are not easily clearable when caches are flushed on environments not using a persistent cache, which can lead to errors if the theme files are renamed, edited, or moved. This changes the caching mechanism to use `wp_cache_set()` instead, and caches these values to the global group so they are still persistent on environments using an object cache, and will be cleared by a cache flush. In addition, the helper `_wp_get_block_patterns` has been moved `WP_Theme::get_block_patterns` for consistency with other block related theme methods and cache helpers for these values, `WP_Theme::get_pattern_cache` and `WP_Theme::set_pattern_cache`, have been made private. Relevant unit tests updated. Props: afercia, flixos90, mukesh27, joemcgill. Fixes #59633. See #59591, #59490. git-svn-id: https://develop.svn.wordpress.org/trunk@56978 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
@@ -1,153 +0,0 @@
|
||||
<?php
|
||||
/**
|
||||
* Tests for _wp_get_block_patterns.
|
||||
*
|
||||
* @package WordPress
|
||||
* @subpackage Blocks
|
||||
* @since 6.4.0
|
||||
*
|
||||
* @group blocks
|
||||
*
|
||||
* @covers ::_wp_get_block_patterns
|
||||
*/
|
||||
class Tests_Blocks_WpGetBlockPatterns extends WP_UnitTestCase {
|
||||
/**
|
||||
* @ticket 59490
|
||||
*
|
||||
* @dataProvider data_wp_get_block_patterns
|
||||
*
|
||||
* @param string $theme The theme's slug.
|
||||
* @param array $expected The expected pattern data.
|
||||
*/
|
||||
public function test_should_return_block_patterns( $theme, $expected ) {
|
||||
$patterns = _wp_get_block_patterns( wp_get_theme( $theme ) );
|
||||
$this->assertSameSets( $expected, $patterns );
|
||||
}
|
||||
|
||||
/**
|
||||
* @ticket 59490
|
||||
*/
|
||||
public function test_delete_theme_cache() {
|
||||
$theme = wp_get_theme( 'block-theme-patterns' );
|
||||
_wp_get_block_patterns( $theme );
|
||||
$this->assertSameSets(
|
||||
array(
|
||||
'cta.php' => array(
|
||||
'title' => 'Centered Call To Action',
|
||||
'slug' => 'block-theme-patterns/cta',
|
||||
'description' => '',
|
||||
'categories' => array( 'call-to-action' ),
|
||||
),
|
||||
),
|
||||
$theme->get_pattern_cache(),
|
||||
'The transient for block theme patterns should be set'
|
||||
);
|
||||
$theme->delete_pattern_cache();
|
||||
$this->assertFalse(
|
||||
$theme->get_pattern_cache(),
|
||||
'The transient for block theme patterns should have been cleared'
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @ticket 59490
|
||||
*/
|
||||
public function test_should_clear_transient_after_switching_theme() {
|
||||
switch_theme( 'block-theme' );
|
||||
$theme1 = wp_get_theme();
|
||||
_wp_get_block_patterns( $theme1 );
|
||||
$this->assertSameSets(
|
||||
array(),
|
||||
$theme1->get_pattern_cache(),
|
||||
'The transient for block theme should be set'
|
||||
);
|
||||
switch_theme( 'block-theme-patterns' );
|
||||
$this->assertFalse( $theme1->get_pattern_cache(), 'Transient should not be set for block theme after switch theme' );
|
||||
$theme2 = wp_get_theme();
|
||||
$this->assertFalse( $theme2->get_pattern_cache(), 'Transient should not be set for block theme patterns before being requested' );
|
||||
_wp_get_block_patterns( $theme2 );
|
||||
$this->assertSameSets(
|
||||
array(
|
||||
'cta.php' => array(
|
||||
'title' => 'Centered Call To Action',
|
||||
'slug' => 'block-theme-patterns/cta',
|
||||
'description' => '',
|
||||
'categories' => array( 'call-to-action' ),
|
||||
),
|
||||
|
||||
),
|
||||
$theme2->get_pattern_cache(),
|
||||
'The transient for block theme patterns should be set'
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Data provider.
|
||||
*
|
||||
* @return array[]
|
||||
*/
|
||||
public function data_wp_get_block_patterns() {
|
||||
return array(
|
||||
array(
|
||||
'theme' => 'block-theme',
|
||||
'patterns' => array(),
|
||||
),
|
||||
array(
|
||||
'theme' => 'block-theme-child',
|
||||
'patterns' => array(),
|
||||
),
|
||||
array(
|
||||
'theme' => 'block-theme-patterns',
|
||||
'patterns' => array(
|
||||
'cta.php' => array(
|
||||
'title' => 'Centered Call To Action',
|
||||
'slug' => 'block-theme-patterns/cta',
|
||||
'description' => '',
|
||||
'categories' => array( 'call-to-action' ),
|
||||
),
|
||||
),
|
||||
),
|
||||
array(
|
||||
'theme' => 'broken-theme',
|
||||
'patterns' => array(),
|
||||
),
|
||||
array(
|
||||
'theme' => 'invalid',
|
||||
'patterns' => array(),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests that _wp_get_block_patterns() clears existing transient when in theme development mode.
|
||||
*
|
||||
* @ticket 59591
|
||||
*/
|
||||
public function test_should_clear_existing_transient_when_in_development_mode() {
|
||||
$theme = wp_get_theme( 'block-theme-patterns' );
|
||||
|
||||
// Calling the function should set the cache.
|
||||
_wp_get_block_patterns( $theme );
|
||||
$this->assertSameSets(
|
||||
array(
|
||||
'cta.php' => array(
|
||||
'title' => 'Centered Call To Action',
|
||||
'slug' => 'block-theme-patterns/cta',
|
||||
'description' => '',
|
||||
'categories' => array( 'call-to-action' ),
|
||||
),
|
||||
),
|
||||
$theme->get_pattern_cache(),
|
||||
'The transient for block theme patterns should be set'
|
||||
);
|
||||
|
||||
// Calling the function while in theme development mode should clear the cache.
|
||||
$GLOBALS['_wp_tests_development_mode'] = 'theme';
|
||||
_wp_get_block_patterns( $theme );
|
||||
unset( $GLOBALS['_wp_tests_development_mode'] ); // Reset to not pollute other tests.
|
||||
$this->assertFalse(
|
||||
$theme->get_pattern_cache(),
|
||||
'The transient for block theme patterns should have been cleared due to theme development mode'
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -26,6 +26,9 @@ class Tests_Theme_WpGetGlobalStylesheet extends WP_Theme_UnitTestCase {
|
||||
private $switch_to_default_theme_at_teardown = false;
|
||||
|
||||
public function tear_down() {
|
||||
// Reset development mode after each test.
|
||||
unset( $GLOBALS['_wp_tests_development_mode'] );
|
||||
|
||||
// Reset the theme support.
|
||||
if ( $this->remove_theme_support_at_teardown ) {
|
||||
$this->remove_theme_support_at_teardown = false;
|
||||
|
||||
198
tests/phpunit/tests/theme/wpThemeGetBlockPatterns.php
Normal file
198
tests/phpunit/tests/theme/wpThemeGetBlockPatterns.php
Normal file
@@ -0,0 +1,198 @@
|
||||
<?php
|
||||
/**
|
||||
* Tests for WP_Theme::get_block_patterns.
|
||||
*
|
||||
* @package WordPress
|
||||
* @subpackage Blocks
|
||||
* @since 6.4.0
|
||||
*
|
||||
* @group blocks
|
||||
* @group themes
|
||||
*
|
||||
* @covers WP_Theme::get_block_patterns
|
||||
*/
|
||||
class Tests_Theme_WPThemeGetBlockPatterns extends WP_UnitTestCase {
|
||||
|
||||
public static function wpSetUpBeforeClass() {
|
||||
// Ensure development mode is reset before running these tests.
|
||||
unset( $GLOBALS['_wp_tests_development_mode'] );
|
||||
}
|
||||
|
||||
public static function wpTearDownAfterClass() {
|
||||
// Ensure development mode is reset after running these tests.
|
||||
unset( $GLOBALS['_wp_tests_development_mode'] );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test helper to access the private get_pattern_cache method of a theme.
|
||||
*
|
||||
* @param WP_Theme $wp_theme A WP_Theme object.
|
||||
* @return array|false Returns an array of patterns if cache is found, otherwise false.
|
||||
*/
|
||||
private function get_pattern_cache( $wp_theme ) {
|
||||
$reflection = new ReflectionMethod( $wp_theme, 'get_pattern_cache' );
|
||||
$reflection->setAccessible( true );
|
||||
|
||||
$pattern_cache = $reflection->invoke( $wp_theme, 'get_pattern_cache' );
|
||||
$reflection->setAccessible( false );
|
||||
|
||||
return $pattern_cache;
|
||||
}
|
||||
|
||||
/**
|
||||
* @ticket 59490
|
||||
*
|
||||
* @dataProvider data_get_block_patterns
|
||||
*
|
||||
* @param string $theme_slug The theme's slug.
|
||||
* @param array $expected The expected pattern data.
|
||||
*/
|
||||
public function test_should_return_block_patterns( $theme_slug, $expected ) {
|
||||
$theme = wp_get_theme( $theme_slug );
|
||||
$patterns = $theme->get_block_patterns();
|
||||
$this->assertSameSets( $expected, $patterns );
|
||||
}
|
||||
|
||||
/**
|
||||
* @ticket 59490
|
||||
*
|
||||
* @covers WP_Theme::delete_pattern_cache
|
||||
*/
|
||||
public function test_delete_pattern_cache() {
|
||||
$theme = wp_get_theme( 'block-theme-patterns' );
|
||||
|
||||
$this->assertTrue( $theme->exists(), 'The test theme could not be found.' );
|
||||
|
||||
$theme->get_block_patterns();
|
||||
|
||||
$this->assertSameSets(
|
||||
array(
|
||||
'cta.php' => array(
|
||||
'title' => 'Centered Call To Action',
|
||||
'slug' => 'block-theme-patterns/cta',
|
||||
'description' => '',
|
||||
'categories' => array( 'call-to-action' ),
|
||||
),
|
||||
),
|
||||
$this->get_pattern_cache( $theme ),
|
||||
'The cache for block theme patterns should match the expected.'
|
||||
);
|
||||
$theme->delete_pattern_cache();
|
||||
$this->assertFalse(
|
||||
$this->get_pattern_cache( $theme ),
|
||||
'The cache for block theme patterns should have been cleared.'
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @ticket 59490
|
||||
*/
|
||||
public function test_should_clear_cache_after_switching_theme() {
|
||||
switch_theme( 'block-theme' );
|
||||
$theme1 = wp_get_theme();
|
||||
|
||||
$this->assertTrue( $theme1->exists(), 'The block-theme test theme could not be found.' );
|
||||
|
||||
$theme1->get_block_patterns();
|
||||
$this->assertSameSets(
|
||||
array(),
|
||||
$this->get_pattern_cache( $theme1 ),
|
||||
'The cache for block theme should be empty.'
|
||||
);
|
||||
|
||||
switch_theme( 'block-theme-patterns' );
|
||||
|
||||
$theme2 = wp_get_theme();
|
||||
$this->assertTrue( $theme2->exists(), 'The block-theme-patterns test theme could not be found.' );
|
||||
|
||||
$this->assertFalse( $this->get_pattern_cache( $theme1 ), 'Cache should not be set for block theme after switch theme.' );
|
||||
$this->assertFalse( $this->get_pattern_cache( $theme2 ), 'Cache should not be set for block theme patterns before being requested.' );
|
||||
|
||||
$theme2->get_block_patterns( $theme2 );
|
||||
$this->assertSameSets(
|
||||
array(
|
||||
'cta.php' => array(
|
||||
'title' => 'Centered Call To Action',
|
||||
'slug' => 'block-theme-patterns/cta',
|
||||
'description' => '',
|
||||
'categories' => array( 'call-to-action' ),
|
||||
),
|
||||
|
||||
),
|
||||
$this->get_pattern_cache( $theme2 ),
|
||||
'The cache for block theme patterns should match the expected.'
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Data provider.
|
||||
*
|
||||
* @return array[]
|
||||
*/
|
||||
public function data_get_block_patterns() {
|
||||
return array(
|
||||
array(
|
||||
'theme' => 'block-theme',
|
||||
'patterns' => array(),
|
||||
),
|
||||
array(
|
||||
'theme' => 'block-theme-child',
|
||||
'patterns' => array(),
|
||||
),
|
||||
array(
|
||||
'theme' => 'block-theme-patterns',
|
||||
'patterns' => array(
|
||||
'cta.php' => array(
|
||||
'title' => 'Centered Call To Action',
|
||||
'slug' => 'block-theme-patterns/cta',
|
||||
'description' => '',
|
||||
'categories' => array( 'call-to-action' ),
|
||||
),
|
||||
),
|
||||
),
|
||||
array(
|
||||
'theme' => 'broken-theme',
|
||||
'patterns' => array(),
|
||||
),
|
||||
array(
|
||||
'theme' => 'invalid',
|
||||
'patterns' => array(),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests that WP_Theme::get_block_patterns() clears existing cache when in theme development mode.
|
||||
*
|
||||
* @ticket 59591
|
||||
*/
|
||||
public function test_should_clear_existing_cache_when_in_development_mode() {
|
||||
$theme = wp_get_theme( 'block-theme-patterns' );
|
||||
|
||||
$this->assertTrue( $theme->exists(), 'The test theme could not be found.' );
|
||||
|
||||
// Calling the function should set the cache.
|
||||
$theme->get_block_patterns();
|
||||
$this->assertSameSets(
|
||||
array(
|
||||
'cta.php' => array(
|
||||
'title' => 'Centered Call To Action',
|
||||
'slug' => 'block-theme-patterns/cta',
|
||||
'description' => '',
|
||||
'categories' => array( 'call-to-action' ),
|
||||
),
|
||||
),
|
||||
$this->get_pattern_cache( $theme ),
|
||||
'The cache for block theme patterns should be set.'
|
||||
);
|
||||
|
||||
// Calling the function while in theme development mode should clear the cache.
|
||||
$GLOBALS['_wp_tests_development_mode'] = 'theme';
|
||||
$theme->get_block_patterns( $theme );
|
||||
unset( $GLOBALS['_wp_tests_development_mode'] ); // Reset to not pollute other tests.
|
||||
$this->assertFalse(
|
||||
$this->get_pattern_cache( $theme ),
|
||||
'The cache for block theme patterns should have been cleared due to theme development mode.'
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user