Skip to content

Tests: Cover prepare_item_for_response() in REST pages controller - #14384

Closed
rejaulalomkhan wants to merge 2 commits into
WordPress:trunkfrom
rejaulalomkhan:tests/40538-pages-controller-prepare-item
Closed

rejaulalomkhan wants to merge 2 commits into
WordPress:trunkfrom
rejaulalomkhan:tests/40538-pages-controller-prepare-item

Conversation

@rejaulalomkhan

Copy link
Copy Markdown

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() on WP_REST_Posts_Controller('page'):

  • Checks HTTP 200 response status.
  • Validates returned schema fields: id, type, title.raw, content.raw, status, and author.
  • Verifies compact links expansion for self, collection, and about.

Follow-up to [64022] and [64366].

Testing Instructions

  1. Run vendor/bin/phpcs tests/phpunit/tests/rest-api/rest-pages-controller.php
  2. Run vendor/bin/phpunit --filter WP_Test_REST_Pages_Controller
  3. Run vendor/bin/phpunit -c tests/phpunit/multisite.xml --filter WP_Test_REST_Pages_Controller

All 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.

Replace empty test_prepare_item() stub with assertions for response status, returned fields, and links.

See #40538.
@github-actions

github-actions Bot commented Oct 17, 2026 •

Copy link
Copy Markdown

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 props-bot label.

Core Committers: Use this line as a base for the props when committing in SVN:

Props rejaulalomkhan, huzaifaalmesbah.

To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook.

@huzaifaalmesbah huzaifaalmesbah left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Comment on lines +638 to +652
$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'] );

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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?

Suggested change
$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' );

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟢 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, and about links.
File Description
tests/​phpunit/​tests/​rest-api/​rest-pages-controller.php Replaces the empty test stub with response and link assertions.

Review effort: Balanced

wporg-sync pushed a commit that referenced this pull request Oct 17, 2026
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
@lancewillett

Copy link
Copy Markdown
Member

wporg-sync pushed a commit to WordPress/WordPress that referenced this pull request Oct 17, 2026
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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants