Skip to content

fix(testing): register themes and plugins in every application of a test process - #752

Merged
ogorzalka merged 5 commits into
developfrom
fix/testing-replay-module-registration
Dec 3, 2026
Merged

ogorzalka merged 5 commits into
developfrom
fix/testing-replay-module-registration

Conversation

@ogorzalka

@ogorzalka ogorzalka commented Dec 3, 2026 •

Copy link
Copy Markdown
Member

Fixes #749.

Cause

WordPress includes functions.php and plugin main files once per PHP process; a test process builds one application per test. Measured on a throwaway skeleton (composer create-project, SQLite, PHP 8.5) with 13.45.1:

application 1 applications 2 and 3
functions.php included once not again
active theme in the registrar probe null
view()->exists('index') true false
config('theme.menus') 2 entries empty
theme service provider bound missing
registered plugins probe-plugin none

Fix

  • pollora_register() calls made while a test process loads WordPress are remembered (Pollora\Testing\ModuleRegistrations, @internal).
  • Each later application repeats them right after the require_once wp-settings.php that includes nothing, in the recorded order.
  • What they hook into plugins_loaded, setup_theme, after_setup_theme, widgets_init, init and wp_loaded is run, and only that; did_action() counts are not increased.
  • Callbacks of an earlier application that the new registration replaces (same code, same priority, new object) are unhooked, so theme classes do not run twice, with or without IsolatesWordPressHooks.
  • app/inc files are now included once per process. Repeating a registration included them again, which is fatal when one declares a function (reproduced).

Production and boot performance

  • Replay runs only under runningUnitTests(): Bootstrap is unchanged and drives it through WordPressLoadRecorder, which it already only calls there.
  • Elsewhere the cost is, per pollora_register() call, one small class autoloaded (once) and one static boolean check. src/Testing is part of the distribution archive.
  • include_once in app/inc: no difference for a module registered once. A file already included by other means is now skipped instead of included again.

Verified

On the same throwaway project, three consecutive applications, with and without the trait: theme views, configuration (menus, supports, images, sidebars, templates), theme provider, asset container, app/inc, nav menus, a plugin registered with pollora_register() (config, views, provider, route). With the trait, the callback counts of after_setup_theme and nav_menu_link_attributes and the output of a theme #[Filter] stay the same.

A child theme (child and parent both calling pollora_register): child view, parent view and child config present in the second application; on 13.45.1 they are missing.

nwidart modules are discovered by ModuleServiceProvider in each application, on 13.45.1 too: a module #[Filter] runs in all three.

GET / answers 200 with the theme template in all three applications (on 13.45.1, 200 in applications 2 and 3 as well, but without the theme: a 2.6 kB page against 7.7 kB). This needs shims in the test, see below.

Framework tests: 4 of the 6 two-application replay tests fail without the change (four more self-contained tests cover the recording for the mutation gate); without include_once the new ModuleAssetManager test kills the run with "Cannot redeclare".

composer qa:fast passes. composer qa (PHP 8.5, 2G): the two known PHP 8.5 failures only (finfo_close() in PHPStan, Extraction\PublicSurfaceTest); 4161 tests passed otherwise. The public surface gate passes, api-surface.json untouched.

Not fixed here: rendering / from a skeleton test

Independently of this issue, a feature test of a fresh skeleton cannot render / as a visitor gets it, even in the first application: in console WP_USE_THEMES is false (404), SERVER_NAME is undefined in wp_head (500), and once themes are forced on, the raw-template guard redirects and calls exit unless SCRIPT_FILENAME is public/index.php. The check above set all three from the test.

Limits

  • Only pollora_register() is repeated. Other code of functions.php, of a plugin file or of app/inc, and a direct call to a registrar, still run once per process.
  • Callbacks a registration hooks into other actions fired during the load are not run.
  • Replaying calls add_theme_support('title-tag') after wp_loaded: WordPress reports it through doing_it_wrong_run; Pollora's logging provider, booted before WordPress, keeps it from raising an error.
  • Without the trait, hooks added by providers and modules still pile up from one application to the next (that is Testing: hooks of a flushed application stay in $wp_filter and break later tests #741).
  • What WordPress keeps in its own globals stays: the emoji script is printed in the first page only, block binding sources keep the resolver of the first application.
  • One more SPL class loader is registered per application, as before this change.
  • MySQL, multisite and persistent workers were not tried.

Documentation (draft, merge at release): Pollora/documentation#150. Rendering a URL from a test is already documented in testing.md ("Why the test loads WordPress itself").

Rector 2.9.2 (separate commits, unrelated to #749)

Rector 2.9.2 reached CI during this pull request (composer.lock is not committed) and failed Code Quality on files already on develop: it refuses to run on the two skips naming rules it no longer ships, and its RemoveEmptyClassMethodRector would delete empty methods of test doubles, some of which are called or reflected. rector.php drops the two skips and skips that rule under tests/. Locally only Rector 2.8.5 could be run; 2.9.2 is checked by CI. Split these commits out if you would rather have them apart.

…est process

WordPress includes functions.php and plugin main files once per process,
while a test process builds one application per test: only the first one
was asked to register the theme and the plugins.

The calls of pollora_register() made while WordPress loads are now
repeated for each later application, what they hooked into the actions
of the load runs again, and the callbacks they replace are unhooked.
Under unit tests only.

The files of app/inc are now included once per process: a second
registration declared their functions again.

Fixes #749
Pollora maintainer added 4 commits December 3, 2026 22:45
The latest Rector refuses to run on a skip naming a rule that does not exist. Rector 2.8 only warned that both were deprecated and never registered.
Rector 2.9.2 removes them, among them methods the code under test calls or reflects.
A mutant runs only the tests that cover its line: the files that describe a process across ordered tests assert in another test than the one that records.
@ogorzalka
ogorzalka merged commit 581ce35 into develop Dec 3, 2026
23 checks passed
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.

1 participant