Skip to content

Commit 0b08957

Browse files
Add more tests.
1 parent d417639 commit 0b08957

19 files changed

+668
-0
lines changed

tests/phpunit/tests/rest-api/rest-autosaves-controller.php

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -889,4 +889,35 @@ public function test_rest_autosave_do_not_create_autosave_when_post_is_unchanged
889889
$this->assertSame( 200, $response->get_status() );
890890
$this->assertSame( $autosave['id'], $data['id'], 'Original autosave was not returned' );
891891
}
892+
893+
/**
894+
* @dataProvider data_head_request_with_specified_fields_returns_success_response
895+
* @ticket 56481
896+
*
897+
* @param string $path The path to test.
898+
*/
899+
public function test_head_request_with_specified_fields_returns_success_response( $path ) {
900+
wp_set_current_user( self::$editor_id );
901+
$request = new WP_REST_Request( 'HEAD', sprintf( $path, self::$post_id, self::$autosave_post_id ) );
902+
$request->set_param( '_fields', 'id' );
903+
$server = rest_get_server();
904+
$response = $server->dispatch( $request );
905+
add_filter( 'rest_post_dispatch', 'rest_filter_response_fields', 10, 3 );
906+
$response = apply_filters( 'rest_post_dispatch', $response, $server, $request );
907+
remove_filter( 'rest_post_dispatch', 'rest_filter_response_fields', 10 );
908+
909+
$this->assertSame( 200, $response->get_status(), 'The response status should be 200.' );
910+
}
911+
912+
/**
913+
* Data provider intended to provide paths for testing HEAD requests.
914+
*
915+
* @return array
916+
*/
917+
public static function data_head_request_with_specified_fields_returns_success_response() {
918+
return array(
919+
'get_item request' => array( '/wp/v2/posts/%d/autosaves/%d' ),
920+
'get_items request' => array( '/wp/v2/posts/%d' ),
921+
);
922+
}
892923
}

tests/phpunit/tests/rest-api/rest-block-type-controller.php

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -662,6 +662,37 @@ public function test_get_items_with_head_request_should_not_prepare_block_type_d
662662
$this->assertSame( array(), $response->get_data(), 'The server should not generate a body in response to a HEAD request.' );
663663
}
664664

665+
/**
666+
* @dataProvider data_head_request_with_specified_fields_returns_success_response
667+
* @ticket 56481
668+
*
669+
* @param string $path The path to test.
670+
*/
671+
public function test_head_request_with_specified_fields_returns_success_response( $path ) {
672+
wp_set_current_user( self::$admin_id );
673+
$request = new WP_REST_Request( 'HEAD', $path );
674+
$request->set_param( '_fields', 'title' );
675+
$server = rest_get_server();
676+
$response = $server->dispatch( $request );
677+
add_filter( 'rest_post_dispatch', 'rest_filter_response_fields', 10, 3 );
678+
$response = apply_filters( 'rest_post_dispatch', $response, $server, $request );
679+
remove_filter( 'rest_post_dispatch', 'rest_filter_response_fields', 10 );
680+
681+
$this->assertSame( 200, $response->get_status(), 'The response status should be 200.' );
682+
}
683+
684+
/**
685+
* Data provider intended to provide paths for testing HEAD requests.
686+
*
687+
* @return array
688+
*/
689+
public static function data_head_request_with_specified_fields_returns_success_response() {
690+
return array(
691+
'get_item request' => array( '/wp/v2/block-types/fake/test' ),
692+
'get_items request' => array( '/wp/v2/block-types' ),
693+
);
694+
}
695+
665696
/**
666697
* @dataProvider data_readable_http_methods
667698
* @ticket 47620

tests/phpunit/tests/rest-api/rest-categories-controller.php

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1362,4 +1362,34 @@ public static function add_custom_header( $response ) {
13621362
}
13631363
$this->assertSame( array(), $response->get_data(), 'The server should not generate a body in response to a HEAD request.' );
13641364
}
1365+
1366+
/**
1367+
* @dataProvider data_head_request_with_specified_fields_returns_success_response
1368+
* @ticket 56481
1369+
*
1370+
* @param string $path The path to test.
1371+
*/
1372+
public function test_head_request_with_specified_fields_returns_success_response( $path ) {
1373+
$request = new WP_REST_Request( 'HEAD', $path );
1374+
$request->set_param( '_fields', 'id' );
1375+
$server = rest_get_server();
1376+
$response = $server->dispatch( $request );
1377+
add_filter( 'rest_post_dispatch', 'rest_filter_response_fields', 10, 3 );
1378+
$response = apply_filters( 'rest_post_dispatch', $response, $server, $request );
1379+
remove_filter( 'rest_post_dispatch', 'rest_filter_response_fields', 10 );
1380+
1381+
$this->assertSame( 200, $response->get_status(), 'The response status should be 200.' );
1382+
}
1383+
1384+
/**
1385+
* Data provider intended to provide paths for testing HEAD requests.
1386+
*
1387+
* @return array
1388+
*/
1389+
public static function data_head_request_with_specified_fields_returns_success_response() {
1390+
return array(
1391+
'get_item request' => array( '/wp/v2/categories/1' ),
1392+
'get_items request' => array( '/wp/v2/categories' ),
1393+
);
1394+
}
13651395
}

