- Timestamp:
- 11/16/2021 06:04:49 PM (3 years ago)
- File:
-
- 1 moved
Legend:
- Unmodified
- Added
- Removed
-
trunk/tests/phpunit/tests/rest-api/wpRestTemplatesController.php
r52185 r52186 7 7 */ 8 8 9 class WP_REST_Template_Controller_Test extends WP_Test_REST_Controller_Testcase { 9 /** 10 * Tests for REST API for templates. 11 * 12 * @covers WP_REST_Templates_Controller 13 * 14 * @group restapi 15 */ 16 class Tests_REST_WpRestTemplatesController extends WP_Test_REST_Controller_Testcase { 10 17 /** 11 18 * @var int … … 54 61 } 55 62 63 /** 64 * @covers WP_REST_Templates_Controller::get_context_param 65 */ 56 66 public function test_context_param() { 57 // TODO: Implement test_context_param() method. 58 } 59 67 // Collection. 68 $request = new WP_REST_Request( 'OPTIONS', '/wp/v2/templates' ); 69 $response = rest_get_server()->dispatch( $request ); 70 $data = $response->get_data(); 71 $this->assertSame( 'view', $data['endpoints'][0]['args']['context']['default'] ); 72 $this->assertSame( array( 'view', 'embed', 'edit' ), $data['endpoints'][0]['args']['context']['enum'] ); 73 // Single. 74 $request = new WP_REST_Request( 'OPTIONS', '/wp/v2/templates/default//my_template' ); 75 $response = rest_get_server()->dispatch( $request ); 76 $data = $response->get_data(); 77 $this->assertSame( 'view', $data['endpoints'][0]['args']['context']['default'] ); 78 $this->assertSame( array( 'view', 'embed', 'edit' ), $data['endpoints'][0]['args']['context']['enum'] ); 79 } 80 81 /** 82 * @covers WP_REST_Templates_Controller::get_items 83 */ 60 84 public function test_get_items() { 61 function find_and_normalize_template_by_id( $templates, $id ) {62 foreach ( $templates as $template ) {63 if ( $template['id'] === $id ) {64 unset( $template['content'] );65 unset( $template['_links'] );66 return $template;67 }68 }69 70 return null;71 }72 73 wp_set_current_user( 0 );74 $request = new WP_REST_Request( 'GET', '/wp/v2/templates' );75 $response = rest_get_server()->dispatch( $request );76 $this->assertErrorResponse( 'rest_cannot_manage_templates', $response, 401 );77 78 85 wp_set_current_user( self::$admin_id ); 79 86 $request = new WP_REST_Request( 'GET', '/wp/v2/templates' ); … … 97 104 'has_theme_file' => false, 98 105 ), 99 find_and_normalize_template_by_id( $data, 'default//my_template' ) 100 ); 101 } 102 106 $this->find_and_normalize_template_by_id( $data, 'default//my_template' ) 107 ); 108 } 109 110 /** 111 * @covers WP_REST_Templates_Controller::get_items 112 */ 113 public function test_get_items_no_permission() { 114 wp_set_current_user( 0 ); 115 $request = new WP_REST_Request( 'GET', '/wp/v2/templates' ); 116 $response = rest_get_server()->dispatch( $request ); 117 $this->assertErrorResponse( 'rest_cannot_manage_templates', $response, 401 ); 118 } 119 120 /** 121 * @covers WP_REST_Templates_Controller::get_item 122 */ 103 123 public function test_get_item() { 104 124 wp_set_current_user( self::$admin_id ); … … 129 149 } 130 150 151 /** 152 * @ticket 54422 153 * @covers WP_REST_Templates_Controller::create_item 154 */ 131 155 public function test_create_item() { 132 156 wp_set_current_user( self::$admin_id ); … … 167 191 } 168 192 193 /** 194 * @ticket 54422 195 * @covers WP_REST_Templates_Controller::create_item 196 */ 197 public function test_create_item_raw() { 198 wp_set_current_user( self::$admin_id ); 199 $request = new WP_REST_Request( 'POST', '/wp/v2/templates' ); 200 $request->set_body_params( 201 array( 202 'slug' => 'my_custom_template_raw', 203 'description' => 'Just a description', 204 'title' => array( 205 'raw' => 'My Template', 206 ), 207 'content' => array( 208 'raw' => 'Content', 209 ), 210 ) 211 ); 212 $response = rest_get_server()->dispatch( $request ); 213 $data = $response->get_data(); 214 unset( $data['_links'] ); 215 unset( $data['wp_id'] ); 216 217 $this->assertSame( 218 array( 219 'id' => 'default//my_custom_template_raw', 220 'theme' => 'default', 221 'content' => array( 222 'raw' => 'Content', 223 ), 224 'slug' => 'my_custom_template_raw', 225 'source' => 'custom', 226 'type' => 'wp_template', 227 'description' => 'Just a description', 228 'title' => array( 229 'raw' => 'My Template', 230 'rendered' => 'My Template', 231 ), 232 'status' => 'publish', 233 'has_theme_file' => false, 234 ), 235 $data 236 ); 237 } 238 239 /** 240 * @covers WP_REST_Templates_Controller::update_item 241 */ 169 242 public function test_update_item() { 170 243 wp_set_current_user( self::$admin_id ); … … 181 254 } 182 255 256 /** 257 * @covers WP_REST_Templates_Controller::update_item 258 */ 259 public function test_update_item_raw() { 260 wp_set_current_user( self::$admin_id ); 261 $request = new WP_REST_Request( 'PUT', '/wp/v2/templates/default//my_template' ); 262 $request->set_body_params( 263 array( 264 'title' => array( 'raw' => 'My new raw Index Title' ), 265 ) 266 ); 267 $response = rest_get_server()->dispatch( $request ); 268 $data = $response->get_data(); 269 $this->assertSame( 'My new raw Index Title', $data['title']['raw'] ); 270 $this->assertSame( 'custom', $data['source'] ); 271 } 272 273 /** 274 * @covers WP_REST_Templates_Controller::delete_item 275 */ 183 276 public function test_delete_item() { 277 // Set up template post. 278 $args = array( 279 'post_type' => 'wp_template', 280 'post_name' => 'my_test_template', 281 'post_title' => 'My Template', 282 'post_content' => 'Content', 283 'post_excerpt' => 'Description of my template.', 284 'tax_input' => array( 285 'wp_theme' => array( 286 get_stylesheet(), 287 ), 288 ), 289 ); 290 $post_id = self::factory()->post->create( $args ); 291 wp_set_post_terms( $post_id, get_stylesheet(), 'wp_theme' ); 292 293 wp_set_current_user( self::$admin_id ); 294 $request = new WP_REST_Request( 'DELETE', '/wp/v2/templates/default//my_test_template' ); 295 $request->set_param( 'force', 'false' ); 296 $response = rest_get_server()->dispatch( $request ); 297 $data = $response->get_data(); 298 $this->assertSame( 'My Template', $data['title']['raw'] ); 299 $this->assertSame( 'trash', $data['status'] ); 300 } 301 302 /** 303 * @covers WP_REST_Templates_Controller::delete_item 304 */ 305 public function test_delete_item_skip_trash() { 306 // Set up template post. 307 $args = array( 308 'post_type' => 'wp_template', 309 'post_name' => 'my_test_template', 310 'post_title' => 'My Template', 311 'post_content' => 'Content', 312 'post_excerpt' => 'Description of my template.', 313 'tax_input' => array( 314 'wp_theme' => array( 315 get_stylesheet(), 316 ), 317 ), 318 ); 319 $post_id = self::factory()->post->create( $args ); 320 wp_set_post_terms( $post_id, get_stylesheet(), 'wp_theme' ); 321 322 wp_set_current_user( self::$admin_id ); 323 $request = new WP_REST_Request( 'DELETE', '/wp/v2/templates/default//my_test_template' ); 324 $request->set_param( 'force', 'true' ); 325 $response = rest_get_server()->dispatch( $request ); 326 $this->assertSame( 200, $response->get_status() ); 327 $data = $response->get_data(); 328 $this->assertTrue( $data['deleted'] ); 329 $this->assertNotEmpty( $data['previous'] ); 330 } 331 332 /** 333 * @covers WP_REST_Templates_Controller::delete_item 334 */ 335 public function test_delete_item_fail() { 184 336 wp_set_current_user( self::$admin_id ); 185 337 $request = new WP_REST_Request( 'DELETE', '/wp/v2/templates/justrandom//template' ); … … 192 344 } 193 345 346 public function test_prepare_item_limit_fields() { 347 wp_set_current_user( self::$admin_id ); 348 349 $endpoint = new WP_REST_Templates_Controller( 'wp_template' ); 350 $request = new WP_REST_Request( 'GET', '/wp/v2/templates/default//my_template' ); 351 $request->set_param( 'context', 'edit' ); 352 $request->set_param( '_fields', 'id,slug' ); 353 $obj = get_block_template( 'default//my_template', 'wp_template' ); 354 $response = $endpoint->prepare_item_for_response( $obj, $request ); 355 $this->assertSame( 356 array( 357 'id', 358 'slug', 359 ), 360 array_keys( $response->get_data() ) 361 ); 362 } 363 364 /** 365 * @ticket 54422 366 * @covers WP_REST_Templates_Controller::get_item_schema 367 */ 194 368 public function test_get_item_schema() { 195 // TODO: Implement test_get_item_schema() method. 196 } 369 $request = new WP_REST_Request( 'OPTIONS', '/wp/v2/templates' ); 370 $response = rest_get_server()->dispatch( $request ); 371 $data = $response->get_data(); 372 $properties = $data['schema']['properties']; 373 $this->assertCount( 11, $properties ); 374 $this->assertArrayHasKey( 'id', $properties ); 375 $this->assertArrayHasKey( 'description', $properties ); 376 $this->assertArrayHasKey( 'slug', $properties ); 377 $this->assertArrayHasKey( 'theme', $properties ); 378 $this->assertArrayHasKey( 'type', $properties ); 379 $this->assertArrayHasKey( 'source', $properties ); 380 $this->assertArrayHasKey( 'content', $properties ); 381 $this->assertArrayHasKey( 'title', $properties ); 382 $this->assertArrayHasKey( 'description', $properties ); 383 $this->assertArrayHasKey( 'status', $properties ); 384 $this->assertArrayHasKey( 'wp_id', $properties ); 385 $this->assertArrayHasKey( 'has_theme_file', $properties ); 386 } 387 388 protected function find_and_normalize_template_by_id( $templates, $id ) { 389 foreach ( $templates as $template ) { 390 if ( $template['id'] === $id ) { 391 unset( $template['content'] ); 392 unset( $template['_links'] ); 393 return $template; 394 } 395 } 396 397 return null; 398 } 399 197 400 }
Note: See TracChangeset
for help on using the changeset viewer.