From 4fbea48a15b8664596f0a5d48fe9b8836340adcd Mon Sep 17 00:00:00 2001 From: Bernie Reiter Date: Tue, 18 Oct 2022 18:40:29 +0200 Subject: [PATCH 01/27] Add unit test for register_block_type with editor_script array --- tests/phpunit/tests/blocks/register.php | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/tests/phpunit/tests/blocks/register.php b/tests/phpunit/tests/blocks/register.php index 554f09c2d2b14..1bb579e9928c3 100644 --- a/tests/phpunit/tests/blocks/register.php +++ b/tests/phpunit/tests/blocks/register.php @@ -552,6 +552,20 @@ public function test_block_register_block_type_proxy_for_metadata() { $this->assertSame( 'tests/notice', $result->name ); } + /** + * @ticket 56707 + */ + public function test_register_block_type_accepts_editor_script_array() { + $settings = array( 'editor_script' => array( 'hello', 'world' ) ); + register_block_type( 'core/test-static', $settings ); + + $registry = WP_Block_Type_Registry::get_instance(); + $registered = $registry->get_all_registered(); + $actual = json_encode( $registered['core/test-static']->editor_script ); + + $this->assertSame( $settings['editor_script'], $actual ); + } + /** * @ticket 52301 */ From 8a8bfc0c24bf0bdc0267348dc135dfa1da36b0dd Mon Sep 17 00:00:00 2001 From: Bernie Reiter Date: Tue, 18 Oct 2022 18:40:49 +0200 Subject: [PATCH 02/27] Continue to support editor_script array for back-compat --- src/wp-includes/class-wp-block-type.php | 24 ++++++++++++++++++++++-- 1 file changed, 22 insertions(+), 2 deletions(-) diff --git a/src/wp-includes/class-wp-block-type.php b/src/wp-includes/class-wp-block-type.php index 19cd1c0791991..10aeb5ebd84cb 100644 --- a/src/wp-includes/class-wp-block-type.php +++ b/src/wp-includes/class-wp-block-type.php @@ -343,11 +343,31 @@ public function __set( $name, $value ) { return; } - if ( ! is_string( $value ) ) { + $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.' ), + '$value' + ), + '6.1.0', + ); + } + + $this->{$new_name} = $filtered; return; } - $new_name = $name . '_handles'; + if ( ! is_string( $value ) ) { + return; + } + $this->{$new_name}[0] = $value; } From efbc779cd12fd4b17e761ffe4dfa02b93088b19c Mon Sep 17 00:00:00 2001 From: Bernie Reiter Date: Tue, 18 Oct 2022 18:59:27 +0200 Subject: [PATCH 03/27] Fix indentation --- src/wp-includes/class-wp-block-type.php | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/src/wp-includes/class-wp-block-type.php b/src/wp-includes/class-wp-block-type.php index 10aeb5ebd84cb..7051f9200a4b9 100644 --- a/src/wp-includes/class-wp-block-type.php +++ b/src/wp-includes/class-wp-block-type.php @@ -350,13 +350,13 @@ public function __set( $name, $value ) { 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.' ), - '$value' - ), - '6.1.0', + __METHOD__, + sprintf( + /* translators: %s: The '$value' argument. */ + __( 'The %s argument must be a string or a string array.' ), + '$value' + ), + '6.1.0', ); } From e1d9ef12fdaa88a2033fc34a0dde6aed14ce961f Mon Sep 17 00:00:00 2001 From: Bernie Reiter Date: Tue, 18 Oct 2022 19:01:51 +0200 Subject: [PATCH 04/27] Fix indentation, again --- src/wp-includes/class-wp-block-type.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/wp-includes/class-wp-block-type.php b/src/wp-includes/class-wp-block-type.php index 7051f9200a4b9..60be86a0b048c 100644 --- a/src/wp-includes/class-wp-block-type.php +++ b/src/wp-includes/class-wp-block-type.php @@ -352,9 +352,9 @@ public function __set( $name, $value ) { _doing_it_wrong( __METHOD__, sprintf( - /* translators: %s: The '$value' argument. */ - __( 'The %s argument must be a string or a string array.' ), - '$value' + /* translators: %s: The '$value' argument. */ + __( 'The %s argument must be a string or a string array.' ), + '$value' ), '6.1.0', ); From 645683c64f7059a1f0d5381c8770ca18e26d7a5c Mon Sep 17 00:00:00 2001 From: Bernie Reiter Date: Tue, 18 Oct 2022 19:02:22 +0200 Subject: [PATCH 05/27] Remove trailing comma --- src/wp-includes/class-wp-block-type.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/wp-includes/class-wp-block-type.php b/src/wp-includes/class-wp-block-type.php index 60be86a0b048c..452c8e2923889 100644 --- a/src/wp-includes/class-wp-block-type.php +++ b/src/wp-includes/class-wp-block-type.php @@ -356,7 +356,7 @@ public function __set( $name, $value ) { __( 'The %s argument must be a string or a string array.' ), '$value' ), - '6.1.0', + '6.1.0' ); } From 8c43473be065bf206a0e0f1261acd3e75db21f91 Mon Sep 17 00:00:00 2001 From: Bernie Reiter Date: Tue, 18 Oct 2022 21:39:34 +0200 Subject: [PATCH 06/27] Fix unit test --- tests/phpunit/tests/blocks/register.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/phpunit/tests/blocks/register.php b/tests/phpunit/tests/blocks/register.php index 1bb579e9928c3..2e17ad734368b 100644 --- a/tests/phpunit/tests/blocks/register.php +++ b/tests/phpunit/tests/blocks/register.php @@ -561,7 +561,7 @@ public function test_register_block_type_accepts_editor_script_array() { $registry = WP_Block_Type_Registry::get_instance(); $registered = $registry->get_all_registered(); - $actual = json_encode( $registered['core/test-static']->editor_script ); + $actual = $registered['core/test-static']->editor_script; $this->assertSame( $settings['editor_script'], $actual ); } From c4d897c4cfdc398a267c9b2bbfd52225da02bbc3 Mon Sep 17 00:00:00 2001 From: Bernie Reiter Date: Tue, 18 Oct 2022 21:39:42 +0200 Subject: [PATCH 07/27] Fix getter --- src/wp-includes/class-wp-block-type.php | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/wp-includes/class-wp-block-type.php b/src/wp-includes/class-wp-block-type.php index 452c8e2923889..0b09d3aa98943 100644 --- a/src/wp-includes/class-wp-block-type.php +++ b/src/wp-includes/class-wp-block-type.php @@ -304,6 +304,10 @@ public function __get( $name ) { } $new_name = $name . '_handles'; + + if ( count( $this->{$new_name} ) > 1 ) { + return $this->{$new_name}; + } return isset( $this->{$new_name}[0] ) ? $this->{$new_name}[0] : null; } From 65eedef2daea8f92aef6dc846e4462c99f494997 Mon Sep 17 00:00:00 2001 From: Bernie Reiter Date: Wed, 19 Oct 2022 11:24:58 +0200 Subject: [PATCH 08/27] Extend test coverage props @costdev --- tests/phpunit/tests/blocks/register.php | 124 ++++++++++++++++++++++-- 1 file changed, 118 insertions(+), 6 deletions(-) diff --git a/tests/phpunit/tests/blocks/register.php b/tests/phpunit/tests/blocks/register.php index 2e17ad734368b..781501f14c7f3 100644 --- a/tests/phpunit/tests/blocks/register.php +++ b/tests/phpunit/tests/blocks/register.php @@ -553,17 +553,129 @@ public function test_block_register_block_type_proxy_for_metadata() { } /** + * 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() { - $settings = array( 'editor_script' => array( 'hello', 'world' ) ); + 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(); - $registered = $registry->get_all_registered(); - $actual = $registered['core/test-static']->editor_script; + $registry = WP_Block_Type_Registry::get_instance(); + $registered = $registry->get_all_registered(); + $actual_script = $registered['core/test-static']->editor_script; + $actual_script_handles = $registered['core/test-static']->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(); + $registered = $registry->get_all_registered(); + $actual_script = $registered['core/test-static']->editor_script; + $actual_script_handles = $registered['core/test-static']->editor_script_handles; + + $this->assertSame( + $expected, + $actual_script, + 'editor_script was not set to the correct value.' + ); - $this->assertSame( $settings['editor_script'], $actual ); + $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', + ), + ); } /** From e3e4b5083ac0764b0e4afe8c04aedc98c28b92bf Mon Sep 17 00:00:00 2001 From: Bernie Reiter Date: Wed, 19 Oct 2022 11:31:59 +0200 Subject: [PATCH 09/27] Make array contiguous props @costdev --- src/wp-includes/class-wp-block-type.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/wp-includes/class-wp-block-type.php b/src/wp-includes/class-wp-block-type.php index 0b09d3aa98943..4de865d8a8010 100644 --- a/src/wp-includes/class-wp-block-type.php +++ b/src/wp-includes/class-wp-block-type.php @@ -364,7 +364,7 @@ public function __set( $name, $value ) { ); } - $this->{$new_name} = $filtered; + $this->{$new_name} = array_values( $filtered ); return; } From 37af4ffbeb27555e46196bf49b9ef877a68d5a53 Mon Sep 17 00:00:00 2001 From: Bernie Reiter Date: Wed, 19 Oct 2022 11:39:16 +0200 Subject: [PATCH 10/27] Have setter override entire array rather than just first element --- src/wp-includes/class-wp-block-type.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/wp-includes/class-wp-block-type.php b/src/wp-includes/class-wp-block-type.php index 4de865d8a8010..08be5a4d09cde 100644 --- a/src/wp-includes/class-wp-block-type.php +++ b/src/wp-includes/class-wp-block-type.php @@ -372,7 +372,7 @@ public function __set( $name, $value ) { return; } - $this->{$new_name}[0] = $value; + $this->{$new_name} = array( $value ); } /** From 37b948697a6c79c4602409d22bd97064f0803824 Mon Sep 17 00:00:00 2001 From: Bernie Reiter Date: Wed, 19 Oct 2022 11:53:14 +0200 Subject: [PATCH 11/27] Update PHPDoc --- src/wp-includes/class-wp-block-type.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/wp-includes/class-wp-block-type.php b/src/wp-includes/class-wp-block-type.php index 08be5a4d09cde..6bd79c43d4099 100644 --- a/src/wp-includes/class-wp-block-type.php +++ b/src/wp-includes/class-wp-block-type.php @@ -295,8 +295,8 @@ 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 ) ) { From 3731af8d9a4cc4e60441f0162a6ff02ceb5c1001 Mon Sep 17 00:00:00 2001 From: Bernie Reiter Date: Wed, 19 Oct 2022 16:53:48 +0200 Subject: [PATCH 12/27] Add REST API endpoint test coverage --- .../rest-api/rest-block-type-controller.php | 30 +++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/tests/phpunit/tests/rest-api/rest-block-type-controller.php b/tests/phpunit/tests/rest-api/rest-block-type-controller.php index ed3ee62fe8236..bd17db2127d7d 100644 --- a/tests/phpunit/tests/rest-api/rest-block-type-controller.php +++ b/tests/phpunit/tests/rest-api/rest-block-type-controller.php @@ -338,6 +338,36 @@ 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' => array( 'editor', 'script' ), + 'script' => array( 'guten', 'berg' ), + 'view_script' => array( 'view', 'script' ), + 'editor_style' => array( 'editor', 'style' ), + '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. + $this->assertSameSets( $settings['editor_script'], $data['editor_script'] ); + $this->assertSameSets( $settings['script'], $data['script'] ); + $this->assertSameSets( $settings['view_script'], $data['view_script'] ); + $this->assertSameSets( $settings['editor_style'], $data['editor_style'] ); + $this->assertSameSets( $settings['style'], $data['style'] ); + } + public function test_get_variation() { $block_type = 'fake/variations'; $settings = array( From 1b6afb88d4d0e1254ac0c17c18e75b02fb90306e Mon Sep 17 00:00:00 2001 From: Bernie Reiter Date: Wed, 19 Oct 2022 21:08:54 +0200 Subject: [PATCH 13/27] Add logic and test coverage to return first array item --- .../class-wp-rest-block-types-controller.php | 4 ++ .../rest-api/rest-block-type-controller.php | 49 +++++++++++++++---- 2 files changed, 44 insertions(+), 9 deletions(-) diff --git a/src/wp-includes/rest-api/endpoints/class-wp-rest-block-types-controller.php b/src/wp-includes/rest-api/endpoints/class-wp-rest-block-types-controller.php index af29924c76edb..6c65772124e22 100644 --- a/src/wp-includes/rest-api/endpoints/class-wp-rest-block-types-controller.php +++ b/src/wp-includes/rest-api/endpoints/class-wp-rest-block-types-controller.php @@ -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 = $field[0]; + } } elseif ( array_key_exists( 'default', $schema['properties'][ $extra_field ] ) ) { $field = $schema['properties'][ $extra_field ]['default']; } else { diff --git a/tests/phpunit/tests/rest-api/rest-block-type-controller.php b/tests/phpunit/tests/rest-api/rest-block-type-controller.php index bd17db2127d7d..a941ba026d4c1 100644 --- a/tests/phpunit/tests/rest-api/rest-block-type-controller.php +++ b/tests/phpunit/tests/rest-api/rest-block-type-controller.php @@ -344,10 +344,40 @@ public function test_get_item_defaults() { public function test_get_item_deprecated() { $block_type = 'fake/deprecated'; $settings = array( - 'editor_script' => array( 'editor', 'script' ), - 'script' => array( 'guten', 'berg' ), - 'view_script' => array( 'view', 'script' ), - 'editor_style' => array( 'editor', 'style' ), + '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 ); @@ -361,11 +391,12 @@ public function test_get_item_deprecated() { $this->assertSameSets( $settings['editor_style'], $data['editor_style_handles'] ); $this->assertSameSets( $settings['style'], $data['style_handles'] ); // Deprecated properties. - $this->assertSameSets( $settings['editor_script'], $data['editor_script'] ); - $this->assertSameSets( $settings['script'], $data['script'] ); - $this->assertSameSets( $settings['view_script'], $data['view_script'] ); - $this->assertSameSets( $settings['editor_style'], $data['editor_style'] ); - $this->assertSameSets( $settings['style'], $data['style'] ); + // 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'] ); } public function test_get_variation() { From 7c1c7f7e492b18809cb7ce79dbf0fdd5ceea5b46 Mon Sep 17 00:00:00 2001 From: Bernie Reiter Date: Wed, 19 Oct 2022 21:16:40 +0200 Subject: [PATCH 14/27] Add isset check --- .../rest-api/endpoints/class-wp-rest-block-types-controller.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/wp-includes/rest-api/endpoints/class-wp-rest-block-types-controller.php b/src/wp-includes/rest-api/endpoints/class-wp-rest-block-types-controller.php index 6c65772124e22..bd11f4ace97f8 100644 --- a/src/wp-includes/rest-api/endpoints/class-wp-rest-block-types-controller.php +++ b/src/wp-includes/rest-api/endpoints/class-wp-rest-block-types-controller.php @@ -297,7 +297,7 @@ public function prepare_item_for_response( $item, $request ) { $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 = $field[0]; + $field = isset( $field[0] ) ? $field[0] : null; } } elseif ( array_key_exists( 'default', $schema['properties'][ $extra_field ] ) ) { $field = $schema['properties'][ $extra_field ]['default']; From faa82385a432653de29649cc3bf4e3028eafecbd Mon Sep 17 00:00:00 2001 From: Bernie Reiter Date: Thu, 20 Oct 2022 12:30:14 +0200 Subject: [PATCH 15/27] Use get_registered() Co-authored-by: Jonny Harris --- tests/phpunit/tests/blocks/register.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/tests/phpunit/tests/blocks/register.php b/tests/phpunit/tests/blocks/register.php index 781501f14c7f3..a8a95105a2645 100644 --- a/tests/phpunit/tests/blocks/register.php +++ b/tests/phpunit/tests/blocks/register.php @@ -574,9 +574,9 @@ public function test_register_block_type_accepts_editor_script_array( $editor_sc register_block_type( 'core/test-static', $settings ); $registry = WP_Block_Type_Registry::get_instance(); - $registered = $registry->get_all_registered(); - $actual_script = $registered['core/test-static']->editor_script; - $actual_script_handles = $registered['core/test-static']->editor_script_handles; + $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, From dfbed22f873fb3216e49739ef2c514893eb5ffc4 Mon Sep 17 00:00:00 2001 From: Bernie Reiter Date: Thu, 20 Oct 2022 12:32:07 +0200 Subject: [PATCH 16/27] Whitespace --- tests/phpunit/tests/blocks/register.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/phpunit/tests/blocks/register.php b/tests/phpunit/tests/blocks/register.php index a8a95105a2645..7bb4e2a190ca6 100644 --- a/tests/phpunit/tests/blocks/register.php +++ b/tests/phpunit/tests/blocks/register.php @@ -574,7 +574,7 @@ public function test_register_block_type_accepts_editor_script_array( $editor_sc register_block_type( 'core/test-static', $settings ); $registry = WP_Block_Type_Registry::get_instance(); - $block_type = $registry->get_registered('core/test-static'); + $block_type = $registry->get_registered('core/test-static'); $actual_script = $block_type->editor_script; $actual_script_handles = $block_type->editor_script_handles; From 034a7ff79577d8963cfa6b169a94f0c2bd0bf643 Mon Sep 17 00:00:00 2001 From: Bernie Reiter Date: Thu, 20 Oct 2022 12:33:01 +0200 Subject: [PATCH 17/27] Use get_registered in other test --- tests/phpunit/tests/blocks/register.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/tests/phpunit/tests/blocks/register.php b/tests/phpunit/tests/blocks/register.php index 7bb4e2a190ca6..4ef516dc65793 100644 --- a/tests/phpunit/tests/blocks/register.php +++ b/tests/phpunit/tests/blocks/register.php @@ -639,9 +639,9 @@ public function test_register_block_type_throws_doing_it_wrong( $editor_script, register_block_type( 'core/test-static', $settings ); $registry = WP_Block_Type_Registry::get_instance(); - $registered = $registry->get_all_registered(); - $actual_script = $registered['core/test-static']->editor_script; - $actual_script_handles = $registered['core/test-static']->editor_script_handles; + $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, From 422744fe22d13dcb29f13a839d25d4b2ca50acf2 Mon Sep 17 00:00:00 2001 From: Bernie Reiter Date: Thu, 20 Oct 2022 12:36:34 +0200 Subject: [PATCH 18/27] Indentation --- src/wp-includes/class-wp-block-type.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/wp-includes/class-wp-block-type.php b/src/wp-includes/class-wp-block-type.php index 6bd79c43d4099..19211a3c4b226 100644 --- a/src/wp-includes/class-wp-block-type.php +++ b/src/wp-includes/class-wp-block-type.php @@ -369,7 +369,7 @@ public function __set( $name, $value ) { } if ( ! is_string( $value ) ) { - return; + return; } $this->{$new_name} = array( $value ); From d8869a107f01d6acbdccffc9ad6bb8774a02c202 Mon Sep 17 00:00:00 2001 From: Bernie Reiter Date: Thu, 20 Oct 2022 12:47:52 +0200 Subject: [PATCH 19/27] Missing whitespace in parens --- tests/phpunit/tests/blocks/register.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/phpunit/tests/blocks/register.php b/tests/phpunit/tests/blocks/register.php index 4ef516dc65793..14122d580ca3a 100644 --- a/tests/phpunit/tests/blocks/register.php +++ b/tests/phpunit/tests/blocks/register.php @@ -574,7 +574,7 @@ public function test_register_block_type_accepts_editor_script_array( $editor_sc register_block_type( 'core/test-static', $settings ); $registry = WP_Block_Type_Registry::get_instance(); - $block_type = $registry->get_registered('core/test-static'); + $block_type = $registry->get_registered( 'core/test-static' ); $actual_script = $block_type->editor_script; $actual_script_handles = $block_type->editor_script_handles; @@ -639,7 +639,7 @@ public function test_register_block_type_throws_doing_it_wrong( $editor_script, register_block_type( 'core/test-static', $settings ); $registry = WP_Block_Type_Registry::get_instance(); - $block_type = $registry->get_registered('core/test-static'); + $block_type = $registry->get_registered( 'core/test-static' ); $actual_script = $block_type->editor_script; $actual_script_handles = $block_type->editor_script_handles; From 45775e0ecb3291d74fb8e846dd471c982e18060f Mon Sep 17 00:00:00 2001 From: Bernie Reiter Date: Thu, 20 Oct 2022 12:48:11 +0200 Subject: [PATCH 20/27] Add property_exists and is_array checks --- src/wp-includes/class-wp-block-type.php | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/wp-includes/class-wp-block-type.php b/src/wp-includes/class-wp-block-type.php index 19211a3c4b226..505c504037439 100644 --- a/src/wp-includes/class-wp-block-type.php +++ b/src/wp-includes/class-wp-block-type.php @@ -305,6 +305,10 @@ public function __get( $name ) { $new_name = $name . '_handles'; + if ( ! property_exists( $this, $new_name ) || ! is_array( $this->{$new_name} ) ) { + return null; + } + if ( count( $this->{$new_name} ) > 1 ) { return $this->{$new_name}; } From 9bc56a53bd3b4cbc3bfc4e78bf5637eec4b4ed14 Mon Sep 17 00:00:00 2001 From: Bernie Reiter Date: Thu, 20 Oct 2022 15:15:12 +0200 Subject: [PATCH 21/27] Add messages to assertions --- .../rest-api/rest-block-type-controller.php | 121 +++++++++++++++--- 1 file changed, 101 insertions(+), 20 deletions(-) diff --git a/tests/phpunit/tests/rest-api/rest-block-type-controller.php b/tests/phpunit/tests/rest-api/rest-block-type-controller.php index a941ba026d4c1..02312ba6ce3ea 100644 --- a/tests/phpunit/tests/rest-api/rest-block-type-controller.php +++ b/tests/phpunit/tests/rest-api/rest-block-type-controller.php @@ -355,17 +355,57 @@ public function test_get_item_deprecated() { $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'] ); + $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'] ); - $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'] ); + $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." + ); } /** @@ -385,18 +425,59 @@ public function test_get_item_deprecated_with_arrays() { $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'] ); + $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. - $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'] ); + // 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() { From afb21eab7243de2927c96efc13f40507e4c83740 Mon Sep 17 00:00:00 2001 From: Bernie Reiter Date: Thu, 20 Oct 2022 19:09:01 +0200 Subject: [PATCH 22/27] Assert that we have the editor_script and editor_script_handles properties Co-authored-by: Jonny Harris --- tests/phpunit/tests/blocks/register.php | 2 ++ 1 file changed, 2 insertions(+) diff --git a/tests/phpunit/tests/blocks/register.php b/tests/phpunit/tests/blocks/register.php index 14122d580ca3a..b52f685ada567 100644 --- a/tests/phpunit/tests/blocks/register.php +++ b/tests/phpunit/tests/blocks/register.php @@ -640,6 +640,8 @@ public function test_register_block_type_throws_doing_it_wrong( $editor_script, $registry = WP_Block_Type_Registry::get_instance(); $block_type = $registry->get_registered( 'core/test-static' ); + $this->assertObjectHasAttribute( 'editor_script', $block_type ); + $this->assertObjectHasAttribute( 'editor_script_handles', $block_type ); $actual_script = $block_type->editor_script; $actual_script_handles = $block_type->editor_script_handles; From 1d5bce519616bc775fd50742982f2cb26082ebac Mon Sep 17 00:00:00 2001 From: Bernie Reiter Date: Thu, 20 Oct 2022 19:09:28 +0200 Subject: [PATCH 23/27] Assert that we have the editor_script and editor_script_handles properties, pt. 2 Co-authored-by: Jonny Harris --- tests/phpunit/tests/blocks/register.php | 2 ++ 1 file changed, 2 insertions(+) diff --git a/tests/phpunit/tests/blocks/register.php b/tests/phpunit/tests/blocks/register.php index b52f685ada567..d5e9e6d17d9ed 100644 --- a/tests/phpunit/tests/blocks/register.php +++ b/tests/phpunit/tests/blocks/register.php @@ -575,6 +575,8 @@ public function test_register_block_type_accepts_editor_script_array( $editor_sc $registry = WP_Block_Type_Registry::get_instance(); $block_type = $registry->get_registered( 'core/test-static' ); + $this->assertObjectHasAttribute( 'editor_script', $block_type ); + $this->assertObjectHasAttribute( 'editor_script_handles', $block_type ); $actual_script = $block_type->editor_script; $actual_script_handles = $block_type->editor_script_handles; From 203f99761ed740b9b659b6b43a2583d14e121fee Mon Sep 17 00:00:00 2001 From: Bernie Reiter Date: Thu, 20 Oct 2022 19:10:51 +0200 Subject: [PATCH 24/27] Use ! empty() and array_shift() Co-authored-by: Jonny Harris --- .../rest-api/endpoints/class-wp-rest-block-types-controller.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/wp-includes/rest-api/endpoints/class-wp-rest-block-types-controller.php b/src/wp-includes/rest-api/endpoints/class-wp-rest-block-types-controller.php index bd11f4ace97f8..22672942ac435 100644 --- a/src/wp-includes/rest-api/endpoints/class-wp-rest-block-types-controller.php +++ b/src/wp-includes/rest-api/endpoints/class-wp-rest-block-types-controller.php @@ -297,7 +297,7 @@ public function prepare_item_for_response( $item, $request ) { $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; + $field = !empty( $field ) ? array_shift( $field ) : ''; } } elseif ( array_key_exists( 'default', $schema['properties'][ $extra_field ] ) ) { $field = $schema['properties'][ $extra_field ]['default']; From a93657f429ed350f2310704ab1a51f77414ec6b5 Mon Sep 17 00:00:00 2001 From: Bernie Reiter Date: Thu, 20 Oct 2022 19:11:39 +0200 Subject: [PATCH 25/27] Whitespace --- .../rest-api/endpoints/class-wp-rest-block-types-controller.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/wp-includes/rest-api/endpoints/class-wp-rest-block-types-controller.php b/src/wp-includes/rest-api/endpoints/class-wp-rest-block-types-controller.php index 22672942ac435..e1f34baef6df0 100644 --- a/src/wp-includes/rest-api/endpoints/class-wp-rest-block-types-controller.php +++ b/src/wp-includes/rest-api/endpoints/class-wp-rest-block-types-controller.php @@ -297,7 +297,7 @@ public function prepare_item_for_response( $item, $request ) { $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 = !empty( $field ) ? array_shift( $field ) : ''; + $field = ! empty( $field ) ? array_shift( $field ) : ''; } } elseif ( array_key_exists( 'default', $schema['properties'][ $extra_field ] ) ) { $field = $schema['properties'][ $extra_field ]['default']; From fe7999a1791b3bcf0c38ea6968ed36890fe4b148 Mon Sep 17 00:00:00 2001 From: Bernie Reiter Date: Thu, 20 Oct 2022 23:26:01 +0200 Subject: [PATCH 26/27] More whitespace --- tests/phpunit/tests/blocks/register.php | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/tests/phpunit/tests/blocks/register.php b/tests/phpunit/tests/blocks/register.php index d5e9e6d17d9ed..5eeac5c476d29 100644 --- a/tests/phpunit/tests/blocks/register.php +++ b/tests/phpunit/tests/blocks/register.php @@ -573,8 +573,8 @@ public function test_register_block_type_accepts_editor_script_array( $editor_sc $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' ); + $registry = WP_Block_Type_Registry::get_instance(); + $block_type = $registry->get_registered( 'core/test-static' ); $this->assertObjectHasAttribute( 'editor_script', $block_type ); $this->assertObjectHasAttribute( 'editor_script_handles', $block_type ); $actual_script = $block_type->editor_script; @@ -640,8 +640,8 @@ public function test_register_block_type_throws_doing_it_wrong( $editor_script, $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' ); + $registry = WP_Block_Type_Registry::get_instance(); + $block_type = $registry->get_registered( 'core/test-static' ); $this->assertObjectHasAttribute( 'editor_script', $block_type ); $this->assertObjectHasAttribute( 'editor_script_handles', $block_type ); $actual_script = $block_type->editor_script; From d2788d88d07056c47530070f0f144caec37b22a5 Mon Sep 17 00:00:00 2001 From: Bernie Reiter Date: Thu, 20 Oct 2022 23:31:43 +0200 Subject: [PATCH 27/27] Remove property assertions for virtual properties --- tests/phpunit/tests/blocks/register.php | 2 -- 1 file changed, 2 deletions(-) diff --git a/tests/phpunit/tests/blocks/register.php b/tests/phpunit/tests/blocks/register.php index 5eeac5c476d29..ca906086a36aa 100644 --- a/tests/phpunit/tests/blocks/register.php +++ b/tests/phpunit/tests/blocks/register.php @@ -575,7 +575,6 @@ public function test_register_block_type_accepts_editor_script_array( $editor_sc $registry = WP_Block_Type_Registry::get_instance(); $block_type = $registry->get_registered( 'core/test-static' ); - $this->assertObjectHasAttribute( 'editor_script', $block_type ); $this->assertObjectHasAttribute( 'editor_script_handles', $block_type ); $actual_script = $block_type->editor_script; $actual_script_handles = $block_type->editor_script_handles; @@ -642,7 +641,6 @@ public function test_register_block_type_throws_doing_it_wrong( $editor_script, $registry = WP_Block_Type_Registry::get_instance(); $block_type = $registry->get_registered( 'core/test-static' ); - $this->assertObjectHasAttribute( 'editor_script', $block_type ); $this->assertObjectHasAttribute( 'editor_script_handles', $block_type ); $actual_script = $block_type->editor_script; $actual_script_handles = $block_type->editor_script_handles;