tests/phpunit/tests/rest-api/rest-comments-controller.php

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3569,4 +3569,34 @@ public static function add_custom_header( $response ) {
35693569
}
35703570
$this->assertSame( array(), $response->get_data(), 'The server should not generate a body in response to a HEAD request.' );
35713571
}
3572+
3573+
/**
3574+
* @dataProvider data_head_request_with_specified_fields_returns_success_response
3575+
* @ticket 56481
3576+
*
3577+
* @param string $path The path to test.
3578+
*/
3579+
public function test_head_request_with_specified_fields_returns_success_response( $path ) {
3580+
$request = new WP_REST_Request( 'HEAD', sprintf( $path, self::$approved_id ) );
3581+
$request->set_param( '_fields', 'id' );
3582+
$server = rest_get_server();
3583+
$response = $server->dispatch( $request );
3584+
add_filter( 'rest_post_dispatch', 'rest_filter_response_fields', 10, 3 );
3585+
$response = apply_filters( 'rest_post_dispatch', $response, $server, $request );
3586+
remove_filter( 'rest_post_dispatch', 'rest_filter_response_fields', 10 );
3587+
3588+
$this->assertSame( 200, $response->get_status(), 'The response status should be 200.' );
3589+
}
3590+
3591+
/**
3592+
* Data provider intended to provide paths for testing HEAD requests.
3593+
*
3594+
* @return array
3595+
*/
3596+
public static function data_head_request_with_specified_fields_returns_success_response() {
3597+
return array(
3598+
'get_item request' => array( '/wp/v2/comments/%d' ),
3599+
'get_items request' => array( '/wp/v2/comments' ),
3600+
);
3601+
}
35723602
}

tests/phpunit/tests/rest-api/rest-global-styles-revisions-controller.php

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -339,6 +339,37 @@ public function test_get_items_should_return_no_response_body_for_head_requests(
339339
$this->assertSame( array(), $response->get_data(), 'The server should not generate a body in response to a HEAD request.' );
340340
}
341341

