Skip to content

fix(testing): register extenders before enabling test extensions (#4600) - #5039

Open
wakqasahmed wants to merge 1 commit into
flarum:2.xfrom
wakqasahmed:fix/issue-4600-locale-extend-order
Open

fix(testing): register extenders before enabling test extensions (#4600)#5039
wakqasahmed wants to merge 1 commit into
flarum:2.xfrom
wakqasahmed:fix/issue-4600-locale-extend-order

Conversation

@wakqasahmed

Copy link
Copy Markdown

Fixes #4600.

OverrideExtensionManagerForTests was calling enable() on each test extension before calling extend(). Any extender that registers a resolving() callback during extend()Extend\Locales does this for LocaleManager — never gets to fire, because enable() had already resolved that singleton via its onEnable() hook. A singleton only resolves once, so the callback registered afterward is too late. In practice this meant an extension's own locale files never loaded into the translator during integration tests; $translator->trans('my-ext.some.key') just echoed the key back instead of resolving it.

The obvious fix is to just swap the two calls, but that alone breaks something else: ExtensionManagerIncludeCurrent::isEnabled() returns false unconditionally while booted is false, specifically so that enable()'s early-return guard (if ($this->isEnabled($name)) return;) doesn't skip its own body — migrations, asset publishing, the Enabling/Enabled events, onEnable(). Flip booted to true before the enable() loop (which you'd need to do to call extend() first, since extend() only pulls extenders for already-enabled extensions) and enable() starts short-circuiting immediately, silently skipping all of that for every test extension.

So this toggles booted off again specifically around the enable() loop: booted = trueextend() (registers extenders, including Locales' resolving() callback) → booted = false → the enable() loop (now behaves exactly as it did before this change) → booted = true again.

Added a regression test: a fake extension in the testing package's own fixtures that ships a locale file via Extend\Locales, asserting its translation key resolves to the real string rather than being echoed back.

Verified with a disposable php:8.3-cli container (no persistent PHP/Composer in my normal setup): the new test fails against the old code exactly as described (flarum-testing-tests.test returned literally), and passes after the fix — ran the full TestCaseTest suite (15 tests, 24 assertions), including the existing migration and enabled-state tests, to make sure the booted toggling doesn't regress anything else in that file. Four unrelated tests in the same container failed on missing the GD PHP extension, unrelated to this change. Didn't touch 1.x — this worktree only has the 2.x version of the file, and the fix needs verifying separately there if you want it backported.

OverrideExtensionManagerForTests called enable() before extend(), so
any resolving() callback an extender registers on ExtensionManager
during extend() (e.g. Extend\Locales, which hooks LocaleManager) was
registered too late -- enable() had already resolved the singleton via
onEnable(), and a resolving() callback never fires retroactively. In
practice this meant extension-shipped locale files never loaded into
the translator during integration tests.

Registering extenders first fixes that, but naively also moving
enable() after 'booted = true' breaks isEnabled()'s early return in
ExtensionManagerIncludeCurrent, which gates on booted specifically so
enable() runs its full body (migrations, assets, Enabling/Enabled
events) instead of being treated as already enabled. So this toggles
booted off again around the enable() loop, then back on once it's
done -- extenders are registered first, but enable()'s own internal
ordering (dependency check, migrate, publish assets, persist enabled
state, onEnable(), dispatch Enabled) is unchanged.

Added a regression test: a fake extension shipping a locale file via
Extend\Locales, asserting its translation key resolves to the real
value instead of being echoed back.
@wakqasahmed
wakqasahmed requested a review from a team as a code owner September 8, 2026 18:50
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.

Extend\Locales translations never register in integration tests (1.x + 2.x)

1 participant