Skip to content

Commit f759769

Browse files
committed
fix: reuse canonical multisite test install
1 parent 94150df commit f759769

3 files changed

Lines changed: 42 additions & 2 deletions

File tree

packages/runtime-playground/src/phpunit-command-handlers.ts

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ export interface PhpunitRunCodeOptions {
2323
projectBootstrap: string
2424
multisite: boolean
2525
databaseType: "sqlite" | "mysql"
26+
managedMultisitePreinstalled?: boolean
2627
/**
2728
* Sandbox-internal, writable path for the structured diagnostics log. Defaults
2829
* to a /tmp path so diagnostics survive read-only plugin mounts and a mid-install
@@ -466,6 +467,7 @@ $bootstrap_mode = ${JSON.stringify(options.bootstrapMode || "managed")};
466467
$project_bootstrap = ${JSON.stringify(options.projectBootstrap)};
467468
$multisite = ${JSON.stringify(options.multisite)};
468469
$database_type = ${JSON.stringify(options.databaseType)};
470+
$managed_multisite_preinstalled = ${JSON.stringify(options.managedMultisitePreinstalled ?? false)};
469471
470472
function pg_build_phpunit_argv($raw): array {
471473
$phpunit_argv = array('phpunit');
@@ -1032,6 +1034,36 @@ function pg_run_install_stage(array $cfg) {
10321034
}
10331035
}
10341036
1037+
function pg_run_preinstalled_wordpress_stage(array $cfg): void {
1038+
global $argv, $pg_stage_output_buffering, $wp_rewrite;
1039+
pg_stage_begin('install');
1040+
try {
1041+
$config_path = $cfg['config_path'];
1042+
$tests_dir = $cfg['tests_dir'];
1043+
require_once $config_path;
1044+
tests_reset__SERVER();
1045+
$GLOBALS['PHP_SELF'] = '/index.php';
1046+
$_SERVER['PHP_SELF'] = '/index.php';
1047+
tests_add_filter('wp_die_handler', '_wp_die_handler_filter_exit');
1048+
$pg_stage_output_buffering = true;
1049+
ob_start();
1050+
require_once ABSPATH . 'wp-settings.php';
1051+
while (ob_get_level() > 0) {
1052+
@ob_end_clean();
1053+
}
1054+
$pg_stage_output_buffering = false;
1055+
if (is_dir($tests_dir . '/data/themedir1')) {
1056+
register_theme_directory($tests_dir . '/data/themedir1');
1057+
}
1058+
pg_log('NOTICE:using canonical preinstalled multisite schema');
1059+
pg_stage_ok('install');
1060+
} catch (Throwable $e) {
1061+
$pg_stage_output_buffering = false;
1062+
pg_stage_fail('install', $e);
1063+
exit(1);
1064+
}
1065+
}
1066+
10351067
function pg_run_load_deps_stage(array $cfg): array {
10361068
pg_stage_begin('load_deps');
10371069
try {
@@ -1261,7 +1293,11 @@ tests_add_filter('muplugins_loaded', function () use ($plugin_slug, $plugin_path
12611293
$deferred_install_init_callbacks = pg_defer_new_wordpress_hook_callbacks('init', $pre_component_init_callbacks);
12621294
});
12631295
1264-
pg_run_install_stage(array('config_path' => $config_path, 'tests_dir' => $tests_dir, 'multisite' => $multisite));
1296+
if ($managed_multisite_preinstalled) {
1297+
pg_run_preinstalled_wordpress_stage(array('config_path' => $config_path, 'tests_dir' => $tests_dir));
1298+
} else {
1299+
pg_run_install_stage(array('config_path' => $config_path, 'tests_dir' => $tests_dir, 'multisite' => $multisite));
1300+
}
12651301
pg_remove_new_wordpress_hook_callbacks('shutdown', $pre_component_shutdown_callbacks);
12661302
$pre_dependency_plugins_loaded_callbacks = pg_snapshot_wordpress_hook_callbacks('plugins_loaded');
12671303
$pre_dependency_init_callbacks = pg_snapshot_wordpress_hook_callbacks('init');

packages/runtime-playground/src/wordpress-command-runners.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -923,6 +923,7 @@ export async function runPhpunitCommand({
923923
const autoloadFile = argValue(args, "autoload-file")?.trim() || (bootstrapMode === "project" ? "" : "/wp-codebox-vendor/autoload.php")
924924
const autoloadFileRole = argValue(args, "autoload-file-role")?.trim() === "harness" ? "harness" : undefined
925925
const processIdentity = boundedProcessIdentity(spec.processIdentity)
926+
const managedMultisitePreinstalled = !explicitCode && requiresManagedMysqlMultisitePreinstall(args, runtimeSpec)
926927
const resultFile = processIdentity ? `/tmp/wp-codebox-phpunit-result-${processIdentity}.txt` : PLUGIN_PHPUNIT_RESULT_FILE
927928
const diagnosticHostFile = `/wordpress/wp-content/plugins/${pluginSlug}/.pg-test-result${processIdentity ? `-${processIdentity}` : ""}.txt`
928929
const code = explicitCode ? await phpCodeFromArgs(args, "wordpress.phpunit", false) : phpunitRunCode({
@@ -956,6 +957,7 @@ export async function runPhpunitCommand({
956957
projectBootstrap: argValue(args, "project-bootstrap")?.trim() || "",
957958
multisite,
958959
databaseType,
960+
managedMultisitePreinstalled,
959961
resultFile,
960962
})
961963
if (!explicitCode && !pluginSlug) {
@@ -964,7 +966,7 @@ export async function runPhpunitCommand({
964966
let response: PlaygroundRunResponse
965967
try {
966968
const bootstrapArgs = explicitCode ? args : [...args, "bootstrap=runtime-only"]
967-
if (!explicitCode && requiresManagedMysqlMultisitePreinstall(args, runtimeSpec)) {
969+
if (managedMultisitePreinstalled) {
968970
const preinstallCode = phpunitMultisitePreinstallCode({
969971
testsDir: argValue(args, "tests-dir")?.trim() || "/wp-codebox-vendor/wp-phpunit/wp-phpunit",
970972
env: jsonObjectArg(args, "env-json"),

tests/phpunit-project-autoload.test.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -738,6 +738,8 @@ assert.equal(mysqlMultisiteInvocations.length, 2, "managed MySQL multisite runs
738738
assert.ok(mysqlMultisiteInvocations[0].includes("'run_ms_tests'"))
739739
assert.equal(mysqlMultisiteInvocations[0].includes("preinstall-sensitive/bootstrap.php"), false, "canonical preinstall excludes recipe-active plugin and Composer preloading")
740740
assert.ok(mysqlMultisiteInvocations[1].includes("$phpunit_argv = pg_build_phpunit_argv"), "normal managed PHPUnit behavior follows preinstall")
741+
assert.ok(mysqlMultisiteInvocations[1].includes("$managed_multisite_preinstalled = true"), "main managed invocation receives the canonical preinstall result")
742+
assert.ok(mysqlMultisiteInvocations[1].includes("pg_run_preinstalled_wordpress_stage"), "main managed invocation boots the canonical schema without reinstalling it")
741743

742744
const failedPreinstallInvocations: string[] = []
743745
await assert.rejects(

0 commit comments

Comments
 (0)