Skip to content

Commit

Permalink
Update unit tests for register_block_from_metadata
Browse files Browse the repository at this point in the history
  • Loading branch information
gziolo committed May 22, 2020
1 parent d69772f commit b86a456
Show file tree
Hide file tree
Showing 4 changed files with 89 additions and 17 deletions.
6 changes: 3 additions & 3 deletions docs/rfc/block-registration.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ This RFC is intended to serve both as a specification and as documentation for t

Behind any block type registration is some abstract concept of a unit of content. This content type can be described without consideration of any particular technology. In much the same way, we should be able to describe the core constructs of a block type in a way which can be interpreted in any runtime.

In more practical terms, an implementation should fulfill requirements that...
In more practical terms, an implementation should fulfill requirements that:

* A block type registration should be declarative and context-agnostic. Any runtime (PHP, JS, or other) should be able to interpret the basics of a block type (see "Block API" in the sections below) and should be able to fetch or retrieve the definitions of the context-specific implementation details. The following things should be made possible:
* Fetching the available block types through REST APIs.
Expand All @@ -16,7 +16,7 @@ In more practical terms, an implementation should fulfill requirements that...

It can statically analyze the files of any plugin to retrieve blocks and their properties.
* It should not require a build tool compilation step (e.g. Babel, Webpack) to author code which would be referenced in a block type definition.
* There should allow the potential to dynamically load ("lazy-load") block types, or parts of block type definitions. It practical terms, it means that the editor should be able to be loaded without enqueuing all the assets (scripts and styles) of all block types. What it needs is the basic metadata (`title`, `description`, `category`, `icon`, etc...) to start with. It should be fine to defer loading all other code (`edit`, `save`, `transforms`, and other JavaScript implementations) until it is explicitly used (inserted into the post content).
* There should allow the potential to dynamically load ("lazy-load") block types, or parts of block type definitions. It practical terms, it means that the editor should be able to be loaded without enqueuing all the assets (scripts and styles) of all block types. What it needs is the basic metadata (`title`, `description`, `category`, `icon`, etc) to start with. It should be fine to defer loading all other code (`edit`, `save`, `transforms`, and other JavaScript implementations) until it is explicitly used (inserted into the post content).

## References

Expand Down Expand Up @@ -67,7 +67,7 @@ To register a new block type, start by creating a `block.json` file. This file:
"category": "common",
"parent": [ "core/group" ],
"icon": "star",
"description": "Shows warning, error or success notices ...",
"description": "Shows warning, error or success notices",
"keywords": [ "alert", "message" ],
"textDomain": "my-plugin",
"attributes": {
Expand Down
18 changes: 12 additions & 6 deletions lib/compat.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,12 +66,15 @@ function register_block_type_from_metadata( $path, $args = array() ) {
$block_dir . '/' . substr_replace( $editor_script, '.asset.php', -3 )
);
if ( ! file_exists( $editor_script_asset_path ) ) {
/* translators: %s: Block name. */
$message = sprintf( __( 'The asset file for the "editorScript" defined in "%s" block definition is missing.' ), $block_name );
$message = sprintf(
/* translators: %s: Block name. */
__( 'The asset file for the "editorScript" defined in "%s" block definition is missing.', 'default' ),
$block_name
);
_doing_it_wrong( __METHOD__, $message, '5.5.0' );
return false;
}
$editor_script_asset = require( $editor_script_asset_path );
$editor_script_asset = require( $editor_script_asset_path );
wp_register_script(
$editor_script_handle,
plugins_url( $editor_script, $metadata_file ),
Expand All @@ -88,12 +91,15 @@ function register_block_type_from_metadata( $path, $args = array() ) {
$block_dir . '/' . substr_replace( $script, '.asset.php', -3 )
);
if ( ! file_exists( $script_asset_path ) ) {
/* translators: %s: Block name. */
$message = sprintf( __( 'The asset file for the "script" defined in "%s" block definition is missing.' ), $block_name );
$message = sprintf(
/* translators: %s: Block name. */
__( 'The asset file for the "script" defined in "%s" block definition is missing.', 'default' ),
$block_name
);
_doing_it_wrong( __METHOD__, $message, '5.5.0' );
return false;
}
$script_asset = require( $script_asset_path );
$script_asset = require( $script_asset_path );
wp_register_script(
$script_handle,
plugins_url( $script, $metadata_file ),
Expand Down
45 changes: 39 additions & 6 deletions phpunit/class-register-block-type-from-metadata-test.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,47 @@ function test_metadata_not_found_in_the_current_directory() {
*/
function test_block_registers_with_metadata_fixture() {
$result = register_block_type_from_metadata(
__DIR__ . '/fixtures',
array(
'foo' => 'bar',
)
__DIR__ . '/fixtures'
);

$this->assertInstanceOf( 'WP_Block_Type', $result );
$this->assertEquals( 'test/block-name', $result->name );
$this->assertEquals( 'bar', $result->foo );
$this->assertEquals( 'my-plugin/notice', $result->name );
$this->assertEquals( 'Notice', $result->title );
$this->assertEquals( 'common', $result->category );
$this->assertEquals( array( 'core/group' ), $result->parent );
$this->assertEquals( 'star', $result->icon );
$this->assertEquals( 'Shows warning, error or success notices…', $result->description );
$this->assertEquals( array( 'alert', 'message' ), $result->keywords );
$this->assertEquals(
array(
'message' => array(
'type' => 'string',
'source' => 'html',
'selector' => '.message',
),
),
$result->attributes
);
$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
);
}
}
37 changes: 35 additions & 2 deletions phpunit/fixtures/block.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,37 @@
{
"name": "test/block-name",
"category": "widgets"
"name": "my-plugin/notice",
"title": "Notice",
"category": "common",
"parent": [
"core/group"
],
"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
},
"styleVariations": [
{
"name": "default",
"label": "Default",
"isDefault": true
},
{
"name": "other",
"label": "Other"
}
]
}

0 comments on commit b86a456

Please sign in to comment.