Make WordPress Core

Opened 5 months ago

Last modified 8 days ago

#64895 new task (blessed)

Tests: Reduce usage of assertEquals for 7.2

Reported by: desrosj Owned by:
Priority: normal Milestone: 7.2
Component: Build/Test Tools Version:
Severity: normal Keywords: has-patch has-unit-tests
Cc: Focuses:

Description

The assertEquals() test method does not check that the types of the expected and actual values match. This can hide subtle bugs especially when the values are falsey.

Tasks:

  • Switch to using assertSame() when the type of the value in the assertion is important
  • Replace overall usage of assertEquals() with type-strict assertion methods, with the aim of potentially removing its usage altogether

To help ease the effort of merging tests, changes should also be made upstream in the Gutenberg repository.

Follow-up to:

Change History (5)

This ticket was mentioned in PR #11707 on WordPress/wordpress-develop by @shreyasikhar26.


3 months ago
#1

  • Keywords has-patch has-unit-tests added

## PR Description

  • Updates multiple PHPUnit tests to use assertSame() instead of assertEquals() to enforce strict type comparisons.
  • Adds an explicit (int) cast for post_author in the bulk edit test to avoid false positives due to string/integer mismatches.
  • Touches tests covering admin bulk edit behavior, REST API endpoints (attachments, global styles, wp-abilities), and theme preview hooks—no production/runtime code changes.

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

## Use of AI Tools

AI assistance: Yes
Tool(s): GitHub Copilot
Used for: Find the files where assertEquals() can be replaced with assertSame().

This ticket was mentioned in PR #12583 on WordPress/wordpress-develop by @Soean.


4 weeks ago
#2

## What

Replaces loose assertions (assertEquals() / assertNotEquals()) with their strict counterparts (assertSame() / assertNotSame()) across a number of PHPUnit test files.

## Why

Strict assertions additionally check the type of the compared values, making the tests more robust against unintended type coercion and better documenting the expected return types of the functions under test.

## Testing

All affected test files pass locally: OK (71 tests, 669 assertions).

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

This ticket was mentioned in PR #12613 on WordPress/wordpress-develop by @thisismyurl.


3 weeks ago
#3

Replaces the remaining assertEquals() calls in the REST schema sanitization tests with assertSame().

These assertions are a good fit for strict comparison, because the type of the returned value *is* the thing under test. rest_sanitize_value_from_schema() promises to coerce a value to the type declared in the schema, and most of these cases pass a deliberately mistyped input to prove it does:

$this->assertSame( array( 1.0 ), rest_sanitize_value_from_schema( array( '1' ), $schema ) );

With a loose assertion that test still passes if the sanitizer hands back the uncoerced array( '1' ), so it cannot fail for the one reason it exists.

Worth flagging for review, since it is the easy thing to get wrong here: every schema in these particular tests declares 'type' => 'number', which returns (float), so the expected values are floats rather than integers. That matches the convention already used by test_type_number() at the top of this file, which asserts assertSame( 1.0, ... ).

I checked each expected literal against its own schema rather than swapping the method names mechanically, which also means three values are deliberately left alone:

  • 'd' => '1' in the nested-properties test stays a string, because d has no matching entry in properties, no patternProperties, and additionalProperties is unset, so it is never coerced.
  • The top-level 'b' => 1 in that same test stays an integer for the same reason.
  • 'b' => 1 in test_type_object() likewise stays an integer.

The strict assertions now document that behaviour instead of glossing over it.

Only assertion methods and expected literals changed. The file already used assertSame() for 65 of its assertions, so this makes it consistent throughout.

One disclosure on verification: this box has no Docker/wp-env, so I could not run the full PHPUnit suite locally. I verified each conversion by reading the schema declared in each test and matching the expected literal to the type rest_sanitize_value_from_schema() returns for it, and php -l is clean. I would appreciate a CI run confirming it.

## Use of AI Tools

AI assistance: Yes
Tool(s): Claude Code
Model(s): Claude Opus 4.8
Used for: Helping locate candidate files and cross-check each expected literal against the schema type. I reviewed the conversions and take responsibility for the final code.

@mukesh27 commented on PR #12613:


13 days ago
#4

Thanks @thisismyurl for the PR! Can you marge latest trunk in to this branch?

#5 @desrosj
8 days ago

  • Milestone 7.17.2
  • Summary Tests: Reduce usage of assertEquals for 7.1Tests: Reduce usage of assertEquals for 7.2

It looks like this did not receive any attention during the 7.1 cycle.

Since there have been no commits made associated with the work here, I'm going to just punt to 7.2.

Note: See TracTickets for help on using tickets.