Repository navigation
Tests: Cover prepare_item_for_response() in REST pages controller - #14384
rejaulalomkhan wants to merge 2 commits into
Conversation
Replace empty test_prepare_item() stub with assertions for response status, returned fields, and links. See #40538.
|
The following accounts have interacted with this PR and/or linked issues. I will continue to update these lists as activity occurs. You can also manually ask me to refresh this list by adding the Core Committers: Use this line as a base for the props when committing in SVN: To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook. |
huzaifaalmesbah
left a comment
There was a problem hiding this comment.
Thanks for working on this, @rejaulalomkhan! Turning the empty test_prepare_item() stub into a real test is a nice step for #40538. I left one small note inline.
AI Review: Opus 5.5
| $this->assertInstanceOf( WP_REST_Response::class, $response ); | ||
| $this->assertSame( 200, $response->get_status() ); | ||
|
|
||
| $data = $response->get_data(); | ||
| $this->assertSame( $page_id, $data['id'] ); | ||
| $this->assertSame( 'page', $data['type'] ); | ||
| $this->assertSame( 'Sample Page', $data['title']['raw'] ); | ||
| $this->assertSame( 'Sample content', $data['content']['raw'] ); | ||
| $this->assertSame( 'publish', $data['status'] ); | ||
| $this->assertSame( self::$editor_id, $data['author'] ); | ||
|
|
||
| $links = test_rest_expand_compact_links( $response->get_links() ); | ||
| $this->assertSame( rest_url( sprintf( 'wp/v2/pages/%d', $page_id ) ), $links['self'][0]['href'] ); | ||
| $this->assertSame( rest_url( 'wp/v2/pages' ), $links['collection'][0]['href'] ); | ||
| $this->assertSame( rest_url( 'wp/v2/types/page' ), $links['about'][0]['href'] ); |
There was a problem hiding this comment.
The matching test in rest-posts-controller.php uses check_get_post_response( $response, 'edit' ), which checks every field and link against the page instead of a hand-picked subset. Would it work to reuse it here?
| $this->assertInstanceOf( WP_REST_Response::class, $response ); | |
| $this->assertSame( 200, $response->get_status() ); | |
| $data = $response->get_data(); | |
| $this->assertSame( $page_id, $data['id'] ); | |
| $this->assertSame( 'page', $data['type'] ); | |
| $this->assertSame( 'Sample Page', $data['title']['raw'] ); | |
| $this->assertSame( 'Sample content', $data['content']['raw'] ); | |
| $this->assertSame( 'publish', $data['status'] ); | |
| $this->assertSame( self::$editor_id, $data['author'] ); | |
| $links = test_rest_expand_compact_links( $response->get_links() ); | |
| $this->assertSame( rest_url( sprintf( 'wp/v2/pages/%d', $page_id ) ), $links['self'][0]['href'] ); | |
| $this->assertSame( rest_url( 'wp/v2/pages' ), $links['collection'][0]['href'] ); | |
| $this->assertSame( rest_url( 'wp/v2/types/page' ), $links['about'][0]['href'] ); | |
| $this->check_get_post_response( $response, 'edit' ); |
There was a problem hiding this comment.
Good catch @huzaifaalmesbah! I tested $this->check_get_post_response( $response, 'edit' ) locally, but it fails with:
Failed asserting that an array does not have the key 'excerpt'.
In WP_Test_REST_Post_Type_Controller_Testcase::check_post_data() (line 155), it asserts $this->assertArrayNotHasKey( 'excerpt', $data ) whenever ! post_type_supports( $post->post_type, 'excerpt' ). Because page does not register excerpt support in Core, but WP_REST_Posts_Controller hardcodes excerpt into the schema for pages ($fixed_schemas['page']), check_get_post_response() cannot be used directly without tripping on that discrepancy (which is also why test_get_item() and test_update_item() in this file assert fields explicitly).
To expand the coverage as intended, I've pushed commit bb5c97a adding explicit assertions for excerpt, parent, and menu_order.
There was a problem hiding this comment.
🟢 Approval recommended
The test-only changes match the controller’s response contract, with no identified blocking issues.
No open issues 🎉
What changed in this PR
Replaces the empty REST pages controller test with coverage for prepare_item_for_response(), supporting ticket #40538.
Changes:
- Verifies response type, HTTP status, and page fields in edit context.
- Checks expanded
self,collection, andaboutlinks.
| File | Description |
|---|---|
| tests/phpunit/tests/rest-api/rest-pages-controller.php | Replaces the empty test stub with response and link assertions. |
Review effort: Balanced
Replace the empty `test_prepare_item()` stub with assertions for page fields in the edit context and the `self`, `collection`, and `about` links returned by `WP_REST_Posts_Controller::prepare_item_for_response()`. Developed in: #14384 Props rejaulalomkhan, huzaifaalmesbah. See #40538. git-svn-id: https://develop.svn.wordpress.org/trunk@64451 602fd350-edb4-49c9-b593-d223f7449a82
|
PR #14384 landed in https://core.trac.wordpress.org/changeset/64451 |
Replace the empty `test_prepare_item()` stub with assertions for page fields in the edit context and the `self`, `collection`, and `about` links returned by `WP_REST_Posts_Controller::prepare_item_for_response()`. Developed in: WordPress/wordpress-develop#14384 Props rejaulalomkhan, huzaifaalmesbah. See #40538. Built from https://develop.svn.wordpress.org/trunk@64451 git-svn-id: http://core.svn.wordpress.org/trunk@63601 1a063a9b-81f0-0310-95a4-ce76da25c4cd
Trac ticket: https://core.trac.wordpress.org/ticket/40538
Description
In
tests/phpunit/tests/rest-api/rest-pages-controller.php,test_prepare_item()was an empty stub annotated with@doesNotPerformAssertions.This PR replaces the empty stub with assertions that verify
prepare_item_for_response()onWP_REST_Posts_Controller('page'):id,type,title.raw,content.raw,status, andauthor.self,collection, andabout.Follow-up to [64022] and [64366].
Testing Instructions
vendor/bin/phpcs tests/phpunit/tests/rest-api/rest-pages-controller.phpvendor/bin/phpunit --filter WP_Test_REST_Pages_Controllervendor/bin/phpunit -c tests/phpunit/multisite.xml --filter WP_Test_REST_Pages_ControllerAll tests pass cleanly.
Use of AI Tools:
I used Antigravity (Gemini 2.5 Pro) to identify the remaining empty stub in
rest-pages-controller.php, run local PHPCS and PHPUnit suites, and craft the assertions following the controller's test patterns.