- Timestamp:
- 03/02/2025 10:05:08 PM (3 months ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/tests/phpunit/tests/rest-api/rest-revisions-controller.php
r59630 r59899 163 163 } 164 164 165 public function test_get_items_no_permission() { 165 /** 166 * @ticket 56481 167 */ 168 public function test_get_items_with_head_request_should_not_prepare_revisions_data() { 169 wp_set_current_user( self::$editor_id ); 170 171 $hook_name = 'rest_prepare_revision'; 172 $filter = new MockAction(); 173 $callback = array( $filter, 'filter' ); 174 175 add_filter( $hook_name, $callback ); 176 $request = new WP_REST_Request( 'HEAD', '/wp/v2/posts/' . self::$post_id . '/revisions' ); 177 $response = rest_get_server()->dispatch( $request ); 178 remove_filter( $hook_name, $callback ); 179 180 $this->assertNotWPError( $response ); 181 $response = rest_ensure_response( $response ); 182 183 $this->assertSame( 200, $response->get_status(), 'The response status should be 200.' ); 184 $this->assertSame( 0, $filter->get_call_count(), 'The "' . $hook_name . '" filter was called when it should not be for HEAD requests.' ); 185 $this->assertNull( $response->get_data(), 'The server should not generate a body in response to a HEAD request.' ); 186 } 187 188 /** 189 * @dataProvider data_readable_http_methods 190 * @ticket 56481 191 * 192 * @param string $method The HTTP method to use. 193 */ 194 public function test_get_items_no_permission( $method ) { 166 195 wp_set_current_user( 0 ); 167 $request = new WP_REST_Request( 'GET', '/wp/v2/posts/' . self::$post_id . '/revisions' );196 $request = new WP_REST_Request( $method, '/wp/v2/posts/' . self::$post_id . '/revisions' ); 168 197 $response = rest_get_server()->dispatch( $request ); 169 198 … … 174 203 } 175 204 176 public function test_get_items_missing_parent() { 177 wp_set_current_user( self::$editor_id ); 178 $request = new WP_REST_Request( 'GET', '/wp/v2/posts/' . REST_TESTS_IMPOSSIBLY_HIGH_NUMBER . '/revisions' ); 205 /** 206 * Data provider intended to provide HTTP method names for testing GET and HEAD requests. 207 * 208 * @return array 209 */ 210 public static function data_readable_http_methods() { 211 return array( 212 'GET request' => array( 'GET' ), 213 'HEAD request' => array( 'HEAD' ), 214 ); 215 } 216 217 /** 218 * @dataProvider data_readable_http_methods 219 * @ticket 56481 220 * 221 * @param string $method The HTTP method to use. 222 */ 223 public function test_get_items_missing_parent( $method ) { 224 wp_set_current_user( self::$editor_id ); 225 $request = new WP_REST_Request( $method, '/wp/v2/posts/' . REST_TESTS_IMPOSSIBLY_HIGH_NUMBER . '/revisions' ); 179 226 $response = rest_get_server()->dispatch( $request ); 180 227 $this->assertErrorResponse( 'rest_post_invalid_parent', $response, 404 ); 181 228 } 182 229 183 public function test_get_items_invalid_parent_post_type() { 184 wp_set_current_user( self::$editor_id ); 185 $request = new WP_REST_Request( 'GET', '/wp/v2/posts/' . self::$page_id . '/revisions' ); 230 /** 231 * @dataProvider data_readable_http_methods 232 * @ticket 56481 233 * 234 * @param string $method The HTTP method to use. 235 */ 236 public function test_get_items_invalid_parent_post_type( $method ) { 237 wp_set_current_user( self::$editor_id ); 238 $request = new WP_REST_Request( $method, '/wp/v2/posts/' . self::$page_id . '/revisions' ); 186 239 $response = rest_get_server()->dispatch( $request ); 187 240 $this->assertErrorResponse( 'rest_post_invalid_parent', $response, 404 ); … … 214 267 } 215 268 269 /** 270 * @dataProvider data_readable_http_methods 271 * @ticket 56481 272 * 273 * @param string $method The HTTP method to use. 274 */ 275 public function test_get_item_should_allow_adding_headers_via_filter( $method ) { 276 wp_set_current_user( self::$editor_id ); 277 $request = new WP_REST_Request( $method, '/wp/v2/posts/' . self::$post_id . '/revisions/' . $this->revision_id1 ); 278 279 $hook_name = 'rest_prepare_revision'; 280 $filter = new MockAction(); 281 $callback = array( $filter, 'filter' ); 282 add_filter( $hook_name, $callback ); 283 $header_filter = new class() { 284 public static function add_custom_header( $response ) { 285 $response->header( 'X-Test-Header', 'Test' ); 286 287 return $response; 288 } 289 }; 290 add_filter( $hook_name, array( $header_filter, 'add_custom_header' ) ); 291 $response = rest_get_server()->dispatch( $request ); 292 remove_filter( $hook_name, $callback ); 293 remove_filter( $hook_name, array( $header_filter, 'add_custom_header' ) ); 294 295 $this->assertSame( 200, $response->get_status(), 'The response status should be 200.' ); 296 $this->assertSame( 1, $filter->get_call_count(), 'The "' . $hook_name . '" filter was not called when it should be for GET/HEAD requests.' ); 297 $headers = $response->get_headers(); 298 $this->assertArrayHasKey( 'X-Test-Header', $headers, 'The "X-Test-Header" header should be present in the response.' ); 299 $this->assertSame( 'Test', $headers['X-Test-Header'], 'The "X-Test-Header" header value should be equal to "Test".' ); 300 if ( 'GET' === $method ) { 301 return null; 302 } 303 $this->assertNull( $response->get_data(), 'The server should not generate a body in response to a HEAD request.' ); 304 } 305 216 306 public function test_get_item_embed_context() { 217 307 wp_set_current_user( self::$editor_id ); … … 232 322 } 233 323 234 public function test_get_item_no_permission() { 324 /** 325 * @dataProvider data_readable_http_methods 326 * @ticket 56481 327 * 328 * @param string $method The HTTP method to use. 329 */ 330 public function test_get_item_no_permission( $method ) { 235 331 wp_set_current_user( 0 ); 236 $request = new WP_REST_Request( 'GET', '/wp/v2/posts/' . self::$post_id . '/revisions/' . $this->revision_id1 );332 $request = new WP_REST_Request( $method, '/wp/v2/posts/' . self::$post_id . '/revisions/' . $this->revision_id1 ); 237 333 238 334 $response = rest_get_server()->dispatch( $request ); … … 243 339 } 244 340 245 public function test_get_item_missing_parent() { 246 wp_set_current_user( self::$editor_id ); 247 $request = new WP_REST_Request( 'GET', '/wp/v2/posts/' . REST_TESTS_IMPOSSIBLY_HIGH_NUMBER . '/revisions/' . $this->revision_id1 ); 341 /** 342 * @dataProvider data_readable_http_methods 343 * @ticket 56481 344 * 345 * @param string $method The HTTP method to use. 346 */ 347 public function test_get_item_missing_parent( $method ) { 348 wp_set_current_user( self::$editor_id ); 349 $request = new WP_REST_Request( $method, '/wp/v2/posts/' . REST_TESTS_IMPOSSIBLY_HIGH_NUMBER . '/revisions/' . $this->revision_id1 ); 248 350 $response = rest_get_server()->dispatch( $request ); 249 351 $this->assertErrorResponse( 'rest_post_invalid_parent', $response, 404 ); 250 352 } 251 353 252 public function test_get_item_invalid_parent_post_type() { 253 wp_set_current_user( self::$editor_id ); 254 $request = new WP_REST_Request( 'GET', '/wp/v2/posts/' . self::$page_id . '/revisions/' . $this->revision_id1 ); 354 /** 355 * @dataProvider data_readable_http_methods 356 * @ticket 56481 357 * 358 * @param string $method The HTTP method to use. 359 */ 360 public function test_get_item_invalid_parent_post_type( $method ) { 361 wp_set_current_user( self::$editor_id ); 362 $request = new WP_REST_Request( $method, '/wp/v2/posts/' . self::$page_id . '/revisions/' . $this->revision_id1 ); 255 363 $response = rest_get_server()->dispatch( $request ); 256 364 $this->assertErrorResponse( 'rest_post_invalid_parent', $response, 404 ); … … 270 378 271 379 /** 380 * @dataProvider data_readable_http_methods 272 381 * @ticket 59875 273 */ 274 public function test_get_item_invalid_parent_id() { 275 wp_set_current_user( self::$editor_id ); 276 $request = new WP_REST_Request( 'GET', '/wp/v2/posts/' . self::$post_id . '/revisions/' . $this->revision_2_1_id ); 382 * @ticket 56481 383 * 384 * @param string $method The HTTP method to use. 385 */ 386 public function test_get_item_invalid_parent_id( $method ) { 387 wp_set_current_user( self::$editor_id ); 388 $request = new WP_REST_Request( $method, '/wp/v2/posts/' . self::$post_id . '/revisions/' . $this->revision_2_1_id ); 277 389 $response = rest_get_server()->dispatch( $request ); 278 390 $this->assertErrorResponse( 'rest_revision_parent_id_mismatch', $response, 404 ); … … 511 623 * Test the pagination header of the first page. 512 624 * 513 * @ticket 40510 514 */ 515 public function test_get_items_pagination_header_of_the_first_page() { 625 * @dataProvider data_readable_http_methods 626 * @ticket 40510 627 * @ticket 56481 628 * 629 * @param string $method The HTTP method to use. 630 */ 631 public function test_get_items_pagination_header_of_the_first_page( $method ) { 516 632 wp_set_current_user( self::$editor_id ); 517 633 … … 521 637 $page = 1; // First page. 522 638 523 $request = new WP_REST_Request( 'GET', $rest_route );639 $request = new WP_REST_Request( $method, $rest_route ); 524 640 $request->set_query_params( 525 641 array( … … 546 662 * Test the pagination header of the last page. 547 663 * 548 * @ticket 40510 549 */ 550 public function test_get_items_pagination_header_of_the_last_page() { 664 * @dataProvider data_readable_http_methods 665 * @ticket 40510 666 * @ticket 56481 667 * 668 * @param string $method The HTTP method to use. 669 */ 670 public function test_get_items_pagination_header_of_the_last_page( $method ) { 551 671 wp_set_current_user( self::$editor_id ); 552 672 … … 556 676 $page = 2; // Last page. 557 677 558 $request = new WP_REST_Request( 'GET', $rest_route );678 $request = new WP_REST_Request( $method, $rest_route ); 559 679 $request->set_query_params( 560 680 array( … … 577 697 } 578 698 699 579 700 /** 580 701 * Test that invalid 'per_page' query should error. 581 702 * 582 * @ticket 40510 583 */ 584 public function test_get_items_invalid_per_page_should_error() { 703 * @dataProvider data_readable_http_methods 704 * @ticket 40510 705 * @ticket 56481 706 * 707 * @param string $method The HTTP method to use. 708 */ 709 public function test_get_items_invalid_per_page_should_error( $method ) { 585 710 wp_set_current_user( self::$editor_id ); 586 711 … … 589 714 $expected_status = 400; 590 715 591 $request = new WP_REST_Request( 'GET', '/wp/v2/posts/' . self::$post_id . '/revisions' );716 $request = new WP_REST_Request( $method, '/wp/v2/posts/' . self::$post_id . '/revisions' ); 592 717 $request->set_param( 'per_page', $per_page ); 593 718 $response = rest_get_server()->dispatch( $request ); … … 598 723 * Test that out of bounds 'page' query should error. 599 724 * 600 * @ticket 40510 601 */ 602 public function test_get_items_out_of_bounds_page_should_error() { 725 * @dataProvider data_readable_http_methods 726 * @ticket 40510 727 * @ticket 56481 728 * 729 * @param string $method The HTTP method to use. 730 */ 731 public function test_get_items_out_of_bounds_page_should_error( $method ) { 603 732 wp_set_current_user( self::$editor_id ); 604 733 … … 609 738 $expected_status = 400; 610 739 611 $request = new WP_REST_Request( 'GET', '/wp/v2/posts/' . self::$post_id . '/revisions' );740 $request = new WP_REST_Request( $method, '/wp/v2/posts/' . self::$post_id . '/revisions' ); 612 741 $request->set_query_params( 613 742 array( … … 623 752 * Test that impossibly high 'page' query should error. 624 753 * 625 * @ticket 40510 626 */ 627 public function test_get_items_invalid_max_pages_should_error() { 754 * @dataProvider data_readable_http_methods 755 * @ticket 40510 756 * @ticket 56481 757 * 758 * @param string $method The HTTP method to use. 759 */ 760 public function test_get_items_invalid_max_pages_should_error( $method ) { 628 761 wp_set_current_user( self::$editor_id ); 629 762 … … 633 766 $expected_status = 400; 634 767 635 $request = new WP_REST_Request( 'GET', '/wp/v2/posts/' . self::$post_id . '/revisions' );768 $request = new WP_REST_Request( $method, '/wp/v2/posts/' . self::$post_id . '/revisions' ); 636 769 $request->set_query_params( 637 770 array( … … 771 904 * Test that out of bound 'offset' query should error. 772 905 * 773 * @ticket 40510 774 */ 775 public function test_get_items_out_of_bound_offset_should_error() { 906 * @dataProvider data_readable_http_methods 907 * @ticket 40510 908 * @ticket 56481 909 * 910 * @param string $method The HTTP method to use. 911 */ 912 public function test_get_items_out_of_bound_offset_should_error( $method ) { 776 913 wp_set_current_user( self::$editor_id ); 777 914 … … 781 918 $expected_status = 400; 782 919 783 $request = new WP_REST_Request( 'GET', '/wp/v2/posts/' . self::$post_id . '/revisions' );920 $request = new WP_REST_Request( $method, '/wp/v2/posts/' . self::$post_id . '/revisions' ); 784 921 $request->set_query_params( 785 922 array( … … 795 932 * Test that impossible high number for 'offset' query should error. 796 933 * 797 * @ticket 40510 798 */ 799 public function test_get_items_impossible_high_number_offset_should_error() { 934 * @dataProvider data_readable_http_methods 935 * @ticket 40510 936 * @ticket 56481 937 * 938 * @param string $method The HTTP method to use. 939 */ 940 public function test_get_items_impossible_high_number_offset_should_error( $method ) { 800 941 wp_set_current_user( self::$editor_id ); 801 942 … … 805 946 $expected_status = 400; 806 947 807 $request = new WP_REST_Request( 'GET', '/wp/v2/posts/' . self::$post_id . '/revisions' );948 $request = new WP_REST_Request( $method, '/wp/v2/posts/' . self::$post_id . '/revisions' ); 808 949 $request->set_query_params( 809 950 array( … … 819 960 * Test that invalid 'offset' query should error. 820 961 * 821 * @ticket 40510 822 */ 823 public function test_get_items_invalid_offset_should_error() { 962 * @dataProvider data_readable_http_methods 963 * @ticket 40510 964 * @ticket 56481 965 * 966 * @param string $method The HTTP method to use. 967 */ 968 public function test_get_items_invalid_offset_should_error( $method ) { 824 969 wp_set_current_user( self::$editor_id ); 825 970 … … 829 974 $expected_status = 400; 830 975 831 $request = new WP_REST_Request( 'GET', '/wp/v2/posts/' . self::$post_id . '/revisions' );976 $request = new WP_REST_Request( $method, '/wp/v2/posts/' . self::$post_id . '/revisions' ); 832 977 $request->set_query_params( 833 978 array(
Note: See TracChangeset
for help on using the changeset viewer.