mirror of
https://github.com/gosticks/wordpress-develop.git
synced 2026-03-31 18:54:29 +00:00
Editor: Introduce new API method that register block from block.json metadata file
Backports changes added to Gutenberg in: - https://github.com/WordPress/gutenberg/pull/20794 - https://github.com/WordPress/gutenberg/pull/22519 `register_block_type_from_metadata` function is going to be used to register all blocks on the server using `block.json` metadata files. Props ocean90, azaozz, aduth, mcsf, jorgefilipecosta, spacedmonkey, nosolosw, swissspidy and noahtallen. Fixes #50263. git-svn-id: https://develop.svn.wordpress.org/trunk@48141 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
6
tests/phpunit/tests/blocks/fixtures/block.asset.php
Normal file
6
tests/phpunit/tests/blocks/fixtures/block.asset.php
Normal file
@@ -0,0 +1,6 @@
|
||||
<?php
|
||||
|
||||
return array(
|
||||
'dependencies' => array(),
|
||||
'version' => 'test',
|
||||
);
|
||||
1
tests/phpunit/tests/blocks/fixtures/block.css
Normal file
1
tests/phpunit/tests/blocks/fixtures/block.css
Normal file
@@ -0,0 +1 @@
|
||||
/* Test CSS file */
|
||||
1
tests/phpunit/tests/blocks/fixtures/block.js
Normal file
1
tests/phpunit/tests/blocks/fixtures/block.js
Normal file
@@ -0,0 +1 @@
|
||||
/* Test JavaScript file. */
|
||||
52
tests/phpunit/tests/blocks/fixtures/block.json
Normal file
52
tests/phpunit/tests/blocks/fixtures/block.json
Normal file
@@ -0,0 +1,52 @@
|
||||
{
|
||||
"name": "my-plugin/notice",
|
||||
"title": "Notice",
|
||||
"category": "common",
|
||||
"parent": [
|
||||
"core/group"
|
||||
],
|
||||
"providesContext": {
|
||||
"my-plugin/message": "message"
|
||||
},
|
||||
"usesContext": [
|
||||
"groupId"
|
||||
],
|
||||
"icon": "star",
|
||||
"description": "Shows warning, error or success notices…",
|
||||
"keywords": [
|
||||
"alert",
|
||||
"message"
|
||||
],
|
||||
"textDomain": "my-plugin",
|
||||
"attributes": {
|
||||
"message": {
|
||||
"type": "string",
|
||||
"source": "html",
|
||||
"selector": ".message"
|
||||
}
|
||||
},
|
||||
"supports": {
|
||||
"align": true,
|
||||
"lightBlockWrapper": true
|
||||
},
|
||||
"styles": [
|
||||
{
|
||||
"name": "default",
|
||||
"label": "Default",
|
||||
"isDefault": true
|
||||
},
|
||||
{
|
||||
"name": "other",
|
||||
"label": "Other"
|
||||
}
|
||||
],
|
||||
"example": {
|
||||
"attributes": {
|
||||
"message": "This is a notice!"
|
||||
}
|
||||
},
|
||||
"editorScript": "my-plugin-notice-editor-script",
|
||||
"script": "my-plugin-notice-script",
|
||||
"editorStyle": "my-plugin-notice-editor-style",
|
||||
"style": "my-plugin-notice-style"
|
||||
}
|
||||
@@ -102,6 +102,248 @@ class WP_Test_Block_Register extends WP_UnitTestCase {
|
||||
$this->assertFalse( $registry->is_registered( $name ) );
|
||||
}
|
||||
|
||||
/**
|
||||
* @ticket 50263
|
||||
*/
|
||||
function test_does_not_remove_block_asset_path_prefix() {
|
||||
$result = remove_block_asset_path_prefix( 'script-handle' );
|
||||
|
||||
$this->assertSame( 'script-handle', $result );
|
||||
}
|
||||
|
||||
/**
|
||||
* @ticket 50263
|
||||
*/
|
||||
function test_removes_block_asset_path_prefix() {
|
||||
$result = remove_block_asset_path_prefix( 'file:./block.js' );
|
||||
|
||||
$this->assertSame( './block.js', $result );
|
||||
}
|
||||
|
||||
/**
|
||||
* @ticket 50263
|
||||
*/
|
||||
function test_generate_block_asset_handle() {
|
||||
$block_name = 'unit-tests/my-block';
|
||||
|
||||
$this->assertSame(
|
||||
'unit-tests-my-block-editor-script',
|
||||
generate_block_asset_handle( $block_name, 'editorScript' )
|
||||
);
|
||||
$this->assertSame(
|
||||
'unit-tests-my-block-script',
|
||||
generate_block_asset_handle( $block_name, 'script' )
|
||||
);
|
||||
$this->assertSame(
|
||||
'unit-tests-my-block-editor-style',
|
||||
generate_block_asset_handle( $block_name, 'editorStyle' )
|
||||
);
|
||||
$this->assertSame(
|
||||
'unit-tests-my-block-style',
|
||||
generate_block_asset_handle( $block_name, 'style' )
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @ticket 50263
|
||||
*/
|
||||
function test_field_not_found_register_block_script_handle() {
|
||||
$result = register_block_script_handle( array(), 'script' );
|
||||
|
||||
$this->assertFalse( $result );
|
||||
}
|
||||
|
||||
/**
|
||||
* @ticket 50263
|
||||
*/
|
||||
function test_empty_value_register_block_script_handle() {
|
||||
$metadata = array( 'script' => '' );
|
||||
$result = register_block_script_handle( $metadata, 'script' );
|
||||
|
||||
$this->assertFalse( $result );
|
||||
}
|
||||
|
||||
/**
|
||||
* @expectedIncorrectUsage register_block_script_handle
|
||||
* @ticket 50263
|
||||
*/
|
||||
function test_missing_asset_file_register_block_script_handle() {
|
||||
$metadata = array(
|
||||
'file' => __FILE__,
|
||||
'name' => 'unit-tests/test-block',
|
||||
'script' => 'file:./fixtures/missing-asset.js',
|
||||
);
|
||||
$result = register_block_script_handle( $metadata, 'script' );
|
||||
|
||||
$this->assertFalse( $result );
|
||||
}
|
||||
|
||||
/**
|
||||
* @ticket 50263
|
||||
*/
|
||||
function test_handle_passed_register_block_script_handle() {
|
||||
$metadata = array(
|
||||
'editorScript' => 'test-script-handle',
|
||||
);
|
||||
$result = register_block_script_handle( $metadata, 'editorScript' );
|
||||
|
||||
$this->assertSame( 'test-script-handle', $result );
|
||||
}
|
||||
|
||||
/**
|
||||
* @ticket 50263
|
||||
*/
|
||||
function test_success_register_block_script_handle() {
|
||||
$metadata = array(
|
||||
'file' => __FILE__,
|
||||
'name' => 'unit-tests/test-block',
|
||||
'script' => 'file:./fixtures/block.js',
|
||||
);
|
||||
$result = register_block_script_handle( $metadata, 'script' );
|
||||
|
||||
$this->assertSame( 'unit-tests-test-block-script', $result );
|
||||
}
|
||||
|
||||
/**
|
||||
* @ticket 50263
|
||||
*/
|
||||
function test_field_not_found_register_block_style_handle() {
|
||||
$result = register_block_style_handle( array(), 'style' );
|
||||
|
||||
$this->assertFalse( $result );
|
||||
}
|
||||
|
||||
/**
|
||||
* @ticket 50263
|
||||
*/
|
||||
function test_empty_value_found_register_block_style_handle() {
|
||||
$metadata = array( 'style' => '' );
|
||||
$result = register_block_style_handle( $metadata, 'style' );
|
||||
|
||||
$this->assertFalse( $result );
|
||||
}
|
||||
|
||||
/**
|
||||
* @ticket 50263
|
||||
*/
|
||||
function test_handle_passed_register_block_style_handle() {
|
||||
$metadata = array(
|
||||
'style' => 'test-style-handle',
|
||||
);
|
||||
$result = register_block_style_handle( $metadata, 'style' );
|
||||
|
||||
$this->assertSame( 'test-style-handle', $result );
|
||||
}
|
||||
|
||||
/**
|
||||
* @ticket 50263
|
||||
*/
|
||||
function test_success_register_block_style_handle() {
|
||||
$metadata = array(
|
||||
'file' => __FILE__,
|
||||
'name' => 'unit-tests/test-block',
|
||||
'style' => 'file:./fixtures/block.css',
|
||||
);
|
||||
$result = register_block_style_handle( $metadata, 'style' );
|
||||
|
||||
$this->assertSame( 'unit-tests-test-block-style', $result );
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests that the function returns false when the `block.json` is not found
|
||||
* in the WordPress core.
|
||||
*
|
||||
* @ticket 50263
|
||||
*/
|
||||
function test_metadata_not_found_in_wordpress_core() {
|
||||
$result = register_block_type_from_metadata( 'unknown' );
|
||||
|
||||
$this->assertFalse( $result );
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests that the function returns false when the `block.json` is not found
|
||||
* in the current directory.
|
||||
*
|
||||
* @ticket 50263
|
||||
*/
|
||||
function test_metadata_not_found_in_the_current_directory() {
|
||||
$result = register_block_type_from_metadata( __DIR__ );
|
||||
|
||||
$this->assertFalse( $result );
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests that the function returns the registered block when the `block.json`
|
||||
* is found in the fixtures directory.
|
||||
*
|
||||
* @ticket 50263
|
||||
*/
|
||||
function test_block_registers_with_metadata_fixture() {
|
||||
$result = register_block_type_from_metadata(
|
||||
__DIR__ . '/fixtures'
|
||||
);
|
||||
|
||||
$this->assertInstanceOf( 'WP_Block_Type', $result );
|
||||
$this->assertSame( 'my-plugin/notice', $result->name );
|
||||
$this->assertSame( 'Notice', $result->title );
|
||||
$this->assertSame( 'common', $result->category );
|
||||
$this->assertEqualSets( array( 'core/group' ), $result->parent );
|
||||
$this->assertSame( 'star', $result->icon );
|
||||
$this->assertSame( 'Shows warning, error or success notices…', $result->description );
|
||||
$this->assertEqualSets( array( 'alert', 'message' ), $result->keywords );
|
||||
$this->assertEquals(
|
||||
array(
|
||||
'message' => array(
|
||||
'type' => 'string',
|
||||
'source' => 'html',
|
||||
'selector' => '.message',
|
||||
),
|
||||
),
|
||||
$result->attributes
|
||||
);
|
||||
$this->assertEquals(
|
||||
array(
|
||||
'my-plugin/message' => 'message',
|
||||
),
|
||||
$result->provides_context
|
||||
);
|
||||
$this->assertEqualSets( array( 'groupId' ), $result->uses_context );
|
||||
$this->assertEquals(
|
||||
array(
|
||||
'align' => true,
|
||||
'lightBlockWrapper' => true,
|
||||
),
|
||||
$result->supports
|
||||
);
|
||||
$this->assertEquals(
|
||||
array(
|
||||
array(
|
||||
'name' => 'default',
|
||||
'label' => 'Default',
|
||||
'isDefault' => true,
|
||||
),
|
||||
array(
|
||||
'name' => 'other',
|
||||
'label' => 'Other',
|
||||
),
|
||||
),
|
||||
$result->styles
|
||||
);
|
||||
$this->assertEquals(
|
||||
array(
|
||||
'attributes' => array(
|
||||
'message' => 'This is a notice!',
|
||||
),
|
||||
),
|
||||
$result->example
|
||||
);
|
||||
$this->assertSame( 'my-plugin-notice-editor-script', $result->editor_script );
|
||||
$this->assertSame( 'my-plugin-notice-script', $result->script );
|
||||
$this->assertSame( 'my-plugin-notice-editor-style', $result->editor_style );
|
||||
$this->assertSame( 'my-plugin-notice-style', $result->style );
|
||||
}
|
||||
|
||||
/**
|
||||
* @ticket 45109
|
||||
*/
|
||||
|
||||
Reference in New Issue
Block a user