Repository navigation
Build/Test Tools: Use strict assertions in the REST API tests. - #13683
Closed
haritpanchal wants to merge 1 commit into
Closed
haritpanchal wants to merge 1 commit into
haritpanchal wants to merge 1 commit into
Conversation
|
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. |
Member
wporg-sync
pushed a commit
that referenced
this pull request
Sep 23, 2026
Use type-strict assertions for REST response values and metadata. Match expected types to raw database values and REST schema sanitization, and explicitly check for null when JSON parameters are absent. Developed in: #13683 Props haritpanchal. See #64895. git-svn-id: https://develop.svn.wordpress.org/trunk@63896 602fd350-edb4-49c9-b593-d223f7449a82
wporg-sync
pushed a commit
to WordPress/WordPress
that referenced
this pull request
Sep 23, 2026
Use type-strict assertions for REST response values and metadata. Match expected types to raw database values and REST schema sanitization, and explicitly check for null when JSON parameters are absent. Developed in: WordPress/wordpress-develop#13683 Props haritpanchal. See #64895. Built from https://develop.svn.wordpress.org/trunk@63896 git-svn-id: http://core.svn.wordpress.org/trunk@63065 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What
Reviewed the 12 files claimed on the ticket under
tests/phpunit/tests/rest-api/(excluding 5 files still covered by the open PR #11707:
rest-attachments-controller.php,rest-global-styles-controller.php,wpRestAbilitiesV1CategoriesController.php,wpRestAbilitiesV1ListController.php,wpRestAbilitiesV1RunController.php).44 of 50 loose assertions convert; 6 stay loose as legitimate object-value comparisons.
Converted
rest-posts-controller.php (15/15) —
$data['author'],$data['id'], and thedate/date_gmt/modified/modified_gmtfields are all real int/string types perclass-wp-rest-posts-controller.php((int) $post->post_author,$post->IDsanitizedto int,
mysql_to_rfc3339()returningstring|null), so these convert directly toassertNotSame(). Two calls needed a cast:get_post_meta( $post_id, 'my_custom_int', true )always returns the raw DB string
'123', not int123—assertEquals( 123, ... )waspassing on loose coercion. Confirmed by mutation: reverting the cast to
123fails withFailed asserting that '123' is identical to 123.rest-comments-controller.php (11/11) — same
123/'123'meta pattern as posts.check_comment_data()'s four property comparisons (comment_ID,comment_post_ID,comment_parent,user_id) compareWP_Comment's raw numeric-string properties againstthe REST response's
(int)-cast fields, so each needs(int)on the expected side.Confirmed by mutation: removing one cast fails with
Failed asserting that 2 is identical to '2'.One (comment_approved) is a plain stringcast (
'1'), and one line was simply inconsistent with its own file — the siblingassertion on the line above already used the same
(string)cast withassertSame().rest-users-controller.php (5/9) —
post_authorreassignment checks follow the sameWP_Post::$post_authorstring contract established earlier on this ticket (propsharitpanchal, changeset 63632), plus the same
123/'123'meta pattern. Left unconverted:two comparisons against
new stdClass()and two comparisons of(object) $user->allcaps/(object) $user->capsagainst the controller's own independently-cast copy — both sides arealways different object instances by construction, so
assertSame()would fail on identityalone regardless of matching properties.
rest-autosaves-controller.php (3/3), rest-revisions-controller.php (1/1) — the
shared
check_get_autosave_response()/check_get_revision_response()helpers compare$autosave->post_author/$revision->post_author(string) against the REST response's(int)-castauthorfield; both need the same(int)cast, confirmed by mutation(
Failed asserting that 2 is identical to '2'.).rest-post-meta-fields.php (3/3), rest-term-meta-fields.php (3/3) — identical setup
in both files:
test_custom_schemais registered withshow_in_rest.schema.type => 'number',so the REST-exposed value is sanitized to
(float)— same finding as PR #12613's REST schemasanitization work.
assertSame( 3.0, ... )is required, not3; confirmed by mutation(
Failed asserting that 3.0 is identical to 3.). The two rawget_post_meta()/get_term_meta()reads in the same tests stay string-cast (
'3','2'), since those bypass the REST schemaentirely.
rest-server.php (1/1) —
test_default_param_is_overridden()sets'foo' => 123(a real int, not a query string) directly viaset_query_params(), and no'type'is declared for thefooarg, so no sanitization runs. The originalassertEquals( '123', $request['foo'] )was passing only because of loose int/stringcoercion — the expected literal was simply wrong. Confirmed by first converting straight to
assertSame( '123', ... ), which failed withFailed asserting that 123 is identical to '123'.; fixed toassertSame( 123, ... )tomatch the actual (and intended) behavior — the test is about default-value override, not
type coercion.
rest-themes-controller.php (1/1) —
wp_list_pluck( $data, 'stylesheet' )alwaysreturns an array of plain strings; direct conversion.
rest-request.php (1/1, not a direct swap) —
test_alternate_json_content_type()'shas_json_paramsassertion is a trap: when the JSON body is parsed,get_param()returnsreal bool
true; when it isn't,get_param()returnsnull(a missing key), neverfalse.A direct
assertSame( $accept_json, ... )swap looks correct but actually breaks the twofalsedata sets, sincenull !== false. Confirmed by mutation: swapping to a plainassertSame()fails 2 of 6 data sets withFailed asserting that null is identical to false.Fixed with an explicit branch —
assertTrue()when JSON is expected,assertNull()otherwise — so each branch asserts the value it actually gets, the same fix shape as the
assertTrue()change requested on PR #13659.Left as assertEquals(), no changes
rest-users-controller.php(4) —$data['capabilities']/$data['extra_capabilities']vs
new stdClass(), and vs(object) $user->allcaps/(object) $user->caps: alwaysdistinct object instances, compared by value only.
rest-block-renderer-controller.php(1) — two independentjson_decode()calls (withoutthe
truearg) produce distinctstdClasstrees each time; compared by value only.wpRestTemplatesController.php(1) — compares againstnew stdClass(); same identityissue as above.
Testing
The one error (
Test_oEmbed_Controller::test_proxy_with_classic_embed_provider,Attempt to read property "queue" on null) is intests/oembed/controller.php, a file untouched bythis PR. Confirmed pre-existing by stashing this branch's changes and re-running the same
test directly against trunk — it fails identically there.
Every risky conversion above (the four
123/'123'meta findings, the four(int)-castpost_author/comment-property findings, the3.0schema-float finding, therest-server.phpliteral-mismatch finding, and the
rest-request.phpnull-vs-false trap) was proven bymutation: reverting the fix and confirming the test fails for the expected reason, then
restoring it.
PHPCS clean on all 10 changed files. Test-only change; no production code touched.
Trac ticket: https://core.trac.wordpress.org/ticket/64895
Use of AI Tools
AI assistance: Yes
Tool(s): Claude Code
Model(s): Claude Opus 5
Used for: Digging through this directory turned up a few things worth flagging beyond the mechanical conversion — get_param() returning null instead of false when a param is simply absent (which a naive strict-assertion swap would have broken outright), get_post_meta()/get_term_meta() silently returning strings for values stored as ints, and a schema declared type => 'number' quietly coercing to float on the way out. One assertion (rest-server.php) turned out to be checking the wrong literal type entirely, just masked by loose comparison the whole time. Each of these was confirmed by mutation before I trusted it. I take responsibility for the result.