mirror of
https://github.com/gosticks/wordpress-develop.git
synced 2026-08-11 20:30:23 +00:00
Blocks: Allow arrays for deprecated asset types in block registration.
In `register_block_type`, continue to allow passing arrays as the `editor_script`, `script`, `view_script`, `editor_style`, and `style` arguments. Note that those fields were soft-deprecated in favor of their `_handles` counterparts in [54155], which would allow specifying multiple items. At the same time, the deprecated fields were limited to `string` or `null`. However, this broke existing code that passed an array as one of those arguments. For backwards compatibility, this change thus restores the previous behavior. It is implemented in `WP_Block_Type` as a pair of `__get()` and `__set()` methods that wrap around the corresponding `_handles` members, which are arrays of strings. It also affects the REST API endpoint for block types. The latter’s schema has never allowed for anything other than `string` or `null` for any of those fields. For this reason, it now returns the first element of the array stored in the corresponding `_handles` member in `WP_Block_Type`. Follow-up [54155]. Props nendeb55, costdev, gziolo, spacedmonkey, mukesh27, sergeybiryukov, audrasjb. Fixes #56707. git-svn-id: https://develop.svn.wordpress.org/trunk@54670 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
@@ -552,6 +552,134 @@ class Tests_Blocks_Register extends WP_UnitTestCase {
|
||||
$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' );
|
||||
$this->assertObjectHasAttribute( 'editor_script_handles', $block_type );
|
||||
$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,
|
||||
),
|
||||
'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();
|
||||
$block_type = $registry->get_registered( 'core/test-static' );
|
||||
$this->assertObjectHasAttribute( 'editor_script_handles', $block_type );
|
||||
$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
|
||||
*/
|
||||
|
||||
@@ -338,6 +338,148 @@ class REST_Block_Type_Controller_Test extends WP_Test_REST_Controller_Testcase {
|
||||
$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'],
|
||||
"Endpoint doesn't return correct array for editor_script_handles."
|
||||
);
|
||||
$this->assertSameSets(
|
||||
array( 'gutenberg' ),
|
||||
$data['script_handles'],
|
||||
"Endpoint doesn't return correct array for script_handles."
|
||||
);
|
||||
$this->assertSameSets(
|
||||
array( 'foo_bar' ),
|
||||
$data['view_script_handles'],
|
||||
"Endpoint doesn't return correct array for view_script_handles."
|
||||
);
|
||||
$this->assertSameSets(
|
||||
array( 'guten_tag' ),
|
||||
$data['editor_style_handles'],
|
||||
"Endpoint doesn't return correct array for editor_style_handles."
|
||||
);
|
||||
$this->assertSameSets(
|
||||
array( 'out_of_style' ),
|
||||
$data['style_handles'],
|
||||
"Endpoint doesn't return correct array for style_handles."
|
||||
);
|
||||
// Deprecated properties.
|
||||
$this->assertSame(
|
||||
'hello_world',
|
||||
$data['editor_script'],
|
||||
"Endpoint doesn't return correct string for editor_script."
|
||||
);
|
||||
$this->assertSame(
|
||||
'gutenberg',
|
||||
$data['script'],
|
||||
"Endpoint doesn't return correct string for script."
|
||||
);
|
||||
$this->assertSame(
|
||||
'foo_bar',
|
||||
$data['view_script'],
|
||||
"Endpoint doesn't return correct string for view_script."
|
||||
);
|
||||
$this->assertSame(
|
||||
'guten_tag',
|
||||
$data['editor_style'],
|
||||
"Endpoint doesn't return correct string for editor_style."
|
||||
);
|
||||
$this->assertSame(
|
||||
'out_of_style',
|
||||
$data['style'],
|
||||
"Endpoint doesn't return correct string for 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'],
|
||||
"Endpoint doesn't return correct array for editor_script_handles."
|
||||
);
|
||||
$this->assertSameSets(
|
||||
$settings['script'],
|
||||
$data['script_handles'],
|
||||
"Endpoint doesn't return correct array for script_handles."
|
||||
);
|
||||
$this->assertSameSets(
|
||||
$settings['view_script'],
|
||||
$data['view_script_handles'],
|
||||
"Endpoint doesn't return correct array for view_script_handles."
|
||||
);
|
||||
$this->assertSameSets(
|
||||
$settings['editor_style'],
|
||||
$data['editor_style_handles'],
|
||||
"Endpoint doesn't return correct array for editor_style_handles."
|
||||
);
|
||||
$this->assertSameSets(
|
||||
$settings['style'],
|
||||
$data['style_handles'],
|
||||
"Endpoint doesn't return correct array for style_handles."
|
||||
);
|
||||
// Deprecated properties.
|
||||
// Since the schema only allows strings or null (but no arrays), we return the first array item.
|
||||
// Deprecated properties.
|
||||
$this->assertSame(
|
||||
'hello',
|
||||
$data['editor_script'],
|
||||
"Endpoint doesn't return first array element for editor_script."
|
||||
);
|
||||
$this->assertSame(
|
||||
'gutenberg',
|
||||
$data['script'],
|
||||
"Endpoint doesn't return first array element for script."
|
||||
);
|
||||
$this->assertSame(
|
||||
'foo',
|
||||
$data['view_script'],
|
||||
"Endpoint doesn't return first array element for view_script."
|
||||
);
|
||||
$this->assertSame(
|
||||
'guten',
|
||||
$data['editor_style'],
|
||||
"Endpoint doesn't return first array element for editor_style."
|
||||
);
|
||||
$this->assertSame(
|
||||
'out',
|
||||
$data['style'],
|
||||
"Endpoint doesn't return first array element for style."
|
||||
);
|
||||
}
|
||||
|
||||
public function test_get_variation() {
|
||||
$block_type = 'fake/variations';
|
||||
$settings = array(
|
||||
|
||||
Reference in New Issue
Block a user