- Timestamp:
- 11/30/2017 11:09:33 PM (8 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/tests/phpunit/tests/rest-api/rest-post-meta-fields.php
r42228 r42343 16 16 public static function wpSetUpBeforeClass( $factory ) { 17 17 self::$wp_meta_keys_saved = $GLOBALS['wp_meta_keys']; 18 self::$post_id = $factory->post->create();18 self::$post_id = $factory->post->create(); 19 19 } 20 20 … … 27 27 parent::setUp(); 28 28 29 register_meta( 'post', 'test_single', array( 30 'show_in_rest' => true, 31 'single' => true, 32 'type' => 'string', 33 )); 34 register_meta( 'post', 'test_multi', array( 35 'show_in_rest' => true, 36 'single' => false, 37 'type' => 'string', 38 )); 39 register_meta( 'post', 'test_bad_auth', array( 40 'show_in_rest' => true, 41 'single' => true, 42 'auth_callback' => '__return_false', 43 'type' => 'string', 44 )); 45 register_meta( 'post', 'test_bad_auth_multi', array( 46 'show_in_rest' => true, 47 'single' => false, 48 'auth_callback' => '__return_false', 49 'type' => 'string', 50 )); 29 register_meta( 30 'post', 'test_single', array( 31 'show_in_rest' => true, 32 'single' => true, 33 'type' => 'string', 34 ) 35 ); 36 register_meta( 37 'post', 'test_multi', array( 38 'show_in_rest' => true, 39 'single' => false, 40 'type' => 'string', 41 ) 42 ); 43 register_meta( 44 'post', 'test_bad_auth', array( 45 'show_in_rest' => true, 46 'single' => true, 47 'auth_callback' => '__return_false', 48 'type' => 'string', 49 ) 50 ); 51 register_meta( 52 'post', 'test_bad_auth_multi', array( 53 'show_in_rest' => true, 54 'single' => false, 55 'auth_callback' => '__return_false', 56 'type' => 'string', 57 ) 58 ); 51 59 register_meta( 'post', 'test_no_rest', array() ); 52 register_meta( 'post', 'test_rest_disabled', array( 53 'show_in_rest' => false, 54 'type' => 'string', 55 )); 56 register_meta( 'post', 'test_custom_schema', array( 57 'single' => true, 58 'type' => 'integer', 59 'show_in_rest' => array( 60 'schema' => array( 61 'type' => 'number', 60 register_meta( 61 'post', 'test_rest_disabled', array( 62 'show_in_rest' => false, 63 'type' => 'string', 64 ) 65 ); 66 register_meta( 67 'post', 'test_custom_schema', array( 68 'single' => true, 69 'type' => 'integer', 70 'show_in_rest' => array( 71 'schema' => array( 72 'type' => 'number', 73 ), 62 74 ), 63 ), 64 )); 65 register_meta( 'post', 'test_custom_schema_multi', array( 66 'single' => false, 67 'type' => 'integer', 68 'show_in_rest' => array( 69 'schema' => array( 70 'type' => 'number', 75 ) 76 ); 77 register_meta( 78 'post', 'test_custom_schema_multi', array( 79 'single' => false, 80 'type' => 'integer', 81 'show_in_rest' => array( 82 'schema' => array( 83 'type' => 'number', 84 ), 71 85 ), 72 ), 73 )); 74 register_meta( 'post', 'test_invalid_type', array( 75 'single' => true, 76 'type' => 'lalala', 77 'show_in_rest' => true, 78 )); 79 register_meta( 'post', 'test_no_type', array( 80 'single' => true, 81 'type' => null, 82 'show_in_rest' => true, 83 )); 84 85 register_meta( 'post', 'test_custom_name', array( 86 'single' => true, 87 'type' => 'string', 88 'show_in_rest' => array( 89 'name' => 'new_name', 90 ), 91 )); 92 93 register_meta( 'post', 'test_custom_name_multi', array( 94 'single' => false, 95 'type' => 'string', 96 'show_in_rest' => array( 97 'name' => 'new_name_multi', 98 ), 99 )); 86 ) 87 ); 88 register_meta( 89 'post', 'test_invalid_type', array( 90 'single' => true, 91 'type' => 'lalala', 92 'show_in_rest' => true, 93 ) 94 ); 95 register_meta( 96 'post', 'test_no_type', array( 97 'single' => true, 98 'type' => null, 99 'show_in_rest' => true, 100 ) 101 ); 102 103 register_meta( 104 'post', 'test_custom_name', array( 105 'single' => true, 106 'type' => 'string', 107 'show_in_rest' => array( 108 'name' => 'new_name', 109 ), 110 ) 111 ); 112 113 register_meta( 114 'post', 'test_custom_name_multi', array( 115 'single' => false, 116 'type' => 'string', 117 'show_in_rest' => array( 118 'name' => 'new_name_multi', 119 ), 120 ) 121 ); 100 122 101 123 /** @var WP_REST_Server $wp_rest_server */ … … 107 129 protected function grant_write_permission() { 108 130 // Ensure we have write permission. 109 $user = $this->factory->user->create( array( 110 'role' => 'editor', 111 )); 131 $user = $this->factory->user->create( 132 array( 133 'role' => 'editor', 134 ) 135 ); 112 136 wp_set_current_user( $user ); 113 137 } … … 116 140 add_post_meta( self::$post_id, 'test_single', 'testvalue' ); 117 141 118 $request = new WP_REST_Request( 'GET', sprintf( '/wp/v2/posts/%d', self::$post_id ) );142 $request = new WP_REST_Request( 'GET', sprintf( '/wp/v2/posts/%d', self::$post_id ) ); 119 143 $response = $this->server->dispatch( $request ); 120 144 … … 202 226 203 227 public function test_get_value_types() { 204 register_meta( 'post', 'test_string', array( 205 'show_in_rest' => true, 206 'single' => true, 207 'type' => 'string', 208 )); 209 register_meta( 'post', 'test_number', array( 210 'show_in_rest' => true, 211 'single' => true, 212 'type' => 'number', 213 )); 214 register_meta( 'post', 'test_bool', array( 215 'show_in_rest' => true, 216 'single' => true, 217 'type' => 'boolean', 218 )); 228 register_meta( 229 'post', 'test_string', array( 230 'show_in_rest' => true, 231 'single' => true, 232 'type' => 'string', 233 ) 234 ); 235 register_meta( 236 'post', 'test_number', array( 237 'show_in_rest' => true, 238 'single' => true, 239 'type' => 'number', 240 ) 241 ); 242 register_meta( 243 'post', 'test_bool', array( 244 'show_in_rest' => true, 245 'single' => true, 246 'type' => 'boolean', 247 ) 248 ); 219 249 220 250 /** @var WP_REST_Server $wp_rest_server */ … … 227 257 add_post_meta( self::$post_id, 'test_bool', 1 ); 228 258 229 $request = new WP_REST_Request( 'GET', sprintf( '/wp/v2/posts/%d', self::$post_id ) );259 $request = new WP_REST_Request( 'GET', sprintf( '/wp/v2/posts/%d', self::$post_id ) ); 230 260 $response = $this->server->dispatch( $request ); 231 261 $this->assertEquals( 200, $response->get_status() ); … … 250 280 add_post_meta( self::$post_id, 'test_custom_name', 'janet' ); 251 281 252 $request = new WP_REST_Request( 'GET', sprintf( '/wp/v2/posts/%d', self::$post_id ) );282 $request = new WP_REST_Request( 'GET', sprintf( '/wp/v2/posts/%d', self::$post_id ) ); 253 283 $response = $this->server->dispatch( $request ); 254 284 … … 273 303 $this->grant_write_permission(); 274 304 275 $data = array(305 $data = array( 276 306 'meta' => array( 277 307 'test_single' => 'test_value', … … 305 335 $this->grant_write_permission(); 306 336 307 $data = array(337 $data = array( 308 338 'meta' => array( 309 339 'test_single' => 'test_value', … … 404 434 $this->grant_write_permission(); 405 435 406 $data = array(436 $data = array( 407 437 'meta' => array( 408 438 'test_invalid_type' => 'test_value', … … 423 453 $this->grant_write_permission(); 424 454 425 $data = array(455 $data = array( 426 456 'meta' => array( 427 457 'test_multi' => array( 'val1' ), … … 467 497 $this->grant_write_permission(); 468 498 469 $data = array(499 $data = array( 470 500 'meta' => array( 471 501 'test_multi' => array( 'c', 'n' ), … … 495 525 wp_set_current_user( 0 ); 496 526 497 $data = array(527 $data = array( 498 528 'meta' => array( 499 529 'test_multi' => array( 'val1' ), … … 511 541 512 542 public function test_set_value_invalid_value() { 513 register_meta( 'post', 'my_meta_key', array( 514 'show_in_rest' => true, 515 'single' => true, 516 'type' => 'string', 517 )); 518 519 $this->grant_write_permission(); 520 521 $data = array( 543 register_meta( 544 'post', 'my_meta_key', array( 545 'show_in_rest' => true, 546 'single' => true, 547 'type' => 'string', 548 ) 549 ); 550 551 $this->grant_write_permission(); 552 553 $data = array( 522 554 'meta' => array( 523 555 'my_meta_key' => array( 'c', 'n' ), … … 532 564 533 565 public function test_set_value_invalid_value_multiple() { 534 register_meta( 'post', 'my_meta_key', array( 535 'show_in_rest' => true, 536 'single' => false, 537 'type' => 'string', 538 )); 539 540 $this->grant_write_permission(); 541 542 $data = array( 566 register_meta( 567 'post', 'my_meta_key', array( 568 'show_in_rest' => true, 569 'single' => false, 570 'type' => 'string', 571 ) 572 ); 573 574 $this->grant_write_permission(); 575 576 $data = array( 543 577 'meta' => array( 544 578 'my_meta_key' => array( array( 'a' ) ), … … 553 587 554 588 public function test_set_value_sanitized() { 555 register_meta( 'post', 'my_meta_key', array( 556 'show_in_rest' => true, 557 'single' => true, 558 'type' => 'integer', 559 )); 560 561 $this->grant_write_permission(); 562 563 $data = array( 589 register_meta( 590 'post', 'my_meta_key', array( 591 'show_in_rest' => true, 592 'single' => true, 593 'type' => 'integer', 594 ) 595 ); 596 597 $this->grant_write_permission(); 598 599 $data = array( 564 600 'meta' => array( 565 601 'my_meta_key' => '1', // Set to a string. … … 570 606 571 607 $response = $this->server->dispatch( $request ); 572 $data = $response->get_data();608 $data = $response->get_data(); 573 609 $this->assertEquals( 1, $data['meta']['my_meta_key'] ); 574 610 } 575 611 576 612 public function test_set_value_csv() { 577 register_meta( 'post', 'my_meta_key', array( 578 'show_in_rest' => true, 579 'single' => false, 580 'type' => 'integer', 581 )); 582 583 $this->grant_write_permission(); 584 585 $data = array( 613 register_meta( 614 'post', 'my_meta_key', array( 615 'show_in_rest' => true, 616 'single' => false, 617 'type' => 'integer', 618 ) 619 ); 620 621 $this->grant_write_permission(); 622 623 $data = array( 586 624 'meta' => array( 587 625 'my_meta_key' => '1,2,3', // Set to a string. … … 592 630 593 631 $response = $this->server->dispatch( $request ); 594 $data = $response->get_data();632 $data = $response->get_data(); 595 633 $this->assertEquals( array( 1, 2, 3 ), $data['meta']['my_meta_key'] ); 596 634 } … … 623 661 $this->grant_write_permission(); 624 662 625 $data = array(663 $data = array( 626 664 'meta' => array( 627 665 'test_multi' => array( 'val1' ), … … 656 694 $this->grant_write_permission(); 657 695 658 $data = array(696 $data = array( 659 697 'meta' => array( 660 698 'test_custom_schema' => 3, … … 685 723 $this->grant_write_permission(); 686 724 687 $data = array(725 $data = array( 688 726 'meta' => array( 689 727 'test_custom_schema_multi' => array( 2 ), … … 729 767 $this->grant_write_permission(); 730 768 731 $data = array(769 $data = array( 732 770 'meta' => array( 733 771 'new_name' => 'janet', … … 758 796 $this->grant_write_permission(); 759 797 760 $data = array(798 $data = array( 761 799 'meta' => array( 762 800 'new_name_multi' => array( 'janet' ), … … 858 896 $this->grant_write_permission(); 859 897 860 $data = array(898 $data = array( 861 899 'meta' => array( 862 900 'test_multi' => array(), … … 889 927 $this->grant_write_permission(); 890 928 891 $data = array(929 $data = array( 892 930 'meta' => array( 893 931 'test_single' => null, … … 914 952 $this->grant_write_permission(); 915 953 916 $data = array(954 $data = array( 917 955 'meta' => array( 918 956 'test_bad_auth' => null, … … 939 977 $this->grant_write_permission(); 940 978 941 $data = array(979 $data = array( 942 980 'meta' => array( 943 981 'test_single' => null, … … 968 1006 $this->grant_write_permission(); 969 1007 970 $data = array(1008 $data = array( 971 1009 'meta' => array( 972 1010 'new_name' => null, … … 984 1022 985 1023 public function test_get_schema() { 986 $request = new WP_REST_Request( 'OPTIONS', sprintf( '/wp/v2/posts/%d', self::$post_id ) );987 $response = $this->server->dispatch( $request ); 988 989 $data = $response->get_data();1024 $request = new WP_REST_Request( 'OPTIONS', sprintf( '/wp/v2/posts/%d', self::$post_id ) ); 1025 $response = $this->server->dispatch( $request ); 1026 1027 $data = $response->get_data(); 990 1028 $schema = $data['schema']; 991 1029
Note: See TracChangeset
for help on using the changeset viewer.