342+
/**
343+
* @dataProvider data_head_request_with_specified_fields_returns_success_response
344+
* @ticket 56481
345+
*
346+
* @param string $path The path to test.
347+
*/
348+
public function test_head_request_with_specified_fields_returns_success_response( $path ) {
349+
wp_set_current_user( self::$admin_id );
350+
$request = new WP_REST_Request( 'GET', sprintf( $path, self::$global_styles_id, $this->revision_1_id ) );
351+
$request->set_param( '_fields', 'id' );
352+
$server = rest_get_server();
353+
$response = $server->dispatch( $request );
354+
add_filter( 'rest_post_dispatch', 'rest_filter_response_fields', 10, 3 );
355+
$response = apply_filters( 'rest_post_dispatch', $response, $server, $request );
356+
remove_filter( 'rest_post_dispatch', 'rest_filter_response_fields', 10 );
357+
358+
$this->assertSame( 200, $response->get_status(), 'The response status should be 200.' );
359+
}
360+
361+
/**
362+
* Data provider intended to provide paths for testing HEAD requests.
363+
*
364+
* @return array
365+
*/
366+
public static function data_head_request_with_specified_fields_returns_success_response() {
367+
return array(
368+
'get_item request' => array( '/wp/v2/global-styles/%d/revisions/%d' ),
369+
'get_items request' => array( '/wp/v2/global-styles/%d/revisions' ),
370+
);
371+
}
372+
342373
/**
343374
* @ticket 59810
344375
*

tests/phpunit/tests/rest-api/rest-pattern-directory-controller.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,23 @@ public function test_get_items_with_head_request_should_not_prepare_block_patter
170170
$this->assertSame( array(), $response->get_data(), 'The server should not generate a body in response to a HEAD request.' );
171171
}
172172

173+
/**
174+
* @ticket 56481
175+
*/
176+
public function test_get_items_head_request_with_specified_fields_returns_success_response() {
177+
wp_set_current_user( self::$contributor_id );
178+
self::mock_successful_response( 'browse-all', true );
179+
$request = new WP_REST_Request( 'HEAD', '/wp/v2/pattern-directory/patterns' );
180+
$request->set_param( '_fields', 'id' );
181+
$server = rest_get_server();
182+
$response = $server->dispatch( $request );
183+
add_filter( 'rest_post_dispatch', 'rest_filter_response_fields', 10, 3 );
184+
$response = apply_filters( 'rest_post_dispatch', $response, $server, $request );
185+
remove_filter( 'rest_post_dispatch', 'rest_filter_response_fields', 10 );
186+
187+
$this->assertSame( 200, $response->get_status(), 'The response status should be 200.' );
188+
}
189+
173190
/**
174191
* @covers WP_REST_Pattern_Directory_Controller::get_items
175192
*

tests/phpunit/tests/rest-api/rest-post-types-controller.php

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,36 @@ public static function add_custom_header( $response ) {
114114
$this->assertSame( array(), $response->get_data(), 'The server should not generate a body in response to a HEAD request.' );
115115
}
116116

117+
/**
118+
* @dataProvider data_head_request_with_specified_fields_returns_success_response
119+
* @ticket 56481
120+
*
121+
* @param string $path The path to test.
122+
*/
123+
public function test_head_request_with_specified_fields_returns_success_response( $path ) {
124+
$request = new WP_REST_Request( 'HEAD', $path );
125+
$request->set_param( '_fields', 'slug' );
126+
$server = rest_get_server();
127+
$response = $server->dispatch( $request );
128+
add_filter( 'rest_post_dispatch', 'rest_filter_response_fields', 10, 3 );
129+
$response = apply_filters( 'rest_post_dispatch', $response, $server, $request );
130+
remove_filter( 'rest_post_dispatch', 'rest_filter_response_fields', 10 );
131+
132+
$this->assertSame( 200, $response->get_status(), 'The response status should be 200.' );
133+
}
134+
135+
/**
136+
* Data provider intended to provide paths for testing HEAD requests.
137+
*
138+
* @return array
139+
*/
140+
public static function data_head_request_with_specified_fields_returns_success_response() {
141+
return array(
142+
'get_item request' => array( '/wp/v2/types/post' ),
143+
'get_items request' => array( '/wp/v2/types' ),
144+
);
145+
}
146+
117147
/**
118148
* @ticket 53656
119149
*/

tests/phpunit/tests/rest-api/rest-posts-controller.php

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2196,6 +2196,36 @@ public static function add_custom_header( $response ) {
21962196
$this->assertSame( array(), $response->get_data(), 'The server should not generate a body in response to a HEAD request.' );
21972197
}
21982198

