Skip to content

Tests: Clear the unloaded text domains after each WP_Translation_Controller_Tests test. - #16251

Open
mayankmajeji wants to merge 2 commits into
WordPress:trunkfrom
mayankmajeji:tests/66755-l10n-unloaded
Open

mayankmajeji wants to merge 2 commits into
WordPress:trunkfrom
mayankmajeji:tests/66755-l10n-unloaded

Conversation

@mayankmajeji

@mayankmajeji mayankmajeji commented Nov 25, 2026 •

Copy link
Copy Markdown

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

WP_Translation_Controller_Tests::tear_down() calls unload_textdomain( 'internationalized-plugin' ). When real translations for that domain are loaded at that point, unload_textdomain() also adds it to the global $l10n_unloaded, and _load_textdomain_just_in_time() then never loads it again. Nothing clears that global, so a later test of the class that needs the domain cannot load it.

That happens when an earlier class leaves internationalized-plugin translations loaded:

  1. WP_REST_Plugins_Controller_Test::test_get_items_status() reads the plugin headers, which adds the domain to $l10n (not yet translated).
  2. Tests_User_wpSendUserRequest::test_should_send_user_request_email_in_user_locale_when_site_is_not_en_us() switches the locale to send the email, which reloads the domain with real translations, and they stay after the locale is restored.
  3. The first WP_Translation_Controller_Tests test to run unloads the domain in tear_down(), which adds it to $l10n_unloaded.
  4. test_switch_to_locale_translations_stay_loaded_custom_textdomain() switches to es_ES, cannot load the internationalized-plugin translations ("Failed asserting that false is true" at the is_textdomain_loaded() check), and stops before restore_previous_locale().

Because the locale stays es_ES, every later test that checks the locale fails too. In the run where this was found, 23 more tests failed in Tests_General_GetLanguageAttributes, Tests_Formatting_GetBloginfo, Tests_L10n_GetLocale and Tests_Date_MaybeDeclineDate.

Tests_L10n_LoadTextdomainJustInTime and Tests_L10n_wpLocaleSwitcher already unset $GLOBALS['l10n_unloaded'] in their own tear_down().

What changes

  • WP_Translation_Controller_Tests::tear_down() unsets the wp-tests-domain and internationalized-plugin entries of $GLOBALS['l10n_unloaded'] after unloading those two text domains, so other domains stay as they are.

Testing

Locally with PHP 8.4.28, MySQL 9.7.4 and PHPUnit 9.6.39, on trunk at r65790.

The config below runs rest-api/rest-plugins-controller.php, user/wpSendUserRequest.php and l10n/wpTranslationController.php, and the --filter keeps the four tests above, in that order (test_load_textdomain() as the first WP_Translation_Controller_Tests test).

  • vendor/bin/phpunit -c chain.xml --filter '/::test_get_items_status$|::test_should_send_user_request_email_in_user_locale_when_site_is_not_en_us$|WP_Translation_Controller_Tests::test_(load_textdomain|switch_to_locale_translations_stay_loaded_custom_textdomain)$/': 1 failure on trunk, OK (4 tests, 25 assertions) with this change.
  • The same on multisite (the config with <const name="WP_TESTS_MULTISITE" value="1" /> added).
  • The same filter without the Tests_User_wpSendUserRequest test: OK on trunk.
  • WP_Translation_Controller_Tests in default order, with --order-by=reverse and with --order-by=random --random-order-seed=7: OK with this change.
  • --group l10n (291 tests, also on multisite) and --group i18n (304 tests): no failures with this change.
  • Found with -c tests/phpunit/multisite.xml --order-by=random --random-order-seed=1717 on trunk at r65781 (24 failures). The random order depends on the set of tests, so the seed gives a different order on current trunk; the config reproduces the failure without a seed.
  • Full suite in default order, with this change: OK on single site (32360 tests) and on multisite (33235 tests).
  • PHPCS reports nothing on the changed file.
<phpunit bootstrap="tests/phpunit/includes/bootstrap.php" backupGlobals="false" convertErrorsToExceptions="true" convertWarningsToExceptions="true" convertNoticesToExceptions="true" convertDeprecationsToExceptions="true">
	<testsuites>
		<testsuite name="chain">
			<file>tests/phpunit/tests/rest-api/rest-plugins-controller.php</file>
			<file>tests/phpunit/tests/user/wpSendUserRequest.php</file>
			<file>tests/phpunit/tests/l10n/wpTranslationController.php</file>
		</testsuite>
	</testsuites>
	<php>
		<const name="WP_RUN_CORE_TESTS" value="1" />
	</php>
</phpunit>

Use of AI Tools

AI assistance: Yes
Tool(s): Claude Code
Model(s): Claude Opus 5.5
Used for: Investigation, code, local testing and PR wording. I reviewed the reasoning, the code and the test results, and I take responsibility for the contribution.

…troller_Tests` test.

When an earlier class left `internationalized-plugin` translations loaded, the class's own `tear_down()` marked the domain as unloaded, so a later test of the class could not load it and left the locale switched for the rest of the run. Unset `$l10n_unloaded` after unloading, as the just-in-time loading and locale switcher tests do.

See #66755.
@github-actions

github-actions Bot commented Nov 25, 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.

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

Props mayankmajeji, lancewillett.

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 or WordPress.org 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.

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

No active review issues are recorded for the translation-controller test-isolation cleanup.

No open issues 🎉

What changed in this PR

This PR improves translation-controller test isolation by clearing unloaded text-domain state after each test, preventing order-dependent translation-loading failures.

Changes:

  • Unsets $GLOBALS['l10n_unloaded'] after unloading both test domains, following cleanup patterns used by other localization tests.
File Description
tests/​phpunit/​tests/​l10n/​wpTranslationController.php Clears unloaded-domain markers in tear_down() so later tests can reload translations.

Review effort: Balanced

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

The four-test sequence reproduced the translation failure on both single-site and multisite. Could you limit the cleanup to the two domains this class unloads? Unsetting all of $GLOBALS['l10n_unloaded'] also makes unrelated domains eligible for just-in-time loading again. I tried unsetting just the wp-tests-domain and internationalized-plugin entries. All four tests passed in both modes, and the 19 translation controller tests passed in random order.

AI review · gpt-6.2-astra

Unsetting all of `$l10n_unloaded` would make unrelated domains eligible for just-in-time loading again.
@mayankmajeji

Copy link
Copy Markdown
Author

Thanks @lancewillett, that makes sense. I've pushed 14604f6, which unsets only the wp-tests-domain and internationalized-plugin entries of $l10n_unloaded.

Rechecked on PHP 8.4:

  • The four-test chain passes on single site and multisite, and still fails without the cleanup.
  • WP_Translation_Controller_Tests passes in default, reverse and random order (seeds 7, 1717 and 42).
  • --group l10n passes on single site and multisite.

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

Confirmed the narrower cleanup fixes the original failure. With the same four tests in order, the translation check failed on trunk on both single-site and multisite. Clearing just these two domain markers passed all 25 assertions in each mode.

AI review · gpt-6.2-astra

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