<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head><meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>[56044] trunk: Editor: Register core block styles in one place.</title>
</head>
<body>

<style type="text/css"><!--
#msg dl.meta { border: 1px #006 solid; background: #369; padding: 6px; color: #fff; }
#msg dl.meta dt { float: left; width: 6em; font-weight: bold; }
#msg dt:after { content:':';}
#msg dl, #msg dt, #msg ul, #msg li, #header, #footer, #logmsg { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt;  }
#msg dl a { font-weight: bold}
#msg dl a:link    { color:#fc3; }
#msg dl a:active  { color:#ff0; }
#msg dl a:visited { color:#cc6; }
h3 { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; font-weight: bold; }
#msg pre { white-space: pre-line; overflow: auto; background: #ffc; border: 1px #fa0 solid; padding: 6px; }
#logmsg { background: #ffc; border: 1px #fa0 solid; padding: 1em 1em 0 1em; }
#logmsg p, #logmsg pre, #logmsg blockquote { margin: 0 0 1em 0; }
#logmsg p, #logmsg li, #logmsg dt, #logmsg dd { line-height: 14pt; }
#logmsg h1, #logmsg h2, #logmsg h3, #logmsg h4, #logmsg h5, #logmsg h6 { margin: .5em 0; }
#logmsg h1:first-child, #logmsg h2:first-child, #logmsg h3:first-child, #logmsg h4:first-child, #logmsg h5:first-child, #logmsg h6:first-child { margin-top: 0; }
#logmsg ul, #logmsg ol { padding: 0; list-style-position: inside; margin: 0 0 0 1em; }
#logmsg ul { text-indent: -1em; padding-left: 1em; }#logmsg ol { text-indent: -1.5em; padding-left: 1.5em; }
#logmsg > ul, #logmsg > ol { margin: 0 0 1em 0; }
#logmsg pre { background: #eee; padding: 1em; }
#logmsg blockquote { border: 1px solid #fa0; border-left-width: 10px; padding: 1em 1em 0 1em; background: white;}
#logmsg dl { margin: 0; }
#logmsg dt { font-weight: bold; }
#logmsg dd { margin: 0; padding: 0 0 0.5em 0; }
#logmsg dd:before { content:'\00bb';}
#logmsg table { border-spacing: 0px; border-collapse: collapse; border-top: 4px solid #fa0; border-bottom: 1px solid #fa0; background: #fff; }
#logmsg table th { text-align: left; font-weight: normal; padding: 0.2em 0.5em; border-top: 1px dotted #fa0; }
#logmsg table td { text-align: right; border-top: 1px dotted #fa0; padding: 0.2em 0.5em; }
#logmsg table thead th { text-align: center; border-bottom: 1px solid #fa0; }
#logmsg table th.Corner { text-align: left; }
#logmsg hr { border: none 0; border-top: 2px dashed #fa0; height: 1px; }
#header, #footer { color: #fff; background: #636; border: 1px #300 solid; padding: 6px; }
#patch { width: 100%; }
#patch h4 {font-family: verdana,arial,helvetica,sans-serif;font-size:10pt;padding:8px;background:#369;color:#fff;margin:0;}
#patch .propset h4, #patch .binary h4 {margin:0;}
#patch pre {padding:0;line-height:1.2em;margin:0;}
#patch .diff {width:100%;background:#eee;padding: 0 0 10px 0;overflow:auto;}
#patch .propset .diff, #patch .binary .diff  {padding:10px 0;}
#patch span {display:block;padding:0 10px;}
#patch .modfile, #patch .addfile, #patch .delfile, #patch .propset, #patch .binary, #patch .copfile {border:1px solid #ccc;margin:10px 0;}
#patch ins {background:#dfd;text-decoration:none;display:block;padding:0 10px;}
#patch del {background:#fdd;text-decoration:none;display:block;padding:0 10px;}
#patch .lines, .info {color:#888;background:#fff;}
--></style>
<div id="msg">
<dl class="meta" style="font-size: 105%">
<dt style="float: left; width: 6em; font-weight: bold">Revision</dt> <dd><a style="font-weight: bold" href="https://core.trac.wordpress.org/changeset/56044">56044</a><script type="application/ld+json">{"@context":"http://schema.org","@type":"EmailMessage","description":"Review this Commit","action":{"@type":"ViewAction","url":"https://core.trac.wordpress.org/changeset/56044","name":"Review Commit"}}</script></dd>
<dt style="float: left; width: 6em; font-weight: bold">Author</dt> <dd>spacedmonkey</dd>
<dt style="float: left; width: 6em; font-weight: bold">Date</dt> <dd>2023-06-26 21:15:21 +0000 (Mon, 26 Jun 2023)</dd>
</dl>

