- 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-comments-controller.php
r57176 r59899 433 433 } 434 434 435 public function test_get_items_no_permission_for_no_post() { 435 /** 436 * @dataProvider data_readable_http_methods 437 * @ticket 56481 438 * 439 * @param string $method HTTP method to use. 440 */ 441 public function test_get_items_no_permission_for_no_post( $method ) { 436 442 wp_set_current_user( 0 ); 437 443 438 $request = new WP_REST_Request( 'GET', '/wp/v2/comments' );444 $request = new WP_REST_Request( $method, '/wp/v2/comments' ); 439 445 $request->set_param( 'post', 0 ); 440 446 $response = rest_get_server()->dispatch( $request ); … … 442 448 } 443 449 444 public function test_get_items_edit_context() { 445 wp_set_current_user( self::$admin_id ); 446 447 $request = new WP_REST_Request( 'GET', '/wp/v2/comments' ); 450 /** 451 * Data provider intended to provide HTTP method names for testing GET and HEAD requests. 452 * 453 * @return array 454 */ 455 public static function data_readable_http_methods() { 456 return array( 457 'GET request' => array( 'GET' ), 458 'HEAD request' => array( 'HEAD' ), 459 ); 460 } 461 462 /** 463 * @dataProvider data_readable_http_methods 464 * @ticket 56481 465 * 466 * @param string $method HTTP method to use. 467 */ 468 public function test_get_items_edit_context( $method ) { 469 wp_set_current_user( self::$admin_id ); 470 471 $request = new WP_REST_Request( $method, '/wp/v2/comments' ); 448 472 $request->set_param( 'context', 'edit' ); 449 473 $response = rest_get_server()->dispatch( $request ); … … 594 618 } 595 619 596 public function test_get_items_private_post_no_permissions() { 620 /** 621 * @dataProvider data_readable_http_methods 622 * @ticket 56481 623 * 624 * @param string $method HTTP method to use. 625 */ 626 public function test_get_items_private_post_no_permissions( $method ) { 597 627 wp_set_current_user( 0 ); 598 628 599 629 $post_id = self::factory()->post->create( array( 'post_status' => 'private' ) ); 600 630 601 $request = new WP_REST_Request( 'GET', '/wp/v2/comments' );631 $request = new WP_REST_Request( $method, '/wp/v2/comments' ); 602 632 $request->set_param( 'post', $post_id ); 603 633 $response = rest_get_server()->dispatch( $request ); … … 802 832 } 803 833 804 public function test_get_comments_pagination_headers() { 834 /** 835 * @dataProvider data_readable_http_methods 836 * @ticket 56481 837 * 838 * @param string $method HTTP method to use. 839 */ 840 public function test_get_comments_pagination_headers( $method ) { 805 841 $total_comments = self::$total_comments; 806 842 $total_pages = (int) ceil( $total_comments / 10 ); … … 809 845 810 846 // Start of the index. 811 $request = new WP_REST_Request( 'GET', '/wp/v2/comments' );847 $request = new WP_REST_Request( $method, '/wp/v2/comments' ); 812 848 $response = rest_get_server()->dispatch( $request ); 813 849 $headers = $response->get_headers(); … … 885 921 } 886 922 887 public function test_get_comments_invalid_date() { 888 $request = new WP_REST_Request( 'GET', '/wp/v2/comments' ); 923 /** 924 * @dataProvider data_readable_http_methods 925 * @ticket 56481 926 * 927 * @param string $method HTTP method to use. 928 */ 929 public function test_get_comments_invalid_date( $method ) { 930 $request = new WP_REST_Request( $method, '/wp/v2/comments' ); 889 931 $request->set_param( 'after', 'foo' ); 890 932 $request->set_param( 'before', 'bar' ); … … 998 1040 } 999 1041 1000 public function test_get_comment_invalid_id() { 1001 $request = new WP_REST_Request( 'GET', '/wp/v2/comments/' . REST_TESTS_IMPOSSIBLY_HIGH_NUMBER ); 1042 /** 1043 * @dataProvider data_readable_http_methods 1044 * @ticket 56481 1045 * 1046 * @param string $method HTTP method to use. 1047 */ 1048 public function test_get_comment_invalid_id( $method ) { 1049 $request = new WP_REST_Request( $method, '/wp/v2/comments/' . REST_TESTS_IMPOSSIBLY_HIGH_NUMBER ); 1002 1050 1003 1051 $response = rest_get_server()->dispatch( $request ); … … 1005 1053 } 1006 1054 1007 public function test_get_comment_invalid_context() { 1055 /** 1056 * @dataProvider data_readable_http_methods 1057 * @ticket 56481 1058 * 1059 * @param string $method HTTP method to use. 1060 */ 1061 public function test_get_comment_invalid_context( $method ) { 1008 1062 wp_set_current_user( 0 ); 1009 1063 1010 $request = new WP_REST_Request( 'GET', sprintf( '/wp/v2/comments/%s', self::$approved_id ) );1064 $request = new WP_REST_Request( $method, sprintf( '/wp/v2/comments/%s', self::$approved_id ) ); 1011 1065 $request->set_param( 'context', 'edit' ); 1012 1066 $response = rest_get_server()->dispatch( $request ); … … 1014 1068 } 1015 1069 1016 public function test_get_comment_invalid_post_id() { 1070 /** 1071 * @dataProvider data_readable_http_methods 1072 * @ticket 56481 1073 * 1074 * @param string $method HTTP method to use. 1075 */ 1076 public function test_get_comment_invalid_post_id( $method ) { 1017 1077 wp_set_current_user( 0 ); 1018 1078 … … 1024 1084 ); 1025 1085 1026 $request = new WP_REST_Request( 'GET', '/wp/v2/comments/' . $comment_id );1086 $request = new WP_REST_Request( $method, '/wp/v2/comments/' . $comment_id ); 1027 1087 $response = rest_get_server()->dispatch( $request ); 1028 1088 $this->assertErrorResponse( 'rest_post_invalid_id', $response, 404 ); 1029 1089 } 1030 1090 1031 public function test_get_comment_invalid_post_id_as_admin() { 1091 /** 1092 * @dataProvider data_readable_http_methods 1093 * @ticket 56481 1094 * 1095 * @param string $method HTTP method to use. 1096 */ 1097 public function test_get_comment_invalid_post_id_as_admin( $method ) { 1032 1098 wp_set_current_user( self::$admin_id ); 1033 1099 … … 1039 1105 ); 1040 1106 1041 $request = new WP_REST_Request( 'GET', '/wp/v2/comments/' . $comment_id );1107 $request = new WP_REST_Request( $method, '/wp/v2/comments/' . $comment_id ); 1042 1108 $response = rest_get_server()->dispatch( $request ); 1043 1109 $this->assertErrorResponse( 'rest_post_invalid_id', $response, 404 ); 1044 1110 } 1045 1111 1046 public function test_get_comment_not_approved() { 1112 /** 1113 * @dataProvider data_readable_http_methods 1114 * @ticket 56481 1115 * 1116 * @param string $method HTTP method to use. 1117 */ 1118 public function test_get_comment_not_approved( $method ) { 1047 1119 wp_set_current_user( 0 ); 1048 1120 1049 $request = new WP_REST_Request( 'GET', sprintf( '/wp/v2/comments/%d', self::$hold_id ) );1121 $request = new WP_REST_Request( $method, sprintf( '/wp/v2/comments/%d', self::$hold_id ) ); 1050 1122 $response = rest_get_server()->dispatch( $request ); 1051 1123 $this->assertErrorResponse( 'rest_cannot_read', $response, 401 ); 1052 1124 } 1053 1125 1054 public function test_get_comment_not_approved_same_user() { 1055 wp_set_current_user( self::$admin_id ); 1056 1057 $request = new WP_REST_Request( 'GET', sprintf( '/wp/v2/comments/%d', self::$hold_id ) ); 1126 /** 1127 * @dataProvider data_readable_http_methods 1128 * @ticket 56481 1129 * 1130 * @param string $method HTTP method to use. 1131 */ 1132 public function test_get_comment_not_approved_same_user( $method ) { 1133 wp_set_current_user( self::$admin_id ); 1134 1135 $request = new WP_REST_Request( $method, sprintf( '/wp/v2/comments/%d', self::$hold_id ) ); 1058 1136 $response = rest_get_server()->dispatch( $request ); 1059 1137 $this->assertSame( 200, $response->get_status() ); … … 1099 1177 } 1100 1178 1101 public function test_get_comment_with_password_without_edit_post_permission() { 1179 /** 1180 * @dataProvider data_readable_http_methods 1181 * @ticket 56481 1182 * 1183 * @param string $method HTTP method to use. 1184 */ 1185 public function test_get_comment_with_password_without_edit_post_permission( $method ) { 1102 1186 wp_set_current_user( self::$subscriber_id ); 1103 1187 … … 1109 1193 $password_comment = self::factory()->comment->create( $args ); 1110 1194 1111 $request = new WP_REST_Request( 'GET', sprintf( '/wp/v2/comments/%s', $password_comment ) );1195 $request = new WP_REST_Request( $method, sprintf( '/wp/v2/comments/%s', $password_comment ) ); 1112 1196 $response = rest_get_server()->dispatch( $request ); 1113 1197 $this->assertErrorResponse( 'rest_cannot_read', $response, 403 ); … … 1115 1199 1116 1200 /** 1201 * @dataProvider data_readable_http_methods 1117 1202 * @ticket 38692 1118 */ 1119 public function test_get_comment_with_password_with_valid_password() { 1203 * @ticket 56481 1204 * 1205 * @param string $method HTTP method to use. 1206 */ 1207 public function test_get_comment_with_password_with_valid_password( $method ) { 1120 1208 wp_set_current_user( self::$subscriber_id ); 1121 1209 … … 1127 1215 $password_comment = self::factory()->comment->create( $args ); 1128 1216 1129 $request = new WP_REST_Request( 'GET', sprintf( '/wp/v2/comments/%s', $password_comment ) );1217 $request = new WP_REST_Request( $method, sprintf( '/wp/v2/comments/%s', $password_comment ) ); 1130 1218 $request->set_param( 'password', 'toomanysecrets' ); 1131 1219 … … 3366 3454 3367 3455 /** 3456 * @dataProvider data_readable_http_methods 3368 3457 * @ticket 42238 3369 */ 3370 public function test_check_read_post_permission_with_invalid_post_type() { 3458 * @ticket 56481 3459 * 3460 * @param string $method HTTP method to use. 3461 */ 3462 public function test_check_read_post_permission_with_invalid_post_type( $method ) { 3371 3463 register_post_type( 3372 3464 'bug-post', … … 3387 3479 3388 3480 wp_set_current_user( self::$admin_id ); 3389 $request = new WP_REST_Request( 'GET', '/wp/v2/comments/' . $comment_id );3481 $request = new WP_REST_Request( $method, '/wp/v2/comments/' . $comment_id ); 3390 3482 $response = rest_get_server()->dispatch( $request ); 3391 3483 $this->assertSame( 403, $response->get_status() ); 3392 3484 } 3485 3486 /** 3487 * @dataProvider data_readable_http_methods 3488 * @ticket 56481 3489 * 3490 * @param string $method HTTP method to use. 3491 */ 3492 public function test_get_items_only_fetches_ids_for_head_requests( $method ) { 3493 $is_head_request = 'HEAD' === $method; 3494 $request = new WP_REST_Request( $method, '/wp/v2/comments' ); 3495 3496 $filter = new MockAction(); 3497 3498 add_filter( 'comments_pre_query', array( $filter, 'filter' ), 10, 2 ); 3499 3500 $response = rest_get_server()->dispatch( $request ); 3501 3502 $this->assertSame( 200, $response->get_status() ); 3503 if ( $is_head_request ) { 3504 $this->assertEmpty( $response->get_data() ); 3505 } else { 3506 $this->assertNotEmpty( $response->get_data() ); 3507 } 3508 3509 $args = $filter->get_args(); 3510 $this->assertTrue( isset( $args[0][1] ), 'Query parameters were not captured.' ); 3511 $this->assertInstanceOf( WP_Comment_Query::class, $args[0][1], 'Query parameters were not captured.' ); 3512 3513 /** @var WP_Comment_Query $query */ 3514 $query = $args[0][1]; 3515 3516 if ( $is_head_request ) { 3517 $this->assertArrayHasKey( 'fields', $query->query_vars, 'The fields parameter is not set in the query vars.' ); 3518 $this->assertSame( 'ids', $query->query_vars['fields'], 'The query must fetch only post IDs.' ); 3519 $this->assertArrayHasKey( 'update_comment_meta_cache', $query->query_vars, 'The update_comment_meta_cache key is missing in the query vars.' ); 3520 $this->assertFalse( $query->query_vars['update_comment_meta_cache'], 'The update_comment_meta_cache value should be false for HEAD requests.' ); 3521 } else { 3522 $this->assertTrue( ! array_key_exists( 'fields', $query->query_vars ) || 'ids' !== $query->query_vars['fields'], 'The fields parameter should not be forced to "ids" for non-HEAD requests.' ); 3523 $this->assertArrayHasKey( 'update_comment_meta_cache', $query->query_vars, 'The update_comment_meta_cache key is missing in the query vars.' ); 3524 $this->assertTrue( $query->query_vars['update_comment_meta_cache'], 'The update_comment_meta_cache value should be true for non-HEAD requests.' ); 3525 return; 3526 } 3527 3528 global $wpdb; 3529 $comments_table = preg_quote( $wpdb->comments, '/' ); 3530 $pattern = '/^SELECT\s+SQL_CALC_FOUND_ROWS\s+' . $comments_table . '\.comment_ID\s+FROM\s+' . $comments_table . '\s+WHERE/i'; 3531 3532 // Assert that the SQL query only fetches the ID column. 3533 $this->assertMatchesRegularExpression( $pattern, $query->request, 'The SQL query does not match the expected string.' ); 3534 } 3535 3536 /** 3537 * @dataProvider data_readable_http_methods 3538 * @ticket 56481 3539 * 3540 * @param string $method The HTTP method to use. 3541 */ 3542 public function test_get_item_should_allow_adding_headers_via_filter( $method ) { 3543 $request = new WP_REST_Request( $method, sprintf( '/wp/v2/comments/%d', self::$approved_id ) ); 3544 3545 $hook_name = 'rest_prepare_comment'; 3546 3547 $filter = new MockAction(); 3548 $callback = array( $filter, 'filter' ); 3549 add_filter( $hook_name, $callback ); 3550 $header_filter = new class() { 3551 public static function add_custom_header( $response ) { 3552 $response->header( 'X-Test-Header', 'Test' ); 3553 3554 return $response; 3555 } 3556 }; 3557 add_filter( $hook_name, array( $header_filter, 'add_custom_header' ) ); 3558 $response = rest_get_server()->dispatch( $request ); 3559 remove_filter( $hook_name, $callback ); 3560 remove_filter( $hook_name, array( $header_filter, 'add_custom_header' ) ); 3561 3562 $this->assertSame( 200, $response->get_status(), 'The response status should be 200.' ); 3563 $headers = $response->get_headers(); 3564 $this->assertSame( 1, $filter->get_call_count(), 'The "' . $hook_name . '" filter was called when it should not be for HEAD requests.' ); 3565 $this->assertArrayHasKey( 'X-Test-Header', $headers, 'The "X-Test-Header" header should be present in the response.' ); 3566 $this->assertSame( 'Test', $headers['X-Test-Header'], 'The "X-Test-Header" header value should be equal to "Test".' ); 3567 if ( 'HEAD' !== $method ) { 3568 return null; 3569 } 3570 $this->assertNull( $response->get_data(), 'The server should not generate a body in response to a HEAD request.' ); 3571 } 3393 3572 }
Note: See TracChangeset
for help on using the changeset viewer.