REST API: Include block_version on Post content object.

The `block_version` denotes which version of Blocks the `post_content` contains. Introduces new `block_version()` function for versioning Blocks.

Merges [43770] from the 5.0 branch to trunk.

Props danielbachhuber, birgire.
Fixes #43887.


git-svn-id: https://develop.svn.wordpress.org/trunk@44127 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Gary Pendergast
2018-12-14 00:54:51 +00:00
parent 225f191f32
commit 63d9540fd8
5 changed files with 157 additions and 34 deletions

View File

@@ -310,4 +310,47 @@ class WP_Test_Block_Type extends WP_UnitTestCase {
return json_encode( $attributes );
}
/**
* Testing the block version.
*
* @ticket 43887
*
* @dataProvider data_block_version
*
* @param string|null $content Content.
* @param int $expected Expected block version.
*/
public function test_block_version( $content, $expected ) {
$this->assertSame( $expected, block_version( $content ) );
}
/**
* Test cases for test_block_version().
*
* @since 5.0.0
*
* @return array {
* @type array {
* @type string|null Content.
* @type int Expected block version.
* }
* }
*/
public function data_block_version() {
return array(
// Null.
array( null, 0 ),
// Empty post content.
array( '', 0 ),
// Post content without blocks.
array( '<hr class="wp-block-separator" />', 0 ),
// Post content with a block.
array( '<!-- wp:core/separator -->', 1 ),
// Post content with a fake block.
array( '<!-- wp:core/fake --><!-- /wp:core/fake -->', 1 ),
// Post content with an invalid block.
array( '<!- - wp:core/separator -->', 0 ),
);
}
}

View File

@@ -1554,6 +1554,65 @@ class WP_Test_REST_Posts_Controller extends WP_Test_REST_Post_Type_Controller_Te
$this->assertTrue( $data['excerpt']['protected'] );
}
/**
* The post response should not have `block_version` when in view context.
*
* @ticket 43887
*/
public function test_get_post_should_not_have_block_version_when_context_view() {
$post_id = $this->factory->post->create(
array(
'post_content' => '<!-- wp:core/separator -->',
)
);
$request = new WP_REST_Request( 'GET', sprintf( '/wp/v2/posts/%d', $post_id ) );
$response = rest_get_server()->dispatch( $request );
$data = $response->get_data();
$this->assertFalse( isset( $data['content']['block_version'] ) );
}
/**
* The post response should have `block_version` indicate that block content is present when in edit context.
*
* @ticket 43887
*/
public function test_get_post_should_have_block_version_indicate_block_content_when_context_edit() {
wp_set_current_user( self::$editor_id );
$post_id = $this->factory->post->create(
array(
'post_content' => '<!-- wp:core/separator -->',
)
);
$request = new WP_REST_Request( 'GET', sprintf( '/wp/v2/posts/%d', $post_id ) );
$request->set_param( 'context', 'edit' );
$response = rest_get_server()->dispatch( $request );
$data = $response->get_data();
$this->assertSame( 1, $data['content']['block_version'] );
}
/**
* The post response should have `block_version` indicate that no block content is present when in edit context.
*
* @ticket 43887
*/
public function test_get_post_should_have_block_version_indicate_no_block_content_when_context_edit() {
wp_set_current_user( self::$editor_id );
$post_id = $this->factory->post->create(
array(
'post_content' => '<hr />',
)
);
$request = new WP_REST_Request( 'GET', sprintf( '/wp/v2/posts/%d', $post_id ) );
$request->set_param( 'context', 'edit' );
$response = rest_get_server()->dispatch( $request );
$data = $response->get_data();
$this->assertSame( 0, $data['content']['block_version'] );
}
public function test_get_item_read_permission_custom_post_status_not_authenticated() {
register_post_status( 'testpubstatus', array( 'public' => true ) );
register_post_status( 'testprivtatus', array( 'public' => false ) );