- Timestamp:
- 11/14/2016 04:35:35 PM (8 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/tests/phpunit/tests/rest-api/rest-post-meta-fields.php
r38975 r39222 27 27 'show_in_rest' => true, 28 28 'single' => true, 29 'type' => 'string', 29 30 )); 30 31 register_meta( 'post', 'test_multi', array( 31 32 'show_in_rest' => true, 32 33 'single' => false, 34 'type' => 'string', 33 35 )); 34 36 register_meta( 'post', 'test_bad_auth', array( … … 36 38 'single' => true, 37 39 'auth_callback' => '__return_false', 40 'type' => 'string', 38 41 )); 39 42 register_meta( 'post', 'test_bad_auth_multi', array( … … 41 44 'single' => false, 42 45 'auth_callback' => '__return_false', 46 'type' => 'string', 43 47 )); 44 48 register_meta( 'post', 'test_no_rest', array() ); 45 49 register_meta( 'post', 'test_rest_disabled', array( 46 50 'show_in_rest' => false, 51 'type' => 'string', 47 52 )); 48 53 register_meta( 'post', 'test_custom_schema', array( … … 55 60 ), 56 61 )); 62 register_meta( 'post', 'test_custom_schema_multi', array( 63 'single' => false, 64 'type' => 'integer', 65 'show_in_rest' => array( 66 'schema' => array( 67 'type' => 'number', 68 ), 69 ), 70 )); 57 71 register_meta( 'post', 'test_invalid_type', array( 58 72 'single' => true, 59 'type' => false, 73 'type' => 'lalala', 74 'show_in_rest' => true, 75 )); 76 register_meta( 'post', 'test_no_type', array( 77 'single' => true, 78 'type' => null, 60 79 'show_in_rest' => true, 61 80 )); … … 342 361 } 343 362 363 public function test_set_value_invalid_type() { 364 $values = get_post_meta( self::$post_id, 'test_invalid_type', false ); 365 $this->assertEmpty( $values ); 366 367 $this->grant_write_permission(); 368 369 $data = array( 370 'meta' => array( 371 'test_invalid_type' => 'test_value', 372 ), 373 ); 374 $request = new WP_REST_Request( 'POST', sprintf( '/wp/v2/posts/%d', self::$post_id ) ); 375 $request->set_body_params( $data ); 376 377 $response = $this->server->dispatch( $request ); 378 $this->assertEmpty( get_post_meta( self::$post_id, 'test_invalid_type', false ) ); 379 } 380 344 381 public function test_set_value_multiple() { 345 382 // Ensure no data exists currently. … … 434 471 $meta = get_post_meta( self::$post_id, 'test_multi', false ); 435 472 $this->assertEmpty( $meta ); 473 } 474 475 public function test_set_value_invalid_value() { 476 register_meta( 'post', 'my_meta_key', array( 477 'show_in_rest' => true, 478 'single' => true, 479 'type' => 'string', 480 )); 481 482 $this->grant_write_permission(); 483 484 $data = array( 485 'meta' => array( 486 'my_meta_key' => array( 'c', 'n' ), 487 ), 488 ); 489 $request = new WP_REST_Request( 'POST', sprintf( '/wp/v2/posts/%d', self::$post_id ) ); 490 $request->set_body_params( $data ); 491 492 $response = $this->server->dispatch( $request ); 493 $this->assertErrorResponse( 'rest_invalid_param', $response, 400 ); 494 } 495 496 public function test_set_value_invalid_value_multiple() { 497 register_meta( 'post', 'my_meta_key', array( 498 'show_in_rest' => true, 499 'single' => false, 500 'type' => 'string', 501 )); 502 503 $this->grant_write_permission(); 504 505 $data = array( 506 'meta' => array( 507 'my_meta_key' => array( array( 'a' ) ), 508 ), 509 ); 510 $request = new WP_REST_Request( 'POST', sprintf( '/wp/v2/posts/%d', self::$post_id ) ); 511 $request->set_body_params( $data ); 512 513 $response = $this->server->dispatch( $request ); 514 $this->assertErrorResponse( 'rest_invalid_param', $response, 400 ); 515 } 516 517 public function test_set_value_sanitized() { 518 register_meta( 'post', 'my_meta_key', array( 519 'show_in_rest' => true, 520 'single' => true, 521 'type' => 'integer', 522 )); 523 524 $this->grant_write_permission(); 525 526 $data = array( 527 'meta' => array( 528 'my_meta_key' => '1', // Set to a string. 529 ), 530 ); 531 $request = new WP_REST_Request( 'POST', sprintf( '/wp/v2/posts/%d', self::$post_id ) ); 532 $request->set_body_params( $data ); 533 534 $response = $this->server->dispatch( $request ); 535 $data = $response->get_data(); 536 $this->assertEquals( 1, $data['meta']['my_meta_key'] ); 537 } 538 539 public function test_set_value_csv() { 540 register_meta( 'post', 'my_meta_key', array( 541 'show_in_rest' => true, 542 'single' => false, 543 'type' => 'integer', 544 )); 545 546 $this->grant_write_permission(); 547 548 $data = array( 549 'meta' => array( 550 'my_meta_key' => '1,2,3', // Set to a string. 551 ), 552 ); 553 $request = new WP_REST_Request( 'POST', sprintf( '/wp/v2/posts/%d', self::$post_id ) ); 554 $request->set_body_params( $data ); 555 556 $response = $this->server->dispatch( $request ); 557 $data = $response->get_data(); 558 $this->assertEquals( array( 1, 2, 3 ), $data['meta']['my_meta_key'] ); 436 559 } 437 560 … … 486 609 } 487 610 611 /** 612 * @depends test_get_value 613 */ 614 public function test_set_value_single_custom_schema() { 615 // Ensure no data exists currently. 616 $values = get_post_meta( self::$post_id, 'test_custom_schema', false ); 617 $this->assertEmpty( $values ); 618 619 $this->grant_write_permission(); 620 621 $data = array( 622 'meta' => array( 623 'test_custom_schema' => 3, 624 ), 625 ); 626 $request = new WP_REST_Request( 'POST', sprintf( '/wp/v2/posts/%d', self::$post_id ) ); 627 $request->set_body_params( $data ); 628 629 $response = $this->server->dispatch( $request ); 630 $this->assertEquals( 200, $response->get_status() ); 631 632 $meta = get_post_meta( self::$post_id, 'test_custom_schema', false ); 633 $this->assertNotEmpty( $meta ); 634 $this->assertCount( 1, $meta ); 635 $this->assertEquals( 3, $meta[0] ); 636 637 $data = $response->get_data(); 638 $meta = (array) $data['meta']; 639 $this->assertArrayHasKey( 'test_custom_schema', $meta ); 640 $this->assertEquals( 3, $meta['test_custom_schema'] ); 641 } 642 643 public function test_set_value_multiple_custom_schema() { 644 // Ensure no data exists currently. 645 $values = get_post_meta( self::$post_id, 'test_custom_schema_multi', false ); 646 $this->assertEmpty( $values ); 647 648 $this->grant_write_permission(); 649 650 $data = array( 651 'meta' => array( 652 'test_custom_schema_multi' => array( 2 ), 653 ), 654 ); 655 $request = new WP_REST_Request( 'POST', sprintf( '/wp/v2/posts/%d', self::$post_id ) ); 656 $request->set_body_params( $data ); 657 658 $response = $this->server->dispatch( $request ); 659 $this->assertEquals( 200, $response->get_status() ); 660 661 $meta = get_post_meta( self::$post_id, 'test_custom_schema_multi', false ); 662 $this->assertNotEmpty( $meta ); 663 $this->assertCount( 1, $meta ); 664 $this->assertEquals( 2, $meta[0] ); 665 666 // Add another value. 667 $data = array( 668 'meta' => array( 669 'test_custom_schema_multi' => array( 2, 8 ), 670 ), 671 ); 672 $request->set_body_params( $data ); 673 674 $response = $this->server->dispatch( $request ); 675 $this->assertEquals( 200, $response->get_status() ); 676 677 $meta = get_post_meta( self::$post_id, 'test_custom_schema_multi', false ); 678 $this->assertNotEmpty( $meta ); 679 $this->assertCount( 2, $meta ); 680 $this->assertContains( 2, $meta ); 681 $this->assertContains( 8, $meta ); 682 } 683 488 684 public function test_remove_multi_value_db_error() { 489 685 add_post_meta( self::$post_id, 'test_multi', 'val1' ); … … 516 712 } 517 713 714 518 715 public function test_delete_value() { 519 716 add_post_meta( self::$post_id, 'test_single', 'val1' ); … … 619 816 $this->assertArrayNotHasKey( 'test_rest_disabled', $meta_schema ); 620 817 $this->assertArrayNotHasKey( 'test_invalid_type', $meta_schema ); 818 $this->assertArrayNotHasKey( 'test_no_type', $meta_schema ); 621 819 } 622 820
Note: See TracChangeset
for help on using the changeset viewer.