Skip to content

Commit

Permalink
Add basic PHPUnit tests for block metadata registration
Browse files Browse the repository at this point in the history
  • Loading branch information
gziolo committed Mar 12, 2020
1 parent 39046f9 commit 3345144
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 6 deletions.
9 changes: 3 additions & 6 deletions lib/compat.php
Expand Up @@ -8,15 +8,13 @@
* @package gutenberg
*/

if ( ! function_exists( 'register_block_type_from_metadata' ) ) {
if ( ! function_exists( 'register_block_type_from_metadata' ) ) {
/**
* Registers a block type from metadata stored in the `block.json` file.
*
* @since 7.8.0
*
* @param string $path Path to the folder where the `block.json` file is located.
* Alternatively it's possible to provide only the name of the
* last folder for blocks located in the WordPress core.
* @param array $args {
* Optional. Array of block type arguments. Any arguments may be defined, however the
* ones described below are supported by default. Default empty array.
Expand All @@ -26,13 +24,12 @@
* @return WP_Block_Type|false The registered block type on success, or false on failure.
*/
function register_block_type_from_metadata( $path, $args = array() ) {
$path = is_dir( $path ) ? $path : ABSPATH . WPINC . '/blocks/' . $path;
$file = trailingslashit( $path ) . 'block.json';
if ( ! file_exists( $file ) ) {
return false;
}

$metadata = json_decode( file_get_contents( $path ), true );
$metadata = json_decode( file_get_contents( $file ), true );
if ( ! is_array( $metadata ) ) {
return false;
}
Expand All @@ -45,7 +42,7 @@ function register_block_type_from_metadata( $path, $args = array() ) {
)
);
}
}
}

/**
* Adds a polyfill for the WHATWG URL in environments which do not support it.
Expand Down
45 changes: 45 additions & 0 deletions phpunit/class-register-block-type-from-metadata-test.php
@@ -0,0 +1,45 @@
<?php
/**
* Test `register_block_type_from_metadata`.
*
* @package Gutenberg
*/

class Register_Block_Type_From_Metadata_Test extends WP_UnitTestCase {
/**
* Tests that the function returns false when the `block.json` is not found
* in the WordPress core.
*/
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.
*/
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.
*/
function test_block_registers_with_metadata_fixture() {
$result = register_block_type_from_metadata(
__DIR__ . '/fixtures',
array(
'foo' => 'bar',
)
);

$this->assertInstanceOf( 'WP_Block_Type', $result );
$this->assertEquals( 'test/block-name', $result->name );
$this->assertEquals( 'bar', $result->foo );
}
}
4 changes: 4 additions & 0 deletions phpunit/fixtures/block.json
@@ -0,0 +1,4 @@
{
"name": "test/block-name",
"category": "widgets"
}

0 comments on commit 3345144

Please sign in to comment.