Changeset 46657
- Timestamp:
- 11/05/2019 08:41:12 PM (5 years ago)
- Location:
- trunk/tests/phpunit/tests/rest-api
- Files:
-
- 7 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/tests/phpunit/tests/rest-api/rest-attachments-controller.php
r46586 r46657 1452 1452 1453 1453 public function test_search_item_by_filename() { 1454 $id 1454 $id1 = $this->factory->attachment->create_object( 1455 1455 $this->test_file, 1456 1456 0, -
trunk/tests/phpunit/tests/rest-api/rest-categories-controller.php
r46586 r46657 16 16 protected static $subscriber; 17 17 18 protected static $category_ids = array(); 19 protected static $total_categories = 30; 20 protected static $per_page = 50; 21 18 22 public static function wpSetUpBeforeClass( $factory ) { 19 23 self::$administrator = $factory->user->create( … … 32 36 ) 33 37 ); 38 39 // Set up categories for pagination tests. 40 for ( $i = 0; $i < self::$total_categories - 1; $i++ ) { 41 $category_ids[] = $factory->category->create( 42 array( 43 'name' => "Category {$i}", 44 ) 45 ); 46 } 34 47 } 35 48 … … 37 50 self::delete_user( self::$administrator ); 38 51 self::delete_user( self::$subscriber ); 52 53 // Remove categories for pagination tests. 54 foreach ( self::$category_ids as $category_id ) { 55 wp_delete_term( $category_id, 'category' ); 56 } 39 57 } 40 58 … … 137 155 138 156 public function test_get_items() { 139 $request = new WP_REST_Request( 'GET', '/wp/v2/categories' ); 157 $request = new WP_REST_Request( 'GET', '/wp/v2/categories' ); 158 $request->set_param( 'per_page', self::$per_page ); 140 159 $response = rest_get_server()->dispatch( $request ); 141 160 $this->check_get_taxonomy_terms_response( $response ); … … 144 163 public function test_get_items_invalid_permission_for_context() { 145 164 wp_set_current_user( 0 ); 165 146 166 $request = new WP_REST_Request( 'GET', '/wp/v2/categories' ); 147 167 $request->set_param( 'context', 'edit' ); … … 154 174 $category1 = $this->factory->category->create( array( 'name' => 'Season 5' ) ); 155 175 $category2 = $this->factory->category->create( array( 'name' => 'The Be Sharps' ) ); 176 177 $total_categories = self::$total_categories + 2; 178 156 179 wp_set_object_terms( $post_id, array( $category1, $category2 ), 'category' ); 157 $request = new WP_REST_Request( 'GET', '/wp/v2/categories' ); 180 181 $request = new WP_REST_Request( 'GET', '/wp/v2/categories' ); 182 $request->set_param( 'per_page', self::$per_page ); 158 183 $request->set_param( 'hide_empty', true ); 159 184 $response = rest_get_server()->dispatch( $request ); … … 167 192 $response = rest_get_server()->dispatch( $request ); 168 193 $data = $response->get_data(); 169 $this->assertEquals( 3, count( $data ) );194 $this->assertEquals( $total_categories, count( $data ) ); 170 195 } 171 196 … … 185 210 ) 186 211 ); 187 $request = new WP_REST_Request( 'GET', '/wp/v2/categories' ); 212 213 $request = new WP_REST_Request( 'GET', '/wp/v2/categories' ); 214 $request->set_param( 'per_page', self::$per_page ); 188 215 $request->set_param( 'parent', 0 ); 189 216 $response = rest_get_server()->dispatch( $request ); … … 215 242 ) 216 243 ); 217 $request = new WP_REST_Request( 'GET', '/wp/v2/categories' ); 244 245 $request = new WP_REST_Request( 'GET', '/wp/v2/categories' ); 246 $request->set_param( 'per_page', self::$per_page ); 218 247 $request->set_param( 'parent', '0' ); 219 248 $response = rest_get_server()->dispatch( $request ); … … 255 284 public function test_get_items_include_query() { 256 285 $id1 = $this->factory->category->create(); 257 $this->factory->category->create(); 258 $id3 = $this->factory->category->create(); 259 $request = new WP_REST_Request( 'GET', '/wp/v2/categories' ); 260 // Orderby=>asc 261 $request->set_param( 'include', array( $id3, $id1 ) ); 286 $id2 = $this->factory->category->create(); 287 288 $request = new WP_REST_Request( 'GET', '/wp/v2/categories' ); 289 290 // 'orderby' => 'asc'. 291 $request->set_param( 'include', array( $id2, $id1 ) ); 262 292 $response = rest_get_server()->dispatch( $request ); 263 293 $data = $response->get_data(); 264 294 $this->assertEquals( 2, count( $data ) ); 265 295 $this->assertEquals( $id1, $data[0]['id'] ); 266 // Orderby=>include 296 297 // 'orderby' => 'include'. 267 298 $request->set_param( 'orderby', 'include' ); 268 299 $response = rest_get_server()->dispatch( $request ); 269 300 $data = $response->get_data(); 270 301 $this->assertEquals( 2, count( $data ) ); 271 $this->assertEquals( $id 3, $data[0]['id'] );302 $this->assertEquals( $id2, $data[0]['id'] ); 272 303 } 273 304 274 305 public function test_get_items_exclude_query() { 275 $id1 = $this->factory->category->create(); 276 $id2 = $this->factory->category->create(); 277 $request = new WP_REST_Request( 'GET', '/wp/v2/categories' ); 278 $response = rest_get_server()->dispatch( $request ); 279 $data = $response->get_data(); 280 $this->assertTrue( in_array( $id1, wp_list_pluck( $data, 'id' ), true ) ); 281 $this->assertTrue( in_array( $id2, wp_list_pluck( $data, 'id' ), true ) ); 306 $id1 = $this->factory->category->create(); 307 $id2 = $this->factory->category->create(); 308 309 $request = new WP_REST_Request( 'GET', '/wp/v2/categories' ); 310 $request->set_param( 'per_page', self::$per_page ); 311 $response = rest_get_server()->dispatch( $request ); 312 $data = $response->get_data(); 313 $ids = wp_list_pluck( $data, 'id' ); 314 $this->assertTrue( in_array( $id1, $ids, true ) ); 315 $this->assertTrue( in_array( $id2, $ids, true ) ); 316 282 317 $request->set_param( 'exclude', array( $id2 ) ); 283 318 $response = rest_get_server()->dispatch( $request ); 284 319 $data = $response->get_data(); 285 $this->assertTrue( in_array( $id1, wp_list_pluck( $data, 'id' ), true ) ); 286 $this->assertFalse( in_array( $id2, wp_list_pluck( $data, 'id' ), true ) ); 320 $ids = wp_list_pluck( $data, 'id' ); 321 $this->assertTrue( in_array( $id1, $ids, true ) ); 322 $this->assertFalse( in_array( $id2, $ids, true ) ); 287 323 } 288 324 … … 290 326 $this->factory->category->create( array( 'name' => 'Apple' ) ); 291 327 $this->factory->category->create( array( 'name' => 'Banana' ) ); 328 292 329 /* 293 330 * Tests: … … 305 342 $this->assertEquals( 1, count( $data ) ); 306 343 $this->assertEquals( 'Uncategorized', $data[0]['name'] ); 344 307 345 $request = new WP_REST_Request( 'GET', '/wp/v2/categories' ); 308 346 $request->set_param( 'orderby', 'name' ); … … 320 358 $this->factory->category->create( array( 'name' => 'Apple' ) ); 321 359 $this->factory->category->create( array( 'name' => 'Banana' ) ); 322 // defaults to orderby=name, order=asc 360 361 // Defaults to 'orderby' => 'name', 'order' => 'asc'. 323 362 $request = new WP_REST_Request( 'GET', '/wp/v2/categories' ); 324 363 $response = rest_get_server()->dispatch( $request ); … … 328 367 $this->assertEquals( 'Banana', $data[1]['name'] ); 329 368 $this->assertEquals( 'Cantaloupe', $data[2]['name'] ); 330 $this->assertEquals( 'Uncategorized', $data[3]['name'] ); 331 // orderby=id, with default order=asc369 370 // 'orderby' => 'id', with default 'order' => 'asc'. 332 371 $request = new WP_REST_Request( 'GET', '/wp/v2/categories' ); 333 372 $request->set_param( 'orderby', 'id' ); … … 335 374 $this->assertEquals( 200, $response->get_status() ); 336 375 $data = $response->get_data(); 337 $this->assertEquals( ' Uncategorized', $data[0]['name'] );338 $this->assertEquals( 'Ca ntaloupe', $data[1]['name'] );339 $this->assertEquals( ' Apple', $data[2]['name'] );340 $this->assertEquals( 'Banana', $data[3]['name'] ); 341 // orderby=id, order=desc376 $this->assertEquals( 'Category 0', $data[1]['name'] ); 377 $this->assertEquals( 'Category 1', $data[2]['name'] ); 378 $this->assertEquals( 'Category 2', $data[3]['name'] ); 379 380 // 'orderby' => 'id', 'order' => 'desc'. 342 381 $request = new WP_REST_Request( 'GET', '/wp/v2/categories' ); 343 382 $request->set_param( 'orderby', 'id' ); … … 487 526 $this->factory->category->create( array( 'name' => 'Apple' ) ); 488 527 $this->factory->category->create( array( 'name' => 'Banana' ) ); 528 489 529 /* 490 530 * Tests: … … 498 538 $this->assertEquals( 1, count( $data ) ); 499 539 $this->assertEquals( 'Apple', $data[0]['name'] ); 540 500 541 $request = new WP_REST_Request( 'GET', '/wp/v2/categories' ); 501 542 $request->set_param( 'search', 'Garbage' ); … … 509 550 $this->factory->category->create( array( 'name' => 'Apple' ) ); 510 551 $this->factory->category->create( array( 'name' => 'Banana' ) ); 552 511 553 $request = new WP_REST_Request( 'GET', '/wp/v2/categories' ); 512 554 $request->set_param( 'slug', 'apple' ); … … 526 568 ) 527 569 ); 570 528 571 $request = new WP_REST_Request( 'GET', '/wp/v2/categories' ); 529 572 $request->set_param( 'parent', $category1 ); … … 535 578 536 579 public function test_get_terms_invalid_parent_arg() { 537 $category1 = $this->factory->category->create( array( 'name' => 'Parent' ) );538 $this->factory->category->create(539 array(540 'name' => 'Child',541 'parent' => $category1,542 )543 );544 580 $request = new WP_REST_Request( 'GET', '/wp/v2/categories' ); 545 581 $request->set_param( 'parent', 'invalid-parent' ); … … 575 611 576 612 public function test_get_terms_pagination_headers() { 577 // Start of the index + Uncategorized default term 578 for ( $i = 0; $i < 49; $i++ ) { 579 $this->factory->category->create( 580 array( 581 'name' => "Category {$i}", 582 ) 583 ); 584 } 613 $total_categories = self::$total_categories; 614 $total_pages = (int) ceil( $total_categories / 10 ); 615 616 // Start of the index + Uncategorized default term. 585 617 $request = new WP_REST_Request( 'GET', '/wp/v2/categories' ); 586 618 $response = rest_get_server()->dispatch( $request ); 587 619 $headers = $response->get_headers(); 588 $this->assertEquals( 50, $headers['X-WP-Total'] );589 $this->assertEquals( 5, $headers['X-WP-TotalPages'] );620 $this->assertEquals( $total_categories, $headers['X-WP-Total'] ); 621 $this->assertEquals( $total_pages, $headers['X-WP-TotalPages'] ); 590 622 $this->assertCount( 10, $response->get_data() ); 591 623 $next_link = add_query_arg( … … 597 629 $this->assertFalse( stripos( $headers['Link'], 'rel="prev"' ) ); 598 630 $this->assertContains( '<' . $next_link . '>; rel="next"', $headers['Link'] ); 599 // 3rd page 600 $this->factory->category->create( 601 array( 602 'name' => 'Category 51', 603 ) 604 ); 631 632 // 3rd page. 633 $this->factory->category->create(); 634 $total_categories++; 635 $total_pages++; 605 636 $request = new WP_REST_Request( 'GET', '/wp/v2/categories' ); 606 637 $request->set_param( 'page', 3 ); 607 638 $response = rest_get_server()->dispatch( $request ); 608 639 $headers = $response->get_headers(); 609 $this->assertEquals( 51, $headers['X-WP-Total'] );610 $this->assertEquals( 6, $headers['X-WP-TotalPages'] );640 $this->assertEquals( $total_categories, $headers['X-WP-Total'] ); 641 $this->assertEquals( $total_pages, $headers['X-WP-TotalPages'] ); 611 642 $this->assertCount( 10, $response->get_data() ); 612 643 $prev_link = add_query_arg( … … 624 655 ); 625 656 $this->assertContains( '<' . $next_link . '>; rel="next"', $headers['Link'] ); 626 // Last page 627 $request = new WP_REST_Request( 'GET', '/wp/v2/categories' ); 628 $request->set_param( 'page', 6 ); 657 658 // Last page. 659 $request = new WP_REST_Request( 'GET', '/wp/v2/categories' ); 660 $request->set_param( 'page', $total_pages ); 629 661 $response = rest_get_server()->dispatch( $request ); 630 662 $headers = $response->get_headers(); 631 $this->assertEquals( 51, $headers['X-WP-Total'] );632 $this->assertEquals( 6, $headers['X-WP-TotalPages'] );663 $this->assertEquals( $total_categories, $headers['X-WP-Total'] ); 664 $this->assertEquals( $total_pages, $headers['X-WP-TotalPages'] ); 633 665 $this->assertCount( 1, $response->get_data() ); 634 666 $prev_link = add_query_arg( 635 667 array( 636 'page' => 5,668 'page' => $total_pages - 1, 637 669 ), 638 670 rest_url( 'wp/v2/categories' ) … … 640 672 $this->assertContains( '<' . $prev_link . '>; rel="prev"', $headers['Link'] ); 641 673 $this->assertFalse( stripos( $headers['Link'], 'rel="next"' ) ); 642 // Out of bounds 643 $request = new WP_REST_Request( 'GET', '/wp/v2/categories' ); 644 $request->set_param( 'page', 8 ); 674 675 // Out of bounds. 676 $request = new WP_REST_Request( 'GET', '/wp/v2/categories' ); 677 $request->set_param( 'page', 100 ); 645 678 $response = rest_get_server()->dispatch( $request ); 646 679 $headers = $response->get_headers(); 647 $this->assertEquals( 51, $headers['X-WP-Total'] );648 $this->assertEquals( 6, $headers['X-WP-TotalPages'] );680 $this->assertEquals( $total_categories, $headers['X-WP-Total'] ); 681 $this->assertEquals( $total_pages, $headers['X-WP-TotalPages'] ); 649 682 $this->assertCount( 0, $response->get_data() ); 650 683 $prev_link = add_query_arg( 651 684 array( 652 'page' => 6,685 'page' => $total_pages, 653 686 ), 654 687 rest_url( 'wp/v2/categories' ) … … 659 692 660 693 public function test_get_items_per_page_exceeds_number_of_items() { 661 // Start of the index + Uncategorized default term 662 for ( $i = 0; $i < 17; $i++ ) { 663 $this->factory->category->create( 664 array( 665 'name' => "Category {$i}", 666 ) 667 ); 668 } 694 // Start of the index + Uncategorized default term. 669 695 $request = new WP_REST_Request( 'GET', '/wp/v2/categories' ); 670 696 $request->set_param( 'page', 1 ); … … 672 698 $response = rest_get_server()->dispatch( $request ); 673 699 $headers = $response->get_headers(); 674 $this->assertEquals( 18, $headers['X-WP-Total'] );700 $this->assertEquals( self::$total_categories, $headers['X-WP-Total'] ); 675 701 $this->assertEquals( 1, $headers['X-WP-TotalPages'] ); 676 $this->assertCount( 18, $response->get_data() ); 702 $this->assertCount( self::$total_categories, $response->get_data() ); 703 677 704 $request = new WP_REST_Request( 'GET', '/wp/v2/categories' ); 678 705 $request->set_param( 'page', 2 ); … … 680 707 $response = rest_get_server()->dispatch( $request ); 681 708 $headers = $response->get_headers(); 682 $this->assertEquals( 18, $headers['X-WP-Total'] );709 $this->assertEquals( self::$total_categories, $headers['X-WP-Total'] ); 683 710 $this->assertEquals( 1, $headers['X-WP-TotalPages'] ); 684 711 $this->assertCount( 0, $response->get_data() ); … … 738 765 public function test_get_item_invalid_permission_for_context() { 739 766 wp_set_current_user( 0 ); 767 740 768 $request = new WP_REST_Request( 'GET', '/wp/v2/categories/1' ); 741 769 $request->set_param( 'context', 'edit' ); … … 760 788 public function test_get_item_incorrect_taxonomy() { 761 789 register_taxonomy( 'robin', 'post' ); 762 $term1 790 $term1 = $this->factory->term->create( 763 791 array( 764 792 'name' => 'Cape', … … 766 794 ) 767 795 ); 796 768 797 $request = new WP_REST_Request( 'GET', '/wp/v2/categories/' . $term1 ); 769 798 $response = rest_get_server()->dispatch( $request ); … … 773 802 public function test_create_item() { 774 803 wp_set_current_user( self::$administrator ); 804 775 805 $request = new WP_REST_Request( 'POST', '/wp/v2/categories' ); 776 806 $request->set_param( 'name', 'My Awesome Term' ); … … 792 822 public function test_create_item_term_already_exists() { 793 823 wp_set_current_user( self::$administrator ); 824 794 825 $existing_id = $this->factory->category->create( array( 'name' => 'Existing' ) ); 795 826 … … 808 839 public function test_create_item_invalid_taxonomy() { 809 840 wp_set_current_user( self::$administrator ); 841 810 842 $request = new WP_REST_Request( 'POST', '/wp/v2/invalid-taxonomy' ); 811 843 $request->set_param( 'name', 'Invalid Taxonomy' ); … … 816 848 public function test_create_item_incorrect_permissions() { 817 849 wp_set_current_user( self::$subscriber ); 850 818 851 $request = new WP_REST_Request( 'POST', '/wp/v2/categories' ); 819 852 $request->set_param( 'name', 'Incorrect permissions' ); … … 824 857 public function test_create_item_incorrect_permissions_contributor() { 825 858 wp_set_current_user( self::$contributor ); 859 826 860 $request = new WP_REST_Request( 'POST', '/wp/v2/categories' ); 827 861 $request->set_param( 'name', 'Incorrect permissions' ); … … 832 866 public function test_create_item_missing_arguments() { 833 867 wp_set_current_user( self::$administrator ); 868 834 869 $request = new WP_REST_Request( 'POST', '/wp/v2/categories' ); 835 870 $response = rest_get_server()->dispatch( $request ); … … 839 874 public function test_create_item_with_parent() { 840 875 wp_set_current_user( self::$administrator ); 841 $parent = wp_insert_term( 'test-category', 'category' ); 876 877 $parent = wp_insert_term( 'test-category', 'category' ); 878 842 879 $request = new WP_REST_Request( 'POST', '/wp/v2/categories' ); 843 880 $request->set_param( 'name', 'My Awesome Term' ); … … 851 888 public function test_create_item_invalid_parent() { 852 889 wp_set_current_user( self::$administrator ); 890 853 891 $term = get_term_by( 'id', $this->factory->category->create(), 'category' ); 854 892 … … 862 900 public function test_create_item_with_no_parent() { 863 901 wp_set_current_user( self::$administrator ); 864 $parent = 0; 902 903 $parent = 0; 904 865 905 $request = new WP_REST_Request( 'POST', '/wp/v2/categories' ); 866 906 $request->set_param( 'name', 'My Awesome Term' ); … … 874 914 public function test_update_item() { 875 915 wp_set_current_user( self::$administrator ); 916 876 917 $orig_args = array( 877 918 'name' => 'Original Name', … … 879 920 'slug' => 'original-slug', 880 921 ); 881 $term = get_term_by( 'id', $this->factory->category->create( $orig_args ), 'category' ); 882 $request = new WP_REST_Request( 'POST', '/wp/v2/categories/' . $term->term_id ); 922 923 $term = get_term_by( 'id', $this->factory->category->create( $orig_args ), 'category' ); 924 925 $request = new WP_REST_Request( 'POST', '/wp/v2/categories/' . $term->term_id ); 883 926 $request->set_param( 'name', 'New Name' ); 884 927 $request->set_param( 'description', 'New Description' ); … … 905 948 public function test_update_item_invalid_taxonomy() { 906 949 wp_set_current_user( self::$administrator ); 950 907 951 $request = new WP_REST_Request( 'POST', '/wp/v2/invalid-taxonomy/' . REST_TESTS_IMPOSSIBLY_HIGH_NUMBER ); 908 952 $request->set_param( 'name', 'Invalid Taxonomy' ); … … 913 957 public function test_update_item_invalid_term() { 914 958 wp_set_current_user( self::$administrator ); 959 915 960 $request = new WP_REST_Request( 'POST', '/wp/v2/categories/' . REST_TESTS_IMPOSSIBLY_HIGH_NUMBER ); 916 961 $request->set_param( 'name', 'Invalid Term' ); … … 921 966 public function test_update_item_incorrect_permissions() { 922 967 wp_set_current_user( self::$subscriber ); 923 $term = get_term_by( 'id', $this->factory->category->create(), 'category' ); 968 969 $term = get_term_by( 'id', $this->factory->category->create(), 'category' ); 970 924 971 $request = new WP_REST_Request( 'POST', '/wp/v2/categories/' . $term->term_id ); 925 972 $request->set_param( 'name', 'Incorrect permissions' ); … … 930 977 public function test_update_item_parent() { 931 978 wp_set_current_user( self::$administrator ); 979 932 980 $parent = get_term_by( 'id', $this->factory->category->create(), 'category' ); 933 981 $term = get_term_by( 'id', $this->factory->category->create(), 'category' ); … … 971 1019 public function test_update_item_invalid_parent() { 972 1020 wp_set_current_user( self::$administrator ); 1021 973 1022 $term = get_term_by( 'id', $this->factory->category->create(), 'category' ); 974 1023 … … 981 1030 public function test_delete_item() { 982 1031 wp_set_current_user( self::$administrator ); 983 $term = get_term_by( 'id', $this->factory->category->create( array( 'name' => 'Deleted Category' ) ), 'category' ); 1032 1033 $term = get_term_by( 'id', $this->factory->category->create( array( 'name' => 'Deleted Category' ) ), 'category' ); 1034 984 1035 $request = new WP_REST_Request( 'DELETE', '/wp/v2/categories/' . $term->term_id ); 985 1036 $request->set_param( 'force', true ); … … 993 1044 public function test_delete_item_no_trash() { 994 1045 wp_set_current_user( self::$administrator ); 1046 995 1047 $term = get_term_by( 'id', $this->factory->category->create( array( 'name' => 'Deleted Category' ) ), 'category' ); 996 1048 … … 1006 1058 public function test_delete_item_invalid_taxonomy() { 1007 1059 wp_set_current_user( self::$administrator ); 1060 1008 1061 $request = new WP_REST_Request( 'DELETE', '/wp/v2/invalid-taxonomy/' . REST_TESTS_IMPOSSIBLY_HIGH_NUMBER ); 1009 1062 $response = rest_get_server()->dispatch( $request ); … … 1013 1066 public function test_delete_item_invalid_term() { 1014 1067 wp_set_current_user( self::$administrator ); 1068 1015 1069 $request = new WP_REST_Request( 'DELETE', '/wp/v2/categories/' . REST_TESTS_IMPOSSIBLY_HIGH_NUMBER ); 1016 1070 $response = rest_get_server()->dispatch( $request ); … … 1020 1074 public function test_delete_item_incorrect_permissions() { 1021 1075 wp_set_current_user( self::$subscriber ); 1076 1022 1077 $term = get_term_by( 'id', $this->factory->category->create(), 'category' ); 1023 1078 $request = new WP_REST_Request( 'DELETE', '/wp/v2/categories/' . $term->term_id ); -
trunk/tests/phpunit/tests/rest-api/rest-comments-controller.php
r46586 r46657 26 26 protected static $hold_id; 27 27 28 protected static $comment_ids = array(); 29 protected static $total_comments = 30; 30 protected static $per_page = 50; 31 28 32 protected $endpoint; 29 33 … … 111 115 ) 112 116 ); 117 118 // Set up comments for pagination tests. 119 for ( $i = 0; $i < self::$total_comments - 1; $i++ ) { 120 $comment_ids[] = $factory->comment->create( 121 array( 122 'comment_content' => "Comment {$i}", 123 'comment_post_ID' => self::$post_id, 124 ) 125 ); 126 } 113 127 } 114 128 … … 130 144 wp_delete_post( self::$approved_id, true ); 131 145 wp_delete_post( self::$hold_id, true ); 146 147 // Remove comments for pagination tests. 148 foreach ( self::$comment_ids as $comment_id ) { 149 wp_delete_comment( $comment_id, true ); 150 } 132 151 } 133 152 … … 198 217 199 218 public function test_get_items() { 200 $this->factory->comment->create_post_comments( self::$post_id, 6 );201 202 219 $request = new WP_REST_Request( 'GET', '/wp/v2/comments' ); 220 $request->set_param( 'per_page', self::$per_page ); 203 221 204 222 $response = rest_get_server()->dispatch( $request ); … … 206 224 207 225 $comments = $response->get_data(); 208 // We created 6 comments in this method, plus self::$approved_id. 209 $this->assertCount( 7, $comments ); 226 $this->assertCount( self::$total_comments, $comments ); 210 227 } 211 228 … … 216 233 wp_set_current_user( 0 ); 217 234 218 $args 235 $args = array( 219 236 'comment_approved' => 1, 220 237 'comment_post_ID' => self::$password_id, 221 238 ); 239 222 240 $password_comment = $this->factory->comment->create( $args ); 223 241 … … 238 256 public function test_get_items_with_password_without_post() { 239 257 wp_set_current_user( 0 ); 240 $args = array( 258 259 $args = array( 241 260 'comment_approved' => 1, 242 261 'comment_post_ID' => self::$password_id, 243 262 ); 263 244 264 $password_comment = $this->factory->comment->create( $args ); 245 265 … … 259 279 public function test_get_items_with_password_with_multiple_post() { 260 280 wp_set_current_user( 0 ); 261 $args = array( 281 282 $args = array( 262 283 'comment_approved' => 1, 263 284 'comment_post_ID' => self::$password_id, 264 285 ); 286 265 287 $password_comment = $this->factory->comment->create( $args ); 266 288 … … 276 298 wp_set_current_user( 0 ); 277 299 278 $args 300 $args = array( 279 301 'comment_approved' => 1, 280 302 'comment_post_ID' => self::$password_id, 281 303 ); 304 282 305 $password_comment = $this->factory->comment->create( $args ); 283 306 … … 294 317 wp_set_current_user( self::$admin_id ); 295 318 296 $args 319 $args = array( 297 320 'comment_approved' => 1, 298 321 'comment_post_ID' => self::$password_id, 299 322 ); 323 300 324 $password_comment = $this->factory->comment->create( $args ); 301 325 … … 312 336 wp_set_current_user( 0 ); 313 337 314 $args 338 $args = array( 315 339 'comment_approved' => 1, 316 340 'comment_post_ID' => self::$private_id, 317 341 ); 342 318 343 $private_comment = $this->factory->comment->create( $args ); 319 344 … … 330 355 wp_set_current_user( self::$admin_id ); 331 356 332 $args 357 $args = array( 333 358 'comment_approved' => 1, 334 359 'comment_post_ID' => self::$private_id, 335 360 ); 361 336 362 $private_comment = $this->factory->comment->create( $args ); 337 363 … … 389 415 public function test_get_items_no_permission_for_context() { 390 416 wp_set_current_user( 0 ); 417 391 418 $request = new WP_REST_Request( 'GET', '/wp/v2/comments' ); 392 419 $request->set_param( 'context', 'edit' ); … … 396 423 397 424 public function test_get_items_no_post() { 425 wp_set_current_user( self::$admin_id ); 426 398 427 $this->factory->comment->create_post_comments( 0, 2 ); 399 wp_set_current_user( self::$admin_id ); 428 400 429 $request = new WP_REST_Request( 'GET', '/wp/v2/comments' ); 401 430 $request->set_param( 'post', 0 ); … … 408 437 public function test_get_items_no_permission_for_no_post() { 409 438 wp_set_current_user( 0 ); 439 410 440 $request = new WP_REST_Request( 'GET', '/wp/v2/comments' ); 411 441 $request->set_param( 'post', 0 ); … … 416 446 public function test_get_items_edit_context() { 417 447 wp_set_current_user( self::$admin_id ); 448 418 449 $request = new WP_REST_Request( 'GET', '/wp/v2/comments' ); 419 450 $request->set_param( 'context', 'edit' ); … … 442 473 public function test_get_items_include_query() { 443 474 wp_set_current_user( self::$admin_id ); 475 444 476 $args = array( 445 477 'comment_approved' => 1, 446 478 'comment_post_ID' => self::$post_id, 447 479 ); 448 $id1 = $this->factory->comment->create( $args ); 449 $this->factory->comment->create( $args ); 450 $id3 = $this->factory->comment->create( $args ); 480 481 $id1 = $this->factory->comment->create( $args ); 482 $id2 = $this->factory->comment->create( $args ); 483 451 484 $request = new WP_REST_Request( 'GET', '/wp/v2/comments' ); 452 // Order=>asc 485 486 // 'order' => 'asc'. 453 487 $request->set_param( 'order', 'asc' ); 454 $request->set_param( 'include', array( $id 3, $id1 ) );488 $request->set_param( 'include', array( $id2, $id1 ) ); 455 489 $response = rest_get_server()->dispatch( $request ); 456 490 $data = $response->get_data(); 457 491 $this->assertEquals( 2, count( $data ) ); 458 492 $this->assertEquals( $id1, $data[0]['id'] ); 459 // Orderby=>include 493 494 // 'orderby' => 'include'. 460 495 $request->set_param( 'orderby', 'include' ); 461 496 $response = rest_get_server()->dispatch( $request ); 462 497 $data = $response->get_data(); 463 498 $this->assertEquals( 2, count( $data ) ); 464 $this->assertEquals( $id3, $data[0]['id'] ); 465 // Orderby=>invalid should fail. 499 $this->assertEquals( $id2, $data[0]['id'] ); 500 501 // Invalid 'orderby' should error. 466 502 $request->set_param( 'orderby', 'invalid' ); 467 503 $response = rest_get_server()->dispatch( $request ); 468 504 $this->assertErrorResponse( 'rest_invalid_param', $response, 400 ); 469 // fails on invalid id. 505 506 // Invalid 'include' should error. 470 507 $request->set_param( 'orderby', array( 'include' ) ); 471 508 $request->set_param( 'include', array( 'invalid' ) ); … … 476 513 public function test_get_items_exclude_query() { 477 514 wp_set_current_user( self::$admin_id ); 478 $args = array( 479 'comment_approved' => 1, 480 'comment_post_ID' => self::$post_id, 481 ); 482 $id1 = $this->factory->comment->create( $args ); 483 $id2 = $this->factory->comment->create( $args ); 484 $request = new WP_REST_Request( 'GET', '/wp/v2/comments' ); 485 $response = rest_get_server()->dispatch( $request ); 486 $data = $response->get_data(); 487 $this->assertTrue( in_array( $id1, wp_list_pluck( $data, 'id' ), true ) ); 488 $this->assertTrue( in_array( $id2, wp_list_pluck( $data, 'id' ), true ) ); 489 $request->set_param( 'exclude', array( $id2 ) ); 490 $response = rest_get_server()->dispatch( $request ); 491 $data = $response->get_data(); 492 $this->assertTrue( in_array( $id1, wp_list_pluck( $data, 'id' ), true ) ); 493 $this->assertFalse( in_array( $id2, wp_list_pluck( $data, 'id' ), true ) ); 494 495 // fails on invalid id. 496 $request->set_param( 'exclude', array( 'invalid' ) ); 497 $response = rest_get_server()->dispatch( $request ); 498 $this->assertErrorResponse( 'rest_invalid_param', $response, 400 ); 499 } 500 501 public function test_get_items_offset_query() { 502 wp_set_current_user( self::$admin_id ); 515 503 516 $args = array( 504 517 'comment_approved' => 1, 505 518 'comment_post_ID' => self::$post_id, 506 519 ); 507 $this->factory->comment->create( $args ); 508 $this->factory->comment->create( $args ); 509 $this->factory->comment->create( $args ); 520 521 $id1 = $this->factory->comment->create( $args ); 522 $id2 = $this->factory->comment->create( $args ); 523 524 $request = new WP_REST_Request( 'GET', '/wp/v2/comments' ); 525 $response = rest_get_server()->dispatch( $request ); 526 $data = $response->get_data(); 527 $ids = wp_list_pluck( $data, 'id' ); 528 $this->assertTrue( in_array( $id1, $ids, true ) ); 529 $this->assertTrue( in_array( $id2, $ids, true ) ); 530 531 $request->set_param( 'exclude', array( $id2 ) ); 532 $response = rest_get_server()->dispatch( $request ); 533 $data = $response->get_data(); 534 $ids = wp_list_pluck( $data, 'id' ); 535 $this->assertTrue( in_array( $id1, $ids, true ) ); 536 $this->assertFalse( in_array( $id2, $ids, true ) ); 537 538 // Invalid 'exclude' should error. 539 $request->set_param( 'exclude', array( 'invalid' ) ); 540 $response = rest_get_server()->dispatch( $request ); 541 $this->assertErrorResponse( 'rest_invalid_param', $response, 400 ); 542 } 543 544 public function test_get_items_offset_query() { 545 wp_set_current_user( self::$admin_id ); 546 510 547 $request = new WP_REST_Request( 'GET', '/wp/v2/comments' ); 548 $request->set_param( 'per_page', self::$per_page ); 511 549 $request->set_param( 'offset', 1 ); 512 550 $response = rest_get_server()->dispatch( $request ); 513 $this->assertCount( 3, $response->get_data() ); 514 // 'offset' works with 'per_page' 551 $this->assertCount( self::$total_comments - 1, $response->get_data() ); 552 553 // 'offset' works with 'per_page'. 515 554 $request->set_param( 'per_page', 2 ); 516 555 $response = rest_get_server()->dispatch( $request ); 517 556 $this->assertCount( 2, $response->get_data() ); 518 // 'offset' takes priority over 'page' 557 558 // 'offset' takes priority over 'page'. 519 559 $request->set_param( 'page', 3 ); 520 560 $response = rest_get_server()->dispatch( $request ); 521 561 $this->assertCount( 2, $response->get_data() ); 522 // 'offset' with invalid value errors. 562 563 // Invalid 'offset' should error. 523 564 $request->set_param( 'offset', 'moreplease' ); 524 565 $response = rest_get_server()->dispatch( $request ); … … 528 569 public function test_get_items_order_query() { 529 570 wp_set_current_user( self::$admin_id ); 571 530 572 $args = array( 531 573 'comment_approved' => 1, 532 574 'comment_post_ID' => self::$post_id, 533 575 ); 534 $this->factory->comment->create( $args ); 535 $ this->factory->comment->create( $args );536 $id3 = $this->factory->comment->create( $args ); 576 577 $id = $this->factory->comment->create( $args ); 578 537 579 $request = new WP_REST_Request( 'GET', '/wp/v2/comments' ); 538 // order defaults to 'desc' 580 581 // Order defaults to 'desc'. 539 582 $response = rest_get_server()->dispatch( $request ); 540 583 $data = $response->get_data(); 541 $this->assertEquals( $id3, $data[0]['id'] ); 542 // order=>asc 584 $this->assertEquals( $id, $data[0]['id'] ); 585 586 // 'order' => 'asc'. 543 587 $request->set_param( 'order', 'asc' ); 544 588 $response = rest_get_server()->dispatch( $request ); 545 589 $data = $response->get_data(); 546 590 $this->assertEquals( self::$approved_id, $data[0]['id'] ); 547 // order=>asc,id should fail 591 592 // 'order' => 'asc,id' should error. 548 593 $request->set_param( 'order', 'asc,id' ); 549 594 $response = rest_get_server()->dispatch( $request ); … … 553 598 public function test_get_items_private_post_no_permissions() { 554 599 wp_set_current_user( 0 ); 600 555 601 $post_id = $this->factory->post->create( array( 'post_status' => 'private' ) ); 602 556 603 $request = new WP_REST_Request( 'GET', '/wp/v2/comments' ); 557 604 $request->set_param( 'post', $post_id ); … … 561 608 562 609 public function test_get_items_author_arg() { 563 // Authorized 564 wp_set_current_user( self::$admin_id ); 610 // Authorized. 611 wp_set_current_user( self::$admin_id ); 612 565 613 $args = array( 566 614 'comment_approved' => 1, … … 568 616 'user_id' => self::$author_id, 569 617 ); 618 570 619 $this->factory->comment->create( $args ); 571 620 $args['user_id'] = self::$subscriber_id; … … 574 623 $this->factory->comment->create( $args ); 575 624 576 // 'author' limits result to 1 of 3625 // Limit to comment author. 577 626 $request = new WP_REST_Request( 'GET', '/wp/v2/comments' ); 578 627 $request->set_param( 'author', self::$author_id ); … … 581 630 $comments = $response->get_data(); 582 631 $this->assertCount( 1, $comments ); 583 // Multiple authors are supported 632 633 // Multiple authors are supported. 584 634 $request->set_param( 'author', array( self::$author_id, self::$subscriber_id ) ); 585 635 $response = rest_get_server()->dispatch( $request ); … … 587 637 $comments = $response->get_data(); 588 638 $this->assertCount( 2, $comments ); 589 // Invalid author param errors 639 640 // Invalid 'author' should error. 590 641 $request->set_param( 'author', 'skippy' ); 591 642 $response = rest_get_server()->dispatch( $request ); 592 643 $this->assertErrorResponse( 'rest_invalid_param', $response, 400 ); 593 // Unavailable to unauthenticated; defaults to error 644 645 // Unavailable to unauthenticated; defaults to error. 594 646 wp_set_current_user( 0 ); 595 647 $request->set_param( 'author', array( self::$author_id, self::$subscriber_id ) ); … … 599 651 600 652 public function test_get_items_author_exclude_arg() { 601 // Authorized 602 wp_set_current_user( self::$admin_id ); 653 // Authorized. 654 wp_set_current_user( self::$admin_id ); 655 603 656 $args = array( 604 657 'comment_approved' => 1, … … 606 659 'user_id' => self::$author_id, 607 660 ); 661 608 662 $this->factory->comment->create( $args ); 609 663 $args['user_id'] = self::$subscriber_id; … … 612 666 $this->factory->comment->create( $args ); 613 667 614 $request = new WP_REST_Request( 'GET', '/wp/v2/comments' ); 668 $total_comments = self::$total_comments + 3; 669 670 $request = new WP_REST_Request( 'GET', '/wp/v2/comments' ); 671 $request->set_param( 'per_page', self::$per_page ); 615 672 $response = rest_get_server()->dispatch( $request ); 616 673 $comments = $response->get_data(); 617 $this->assertCount( 4, $comments );618 619 // 'author_exclude' limits result to 3 of 4674 $this->assertCount( $total_comments, $comments ); 675 676 // Exclude comment author. 620 677 $request = new WP_REST_Request( 'GET', '/wp/v2/comments' ); 678 $request->set_param( 'per_page', self::$per_page ); 621 679 $request->set_param( 'author_exclude', self::$author_id ); 622 680 $response = rest_get_server()->dispatch( $request ); 623 681 $this->assertEquals( 200, $response->get_status() ); 624 682 $comments = $response->get_data(); 625 $this->assertCount( 3, $comments ); 626 // 'author_exclude' for both comment authors (2 of 4) 683 $this->assertCount( $total_comments - 1, $comments ); 684 685 // Exclude both comment authors. 627 686 $request = new WP_REST_Request( 'GET', '/wp/v2/comments' ); 687 $request->set_param( 'per_page', self::$per_page ); 628 688 $request->set_param( 'author_exclude', array( self::$author_id, self::$subscriber_id ) ); 629 689 $response = rest_get_server()->dispatch( $request ); 630 690 $this->assertEquals( 200, $response->get_status() ); 631 691 $comments = $response->get_data(); 632 $this->assertCount( 2, $comments ); 633 // 'author_exclude' for both invalid author 692 $this->assertCount( $total_comments - 2, $comments ); 693 694 // 'author_exclude' for invalid author. 634 695 $request = new WP_REST_Request( 'GET', '/wp/v2/comments' ); 635 696 $request->set_param( 'author_exclude', 'skippy' ); 636 697 $response = rest_get_server()->dispatch( $request ); 637 698 $this->assertErrorResponse( 'rest_invalid_param', $response, 400 ); 638 // Unavailable to unauthenticated; defaults to error 699 700 // Unavailable to unauthenticated; defaults to error. 639 701 wp_set_current_user( 0 ); 640 702 $request->set_param( 'author_exclude', array( self::$author_id, self::$subscriber_id ) ); … … 654 716 $args['comment_parent'] = $parent_id2; 655 717 $this->factory->comment->create( $args ); 656 // All comments in the database 657 $request = new WP_REST_Request( 'GET', '/wp/v2/comments' ); 658 $response = rest_get_server()->dispatch( $request ); 659 $this->assertCount( 5, $response->get_data() ); 660 // Limit to the parent 718 719 $total_comments = self::$total_comments + 4; 720 721 // All comments in the database. 722 $request = new WP_REST_Request( 'GET', '/wp/v2/comments' ); 723 $request->set_param( 'per_page', self::$per_page ); 724 $response = rest_get_server()->dispatch( $request ); 725 $this->assertCount( $total_comments, $response->get_data() ); 726 727 // Limit to the parent. 661 728 $request->set_param( 'parent', $parent_id ); 662 729 $response = rest_get_server()->dispatch( $request ); 663 730 $this->assertCount( 1, $response->get_data() ); 664 // Limit to two parents 731 732 // Limit to two parents. 665 733 $request->set_param( 'parent', array( $parent_id, $parent_id2 ) ); 666 734 $response = rest_get_server()->dispatch( $request ); 667 735 $this->assertCount( 2, $response->get_data() ); 668 // Invalid parent should error 736 737 // Invalid 'parent' should error. 669 738 $request->set_param( 'parent', 'invalid' ); 670 739 $response = rest_get_server()->dispatch( $request ); … … 683 752 $args['comment_parent'] = $parent_id2; 684 753 $this->factory->comment->create( $args ); 685 // All comments in the database 686 $request = new WP_REST_Request( 'GET', '/wp/v2/comments' ); 687 $response = rest_get_server()->dispatch( $request ); 688 $this->assertCount( 5, $response->get_data() ); 689 // Exclude this particular parent 754 755 $total_comments = self::$total_comments + 4; 756 757 // All comments in the database. 758 $request = new WP_REST_Request( 'GET', '/wp/v2/comments' ); 759 $request->set_param( 'per_page', self::$per_page ); 760 $response = rest_get_server()->dispatch( $request ); 761 $this->assertCount( $total_comments, $response->get_data() ); 762 763 // Exclude this particular parent. 690 764 $request->set_param( 'parent_exclude', $parent_id ); 691 765 $response = rest_get_server()->dispatch( $request ); 692 $this->assertCount( 4, $response->get_data() ); 693 // Exclude both comment parents 766 $this->assertCount( $total_comments - 1, $response->get_data() ); 767 768 // Exclude both comment parents. 694 769 $request->set_param( 'parent_exclude', array( $parent_id, $parent_id2 ) ); 695 770 $response = rest_get_server()->dispatch( $request ); 696 $this->assertCount( 3, $response->get_data() ); 697 // Invalid parent id should error 771 $this->assertCount( $total_comments - 2, $response->get_data() ); 772 773 // Invalid 'parent_exclude' should error. 698 774 $request->set_param( 'parent_exclude', 'invalid' ); 699 775 $response = rest_get_server()->dispatch( $request ); … … 703 779 public function test_get_items_search_query() { 704 780 wp_set_current_user( self::$admin_id ); 705 $args = array( 781 782 $args = array( 706 783 'comment_approved' => 1, 707 784 'comment_post_ID' => self::$post_id, … … 709 786 'comment_author' => 'Homer J Simpson', 710 787 ); 711 $id1 = $this->factory->comment->create( $args ); 712 $args['comment_content'] = 'bar'; 713 $this->factory->comment->create( $args ); 714 $args['comment_content'] = 'burrito'; 715 $this->factory->comment->create( $args ); 716 // 3 comments, plus 1 created in construct 717 $request = new WP_REST_Request( 'GET', '/wp/v2/comments' ); 718 $response = rest_get_server()->dispatch( $request ); 719 $this->assertCount( 4, $response->get_data() ); 720 // One matching comments 788 789 $id = $this->factory->comment->create( $args ); 790 791 $total_comments = self::$total_comments + 1; 792 793 $request = new WP_REST_Request( 'GET', '/wp/v2/comments' ); 794 $request->set_param( 'per_page', self::$per_page ); 795 $response = rest_get_server()->dispatch( $request ); 796 $this->assertCount( $total_comments, $response->get_data() ); 797 798 // One matching comment. 721 799 $request->set_param( 'search', 'foo' ); 722 800 $response = rest_get_server()->dispatch( $request ); 723 801 $data = $response->get_data(); 724 802 $this->assertCount( 1, $data ); 725 $this->assertEquals( $id 1, $data[0]['id'] );803 $this->assertEquals( $id, $data[0]['id'] ); 726 804 } 727 805 728 806 public function test_get_comments_pagination_headers() { 729 wp_set_current_user( self::$admin_id ); 730 // Start of the index 731 for ( $i = 0; $i < 49; $i++ ) { 732 $this->factory->comment->create( 733 array( 734 'comment_content' => "Comment {$i}", 735 'comment_post_ID' => self::$post_id, 736 ) 737 ); 738 } 807 $total_comments = self::$total_comments; 808 $total_pages = (int) ceil( $total_comments / 10 ); 809 810 wp_set_current_user( self::$admin_id ); 811 812 // Start of the index. 739 813 $request = new WP_REST_Request( 'GET', '/wp/v2/comments' ); 740 814 $response = rest_get_server()->dispatch( $request ); 741 815 $headers = $response->get_headers(); 742 $this->assertEquals( 50, $headers['X-WP-Total'] );743 $this->assertEquals( 5, $headers['X-WP-TotalPages'] );816 $this->assertEquals( $total_comments, $headers['X-WP-Total'] ); 817 $this->assertEquals( $total_pages, $headers['X-WP-TotalPages'] ); 744 818 $next_link = add_query_arg( 745 819 array( … … 750 824 $this->assertFalse( stripos( $headers['Link'], 'rel="prev"' ) ); 751 825 $this->assertContains( '<' . $next_link . '>; rel="next"', $headers['Link'] ); 752 // 3rd page 826 827 // 3rd page. 753 828 $this->factory->comment->create( 754 829 array( 755 'comment_content' => 'Comment 51',756 830 'comment_post_ID' => self::$post_id, 757 831 ) 758 832 ); 833 $total_comments++; 834 $total_pages++; 759 835 $request = new WP_REST_Request( 'GET', '/wp/v2/comments' ); 760 836 $request->set_param( 'page', 3 ); 761 837 $response = rest_get_server()->dispatch( $request ); 762 838 $headers = $response->get_headers(); 763 $this->assertEquals( 51, $headers['X-WP-Total'] );764 $this->assertEquals( 6, $headers['X-WP-TotalPages'] );839 $this->assertEquals( $total_comments, $headers['X-WP-Total'] ); 840 $this->assertEquals( $total_pages, $headers['X-WP-TotalPages'] ); 765 841 $prev_link = add_query_arg( 766 842 array( … … 777 853 ); 778 854 $this->assertContains( '<' . $next_link . '>; rel="next"', $headers['Link'] ); 779 // Last page 855 856 // Last page. 780 857 $request = new WP_REST_Request( 'GET', '/wp/v2/comments' ); 781 $request->set_param( 'page', 6);858 $request->set_param( 'page', $total_pages ); 782 859 $response = rest_get_server()->dispatch( $request ); 783 860 $headers = $response->get_headers(); 784 $this->assertEquals( 51, $headers['X-WP-Total'] );785 $this->assertEquals( 6, $headers['X-WP-TotalPages'] );861 $this->assertEquals( $total_comments, $headers['X-WP-Total'] ); 862 $this->assertEquals( $total_pages, $headers['X-WP-TotalPages'] ); 786 863 $prev_link = add_query_arg( 787 864 array( 788 'page' => 5,865 'page' => $total_pages - 1, 789 866 ), 790 867 rest_url( '/wp/v2/comments' ) … … 792 869 $this->assertContains( '<' . $prev_link . '>; rel="prev"', $headers['Link'] ); 793 870 $this->assertFalse( stripos( $headers['Link'], 'rel="next"' ) ); 794 // Out of bounds 871 872 // Out of bounds. 795 873 $request = new WP_REST_Request( 'GET', '/wp/v2/comments' ); 796 $request->set_param( 'page', 8);874 $request->set_param( 'page', 100 ); 797 875 $response = rest_get_server()->dispatch( $request ); 798 876 $headers = $response->get_headers(); 799 $this->assertEquals( 51, $headers['X-WP-Total'] );800 $this->assertEquals( 6, $headers['X-WP-TotalPages'] );877 $this->assertEquals( $total_comments, $headers['X-WP-Total'] ); 878 $this->assertEquals( $total_pages, $headers['X-WP-TotalPages'] ); 801 879 $prev_link = add_query_arg( 802 880 array( 803 'page' => 6,881 'page' => $total_pages, 804 882 ), 805 883 rest_url( '/wp/v2/comments' ) … … 858 936 public function test_prepare_item() { 859 937 wp_set_current_user( self::$admin_id ); 938 860 939 $request = new WP_REST_Request( 'GET', sprintf( '/wp/v2/comments/%d', self::$approved_id ) ); 861 940 $request->set_query_params( … … 874 953 public function test_prepare_item_limit_fields() { 875 954 wp_set_current_user( self::$admin_id ); 955 876 956 $endpoint = new WP_REST_Comments_Controller; 877 957 $request = new WP_REST_Request( 'GET', sprintf( '/wp/v2/comments/%d', self::$approved_id ) ); … … 916 996 public function test_get_comment_invalid_context() { 917 997 wp_set_current_user( 0 ); 998 918 999 $request = new WP_REST_Request( 'GET', sprintf( '/wp/v2/comments/%s', self::$approved_id ) ); 919 1000 $request->set_param( 'context', 'edit' ); … … 924 1005 public function test_get_comment_invalid_post_id() { 925 1006 wp_set_current_user( 0 ); 1007 926 1008 $comment_id = $this->factory->comment->create( 927 1009 array( … … 930 1012 ) 931 1013 ); 932 $request = new WP_REST_Request( 'GET', '/wp/v2/comments/' . $comment_id ); 933 1014 1015 $request = new WP_REST_Request( 'GET', '/wp/v2/comments/' . $comment_id ); 934 1016 $response = rest_get_server()->dispatch( $request ); 935 1017 $this->assertErrorResponse( 'rest_post_invalid_id', $response, 404 ); … … 938 1020 public function test_get_comment_invalid_post_id_as_admin() { 939 1021 wp_set_current_user( self::$admin_id ); 1022 940 1023 $comment_id = $this->factory->comment->create( 941 1024 array( … … 944 1027 ) 945 1028 ); 946 $request = new WP_REST_Request( 'GET', '/wp/v2/comments/' . $comment_id ); 947 1029 1030 $request = new WP_REST_Request( 'GET', '/wp/v2/comments/' . $comment_id ); 948 1031 $response = rest_get_server()->dispatch( $request ); 949 1032 $this->assertErrorResponse( 'rest_post_invalid_id', $response, 404 ); … … 953 1036 wp_set_current_user( 0 ); 954 1037 955 $request = new WP_REST_Request( 'GET', sprintf( '/wp/v2/comments/%d', self::$hold_id ) ); 956 1038 $request = new WP_REST_Request( 'GET', sprintf( '/wp/v2/comments/%d', self::$hold_id ) ); 957 1039 $response = rest_get_server()->dispatch( $request ); 958 1040 $this->assertErrorResponse( 'rest_cannot_read', $response, 401 ); … … 962 1044 wp_set_current_user( self::$admin_id ); 963 1045 964 $request = new WP_REST_Request( 'GET', sprintf( '/wp/v2/comments/%d', self::$hold_id ) ); 965 1046 $request = new WP_REST_Request( 'GET', sprintf( '/wp/v2/comments/%d', self::$hold_id ) ); 966 1047 $response = rest_get_server()->dispatch( $request ); 967 1048 $this->assertEquals( 200, $response->get_status() ); … … 1009 1090 public function test_get_comment_with_password_without_edit_post_permission() { 1010 1091 wp_set_current_user( self::$subscriber_id ); 1011 $args = array( 1092 1093 $args = array( 1012 1094 'comment_approved' => 1, 1013 1095 'comment_post_ID' => self::$password_id, 1014 1096 ); 1097 1015 1098 $password_comment = $this->factory->comment->create( $args ); 1016 $request = new WP_REST_Request( 'GET', sprintf( '/wp/v2/comments/%s', $password_comment ) ); 1017 $response = rest_get_server()->dispatch( $request ); 1099 1100 $request = new WP_REST_Request( 'GET', sprintf( '/wp/v2/comments/%s', $password_comment ) ); 1101 $response = rest_get_server()->dispatch( $request ); 1018 1102 $this->assertErrorResponse( 'rest_cannot_read', $response, 403 ); 1019 1103 } … … 1025 1109 wp_set_current_user( self::$subscriber_id ); 1026 1110 1027 $args 1111 $args = array( 1028 1112 'comment_approved' => 1, 1029 1113 'comment_post_ID' => self::$password_id, 1030 1114 ); 1115 1031 1116 $password_comment = $this->factory->comment->create( $args ); 1032 1117 … … 1114 1199 public function test_create_comment_date( $params, $results ) { 1115 1200 wp_set_current_user( self::$admin_id ); 1201 1116 1202 update_option( 'timezone_string', $params['timezone_string'] ); 1117 1203 … … 1234 1320 public function test_create_comment_missing_required_author_email() { 1235 1321 wp_set_current_user( self::$admin_id ); 1322 1236 1323 update_option( 'require_name_email', 1 ); 1237 1324 … … 1252 1339 public function test_create_comment_empty_required_author_email() { 1253 1340 wp_set_current_user( self::$admin_id ); 1341 1254 1342 update_option( 'require_name_email', 1 ); 1255 1343 … … 1343 1431 1344 1432 wp_set_current_user( self::$admin_id ); 1345 $params = array( 1433 1434 $params = array( 1346 1435 'post' => self::$post_id, 1347 1436 'author_name' => 'Comic Book Guy', … … 1352 1441 'date' => '2014-11-07T10:14:25', 1353 1442 ); 1443 1354 1444 $request = new WP_REST_Request( 'POST', '/wp/v2/comments' ); 1355 1445 $request->add_header( 'content-type', 'application/json' ); … … 1365 1455 public function test_create_comment_without_type() { 1366 1456 $post_id = $this->factory->post->create(); 1457 1367 1458 wp_set_current_user( self::$admin_id ); 1368 1459 … … 1402 1493 public function test_create_comment_with_invalid_type() { 1403 1494 $post_id = $this->factory->post->create(); 1495 1404 1496 wp_set_current_user( self::$admin_id ); 1405 1497 … … 1425 1517 public function test_create_comment_invalid_email() { 1426 1518 $post_id = $this->factory->post->create(); 1519 1427 1520 wp_set_current_user( self::$admin_id ); 1428 1521 … … 1569 1662 public function test_create_comment_with_status_IP_and_user_agent() { 1570 1663 $post_id = $this->factory->post->create(); 1664 1571 1665 wp_set_current_user( self::$admin_id ); 1572 1666 … … 1663 1757 public function test_create_comment_author_ip_no_permission() { 1664 1758 wp_set_current_user( self::$subscriber_id ); 1665 $params = array( 1759 1760 $params = array( 1666 1761 'author_name' => 'Comic Book Guy', 1667 1762 'author_email' => 'cbg@androidsdungeon.com', … … 1671 1766 'status' => 'approved', 1672 1767 ); 1768 1673 1769 $request = new WP_REST_Request( 'POST', '/wp/v2/comments' ); 1674 1770 $request->add_header( 'content-type', 'application/json' ); … … 1680 1776 public function test_create_comment_author_ip_defaults_to_remote_addr() { 1681 1777 wp_set_current_user( self::$admin_id ); 1778 1682 1779 $_SERVER['REMOTE_ADDR'] = '127.0.0.2'; 1683 $params = array( 1780 1781 $params = array( 1684 1782 'post' => self::$post_id, 1685 1783 'author_name' => 'Comic Book Guy', … … 1688 1786 'content' => 'Worst Comment Ever!', 1689 1787 ); 1690 $request = new WP_REST_Request( 'POST', '/wp/v2/comments' ); 1788 1789 $request = new WP_REST_Request( 'POST', '/wp/v2/comments' ); 1691 1790 $request->add_header( 'content-type', 'application/json' ); 1692 1791 $request->set_body( wp_json_encode( $params ) ); … … 1700 1799 wp_set_current_user( self::$admin_id ); 1701 1800 1702 $params 1801 $params = array( 1703 1802 'author_name' => 'Comic Book Guy', 1704 1803 'author_email' => 'cbg@androidsdungeon.com', … … 1707 1806 'status' => 'approved', 1708 1807 ); 1808 1709 1809 $request = new WP_REST_Request( 'POST', '/wp/v2/comments' ); 1710 1810 $request->add_header( 'content-type', 'application/json' ); … … 1719 1819 wp_set_current_user( self::$subscriber_id ); 1720 1820 1721 $params 1821 $params = array( 1722 1822 'author_name' => 'Homer Jay Simpson', 1723 1823 'author_email' => 'chunkylover53@aol.com', … … 1726 1826 'author' => self::$subscriber_id, 1727 1827 ); 1828 1728 1829 $request = new WP_REST_Request( 'POST', '/wp/v2/comments' ); 1729 1830 $request->add_header( 'content-type', 'application/json' ); … … 1757 1858 wp_set_current_user( self::$subscriber_id ); 1758 1859 1759 $params 1860 $params = array( 1760 1861 'post' => self::$draft_id, 1761 1862 'author_name' => 'Ishmael', … … 1765 1866 'author' => self::$subscriber_id, 1766 1867 ); 1767 $request = new WP_REST_Request( 'POST', '/wp/v2/comments' ); 1768 $request ->add_header( 'content-type', 'application/json' );1769 $request-> set_body( wp_json_encode( $params ));1770 1771 $response = rest_get_server()->dispatch( $request ); 1772 1868 1869 $request = new WP_REST_Request( 'POST', '/wp/v2/comments' ); 1870 $request->add_header( 'content-type', 'application/json' ); 1871 $request->set_body( wp_json_encode( $params ) ); 1872 1873 $response = rest_get_server()->dispatch( $request ); 1773 1874 $this->assertErrorResponse( 'rest_comment_draft_post', $response, 403 ); 1774 1875 } … … 1777 1878 wp_set_current_user( self::$subscriber_id ); 1778 1879 1779 $params 1880 $params = array( 1780 1881 'post' => self::$trash_id, 1781 1882 'author_name' => 'Ishmael', … … 1785 1886 'author' => self::$subscriber_id, 1786 1887 ); 1888 1787 1889 $request = new WP_REST_Request( 'POST', '/wp/v2/comments' ); 1788 1890 $request->add_header( 'content-type', 'application/json' ); … … 1797 1899 wp_set_current_user( self::$subscriber_id ); 1798 1900 1799 $params 1901 $params = array( 1800 1902 'post' => self::$private_id, 1801 1903 'author_name' => 'Homer Jay Simpson', … … 1805 1907 'author' => self::$subscriber_id, 1806 1908 ); 1807 $request = new WP_REST_Request( 'POST', '/wp/v2/comments' ); 1808 $request ->add_header( 'content-type', 'application/json' );1809 $request-> set_body( wp_json_encode( $params ));1810 1811 $response = rest_get_server()->dispatch( $request ); 1812 1909 1910 $request = new WP_REST_Request( 'POST', '/wp/v2/comments' ); 1911 $request->add_header( 'content-type', 'application/json' ); 1912 $request->set_body( wp_json_encode( $params ) ); 1913 1914 $response = rest_get_server()->dispatch( $request ); 1813 1915 $this->assertErrorResponse( 'rest_cannot_read_post', $response, 403 ); 1814 1916 } … … 1817 1919 wp_set_current_user( self::$subscriber_id ); 1818 1920 1819 $params 1921 $params = array( 1820 1922 'post' => self::$password_id, 1821 1923 'author_name' => 'Homer Jay Simpson', … … 1825 1927 'author' => self::$subscriber_id, 1826 1928 ); 1929 1827 1930 $request = new WP_REST_Request( 'POST', '/wp/v2/comments' ); 1828 1931 $request->add_header( 'content-type', 'application/json' ); … … 1835 1938 public function test_create_item_duplicate() { 1836 1939 wp_set_current_user( self::$subscriber_id ); 1940 1837 1941 $this->factory->comment->create( 1838 1942 array( … … 1865 1969 ) 1866 1970 ); 1971 1867 1972 wp_set_current_user( self::$subscriber_id ); 1868 1973 … … 1881 1986 public function test_create_comment_require_login() { 1882 1987 wp_set_current_user( 0 ); 1988 1883 1989 update_option( 'comment_registration', 1 ); 1884 1990 add_filter( 'rest_allow_anonymous_comments', '__return_true' ); 1991 1885 1992 $request = new WP_REST_Request( 'POST', '/wp/v2/comments' ); 1886 1993 $request->set_param( 'post', self::$post_id ); … … 1998 2105 wp_set_current_user( self::$subscriber_id ); 1999 2106 2000 $params 2107 $params = array( 2001 2108 'post' => self::$post_id, 2002 2109 'author_name' => rand_long_str( 246 ), … … 2006 2113 'date' => '1995-04-30T10:22:00', 2007 2114 ); 2115 2008 2116 $request = new WP_REST_Request( 'POST', '/wp/v2/comments' ); 2009 2117 … … 2021 2129 wp_set_current_user( self::$subscriber_id ); 2022 2130 2023 $params 2131 $params = array( 2024 2132 'post' => self::$post_id, 2025 2133 'author_name' => 'Bleeding Gums Murphy', … … 2029 2137 'date' => '1995-04-30T10:22:00', 2030 2138 ); 2139 2031 2140 $request = new WP_REST_Request( 'POST', '/wp/v2/comments' ); 2032 2141 … … 2044 2153 wp_set_current_user( self::$subscriber_id ); 2045 2154 2046 $params 2155 $params = array( 2047 2156 'post' => self::$post_id, 2048 2157 'author_name' => 'Bleeding Gums Murphy', … … 2052 2161 'date' => '1995-04-30T10:22:00', 2053 2162 ); 2163 2054 2164 $request = new WP_REST_Request( 'POST', '/wp/v2/comments' ); 2055 2165 … … 2067 2177 wp_set_current_user( self::$subscriber_id ); 2068 2178 2069 $params 2179 $params = array( 2070 2180 'post' => self::$post_id, 2071 2181 'author_name' => 'Bleeding Gums Murphy', … … 2075 2185 'date' => '1995-04-30T10:22:00', 2076 2186 ); 2187 2077 2188 $request = new WP_REST_Request( 'POST', '/wp/v2/comments' ); 2078 2189 … … 2087 2198 wp_set_current_user( self::$subscriber_id ); 2088 2199 2089 $params 2200 $params = array( 2090 2201 'post' => self::$password_id, 2091 2202 'author_name' => 'Bleeding Gums Murphy', … … 2094 2205 'content' => 'This isn\'t a saxophone. It\'s an umbrella.', 2095 2206 ); 2207 2096 2208 $request = new WP_REST_Request( 'POST', '/wp/v2/comments' ); 2097 2209 … … 2106 2218 add_filter( 'rest_allow_anonymous_comments', '__return_true' ); 2107 2219 2108 $params 2220 $params = array( 2109 2221 'post' => self::$password_id, 2110 2222 'author_name' => 'Bleeding Gums Murphy', … … 2114 2226 'password' => 'toomanysecrets', 2115 2227 ); 2228 2116 2229 $request = new WP_REST_Request( 'POST', '/wp/v2/comments' ); 2117 2230 … … 2127 2240 wp_set_current_user( self::$admin_id ); 2128 2241 2129 $params 2242 $params = array( 2130 2243 'author' => self::$subscriber_id, 2131 2244 'author_name' => 'Disco Stu', … … 2137 2250 'post' => $post_id, 2138 2251 ); 2252 2139 2253 $request = new WP_REST_Request( 'PUT', sprintf( '/wp/v2/comments/%d', self::$approved_id ) ); 2140 2254 $request->add_header( 'content-type', 'application/json' ); … … 2163 2277 public function test_update_comment_date( $params, $results ) { 2164 2278 wp_set_current_user( self::$editor_id ); 2279 2165 2280 update_option( 'timezone_string', $params['timezone_string'] ); 2166 2281 … … 2214 2329 2215 2330 wp_set_current_user( self::$admin_id ); 2331 2216 2332 $request = new WP_REST_Request( 'PUT', sprintf( '/wp/v2/comments/%d', self::$approved_id ) ); 2217 2333 $request->set_param( 'post', $comment->comment_post_ID ); … … 2236 2352 ); 2237 2353 2238 $params 2354 $params = array( 2239 2355 'status' => 'approve', 2240 2356 ); 2357 2241 2358 $request = new WP_REST_Request( 'PUT', sprintf( '/wp/v2/comments/%d', $comment_id ) ); 2242 2359 $request->add_header( 'content-type', 'application/json' ); … … 2263 2380 ); 2264 2381 2265 $params 2382 $params = array( 2266 2383 'status' => 'approve', 2267 2384 ); 2385 2268 2386 $request = new WP_REST_Request( 'PUT', sprintf( '/wp/v2/comments/%d', $comment_id ) ); 2269 2387 $request->add_header( 'content-type', 'application/json' ); … … 2283 2401 wp_set_current_user( self::$admin_id ); 2284 2402 2285 $params 2403 $params = array( 2286 2404 'date_gmt' => '2015-05-07T10:14:25', 2287 2405 'content' => 'I\'ll be deep in the cold, cold ground before I recognize Missouri.', 2288 2406 ); 2407 2289 2408 $request = new WP_REST_Request( 'PUT', sprintf( '/wp/v2/comments/%d', self::$approved_id ) ); 2290 2409 $request->add_header( 'content-type', 'application/json' ); … … 2302 2421 public function test_update_comment_author_email_only() { 2303 2422 wp_set_current_user( self::$editor_id ); 2423 2304 2424 update_option( 'require_name_email', 1 ); 2305 2425 … … 2320 2440 public function test_update_comment_empty_author_name() { 2321 2441 wp_set_current_user( self::$editor_id ); 2442 2322 2443 update_option( 'require_name_email', 1 ); 2323 2444 … … 2339 2460 public function test_update_comment_author_name_only() { 2340 2461 wp_set_current_user( self::$admin_id ); 2462 2341 2463 update_option( 'require_name_email', 1 ); 2342 2464 … … 2357 2479 public function test_update_comment_empty_author_email() { 2358 2480 wp_set_current_user( self::$admin_id ); 2481 2359 2482 update_option( 'require_name_email', 1 ); 2360 2483 … … 2397 2520 wp_set_current_user( self::$admin_id ); 2398 2521 2399 $params 2522 $params = array( 2400 2523 'type' => 'trackback', 2401 2524 ); 2525 2402 2526 $request = new WP_REST_Request( 'PUT', sprintf( '/wp/v2/comments/%d', self::$approved_id ) ); 2403 2527 $request->add_header( 'content-type', 'application/json' ); … … 2411 2535 wp_set_current_user( self::$admin_id ); 2412 2536 2413 $params 2537 $params = array( 2414 2538 'content' => array( 2415 2539 'raw' => 'What the heck kind of name is Persephone?', 2416 2540 ), 2417 2541 ); 2542 2418 2543 $request = new WP_REST_Request( 'PUT', sprintf( '/wp/v2/comments/%d', self::$approved_id ) ); 2419 2544 $request->add_header( 'content-type', 'application/json' ); … … 2464 2589 wp_set_current_user( self::$subscriber_id ); 2465 2590 2466 $params 2591 $params = array( 2467 2592 'content' => 'Oh, they have the internet on computers now!', 2468 2593 ); 2594 2469 2595 $request = new WP_REST_Request( 'PUT', '/wp/v2/comments/' . REST_TESTS_IMPOSSIBLY_HIGH_NUMBER ); 2470 2596 $request->add_header( 'content-type', 'application/json' ); … … 2488 2614 add_filter( 'rest_allow_anonymous_comments', '__return_true' ); 2489 2615 2490 $params 2616 $params = array( 2491 2617 'content' => 'Disco Stu likes disco music.', 2492 2618 ); 2619 2493 2620 $request = new WP_REST_Request( 'PUT', sprintf( '/wp/v2/comments/%d', self::$hold_id ) ); 2494 2621 $request->add_header( 'content-type', 'application/json' ); … … 2505 2632 wp_set_current_user( self::$moderator_id ); 2506 2633 2507 $request = new WP_REST_Request( 'PUT', sprintf( '/wp/v2/comments/%d', self::$approved_id ) ); 2508 $params = array( 2634 $params = array( 2509 2635 'content' => 'Updated comment.', 2510 2636 'date' => '2019-10-07T23:14:25', 2511 2637 ); 2638 2639 $request = new WP_REST_Request( 'PUT', sprintf( '/wp/v2/comments/%d', self::$approved_id ) ); 2512 2640 $request->add_header( 'content-type', 'application/json' ); 2513 2641 $request->set_body( wp_json_encode( $params ) ); … … 2535 2663 wp_set_current_user( self::$subscriber_id ); 2536 2664 2537 $params 2665 $params = array( 2538 2666 'content' => 'Disco Stu likes disco music.', 2539 2667 ); 2668 2540 2669 $request = new WP_REST_Request( 'PUT', sprintf( '/wp/v2/comments/%d', $private_comment_id ) ); 2541 2670 $request->add_header( 'content-type', 'application/json' ); … … 2548 2677 public function test_update_comment_with_children_link() { 2549 2678 wp_set_current_user( self::$admin_id ); 2679 2550 2680 $comment_id_1 = $this->factory->comment->create( 2551 2681 array( … … 2590 2720 wp_set_current_user( self::$admin_id ); 2591 2721 2592 $params 2722 $params = array( 2593 2723 'author_name' => rand_long_str( 246 ), 2594 2724 'content' => 'This isn\'t a saxophone. It\'s an umbrella.', 2595 2725 ); 2726 2596 2727 $request = new WP_REST_Request( 'PUT', sprintf( '/wp/v2/comments/%d', self::$approved_id ) ); 2597 2728 … … 2609 2740 wp_set_current_user( self::$admin_id ); 2610 2741 2611 $params 2742 $params = array( 2612 2743 'author_email' => 'murphy@' . rand_long_str( 190 ) . '.com', 2613 2744 'content' => 'This isn\'t a saxophone. It\'s an umbrella.', 2614 2745 ); 2746 2615 2747 $request = new WP_REST_Request( 'PUT', sprintf( '/wp/v2/comments/%d', self::$approved_id ) ); 2616 2748 … … 2628 2760 wp_set_current_user( self::$admin_id ); 2629 2761 2630 $params 2762 $params = array( 2631 2763 'author_url' => 'http://jazz.' . rand_long_str( 185 ) . '.com', 2632 2764 'content' => 'This isn\'t a saxophone. It\'s an umbrella.', 2633 2765 ); 2766 2634 2767 $request = new WP_REST_Request( 'PUT', sprintf( '/wp/v2/comments/%d', self::$approved_id ) ); 2635 2768 … … 2647 2780 wp_set_current_user( self::$admin_id ); 2648 2781 2649 $params 2782 $params = array( 2650 2783 'content' => rand_long_str( 66525 ), 2651 2784 ); 2785 2652 2786 $request = new WP_REST_Request( 'PUT', sprintf( '/wp/v2/comments/%d', self::$approved_id ) ); 2653 2787 … … 2712 2846 public function test_comment_roundtrip_as_editor() { 2713 2847 wp_set_current_user( self::$editor_id ); 2848 2714 2849 $this->assertEquals( ! is_multisite(), current_user_can( 'unfiltered_html' ) ); 2715 2850 $this->verify_comment_roundtrip( … … 2732 2867 public function test_comment_roundtrip_as_editor_unfiltered_html() { 2733 2868 wp_set_current_user( self::$editor_id ); 2869 2734 2870 if ( is_multisite() ) { 2735 2871 $this->assertFalse( current_user_can( 'unfiltered_html' ) ); … … 2771 2907 public function test_comment_roundtrip_as_superadmin() { 2772 2908 wp_set_current_user( self::$superadmin_id ); 2909 2773 2910 $this->assertTrue( current_user_can( 'unfiltered_html' ) ); 2774 2911 $this->verify_comment_roundtrip( … … 2791 2928 public function test_comment_roundtrip_as_superadmin_unfiltered_html() { 2792 2929 wp_set_current_user( self::$superadmin_id ); 2930 2793 2931 $this->assertTrue( current_user_can( 'unfiltered_html' ) ); 2794 2932 $this->verify_comment_roundtrip( … … 2832 2970 wp_set_current_user( self::$admin_id ); 2833 2971 2834 $comment_id 2972 $comment_id = $this->factory->comment->create( 2835 2973 array( 2836 2974 'comment_approved' => 1, … … 2839 2977 ) 2840 2978 ); 2979 2841 2980 $request = new WP_REST_Request( 'DELETE', sprintf( '/wp/v2/comments/%d', $comment_id ) ); 2842 2981 $request['force'] = true; … … 2859 2998 ) 2860 2999 ); 2861 $request = new WP_REST_Request( 'DELETE', sprintf( '/wp/v2/comments/%d', $comment_id ) ); 2862 $response = rest_get_server()->dispatch( $request ); 3000 3001 $request = new WP_REST_Request( 'DELETE', sprintf( '/wp/v2/comments/%d', $comment_id ) ); 3002 $response = rest_get_server()->dispatch( $request ); 2863 3003 $this->assertEquals( 200, $response->get_status() ); 2864 3004 $data = $response->get_data(); … … 2870 3010 wp_set_current_user( self::$admin_id ); 2871 3011 2872 $request = new WP_REST_Request( 'DELETE', sprintf( '/wp/v2/comments/%d', REST_TESTS_IMPOSSIBLY_HIGH_NUMBER ) ); 2873 3012 $request = new WP_REST_Request( 'DELETE', sprintf( '/wp/v2/comments/%d', REST_TESTS_IMPOSSIBLY_HIGH_NUMBER ) ); 2874 3013 $response = rest_get_server()->dispatch( $request ); 2875 3014 $this->assertErrorResponse( 'rest_comment_invalid_id', $response, 404 ); … … 2879 3018 wp_set_current_user( self::$subscriber_id ); 2880 3019 2881 $request = new WP_REST_Request( 'DELETE', sprintf( '/wp/v2/comments/%d', self::$approved_id ) ); 2882 3020 $request = new WP_REST_Request( 'DELETE', sprintf( '/wp/v2/comments/%d', self::$approved_id ) ); 2883 3021 $response = rest_get_server()->dispatch( $request ); 2884 3022 $this->assertErrorResponse( 'rest_cannot_delete', $response, 403 ); … … 2887 3025 public function test_delete_child_comment_link() { 2888 3026 wp_set_current_user( self::$admin_id ); 3027 2889 3028 $comment_id_1 = $this->factory->comment->create( 2890 3029 array( … … 2948 3087 public function test_get_item_schema_show_avatar() { 2949 3088 update_option( 'show_avatars', false ); 3089 2950 3090 $request = new WP_REST_Request( 'OPTIONS', '/wp/v2/users' ); 2951 3091 $response = rest_get_server()->dispatch( $request ); … … 2975 3115 ); 2976 3116 2977 $request = new WP_REST_Request( 'OPTIONS', '/wp/v2/comments' ); 2978 3117 $request = new WP_REST_Request( 'OPTIONS', '/wp/v2/comments' ); 2979 3118 $response = rest_get_server()->dispatch( $request ); 2980 3119 $data = $response->get_data(); … … 2983 3122 $this->assertEquals( $schema, $data['schema']['properties']['my_custom_int'] ); 2984 3123 2985 $request = new WP_REST_Request( 'GET', '/wp/v2/comments/' . self::$approved_id ); 2986 3124 $request = new WP_REST_Request( 'GET', '/wp/v2/comments/' . self::$approved_id ); 2987 3125 $response = rest_get_server()->dispatch( $request ); 2988 3126 $this->assertArrayHasKey( 'my_custom_int', $response->data ); -
trunk/tests/phpunit/tests/rest-api/rest-pages-controller.php
r46586 r46657 126 126 ) 127 127 ); 128 // No parent 128 129 // No parent. 129 130 $request = new WP_REST_Request( 'GET', '/wp/v2/pages' ); 130 131 $response = rest_get_server()->dispatch( $request ); 131 132 $data = $response->get_data(); 132 133 $this->assertEquals( 2, count( $data ) ); 133 // Filter to parent 134 135 // Filter to parent. 134 136 $request->set_param( 'parent', $id1 ); 135 137 $response = rest_get_server()->dispatch( $request ); … … 137 139 $this->assertEquals( 1, count( $data ) ); 138 140 $this->assertEquals( $id2, $data[0]['id'] ); 139 // Invalid parent should fail 141 142 // Invalid 'parent' should error. 140 143 $request->set_param( 'parent', 'some-slug' ); 141 144 $response = rest_get_server()->dispatch( $request ); … … 170 173 ) 171 174 ); 172 // No parent 175 176 // No parent. 173 177 $request = new WP_REST_Request( 'GET', '/wp/v2/pages' ); 174 178 $response = rest_get_server()->dispatch( $request ); 175 179 $data = $response->get_data(); 176 180 $this->assertEquals( 4, count( $data ) ); 177 // Filter to parents 181 182 // Filter to parents. 178 183 $request->set_param( 'parent', array( $id1, $id3 ) ); 179 184 $response = rest_get_server()->dispatch( $request ); … … 197 202 ) 198 203 ); 199 // No parent 204 205 // No parent. 200 206 $request = new WP_REST_Request( 'GET', '/wp/v2/pages' ); 201 207 $response = rest_get_server()->dispatch( $request ); 202 208 $data = $response->get_data(); 203 209 $this->assertEquals( 2, count( $data ) ); 204 // Filter to parent 210 211 // Filter to parent. 205 212 $request->set_param( 'parent_exclude', $id1 ); 206 213 $response = rest_get_server()->dispatch( $request ); … … 208 215 $this->assertEquals( 1, count( $data ) ); 209 216 $this->assertEquals( $id1, $data[0]['id'] ); 210 // Invalid parent_exclude should error 217 218 // Invalid 'parent_exclude' should error. 211 219 $request->set_param( 'parent_exclude', 'some-slug' ); 212 220 $response = rest_get_server()->dispatch( $request ); … … 242 250 ) 243 251 ); 244 // No parent 252 253 // No parent. 245 254 $request = new WP_REST_Request( 'GET', '/wp/v2/pages' ); 246 255 $response = rest_get_server()->dispatch( $request ); 247 256 $data = $response->get_data(); 248 257 $this->assertEqualSets( array( $id1, $id2, $id3, $id4 ), wp_list_pluck( $data, 'id' ) ); 249 // Filter to menu_order 258 259 // Filter to 'menu_order'. 250 260 $request->set_param( 'menu_order', 1 ); 251 261 $response = rest_get_server()->dispatch( $request ); 252 262 $data = $response->get_data(); 253 263 $this->assertEqualSets( array( $id4 ), wp_list_pluck( $data, 'id' ) ); 254 // Order by menu order 264 265 // Order by 'menu order'. 255 266 $request = new WP_REST_Request( 'GET', '/wp/v2/pages' ); 256 267 $request->set_param( 'order', 'asc' ); … … 262 273 $this->assertEquals( $id2, $data[2]['id'] ); 263 274 $this->assertEquals( $id3, $data[3]['id'] ); 264 // Invalid menu_order should fail 275 276 // Invalid 'menu_order' should error. 265 277 $request = new WP_REST_Request( 'GET', '/wp/v2/pages' ); 266 278 $request->set_param( 'menu_order', 'top-first' ); -
trunk/tests/phpunit/tests/rest-api/rest-posts-controller.php
r46648 r46657 20 20 21 21 protected static $supported_formats; 22 protected static $post_ids = array(); 23 protected static $total_posts = 30; 24 protected static $per_page = 50; 22 25 23 26 protected $forbidden_cat; … … 62 65 self::$supported_formats = get_theme_support( 'post-formats' ); 63 66 add_theme_support( 'post-formats', array( 'post', 'gallery' ) ); 67 68 // Set up posts for pagination tests. 69 for ( $i = 0; $i < self::$total_posts - 1; $i++ ) { 70 self::$post_ids[] = $factory->post->create( 71 array( 72 'post_title' => "Post {$i}", 73 ) 74 ); 75 } 64 76 } 65 77 … … 70 82 } else { 71 83 remove_theme_support( 'post-formats' ); 84 } 85 86 // Remove posts for pagination tests. 87 foreach ( self::$post_ids as $post_id ) { 88 wp_delete_post( $post_id, true ); 72 89 } 73 90 … … 204 221 205 222 wp_set_current_user( self::$editor_id ); 223 206 224 $request = new WP_REST_Request( 'OPTIONS', sprintf( '/wp/v2/posts/%d', self::$post_id ) ); 207 225 $response = rest_get_server()->dispatch( $request ); … … 241 259 $this->factory->post->create( array( 'post_author' => self::$editor_id ) ); 242 260 $this->factory->post->create( array( 'post_author' => self::$author_id ) ); 243 // All 3 posts 244 $request = new WP_REST_Request( 'GET', '/wp/v2/posts' ); 261 262 $total_posts = self::$total_posts + 2; 263 264 // All posts in the database. 265 $request = new WP_REST_Request( 'GET', '/wp/v2/posts' ); 266 $request->set_param( 'per_page', self::$per_page ); 245 267 $response = rest_get_server()->dispatch( $request ); 246 268 $this->assertEquals( 200, $response->get_status() ); 247 $this->assertEquals( 3, count( $response->get_data() ) ); 248 // 2 of 3 posts 269 $this->assertEquals( $total_posts, count( $response->get_data() ) ); 270 271 // Limit to editor and author. 249 272 $request = new WP_REST_Request( 'GET', '/wp/v2/posts' ); 250 273 $request->set_param( 'author', array( self::$editor_id, self::$author_id ) ); … … 254 277 $this->assertEquals( 2, count( $data ) ); 255 278 $this->assertEqualSets( array( self::$editor_id, self::$author_id ), wp_list_pluck( $data, 'author' ) ); 256 // 1 of 3 posts 279 280 // Limit to editor. 257 281 $request = new WP_REST_Request( 'GET', '/wp/v2/posts' ); 258 282 $request->set_param( 'author', self::$editor_id ); … … 267 291 $this->factory->post->create( array( 'post_author' => self::$editor_id ) ); 268 292 $this->factory->post->create( array( 'post_author' => self::$author_id ) ); 269 // All 3 posts 270 $request = new WP_REST_Request( 'GET', '/wp/v2/posts' ); 293 294 $total_posts = self::$total_posts + 2; 295 296 // All posts in the database. 297 $request = new WP_REST_Request( 'GET', '/wp/v2/posts' ); 298 $request->set_param( 'per_page', self::$per_page ); 271 299 $response = rest_get_server()->dispatch( $request ); 272 300 $this->assertEquals( 200, $response->get_status() ); 273 $this->assertEquals( 3, count( $response->get_data() ) ); 274 // 1 of 3 posts 275 $request = new WP_REST_Request( 'GET', '/wp/v2/posts' ); 301 $this->assertEquals( $total_posts, count( $response->get_data() ) ); 302 303 // Exclude editor and author. 304 $request = new WP_REST_Request( 'GET', '/wp/v2/posts' ); 305 $request->set_param( 'per_page', self::$per_page ); 276 306 $request->set_param( 'author_exclude', array( self::$editor_id, self::$author_id ) ); 277 307 $response = rest_get_server()->dispatch( $request ); 278 308 $this->assertEquals( 200, $response->get_status() ); 279 309 $data = $response->get_data(); 280 $this->assertEquals( 1, count( $data ) );310 $this->assertEquals( $total_posts - 2, count( $data ) ); 281 311 $this->assertNotEquals( self::$editor_id, $data[0]['author'] ); 282 312 $this->assertNotEquals( self::$author_id, $data[0]['author'] ); 283 // 2 of 3 posts 284 $request = new WP_REST_Request( 'GET', '/wp/v2/posts' ); 313 314 // Exclude editor. 315 $request = new WP_REST_Request( 'GET', '/wp/v2/posts' ); 316 $request->set_param( 'per_page', self::$per_page ); 285 317 $request->set_param( 'author_exclude', self::$editor_id ); 286 318 $response = rest_get_server()->dispatch( $request ); 287 319 $this->assertEquals( 200, $response->get_status() ); 288 320 $data = $response->get_data(); 289 $this->assertEquals( 2, count( $data ) );321 $this->assertEquals( $total_posts - 1, count( $data ) ); 290 322 $this->assertNotEquals( self::$editor_id, $data[0]['author'] ); 291 323 $this->assertNotEquals( self::$editor_id, $data[1]['author'] ); 292 // invalid author_exclude errors 324 325 // Invalid 'author_exclude' should error. 293 326 $request = new WP_REST_Request( 'GET', '/wp/v2/posts' ); 294 327 $request->set_param( 'author_exclude', 'invalid' ); … … 299 332 public function test_get_items_include_query() { 300 333 $id1 = $this->factory->post->create( array( 'post_status' => 'publish' ) ); 301 $this->factory->post->create( array( 'post_status' => 'publish' ) ); 302 $id3 = $this->factory->post->create( array( 'post_status' => 'publish' ) ); 303 $request = new WP_REST_Request( 'GET', '/wp/v2/posts' ); 304 // Orderby=>desc 305 $request->set_param( 'include', array( $id1, $id3 ) ); 334 $id2 = $this->factory->post->create( array( 'post_status' => 'publish' ) ); 335 336 $request = new WP_REST_Request( 'GET', '/wp/v2/posts' ); 337 338 // Order defaults to 'desc'. 339 $request->set_param( 'include', array( $id1, $id2 ) ); 306 340 $response = rest_get_server()->dispatch( $request ); 307 341 $data = $response->get_data(); 308 342 $this->assertEquals( 2, count( $data ) ); 309 $this->assertEquals( $id 3, $data[0]['id'] );343 $this->assertEquals( $id2, $data[0]['id'] ); 310 344 $this->assertPostsOrderedBy( '{posts}.post_date DESC' ); 311 // Orderby=>include 345 346 // 'orderby' => 'include' 312 347 $request->set_param( 'orderby', 'include' ); 313 348 $response = rest_get_server()->dispatch( $request ); … … 315 350 $this->assertEquals( 2, count( $data ) ); 316 351 $this->assertEquals( $id1, $data[0]['id'] ); 317 $this->assertPostsOrderedBy( "FIELD({posts}.ID,$id1,$id3)" ); 318 // Invalid include should error 352 $this->assertPostsOrderedBy( "FIELD({posts}.ID,$id1,$id2)" ); 353 354 // Invalid 'include' should error. 319 355 $request = new WP_REST_Request( 'GET', '/wp/v2/posts' ); 320 356 $request->set_param( 'include', 'invalid' ); … … 421 457 422 458 public function test_get_items_exclude_query() { 423 $id1 = $this->factory->post->create( array( 'post_status' => 'publish' ) ); 424 $id2 = $this->factory->post->create( array( 'post_status' => 'publish' ) ); 459 $id1 = $this->factory->post->create( array( 'post_status' => 'publish' ) ); 460 $id2 = $this->factory->post->create( array( 'post_status' => 'publish' ) ); 461 425 462 $request = new WP_REST_Request( 'GET', '/wp/v2/posts' ); 426 463 $response = rest_get_server()->dispatch( $request ); 427 464 $data = $response->get_data(); 428 $this->assertTrue( in_array( $id1, wp_list_pluck( $data, 'id' ), true ) ); 429 $this->assertTrue( in_array( $id2, wp_list_pluck( $data, 'id' ), true ) ); 465 $ids = wp_list_pluck( $data, 'id' ); 466 $this->assertTrue( in_array( $id1, $ids, true ) ); 467 $this->assertTrue( in_array( $id2, $ids, true ) ); 430 468 431 469 $request->set_param( 'exclude', array( $id2 ) ); 432 470 $response = rest_get_server()->dispatch( $request ); 433 471 $data = $response->get_data(); 434 $this->assertTrue( in_array( $id1, wp_list_pluck( $data, 'id' ), true ) ); 435 $this->assertFalse( in_array( $id2, wp_list_pluck( $data, 'id' ), true ) ); 472 $ids = wp_list_pluck( $data, 'id' ); 473 $this->assertTrue( in_array( $id1, $ids, true ) ); 474 $this->assertFalse( in_array( $id2, $ids, true ) ); 436 475 437 476 $request->set_param( 'exclude', "$id2" ); 438 477 $response = rest_get_server()->dispatch( $request ); 439 478 $data = $response->get_data(); 440 $this->assertTrue( in_array( $id1, wp_list_pluck( $data, 'id' ), true ) ); 441 $this->assertFalse( in_array( $id2, wp_list_pluck( $data, 'id' ), true ) ); 479 $ids = wp_list_pluck( $data, 'id' ); 480 $this->assertTrue( in_array( $id1, $ids, true ) ); 481 $this->assertFalse( in_array( $id2, $ids, true ) ); 442 482 443 483 $request->set_param( 'exclude', 'invalid' ); … … 447 487 448 488 public function test_get_items_search_query() { 449 for ( $i = 0; $i < 5; $i++ ) {450 $this->factory->post->create( array( 'post_status' => 'publish' ) );451 }452 489 $this->factory->post->create( 453 490 array( … … 456 493 ) 457 494 ); 458 $request = new WP_REST_Request( 'GET', '/wp/v2/posts' ); 459 $response = rest_get_server()->dispatch( $request ); 460 $this->assertEquals( 7, count( $response->get_data() ) ); 495 $total_posts = self::$total_posts + 1; 496 497 $request = new WP_REST_Request( 'GET', '/wp/v2/posts' ); 498 $request->set_param( 'per_page', self::$per_page ); 499 $response = rest_get_server()->dispatch( $request ); 500 $this->assertEquals( $total_posts, count( $response->get_data() ) ); 501 461 502 $request = new WP_REST_Request( 'GET', '/wp/v2/posts' ); 462 503 $request->set_param( 'search', 'Search Result' ); … … 480 521 ) 481 522 ); 523 482 524 $request = new WP_REST_Request( 'GET', '/wp/v2/posts' ); 483 525 $request->set_param( 'slug', 'apple' ); … … 508 550 ) 509 551 ); 552 510 553 $request = new WP_REST_Request( 'GET', '/wp/v2/posts' ); 511 554 $request->set_param( 'slug', array( 'banana', 'peach' ) ); … … 541 584 ) 542 585 ); 586 543 587 $request = new WP_REST_Request( 'GET', '/wp/v2/posts' ); 544 588 $request->set_param( 'slug', 'apple,banana' ); … … 557 601 public function test_get_items_status_query() { 558 602 wp_set_current_user( 0 ); 603 559 604 $this->factory->post->create( array( 'post_status' => 'draft' ) ); 560 $request = new WP_REST_Request( 'GET', '/wp/v2/posts' ); 605 606 $request = new WP_REST_Request( 'GET', '/wp/v2/posts' ); 607 $request->set_param( 'per_page', self::$per_page ); 561 608 $request->set_param( 'status', 'publish' ); 562 609 $response = rest_get_server()->dispatch( $request ); 563 610 $this->assertEquals( 200, $response->get_status() ); 564 $this->assertEquals( 1, count( $response->get_data() ) ); 611 $this->assertEquals( self::$total_posts, count( $response->get_data() ) ); 612 565 613 $request = new WP_REST_Request( 'GET', '/wp/v2/posts' ); 566 614 $request->set_param( 'status', 'draft' ); 567 615 $response = rest_get_server()->dispatch( $request ); 568 616 $this->assertErrorResponse( 'rest_invalid_param', $response, 400 ); 569 wp_set_current_user( self::$editor_id ); 617 618 wp_set_current_user( self::$editor_id ); 619 570 620 $request = new WP_REST_Request( 'GET', '/wp/v2/posts' ); 571 621 $request->set_param( 'status', 'draft' ); … … 636 686 637 687 wp_set_current_user( self::$private_reader_id ); 688 638 689 $request = new WP_REST_Request( 'GET', '/wp/v2/posts' ); 639 690 $request->set_param( 'status', array( 'private', 'future' ) ); … … 645 696 public function test_get_items_invalid_status_query() { 646 697 wp_set_current_user( 0 ); 698 647 699 $request = new WP_REST_Request( 'GET', '/wp/v2/posts' ); 648 700 $request->set_param( 'status', 'invalid' ); … … 657 709 ) 658 710 ); 711 659 712 wp_set_current_user( 0 ); 660 713 … … 695 748 ) 696 749 ); 750 697 751 $request = new WP_REST_Request( 'GET', '/wp/v2/posts' ); 698 752 $request->set_param( 'search', 'Apple' ); 699 // order defaults to 'desc' 753 754 // Order defaults to 'desc'. 700 755 $request->set_param( 'orderby', 'title' ); 701 756 $response = rest_get_server()->dispatch( $request ); … … 703 758 $this->assertEquals( 'Apple Sauce', $data[0]['title']['rendered'] ); 704 759 $this->assertPostsOrderedBy( '{posts}.post_title DESC' ); 705 // order=>asc 760 761 // 'order' => 'asc'. 706 762 $request->set_param( 'order', 'asc' ); 707 763 $response = rest_get_server()->dispatch( $request ); … … 709 765 $this->assertEquals( 'Apple Cobbler', $data[0]['title']['rendered'] ); 710 766 $this->assertPostsOrderedBy( '{posts}.post_title ASC' ); 711 // order=>asc,id should fail 767 768 // 'order' => 'asc,id' should error. 712 769 $request->set_param( 'order', 'asc,id' ); 713 770 $response = rest_get_server()->dispatch( $request ); 714 771 $this->assertErrorResponse( 'rest_invalid_param', $response, 400 ); 715 // orderby=>content should fail (invalid param test) 772 773 // 'orderby' => 'content' should error (invalid param test). 716 774 $request->set_param( 'order', 'asc' ); 717 775 $request->set_param( 'orderby', 'content' ); … … 722 780 public function test_get_items_with_orderby_include_without_include_param() { 723 781 $this->factory->post->create( array( 'post_status' => 'publish' ) ); 782 724 783 $request = new WP_REST_Request( 'GET', '/wp/v2/posts' ); 725 784 $request->set_param( 'orderby', 'include' ); … … 818 877 819 878 public function test_get_items_with_orderby_relevance() { 820 $id1 879 $id1 = $this->factory->post->create( 821 880 array( 822 881 'post_title' => 'Title is more relevant', … … 825 884 ) 826 885 ); 827 $id2 886 $id2 = $this->factory->post->create( 828 887 array( 829 888 'post_title' => 'Title is', … … 832 891 ) 833 892 ); 893 834 894 $request = new WP_REST_Request( 'GET', '/wp/v2/posts' ); 835 895 $request->set_param( 'orderby', 'relevance' ); … … 845 905 846 906 public function test_get_items_with_orderby_relevance_two_terms() { 847 $id1 907 $id1 = $this->factory->post->create( 848 908 array( 849 909 'post_title' => 'Title is more relevant', … … 852 912 ) 853 913 ); 854 $id2 914 $id2 = $this->factory->post->create( 855 915 array( 856 916 'post_title' => 'Title is', … … 859 919 ) 860 920 ); 921 861 922 $request = new WP_REST_Request( 'GET', '/wp/v2/posts' ); 862 923 $request->set_param( 'orderby', 'relevance' ); … … 879 940 880 941 public function test_get_items_offset_query() { 881 $id1 = self::$post_id; 882 $id2 = $this->factory->post->create( array( 'post_status' => 'publish' ) ); 883 $id3 = $this->factory->post->create( array( 'post_status' => 'publish' ) ); 884 $id4 = $this->factory->post->create( array( 'post_status' => 'publish' ) ); 885 $request = new WP_REST_Request( 'GET', '/wp/v2/posts' ); 942 $request = new WP_REST_Request( 'GET', '/wp/v2/posts' ); 943 $request->set_param( 'per_page', self::$per_page ); 886 944 $request->set_param( 'offset', 1 ); 887 945 $response = rest_get_server()->dispatch( $request ); 888 $this->assertCount( 3, $response->get_data() ); 889 // 'offset' works with 'per_page' 946 $this->assertCount( self::$total_posts - 1, $response->get_data() ); 947 948 // 'offset' works with 'per_page'. 890 949 $request->set_param( 'per_page', 2 ); 891 950 $response = rest_get_server()->dispatch( $request ); 892 951 $this->assertCount( 2, $response->get_data() ); 893 // 'offset' takes priority over 'page' 952 953 // 'offset' takes priority over 'page'. 894 954 $request->set_param( 'page', 2 ); 895 955 $response = rest_get_server()->dispatch( $request ); 896 956 $this->assertCount( 2, $response->get_data() ); 897 // Invalid 'offset' should error 957 958 // Invalid 'offset' should error. 898 959 $request->set_param( 'offset', 'moreplease' ); 899 960 $response = rest_get_server()->dispatch( $request ); … … 903 964 public function test_get_items_tags_query() { 904 965 $id1 = self::$post_id; 905 $id2 = $this->factory->post->create( array( 'post_status' => 'publish' ) );906 $id3 = $this->factory->post->create( array( 'post_status' => 'publish' ) );907 $id4 = $this->factory->post->create( array( 'post_status' => 'publish' ) );908 966 $tag = wp_insert_term( 'My Tag', 'post_tag' ); 909 967 910 968 wp_set_object_terms( $id1, array( $tag['term_id'] ), 'post_tag' ); 969 911 970 $request = new WP_REST_Request( 'GET', '/wp/v2/posts' ); 912 971 $request->set_param( 'tags', array( $tag['term_id'] ) ); … … 925 984 $tag = wp_insert_term( 'My Tag', 'post_tag' ); 926 985 986 $total_posts = self::$total_posts + 3; 987 927 988 wp_set_object_terms( $id1, array( $tag['term_id'] ), 'post_tag' ); 928 $request = new WP_REST_Request( 'GET', '/wp/v2/posts' ); 989 990 $request = new WP_REST_Request( 'GET', '/wp/v2/posts' ); 991 $request->set_param( 'per_page', self::$per_page ); 929 992 $request->set_param( 'tags_exclude', array( $tag['term_id'] ) ); 930 993 931 994 $response = rest_get_server()->dispatch( $request ); 932 995 $data = $response->get_data(); 933 $this->assertCount( 3, $data );996 $this->assertCount( $total_posts - 1, $data ); 934 997 $this->assertEquals( $id4, $data[0]['id'] ); 935 998 $this->assertEquals( $id3, $data[1]['id'] ); … … 940 1003 $id1 = self::$post_id; 941 1004 $id2 = $this->factory->post->create( array( 'post_status' => 'publish' ) ); 942 $id3 = $this->factory->post->create( array( 'post_status' => 'publish' ) );943 $id4 = $this->factory->post->create( array( 'post_status' => 'publish' ) );944 1005 $tag = wp_insert_term( 'My Tag', 'post_tag' ); 945 1006 $category = wp_insert_term( 'My Category', 'category' ); … … 967 1028 $id1 = self::$post_id; 968 1029 $id2 = $this->factory->post->create( array( 'post_status' => 'publish' ) ); 969 $id3 = $this->factory->post->create( array( 'post_status' => 'publish' ) );970 $id4 = $this->factory->post->create( array( 'post_status' => 'publish' ) );971 1030 $tag = wp_insert_term( 'My Tag', 'post_tag' ); 972 1031 $category = wp_insert_term( 'My Category', 'category' ); … … 990 1049 public function test_get_items_tags_and_categories_exclude_query() { 991 1050 $id1 = self::$post_id; 1051 $id2 = $this->factory->post->create( array( 'post_status' => 'publish' ) ); 1052 $tag = wp_insert_term( 'My Tag', 'post_tag' ); 1053 $category = wp_insert_term( 'My Category', 'category' ); 1054 1055 wp_set_object_terms( $id1, array( $tag['term_id'] ), 'post_tag' ); 1056 wp_set_object_terms( $id2, array( $tag['term_id'] ), 'post_tag' ); 1057 wp_set_object_terms( $id1, array( $category['term_id'] ), 'category' ); 1058 1059 $request = new WP_REST_Request( 'GET', '/wp/v2/posts' ); 1060 $request->set_param( 'tags', array( $tag['term_id'] ) ); 1061 $request->set_param( 'categories_exclude', array( $category['term_id'] ) ); 1062 1063 $response = rest_get_server()->dispatch( $request ); 1064 $data = $response->get_data(); 1065 $this->assertCount( 1, $data ); 1066 $this->assertEquals( $id2, $data[0]['id'] ); 1067 1068 $request->set_param( 'tags_exclude', array( 'my-tag' ) ); 1069 $response = rest_get_server()->dispatch( $request ); 1070 $this->assertErrorResponse( 'rest_invalid_param', $response, 400 ); 1071 } 1072 1073 /** 1074 * @ticket 44326 1075 */ 1076 public function test_get_items_tags_or_categories_exclude_query() { 1077 $id1 = end( self::$post_ids ); 992 1078 $id2 = $this->factory->post->create( array( 'post_status' => 'publish' ) ); 993 1079 $id3 = $this->factory->post->create( array( 'post_status' => 'publish' ) ); … … 996 1082 $category = wp_insert_term( 'My Category', 'category' ); 997 1083 998 wp_set_object_terms( $id1, array( $tag['term_id'] ), 'post_tag' ); 999 wp_set_object_terms( $id2, array( $tag['term_id'] ), 'post_tag' ); 1000 wp_set_object_terms( $id1, array( $category['term_id'] ), 'category' ); 1001 1002 $request = new WP_REST_Request( 'GET', '/wp/v2/posts' ); 1003 $request->set_param( 'tags', array( $tag['term_id'] ) ); 1004 $request->set_param( 'categories_exclude', array( $category['term_id'] ) ); 1005 1006 $response = rest_get_server()->dispatch( $request ); 1007 $data = $response->get_data(); 1008 $this->assertCount( 1, $data ); 1009 $this->assertEquals( $id2, $data[0]['id'] ); 1010 1011 $request->set_param( 'tags_exclude', array( 'my-tag' ) ); 1012 $response = rest_get_server()->dispatch( $request ); 1013 $this->assertErrorResponse( 'rest_invalid_param', $response, 400 ); 1014 } 1015 1016 /** 1017 * @ticket 44326 1018 */ 1019 public function test_get_items_tags_or_categories_exclude_query() { 1020 $id1 = self::$post_id; 1021 $id2 = $this->factory->post->create( array( 'post_status' => 'publish' ) ); 1022 $id3 = $this->factory->post->create( array( 'post_status' => 'publish' ) ); 1023 $id4 = $this->factory->post->create( array( 'post_status' => 'publish' ) ); 1024 $tag = wp_insert_term( 'My Tag', 'post_tag' ); 1025 $category = wp_insert_term( 'My Category', 'category' ); 1084 $total_posts = self::$total_posts + 3; 1026 1085 1027 1086 wp_set_object_terms( $id1, array( $tag['term_id'] ), 'post_tag' ); … … 1031 1090 1032 1091 $request = new WP_REST_Request( 'GET', '/wp/v2/posts' ); 1092 $request->set_param( 'per_page', self::$per_page ); 1033 1093 $request->set_param( 'tags', array( $tag['term_id'] ) ); 1034 1094 $request->set_param( 'categories_exclude', array( $category['term_id'] ) ); … … 1038 1098 $response = rest_get_server()->dispatch( $request ); 1039 1099 $data = $response->get_data(); 1040 $this->assertCount( 3, $data );1100 $this->assertCount( $total_posts - 1, $data ); 1041 1101 $this->assertEquals( $id4, $data[0]['id'] ); 1042 1102 $this->assertEquals( $id2, $data[1]['id'] ); … … 1082 1142 $id1 = self::$post_id; 1083 1143 $id2 = $this->factory->post->create( array( 'post_status' => 'publish' ) ); 1084 $id3 = $this->factory->post->create( array( 'post_status' => 'publish' ) );1085 1144 1086 1145 update_option( 'sticky_posts', array( $id2 ) ); … … 1117 1176 1118 1177 public function test_get_items_sticky_no_sticky_posts() { 1119 $id1 = self::$post_id;1120 1121 1178 update_option( 'sticky_posts', array() ); 1122 1179 … … 1154 1211 1155 1212 public function test_get_items_not_sticky() { 1156 $id1 = self::$post_id;1213 $id1 = end( self::$post_ids ); 1157 1214 $id2 = $this->factory->post->create( array( 'post_status' => 'publish' ) ); 1158 1215 1216 $total_posts = self::$total_posts + 1; 1217 1159 1218 update_option( 'sticky_posts', array( $id2 ) ); 1160 1219 1161 1220 $request = new WP_REST_Request( 'GET', '/wp/v2/posts' ); 1221 $request->set_param( 'per_page', self::$per_page ); 1162 1222 $request->set_param( 'sticky', false ); 1163 1223 1164 1224 $response = rest_get_server()->dispatch( $request ); 1165 $this->assertCount( 1, $response->get_data() );1225 $this->assertCount( $total_posts - 1, $response->get_data() ); 1166 1226 1167 1227 $posts = $response->get_data(); … … 1173 1233 1174 1234 public function test_get_items_not_sticky_with_exclude() { 1175 $id1 = self::$post_id;1235 $id1 = end( self::$post_ids ); 1176 1236 $id2 = $this->factory->post->create( array( 'post_status' => 'publish' ) ); 1177 1237 $id3 = $this->factory->post->create( array( 'post_status' => 'publish' ) ); 1178 1238 1239 $total_posts = self::$total_posts + 2; 1240 1179 1241 update_option( 'sticky_posts', array( $id2 ) ); 1180 1242 1181 1243 $request = new WP_REST_Request( 'GET', '/wp/v2/posts' ); 1244 $request->set_param( 'per_page', self::$per_page ); 1182 1245 $request->set_param( 'sticky', false ); 1183 1246 $request->set_param( 'exclude', array( $id3 ) ); 1184 1247 1185 1248 $response = rest_get_server()->dispatch( $request ); 1186 $this->assertCount( 1, $response->get_data() );1249 $this->assertCount( $total_posts - 2, $response->get_data() ); 1187 1250 1188 1251 $posts = $response->get_data(); 1189 $post = $posts[0]; 1190 $this->assertEquals( $id1, $post['id'] ); 1252 $ids = wp_list_pluck( $posts, 'id' ); 1253 $this->assertTrue( in_array( $id1, $ids, true ) ); 1254 $this->assertFalse( in_array( $id2, $ids, true ) ); 1255 $this->assertFalse( in_array( $id3, $ids, true ) ); 1191 1256 1192 1257 $this->assertPostsWhere( " AND {posts}.ID NOT IN ($id3,$id2) AND {posts}.post_type = 'post' AND (({posts}.post_status = 'publish'))" ); … … 1198 1263 $id3 = $this->factory->post->create( array( 'post_status' => 'publish' ) ); 1199 1264 1265 $total_posts = self::$total_posts + 2; 1266 1200 1267 update_option( 'sticky_posts', array() ); 1201 1268 1202 1269 $request = new WP_REST_Request( 'GET', '/wp/v2/posts' ); 1270 $request->set_param( 'per_page', self::$per_page ); 1203 1271 $request->set_param( 'sticky', false ); 1204 1272 $request->set_param( 'exclude', array( $id3 ) ); 1205 1273 1206 1274 $response = rest_get_server()->dispatch( $request ); 1207 $this->assertCount( 2, $response->get_data() );1275 $this->assertCount( $total_posts - 1, $response->get_data() ); 1208 1276 1209 1277 $posts = $response->get_data(); 1210 1278 $ids = wp_list_pluck( $posts, 'id' ); 1211 sort( $ids ); 1212 $this->assertEquals( array( $id1, $id2 ), $ids ); 1279 $this->assertTrue( in_array( $id1, $ids, true ) ); 1280 $this->assertTrue( in_array( $id2, $ids, true ) ); 1281 $this->assertFalse( in_array( $id3, $ids, true ) ); 1213 1282 1214 1283 $this->assertPostsWhere( " AND {posts}.ID NOT IN ($id3) AND {posts}.post_type = 'post' AND (({posts}.post_status = 'publish'))" ); … … 1216 1285 1217 1286 public function test_get_items_pagination_headers() { 1218 // Start of the index 1219 for ( $i = 0; $i < 49; $i++ ) { 1220 $this->factory->post->create( 1221 array( 1222 'post_title' => "Post {$i}", 1223 ) 1224 ); 1225 } 1287 $total_posts = self::$total_posts; 1288 $total_pages = (int) ceil( $total_posts / 10 ); 1289 1290 // Start of the index. 1226 1291 $request = new WP_REST_Request( 'GET', '/wp/v2/posts' ); 1227 1292 $response = rest_get_server()->dispatch( $request ); 1228 1293 $headers = $response->get_headers(); 1229 $this->assertEquals( 50, $headers['X-WP-Total'] );1230 $this->assertEquals( 5, $headers['X-WP-TotalPages'] );1294 $this->assertEquals( $total_posts, $headers['X-WP-Total'] ); 1295 $this->assertEquals( $total_pages, $headers['X-WP-TotalPages'] ); 1231 1296 $next_link = add_query_arg( 1232 1297 array( … … 1237 1302 $this->assertFalse( stripos( $headers['Link'], 'rel="prev"' ) ); 1238 1303 $this->assertContains( '<' . $next_link . '>; rel="next"', $headers['Link'] ); 1239 // 3rd page 1240 $this->factory->post->create( 1241 array( 1242 'post_title' => 'Post 51', 1243 ) 1244 ); 1304 1305 // 3rd page. 1306 $this->factory->post->create(); 1307 $total_posts++; 1308 $total_pages++; 1245 1309 $request = new WP_REST_Request( 'GET', '/wp/v2/posts' ); 1246 1310 $request->set_param( 'page', 3 ); 1247 1311 $response = rest_get_server()->dispatch( $request ); 1248 1312 $headers = $response->get_headers(); 1249 $this->assertEquals( 51, $headers['X-WP-Total'] );1250 $this->assertEquals( 6, $headers['X-WP-TotalPages'] );1313 $this->assertEquals( $total_posts, $headers['X-WP-Total'] ); 1314 $this->assertEquals( $total_pages, $headers['X-WP-TotalPages'] ); 1251 1315 $prev_link = add_query_arg( 1252 1316 array( … … 1263 1327 ); 1264 1328 $this->assertContains( '<' . $next_link . '>; rel="next"', $headers['Link'] ); 1265 // Last page 1266 $request = new WP_REST_Request( 'GET', '/wp/v2/posts' ); 1267 $request->set_param( 'page', 6 ); 1329 1330 // Last page. 1331 $request = new WP_REST_Request( 'GET', '/wp/v2/posts' ); 1332 $request->set_param( 'page', $total_pages ); 1268 1333 $response = rest_get_server()->dispatch( $request ); 1269 1334 $headers = $response->get_headers(); 1270 $this->assertEquals( 51, $headers['X-WP-Total'] );1271 $this->assertEquals( 6, $headers['X-WP-TotalPages'] );1335 $this->assertEquals( $total_posts, $headers['X-WP-Total'] ); 1336 $this->assertEquals( $total_pages, $headers['X-WP-TotalPages'] ); 1272 1337 $prev_link = add_query_arg( 1273 1338 array( 1274 'page' => 5,1339 'page' => $total_pages - 1, 1275 1340 ), 1276 1341 rest_url( '/wp/v2/posts' ) … … 1279 1344 $this->assertFalse( stripos( $headers['Link'], 'rel="next"' ) ); 1280 1345 1281 // Out of bounds 1282 $request = new WP_REST_Request( 'GET', '/wp/v2/posts' ); 1283 $request->set_param( 'page', 8);1346 // Out of bounds. 1347 $request = new WP_REST_Request( 'GET', '/wp/v2/posts' ); 1348 $request->set_param( 'page', 100 ); 1284 1349 $response = rest_get_server()->dispatch( $request ); 1285 1350 $headers = $response->get_headers(); … … 1287 1352 1288 1353 // With query params. 1289 $request = new WP_REST_Request( 'GET', '/wp/v2/posts' ); 1354 $total_pages = (int) ceil( $total_posts / 5 ); 1355 $request = new WP_REST_Request( 'GET', '/wp/v2/posts' ); 1290 1356 $request->set_query_params( 1291 1357 array( … … 1296 1362 $response = rest_get_server()->dispatch( $request ); 1297 1363 $headers = $response->get_headers(); 1298 $this->assertEquals( 51, $headers['X-WP-Total'] );1299 $this->assertEquals( 11, $headers['X-WP-TotalPages'] );1364 $this->assertEquals( $total_posts, $headers['X-WP-Total'] ); 1365 $this->assertEquals( $total_pages, $headers['X-WP-TotalPages'] ); 1300 1366 $prev_link = add_query_arg( 1301 1367 array( … … 1321 1387 // Drafts status query var inaccessible to unauthorized users. 1322 1388 wp_set_current_user( 0 ); 1389 1323 1390 $request = new WP_REST_Request( 'GET', '/wp/v2/posts' ); 1324 1391 $request->set_param( 'status', 'draft' ); … … 1328 1395 // Users with 'read_private_posts' cap shouldn't also be able to view drafts. 1329 1396 wp_set_current_user( self::$private_reader_id ); 1397 1330 1398 $request = new WP_REST_Request( 'GET', '/wp/v2/posts' ); 1331 1399 $request->set_param( 'status', 'draft' ); … … 1335 1403 // But drafts are accessible to authorized users. 1336 1404 wp_set_current_user( self::$editor_id ); 1337 $response = rest_get_server()->dispatch( $request ); 1338 $ data = $response->get_data();1339 1405 1406 $response = rest_get_server()->dispatch( $request ); 1407 $data = $response->get_data(); 1340 1408 $this->assertEquals( $draft_id, $data[0]['id'] ); 1341 1409 } … … 1348 1416 1349 1417 wp_set_current_user( 0 ); 1418 1350 1419 $request = new WP_REST_Request( 'GET', '/wp/v2/posts' ); 1351 1420 $request->set_param( 'status', 'private' ); … … 1354 1423 1355 1424 wp_set_current_user( self::$private_reader_id ); 1425 1356 1426 $request = new WP_REST_Request( 'GET', '/wp/v2/posts' ); 1357 1427 $request->set_param( 'status', 'private' ); … … 1521 1591 ) 1522 1592 ); 1593 1523 1594 wp_set_current_user( 0 ); 1524 1595 … … 1553 1624 public function test_get_post_list_context_without_permission() { 1554 1625 wp_set_current_user( 0 ); 1626 1555 1627 $request = new WP_REST_Request( 'GET', '/wp/v2/posts' ); 1556 1628 $request->set_query_params( … … 1566 1638 public function test_get_post_context_without_permission() { 1567 1639 wp_set_current_user( 0 ); 1640 1568 1641 $request = new WP_REST_Request( 'GET', sprintf( '/wp/v2/posts/%d', self::$post_id ) ); 1569 1642 $request->set_query_params( … … 1605 1678 ); 1606 1679 1607 $post = get_post( $post_id ); 1680 $post = get_post( $post_id ); 1681 1608 1682 $request = new WP_REST_Request( 'GET', sprintf( '/wp/v2/posts/%d', $post_id ) ); 1609 1683 $request->set_param( 'password', '$inthebananastand' ); … … 1626 1700 ); 1627 1701 1628 $post = get_post( $post_id ); 1702 $post = get_post( $post_id ); 1703 1629 1704 $request = new WP_REST_Request( 'GET', sprintf( '/wp/v2/posts/%d', $post_id ) ); 1630 1705 $request->set_param( 'password', 'wrongpassword' ); … … 1635 1710 1636 1711 public function test_get_post_with_password_without_permission() { 1637 $post_id 1712 $post_id = $this->factory->post->create( 1638 1713 array( 1639 1714 'post_password' => '$inthebananastand', … … 1642 1717 ) 1643 1718 ); 1719 1644 1720 $request = new WP_REST_Request( 'GET', sprintf( '/wp/v2/posts/%d', $post_id ) ); 1645 1721 $response = rest_get_server()->dispatch( $request ); … … 1704 1780 ) 1705 1781 ); 1782 1706 1783 $request = new WP_REST_Request( 'GET', sprintf( '/wp/v2/posts/%d', $post_id ) ); 1707 1784 $request->set_param( 'context', 'edit' ); … … 1714 1791 register_post_status( 'testpubstatus', array( 'public' => true ) ); 1715 1792 register_post_status( 'testprivtatus', array( 'public' => false ) ); 1716 // Public status 1793 1794 // Public status. 1717 1795 wp_update_post( 1718 1796 array( … … 1721 1799 ) 1722 1800 ); 1801 1723 1802 $request = new WP_REST_Request( 'GET', sprintf( '/wp/v2/posts/%d', self::$post_id ) ); 1724 1803 $response = rest_get_server()->dispatch( $request ); 1725 1804 $this->assertEquals( 200, $response->get_status() ); 1726 // Private status 1805 1806 // Private status. 1727 1807 wp_update_post( 1728 1808 array( … … 1731 1811 ) 1732 1812 ); 1813 1733 1814 $request = new WP_REST_Request( 'GET', sprintf( '/wp/v2/posts/%d', self::$post_id ) ); 1734 1815 $response = rest_get_server()->dispatch( $request ); … … 1748 1829 public function test_prepare_item_limit_fields() { 1749 1830 wp_set_current_user( self::$editor_id ); 1831 1750 1832 $endpoint = new WP_REST_Posts_Controller( 'post' ); 1751 1833 $request = new WP_REST_Request( 'GET', sprintf( '/wp/v2/posts/%d', self::$post_id ) ); … … 1775 1857 1776 1858 wp_set_current_user( self::$editor_id ); 1859 1777 1860 $endpoint = new WP_REST_Posts_Controller( 'post' ); 1778 $request = new WP_REST_R EQUEST( 'GET', sprintf( '/wp/v2/posts/%d', self::$post_id ) );1861 $request = new WP_REST_Request( 'GET', sprintf( '/wp/v2/posts/%d', self::$post_id ) ); 1779 1862 1780 1863 $request->set_param( 'context', 'edit' ); … … 1810 1893 1811 1894 wp_set_current_user( self::$editor_id ); 1895 1812 1896 $endpoint = new WP_REST_Posts_Controller( 'post' ); 1813 $request = new WP_REST_R EQUEST( 'GET', sprintf( '/wp/v2/posts/%d', self::$post_id ) );1897 $request = new WP_REST_Request( 'GET', sprintf( '/wp/v2/posts/%d', self::$post_id ) ); 1814 1898 1815 1899 $request->set_param( 'context', 'edit' ); … … 1920 2004 public function test_create_post_date( $status, $params, $results ) { 1921 2005 wp_set_current_user( self::$editor_id ); 2006 1922 2007 update_option( 'timezone_string', $params['timezone_string'] ); 1923 2008 … … 1953 2038 public function test_create_item_with_template() { 1954 2039 wp_set_current_user( self::$editor_id ); 2040 1955 2041 add_filter( 'theme_post_templates', array( $this, 'filter_theme_post_templates' ) ); 1956 2042 … … 2002 2088 public function test_create_item_with_template_none() { 2003 2089 wp_set_current_user( self::$editor_id ); 2090 2004 2091 add_filter( 'theme_post_templates', array( $this, 'filter_theme_post_templates' ) ); 2005 2092 update_post_meta( self::$post_id, '_wp_page_template', 'post-my-test-template.php' ); … … 2050 2137 public function test_create_post_as_contributor() { 2051 2138 wp_set_current_user( self::$contributor_id ); 2139 2052 2140 update_option( 'timezone_string', 'America/Chicago' ); 2053 2141 … … 2183 2271 public function test_create_post_private_without_permission() { 2184 2272 wp_set_current_user( self::$author_id ); 2273 2185 2274 $user = wp_get_current_user(); 2186 2275 $user->add_cap( 'publish_posts', false ); … … 2204 2293 public function test_create_post_publish_without_permission() { 2205 2294 wp_set_current_user( self::$author_id ); 2295 2206 2296 $user = wp_get_current_user(); 2207 2297 $user->add_cap( 'publish_posts', false ); … … 2409 2499 2410 2500 $data = $response->get_data(); 2411 2412 2501 $this->assertEquals( '0', $data['password'] ); 2413 2502 } … … 2552 2641 $request->set_body_params( $params ); 2553 2642 $response = rest_get_server()->dispatch( $request ); 2554 $new_data = $response->get_data(); 2555 $this->assertEquals( "Rob O'Rourke's Diary", $new_data['title']['raw'] ); 2643 2644 $data = $response->get_data(); 2645 $this->assertEquals( "Rob O'Rourke's Diary", $data['title']['raw'] ); 2556 2646 } 2557 2647 2558 2648 public function test_create_post_with_categories() { 2559 2649 wp_set_current_user( self::$editor_id ); 2650 2560 2651 $category = wp_insert_term( 'Test Category', 'category' ); 2561 $request = new WP_REST_Request( 'POST', '/wp/v2/posts' ); 2562 $params = $this->set_post_data( 2652 2653 $request = new WP_REST_Request( 'POST', '/wp/v2/posts' ); 2654 $params = $this->set_post_data( 2563 2655 array( 2564 2656 'password' => 'testing', … … 2577 2669 public function test_create_post_with_categories_as_csv() { 2578 2670 wp_set_current_user( self::$editor_id ); 2671 2579 2672 $category = wp_insert_term( 'Chicken', 'category' ); 2580 2673 $category2 = wp_insert_term( 'Ribs', 'category' ); 2581 $request = new WP_REST_Request( 'POST', '/wp/v2/posts' ); 2582 $params = $this->set_post_data( 2674 2675 $request = new WP_REST_Request( 'POST', '/wp/v2/posts' ); 2676 $params = $this->set_post_data( 2583 2677 array( 2584 2678 'categories' => $category['term_id'] . ',' . $category2['term_id'], … … 2594 2688 public function test_create_post_with_invalid_categories() { 2595 2689 wp_set_current_user( self::$editor_id ); 2690 2596 2691 $request = new WP_REST_Request( 'POST', '/wp/v2/posts' ); 2597 2692 $params = $this->set_post_data( … … 2618 2713 2619 2714 wp_set_current_user( self::$editor_id ); 2715 2620 2716 $request = new WP_REST_Request( 'POST', '/wp/v2/posts' ); 2621 2717 $params = $this->set_post_data( … … 2664 2760 public function test_update_item_no_change() { 2665 2761 wp_set_current_user( self::$editor_id ); 2762 2666 2763 $post = get_post( self::$post_id ); 2667 2764 … … 2709 2806 // Create a new test post. 2710 2807 $post_id = $this->factory->post->create(); 2808 2711 2809 wp_set_current_user( self::$editor_id ); 2712 2810 2713 2811 // Set the post date to the future. 2714 2812 $future_date = '2919-07-29T18:00:00'; 2715 $request = new WP_REST_Request( 'PUT', sprintf( '/wp/v2/posts/%d', $post_id ) ); 2813 2814 $request = new WP_REST_Request( 'PUT', sprintf( '/wp/v2/posts/%d', $post_id ) ); 2716 2815 $request->add_header( 'content-type', 'application/json' ); 2717 2816 $params = $this->set_post_data( … … 2797 2896 public function test_update_post_without_permission() { 2798 2897 wp_set_current_user( self::$editor_id ); 2898 2799 2899 $user = wp_get_current_user(); 2800 2900 $user->add_cap( 'edit_published_posts', false ); … … 2951 3051 public function test_update_post_date( $status, $params, $results ) { 2952 3052 wp_set_current_user( self::$editor_id ); 3053 2953 3054 update_option( 'timezone_string', $params['timezone_string'] ); 2954 3055 … … 3013 3114 3014 3115 wp_set_current_user( self::$editor_id ); 3116 3015 3117 update_option( 'timezone_string', 'America/Chicago' ); 3016 3118 … … 3191 3293 public function test_update_post_with_empty_password() { 3192 3294 wp_set_current_user( self::$editor_id ); 3295 3193 3296 wp_update_post( 3194 3297 array( … … 3281 3384 3282 3385 public function test_update_post_with_categories() { 3283 3284 wp_set_current_user( self::$editor_id ); 3386 wp_set_current_user( self::$editor_id ); 3387 3285 3388 $category = wp_insert_term( 'Test Category', 'category' ); 3286 3389 … … 3307 3410 $query = parse_url( $categories_path, PHP_URL_QUERY ); 3308 3411 parse_str( $query, $args ); 3412 3309 3413 $request = new WP_REST_Request( 'GET', $args['rest_route'] ); 3310 3414 unset( $args['rest_route'] ); … … 3317 3421 3318 3422 public function test_update_post_with_empty_categories() { 3319 3320 wp_set_current_user( self::$editor_id ); 3423 wp_set_current_user( self::$editor_id ); 3424 3321 3425 $category = wp_insert_term( 'Test Category', 'category' ); 3322 3426 wp_set_object_terms( self::$post_id, $category['term_id'], 'category' ); … … 3343 3447 3344 3448 wp_set_current_user( self::$editor_id ); 3449 3345 3450 $request = new WP_REST_Request( 'PUT', sprintf( '/wp/v2/posts/%d', self::$post_id ) ); 3346 3451 $params = $this->set_post_data( … … 3364 3469 public function test_update_item_with_template() { 3365 3470 wp_set_current_user( self::$editor_id ); 3471 3366 3472 add_filter( 'theme_post_templates', array( $this, 'filter_theme_post_templates' ) ); 3367 3473 … … 3393 3499 public function test_update_item_with_template_none() { 3394 3500 wp_set_current_user( self::$editor_id ); 3501 3395 3502 add_filter( 'theme_post_templates', array( $this, 'filter_theme_post_templates' ) ); 3396 3503 update_post_meta( self::$post_id, '_wp_page_template', 'post-my-test-template.php' ); … … 3425 3532 */ 3426 3533 public function test_update_item_with_same_template_that_no_longer_exists() { 3427 3428 3534 wp_set_current_user( self::$editor_id ); 3429 3535 … … 3598 3704 public function test_post_roundtrip_as_author( $raw, $expected ) { 3599 3705 wp_set_current_user( self::$author_id ); 3706 3600 3707 $this->assertFalse( current_user_can( 'unfiltered_html' ) ); 3601 3708 $this->verify_post_roundtrip( $raw, $expected ); … … 3604 3711 public function test_post_roundtrip_as_editor_unfiltered_html() { 3605 3712 wp_set_current_user( self::$editor_id ); 3713 3606 3714 if ( is_multisite() ) { 3607 3715 $this->assertFalse( current_user_can( 'unfiltered_html' ) ); … … 3655 3763 public function test_post_roundtrip_as_superadmin_unfiltered_html() { 3656 3764 wp_set_current_user( self::$superadmin_id ); 3765 3657 3766 $this->assertTrue( current_user_can( 'unfiltered_html' ) ); 3658 3767 $this->verify_post_roundtrip( … … 3681 3790 public function test_delete_item() { 3682 3791 $post_id = $this->factory->post->create( array( 'post_title' => 'Deleted post' ) ); 3792 3683 3793 wp_set_current_user( self::$editor_id ); 3684 3794 … … 3695 3805 public function test_delete_item_skip_trash() { 3696 3806 $post_id = $this->factory->post->create( array( 'post_title' => 'Deleted post' ) ); 3807 3697 3808 wp_set_current_user( self::$editor_id ); 3698 3809 … … 3709 3820 public function test_delete_item_already_trashed() { 3710 3821 $post_id = $this->factory->post->create( array( 'post_title' => 'Deleted post' ) ); 3711 wp_set_current_user( self::$editor_id ); 3822 3823 wp_set_current_user( self::$editor_id ); 3824 3712 3825 $request = new WP_REST_Request( 'DELETE', sprintf( '/wp/v2/posts/%d', $post_id ) ); 3713 3826 $response = rest_get_server()->dispatch( $request ); … … 3728 3841 public function test_delete_post_invalid_post_type() { 3729 3842 $page_id = $this->factory->post->create( array( 'post_type' => 'page' ) ); 3843 3730 3844 wp_set_current_user( self::$editor_id ); 3731 3845 … … 3946 4060 ); 3947 4061 3948 $request = new WP_REST_Request( 'OPTIONS', '/wp/v2/posts' ); 3949 4062 $request = new WP_REST_Request( 'OPTIONS', '/wp/v2/posts' ); 3950 4063 $response = rest_get_server()->dispatch( $request ); 3951 4064 $data = $response->get_data(); … … 3958 4071 $post_id = $this->factory->post->create(); 3959 4072 3960 $request = new WP_REST_Request( 'GET', '/wp/v2/posts/' . $post_id ); 3961 4073 $request = new WP_REST_Request( 'GET', '/wp/v2/posts/' . $post_id ); 3962 4074 $response = rest_get_server()->dispatch( $request ); 3963 4075 $this->assertArrayHasKey( 'my_custom_int', $response->data ); … … 4044 4156 4045 4157 wp_set_current_user( self::$editor_id ); 4158 4046 4159 // Check for error on update. 4047 4160 $request = new WP_REST_Request( 'POST', sprintf( '/wp/v2/posts/%d', self::$post_id ) ); … … 4072 4185 4073 4186 public function test_publish_action_ldo_registered() { 4074 4075 4187 $response = rest_get_server()->dispatch( new WP_REST_Request( 'OPTIONS', '/wp/v2/posts' ) ); 4076 4188 $data = $response->get_data(); … … 4084 4196 4085 4197 public function test_sticky_action_ldo_registered_for_posts() { 4086 4087 4198 $response = rest_get_server()->dispatch( new WP_REST_Request( 'OPTIONS', '/wp/v2/posts' ) ); 4088 4199 $data = $response->get_data(); … … 4096 4207 4097 4208 public function test_sticky_action_ldo_not_registered_for_non_posts() { 4098 4099 4209 $response = rest_get_server()->dispatch( new WP_REST_Request( 'OPTIONS', '/wp/v2/pages' ) ); 4100 4210 $data = $response->get_data(); … … 4108 4218 4109 4219 public function test_author_action_ldo_registered_for_post_types_with_author_support() { 4110 4111 4220 $response = rest_get_server()->dispatch( new WP_REST_Request( 'OPTIONS', '/wp/v2/posts' ) ); 4112 4221 $data = $response->get_data(); … … 4120 4229 4121 4230 public function test_author_action_ldo_not_registered_for_post_types_without_author_support() { 4122 4123 4231 remove_post_type_support( 'post', 'author' ); 4124 4232 … … 4140 4248 4141 4249 public function test_term_action_ldos_registered() { 4142 4143 4250 $response = rest_get_server()->dispatch( new WP_REST_Request( 'OPTIONS', '/wp/v2/posts' ) ); 4144 4251 $data = $response->get_data(); … … 4160 4267 4161 4268 public function test_action_links_only_available_in_edit_context() { 4162 4163 4269 wp_set_current_user( self::$author_id ); 4164 4270 … … 4176 4282 4177 4283 public function test_publish_action_link_exists_for_author() { 4178 4179 4284 wp_set_current_user( self::$author_id ); 4180 4285 … … 4192 4297 4193 4298 public function test_publish_action_link_does_not_exist_for_contributor() { 4194 4195 4299 wp_set_current_user( self::$contributor_id ); 4196 4300 … … 4208 4312 4209 4313 public function test_sticky_action_exists_for_editor() { 4210 4211 4314 wp_set_current_user( self::$editor_id ); 4212 4315 … … 4224 4327 4225 4328 public function test_sticky_action_does_not_exist_for_author() { 4226 4227 4329 wp_set_current_user( self::$author_id ); 4228 4330 … … 4240 4342 4241 4343 public function test_sticky_action_does_not_exist_for_non_post_posts() { 4242 4243 4344 wp_set_current_user( self::$editor_id ); 4244 4345 … … 4262 4363 4263 4364 public function test_assign_author_action_exists_for_editor() { 4264 4265 4365 wp_set_current_user( self::$editor_id ); 4266 4366 … … 4278 4378 4279 4379 public function test_assign_author_action_does_not_exist_for_author() { 4280 4281 4380 wp_set_current_user( self::$author_id ); 4282 4381 … … 4294 4393 4295 4394 public function test_assign_author_action_does_not_exist_for_post_types_without_author_support() { 4296 4297 4395 remove_post_type_support( 'post', 'author' ); 4298 4396 … … 4312 4410 4313 4411 public function test_create_term_action_exists_for_editor() { 4314 4315 4412 wp_set_current_user( self::$editor_id ); 4316 4413 … … 4330 4427 4331 4428 public function test_create_term_action_non_hierarchical_exists_for_author() { 4332 4333 4429 wp_set_current_user( self::$author_id ); 4334 4430 … … 4346 4442 4347 4443 public function test_create_term_action_hierarchical_does_not_exists_for_author() { 4348 4349 4444 wp_set_current_user( self::$author_id ); 4350 4445 … … 4362 4457 4363 4458 public function test_assign_term_action_exists_for_contributor() { 4364 4365 4459 wp_set_current_user( self::$contributor_id ); 4366 4460 … … 4385 4479 public function test_assign_unfiltered_html_action_superadmin() { 4386 4480 $post_id = self::factory()->post->create(); 4481 4387 4482 wp_set_current_user( self::$superadmin_id ); 4483 4388 4484 $request = new WP_REST_Request( 'GET', '/wp/v2/posts/' . $post_id ); 4389 4485 $request->set_param( 'context', 'edit' ); … … 4395 4491 public function test_assign_unfiltered_html_action_editor() { 4396 4492 $post_id = self::factory()->post->create(); 4397 wp_set_current_user( self::$editor_id ); 4493 4494 wp_set_current_user( self::$editor_id ); 4495 4398 4496 $request = new WP_REST_Request( 'GET', '/wp/v2/posts/' . $post_id ); 4399 4497 $request->set_param( 'context', 'edit' ); … … 4410 4508 public function test_assign_unfiltered_html_action_author() { 4411 4509 $post_id = self::factory()->post->create(); 4510 4412 4511 wp_set_current_user( self::$author_id ); 4512 4413 4513 $request = new WP_REST_Request( 'GET', '/wp/v2/posts/' . $post_id ); 4414 4514 $request->set_param( 'context', 'edit' ); … … 4491 4591 */ 4492 4592 public function test_putting_same_publish_date_does_not_remove_floating_date() { 4493 4494 4593 wp_set_current_user( self::$superadmin_id ); 4495 4594 … … 4527 4626 */ 4528 4627 public function test_putting_different_publish_date_removes_floating_date() { 4529 4530 4628 wp_set_current_user( self::$superadmin_id ); 4531 4629 … … 4570 4668 */ 4571 4669 public function test_publishing_post_with_same_date_removes_floating_date() { 4572 4573 4670 wp_set_current_user( self::$superadmin_id ); 4574 4671 -
trunk/tests/phpunit/tests/rest-api/rest-tags-controller.php
r46586 r46657 17 17 protected static $subscriber; 18 18 19 protected static $tag_ids = array(); 20 protected static $total_tags = 30; 21 protected static $per_page = 50; 22 19 23 public static function wpSetUpBeforeClass( $factory ) { 20 24 self::$superadmin = $factory->user->create( … … 44 48 ) 45 49 ); 50 46 51 if ( is_multisite() ) { 47 52 update_site_option( 'site_admins', array( 'superadmin' ) ); 53 } 54 55 // Set up tags for pagination tests. 56 for ( $i = 0; $i < self::$total_tags; $i++ ) { 57 $tag_ids[] = $factory->tag->create( 58 array( 59 'name' => "Tag {$i}", 60 ) 61 ); 48 62 } 49 63 } … … 54 68 self::delete_user( self::$editor ); 55 69 self::delete_user( self::$subscriber ); 70 71 // Remove tags for pagination tests. 72 foreach ( self::$tag_ids as $tag_id ) { 73 wp_delete_term( $tag_id, 'post_tag' ); 74 } 56 75 } 57 76 … … 155 174 public function test_get_items() { 156 175 $this->factory->tag->create(); 157 $request = new WP_REST_Request( 'GET', '/wp/v2/tags' ); 176 177 $request = new WP_REST_Request( 'GET', '/wp/v2/tags' ); 178 $request->set_param( 'per_page', self::$per_page ); 158 179 $response = rest_get_server()->dispatch( $request ); 159 180 $this->check_get_taxonomy_terms_response( $response ); … … 162 183 public function test_get_items_invalid_permission_for_context() { 163 184 wp_set_current_user( 0 ); 185 164 186 $request = new WP_REST_Request( 'GET', '/wp/v2/tags' ); 165 187 $request->set_param( 'context', 'edit' ); … … 172 194 $tag1 = $this->factory->tag->create( array( 'name' => 'Season 5' ) ); 173 195 $tag2 = $this->factory->tag->create( array( 'name' => 'The Be Sharps' ) ); 196 174 197 wp_set_object_terms( $post_id, array( $tag1, $tag2 ), 'post_tag' ); 198 175 199 $request = new WP_REST_Request( 'GET', '/wp/v2/tags' ); 176 200 $request->set_param( 'hide_empty', true ); … … 180 204 $this->assertEquals( 'Season 5', $data[0]['name'] ); 181 205 $this->assertEquals( 'The Be Sharps', $data[1]['name'] ); 182 // invalid value should fail 206 207 // Invalid 'hide_empty' should error. 183 208 $request->set_param( 'hide_empty', 'nothanks' ); 184 209 $response = rest_get_server()->dispatch( $request ); … … 187 212 188 213 public function test_get_items_include_query() { 189 $id1 = $this->factory->tag->create(); 190 $id2 = $this->factory->tag->create(); 191 $id3 = $this->factory->tag->create(); 192 $request = new WP_REST_Request( 'GET', '/wp/v2/tags' ); 193 // Orderby=>asc 194 $request->set_param( 'include', array( $id3, $id1 ) ); 214 $id1 = $this->factory->tag->create(); 215 $id2 = $this->factory->tag->create(); 216 217 $request = new WP_REST_Request( 'GET', '/wp/v2/tags' ); 218 219 // 'orderby' => 'asc'. 220 $request->set_param( 'include', array( $id2, $id1 ) ); 195 221 $response = rest_get_server()->dispatch( $request ); 196 222 $data = $response->get_data(); 197 223 $this->assertEquals( 2, count( $data ) ); 198 224 $this->assertEquals( $id1, $data[0]['id'] ); 199 // Orderby=>include 225 226 // 'orderby' => 'include'. 200 227 $request->set_param( 'orderby', 'include' ); 201 228 $response = rest_get_server()->dispatch( $request ); 202 229 $data = $response->get_data(); 203 230 $this->assertEquals( 2, count( $data ) ); 204 $this->assertEquals( $id3, $data[0]['id'] ); 205 // Include invalid value shoud fail 231 $this->assertEquals( $id2, $data[0]['id'] ); 232 233 // Invalid 'include' should error. 206 234 $request->set_param( 'include', array( 'myterm' ) ); 207 235 $response = rest_get_server()->dispatch( $request ); … … 210 238 211 239 public function test_get_items_exclude_query() { 212 $id1 = $this->factory->tag->create(); 213 $id2 = $this->factory->tag->create(); 214 $request = new WP_REST_Request( 'GET', '/wp/v2/tags' ); 240 $id1 = $this->factory->tag->create(); 241 $id2 = $this->factory->tag->create(); 242 243 $request = new WP_REST_Request( 'GET', '/wp/v2/tags' ); 244 $request->set_param( 'per_page', self::$per_page ); 215 245 $response = rest_get_server()->dispatch( $request ); 216 246 $data = $response->get_data(); 217 $this->assertTrue( in_array( $id1, wp_list_pluck( $data, 'id' ), true ) ); 218 $this->assertTrue( in_array( $id2, wp_list_pluck( $data, 'id' ), true ) ); 247 $ids = wp_list_pluck( $data, 'id' ); 248 $this->assertTrue( in_array( $id1, $ids, true ) ); 249 $this->assertTrue( in_array( $id2, $ids, true ) ); 250 219 251 $request->set_param( 'exclude', array( $id2 ) ); 220 252 $response = rest_get_server()->dispatch( $request ); 221 253 $data = $response->get_data(); 222 $this->assertTrue( in_array( $id1, wp_list_pluck( $data, 'id' ), true ) ); 223 $this->assertFalse( in_array( $id2, wp_list_pluck( $data, 'id' ), true ) ); 224 // Invalid exclude value should fail 254 $ids = wp_list_pluck( $data, 'id' ); 255 $this->assertTrue( in_array( $id1, $ids, true ) ); 256 $this->assertFalse( in_array( $id2, $ids, true ) ); 257 258 // Invalid 'exclude' should error. 225 259 $request->set_param( 'exclude', array( 'invalid' ) ); 226 260 $response = rest_get_server()->dispatch( $request ); … … 229 263 230 264 public function test_get_items_offset_query() { 231 $id1 = $this->factory->tag->create(); 232 $id2 = $this->factory->tag->create(); 233 $id3 = $this->factory->tag->create(); 234 $id4 = $this->factory->tag->create(); 235 $request = new WP_REST_Request( 'GET', '/wp/v2/tags' ); 265 $request = new WP_REST_Request( 'GET', '/wp/v2/tags' ); 266 $request->set_param( 'per_page', self::$per_page ); 236 267 $request->set_param( 'offset', 1 ); 237 268 $response = rest_get_server()->dispatch( $request ); 238 $this->assertCount( 3, $response->get_data() ); 239 // 'offset' works with 'per_page' 269 $this->assertCount( self::$total_tags - 1, $response->get_data() ); 270 271 // 'offset' works with 'per_page'. 240 272 $request->set_param( 'per_page', 2 ); 241 273 $response = rest_get_server()->dispatch( $request ); 242 274 $this->assertCount( 2, $response->get_data() ); 243 // 'offset' takes priority over 'page' 275 276 // 'offset' takes priority over 'page'. 244 277 $request->set_param( 'page', 3 ); 245 278 $response = rest_get_server()->dispatch( $request ); 246 279 $this->assertCount( 2, $response->get_data() ); 247 // 'offset' invalid value shoudl fail 280 281 // Invalid 'offset' should error. 248 282 $request->set_param( 'offset', 'moreplease' ); 249 283 $response = rest_get_server()->dispatch( $request ); … … 254 288 public function test_get_items_orderby_args() { 255 289 $tag1 = $this->factory->tag->create( array( 'name' => 'Apple' ) ); 256 $tag2 = $this->factory->tag->create( array( 'name' => 'Banana' ) ); 290 $tag2 = $this->factory->tag->create( array( 'name' => 'Zucchini' ) ); 291 257 292 /* 258 293 * Tests: … … 269 304 $data = $response->get_data(); 270 305 $this->assertEquals( 1, count( $data ) ); 271 $this->assertEquals( 'Banana', $data[0]['name'] ); 306 $this->assertEquals( 'Zucchini', $data[0]['name'] ); 307 272 308 $request = new WP_REST_Request( 'GET', '/wp/v2/tags' ); 273 309 $request->set_param( 'orderby', 'name' ); … … 279 315 $this->assertEquals( 2, count( $data ) ); 280 316 $this->assertEquals( 'Apple', $data[0]['name'] ); 281 // Invalid orderby should fail. 317 318 // Invalid 'orderby' should error. 282 319 $request->set_param( 'orderby', 'invalid' ); 283 320 $response = rest_get_server()->dispatch( $request ); … … 289 326 $tag1 = $this->factory->tag->create( array( 'name' => 'Apple' ) ); 290 327 $tag2 = $this->factory->tag->create( array( 'name' => 'Banana' ) ); 291 // defaults to orderby=name, order=asc 328 329 // Defaults to 'orderby' => 'name', 'order' => 'asc'. 292 330 $request = new WP_REST_Request( 'GET', '/wp/v2/tags' ); 293 331 $response = rest_get_server()->dispatch( $request ); … … 297 335 $this->assertEquals( 'Banana', $data[1]['name'] ); 298 336 $this->assertEquals( 'Cantaloupe', $data[2]['name'] ); 299 // orderby=id, with default order=asc 337 338 // 'orderby' => 'id', with default 'order' => 'asc'. 300 339 $request = new WP_REST_Request( 'GET', '/wp/v2/tags' ); 301 340 $request->set_param( 'orderby', 'id' ); … … 303 342 $this->assertEquals( 200, $response->get_status() ); 304 343 $data = $response->get_data(); 305 $this->assertEquals( 'Cantaloupe', $data[0]['name'] ); 306 $this->assertEquals( 'Apple', $data[1]['name'] ); 307 $this->assertEquals( 'Banana', $data[2]['name'] ); 308 // orderby=id, order=desc 344 $this->assertEquals( 'Tag 0', $data[0]['name'] ); 345 $this->assertEquals( 'Tag 1', $data[1]['name'] ); 346 $this->assertEquals( 'Tag 2', $data[2]['name'] ); 347 348 // 'orderby' => 'id', 'order' => 'desc'. 309 349 $request = new WP_REST_Request( 'GET', '/wp/v2/tags' ); 310 350 $request->set_param( 'orderby', 'id' ); … … 339 379 $tag2 = $this->factory->tag->create( array( 'name' => 'Marvel' ) ); 340 380 $this->factory->tag->create( array( 'name' => 'Dark Horse' ) ); 381 341 382 wp_set_object_terms( $post_id, array( $tag1, $tag2 ), 'post_tag' ); 342 383 … … 350 391 $this->assertEquals( 'DC', $data[0]['name'] ); 351 392 352 // Invalid postshould error.393 // Invalid 'post' should error. 353 394 $request = new WP_REST_Request( 'GET', '/wp/v2/tags' ); 354 395 $request->set_param( 'post', 'invalid-post' ); … … 359 400 public function test_get_terms_post_args_paging() { 360 401 $post_id = $this->factory->post->create(); 361 $tag_ids = array(); 362 363 for ( $i = 0; $i < 30; $i++ ) { 364 $tag_ids[] = $this->factory->tag->create( 365 array( 366 'name' => "Tag {$i}", 367 ) 368 ); 369 } 370 wp_set_object_terms( $post_id, $tag_ids, 'post_tag' ); 402 403 wp_set_object_terms( $post_id, self::$tag_ids, 'post_tag' ); 371 404 372 405 $request = new WP_REST_Request( 'GET', '/wp/v2/tags' ); … … 433 466 ); 434 467 $post_id = $this->factory->post->create(); 468 435 469 wp_set_object_terms( $post_id, array( $term1, $term2 ), 'batman' ); 436 470 … … 448 482 $tag1 = $this->factory->tag->create( array( 'name' => 'Apple' ) ); 449 483 $tag2 = $this->factory->tag->create( array( 'name' => 'Banana' ) ); 484 450 485 /* 451 486 * Tests: … … 459 494 $this->assertEquals( 1, count( $data ) ); 460 495 $this->assertEquals( 'Apple', $data[0]['name'] ); 496 461 497 $request = new WP_REST_Request( 'GET', '/wp/v2/tags' ); 462 498 $request->set_param( 'search', 'Garbage' ); … … 468 504 469 505 public function test_get_items_slug_arg() { 470 $tag1 = $this->factory->tag->create( array( 'name' => 'Apple' ) ); 471 $tag2 = $this->factory->tag->create( array( 'name' => 'Banana' ) ); 506 $tag1 = $this->factory->tag->create( array( 'name' => 'Apple' ) ); 507 $tag2 = $this->factory->tag->create( array( 'name' => 'Banana' ) ); 508 472 509 $request = new WP_REST_Request( 'GET', '/wp/v2/tags' ); 473 510 $request->set_param( 'slug', 'apple' ); … … 484 521 $id3 = $this->factory->tag->create( array( 'name' => 'Burrito' ) ); 485 522 $this->factory->tag->create( array( 'name' => 'Pizza' ) ); 523 486 524 $request = new WP_REST_Request( 'GET', '/wp/v2/tags' ); 487 525 $request->set_param( … … 506 544 $id3 = $this->factory->tag->create( array( 'name' => 'Burrito' ) ); 507 545 $this->factory->tag->create( array( 'name' => 'Pizza' ) ); 546 508 547 $request = new WP_REST_Request( 'GET', '/wp/v2/tags' ); 509 548 $request->set_param( 'slug', 'taco,burrito, enchilada' ); … … 537 576 538 577 public function test_get_terms_pagination_headers() { 539 // Start of the index 540 for ( $i = 0; $i < 50; $i++ ) { 541 $this->factory->tag->create( 542 array( 543 'name' => "Tag {$i}", 544 ) 545 ); 546 } 578 $total_tags = self::$total_tags; 579 $total_pages = (int) ceil( $total_tags / 10 ); 580 581 // Start of the index. 547 582 $request = new WP_REST_Request( 'GET', '/wp/v2/tags' ); 548 583 $response = rest_get_server()->dispatch( $request ); 549 584 $headers = $response->get_headers(); 550 $this->assertEquals( 50, $headers['X-WP-Total'] );551 $this->assertEquals( 5, $headers['X-WP-TotalPages'] );585 $this->assertEquals( $total_tags, $headers['X-WP-Total'] ); 586 $this->assertEquals( $total_pages, $headers['X-WP-TotalPages'] ); 552 587 $next_link = add_query_arg( 553 588 array( … … 558 593 $this->assertFalse( stripos( $headers['Link'], 'rel="prev"' ) ); 559 594 $this->assertContains( '<' . $next_link . '>; rel="next"', $headers['Link'] ); 560 // 3rd page 561 $this->factory->tag->create( 562 array( 563 'name' => 'Tag 51', 564 ) 565 ); 595 596 // 3rd page. 597 $this->factory->tag->create(); 598 $total_tags++; 599 $total_pages++; 566 600 $request = new WP_REST_Request( 'GET', '/wp/v2/tags' ); 567 601 $request->set_param( 'page', 3 ); 568 602 $response = rest_get_server()->dispatch( $request ); 569 603 $headers = $response->get_headers(); 570 $this->assertEquals( 51, $headers['X-WP-Total'] );571 $this->assertEquals( 6, $headers['X-WP-TotalPages'] );604 $this->assertEquals( $total_tags, $headers['X-WP-Total'] ); 605 $this->assertEquals( $total_pages, $headers['X-WP-TotalPages'] ); 572 606 $prev_link = add_query_arg( 573 607 array( … … 584 618 ); 585 619 $this->assertContains( '<' . $next_link . '>; rel="next"', $headers['Link'] ); 586 // Last page 587 $request = new WP_REST_Request( 'GET', '/wp/v2/tags' ); 588 $request->set_param( 'page', 6 ); 620 621 // Last page. 622 $request = new WP_REST_Request( 'GET', '/wp/v2/tags' ); 623 $request->set_param( 'page', $total_pages ); 589 624 $response = rest_get_server()->dispatch( $request ); 590 625 $headers = $response->get_headers(); 591 $this->assertEquals( 51, $headers['X-WP-Total'] );592 $this->assertEquals( 6, $headers['X-WP-TotalPages'] );626 $this->assertEquals( $total_tags, $headers['X-WP-Total'] ); 627 $this->assertEquals( $total_pages, $headers['X-WP-TotalPages'] ); 593 628 $prev_link = add_query_arg( 594 629 array( 595 'page' => 5,630 'page' => $total_pages - 1, 596 631 ), 597 632 rest_url( 'wp/v2/tags' ) … … 599 634 $this->assertContains( '<' . $prev_link . '>; rel="prev"', $headers['Link'] ); 600 635 $this->assertFalse( stripos( $headers['Link'], 'rel="next"' ) ); 601 // Out of bounds 602 $request = new WP_REST_Request( 'GET', '/wp/v2/tags' ); 603 $request->set_param( 'page', 8 ); 636 637 // Out of bounds. 638 $request = new WP_REST_Request( 'GET', '/wp/v2/tags' ); 639 $request->set_param( 'page', 100 ); 604 640 $response = rest_get_server()->dispatch( $request ); 605 641 $headers = $response->get_headers(); 606 $this->assertEquals( 51, $headers['X-WP-Total'] );607 $this->assertEquals( 6, $headers['X-WP-TotalPages'] );642 $this->assertEquals( $total_tags, $headers['X-WP-Total'] ); 643 $this->assertEquals( $total_pages, $headers['X-WP-TotalPages'] ); 608 644 $prev_link = add_query_arg( 609 645 array( 610 'page' => 6,646 'page' => $total_pages, 611 647 ), 612 648 rest_url( 'wp/v2/tags' ) … … 624 660 625 661 public function test_get_item() { 626 $id = $this->factory->tag->create(); 662 $id = $this->factory->tag->create(); 663 627 664 $request = new WP_REST_Request( 'GET', '/wp/v2/tags/' . $id ); 628 665 $response = rest_get_server()->dispatch( $request ); … … 634 671 */ 635 672 public function test_get_item_meta() { 636 $id = $this->factory->tag->create(); 673 $id = $this->factory->tag->create(); 674 637 675 $request = new WP_REST_Request( 'GET', '/wp/v2/tags/' . $id ); 638 676 $response = rest_get_server()->dispatch( $request ); … … 655 693 */ 656 694 public function test_get_item_meta_registered_for_different_taxonomy() { 657 $id = $this->factory->tag->create(); 695 $id = $this->factory->tag->create(); 696 658 697 $request = new WP_REST_Request( 'GET', '/wp/v2/tags/' . $id ); 659 698 $response = rest_get_server()->dispatch( $request ); … … 673 712 public function test_get_item_invalid_permission_for_context() { 674 713 $id = $this->factory->tag->create(); 714 675 715 wp_set_current_user( 0 ); 716 676 717 $request = new WP_REST_Request( 'GET', '/wp/v2/tags/' . $id ); 677 718 $request->set_param( 'context', 'edit' ); … … 696 737 public function test_get_item_incorrect_taxonomy() { 697 738 register_taxonomy( 'robin', 'post' ); 698 $term1 739 $term1 = $this->factory->term->create( 699 740 array( 700 741 'name' => 'Cape', … … 702 743 ) 703 744 ); 745 704 746 $request = new WP_REST_Request( 'GET', '/wp/v2/tags/' . $term1 ); 705 747 $response = rest_get_server()->dispatch( $request ); … … 709 751 public function test_create_item() { 710 752 wp_set_current_user( self::$administrator ); 753 711 754 $request = new WP_REST_Request( 'POST', '/wp/v2/tags' ); 712 755 $request->set_param( 'name', 'My Awesome Term' ); … … 725 768 public function test_create_item_contributor() { 726 769 wp_set_current_user( self::$contributor ); 770 727 771 $request = new WP_REST_Request( 'POST', '/wp/v2/tags' ); 728 772 $request->set_param( 'name', 'My Awesome Term' ); … … 741 785 public function test_create_item_incorrect_permissions() { 742 786 wp_set_current_user( self::$subscriber ); 787 743 788 $request = new WP_REST_Request( 'POST', '/wp/v2/tags' ); 744 789 $request->set_param( 'name', 'Incorrect permissions' ); … … 749 794 public function test_create_item_missing_arguments() { 750 795 wp_set_current_user( self::$administrator ); 796 751 797 $request = new WP_REST_Request( 'POST', '/wp/v2/tags' ); 752 798 $response = rest_get_server()->dispatch( $request ); … … 766 812 public function test_create_item_with_meta() { 767 813 wp_set_current_user( self::$administrator ); 814 768 815 $request = new WP_REST_Request( 'POST', '/wp/v2/tags' ); 769 816 $request->set_param( 'name', 'My Awesome Term' ); … … 780 827 public function test_create_item_with_meta_wrong_id() { 781 828 wp_set_current_user( self::$administrator ); 829 782 830 $existing_tag_id = $this->factory->tag->create( array( 'name' => 'My Not So Awesome Term' ) ); 783 $request = new WP_REST_Request( 'POST', '/wp/v2/tags' ); 831 832 $request = new WP_REST_Request( 'POST', '/wp/v2/tags' ); 784 833 $request->set_param( 'name', 'My Awesome Term' ); 785 834 $request->set_param( 'meta', array( 'test_tag_single' => 'hello' ) ); … … 797 846 public function test_update_item() { 798 847 wp_set_current_user( self::$administrator ); 848 799 849 $orig_args = array( 800 850 'name' => 'Original Name', … … 802 852 'slug' => 'original-slug', 803 853 ); 804 $term = get_term_by( 'id', $this->factory->tag->create( $orig_args ), 'post_tag' ); 805 $request = new WP_REST_Request( 'POST', '/wp/v2/tags/' . $term->term_id ); 854 855 $term = get_term_by( 'id', $this->factory->tag->create( $orig_args ), 'post_tag' ); 856 857 $request = new WP_REST_Request( 'POST', '/wp/v2/tags/' . $term->term_id ); 806 858 $request->set_param( 'name', 'New Name' ); 807 859 $request->set_param( 'description', 'New Description' ); … … 828 880 public function test_update_item_no_change() { 829 881 wp_set_current_user( self::$administrator ); 882 830 883 $term = get_term_by( 'id', $this->factory->tag->create(), 'post_tag' ); 831 884 832 $request = new WP_REST_Request( 'PUT', '/wp/v2/tags/' . $term->term_id ); 833 885 $request = new WP_REST_Request( 'PUT', '/wp/v2/tags/' . $term->term_id ); 834 886 $response = rest_get_server()->dispatch( $request ); 835 887 $this->assertEquals( 200, $response->get_status() ); … … 847 899 public function test_update_item_invalid_term() { 848 900 wp_set_current_user( self::$administrator ); 901 849 902 $request = new WP_REST_Request( 'POST', '/wp/v2/tags/' . REST_TESTS_IMPOSSIBLY_HIGH_NUMBER ); 850 903 $request->set_param( 'name', 'Invalid Term' ); … … 855 908 public function test_update_item_incorrect_permissions() { 856 909 wp_set_current_user( self::$subscriber ); 857 $term = get_term_by( 'id', $this->factory->tag->create(), 'post_tag' ); 910 911 $term = get_term_by( 'id', $this->factory->tag->create(), 'post_tag' ); 912 858 913 $request = new WP_REST_Request( 'POST', '/wp/v2/tags/' . $term->term_id ); 859 914 $request->set_param( 'name', 'Incorrect permissions' ); … … 867 922 public function test_update_item_with_edit_term_cap_granted() { 868 923 wp_set_current_user( self::$subscriber ); 869 $term = $this->factory->tag->create_and_get(); 924 925 $term = $this->factory->tag->create_and_get(); 926 870 927 $request = new WP_REST_Request( 'POST', '/wp/v2/tags/' . $term->term_id ); 871 928 $request->set_param( 'name', 'New Name' ); … … 892 949 public function test_update_item_with_edit_term_cap_revoked() { 893 950 wp_set_current_user( self::$administrator ); 894 $term = $this->factory->tag->create_and_get(); 951 952 $term = $this->factory->tag->create_and_get(); 953 895 954 $request = new WP_REST_Request( 'POST', '/wp/v2/tags/' . $term->term_id ); 896 955 $request->set_param( 'name', 'New Name' ); … … 912 971 public function test_update_item_parent_non_hierarchical_taxonomy() { 913 972 wp_set_current_user( self::$administrator ); 973 914 974 $term = get_term_by( 'id', $this->factory->tag->create(), 'post_tag' ); 915 975 … … 960 1020 public function test_tag_roundtrip_as_editor() { 961 1021 wp_set_current_user( self::$editor ); 1022 962 1023 $this->assertEquals( ! is_multisite(), current_user_can( 'unfiltered_html' ) ); 963 1024 $this->verify_tag_roundtrip( … … 975 1036 public function test_tag_roundtrip_as_editor_html() { 976 1037 wp_set_current_user( self::$editor ); 1038 977 1039 if ( is_multisite() ) { 978 1040 $this->assertFalse( current_user_can( 'unfiltered_html' ) ); … … 1004 1066 public function test_tag_roundtrip_as_superadmin() { 1005 1067 wp_set_current_user( self::$superadmin ); 1068 1006 1069 $this->assertTrue( current_user_can( 'unfiltered_html' ) ); 1007 1070 $this->verify_tag_roundtrip( … … 1019 1082 public function test_tag_roundtrip_as_superadmin_html() { 1020 1083 wp_set_current_user( self::$superadmin ); 1084 1021 1085 $this->assertTrue( current_user_can( 'unfiltered_html' ) ); 1022 1086 $this->verify_tag_roundtrip( … … 1034 1098 public function test_delete_item() { 1035 1099 wp_set_current_user( self::$administrator ); 1036 $term = get_term_by( 'id', $this->factory->tag->create( array( 'name' => 'Deleted Tag' ) ), 'post_tag' ); 1100 1101 $term = get_term_by( 'id', $this->factory->tag->create( array( 'name' => 'Deleted Tag' ) ), 'post_tag' ); 1102 1037 1103 $request = new WP_REST_Request( 'DELETE', '/wp/v2/tags/' . $term->term_id ); 1038 1104 $request->set_param( 'force', true ); … … 1046 1112 public function test_delete_item_no_trash() { 1047 1113 wp_set_current_user( self::$administrator ); 1114 1048 1115 $term = get_term_by( 'id', $this->factory->tag->create( array( 'name' => 'Deleted Tag' ) ), 'post_tag' ); 1049 1116 … … 1059 1126 public function test_delete_item_invalid_term() { 1060 1127 wp_set_current_user( self::$administrator ); 1128 1061 1129 $request = new WP_REST_Request( 'DELETE', '/wp/v2/tags/' . REST_TESTS_IMPOSSIBLY_HIGH_NUMBER ); 1062 1130 $response = rest_get_server()->dispatch( $request ); … … 1066 1134 public function test_delete_item_incorrect_permissions() { 1067 1135 wp_set_current_user( self::$subscriber ); 1068 $term = get_term_by( 'id', $this->factory->tag->create(), 'post_tag' ); 1136 1137 $term = get_term_by( 'id', $this->factory->tag->create(), 'post_tag' ); 1138 1069 1139 $request = new WP_REST_Request( 'DELETE', '/wp/v2/tags/' . $term->term_id ); 1070 1140 $response = rest_get_server()->dispatch( $request ); … … 1077 1147 public function test_delete_item_with_delete_term_cap_granted() { 1078 1148 wp_set_current_user( self::$subscriber ); 1079 $term = get_term_by( 'id', $this->factory->tag->create( array( 'name' => 'Deleted Tag' ) ), 'post_tag' ); 1149 1150 $term = get_term_by( 'id', $this->factory->tag->create( array( 'name' => 'Deleted Tag' ) ), 'post_tag' ); 1151 1080 1152 $request = new WP_REST_Request( 'DELETE', '/wp/v2/tags/' . $term->term_id ); 1081 1153 $request->set_param( 'force', true ); … … 1103 1175 public function test_delete_item_with_delete_term_cap_revoked() { 1104 1176 wp_set_current_user( self::$administrator ); 1105 $term = get_term_by( 'id', $this->factory->tag->create( array( 'name' => 'Deleted Tag' ) ), 'post_tag' ); 1177 1178 $term = get_term_by( 'id', $this->factory->tag->create( array( 'name' => 'Deleted Tag' ) ), 'post_tag' ); 1179 1106 1180 $request = new WP_REST_Request( 'DELETE', '/wp/v2/tags/' . $term->term_id ); 1107 1181 $request->set_param( 'force', true ); … … 1122 1196 1123 1197 public function test_prepare_item() { 1124 $term = get_term_by( 'id', $this->factory->tag->create(), 'post_tag' ); 1198 $term = get_term_by( 'id', $this->factory->tag->create(), 'post_tag' ); 1199 1125 1200 $request = new WP_REST_Request( 'GET', '/wp/v2/tags/' . $term->term_id ); 1126 1201 $response = rest_get_server()->dispatch( $request ); … … 1196 1271 $this->assertEquals( $schema, $data['schema']['properties']['my_custom_int'] ); 1197 1272 1198 $tag_id 1199 $request = new WP_REST_Request( 'GET', '/wp/v2/tags/' . $tag_id ); 1200 1273 $tag_id = $this->factory->tag->create(); 1274 1275 $request = new WP_REST_Request( 'GET', '/wp/v2/tags/' . $tag_id ); 1201 1276 $response = rest_get_server()->dispatch( $request ); 1202 1277 $this->assertArrayHasKey( 'my_custom_int', $response->data ); … … 1225 1300 1226 1301 wp_set_current_user( self::$administrator ); 1302 1227 1303 $tag_id = $this->factory->tag->create(); 1304 1228 1305 // Check for error on update. 1229 1306 $request = new WP_REST_Request( 'POST', sprintf( '/wp/v2/tags/%d', $tag_id ) ); -
trunk/tests/phpunit/tests/rest-api/rest-users-controller.php
r46586 r46657 16 16 protected static $draft_editor; 17 17 protected static $subscriber; 18 protected static $authors = array(); 19 protected static $posts = array(); 18 19 protected static $authors = array(); 20 protected static $posts = array(); 21 protected static $user_ids = array(); 22 protected static $total_users = 30; 23 protected static $per_page = 50; 24 20 25 protected static $site; 21 26 … … 101 106 update_site_option( 'site_admins', array( 'superadmin' ) ); 102 107 } 108 109 // Set up users for pagination tests. 110 for ( $i = 0; $i < self::$total_users - 10; $i++ ) { 111 self::$user_ids[] = $factory->user->create( 112 array( 113 'role' => 'contributor', 114 'display_name' => "User {$i}", 115 ) 116 ); 117 } 103 118 } 104 119 … … 111 126 wp_delete_post( $post, true ); 112 127 } 128 113 129 foreach ( self::$authors as $author ) { 114 130 self::delete_user( $author ); 115 131 } 132 116 133 _unregister_post_type( 'r_true_p_true' ); 117 134 _unregister_post_type( 'r_true_p_false' ); … … 121 138 if ( is_multisite() ) { 122 139 wpmu_delete_blog( self::$site, true ); 140 } 141 142 // Remove users for pagination tests. 143 foreach ( self::$user_ids as $user_id ) { 144 self::delete_user( $user_id ); 123 145 } 124 146 } … … 213 235 214 236 public function test_get_items_with_edit_context_without_permission() { 215 // test with a user not logged in237 // Test with a user not logged in. 216 238 $request = new WP_REST_Request( 'GET', '/wp/v2/users' ); 217 239 $request->set_param( 'context', 'edit' ); … … 220 242 $this->assertEquals( 401, $response->get_status() ); 221 243 222 //test with a user logged in but without sufficient capabilities; capability in question: 'list_users' 244 // Test with a user logged in but without sufficient capabilities; 245 // capability in question: 'list_users'. 223 246 wp_set_current_user( self::$editor ); 247 224 248 $request = new WP_REST_Request( 'GET', '/wp/v2/users' ); 225 249 $request->set_param( 'context', 'edit' ); … … 281 305 282 306 public function test_get_items_pagination_headers() { 283 wp_set_current_user( self::$user ); 284 for ( $i = 0; $i < 44; $i++ ) { 285 $this->factory->user->create( 286 array( 287 'name' => "User {$i}", 288 ) 289 ); 290 } 307 $total_users = self::$total_users; 308 $total_pages = (int) ceil( $total_users / 10 ); 309 310 wp_set_current_user( self::$user ); 311 312 // Start of the index. 291 313 $request = new WP_REST_Request( 'GET', '/wp/v2/users' ); 292 314 $response = rest_get_server()->dispatch( $request ); 293 315 $headers = $response->get_headers(); 294 $this->assertEquals( 54, $headers['X-WP-Total'] );295 $this->assertEquals( 6, $headers['X-WP-TotalPages'] );316 $this->assertEquals( $total_users, $headers['X-WP-Total'] ); 317 $this->assertEquals( $total_pages, $headers['X-WP-TotalPages'] ); 296 318 $next_link = add_query_arg( 297 319 array( … … 302 324 $this->assertFalse( stripos( $headers['Link'], 'rel="prev"' ) ); 303 325 $this->assertContains( '<' . $next_link . '>; rel="next"', $headers['Link'] ); 304 // 3rd page 305 $this->factory->user->create( 306 array( 307 'name' => 'User 51', 308 ) 309 ); 326 327 // 3rd page. 328 $this->factory->user->create(); 329 $total_users++; 330 $total_pages++; 310 331 $request = new WP_REST_Request( 'GET', '/wp/v2/users' ); 311 332 $request->set_param( 'page', 3 ); 312 333 $response = rest_get_server()->dispatch( $request ); 313 334 $headers = $response->get_headers(); 314 $this->assertEquals( 55, $headers['X-WP-Total'] );315 $this->assertEquals( 6, $headers['X-WP-TotalPages'] );335 $this->assertEquals( $total_users, $headers['X-WP-Total'] ); 336 $this->assertEquals( $total_pages, $headers['X-WP-TotalPages'] ); 316 337 $prev_link = add_query_arg( 317 338 array( … … 328 349 ); 329 350 $this->assertContains( '<' . $next_link . '>; rel="next"', $headers['Link'] ); 330 // Last page 331 $request = new WP_REST_Request( 'GET', '/wp/v2/users' ); 332 $request->set_param( 'page', 6 ); 351 352 // Last page. 353 $request = new WP_REST_Request( 'GET', '/wp/v2/users' ); 354 $request->set_param( 'page', $total_pages ); 333 355 $response = rest_get_server()->dispatch( $request ); 334 356 $headers = $response->get_headers(); 335 $this->assertEquals( 55, $headers['X-WP-Total'] );336 $this->assertEquals( 6, $headers['X-WP-TotalPages'] );357 $this->assertEquals( $total_users, $headers['X-WP-Total'] ); 358 $this->assertEquals( $total_pages, $headers['X-WP-TotalPages'] ); 337 359 $prev_link = add_query_arg( 338 360 array( 339 'page' => 5,361 'page' => $total_pages - 1, 340 362 ), 341 363 rest_url( 'wp/v2/users' ) … … 343 365 $this->assertContains( '<' . $prev_link . '>; rel="prev"', $headers['Link'] ); 344 366 $this->assertFalse( stripos( $headers['Link'], 'rel="next"' ) ); 345 // Out of bounds 346 $request = new WP_REST_Request( 'GET', '/wp/v2/users' ); 347 $request->set_param( 'page', 8 ); 367 368 // Out of bounds. 369 $request = new WP_REST_Request( 'GET', '/wp/v2/users' ); 370 $request->set_param( 'page', 100 ); 348 371 $response = rest_get_server()->dispatch( $request ); 349 372 $headers = $response->get_headers(); 350 $this->assertEquals( 55, $headers['X-WP-Total'] );351 $this->assertEquals( 6, $headers['X-WP-TotalPages'] );373 $this->assertEquals( $total_users, $headers['X-WP-Total'] ); 374 $this->assertEquals( $total_pages, $headers['X-WP-TotalPages'] ); 352 375 $prev_link = add_query_arg( 353 376 array( 354 'page' => 6,377 'page' => $total_pages, 355 378 ), 356 379 rest_url( 'wp/v2/users' ) … … 362 385 public function test_get_items_per_page() { 363 386 wp_set_current_user( self::$user ); 364 for ( $i = 0; $i < 20; $i++ ) { 365 $this->factory->user->create( array( 'display_name' => "User {$i}" ) ); 366 } 387 367 388 $request = new WP_REST_Request( 'GET', '/wp/v2/users' ); 368 389 $response = rest_get_server()->dispatch( $request ); 369 390 $this->assertEquals( 10, count( $response->get_data() ) ); 391 370 392 $request = new WP_REST_Request( 'GET', '/wp/v2/users' ); 371 393 $request->set_param( 'per_page', 5 ); … … 376 398 public function test_get_items_page() { 377 399 wp_set_current_user( self::$user ); 378 for ( $i = 0; $i < 20; $i++ ) { 379 $this->factory->user->create( array( 'display_name' => "User {$i}" ) ); 380 } 400 381 401 $request = new WP_REST_Request( 'GET', '/wp/v2/users' ); 382 402 $request->set_param( 'per_page', 5 ); … … 397 417 public function test_get_items_orderby_name() { 398 418 wp_set_current_user( self::$user ); 419 399 420 $low_id = $this->factory->user->create( array( 'display_name' => 'AAAAA' ) ); 400 421 $mid_id = $this->factory->user->create( array( 'display_name' => 'NNNNN' ) ); 401 422 $high_id = $this->factory->user->create( array( 'display_name' => 'ZZZZ' ) ); 423 402 424 $request = new WP_REST_Request( 'GET', '/wp/v2/users' ); 403 425 $request->set_param( 'orderby', 'name' ); … … 407 429 $data = $response->get_data(); 408 430 $this->assertEquals( $high_id, $data[0]['id'] ); 431 409 432 $request = new WP_REST_Request( 'GET', '/wp/v2/users' ); 410 433 $request->set_param( 'orderby', 'name' ); … … 429 452 $response = rest_get_server()->dispatch( $request ); 430 453 $data = $response->get_data(); 431 432 454 $this->assertEquals( $high_id, $data[0]['id'] ); 433 455 … … 455 477 $response = rest_get_server()->dispatch( $request ); 456 478 $data = $response->get_data(); 457 458 479 $this->assertEquals( $high_id, $data[0]['id'] ); 459 480 … … 543 564 public function test_get_items_offset() { 544 565 wp_set_current_user( self::$user ); 545 // 9 users created in wpSetUpBeforeClass(), plus default user. 546 $ this->factory->user->create();547 $request = new WP_REST_Request( 'GET', '/wp/v2/users');566 567 $request = new WP_REST_Request( 'GET', '/wp/v2/users' ); 568 $request->set_param( 'per_page', self::$per_page ); 548 569 $request->set_param( 'offset', 1 ); 549 570 $response = rest_get_server()->dispatch( $request ); 550 $this->assertCount( 10, $response->get_data() ); 551 // 'offset' works with 'per_page' 571 $this->assertCount( self::$total_users - 1, $response->get_data() ); 572 573 // 'offset' works with 'per_page'. 552 574 $request->set_param( 'per_page', 2 ); 553 575 $response = rest_get_server()->dispatch( $request ); 554 576 $this->assertCount( 2, $response->get_data() ); 555 // 'offset' takes priority over 'page' 577 578 // 'offset' takes priority over 'page'. 556 579 $request->set_param( 'page', 3 ); 557 580 $response = rest_get_server()->dispatch( $request ); 558 581 $this->assertCount( 2, $response->get_data() ); 559 // 'offset' invalid value should error 582 583 // Invalid 'offset' should error. 560 584 $request->set_param( 'offset', 'moreplease' ); 561 585 $response = rest_get_server()->dispatch( $request ); … … 565 589 public function test_get_items_include_query() { 566 590 wp_set_current_user( self::$user ); 567 $id1 = $this->factory->user->create(); 568 $id2 = $this->factory->user->create(); 569 $id3 = $this->factory->user->create(); 570 $request = new WP_REST_Request( 'GET', '/wp/v2/users' ); 571 // Orderby=>asc 572 $request->set_param( 'include', array( $id3, $id1 ) ); 591 592 $id1 = $this->factory->user->create(); 593 $id2 = $this->factory->user->create(); 594 595 $request = new WP_REST_Request( 'GET', '/wp/v2/users' ); 596 597 // 'orderby' => 'asc'. 598 $request->set_param( 'include', array( $id2, $id1 ) ); 573 599 $response = rest_get_server()->dispatch( $request ); 574 600 $data = $response->get_data(); 575 601 $this->assertEquals( 2, count( $data ) ); 576 602 $this->assertEquals( $id1, $data[0]['id'] ); 577 // Orderby=>include 603 604 // 'orderby' => 'include'. 578 605 $request->set_param( 'orderby', 'include' ); 579 606 $response = rest_get_server()->dispatch( $request ); 580 607 $data = $response->get_data(); 581 608 $this->assertEquals( 2, count( $data ) ); 582 $this->assertEquals( $id3, $data[0]['id'] ); 583 // Invalid include should fail 609 $this->assertEquals( $id2, $data[0]['id'] ); 610 611 // Invalid 'include' should error. 584 612 $request->set_param( 'include', 'invalid' ); 585 613 $response = rest_get_server()->dispatch( $request ); 586 614 $this->assertErrorResponse( 'rest_invalid_param', $response, 400 ); 587 // No privileges 588 $request->set_param( 'include', array( $id3, $id1 ) ); 615 616 // No privileges. 617 $request->set_param( 'include', array( $id2, $id1 ) ); 589 618 wp_set_current_user( 0 ); 590 619 $response = rest_get_server()->dispatch( $request ); … … 596 625 public function test_get_items_exclude_query() { 597 626 wp_set_current_user( self::$user ); 598 $id1 = $this->factory->user->create(); 599 $id2 = $this->factory->user->create(); 600 $request = new WP_REST_Request( 'GET', '/wp/v2/users' ); 601 $request->set_param( 'per_page', 20 ); // there are >10 users at this point 602 $response = rest_get_server()->dispatch( $request ); 603 $data = $response->get_data(); 604 $this->assertTrue( in_array( $id1, wp_list_pluck( $data, 'id' ), true ) ); 605 $this->assertTrue( in_array( $id2, wp_list_pluck( $data, 'id' ), true ) ); 627 628 $id1 = $this->factory->user->create(); 629 $id2 = $this->factory->user->create(); 630 631 $request = new WP_REST_Request( 'GET', '/wp/v2/users' ); 632 $request->set_param( 'per_page', self::$per_page ); // There are >10 users at this point. 633 $response = rest_get_server()->dispatch( $request ); 634 $data = $response->get_data(); 635 $ids = wp_list_pluck( $data, 'id' ); 636 $this->assertTrue( in_array( $id1, $ids, true ) ); 637 $this->assertTrue( in_array( $id2, $ids, true ) ); 638 606 639 $request->set_param( 'exclude', array( $id2 ) ); 607 640 $response = rest_get_server()->dispatch( $request ); 608 641 $data = $response->get_data(); 609 $this->assertTrue( in_array( $id1, wp_list_pluck( $data, 'id' ), true ) ); 610 $this->assertFalse( in_array( $id2, wp_list_pluck( $data, 'id' ), true ) ); 611 // Invalid exlude value should error. 642 $ids = wp_list_pluck( $data, 'id' ); 643 $this->assertTrue( in_array( $id1, $ids, true ) ); 644 $this->assertFalse( in_array( $id2, $ids, true ) ); 645 646 // Invalid 'exclude' should error. 612 647 $request->set_param( 'exclude', 'none-of-those-please' ); 613 648 $response = rest_get_server()->dispatch( $request ); … … 617 652 public function test_get_items_search() { 618 653 wp_set_current_user( self::$user ); 654 619 655 $request = new WP_REST_Request( 'GET', '/wp/v2/users' ); 620 656 $request->set_param( 'search', 'yololololo' ); 621 657 $response = rest_get_server()->dispatch( $request ); 622 658 $this->assertEquals( 0, count( $response->get_data() ) ); 659 623 660 $yolo_id = $this->factory->user->create( array( 'display_name' => 'yololololo' ) ); 661 624 662 $request = new WP_REST_Request( 'GET', '/wp/v2/users' ); 625 663 $request->set_param( 'search', 'yololololo' ); … … 633 671 ) 634 672 ); 673 635 674 $request = new WP_REST_Request( 'GET', '/wp/v2/users' ); 636 675 $request->set_param( 'search', 'ada' ); … … 643 682 public function test_get_items_slug_query() { 644 683 wp_set_current_user( self::$user ); 684 645 685 $this->factory->user->create( 646 686 array( … … 649 689 ) 650 690 ); 651 $id2 691 $id2 = $this->factory->user->create( 652 692 array( 653 693 'display_name' => 'Moo', … … 655 695 ) 656 696 ); 697 657 698 $request = new WP_REST_Request( 'GET', '/wp/v2/users' ); 658 699 $request->set_param( 'slug', 'foo' ); … … 665 706 public function test_get_items_slug_array_query() { 666 707 wp_set_current_user( self::$user ); 708 667 709 $id1 = $this->factory->user->create( 668 710 array( … … 689 731 ) 690 732 ); 733 691 734 $request = new WP_REST_Request( 'GET', '/wp/v2/users' ); 692 735 $request->set_param( … … 709 752 public function test_get_items_slug_csv_query() { 710 753 wp_set_current_user( self::$user ); 754 711 755 $id1 = $this->factory->user->create( 712 756 array( … … 733 777 ) 734 778 ); 779 735 780 $request = new WP_REST_Request( 'GET', '/wp/v2/users' ); 736 781 $request->set_param( 'slug', 'taco,burrito , enchilada' ); … … 747 792 public function test_get_items_roles() { 748 793 wp_set_current_user( self::$user ); 749 $tango = $this->factory->user->create( 794 795 $tango = $this->factory->user->create( 750 796 array( 751 797 'display_name' => 'tango', … … 753 799 ) 754 800 ); 755 $yolo 801 $yolo = $this->factory->user->create( 756 802 array( 757 803 'display_name' => 'yolo', … … 759 805 ) 760 806 ); 807 761 808 $request = new WP_REST_Request( 'GET', '/wp/v2/users' ); 762 809 $request->set_param( 'roles', 'author,subscriber' ); … … 766 813 $this->assertEquals( $tango, $data[1]['id'] ); 767 814 $this->assertEquals( $yolo, $data[2]['id'] ); 815 768 816 $request->set_param( 'roles', 'author' ); 769 817 $response = rest_get_server()->dispatch( $request ); … … 771 819 $this->assertEquals( 1, count( $data ) ); 772 820 $this->assertEquals( $yolo, $data[0]['id'] ); 821 773 822 wp_set_current_user( 0 ); 823 774 824 $request->set_param( 'roles', 'author' ); 775 825 $response = rest_get_server()->dispatch( $request ); 776 826 $this->assertErrorResponse( 'rest_user_cannot_view', $response, 401 ); 827 777 828 wp_set_current_user( self::$editor ); 829 778 830 $request->set_param( 'roles', 'author' ); 779 831 $response = rest_get_server()->dispatch( $request ); … … 783 835 public function test_get_items_invalid_roles() { 784 836 wp_set_current_user( self::$user ); 785 $lolz = $this->factory->user->create( 837 838 $lolz = $this->factory->user->create( 786 839 array( 787 840 'display_name' => 'lolz', … … 789 842 ) 790 843 ); 844 791 845 $request = new WP_REST_Request( 'GET', '/wp/v2/users' ); 792 846 $request->set_param( 'roles', 'ilovesteak,author' ); … … 795 849 $this->assertEquals( 1, count( $data ) ); 796 850 $this->assertEquals( $lolz, $data[0]['id'] ); 851 797 852 $request = new WP_REST_Request( 'GET', '/wp/v2/users' ); 798 853 $request->set_param( 'roles', 'steakisgood' ); … … 805 860 public function test_get_items_who_author_query() { 806 861 wp_set_current_user( self::$superadmin ); 862 807 863 // First request should include subscriber in the set. 808 864 $request = new WP_REST_Request( 'GET', '/wp/v2/users' ); … … 811 867