Skip to content

Commit

Permalink
Add basic unit tests to cover functions that register handles
Browse files Browse the repository at this point in the history
  • Loading branch information
gziolo committed Jun 4, 2020
1 parent 1851534 commit 7a1b936
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 12 deletions.
32 changes: 20 additions & 12 deletions lib/compat.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,13 @@ function gutenberg_generate_block_asset_handle( $block_name, $field_name ) {
* @param array $metadata Block metadata.
* @param string $field_name Field name to pick from metadata.
*
* @return string Script handle provided directly or created through script's registration.
* @return string|boolean Script handle provided directly or created through
* script's registration, or false on failure.
*/
function gutenberg_get_script_handle( $metadata, $field_name ) {
function register_block_script_handle( $metadata, $field_name ) {
if ( empty( $metadata[ $field_name ] ) ) {
return false;
}
$script_handle = $metadata[ $field_name ];
$script_path = gutenberg_remove_block_asset_path_prefix( $metadata[ $field_name ] );
if ( $script_handle === $script_path ) {
Expand All @@ -88,13 +92,13 @@ function gutenberg_get_script_handle( $metadata, $field_name ) {
return false;
}
$script_asset = require( $script_asset_path );
wp_register_script(
$result = wp_register_script(
$script_handle,
plugins_url( $script_path, $metadata['file'] ),
$script_asset['dependencies'],
$script_asset['version']
);
return $script_handle;
return $result ? $script_handle : false;
}

/**
Expand All @@ -107,9 +111,13 @@ function gutenberg_get_script_handle( $metadata, $field_name ) {
* @param array $metadata Block metadata.
* @param string $field_name Field name to pick from metadata.
*
* @return string Style handle provided directly or created through style's registration.
* @return string|boolean Style handle provided directly or created through
* style's registration, or false on failure.
*/
function gutenberg_get_style_handle( $metadata, $field_name ) {
function register_block_style_handle( $metadata, $field_name ) {
if ( empty( $metadata[ $field_name ] ) ) {
return false;
}
$style_handle = $metadata[ $field_name ];
$style_path = gutenberg_remove_block_asset_path_prefix( $metadata[ $field_name ] );
if ( $style_handle === $style_path ) {
Expand All @@ -118,13 +126,13 @@ function gutenberg_get_style_handle( $metadata, $field_name ) {

$style_handle = gutenberg_generate_block_asset_handle( $metadata['name'], $field_name );
$block_dir = dirname( $metadata['file'] );
wp_register_style(
$result = wp_register_style(
$style_handle,
plugins_url( $style_path, $metadata['file'] ),
array(),
filemtime( realpath( "$block_dir/$style_path" ) )
);
return $style_handle;
return $result ? $style_handle : false;
}

/**
Expand Down Expand Up @@ -178,28 +186,28 @@ function register_block_type_from_metadata( $file_or_folder, $args = array() ) {
}

if ( ! empty( $metadata['editorScript'] ) ) {
$settings['editor_script'] = gutenberg_get_script_handle(
$settings['editor_script'] = register_block_script_handle(
$metadata,
'editorScript'
);
}

if ( ! empty( $metadata['script'] ) ) {
$settings['script'] = gutenberg_get_script_handle(
$settings['script'] = register_block_script_handle(
$metadata,
'script'
);
}

if ( ! empty( $metadata['editorStyle'] ) ) {
$settings['editor_style'] = gutenberg_get_style_handle(
$settings['editor_style'] = register_block_style_handle(
$metadata,
'editorStyle'
);
}

if ( ! empty( $metadata['style'] ) ) {
$settings['style'] = gutenberg_get_style_handle(
$settings['style'] = register_block_style_handle(
$metadata,
'style'
);
Expand Down
44 changes: 44 additions & 0 deletions phpunit/class-register-block-type-from-metadata-test.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,50 @@ function test_generate_block_asset_handle() {
);
}

function test_field_not_found_register_block_script_handle() {
$result = register_block_script_handle( array(), 'script' );

$this->assertFalse( $result );
}

function test_empty_value_register_block_script_handle() {
$metadata = array( 'script' => '' );
$result = register_block_script_handle( $metadata, 'script' );

$this->assertFalse( $result );
}

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 );
}

function test_field_not_found_register_block_style_handle() {
$result = register_block_script_handle( array(), 'style' );

$this->assertFalse( $result );
}

function test_empty_value_found_register_block_style_handle() {
$metadata = array( 'style' => '' );
$result = register_block_script_handle( $metadata, 'style' );

$this->assertFalse( $result );
}

function test_handle_passed_register_block_style_handle() {
$metadata = array(
'style' => 'test-style-handle',
);
$result = register_block_script_handle( $metadata, 'style' );

$this->assertSame( 'test-style-handle', $result );
}

/**
* Tests that the function returns false when the `block.json` is not found
* in the WordPress core.
Expand Down

0 comments on commit 7a1b936

Please sign in to comment.