diff --git a/phpunit/bootstrap.php b/phpunit/bootstrap.php index 54cacec08c0309..acc7cfde89dbd4 100644 --- a/phpunit/bootstrap.php +++ b/phpunit/bootstrap.php @@ -186,18 +186,6 @@ function gutenberg_register_test_block_for_feature_selectors() { } tests_add_filter( 'init', 'gutenberg_register_test_block_for_feature_selectors' ); -// Used in class-wp-theme-json-test.php to mock theme file URIs. -function gutenberg_tests_filter_theme_file_uri( $file ) { - // Tests can pass a file path with `example/` to return a dummy absolute theme file URI. - if ( strpos( $file, 'example/' ) ) { - $file_name = explode( 'example/', $file )[1]; - return 'https://example.org/wp-content/themes/example-theme/example/' . $file_name; - } - // Default active theme URI. - return get_stylesheet_directory_uri() . '/' . $file; -} -tests_add_filter( 'theme_file_uri', 'gutenberg_tests_filter_theme_file_uri' ); - // Start up the WP testing environment. require $_tests_dir . '/includes/bootstrap.php'; diff --git a/phpunit/class-wp-theme-json-test.php b/phpunit/class-wp-theme-json-test.php index ee3f7e34dc3430..8adf9f614b33b9 100644 --- a/phpunit/class-wp-theme-json-test.php +++ b/phpunit/class-wp-theme-json-test.php @@ -4880,7 +4880,20 @@ public function test_get_top_level_background_image_styles() { 'version' => WP_Theme_JSON_Gutenberg::LATEST_SCHEMA, ); $expected_styles = "body { margin: 0; }.wp-site-blocks > .alignleft { float: left; margin-right: 2em; }.wp-site-blocks > .alignright { float: right; margin-left: 2em; }.wp-site-blocks > .aligncenter { justify-content: center; margin-left: auto; margin-right: auto; }:where(.is-layout-flex){gap: 0.5em;}:where(.is-layout-grid){gap: 0.5em;}.is-layout-flow > .alignleft{float: left;margin-inline-start: 0;margin-inline-end: 2em;}.is-layout-flow > .alignright{float: right;margin-inline-start: 2em;margin-inline-end: 0;}.is-layout-flow > .aligncenter{margin-left: auto !important;margin-right: auto !important;}.is-layout-constrained > .alignleft{float: left;margin-inline-start: 0;margin-inline-end: 2em;}.is-layout-constrained > .alignright{float: right;margin-inline-start: 2em;margin-inline-end: 0;}.is-layout-constrained > .aligncenter{margin-left: auto !important;margin-right: auto !important;}.is-layout-constrained > :where(:not(.alignleft):not(.alignright):not(.alignfull)){margin-left: auto !important;margin-right: auto !important;}body .is-layout-flex{display: flex;}.is-layout-flex{flex-wrap: wrap;align-items: center;}.is-layout-flex > :is(*, div){margin: 0;}body .is-layout-grid{display: grid;}.is-layout-grid > :is(*, div){margin: 0;}html{min-height: calc(100% - var(--wp-admin--admin-bar--height, 0px));}:where(body){background-image: url('https://example.org/wp-content/themes/example-theme/example/img/image.png');}"; + + /* + * This filter callback normalizes the return value from `get_theme_file_uri` + * to guard against changes in test environments. + * The test suite otherwise returns full system dir path, e.g., + * /wordpress-phpunit/includes/../data/themedir1/default/example/img/image.png + */ + $filter_theme_file_uri_callback = function ( $file ) { + return 'https://example.org/wp-content/themes/example-theme/example/' . explode( 'example/', $file )[1]; + }; + add_filter( 'theme_file_uri', $filter_theme_file_uri_callback ); $this->assertSame( $expected_styles, $theme_json->resolve_theme_file_uris()->get_stylesheet(), 'Styles returned from "::resolve_theme_file_uris()->get_stylesheet()" with resolved top-level theme background images do not match expected string' ); + remove_filter( 'theme_file_uri', $filter_theme_file_uri_callback ); + $this->assertSame( $expected_data, $theme_json->get_data(), 'Styles returned from "::get_data()" with resolved top-level theme background images do not match expected array' ); }