<pre style='padding-left: 1em; margin: 2em 0; border-left: 2px solid #ccc; line-height: 1.25; font-size: 105%; font-family: sans-serif'>Editor: Register core block styles in one place. 

Register all core blocks in a new function called `register_core_block_style_handles`. This mirrors the function `wp_default_styles` where all core styles are registered in one place. This improves block registration performance, as it avoids expensive file lookups, like realpath in `register_block_style_handle`. The new function `register_core_block_style_handles` uses `glob` to get all css files in the blocks directory. This glob is cached in a transient to save lookups on subsequent requests. The function `register_block_style_handle` now checks to see if the style handle is already registered before trying to register it again. 

Props mukesh27, westonruter, flixos90, joemcgill, spacedmonkey.
Fixes <a href="https://core.trac.wordpress.org/ticket/58528">#58528</a>.</pre>

<h3>Modified Paths</h3>
<ul>
<li><a href="#trunksrcwpadminincludesupgradephp">trunk/src/wp-admin/includes/upgrade.php</a></li>
<li><a href="#trunksrcwpincludesblocksindexphp">trunk/src/wp-includes/blocks/index.php</a></li>
<li><a href="#trunksrcwpincludesblocksphp">trunk/src/wp-includes/blocks.php</a></li>
<li><a href="#trunktestsphpunittestsblocksregisterphp">trunk/tests/phpunit/tests/blocks/register.php</a></li>
</ul>

<h3>Added Paths</h3>
<ul>
<li><a href="#trunktestsphpunittestsblocksregisterCoreBlockStyleHandlesphp">trunk/tests/phpunit/tests/blocks/registerCoreBlockStyleHandles.php</a></li>
</ul>

