Skip to content

Tests: Use strict assertions or document intentional assertEquals() in new admin tests - #15891

Open
shail-mehta wants to merge 12 commits into
WordPress:trunkfrom
shail-mehta:tests/64895-strict-assertions
Open

shail-mehta wants to merge 12 commits into
WordPress:trunkfrom
shail-mehta:tests/64895-strict-assertions

Conversation

@shail-mehta

Copy link
Copy Markdown
Member

Trac Ticket: #64895

What?

Converts same-type assertEquals() calls to assertSame() in newer admin/color-scheme and update PHP annotation tests, and documents the remaining intentional loose comparisons (DB numeric strings, POST/user-meta type mismatches, and object value comparisons).

Why?

assertEquals() does not compare types, so it can hide regressions that change a return value’s type, especially falsy values. On #64895, Lance noted loose assertEquals() usage rose from 165 to 183, mostly from new tests. Done looks like either assertSame(), or a short comment explaining why assertEquals() is kept.

How?

  • adminColorSchemePicker.php: 10 same-type assertions → assertSame() (string/int/array comparisons).
  • wpGetUpdatePhpAnnotation.php: empty-string assertion → assertSame().
  • saveAttachmentOrder.php / sendAttachmentToEditor.php: keep assertEquals() with comments (menu_order / post_parent are numeric strings from the DB).
  • setScreenOptions.php: keep assertEquals() with comments (integer expectations vs string meta/POST values).
  • includesUpdate.php: keep assertEquals() with comments (translation update objects compared by value).

Testing Instructions

  1. Run the affected tests:
npm run test:php -- --filter Tests_admin_color_scheme_picker
npm run test:php -- --filter Tests_set_screen_options
npm run test:php -- --filter Tests_functions_wpGetUpdatePhpAnnotation
npm run test:php -- --filter Tests_Admin_IncludesUpdate
npm run test:php -- --filter Tests_wp_ajax_save_attachment_order
npm run test:php -- --filter Tests_wp_ajax_send_attachment_to_editor
  1. Run the same filters under multisite:
npm run test:php -- -c tests/phpunit/multisite.xml --filter Tests_admin_color_scheme_picker
npm run test:php -- -c tests/phpunit/multisite.xml --filter Tests_set_screen_options
npm run test:php -- -c tests/phpunit/multisite.xml --filter Tests_functions_wpGetUpdatePhpAnnotation
npm run test:php -- -c tests/phpunit/multisite.xml --filter Tests_Admin_IncludesUpdate
npm run test:php -- -c tests/phpunit/multisite.xml --filter Tests_wp_ajax_save_attachment_order
npm run test:php -- -c tests/phpunit/multisite.xml --filter Tests_wp_ajax_send_attachment_to_editor

Use of AI tools

  • Cursor

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.

🟡 Changes recommended

Two test comments incorrectly describe sanitized integer fields as numeric strings, unjustifiably retaining loose comparisons instead of using assertSame().

2 open issues
What changed in this PR

Strengthens WordPress PHPUnit tests for Trac #64895 by using strict assertions where types should match and documenting retained loose comparisons. No production code changes.

Changes:

  • Converts 10 color-scheme picker assertions and one PHP annotation assertion to assertSame().
  • Adds comments explaining retained assertEquals() calls in attachment, screen-option, and translation-update tests.
File Description
tests/​phpunit/​tests/​functions/​wpGetUpdatePhpAnnotation.php Strictly checks the empty-string result.
tests/​phpunit/​tests/​admin/​includesUpdate.php Adds comments explaining object comparisons by value.
tests/​phpunit/​tests/​admin/​includes/​misc/​setScreenOptions.php Adds comments explaining loose metadata comparisons.
tests/​phpunit/​tests/​admin/​includes/​misc/​adminColorSchemePicker.php Uses strict assertions for ordering and rendered output.
tests/​phpunit/​tests/​admin/​includes/​ajax-actions/​sendAttachmentToEditor.php Adds a comment explaining the retained parent-ID comparison.
tests/​phpunit/​tests/​admin/​includes/​ajax-actions/​saveAttachmentOrder.php Adds a comment explaining retained menu-order comparisons.

Review effort: Balanced

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

There are no active review issues for this test-only change, which strengthens type assertions and documents intentional loose comparisons.

No open issues 🎉

2 resolved since last review

Review effort: Balanced

@shail-mehta
shail-mehta marked this pull request as ready for review November 11, 2026 12:30
@github-actions

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.

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

Props shailu25.

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


Props Bot commands

You can trigger Props Bot actions by commenting on this PR:

  • @propsbot add <username> to give props to a GitHub user. Requires write access.
  • @propsbot remove <username> to leave a user out of the props. Requires write access.
  • @propsbot refresh to refresh this list. Adding the props-bot label works too.

@shail-mehta shail-mehta self-assigned this Nov 12, 2026

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

Reviewed the current patch and the addressed Copilot findings. All 50 affected tests (101 assertions) passed with three randomized seeds in both single-site and multisite; PHPCS passed. The strict empty-string assertion also catches a falsy wrong-type return that the old comparison accepts. Intentional metadata and object-value comparisons remain appropriate. No new tests or fixtures are needed.

Minor documentation note: the PR description still says the attachment assertions remain loose; the current patch correctly makes them strict.

AI review · gpt-6

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Ready

Development

Successfully merging this pull request may close these issues.

3 participants