Editor: Add viewStyle property to block.json for frontend-only block styles

Related issue in Gutenberg: https://github.com/WordPress/gutenberg/issues/54491.

For block scripts there was already `script`, `viewScript` and `editorScript`. For block styles there was only `style` and `editorStyle`. This brings the parity.

Props gaambo.
Fixes #59673. 



git-svn-id: https://develop.svn.wordpress.org/trunk@57493 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Greg Ziółkowski
2024-01-31 09:09:22 +00:00
parent dc15d66ee6
commit 1a5ff3fc6d
8 changed files with 68 additions and 4 deletions
@@ -0,0 +1 @@
/* Test front end only CSS file */
@@ -70,5 +70,6 @@
"viewScript": [ "tests-notice-view-script", "tests-notice-view-script-2" ],
"editorStyle": "tests-notice-editor-style",
"style": [ "tests-notice-style", "tests-notice-style-2" ],
"viewStyle": [ "tests-notice-view-style" ],
"render": "file:./render.php"
}
+34 -3
View File
@@ -156,6 +156,12 @@ class Tests_Blocks_Register extends WP_UnitTestCase {
'unit-tests-my-block-style',
generate_block_asset_handle( $block_name, 'style' )
);
// @ticket 59673
$this->assertSame(
'unit-tests-my-block-view-style',
generate_block_asset_handle( $block_name, 'viewStyle' ),
'asset handle for viewStyle is not generated correctly'
);
}
/**
@@ -439,9 +445,10 @@ class Tests_Blocks_Register extends WP_UnitTestCase {
*/
public function test_success_register_block_style_handle() {
$metadata = array(
'file' => DIR_TESTDATA . '/blocks/notice/block.json',
'name' => 'unit-tests/test-block',
'style' => 'file:./block.css',
'file' => DIR_TESTDATA . '/blocks/notice/block.json',
'name' => 'unit-tests/test-block',
'style' => 'file:./block.css',
'viewStyle' => 'file:./block-view.css',
);
$result = register_block_style_handle( $metadata, 'style' );
@@ -454,6 +461,17 @@ class Tests_Blocks_Register extends WP_UnitTestCase {
wp_normalize_path( wp_styles()->get_data( 'unit-tests-test-block-style', 'path' ) )
);
// Test viewStyle property
$result = register_block_style_handle( $metadata, 'viewStyle' );
$this->assertSame( 'unit-tests-test-block-view-style', $result );
// @ticket 59673
$this->assertSame(
wp_normalize_path( realpath( DIR_TESTDATA . '/blocks/notice/block-view.css' ) ),
wp_normalize_path( wp_styles()->get_data( 'unit-tests-test-block-view-style', 'path' ) ),
'viewStyle asset path is not correct'
);
// Test the behavior directly within the unit test
$this->assertFalse(
strpos(
@@ -837,6 +855,12 @@ class Tests_Blocks_Register extends WP_UnitTestCase {
array( 'tests-notice-style', 'tests-notice-style-2' ),
$result->style_handles
);
// @ticket 59673
$this->assertSameSets(
array( 'tests-notice-view-style' ),
$result->view_style_handles,
'parsed view_style_handles is not correct'
);
// @ticket 50328
$this->assertSame(
@@ -844,6 +868,13 @@ class Tests_Blocks_Register extends WP_UnitTestCase {
wp_normalize_path( wp_styles()->get_data( 'unit-tests-test-block-style', 'path' ) )
);
// @ticket 59673
$this->assertSame(
wp_normalize_path( realpath( DIR_TESTDATA . '/blocks/notice/block-view.css' ) ),
wp_normalize_path( wp_styles()->get_data( 'unit-tests-test-block-view-style', 'path' ) ),
'viewStyle asset path is not correct'
);
// @ticket 53148
$this->assertIsCallable( $result->render_callback );
}
@@ -557,7 +557,7 @@ class REST_Block_Type_Controller_Test extends WP_Test_REST_Controller_Testcase {
$response = rest_get_server()->dispatch( $request );
$data = $response->get_data();
$properties = $data['schema']['properties'];
$this->assertCount( 30, $properties );
$this->assertCount( 31, $properties );
$this->assertArrayHasKey( 'api_version', $properties );
$this->assertArrayHasKey( 'name', $properties );
$this->assertArrayHasKey( 'title', $properties );
@@ -582,6 +582,7 @@ class REST_Block_Type_Controller_Test extends WP_Test_REST_Controller_Testcase {
$this->assertArrayHasKey( 'view_script_handles', $properties );
$this->assertArrayHasKey( 'editor_style_handles', $properties );
$this->assertArrayHasKey( 'style_handles', $properties );
$this->assertArrayHasKey( 'view_style_handles', $properties, 'schema must contain view_style_handles' );
$this->assertArrayHasKey( 'is_dynamic', $properties );
// Deprecated properties.
$this->assertArrayHasKey( 'editor_script', $properties );