Skip to content

Commit

Permalink
Register post_id. Add context. Fix tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
miina committed Apr 16, 2018
1 parent 3c60d4b commit a10bfac
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 9 deletions.
28 changes: 23 additions & 5 deletions lib/class-wp-rest-block-renderer-controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -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' ),
Expand Down Expand Up @@ -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(
Expand All @@ -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 );
Expand All @@ -138,6 +155,7 @@ public function get_item_schema() {
'description' => __( 'The rendered block.', 'gutenberg' ),
'type' => 'string',
'required' => true,
'context' => array( 'edit' ),
),
),
);
Expand Down
30 changes: 26 additions & 4 deletions phpunit/class-rest-block-renderer-controller-test.php
Original file line number Diff line number Diff line change
Expand Up @@ -160,20 +160,36 @@ 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.
*
* @covers WP_REST_Block_Renderer_Controller::get_item()
*/
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 );
Expand All @@ -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!' ),
) );
Expand All @@ -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',
) );
Expand All @@ -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() );
Expand Down Expand Up @@ -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() );
Expand All @@ -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 );
Expand Down Expand Up @@ -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'] );
Expand All @@ -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() {
Expand All @@ -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() {
Expand Down

0 comments on commit a10bfac

Please sign in to comment.