From 55212745fc4f074097553ab63a384d770ad9c91c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9?= <583546+oandregal@users.noreply.github.com> Date: Thu, 27 Oct 2022 10:18:15 +0200 Subject: [PATCH] Add test for global styles coming from 3rd party blocks --- .../data/themedir1/block-theme/theme.json | 5 ++ .../theme/wpGetGlobalStylesForBlocks.php | 90 +++++++++++++++++++ 2 files changed, 95 insertions(+) create mode 100644 tests/phpunit/tests/theme/wpGetGlobalStylesForBlocks.php diff --git a/tests/phpunit/data/themedir1/block-theme/theme.json b/tests/phpunit/data/themedir1/block-theme/theme.json index 02fcb395dd393..d023faec53125 100644 --- a/tests/phpunit/data/themedir1/block-theme/theme.json +++ b/tests/phpunit/data/themedir1/block-theme/theme.json @@ -69,6 +69,11 @@ "filter": { "duotone": "var(--wp--preset--duotone--custom-duotone)" } + }, + "my/third-party-block": { + "color": { + "background": "hotpink" + } } }, "elements": { diff --git a/tests/phpunit/tests/theme/wpGetGlobalStylesForBlocks.php b/tests/phpunit/tests/theme/wpGetGlobalStylesForBlocks.php new file mode 100644 index 0000000000000..ca7e822641588 --- /dev/null +++ b/tests/phpunit/tests/theme/wpGetGlobalStylesForBlocks.php @@ -0,0 +1,90 @@ +orig_theme_dir = $GLOBALS['wp_theme_directories']; + $this->theme_root = realpath( DIR_TESTDATA . '/themedir1' ); + + // /themes is necessary as theme.php functions assume /themes is the root if there is only one root. + $GLOBALS['wp_theme_directories'] = array( WP_CONTENT_DIR . '/themes', $this->theme_root ); + + // Set up the new root. + add_filter( 'theme_root', array( $this, 'filter_set_theme_root' ) ); + add_filter( 'stylesheet_root', array( $this, 'filter_set_theme_root' ) ); + add_filter( 'template_root', array( $this, 'filter_set_theme_root' ) ); + + // Clear caches. + wp_clean_themes_cache(); + unset( $GLOBALS['wp_themes'] ); + } + + public function tear_down() { + $GLOBALS['wp_theme_directories'] = $this->orig_theme_dir; + + // Clear up the filters to modify the theme root. + remove_filter( 'theme_root', array( $this, 'filter_set_theme_root' ) ); + remove_filter( 'stylesheet_root', array( $this, 'filter_set_theme_root' ) ); + remove_filter( 'template_root', array( $this, 'filter_set_theme_root' ) ); + + wp_clean_themes_cache(); + unset( $GLOBALS['wp_themes'] ); + + parent::tear_down(); + } + + /** + * Cleans up global scope. + * + * @global WP_Styles $wp_styles + */ + public function clean_up_global_scope() { + global $wp_styles; + parent::clean_up_global_scope(); + $wp_styles = null; + } + + public function filter_set_theme_root() { + return $this->theme_root; + } + + public function test_styles_for_blocks() { + switch_theme( 'block-theme' ); + + $name = 'my/third-party-block'; + $settings = array( + 'icon' => 'text', + 'category' => 'common', + 'render_callback' => 'foo', + ); + register_block_type( $name, $settings ); + wp_enqueue_global_styles(); + unregister_block_type( $name ); + + $this->assertStringContainsString( '.wp-block-my-third-party-block{background-color: hotpink;}', get_echo( 'wp_print_styles' ), '3rd party block styles are included' ); + + switch_theme( WP_DEFAULT_THEME ); + } + +}