REST API: Always include title.raw/content.raw for Blocks in context=view.

Demarcations for reusable blocks are always expected to be accessible by clients.

Props noisysocks, youknowriad.

Merges [43917] to trunk.

See #45145 for the patch, #45098 for the original ticket.

git-svn-id: https://develop.svn.wordpress.org/trunk@44268 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Jonathan Desrosiers
2018-12-17 17:21:05 +00:00
parent e0942aec0c
commit abc29f4cef
2 changed files with 95 additions and 17 deletions

View File

@@ -29,13 +29,13 @@ class REST_Blocks_Controller_Test extends WP_UnitTestCase {
protected static $post_id;
/**
* Our fake user's ID.
* Our fake user IDs, keyed by their role.
*
* @since 5.0.0
*
* @var int
* @var array
*/
protected static $user_id;
protected static $user_ids;
/**
* Create fake data before our tests run.
@@ -50,14 +50,14 @@ class REST_Blocks_Controller_Test extends WP_UnitTestCase {
'post_type' => 'wp_block',
'post_status' => 'publish',
'post_title' => 'My cool block',
'post_content' => '<!-- wp:core/paragraph --><p>Hello!</p><!-- /wp:core/paragraph -->',
'post_content' => '<!-- wp:paragraph --><p>Hello!</p><!-- /wp:paragraph -->',
)
);
self::$user_id = $factory->user->create(
array(
'role' => 'editor',
)
self::$user_ids = array(
'editor' => $factory->user->create( array( 'role' => 'editor' ) ),
'author' => $factory->user->create( array( 'role' => 'author' ) ),
'contributor' => $factory->user->create( array( 'role' => 'contributor' ) ),
);
}
@@ -69,7 +69,9 @@ class REST_Blocks_Controller_Test extends WP_UnitTestCase {
public static function wpTearDownAfterClass() {
wp_delete_post( self::$post_id );
self::delete_user( self::$user_id );
foreach ( self::$user_ids as $user_id ) {
self::delete_user( $user_id );
}
}
/**
@@ -114,7 +116,7 @@ class REST_Blocks_Controller_Test extends WP_UnitTestCase {
*/
public function test_capabilities( $action, $role, $expected_status ) {
if ( $role ) {
$user_id = $this->factory->user->create( array( 'role' => $role ) );
$user_id = self::$user_ids[ $role ];
wp_set_current_user( $user_id );
} else {
wp_set_current_user( 0 );
@@ -126,7 +128,7 @@ class REST_Blocks_Controller_Test extends WP_UnitTestCase {
$request->set_body_params(
array(
'title' => 'Test',
'content' => '<!-- wp:core/paragraph --><p>Test</p><!-- /wp:core/paragraph -->',
'content' => '<!-- wp:paragraph --><p>Test</p><!-- /wp:paragraph -->',
)
);
@@ -149,7 +151,7 @@ class REST_Blocks_Controller_Test extends WP_UnitTestCase {
'post_type' => 'wp_block',
'post_status' => 'publish',
'post_title' => 'My cool block',
'post_content' => '<!-- wp:core/paragraph --><p>Hello!</p><!-- /wp:core/paragraph -->',
'post_content' => '<!-- wp:paragraph --><p>Hello!</p><!-- /wp:paragraph -->',
'post_author' => $user_id,
)
);
@@ -158,7 +160,7 @@ class REST_Blocks_Controller_Test extends WP_UnitTestCase {
$request->set_body_params(
array(
'title' => 'Test',
'content' => '<!-- wp:core/paragraph --><p>Test</p><!-- /wp:core/paragraph -->',
'content' => '<!-- wp:paragraph --><p>Test</p><!-- /wp:paragraph -->',
)
);
@@ -179,7 +181,7 @@ class REST_Blocks_Controller_Test extends WP_UnitTestCase {
$request->set_body_params(
array(
'title' => 'Test',
'content' => '<!-- wp:core/paragraph --><p>Test</p><!-- /wp:core/paragraph -->',
'content' => '<!-- wp:paragraph --><p>Test</p><!-- /wp:paragraph -->',
)
);
@@ -196,9 +198,32 @@ class REST_Blocks_Controller_Test extends WP_UnitTestCase {
default:
$this->fail( "'$action' is not a valid action." );
}
}
if ( isset( $user_id ) ) {
self::delete_user( $user_id );
}
/**
* Check that the raw title and content of a block can be accessed when there
* is no set schema, and that the rendered content of a block is not included
* in the response.
*/
public function test_content() {
wp_set_current_user( self::$user_ids['author'] );
$request = new WP_REST_Request( 'GET', '/wp/v2/blocks/' . self::$post_id );
$response = rest_get_server()->dispatch( $request );
$data = $response->get_data();
$this->assertEquals(
array(
'raw' => 'My cool block',
),
$data['title']
);
$this->assertEquals(
array(
'raw' => '<!-- wp:paragraph --><p>Hello!</p><!-- /wp:paragraph -->',
'protected' => false,
),
$data['content']
);
}
}