Skip to content

Tests: Use strict assertions and document value comparisons in REST API tests - #13936

Closed
noruzzamans wants to merge 4 commits into
WordPress:trunkfrom
noruzzamans:test/rest-api-assert-equals
Closed

noruzzamans wants to merge 4 commits into
WordPress:trunkfrom
noruzzamans:test/rest-api-assert-equals

Conversation

@noruzzamans

Copy link
Copy Markdown

Description

Reviewed tests/phpunit/tests/rest-api/ for ticket #64895.

There were 16 calls to assertEquals() across 5 files:

Strict Assertion Replacements (assertSame()):

  1. tests/phpunit/tests/rest-api/wpRestAbilitiesV1ListController.php:
    • Line 438: Converted assertEquals( 200, $response->get_status() ) to assertSame().
    • Line 469: Converted assertEquals( 404, $response->get_status() ) to assertSame().
  2. tests/phpunit/tests/rest-api/wpRestAbilitiesV1RunController.php:
    • Line 432: Converted status code / data comparison assertEquals( 8, $response->get_data() ) to assertSame().
    • Line 454: Converted assertEquals( self::$user_id, $data['id'] ) to assertSame().
    • Line 678: Converted status code comparison assertEquals( 200, $response->get_status() ) to assertSame().
    • Line 792: Converted array comparison assertEquals( array( 1, 2, 3 ), $data['array'] ) to assertSame().
    • Line 1300: Converted assertEquals( $inputs, $data['echo'] ) to assertSame().
    • Lines 1349–1351: Converted string assertions for $input['utf8'], $input['emoji'], and $input['html'] to assertSame().

Object Value Comparisons Documented:

  1. tests/phpunit/tests/rest-api/rest-block-renderer-controller.php:
    • Line 383: Comparing json_decode() stdClass instances returned by the block type renderer against $data['rendered'].
  2. tests/phpunit/tests/rest-api/rest-users-controller.php:
    • Lines 1178–1179: Comparing $data['capabilities'] and $data['extra_capabilities'] against new stdClass().
    • Lines 3358–3359: Comparing (object) $user->allcaps and (object) $user->caps against $data['capabilities'] and $data['extra_capabilities'].
  3. tests/phpunit/tests/rest-api/wpRestTemplatesController.php:
    • Line 1184: Comparing new stdClass() with $data when a fallback template is not found.

In accordance with maintainer guidance on Trac #64895, scalar/integer/array assertions are converted to assertSame(), and intentional object-value comparisons are preserved and documented with:
// Keep assertEquals() because the objects are intentionally compared by value.

Testing Instructions

npm run test:php -- tests/phpunit/tests/rest-api/wpRestAbilitiesV1ListController.php tests/phpunit/tests/rest-api/wpRestAbilitiesV1RunController.php tests/phpunit/tests/rest-api/rest-block-renderer-controller.php tests/phpunit/tests/rest-api/wpRestTemplatesController.php
npm run test:php -- --filter WP_Test_REST_Users_Controller
npm run test:php -- -c tests/phpunit/multisite.xml --filter WP_Test_REST_Users_Controller
composer run lint tests/phpunit/tests/rest-api/wpRestAbilitiesV1ListController.php tests/phpunit/tests/rest-api/wpRestAbilitiesV1RunController.php tests/phpunit/tests/rest-api/rest-block-renderer-controller.php tests/phpunit/tests/rest-api/rest-users-controller.php tests/phpunit/tests/rest-api/wpRestTemplatesController.php

Trac ticket: https://core.trac.wordpress.org/ticket/64895

Use of AI Tools

AI assistance: Yes
Tool(s): Antigravity
Model(s): Gemini
Used for: Analyzing assertions and drafting documentation comments. Implementation and test verification were reviewed and executed locally.


This Pull Request is for code review only. Please keep all other discussion in the Trac ticket. Do not merge this Pull Request. See GitHub Pull Requests for Code Review in the Core Handbook for more details.

@noruzzamans
noruzzamans marked this pull request as ready for review October 2, 2026 18:31
@github-actions

github-actions Bot commented Oct 2, 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 noruzzaman, lancewillett.

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

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.

Copilot review overview

🟢 Approval recommended

The assertion changes match the tested value types, and the retained loose comparisons are appropriately documented.

Review effort: Balanced
Findings: None

What changed in this PR

Updates REST API tests for stricter comparisons and clarifies intentional object-value assertions.

Changes:

  • Replaces 10 loose assertions with assertSame().
  • Documents six intentional object-value comparisons using assertEquals().
File Description
rest-block-renderer-controller.php Documents an object-value comparison.
rest-users-controller.php Documents capability object comparisons.
wpRestAbilitiesV1ListController.php Uses strict status-code assertions.
wpRestAbilitiesV1RunController.php Uses strict response and input assertions.
wpRestTemplatesController.php Documents the empty-object comparison.

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

@lancewillett lancewillett 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.

One assertion needs adjustment before landing. Applying this patch to current trunk produces the same calculator test failure in both single-site and multisite runs; the baseline passes. The other changed assertions passed.

AI review · gpt-6


$this->assertSame( 200, $response->get_status() );
$this->assertEquals( 8, $response->get_data() );
$this->assertSame( 8, $response->get_data() );

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.

This strict comparison fails on current trunk: Failed asserting that 8.0 is identical to 8. I reproduced it in both single-site and multisite runs.

The calculator declares its inputs as number. REST sanitizes those values to floats before the callback adds them, so the result is 8.0. See the number sanitizer.

Could you retain the numeric value comparison here, with a short explanation? The schema permits both integers and floats. If this test is intended to verify float normalization specifically, use an explicit float expectation and document that intent. Please recheck against current trunk.

AI review · gpt-6

@noruzzamans

Copy link
Copy Markdown
Author

Updated! The test/calculator ability defines an output_schema of type number, which rest_sanitize_value_from_schema() casts to a float (8.0). I've updated the assertion to $this->assertSame( 8.0, $response->get_data() );.

All 47 tests and 217 assertions pass in both single-site and multisite runs.

@lancewillett

Copy link
Copy Markdown
Member

wporg-sync pushed a commit that referenced this pull request Oct 2, 2026
Use `assertSame()` for status codes, numeric results, arrays, and strings in the Abilities API tests. Expect the calculator result as a float because its `number` inputs are normalized to floats.

Document why comparisons of separate objects retain `assertEquals()` in the block renderer, users, and templates tests.

Developed in: #13936

Props noruzzaman.
See #64895.


git-svn-id: https://develop.svn.wordpress.org/trunk@64070 602fd350-edb4-49c9-b593-d223f7449a82
wporg-sync pushed a commit to WordPress/WordPress that referenced this pull request Oct 2, 2026
Use `assertSame()` for status codes, numeric results, arrays, and strings in the Abilities API tests. Expect the calculator result as a float because its `number` inputs are normalized to floats.

Document why comparisons of separate objects retain `assertEquals()` in the block renderer, users, and templates tests.

Developed in: WordPress/wordpress-develop#13936

Props noruzzaman.
See #64895.

Built from https://develop.svn.wordpress.org/trunk@64070


git-svn-id: http://core.svn.wordpress.org/trunk@63229 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.

3 participants