2199+
/**
2200+
* @dataProvider data_head_request_with_specified_fields_returns_success_response
2201+
* @ticket 56481
2202+
*
2203+
* @param string $path The path to test.
2204+
*/
2205+
public function test_head_request_with_specified_fields_returns_success_response( $path ) {
2206+
$request = new WP_REST_Request( 'HEAD', sprintf( $path, self::$post_id ) );
2207+
$request->set_param( '_fields', 'id' );
2208+
$server = rest_get_server();
2209+
$response = $server->dispatch( $request );
2210+
add_filter( 'rest_post_dispatch', 'rest_filter_response_fields', 10, 3 );
2211+
$response = apply_filters( 'rest_post_dispatch', $response, $server, $request );
2212+
remove_filter( 'rest_post_dispatch', 'rest_filter_response_fields', 10 );
2213+
2214+
$this->assertSame( 200, $response->get_status(), 'The response status should be 200.' );
2215+
}
2216+
2217+
/**
2218+
* Data provider intended to provide paths for testing HEAD requests.
2219+
*
2220+
* @return array
2221+
*/
2222+
public static function data_head_request_with_specified_fields_returns_success_response() {
2223+
return array(
2224+
'get_item request' => array( '/wp/v2/posts/%d' ),
2225+
'get_items request' => array( '/wp/v2/posts' ),
2226+
);
2227+
}
2228+
21992229
public function test_get_item_links() {
22002230
$request = new WP_REST_Request( 'GET', sprintf( '/wp/v2/posts/%d', self::$post_id ) );
22012231
$response = rest_get_server()->dispatch( $request );

tests/phpunit/tests/rest-api/rest-revisions-controller.php

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -303,6 +303,38 @@ public static function add_custom_header( $response ) {
303303
$this->assertSame( array(), $response->get_data(), 'The server should not generate a body in response to a HEAD request.' );
304304
}
305305

306+
/**
307+
* @dataProvider data_head_request_with_specified_fields_returns_success_response
308+
* @ticket 56481
309+
*
310+
* @param string $path The path to test.
311+
*/
312+
public function test_head_request_with_specified_fields_returns_success_response( $path ) {
313+
wp_set_current_user( self::$editor_id );
314+
$request = new WP_REST_Request( 'HEAD', sprintf( $path, self::$post_id, $this->revision_id1 ) );
315+
$request->set_param( '_fields', 'id' );
316+
$server = rest_get_server();
317+
$response = $server->dispatch( $request );
318+
add_filter( 'rest_post_dispatch', 'rest_filter_response_fields', 10, 3 );
319+
$response = apply_filters( 'rest_post_dispatch', $response, $server, $request );
320+
remove_filter( 'rest_post_dispatch', 'rest_filter_response_fields', 10 );
321+
322+
$this->assertSame( 200, $response->get_status(), 'The response status should be 200.' );
323+
}
324+
325+
/**
326+
* Data provider intended to provide paths for testing HEAD requests.
327+
*
328+
* @return array
329+
*/
330+
public static function data_head_request_with_specified_fields_returns_success_response() {
331+
return array(
332+
333+
'get_item request' => array( '/wp/v2/posts/%d/revisions/%d' ),
334+
'get_items request' => array( '/wp/v2/posts/%d/revisions' ),
335+
);
336+
}
337+
306338
public function test_get_item_embed_context() {
307339
wp_set_current_user( self::$editor_id );
308340
$request = new WP_REST_Request( 'GET', '/wp/v2/posts/' . self::$post_id . '/revisions/' . $this->revision_id1 );

tests/phpunit/tests/rest-api/rest-sidebars-controller.php

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -570,6 +570,45 @@ public static function add_custom_header( $response ) {
570570
$this->assertSame( array(), $response->get_data(), 'The server should not generate a body in response to a HEAD request.' );
571571
}
572572

573+
/**
574+
* @dataProvider data_head_request_with_specified_fields_returns_success_response
575+
* @ticket 56481
576+
*
577+
* @param string $path The path to test.
578+
*/
579+
public function test_head_request_with_specified_fields_returns_success_response( $path ) {
580+
$this->setup_sidebar(
581+
'sidebar-1',
582+
array(
583+
'name' => 'Test sidebar',
584+
)
585+
);
586+
587+
$request = new WP_REST_Request( 'HEAD', $path );
588+
// This endpoint doesn't seem to support _fields param, but we need to set it to reproduce the fatal error.
589+
$request->set_param( '_fields', 'name' );
590+
$server = rest_get_server();
591+
$response = $server->dispatch( $request );
592+
add_filter( 'rest_post_dispatch', 'rest_filter_response_fields', 10, 3 );
593+
$response = apply_filters( 'rest_post_dispatch', $response, $server, $request );
594+
remove_filter( 'rest_post_dispatch', 'rest_filter_response_fields', 10 );
595+
596+
$this->assertSame( 200, $response->get_status(), 'The response status should be 200.' );
597+
}
598+
599+
/**
600+
* Data provider intended to provide paths for testing HEAD requests.
601+
*
602+
* @return array
603+
*/
604+
public static function data_head_request_with_specified_fields_returns_success_response() {
605+
return array(
606+
607+
'get_item request' => array( '/wp/v2/sidebars/sidebar-1' ),
608+
'get_items request' => array( '/wp/v2/sidebars' ),
609+
);
610+
}
611+
573612
/**
574613
* Data provider intended to provide HTTP method names for testing GET and HEAD requests.
575614
*

0 commit comments

Comments
 (0)