Skip to content

Commit 15d5bd8

Browse files
committed
Improve ensuring CSS files are present during tests
1 parent 23debdb commit 15d5bd8

File tree

1 file changed

+12
-21
lines changed

1 file changed

+12
-21
lines changed

tests/phpunit/tests/template.php

Lines changed: 12 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1706,30 +1706,18 @@ static function () {
17061706
register_core_block_style_handles();
17071707
$this->assertTrue( WP_Block_Type_Registry::get_instance()->is_registered( 'core/separator' ), 'Expected the core/separator block to be registered.' );
17081708

1709-
// Ensure all registered styles added as part of the build process are present on the disk. .These files are created as part of the build process, but the unit tests don't run the build prior to running unit tests on GHA.
1710-
foreach ( wp_styles()->registered as $handle => $dependency ) {
1711-
if ( ! is_string( $dependency->src ) ) {
1712-
continue;
1713-
}
1714-
$path = wp_parse_url( $dependency->src, PHP_URL_PATH );
1715-
$position = strpos( $path, WPINC . '/' );
1716-
if ( false === $position ) {
1717-
continue;
1718-
}
1719-
$absolute_path = ABSPATH . substr( $path, $position );
1720-
if ( ! file_exists( $absolute_path ) ) {
1721-
self::touch( $absolute_path );
1722-
file_put_contents( $absolute_path, "/* Stub for $handle */" );
1723-
}
1724-
}
1725-
17261709
// Ensure stylesheet files exist on the filesystem since a build may not have been done.
17271710
$this->ensure_style_asset_file_created(
17281711
'wp-block-library',
1729-
wp_should_load_separate_core_block_assets() ? 'css/dist/block-library/common.css' : 'css/dist/block-library/style.css'
1712+
wp_should_load_separate_core_block_assets() ? 'css/dist/block-library/common.css' : 'css/dist/block-library/style.css',
1713+
wp_should_load_separate_core_block_assets()
17301714
);
1715+
$dependency = wp_styles()->query( 'wp-block-library' );
1716+
$this->assertTrue( (bool) $dependency, 'Expected wp-block-library stylesheet to be registered.' );
1717+
$this->assertIsString( $dependency->src, 'Expected wp-block-library to have a string src. Dependency: ' . json_encode( $dependency ) );
1718+
17311719
if ( wp_should_load_separate_core_block_assets() ) {
1732-
$this->ensure_style_asset_file_created( 'wp-block-separator', 'blocks/separator/style.css' );
1720+
$this->ensure_style_asset_file_created( 'wp-block-separator', 'blocks/separator/style.css', true );
17331721
}
17341722
$this->assertFalse( wp_is_block_theme(), 'Test is not relevant to block themes (only classic themes).' );
17351723

@@ -1834,10 +1822,11 @@ static function () {
18341822
*
18351823
* @param string $handle Style handle.
18361824
* @param string $relative_path Relative path to the CSS file in wp-includes.
1825+
* @param bool $add_path_data Whether to add the path data.
18371826
*
18381827
* @throws Exception If the supplied style handle is not registered as expected.
18391828
*/
1840-
private function ensure_style_asset_file_created( string $handle, string $relative_path ) {
1829+
private function ensure_style_asset_file_created( string $handle, string $relative_path, bool $add_path_data = false ) {
18411830
$dependency = wp_styles()->query( $handle );
18421831
if ( ! $dependency ) {
18431832
throw new Exception( "The stylesheet for $handle is not registered." );
@@ -1851,7 +1840,9 @@ private function ensure_style_asset_file_created( string $handle, string $relati
18511840
}
18521841
file_put_contents( $path, "/* CSS for $handle */" );
18531842
}
1854-
wp_style_add_data( $handle, 'path', $path );
1843+
if ( $add_path_data ) {
1844+
wp_style_add_data( $handle, 'path', $path );
1845+
}
18551846
}
18561847

18571848
public function assertTemplateHierarchy( $url, array $expected, $message = '' ) {

0 commit comments

Comments
 (0)