Skip to content

Commit 4954283

Browse files
committed
Add tests cases covering context and filtering by fields in REST API
1 parent 61a06f4 commit 4954283

File tree

1 file changed

+45
-17
lines changed

1 file changed

+45
-17
lines changed

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

Lines changed: 45 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -295,15 +295,60 @@ public function test_get_item(): void {
295295
$this->assertEquals( 200, $response->get_status() );
296296

297297
$data = $response->get_data();
298+
$this->assertCount( 7, $data, 'Response should contain all fields.' );
298299
$this->assertEquals( 'test/calculator', $data['name'] );
299300
$this->assertEquals( 'Calculator', $data['label'] );
300301
$this->assertEquals( 'Performs basic calculations', $data['description'] );
302+
$this->assertEquals( 'math', $data['category'] );
301303
$this->assertArrayHasKey( 'input_schema', $data );
302304
$this->assertArrayHasKey( 'output_schema', $data );
303305
$this->assertArrayHasKey( 'meta', $data );
304306
$this->assertTrue( $data['meta']['show_in_rest'] );
305307
}
306308

309+
/**
310+
* Test getting a specific ability with only selected fields.
311+
*
312+
* @ticket 64098
313+
*/
314+
public function test_get_item_with_selected_fields(): void {
315+
$request = new WP_REST_Request( 'GET', '/wp/v2/abilities/test/calculator' );
316+
$request->set_param( '_fields', 'name,label' );
317+
$response = $this->server->dispatch( $request );
318+
add_filter( 'rest_post_dispatch', 'rest_filter_response_fields', 10, 3 );
319+
$response = apply_filters( 'rest_post_dispatch', $response, $this->server, $request );
320+
remove_filter( 'rest_post_dispatch', 'rest_filter_response_fields', 10 );
321+
322+
$this->assertEquals( 200, $response->get_status() );
323+
324+
$data = $response->get_data();
325+
$this->assertCount( 2, $data, 'Response should only contain the requested fields.' );
326+
$this->assertEquals( 'test/calculator', $data['name'] );
327+
$this->assertEquals( 'Calculator', $data['label'] );
328+
}
329+
330+
/**
331+
* Test getting a specific ability with embed context.
332+
*
333+
* @ticket 64098
334+
*/
335+
public function test_get_item_with_embed_context(): void {
336+
$request = new WP_REST_Request( 'GET', '/wp/v2/abilities/test/calculator' );
337+
$request->set_param( 'context', 'embed' );
338+
$response = $this->server->dispatch( $request );
339+
add_filter( 'rest_post_dispatch', 'rest_filter_response_fields', 10, 3 );
340+
$response = apply_filters( 'rest_post_dispatch', $response, $this->server, $request );
341+
remove_filter( 'rest_post_dispatch', 'rest_filter_response_fields', 10 );
342+
343+
$this->assertEquals( 200, $response->get_status() );
344+
345+
$data = $response->get_data();
346+
$this->assertCount( 3, $data, 'Response should only contain the fields for embed context.' );
347+
$this->assertEquals( 'test/calculator', $data['name'] );
348+
$this->assertEquals( 'Calculator', $data['label'] );
349+
$this->assertEquals( 'math', $data['category'] );
350+
}
351+
307352
/**
308353
* Test getting a non-existent ability returns 404.
309354
*
@@ -729,21 +774,4 @@ public function test_filter_by_nonexistent_category(): void {
729774
$this->assertIsArray( $data );
730775
$this->assertEmpty( $data, 'Should return empty array for non-existent category' );
731776
}
732-
733-
/**
734-
* Test that category field is present in response.
735-
*
736-
* @ticket 64098
737-
*/
738-
public function test_category_field_in_response(): void {
739-
$request = new WP_REST_Request( 'GET', '/wp/v2/abilities/test/calculator' );
740-
$response = $this->server->dispatch( $request );
741-
742-
$this->assertEquals( 200, $response->get_status() );
743-
744-
$data = $response->get_data();
745-
$this->assertArrayHasKey( 'category', $data );
746-
$this->assertEquals( 'math', $data['category'] );
747-
$this->assertIsString( $data['category'], 'Category should be a string' );
748-
}
749777
}

0 commit comments

Comments
 (0)