Skip to content

Build/Test Tools: Always reset the mock PHPMailer instance in tests - #13887

Open
zakariabinsaifullah wants to merge 1 commit into
WordPress:trunkfrom
zakariabinsaifullah:28618-reset-phpmailer-instance
Open

zakariabinsaifullah wants to merge 1 commit into
WordPress:trunkfrom
zakariabinsaifullah:28618-reset-phpmailer-instance

Conversation

@zakariabinsaifullah

@zakariabinsaifullah zakariabinsaifullah commented Oct 1, 2026 •

Copy link
Copy Markdown

This implements the approach suggested by @SirLouen in comment:8: reset the mock mailer unconditionally.

reset_phpmailer_instance() only replaced the global $phpmailer when it was already truthy. If a test unset the global (or set it to null/false), the helper returned false without doing anything. The next wp_mail() call then created a real WP_PHPMailer, so the test suite could send real email.

Changes

  • tests/phpunit/includes/mock-mailer.php: reset_phpmailer_instance() now always installs a fresh MockPHPMailer and returns true. No callers in the suite use the return value.
  • tests/phpunit/tests/pluggable/wpMail.php: new tests (@ticket 28618) cover an unset global, null, false and a real WP_PHPMailer, plus an end-to-end check that wp_mail() goes through the mock after the global was unset.

This is a test-suite-only change. It doesn't add the wp_phpmailer filter to core proposed in #10285. That PR now conflicts with trunk, and removing the override in tests/phpunit/includes/install.php would leave the install step without a mock mailer. Whether core should get a filter can be decided on the ticket separately.

Testing (local, MariaDB 13, PHP 8.5)

  • New tests against trunk's helper: 4 of 5 fail. Most importantly, wp_mail() ends up using a real WP_PHPMailer after the global is unset.
  • With the fix:
    • wpMail.php: OK (37 tests, 95 assertions)
    • --group mail: OK (39 tests)
    • Every file that calls reset_phpmailer_instance() passes: comment.php, user.php, user/wpSendUserRequest.php, and the three privacy/wpPrivacySend* files.
  • phpcs passes on both changed files.

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

Use of AI Tools

AI assistance: Yes
Tool(s): Claude Code
Model(s): Claude Opus 5.5
Used for: Analyzing the ticket history, drafting the fix and tests, and running the test suite. I reviewed the final changes and take responsibility for them.


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.

`reset_phpmailer_instance()` only replaced the global `$phpmailer` when it
was already set. If a test unset the global or set it to a falsey value,
the helper returned early, and the next call to `wp_mail()` created a real
`WP_PHPMailer` instance instead of the `MockPHPMailer`.

The helper now always installs a fresh `MockPHPMailer`, so tests reliably
send email through the mock.

Includes tests covering an unset, `null`, `false`, or real mailer global.

See #28618.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@github-actions

github-actions Bot commented Oct 1, 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 binsaifullah, sirlouen.

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

@SirLouen

SirLouen commented Oct 7, 2026

Copy link
Copy Markdown
Member

I'm still not sure if this is a better solution than #9403
In fact I'm not even sure if this should be fixed at all sincere there are few real examples of the utility other than not practical edge cases unlikely to happen.

Personally I'm inclined to close everything including #28618 unless @zakariabinsaifullah can give me more insight or an answer in the ticket to my last concerns.

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

Labels

None yet

Projects

Status: In review

Development

Successfully merging this pull request may close these issues.

3 participants