Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

register_block_type: Accept editor_script array for back-compat #3487

Closed
Show file tree
Hide file tree
Changes from 18 commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
4fbea48
Add unit test for register_block_type with editor_script array
ockham Oct 18, 2022
8a8bfc0
Continue to support editor_script array for back-compat
ockham Oct 18, 2022
efbc779
Fix indentation
ockham Oct 18, 2022
e1d9ef1
Fix indentation, again
ockham Oct 18, 2022
645683c
Remove trailing comma
ockham Oct 18, 2022
8c43473
Fix unit test
ockham Oct 18, 2022
c4d897c
Fix getter
ockham Oct 18, 2022
65eedef
Extend test coverage
ockham Oct 19, 2022
e3e4b50
Make array contiguous
ockham Oct 19, 2022
37af4ff
Have setter override entire array rather than just first element
ockham Oct 19, 2022
37b9486
Update PHPDoc
ockham Oct 19, 2022
3731af8
Add REST API endpoint test coverage
ockham Oct 19, 2022
1b6afb8
Add logic and test coverage to return first array item
ockham Oct 19, 2022
7c1c7f7
Add isset check
ockham Oct 19, 2022
faa8238
Use get_registered()
ockham Oct 20, 2022
dfbed22
Whitespace
ockham Oct 20, 2022
034a7ff
Use get_registered in other test
ockham Oct 20, 2022
422744f
Indentation
ockham Oct 20, 2022
d8869a1
Missing whitespace in parens
ockham Oct 20, 2022
45775e0
Add property_exists and is_array checks
ockham Oct 20, 2022
9bc56a5
Add messages to assertions
ockham Oct 20, 2022
afb21ea
Assert that we have the editor_script and editor_script_handles prope…
ockham Oct 20, 2022
1d5bce5
Assert that we have the editor_script and editor_script_handles prope…
ockham Oct 20, 2022
203f997
Use ! empty() and array_shift()
ockham Oct 20, 2022
a93657f
Whitespace
ockham Oct 20, 2022
fe7999a
More whitespace
ockham Oct 20, 2022
d2788d8
Remove property assertions for virtual properties
ockham Oct 20, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 28 additions & 4 deletions src/wp-includes/class-wp-block-type.php
Original file line number Diff line number Diff line change
Expand Up @@ -295,15 +295,19 @@ public function __construct( $block_type, $args = array() ) {
*
* @param string $name Deprecated property name.
*
* @return string|null|void The value read from the new property if the first item in the array provided,
* null when value not found, or void when unknown property name provided.
* @return string|string[]|null|void The value read from the new property if the first item in the array provided,
* null when value not found, or void when unknown property name provided.
*/
public function __get( $name ) {
if ( ! in_array( $name, $this->deprecated_properties ) ) {
return;
}

$new_name = $name . '_handles';

if ( count( $this->{$new_name} ) > 1 ) {
ockham marked this conversation as resolved.
Show resolved Hide resolved
return $this->{$new_name};
ockham marked this conversation as resolved.
Show resolved Hide resolved
}
return isset( $this->{$new_name}[0] ) ? $this->{$new_name}[0] : null;
}

Expand Down Expand Up @@ -343,12 +347,32 @@ public function __set( $name, $value ) {
return;
}

$new_name = $name . '_handles';

if ( is_array( $value ) ) {
$filtered = array_filter( $value, 'is_string' );

if ( count( $filtered ) !== count( $value ) ) {
_doing_it_wrong(
__METHOD__,
sprintf(
/* translators: %s: The '$value' argument. */
__( 'The %s argument must be a string or a string array.' ),
'<code>$value</code>'
),
'6.1.0'
);
}

$this->{$new_name} = array_values( $filtered );
return;
}

if ( ! is_string( $value ) ) {
return;
}

$new_name = $name . '_handles';
$this->{$new_name}[0] = $value;
$this->{$new_name} = array( $value );
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -295,6 +295,10 @@ public function prepare_item_for_response( $item, $request ) {
if ( rest_is_field_included( $extra_field, $fields ) ) {
if ( isset( $block_type->$extra_field ) ) {
$field = $block_type->$extra_field;
if ( in_array( $extra_field, $deprecated_fields, true ) && is_array( $field ) ) {
// Since the schema only allows strings or null (but no arrays), we return the first array item.
$field = isset( $field[0] ) ? $field[0] : null;
ockham marked this conversation as resolved.
Show resolved Hide resolved
}
} elseif ( array_key_exists( 'default', $schema['properties'][ $extra_field ] ) ) {
$field = $schema['properties'][ $extra_field ]['default'];
} else {
Expand Down
126 changes: 126 additions & 0 deletions tests/phpunit/tests/blocks/register.php
Original file line number Diff line number Diff line change
Expand Up @@ -552,6 +552,132 @@ public function test_block_register_block_type_proxy_for_metadata() {
$this->assertSame( 'tests/notice', $result->name );
}

/**
* Tests that an array value for 'editor_script' is correctly set and retrieved.
*
* As 'editor_script' is now a deprecated property, this should also set
* the value for the 'editor_script_handles' property.
*
* @ticket 56707
*
* @covers ::register_block_type
* @covers WP_Block_Type::__set
* @covers WP_Block_Type::__get
*
* @dataProvider data_register_block_type_accepts_editor_script_array
*
* @param array $editor_script The editor script array to register.
* @param array $expected The expected registered editor script.
*/
public function test_register_block_type_accepts_editor_script_array( $editor_script, $expected ) {
$settings = array( 'editor_script' => $editor_script );
register_block_type( 'core/test-static', $settings );

$registry = WP_Block_Type_Registry::get_instance();
$block_type = $registry->get_registered('core/test-static');
$actual_script = $block_type->editor_script;
$actual_script_handles = $block_type->editor_script_handles;

$this->assertSame(
$expected,
$actual_script,
'editor_script was not set to the correct value.'
);

$this->assertSame(
(array) $expected,
$actual_script_handles,
'editor_script_handles was not set to the correct value.'
);
}

/**
* Data provider for test_register_block_type_accepts_editor_script_array().
*
* @return array
*/
public function data_register_block_type_accepts_editor_script_array() {
return array(
'an empty array' => array(
'editor_script' => array(),
'expected' => null,
gziolo marked this conversation as resolved.
Show resolved Hide resolved
),
'a single item array' => array(
'editor_script' => array( 'hello' ),
'expected' => 'hello',
),
'a multi-item array' => array(
'editor_script' => array( 'hello', 'world' ),
'expected' => array( 'hello', 'world' ),
),
);
}

/**
* Tests that an array value for 'editor_script' containing invalid values
* correctly triggers _doing_it_wrong(), filters the value, and sets the
* property to the result.
*
* As 'editor_script' is now a deprecated property, this should also set
* the value for the 'editor_script_handles' property.
*
* @ticket 56707
*
* @covers ::register_block_type
* @covers WP_Block_Type::__set
* @covers WP_Block_Type::__get
*
* @dataProvider data_register_block_type_throws_doing_it_wrong
*
* @expectedIncorrectUsage WP_Block_Type::__set
*
* @param array $editor_script The editor script array to register.
* @param array $expected The expected registered editor script.
*/
public function test_register_block_type_throws_doing_it_wrong( $editor_script, $expected ) {
$settings = array( 'editor_script' => $editor_script );
register_block_type( 'core/test-static', $settings );

$registry = WP_Block_Type_Registry::get_instance();
ockham marked this conversation as resolved.
Show resolved Hide resolved
$block_type = $registry->get_registered('core/test-static');
$actual_script = $block_type->editor_script;
$actual_script_handles = $block_type->editor_script_handles;

$this->assertSame(
$expected,
$actual_script,
'editor_script was not set to the correct value.'
);

$this->assertSame(
(array) $expected,
$actual_script_handles,
'editor_script_handles was not set to the correct value.'
);
}

/**
* Data provider for test_register_block_type_throws_doing_it_wrong().
*
* @return array
*/
public function data_register_block_type_throws_doing_it_wrong() {
return array(
'a non-string array' => array(
'editor_script' => array( null, false, true, -1, 0, 1, -1.0, 0.0, 1.0, INF, NAN, new stdClass() ),
'expected' => null,
),
'a partial string array' => array(
'editor_script' => array( null, false, 'script.js', true, 0, 'actions.js', 1, INF ),
'expected' => array( 'script.js', 'actions.js' ),
),
'a partial string array that results in one item with non-zero index' => array(
'editor_script' => array( null, false, 'script.js' ),
'expected' => 'script.js',
),
);
}

/**
* @ticket 52301
*/
Expand Down
61 changes: 61 additions & 0 deletions tests/phpunit/tests/rest-api/rest-block-type-controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -338,6 +338,67 @@ public function test_get_item_defaults() {
$this->assertNull( $data['style'] );
}

/**
* @ticket 56733
*/
public function test_get_item_deprecated() {
$block_type = 'fake/deprecated';
$settings = array(
'editor_script' => 'hello_world',
'script' => 'gutenberg',
'view_script' => 'foo_bar',
'editor_style' => 'guten_tag',
'style' => 'out_of_style',
);
register_block_type( $block_type, $settings );
wp_set_current_user( self::$admin_id );
$request = new WP_REST_Request( 'GET', '/wp/v2/block-types/' . $block_type );
$response = rest_get_server()->dispatch( $request );
$data = $response->get_data();
$this->assertSameSets( array( 'hello_world' ), $data['editor_script_handles'] );
$this->assertSameSets( array( 'gutenberg' ), $data['script_handles'] );
$this->assertSameSets( array( 'foo_bar' ), $data['view_script_handles'] );
$this->assertSameSets( array( 'guten_tag' ), $data['editor_style_handles'] );
$this->assertSameSets( array( 'out_of_style' ), $data['style_handles'] );
// Deprecated properties.
$this->assertSame( 'hello_world', $data['editor_script'] );
$this->assertSame( 'gutenberg', $data['script'] );
$this->assertSame( 'foo_bar', $data['view_script'] );
$this->assertSame( 'guten_tag', $data['editor_style'] );
$this->assertSame( 'out_of_style', $data['style'] );
}

/**
* @ticket 56733
*/
public function test_get_item_deprecated_with_arrays() {
$block_type = 'fake/deprecated-with-arrays';
$settings = array(
'editor_script' => array( 'hello', 'world' ),
'script' => array( 'gutenberg' ),
'view_script' => array( 'foo', 'bar' ),
'editor_style' => array( 'guten', 'tag' ),
'style' => array( 'out', 'of', 'style' ),
);
register_block_type( $block_type, $settings );
wp_set_current_user( self::$admin_id );
$request = new WP_REST_Request( 'GET', '/wp/v2/block-types/' . $block_type );
$response = rest_get_server()->dispatch( $request );
$data = $response->get_data();
$this->assertSameSets( $settings['editor_script'], $data['editor_script_handles'] );
$this->assertSameSets( $settings['script'], $data['script_handles'] );
$this->assertSameSets( $settings['view_script'], $data['view_script_handles'] );
$this->assertSameSets( $settings['editor_style'], $data['editor_style_handles'] );
$this->assertSameSets( $settings['style'], $data['style_handles'] );
// Deprecated properties.
// Since the schema only allows strings or null (but no arrays), we return the first array item.
$this->assertSame( 'hello', $data['editor_script'] );
$this->assertSame( 'gutenberg', $data['script'] );
$this->assertSame( 'foo', $data['view_script'] );
$this->assertSame( 'guten', $data['editor_style'] );
$this->assertSame( 'out', $data['style'] );
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Test methods with multiple assertions need a message parameter for each assertion, per Core Handbook - Writing PHPUnit Tests - Using Assertions.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Addressed in 9bc56a5 😊

}

public function test_get_variation() {
$block_type = 'fake/variations';
$settings = array(
Expand Down