Skip to content

Commit 2cd9757

Browse files
committed
Add unit test covering the edge case
1 parent d48cdcd commit 2cd9757

File tree

1 file changed

+51
-9
lines changed

1 file changed

+51
-9
lines changed

tests/phpunit/tests/rest-api/wpRestAbilitiesV1RunController.php

Lines changed: 51 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -921,12 +921,11 @@ public function test_ability_without_annotations_defaults_to_post_method(): void
921921
}
922922

923923
/**
924-
* Test edge case with empty input for both GET and POST methods.
924+
* Test edge case with empty input for GET method.
925925
*
926926
* @ticket 64098
927927
*/
928-
public function test_empty_input_handling(): void {
929-
// Registers abilities for empty input testing.
928+
public function test_empty_input_handling_get_method(): void {
930929
wp_register_ability(
931930
'test/read-only-empty',
932931
array(
@@ -946,6 +945,55 @@ public function test_empty_input_handling(): void {
946945
)
947946
);
948947

948+
// Tests GET with no input parameter.
949+
$get_request = new WP_REST_Request( 'GET', '/wp-abilities/v1/abilities/test/read-only-empty/run' );
950+
$get_response = $this->server->dispatch( $get_request );
951+
$this->assertEquals( 200, $get_response->get_status() );
952+
$this->assertTrue( $get_response->get_data()['input_was_empty'] );
953+
}
954+
955+
/**
956+
* Test edge case with empty input for GET method, and normalized input using schema.
957+
*
958+
* @ticket 64098
959+
*/
960+
public function test_empty_input_handling_get_method_with_normalized_input(): void {
961+
wp_register_ability(
962+
'test/read-only-empty-array',
963+
array(
964+
'label' => 'Read-only Empty Array',
965+
'description' => 'Read-only with inferred empty array input from schema.',
966+
'category' => 'general',
967+
'input_schema' => array(
968+
'type' => 'array',
969+
'default' => array(),
970+
),
971+
'execute_callback' => static function ( $input ) {
972+
return is_array( $input ) && empty( $input );
973+
},
974+
'permission_callback' => '__return_true',
975+
'meta' => array(
976+
'annotations' => array(
977+
'readonly' => true,
978+
),
979+
'show_in_rest' => true,
980+
),
981+
)
982+
);
983+
984+
// Tests GET with no input parameter.
985+
$get_request = new WP_REST_Request( 'GET', '/wp-abilities/v1/abilities/test/read-only-empty-array/run' );
986+
$get_response = $this->server->dispatch( $get_request );
987+
$this->assertEquals( 200, $get_response->get_status() );
988+
$this->assertTrue( $get_response->get_data() );
989+
}
990+
991+
/**
992+
* Test edge case with empty input for POST method.
993+
*
994+
* @ticket 64098
995+
*/
996+
public function test_empty_input_handling_post_method(): void {
949997
wp_register_ability(
950998
'test/regular-empty',
951999
array(
@@ -962,12 +1010,6 @@ public function test_empty_input_handling(): void {
9621010
)
9631011
);
9641012

965-
// Tests GET with no input parameter.
966-
$get_request = new WP_REST_Request( 'GET', '/wp-abilities/v1/abilities/test/read-only-empty/run' );
967-
$get_response = $this->server->dispatch( $get_request );
968-
$this->assertEquals( 200, $get_response->get_status() );
969-
$this->assertTrue( $get_response->get_data()['input_was_empty'] );
970-
9711013
// Tests POST with no body.
9721014
$post_request = new WP_REST_Request( 'POST', '/wp-abilities/v1/abilities/test/regular-empty/run' );
9731015
$post_request->set_header( 'Content-Type', 'application/json' );

0 commit comments

Comments
 (0)