From a10bfacd4de9ff159807ecf1f05d13323a7eecef Mon Sep 17 00:00:00 2001 From: Miina Sikk Date: Mon, 16 Apr 2018 22:33:02 +0300 Subject: [PATCH] Register post_id. Add context. Fix tests. --- ...lass-wp-rest-block-renderer-controller.php | 28 +++++++++++++---- ...ss-rest-block-renderer-controller-test.php | 30 ++++++++++++++++--- 2 files changed, 49 insertions(+), 9 deletions(-) diff --git a/lib/class-wp-rest-block-renderer-controller.php b/lib/class-wp-rest-block-renderer-controller.php index ddb66f14571dec..8ea05e3872ce6b 100644 --- a/lib/class-wp-rest-block-renderer-controller.php +++ b/lib/class-wp-rest-block-renderer-controller.php @@ -59,6 +59,10 @@ public function register_routes() { 'additionalProperties' => false, 'properties' => $block_type->attributes, ), + 'post_id' => array( + 'description' => __( 'ID of the post context.', 'travel' ), + 'type' => 'integer', + ), ), ), 'schema' => array( $this, 'get_public_item_schema' ), @@ -87,10 +91,6 @@ public function get_item_permissions_check( $request ) { 'status' => rest_authorization_required_code(), ) ); } - - // Set up postdata since this will be needed if post_ID was set. - setup_postdata( $post ); - } else { if ( ! current_user_can( 'edit_posts' ) ) { return new WP_Error( 'gutenberg_block_cannot_read', __( 'Sorry, you are not allowed to read Gutenberg blocks as this user.', 'gutenberg' ), array( @@ -112,9 +112,26 @@ public function get_item_permissions_check( $request ) { * @return WP_REST_Response|WP_Error Response object on success, or WP_Error object on failure. */ public function get_item( $request ) { + global $post; + + $post_ID = isset( $request['post_id'] ) ? intval( $request['post_id'] ) : 0; + + if ( 0 < $post_ID ) { + $post = get_post( $post_ID ); + + // Set up postdata since this will be needed if post_id was set. + setup_postdata( $post ); + } $registry = WP_Block_Type_Registry::get_instance(); $block = $registry->get_registered( $request['name'] ); - $data = array( + + if ( null === $block ) { + return new WP_Error( 'gutenberg_block_invalid', __( 'Invalid block.', 'gutenberg' ), array( + 'status' => 404 + ) ); + } + + $data = array( 'rendered' => $block->render( $request->get_param( 'attributes' ) ), ); return rest_ensure_response( $data ); @@ -138,6 +155,7 @@ public function get_item_schema() { 'description' => __( 'The rendered block.', 'gutenberg' ), 'type' => 'string', 'required' => true, + 'context' => array( 'edit' ), ), ), ); diff --git a/phpunit/class-rest-block-renderer-controller-test.php b/phpunit/class-rest-block-renderer-controller-test.php index 4b0af8099dac80..1d1b19b735efc9 100644 --- a/phpunit/class-rest-block-renderer-controller-test.php +++ b/phpunit/class-rest-block-renderer-controller-test.php @@ -160,12 +160,26 @@ public function test_register_routes() { public function test_get_item_without_permissions() { wp_set_current_user( 0 ); - $request = new WP_REST_Request( 'GET', '/gutenberg/v1/block-renderer/' . self::$block_name ); + $request = new WP_REST_Request( 'GET', '/gutenberg/v1/block-renderer/' . self::$block_name ); + $request->set_param( 'context', 'edit' ); + $response = $this->server->dispatch( $request ); $this->assertErrorResponse( 'gutenberg_block_cannot_read', $response, rest_authorization_required_code() ); } + /** + * Test getting item without 'edit' context. + */ + public function test_get_item_with_invalid_context() { + wp_set_current_user( self::$user_id ); + + $request = new WP_REST_Request( 'GET', '/gutenberg/v1/block-renderer/' . self::$block_name ); + $response = $this->server->dispatch( $request ); + + $this->assertErrorResponse( 'rest_invalid_param', $response, 400 ); + } + /** * Test getting item with invalid block name. * @@ -173,7 +187,9 @@ public function test_get_item_without_permissions() { */ public function test_get_item_invalid_block_name() { wp_set_current_user( self::$user_id ); - $request = new WP_REST_Request( 'GET', '/gutenberg/v1/block-renderer/core/123' ); + $request = new WP_REST_Request( 'GET', '/gutenberg/v1/block-renderer/core/123' ); + + $request->set_param( 'context', 'edit' ); $response = $this->server->dispatch( $request ); $this->assertErrorResponse( 'rest_no_route', $response, 404 ); @@ -187,6 +203,7 @@ public function test_get_item_invalid_block_name() { public function test_get_item_invalid_attribute() { wp_set_current_user( self::$user_id ); $request = new WP_REST_Request( 'GET', '/gutenberg/v1/block-renderer/' . self::$block_name ); + $request->set_param( 'context', 'edit' ); $request->set_param( 'attributes', array( 'some_string' => array( 'no!' ), ) ); @@ -202,6 +219,7 @@ public function test_get_item_invalid_attribute() { public function test_get_item_unrecognized_attribute() { wp_set_current_user( self::$user_id ); $request = new WP_REST_Request( 'GET', '/gutenberg/v1/block-renderer/' . self::$block_name ); + $request->set_param( 'context', 'edit' ); $request->set_param( 'attributes', array( 'unrecognized' => 'yes', ) ); @@ -224,6 +242,7 @@ public function test_get_item_default_attributes() { } $request = new WP_REST_Request( 'GET', '/gutenberg/v1/block-renderer/' . self::$block_name ); + $request->set_param( 'context', 'edit' ); $request->set_param( 'attributes', array() ); $response = $this->server->dispatch( $request ); $this->assertEquals( 200, $response->get_status() ); @@ -256,6 +275,7 @@ public function test_get_item() { $expected_attributes['some_array'] = array_map( 'intval', $expected_attributes['some_array'] ); $request = new WP_REST_Request( 'GET', '/gutenberg/v1/block-renderer/' . self::$block_name ); + $request->set_param( 'context', 'edit' ); $request->set_param( 'attributes', $attributes ); $response = $this->server->dispatch( $request ); $this->assertEquals( 200, $response->get_status() ); @@ -276,6 +296,7 @@ public function test_get_item_with_post_context() { $expected_title = 'Test Post'; $request = new WP_REST_Request( 'GET', '/gutenberg/v1/block-renderer/' . self::$context_block_name ); + $request->set_param( 'context', 'edit' ); // Test without post ID. $response = $this->server->dispatch( $request ); @@ -307,7 +328,7 @@ public function test_get_item_schema() { $this->assertEqualSets( array( 'GET' ), $data['endpoints'][0]['methods'] ); $this->assertEqualSets( - array( 'name', 'context', 'attributes' ), + array( 'name', 'context', 'attributes', 'post_id' ), array_keys( $data['endpoints'][0]['args'] ) ); $this->assertEquals( 'object', $data['endpoints'][0]['args']['attributes']['type'] ); @@ -317,6 +338,7 @@ public function test_get_item_schema() { $this->assertEquals( 'object', $data['schema']['type'] ); $this->arrayHasKey( 'rendered', $data['schema']['properties'] ); $this->arrayHasKey( 'string', $data['schema']['properties']['rendered']['type'] ); + $this->assertEquals( array( 'edit' ), $data['schema']['properties']['rendered']['context'] ); } public function test_update_item() { @@ -336,7 +358,7 @@ public function test_get_items() { } public function test_context_param() { - $this->markTestIncomplete( 'Controller doesn\'t implement context_param().' ); + $this->markTestSkipped( 'Controller doesn\'t implement context_param().' ); } public function test_prepare_item() {