- Timestamp:
- 10/27/2016 02:56:28 AM (9 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/tests/phpunit/tests/rest-api/rest-post-meta-fields.php
r38832 r38975 11 11 */ 12 12 class WP_Test_REST_Post_Meta_Fields extends WP_Test_REST_TestCase { 13 protected static $post_id; 14 15 public static function wpSetUpBeforeClass( $factory ) { 16 self::$post_id = $factory->post->create(); 17 } 18 19 public static function wpTearDownAfterClass() { 20 wp_delete_post( self::$post_id, true ); 21 } 22 13 23 public function setUp() { 14 24 parent::setUp(); … … 55 65 $this->server = $wp_rest_server = new Spy_REST_Server; 56 66 do_action( 'rest_api_init' ); 57 58 $this->post_id = $this->factory->post->create();59 67 } 60 68 … … 68 76 69 77 public function test_get_value() { 70 add_post_meta( $this->post_id, 'test_single', 'testvalue' );71 72 $request = new WP_REST_Request( 'GET', sprintf( '/wp/v2/posts/%d', $this->post_id ) );78 add_post_meta( self::$post_id, 'test_single', 'testvalue' ); 79 80 $request = new WP_REST_Request( 'GET', sprintf( '/wp/v2/posts/%d', self::$post_id ) ); 73 81 $response = $this->server->dispatch( $request ); 74 82 … … 87 95 */ 88 96 public function test_get_multi_value() { 89 add_post_meta( $this->post_id, 'test_multi', 'value1' );90 $request = new WP_REST_Request( 'GET', sprintf( '/wp/v2/posts/%d', $this->post_id ) );97 add_post_meta( self::$post_id, 'test_multi', 'value1' ); 98 $request = new WP_REST_Request( 'GET', sprintf( '/wp/v2/posts/%d', self::$post_id ) ); 91 99 92 100 $response = $this->server->dispatch( $request ); … … 100 108 101 109 // Check after an update. 102 add_post_meta( $this->post_id, 'test_multi', 'value2' );110 add_post_meta( self::$post_id, 'test_multi', 'value2' ); 103 111 104 112 $response = $this->server->dispatch( $request ); … … 114 122 */ 115 123 public function test_get_unregistered() { 116 add_post_meta( $this->post_id, 'test_unregistered', 'value1' );117 $request = new WP_REST_Request( 'GET', sprintf( '/wp/v2/posts/%d', $this->post_id ) );124 add_post_meta( self::$post_id, 'test_unregistered', 'value1' ); 125 $request = new WP_REST_Request( 'GET', sprintf( '/wp/v2/posts/%d', self::$post_id ) ); 118 126 119 127 $response = $this->server->dispatch( $request ); … … 129 137 */ 130 138 public function test_get_registered_no_api_access() { 131 add_post_meta( $this->post_id, 'test_no_rest', 'for_the_wicked' );132 $request = new WP_REST_Request( 'GET', sprintf( '/wp/v2/posts/%d', $this->post_id ) );139 add_post_meta( self::$post_id, 'test_no_rest', 'for_the_wicked' ); 140 $request = new WP_REST_Request( 'GET', sprintf( '/wp/v2/posts/%d', self::$post_id ) ); 133 141 134 142 $response = $this->server->dispatch( $request ); … … 144 152 */ 145 153 public function test_get_registered_api_disabled() { 146 add_post_meta( $this->post_id, 'test_rest_disabled', 'sleepless_nights' );147 $request = new WP_REST_Request( 'GET', sprintf( '/wp/v2/posts/%d', $this->post_id ) );154 add_post_meta( self::$post_id, 'test_rest_disabled', 'sleepless_nights' ); 155 $request = new WP_REST_Request( 'GET', sprintf( '/wp/v2/posts/%d', self::$post_id ) ); 148 156 149 157 $response = $this->server->dispatch( $request ); … … 177 185 do_action( 'rest_api_init' ); 178 186 179 add_post_meta( $this->post_id, 'test_string', 42 );180 add_post_meta( $this->post_id, 'test_number', '42' );181 add_post_meta( $this->post_id, 'test_bool', 1 );182 183 $request = new WP_REST_Request( 'GET', sprintf( '/wp/v2/posts/%d', $this->post_id ) );187 add_post_meta( self::$post_id, 'test_string', 42 ); 188 add_post_meta( self::$post_id, 'test_number', '42' ); 189 add_post_meta( self::$post_id, 'test_bool', 1 ); 190 191 $request = new WP_REST_Request( 'GET', sprintf( '/wp/v2/posts/%d', self::$post_id ) ); 184 192 $response = $this->server->dispatch( $request ); 185 193 $this->assertEquals( 200, $response->get_status() ); … … 206 214 public function test_set_value() { 207 215 // Ensure no data exists currently. 208 $values = get_post_meta( $this->post_id, 'test_single', false );216 $values = get_post_meta( self::$post_id, 'test_single', false ); 209 217 $this->assertEmpty( $values ); 210 218 … … 216 224 ), 217 225 ); 218 $request = new WP_REST_Request( 'POST', sprintf( '/wp/v2/posts/%d', $this->post_id ) );219 $request->set_body_params( $data ); 220 221 $response = $this->server->dispatch( $request ); 222 $this->assertEquals( 200, $response->get_status() ); 223 224 $meta = get_post_meta( $this->post_id, 'test_single', false );226 $request = new WP_REST_Request( 'POST', sprintf( '/wp/v2/posts/%d', self::$post_id ) ); 227 $request->set_body_params( $data ); 228 229 $response = $this->server->dispatch( $request ); 230 $this->assertEquals( 200, $response->get_status() ); 231 232 $meta = get_post_meta( self::$post_id, 'test_single', false ); 225 233 $this->assertNotEmpty( $meta ); 226 234 $this->assertCount( 1, $meta ); … … 238 246 public function test_set_duplicate_single_value() { 239 247 // Start with an existing metakey and value. 240 $values = update_post_meta( $this->post_id, 'test_single', 'test_value' );241 $this->assertEquals( 'test_value', get_post_meta( $this->post_id, 'test_single', true ) );248 $values = update_post_meta( self::$post_id, 'test_single', 'test_value' ); 249 $this->assertEquals( 'test_value', get_post_meta( self::$post_id, 'test_single', true ) ); 242 250 243 251 $this->grant_write_permission(); … … 248 256 ), 249 257 ); 250 $request = new WP_REST_Request( 'POST', sprintf( '/wp/v2/posts/%d', $this->post_id ) );251 $request->set_body_params( $data ); 252 253 $response = $this->server->dispatch( $request ); 254 $this->assertEquals( 200, $response->get_status() ); 255 256 $meta = get_post_meta( $this->post_id, 'test_single', true );258 $request = new WP_REST_Request( 'POST', sprintf( '/wp/v2/posts/%d', self::$post_id ) ); 259 $request->set_body_params( $data ); 260 261 $response = $this->server->dispatch( $request ); 262 $this->assertEquals( 200, $response->get_status() ); 263 264 $meta = get_post_meta( self::$post_id, 'test_single', true ); 257 265 $this->assertNotEmpty( $meta ); 258 266 $this->assertEquals( 'test_value', $meta ); … … 276 284 wp_set_current_user( 0 ); 277 285 278 $request = new WP_REST_Request( 'POST', sprintf( '/wp/v2/posts/%d', $this->post_id ) );286 $request = new WP_REST_Request( 'POST', sprintf( '/wp/v2/posts/%d', self::$post_id ) ); 279 287 $request->set_body_params( $data ); 280 288 … … 283 291 284 292 // Check that the value wasn't actually updated. 285 $this->assertEmpty( get_post_meta( $this->post_id, 'test_single', false ) );293 $this->assertEmpty( get_post_meta( self::$post_id, 'test_single', false ) ); 286 294 } 287 295 … … 298 306 $this->grant_write_permission(); 299 307 300 $request = new WP_REST_Request( 'POST', sprintf( '/wp/v2/posts/%d', $this->post_id ) );308 $request = new WP_REST_Request( 'POST', sprintf( '/wp/v2/posts/%d', self::$post_id ) ); 301 309 $request->set_body_params( $data ); 302 310 303 311 $response = $this->server->dispatch( $request ); 304 312 $this->assertErrorResponse( 'rest_cannot_update', $response, 403 ); 305 $this->assertEmpty( get_post_meta( $this->post_id, 'test_bad_auth', false ) );313 $this->assertEmpty( get_post_meta( self::$post_id, 'test_bad_auth', false ) ); 306 314 } 307 315 … … 318 326 $this->grant_write_permission(); 319 327 320 $request = new WP_REST_Request( 'POST', sprintf( '/wp/v2/posts/%d', $this->post_id ) );328 $request = new WP_REST_Request( 'POST', sprintf( '/wp/v2/posts/%d', self::$post_id ) ); 321 329 $request->set_body_params( $data ); 322 330 … … 336 344 public function test_set_value_multiple() { 337 345 // Ensure no data exists currently. 338 $values = get_post_meta( $this->post_id, 'test_multi', false );346 $values = get_post_meta( self::$post_id, 'test_multi', false ); 339 347 $this->assertEmpty( $values ); 340 348 … … 346 354 ), 347 355 ); 348 $request = new WP_REST_Request( 'POST', sprintf( '/wp/v2/posts/%d', $this->post_id ) );349 $request->set_body_params( $data ); 350 351 $response = $this->server->dispatch( $request ); 352 $this->assertEquals( 200, $response->get_status() ); 353 354 $meta = get_post_meta( $this->post_id, 'test_multi', false );356 $request = new WP_REST_Request( 'POST', sprintf( '/wp/v2/posts/%d', self::$post_id ) ); 357 $request->set_body_params( $data ); 358 359 $response = $this->server->dispatch( $request ); 360 $this->assertEquals( 200, $response->get_status() ); 361 362 $meta = get_post_meta( self::$post_id, 'test_multi', false ); 355 363 $this->assertNotEmpty( $meta ); 356 364 $this->assertCount( 1, $meta ); … … 368 376 $this->assertEquals( 200, $response->get_status() ); 369 377 370 $meta = get_post_meta( $this->post_id, 'test_multi', false );378 $meta = get_post_meta( self::$post_id, 'test_multi', false ); 371 379 $this->assertNotEmpty( $meta ); 372 380 $this->assertCount( 2, $meta ); … … 379 387 */ 380 388 public function test_set_value_remove_one() { 381 add_post_meta( $this->post_id, 'test_multi', 'c' );382 add_post_meta( $this->post_id, 'test_multi', 'n' );383 add_post_meta( $this->post_id, 'test_multi', 'n' );389 add_post_meta( self::$post_id, 'test_multi', 'c' ); 390 add_post_meta( self::$post_id, 'test_multi', 'n' ); 391 add_post_meta( self::$post_id, 'test_multi', 'n' ); 384 392 385 393 $this->grant_write_permission(); … … 390 398 ), 391 399 ); 392 $request = new WP_REST_Request( 'POST', sprintf( '/wp/v2/posts/%d', $this->post_id ) );393 $request->set_body_params( $data ); 394 395 $response = $this->server->dispatch( $request ); 396 $this->assertEquals( 200, $response->get_status() ); 397 398 $meta = get_post_meta( $this->post_id, 'test_multi', false );400 $request = new WP_REST_Request( 'POST', sprintf( '/wp/v2/posts/%d', self::$post_id ) ); 401 $request->set_body_params( $data ); 402 403 $response = $this->server->dispatch( $request ); 404 $this->assertEquals( 200, $response->get_status() ); 405 406 $meta = get_post_meta( self::$post_id, 'test_multi', false ); 399 407 $this->assertNotEmpty( $meta ); 400 408 $this->assertCount( 2, $meta ); … … 408 416 public function test_set_value_multiple_unauthenticated() { 409 417 // Ensure no data exists currently. 410 $values = get_post_meta( $this->post_id, 'test_multi', false );418 $values = get_post_meta( self::$post_id, 'test_multi', false ); 411 419 $this->assertEmpty( $values ); 412 420 … … 418 426 ), 419 427 ); 420 $request = new WP_REST_Request( 'POST', sprintf( '/wp/v2/posts/%d', $this->post_id ) );428 $request = new WP_REST_Request( 'POST', sprintf( '/wp/v2/posts/%d', self::$post_id ) ); 421 429 $request->set_body_params( $data ); 422 430 … … 424 432 $this->assertErrorResponse( 'rest_cannot_edit', $response, 401 ); 425 433 426 $meta = get_post_meta( $this->post_id, 'test_multi', false );434 $meta = get_post_meta( self::$post_id, 'test_multi', false ); 427 435 $this->assertEmpty( $meta ); 428 436 } … … 440 448 $this->grant_write_permission(); 441 449 442 $request = new WP_REST_Request( 'POST', sprintf( '/wp/v2/posts/%d', $this->post_id ) );450 $request = new WP_REST_Request( 'POST', sprintf( '/wp/v2/posts/%d', self::$post_id ) ); 443 451 $request->set_body_params( $data ); 444 452 445 453 $response = $this->server->dispatch( $request ); 446 454 $this->assertErrorResponse( 'rest_cannot_update', $response, 403 ); 447 $this->assertEmpty( get_post_meta( $this->post_id, 'test_bad_auth_multi', false ) );455 $this->assertEmpty( get_post_meta( self::$post_id, 'test_bad_auth_multi', false ) ); 448 456 } 449 457 450 458 public function test_add_multi_value_db_error() { 451 459 // Ensure no data exists currently. 452 $values = get_post_meta( $this->post_id, 'test_multi', false );460 $values = get_post_meta( self::$post_id, 'test_multi', false ); 453 461 $this->assertEmpty( $values ); 454 462 … … 460 468 ), 461 469 ); 462 $request = new WP_REST_Request( 'POST', sprintf( '/wp/v2/posts/%d', $this->post_id ) );470 $request = new WP_REST_Request( 'POST', sprintf( '/wp/v2/posts/%d', self::$post_id ) ); 463 471 $request->set_body_params( $data ); 464 472 … … 479 487 480 488 public function test_remove_multi_value_db_error() { 481 add_post_meta( $this->post_id, 'test_multi', 'val1' );482 $values = get_post_meta( $this->post_id, 'test_multi', false );489 add_post_meta( self::$post_id, 'test_multi', 'val1' ); 490 $values = get_post_meta( self::$post_id, 'test_multi', false ); 483 491 $this->assertEquals( array( 'val1' ), $values ); 484 492 … … 490 498 ), 491 499 ); 492 $request = new WP_REST_Request( 'POST', sprintf( '/wp/v2/posts/%d', $this->post_id ) );500 $request = new WP_REST_Request( 'POST', sprintf( '/wp/v2/posts/%d', self::$post_id ) ); 493 501 $request->set_body_params( $data ); 494 502 … … 509 517 510 518 public function test_delete_value() { 511 add_post_meta( $this->post_id, 'test_single', 'val1' );512 $current = get_post_meta( $this->post_id, 'test_single', true );519 add_post_meta( self::$post_id, 'test_single', 'val1' ); 520 $current = get_post_meta( self::$post_id, 'test_single', true ); 513 521 $this->assertEquals( 'val1', $current ); 514 522 … … 520 528 ), 521 529 ); 522 $request = new WP_REST_Request( 'POST', sprintf( '/wp/v2/posts/%d', $this->post_id ) );523 $request->set_body_params( $data ); 524 525 $response = $this->server->dispatch( $request ); 526 $this->assertEquals( 200, $response->get_status() ); 527 528 $meta = get_post_meta( $this->post_id, 'test_single', false );530 $request = new WP_REST_Request( 'POST', sprintf( '/wp/v2/posts/%d', self::$post_id ) ); 531 $request->set_body_params( $data ); 532 533 $response = $this->server->dispatch( $request ); 534 $this->assertEquals( 200, $response->get_status() ); 535 536 $meta = get_post_meta( self::$post_id, 'test_single', false ); 529 537 $this->assertEmpty( $meta ); 530 538 } … … 534 542 */ 535 543 public function test_delete_value_blocked() { 536 add_post_meta( $this->post_id, 'test_bad_auth', 'val1' );537 $current = get_post_meta( $this->post_id, 'test_bad_auth', true );544 add_post_meta( self::$post_id, 'test_bad_auth', 'val1' ); 545 $current = get_post_meta( self::$post_id, 'test_bad_auth', true ); 538 546 $this->assertEquals( 'val1', $current ); 539 547 … … 545 553 ), 546 554 ); 547 $request = new WP_REST_Request( 'POST', sprintf( '/wp/v2/posts/%d', $this->post_id ) );555 $request = new WP_REST_Request( 'POST', sprintf( '/wp/v2/posts/%d', self::$post_id ) ); 548 556 $request->set_body_params( $data ); 549 557 … … 551 559 $this->assertErrorResponse( 'rest_cannot_delete', $response, 403 ); 552 560 553 $meta = get_post_meta( $this->post_id, 'test_bad_auth', true );561 $meta = get_post_meta( self::$post_id, 'test_bad_auth', true ); 554 562 $this->assertEquals( 'val1', $meta ); 555 563 } … … 559 567 */ 560 568 public function test_delete_value_db_error() { 561 add_post_meta( $this->post_id, 'test_single', 'val1' );562 $current = get_post_meta( $this->post_id, 'test_single', true );569 add_post_meta( self::$post_id, 'test_single', 'val1' ); 570 $current = get_post_meta( self::$post_id, 'test_single', true ); 563 571 $this->assertEquals( 'val1', $current ); 564 572 … … 570 578 ), 571 579 ); 572 $request = new WP_REST_Request( 'POST', sprintf( '/wp/v2/posts/%d', $this->post_id ) );580 $request = new WP_REST_Request( 'POST', sprintf( '/wp/v2/posts/%d', self::$post_id ) ); 573 581 $request->set_body_params( $data ); 574 582 /** … … 588 596 589 597 public function test_get_schema() { 590 $request = new WP_REST_Request( 'OPTIONS', sprintf( '/wp/v2/posts/%d', $this->post_id ) );598 $request = new WP_REST_Request( 'OPTIONS', sprintf( '/wp/v2/posts/%d', self::$post_id ) ); 591 599 $response = $this->server->dispatch( $request ); 592 600
Note: See TracChangeset
for help on using the changeset viewer.