rest-blocks-controller.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. <?php
  2. /**
  3. * WP_REST_Blocks_Controller tests
  4. *
  5. * @package WordPress
  6. * @subpackage REST_API
  7. * @since 5.0.0
  8. */
  9. /**
  10. * Tests for WP_REST_Blocks_Controller.
  11. *
  12. * @since 5.0.0
  13. *
  14. * @see WP_Test_REST_Controller_Testcase
  15. *
  16. * @group restapi-blocks
  17. * @group restapi
  18. */
  19. class REST_Blocks_Controller_Test extends WP_UnitTestCase {
  20. /**
  21. * Our fake block's post ID.
  22. *
  23. * @since 5.0.0
  24. *
  25. * @var int
  26. */
  27. protected static $post_id;
  28. /**
  29. * Our fake user IDs, keyed by their role.
  30. *
  31. * @since 5.0.0
  32. *
  33. * @var array
  34. */
  35. protected static $user_ids;
  36. /**
  37. * Create fake data before our tests run.
  38. *
  39. * @since 5.0.0
  40. *
  41. * @param WP_UnitTest_Factory $factory Helper that lets us create fake data.
  42. */
  43. public static function wpSetUpBeforeClass( WP_UnitTest_Factory $factory ) {
  44. self::$post_id = wp_insert_post(
  45. array(
  46. 'post_type' => 'wp_block',
  47. 'post_status' => 'publish',
  48. 'post_title' => 'My cool block',
  49. 'post_content' => '<!-- wp:paragraph --><p>Hello!</p><!-- /wp:paragraph -->',
  50. )
  51. );
  52. self::$user_ids = array(
  53. 'editor' => $factory->user->create( array( 'role' => 'editor' ) ),
  54. 'author' => $factory->user->create( array( 'role' => 'author' ) ),
  55. 'contributor' => $factory->user->create( array( 'role' => 'contributor' ) ),
  56. );
  57. }
  58. /**
  59. * Delete our fake data after our tests run.
  60. *
  61. * @since 5.0.0
  62. */
  63. public static function wpTearDownAfterClass() {
  64. wp_delete_post( self::$post_id );
  65. foreach ( self::$user_ids as $user_id ) {
  66. self::delete_user( $user_id );
  67. }
  68. }
  69. /**
  70. * Test cases for test_capabilities().
  71. *
  72. * @since 5.0.0
  73. */
  74. public function data_capabilities() {
  75. return array(
  76. array( 'create', 'editor', 201 ),
  77. array( 'create', 'author', 201 ),
  78. array( 'create', 'contributor', 403 ),
  79. array( 'create', null, 401 ),
  80. array( 'read', 'editor', 200 ),
  81. array( 'read', 'author', 200 ),
  82. array( 'read', 'contributor', 200 ),
  83. array( 'read', null, 401 ),
  84. array( 'update_delete_own', 'editor', 200 ),
  85. array( 'update_delete_own', 'author', 200 ),
  86. array( 'update_delete_own', 'contributor', 403 ),
  87. array( 'update_delete_others', 'editor', 200 ),
  88. array( 'update_delete_others', 'author', 403 ),
  89. array( 'update_delete_others', 'contributor', 403 ),
  90. array( 'update_delete_others', null, 401 ),
  91. );
  92. }
  93. /**
  94. * Exhaustively check that each role either can or cannot create, edit,
  95. * update, and delete reusable blocks.
  96. *
  97. * @ticket 45098
  98. *
  99. * @dataProvider data_capabilities
  100. *
  101. * @param string $action Action to perform in the test.
  102. * @param string $role User role to test.
  103. * @param int $expected_status Expected HTTP response status.
  104. */
  105. public function test_capabilities( $action, $role, $expected_status ) {
  106. if ( $role ) {
  107. $user_id = self::$user_ids[ $role ];
  108. wp_set_current_user( $user_id );
  109. } else {
  110. wp_set_current_user( 0 );
  111. }
  112. switch ( $action ) {
  113. case 'create':
  114. $request = new WP_REST_Request( 'POST', '/wp/v2/blocks' );
  115. $request->set_body_params(
  116. array(
  117. 'title' => 'Test',
  118. 'content' => '<!-- wp:paragraph --><p>Test</p><!-- /wp:paragraph -->',
  119. )
  120. );
  121. $response = rest_get_server()->dispatch( $request );
  122. $this->assertSame( $expected_status, $response->get_status() );
  123. break;
  124. case 'read':
  125. $request = new WP_REST_Request( 'GET', '/wp/v2/blocks/' . self::$post_id );
  126. $response = rest_get_server()->dispatch( $request );
  127. $this->assertSame( $expected_status, $response->get_status() );
  128. break;
  129. case 'update_delete_own':
  130. $post_id = wp_insert_post(
  131. array(
  132. 'post_type' => 'wp_block',
  133. 'post_status' => 'publish',
  134. 'post_title' => 'My cool block',
  135. 'post_content' => '<!-- wp:paragraph --><p>Hello!</p><!-- /wp:paragraph -->',
  136. 'post_author' => $user_id,
  137. )
  138. );
  139. $request = new WP_REST_Request( 'PUT', '/wp/v2/blocks/' . $post_id );
  140. $request->set_body_params(
  141. array(
  142. 'title' => 'Test',
  143. 'content' => '<!-- wp:paragraph --><p>Test</p><!-- /wp:paragraph -->',
  144. )
  145. );
  146. $response = rest_get_server()->dispatch( $request );
  147. $this->assertSame( $expected_status, $response->get_status() );
  148. $request = new WP_REST_Request( 'DELETE', '/wp/v2/blocks/' . $post_id );
  149. $response = rest_get_server()->dispatch( $request );
  150. $this->assertSame( $expected_status, $response->get_status() );
  151. wp_delete_post( $post_id );
  152. break;
  153. case 'update_delete_others':
  154. $request = new WP_REST_Request( 'PUT', '/wp/v2/blocks/' . self::$post_id );
  155. $request->set_body_params(
  156. array(
  157. 'title' => 'Test',
  158. 'content' => '<!-- wp:paragraph --><p>Test</p><!-- /wp:paragraph -->',
  159. )
  160. );
  161. $response = rest_get_server()->dispatch( $request );
  162. $this->assertSame( $expected_status, $response->get_status() );
  163. $request = new WP_REST_Request( 'DELETE', '/wp/v2/blocks/' . self::$post_id );
  164. $response = rest_get_server()->dispatch( $request );
  165. $this->assertSame( $expected_status, $response->get_status() );
  166. break;
  167. default:
  168. $this->fail( "'$action' is not a valid action." );
  169. }
  170. }
  171. /**
  172. * Check that the raw title and content of a block can be accessed when there
  173. * is no set schema, and that the rendered content of a block is not included
  174. * in the response.
  175. */
  176. public function test_content() {
  177. wp_set_current_user( self::$user_ids['author'] );
  178. $request = new WP_REST_Request( 'GET', '/wp/v2/blocks/' . self::$post_id );
  179. $response = rest_get_server()->dispatch( $request );
  180. $data = $response->get_data();
  181. $this->assertSame(
  182. array(
  183. 'raw' => 'My cool block',
  184. ),
  185. $data['title']
  186. );
  187. $this->assertSame(
  188. array(
  189. 'raw' => '<!-- wp:paragraph --><p>Hello!</p><!-- /wp:paragraph -->',
  190. 'protected' => false,
  191. ),
  192. $data['content']
  193. );
  194. }
  195. }