From da28362ba1a99a86211dfc75c376b16f8218b967 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Greg=20Zi=C3=83=C2=B3=C3=85=E2=80=9Akowski?= Date: Thu, 14 Sep 2023 14:54:41 +0000 Subject: [PATCH] Tests: Improve the assertions for REST API endpoint for block types Follow-up to [56587], [55673]. While working on #59346, it was noted that selectors fiels is not always included in the assertions. While looking at it is was difficult to spot the issue because the random order of how REST API fields where listed. Reorderd the REST API fields in the test cases to follow the list from the documentation: https://github.com/WordPress/gutenberg/blob/trunk/docs/reference-guides/block-api/block-metadata.md. This way it's going to be easier to maintain the list moving forward. Props ockham. See #59346, #59313, #57585. git-svn-id: https://develop.svn.wordpress.org/trunk@56588 602fd350-edb4-49c9-b593-d223f7449a82 --- .../rest-api/rest-block-type-controller.php | 150 +++++++++--------- 1 file changed, 76 insertions(+), 74 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 51e702730f..b1e1adff14 100644 --- a/tests/phpunit/tests/rest-api/rest-block-type-controller.php +++ b/tests/phpunit/tests/rest-api/rest-block-type-controller.php @@ -201,28 +201,28 @@ class REST_Block_Type_Controller_Test extends WP_Test_REST_Controller_Testcase { $block_type = 'fake/invalid'; $settings = array( 'title' => true, - 'description' => true, + 'category' => true, + 'parent' => 'invalid_parent', + 'ancestor' => 'invalid_ancestor', 'icon' => true, + 'description' => true, + 'keywords' => 'invalid_keywords', + 'textdomain' => true, 'attributes' => 'invalid_attributes', 'provides_context' => 'invalid_provides_context', 'uses_context' => 'invalid_uses_context', + 'selectors' => 'invalid_selectors', + 'supports' => 'invalid_supports', + 'styles' => array(), + 'example' => 'invalid_example', + 'variations' => 'invalid_variations', 'block_hooks' => 'invalid_block_hooks', - 'category' => true, + 'render_callback' => 'invalid_callback', 'editor_script' => true, 'script' => true, 'view_script' => true, 'editor_style' => true, 'style' => true, - 'keywords' => 'invalid_keywords', - 'example' => 'invalid_example', - 'parent' => 'invalid_parent', - 'ancestor' => 'invalid_ancestor', - 'selectors' => 'invalid_selectors', - 'supports' => 'invalid_supports', - 'styles' => array(), - 'render_callback' => 'invalid_callback', - 'textdomain' => true, - 'variations' => 'invalid_variations', ); register_block_type( $block_type, $settings ); wp_set_current_user( self::$admin_id ); @@ -231,14 +231,13 @@ class REST_Block_Type_Controller_Test extends WP_Test_REST_Controller_Testcase { $data = $response->get_data(); $this->assertSame( $block_type, $data['name'] ); $this->assertSame( '1', $data['title'] ); - $this->assertSame( '1', $data['description'] ); + $this->assertNull( $data['category'] ); + $this->assertSameSets( array( 'invalid_parent' ), $data['parent'] ); + $this->assertSameSets( array( 'invalid_ancestor' ), $data['ancestor'] ); $this->assertNull( $data['icon'] ); - $this->assertSameSets( array(), $data['editor_script_handles'] ); - $this->assertSameSets( array(), $data['script_handles'] ); - $this->assertSameSets( array(), $data['view_script_handles'] ); - $this->assertSameSets( array(), $data['editor_style_handles'] ); - $this->assertSameSets( array(), $data['style_handles'] ); - $this->assertSameSets( array(), $data['provides_context'] ); + $this->assertSame( '1', $data['description'] ); + $this->assertSameSets( array( 'invalid_keywords' ), $data['keywords'] ); + $this->assertNull( $data['textdomain'] ); $this->assertSameSetsWithIndex( array( 'lock' => array( 'type' => 'object' ), @@ -246,18 +245,19 @@ class REST_Block_Type_Controller_Test extends WP_Test_REST_Controller_Testcase { $data['attributes'] ); $this->assertSameSets( array( 'invalid_uses_context' ), $data['uses_context'] ); - $this->assertSameSets( array(), $data['block_hooks'], 'invalid block_hooks defaults to empty array' ); - $this->assertSameSets( array( 'invalid_keywords' ), $data['keywords'] ); - $this->assertSameSets( array( 'invalid_parent' ), $data['parent'] ); - $this->assertSameSets( array( 'invalid_ancestor' ), $data['ancestor'] ); + $this->assertSameSets( array(), $data['provides_context'] ); $this->assertSameSets( array(), $data['selectors'], 'invalid selectors defaults to empty array' ); $this->assertSameSets( array(), $data['supports'] ); $this->assertSameSets( array(), $data['styles'] ); $this->assertNull( $data['example'] ); - $this->assertNull( $data['category'] ); - $this->assertNull( $data['textdomain'] ); - $this->assertFalse( $data['is_dynamic'] ); $this->assertSameSets( array( array() ), $data['variations'] ); + $this->assertSameSets( array(), $data['block_hooks'], 'invalid block_hooks defaults to empty array' ); + $this->assertSameSets( array(), $data['editor_script_handles'] ); + $this->assertSameSets( array(), $data['script_handles'] ); + $this->assertSameSets( array(), $data['view_script_handles'] ); + $this->assertSameSets( array(), $data['editor_style_handles'] ); + $this->assertSameSets( array(), $data['style_handles'] ); + $this->assertFalse( $data['is_dynamic'] ); // Deprecated properties. $this->assertNull( $data['editor_script'] ); $this->assertNull( $data['script'] ); @@ -275,28 +275,28 @@ class REST_Block_Type_Controller_Test extends WP_Test_REST_Controller_Testcase { $block_type = 'fake/false'; $settings = array( 'title' => false, - 'description' => false, + 'category' => false, + 'parent' => false, + 'ancestor' => false, 'icon' => false, + 'description' => false, + 'keywords' => false, + 'textdomain' => false, 'attributes' => false, 'provides_context' => false, 'uses_context' => false, + 'selectors' => false, + 'supports' => false, + 'styles' => false, + 'example' => false, + 'variations' => false, 'block_hooks' => false, - 'category' => false, 'editor_script' => false, 'script' => false, 'view_script' => false, 'editor_style' => false, 'style' => false, - 'keywords' => false, - 'parent' => false, - 'ancestor' => false, - 'selectors' => false, - 'supports' => false, - 'styles' => false, 'render_callback' => false, - 'textdomain' => false, - 'example' => false, - 'variations' => false, ); register_block_type( $block_type, $settings ); wp_set_current_user( self::$admin_id ); @@ -305,13 +305,13 @@ class REST_Block_Type_Controller_Test extends WP_Test_REST_Controller_Testcase { $data = $response->get_data(); $this->assertSame( $block_type, $data['name'] ); $this->assertSame( '', $data['title'] ); - $this->assertSame( '', $data['description'] ); + $this->assertNull( $data['category'] ); + $this->assertSameSets( array(), $data['parent'] ); + $this->assertSameSets( array(), $data['ancestor'] ); $this->assertNull( $data['icon'] ); - $this->assertSameSets( array(), $data['editor_script_handles'] ); - $this->assertSameSets( array(), $data['script_handles'] ); - $this->assertSameSets( array(), $data['view_script_handles'] ); - $this->assertSameSets( array(), $data['editor_style_handles'] ); - $this->assertSameSets( array(), $data['style_handles'] ); + $this->assertSame( '', $data['description'] ); + $this->assertSameSets( array(), $data['keywords'] ); + $this->assertNull( $data['textdomain'] ); $this->assertSameSetsWithIndex( array( 'lock' => array( 'type' => 'object' ), @@ -319,20 +319,19 @@ class REST_Block_Type_Controller_Test extends WP_Test_REST_Controller_Testcase { $data['attributes'] ); $this->assertSameSets( array(), $data['provides_context'] ); - $this->assertSameSets( array(), $data['block_hooks'], 'block_hooks defaults to empty array' ); $this->assertSameSets( array(), $data['uses_context'] ); - $this->assertSameSets( array(), $data['keywords'] ); - $this->assertSameSets( array(), $data['parent'] ); - $this->assertSameSets( array(), $data['ancestor'] ); $this->assertSameSets( array(), $data['selectors'], 'selectors defaults to empty array' ); $this->assertSameSets( array(), $data['supports'] ); $this->assertSameSets( array(), $data['styles'] ); $this->assertNull( $data['example'] ); - $this->assertNull( $data['category'] ); - $this->assertNull( $data['example'] ); - $this->assertNull( $data['textdomain'] ); - $this->assertFalse( $data['is_dynamic'] ); $this->assertSameSets( array(), $data['variations'] ); + $this->assertSameSets( array(), $data['block_hooks'], 'block_hooks defaults to empty array' ); + $this->assertSameSets( array(), $data['editor_script_handles'] ); + $this->assertSameSets( array(), $data['script_handles'] ); + $this->assertSameSets( array(), $data['view_script_handles'] ); + $this->assertSameSets( array(), $data['editor_style_handles'] ); + $this->assertSameSets( array(), $data['style_handles'] ); + $this->assertFalse( $data['is_dynamic'] ); // Deprecated properties. $this->assertNull( $data['editor_script'] ); $this->assertNull( $data['script'] ); @@ -554,30 +553,30 @@ class REST_Block_Type_Controller_Test extends WP_Test_REST_Controller_Testcase { $properties = $data['schema']['properties']; $this->assertCount( 30, $properties ); $this->assertArrayHasKey( 'api_version', $properties ); + $this->assertArrayHasKey( 'name', $properties ); $this->assertArrayHasKey( 'title', $properties ); + $this->assertArrayHasKey( 'category', $properties ); + $this->assertArrayHasKey( 'parent', $properties ); + $this->assertArrayHasKey( 'ancestor', $properties ); $this->assertArrayHasKey( 'icon', $properties ); $this->assertArrayHasKey( 'description', $properties ); $this->assertArrayHasKey( 'keywords', $properties ); - $this->assertArrayHasKey( 'styles', $properties ); $this->assertArrayHasKey( 'textdomain', $properties ); - $this->assertArrayHasKey( 'name', $properties ); $this->assertArrayHasKey( 'attributes', $properties ); + $this->assertArrayHasKey( 'provides_context', $properties ); + $this->assertArrayHasKey( 'uses_context', $properties ); $this->assertArrayHasKey( 'selectors', $properties, 'schema must contain selectors' ); $this->assertArrayHasKey( 'supports', $properties ); - $this->assertArrayHasKey( 'category', $properties ); - $this->assertArrayHasKey( 'is_dynamic', $properties ); + $this->assertArrayHasKey( 'styles', $properties ); + $this->assertArrayHasKey( 'example', $properties ); + $this->assertArrayHasKey( 'variations', $properties ); + $this->assertArrayHasKey( 'block_hooks', $properties ); $this->assertArrayHasKey( 'editor_script_handles', $properties ); $this->assertArrayHasKey( 'script_handles', $properties ); $this->assertArrayHasKey( 'view_script_handles', $properties ); $this->assertArrayHasKey( 'editor_style_handles', $properties ); $this->assertArrayHasKey( 'style_handles', $properties ); - $this->assertArrayHasKey( 'parent', $properties ); - $this->assertArrayHasKey( 'example', $properties ); - $this->assertArrayHasKey( 'uses_context', $properties ); - $this->assertArrayHasKey( 'provides_context', $properties ); - $this->assertArrayHasKey( 'block_hooks', $properties ); - $this->assertArrayHasKey( 'variations', $properties ); - $this->assertArrayHasKey( 'ancestor', $properties ); + $this->assertArrayHasKey( 'is_dynamic', $properties ); // Deprecated properties. $this->assertArrayHasKey( 'editor_script', $properties ); $this->assertArrayHasKey( 'script', $properties ); @@ -686,24 +685,27 @@ class REST_Block_Type_Controller_Test extends WP_Test_REST_Controller_Testcase { $extra_fields = array( 'api_version', 'name', + 'title', 'category', + 'parent', + 'ancestor', + 'icon', + 'description', + 'keywords', + 'textdomain', + 'provides_context', + 'uses_context', + 'selectors', + 'supports', + 'styles', + 'example', + 'variations', + 'block_hooks', 'editor_script_handles', 'script_handles', 'view_script_handles', 'editor_style_handles', 'style_handles', - 'title', - 'icon', - 'description', - 'keywords', - 'parent', - 'provides_context', - 'uses_context', - 'block_hooks', - 'supports', - 'styles', - 'textdomain', - 'example', // Deprecated fields. 'editor_script', 'script',