</div>
<div id="patch">
<h3>Diff</h3>
<a id="trunksrcwpadminincludesupgradephp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/src/wp-admin/includes/upgrade.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/src/wp-admin/includes/upgrade.php   2023-06-26 20:55:04 UTC (rev 56043)
+++ trunk/src/wp-admin/includes/upgrade.php     2023-06-26 21:15:21 UTC (rev 56044)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -654,6 +654,8 @@
</span><span class="cx" style="display: block; padding: 0 10px">                        update_site_meta( get_current_blog_id(), 'db_last_updated', microtime() );
</span><span class="cx" style="display: block; padding: 0 10px">                }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                delete_transient( 'wp_core_block_css_files' );
+
</ins><span class="cx" style="display: block; padding: 0 10px">                 /**
</span><span class="cx" style="display: block; padding: 0 10px">                 * Fires after a site is fully upgraded.
</span><span class="cx" style="display: block; padding: 0 10px">                 *
</span></span></pre></div>
<a id="trunksrcwpincludesblocksindexphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/src/wp-includes/blocks/index.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/src/wp-includes/blocks/index.php    2023-06-26 20:55:04 UTC (rev 56043)
+++ trunk/src/wp-includes/blocks/index.php      2023-06-26 21:15:21 UTC (rev 56044)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -13,6 +13,92 @@
</span><span class="cx" style="display: block; padding: 0 10px"> require BLOCKS_PATH . 'require-dynamic-blocks.php';
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px"> /**
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * Registers core block style handles.
+ *
+ * While {@see register_block_style_handle()} is typically used for that, the way it is
+ * implemented is inefficient for core block styles. Registering those style handles here
+ * avoids unnecessary logic and filesystem lookups in the other function.
+ *
+ * @since 6.3.0
+ */
+function register_core_block_style_handles() {
+       if ( ! wp_should_load_separate_core_block_assets() ) {
+               return;
+       }
+
+       static $core_blocks_meta;
+       if ( ! $core_blocks_meta ) {
+               $core_blocks_meta = require ABSPATH . WPINC . '/blocks/blocks-json.php';
+       }
+
+       $includes_url  = includes_url();
+       $includes_path = ABSPATH . WPINC . '/';
+       $suffix        = wp_scripts_get_suffix();
+       $wp_styles     = wp_styles();
+       $style_fields  = array(
+               'style'       => 'style',
+               'editorStyle' => 'editor',
+       );
+
+       /*
+        * Ignore transient cache when the development mode is set to 'core'. Why? To avoid interfering with
+        * the core developer's workflow.
+        */
+       if ( 'core' !== wp_get_development_mode() ) {
+               $transient_name = 'wp_core_block_css_files';
+               $files          = get_transient( $transient_name );
+               if ( ! $files ) {
+                       $files = glob( __DIR__ . '/**/**.css' );
+                       set_transient( $transient_name, $files );
+               }
+       } else {
+               $files = glob( __DIR__ . '/**/**.css' );
+       }
+
+       foreach ( $core_blocks_meta as $name => $schema ) {
+               /** This filter is documented in wp-includes/blocks.php */
+               $schema = apply_filters( 'block_type_metadata', $schema );
+
+               // Backfill these properties similar to `register_block_type_from_metadata()`.
+               if ( ! isset( $schema['style'] ) ) {
+                       $schema['style'] = "wp-block-{$name}";
+               }
+               if ( ! isset( $schema['editorStyle'] ) ) {
+                       $schema['editorStyle'] = "wp-block-{$name}-editor";
+               }
+
+               foreach ( $style_fields as $style_field => $filename ) {
+                       $style_handle = $schema[ $style_field ];
+                       if ( is_array( $style_handle ) ) {
+                               continue;
+                       }
+
+                       $style_path = "blocks/{$name}/{$filename}{$suffix}.css";
+                       $path       = $includes_path . $style_path;
+
+                       if ( ! in_array( $path, $files, true ) ) {
+                               $wp_styles->add(
+                                       $style_handle,
+                                       false
+                               );
+                               continue;
+                       }
+
+                       $wp_styles->add( $style_handle, $includes_url . $style_path );
+                       $wp_styles->add_data( $style_handle, 'path', $path );
+
+                       $rtl_file = str_replace( "{$suffix}.css", "-rtl{$suffix}.css", $path );
+                       if ( is_rtl() && in_array( $rtl_file, $files, true ) ) {
+                               $wp_styles->add_data( $style_handle, 'rtl', 'replace' );
+                               $wp_styles->add_data( $style_handle, 'suffix', $suffix );
+                               $wp_styles->add_data( $style_handle, 'path', $rtl_file );
+                       }
+               }
+       }
+}
+add_action( 'init', 'register_core_block_style_handles', 9 );
+
+/**
</ins><span class="cx" style="display: block; padding: 0 10px">  * Registers core block types using metadata files.
</span><span class="cx" style="display: block; padding: 0 10px">  * Dynamic core blocks are registered separately.
</span><span class="cx" style="display: block; padding: 0 10px">  *
</span></span></pre></div>
<a id="trunksrcwpincludesblocksphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/src/wp-includes/blocks.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/src/wp-includes/blocks.php  2023-06-26 20:55:04 UTC (rev 56043)
+++ trunk/src/wp-includes/blocks.php    2023-06-26 21:15:21 UTC (rev 56044)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -186,6 +186,20 @@
</span><span class="cx" style="display: block; padding: 0 10px">                return false;
</span><span class="cx" style="display: block; padding: 0 10px">        }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+        $style_handle = $metadata[ $field_name ];
+       if ( is_array( $style_handle ) ) {
+               if ( empty( $style_handle[ $index ] ) ) {
+                       return false;
+               }
+               $style_handle = $style_handle[ $index ];
+       }
+
+       $style_handle_name = generate_block_asset_handle( $metadata['name'], $field_name, $index );
+       // If the style handle is already registered, skip re-registering.
+       if ( wp_style_is( $style_handle_name, 'registered' ) ) {
+               return $style_handle_name;
+       }
+
</ins><span class="cx" style="display: block; padding: 0 10px">         static $wpinc_path_norm = '';
</span><span class="cx" style="display: block; padding: 0 10px">        if ( ! $wpinc_path_norm ) {
</span><span class="cx" style="display: block; padding: 0 10px">                $wpinc_path_norm = wp_normalize_path( realpath( ABSPATH . WPINC ) );
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -197,14 +211,6 @@
</span><span class="cx" style="display: block; padding: 0 10px">                return false;
</span><span class="cx" style="display: block; padding: 0 10px">        }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-        $style_handle = $metadata[ $field_name ];
-       if ( is_array( $style_handle ) ) {
-               if ( empty( $style_handle[ $index ] ) ) {
-                       return false;
-               }
-               $style_handle = $style_handle[ $index ];
-       }
-
</del><span class="cx" style="display: block; padding: 0 10px">         $style_path      = remove_block_asset_path_prefix( $style_handle );
</span><span class="cx" style="display: block; padding: 0 10px">        $is_style_handle = $style_handle === $style_path;
</span><span class="cx" style="display: block; padding: 0 10px">        // Allow only passing style handles for core blocks.
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -246,10 +252,9 @@
</span><span class="cx" style="display: block; padding: 0 10px">                $style_uri = false;
</span><span class="cx" style="display: block; padding: 0 10px">        }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-        $style_handle = generate_block_asset_handle( $metadata['name'], $field_name, $index );
-       $version      = ! $is_core_block && isset( $metadata['version'] ) ? $metadata['version'] : false;
-       $result       = wp_register_style(
-               $style_handle,
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $version = ! $is_core_block && isset( $metadata['version'] ) ? $metadata['version'] : false;
+       $result  = wp_register_style(
+               $style_handle_name,
</ins><span class="cx" style="display: block; padding: 0 10px">                 $style_uri,
</span><span class="cx" style="display: block; padding: 0 10px">                array(),
</span><span class="cx" style="display: block; padding: 0 10px">                $version
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -259,7 +264,7 @@
</span><span class="cx" style="display: block; padding: 0 10px">        }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">        if ( $has_style_file ) {
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                wp_style_add_data( $style_handle, 'path', $style_path_norm );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         wp_style_add_data( $style_handle_name, 'path', $style_path_norm );
</ins><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">                if ( $is_core_block ) {
</span><span class="cx" style="display: block; padding: 0 10px">                        $rtl_file = str_replace( "{$suffix}.css", "-rtl{$suffix}.css", $style_path_norm );
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -268,13 +273,13 @@
</span><span class="cx" style="display: block; padding: 0 10px">                }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">                if ( is_rtl() && file_exists( $rtl_file ) ) {
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                        wp_style_add_data( $style_handle, 'rtl', 'replace' );
-                       wp_style_add_data( $style_handle, 'suffix', $suffix );
-                       wp_style_add_data( $style_handle, 'path', $rtl_file );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                 wp_style_add_data( $style_handle_name, 'rtl', 'replace' );
+                       wp_style_add_data( $style_handle_name, 'suffix', $suffix );
+                       wp_style_add_data( $style_handle_name, 'path', $rtl_file );
</ins><span class="cx" style="display: block; padding: 0 10px">                 }
</span><span class="cx" style="display: block; padding: 0 10px">        }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-        return $style_handle;
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ return $style_handle_name;
</ins><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px"> /**
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -320,7 +325,7 @@
</span><span class="cx" style="display: block; padding: 0 10px">         */
</span><span class="cx" style="display: block; padding: 0 10px">        static $core_blocks_meta;
</span><span class="cx" style="display: block; padding: 0 10px">        if ( ! $core_blocks_meta ) {
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                $core_blocks_meta = require_once ABSPATH . WPINC . '/blocks/blocks-json.php';
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         $core_blocks_meta = require ABSPATH . WPINC . '/blocks/blocks-json.php';
</ins><span class="cx" style="display: block; padding: 0 10px">         }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">        $metadata_file = ( ! str_ends_with( $file_or_folder, 'block.json' ) ) ?
</span></span></pre></div>
<a id="trunktestsphpunittestsblocksregisterphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/tests/phpunit/tests/blocks/register.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/tests/phpunit/tests/blocks/register.php     2023-06-26 20:55:04 UTC (rev 56043)
+++ trunk/tests/phpunit/tests/blocks/register.php       2023-06-26 21:15:21 UTC (rev 56044)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -397,6 +397,7 @@
</span><span class="cx" style="display: block; padding: 0 10px">         */
</span><span class="cx" style="display: block; padding: 0 10px">        public function test_handle_passed_register_block_style_handle() {
</span><span class="cx" style="display: block; padding: 0 10px">                $metadata = array(
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                        'name'  => 'test-block',
</ins><span class="cx" style="display: block; padding: 0 10px">                         'style' => 'test-style-handle',
</span><span class="cx" style="display: block; padding: 0 10px">                );
</span><span class="cx" style="display: block; padding: 0 10px">                $result   = register_block_style_handle( $metadata, 'style' );
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -406,6 +407,7 @@
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">        public function test_handles_passed_register_block_style_handles() {
</span><span class="cx" style="display: block; padding: 0 10px">                $metadata = array(
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                        'name'  => 'test-block',
</ins><span class="cx" style="display: block; padding: 0 10px">                         'style' => array( 'test-style-handle', 'test-style-handle-2' ),
</span><span class="cx" style="display: block; padding: 0 10px">                );
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -524,6 +526,26 @@
</span><span class="cx" style="display: block; padding: 0 10px">        }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">        /**
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         * @ticket 58528
+        *
+        * @covers ::register_block_style_handle
+        */
+       public function test_success_register_block_style_handle_exists() {
+               $expected_style_handle = 'block-theme-example-block-editor-style';
+               wp_register_style( $expected_style_handle, false );
+               switch_theme( 'block-theme' );
+
+               $metadata = array(
+                       'file'        => wp_normalize_path( get_theme_file_path( 'blocks/example-block/block.json' ) ),
+                       'name'        => 'block-theme/example-block',
+                       'editorStyle' => 'file:./editor-style.css',
+               );
+               $result   = register_block_style_handle( $metadata, 'editorStyle' );
+
+               $this->assertSame( $expected_style_handle, $result );
+       }
+
+       /**
</ins><span class="cx" style="display: block; padding: 0 10px">          * Tests that the function returns false when the `block.json` is not found
</span><span class="cx" style="display: block; padding: 0 10px">         * in the WordPress core.
</span><span class="cx" style="display: block; padding: 0 10px">         *
</span></span></pre></div>
<a id="trunktestsphpunittestsblocksregisterCoreBlockStyleHandlesphp"></a>
<div class="addfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Added: trunk/tests/phpunit/tests/blocks/registerCoreBlockStyleHandles.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/tests/phpunit/tests/blocks/registerCoreBlockStyleHandles.php                                (rev 0)
+++ trunk/tests/phpunit/tests/blocks/registerCoreBlockStyleHandles.php  2023-06-26 21:15:21 UTC (rev 56044)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -0,0 +1,124 @@
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+<?php
+
+/**
+ * Tests for block style handles.
+ *
+ * @package WordPress
+ * @subpackage Blocks
+ *
+ * @since 6.3.0
+ *
+ * @group blocks
+ *
+ * @covers ::register_core_block_style_handles
+ */
+class Tests_Blocks_registerCoreBlockStyleHandles extends WP_UnitTestCase {
+
+       /**
+        * @var WP_Styles
+        */
+       private $old_wp_styles;
+
+       /**
+        * @var string
+        */
+       private $includes_url;
+
+       const STYLE_FIELDS = array(
+               'style'       => 'style',
+               'editorStyle' => 'editor',
+       );
+
+       public function set_up() {
+               parent::set_up();
+
+               $this->old_wp_styles = $GLOBALS['wp_styles'];
+
+               $this->includes_url = includes_url();
+
+               remove_action( 'wp_default_styles', 'wp_default_styles' );
+
+               if ( empty( $GLOBALS['wp_styles'] ) ) {
+                       $GLOBALS['wp_styles'] = null;
+               }
+       }
+
+       public function tear_down() {
+               $GLOBALS['wp_styles'] = $this->old_wp_styles;
+
+               add_action( 'wp_default_styles', 'wp_default_styles' );
+
+               parent::tear_down();
+       }
+
+       /**
+        * @ticket 58528
+        *
+        * @dataProvider data_block_data
+        */
+       public function test_wp_should_load_separate_core_block_assets_false( $name, $schema ) {
+               register_core_block_style_handles();
+
+               foreach ( self::STYLE_FIELDS as $style_field => $filename ) {
+                       $style_handle = $schema[ $style_field ];
+                       if ( is_array( $style_handle ) ) {
+                               continue;
+                       }
+
+                       $this->assertArrayNotHasKey( $style_handle, $GLOBALS['wp_styles']->registered, 'The key should not exist, as this style should not be registered' );
+               }
+       }
+
+
+       /**
+        * @ticket 58528
+        *
+        * @dataProvider data_block_data
+        */
+       public function test_wp_should_load_separate_core_block_assets_true( $name, $schema ) {
+               add_filter( 'should_load_separate_core_block_assets', '__return_true' );
+               register_core_block_style_handles();
+
+               $wp_styles = $GLOBALS['wp_styles'];
+
+               foreach ( self::STYLE_FIELDS as $style_field => $filename ) {
+                       $style_handle = $schema[ $style_field ];
+                       if ( is_array( $style_handle ) ) {
+                               continue;
+                       }
+
+                       $this->assertArrayHasKey( $style_handle, $wp_styles->registered, 'The key should exist, as this style should be registered' );
+                       if ( false === $wp_styles->registered[ $style_handle ]->src ) {
+                               $this->assertEmpty( $wp_styles->registered[ $style_handle ]->extra, 'If source is false, not style path should be set' );
+                       } else {
+                               $this->assertStringContainsString( $this->includes_url, $wp_styles->registered[ $style_handle ]->src, 'Source of style should contain the includes url' );
+                               $this->assertNotEmpty( $wp_styles->registered[ $style_handle ]->extra, 'The path of the style should exist' );
+                               $this->assertArrayHasKey( 'path', $wp_styles->registered[ $style_handle ]->extra, 'The path key of the style should exist in extra array' );
+                               $this->assertNotEmpty( $wp_styles->registered[ $style_handle ]->extra['path'], 'The path key of the style should not be empty' );
+                       }
+               }
+       }
+
+
+       public function data_block_data() {
+               $core_blocks_meta = require ABSPATH . WPINC . '/blocks/blocks-json.php';
+
+               // Remove this blocks for now, as they are registered elsewhere.
+               unset( $core_blocks_meta['archives'] );
+               unset( $core_blocks_meta['widget-group'] );
+
+               $data = array();
+               foreach ( $core_blocks_meta as $name => $schema ) {
+                       if ( ! isset( $schema['style'] ) ) {
+                               $schema['style'] = "wp-block-$name";
+                       }
+                       if ( ! isset( $schema['editorStyle'] ) ) {
+                               $schema['editorStyle'] = "wp-block-{$name}-editor";
+                       }
+
+                       $data[ $name ] = array( $name, $schema );
+               }
+
+               return $data;
+       }
+}
</ins><span class="cx" style="display: block; padding: 0 10px">Property changes on: trunk/tests/phpunit/tests/blocks/registerCoreBlockStyleHandles.php
</span><span class="cx" style="display: block; padding: 0 10px">___________________________________________________________________
</span></span></pre></div>
<a id="svneolstyle"></a>
<div class="addfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Added: svn:eol-style</h4></div>
<ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+native
</ins><span class="cx" style="display: block; padding: 0 10px">\ No newline at end of property
</span></div>

</body>
</html>