diff --git a/packages/runtime-core/src/recipe-builders.ts b/packages/runtime-core/src/recipe-builders.ts index 64ae935f..540f7654 100644 --- a/packages/runtime-core/src/recipe-builders.ts +++ b/packages/runtime-core/src/recipe-builders.ts @@ -97,7 +97,10 @@ export function buildWordPressPhpunitRecipe(options: WordPressPhpunitRecipeOptio ...(options.phpVersion ? { phpVersion: options.phpVersion } : {}), ...(options.workers !== undefined ? { workers: options.workers } : {}), ...(options.wordpressInstallMode ? { wordpressInstallMode: options.wordpressInstallMode } : {}), - blueprint: blueprintWithMultisite(options.blueprint ?? { steps: [] }, (options.multisite ?? false) && options.databaseType !== "mysql"), + blueprint: blueprintWithMultisite( + blueprintWithWpConfigDefines(options.blueprint ?? { steps: [] }, options.wpConfigDefines ?? {}, "rewrite-wp-config"), + (options.multisite ?? false) && options.databaseType !== "mysql", + ), ...(options.preview || options.multisite ? { preview: multisitePreview(options.preview, options.multisite ?? false) } : {}), ...(options.extensions?.length ? { extensions: options.extensions } : {}), ...(options.backendPackage ? { backendPackage: options.backendPackage } : {}), @@ -334,21 +337,17 @@ function normalizeExtraPlugins(plugins: readonly WorkspaceRecipeExtraPlugin[] = }) } -function blueprintWithWpConfigDefines(blueprint: unknown, defines: JsonObject): unknown { +function blueprintWithWpConfigDefines(blueprint: unknown, defines: JsonObject, method?: "rewrite-wp-config"): unknown { const defineKeys = Object.keys(defines) if (defineKeys.length === 0) { return blueprint } - if (!isPlainObject(blueprint)) { - return { steps: [{ step: "defineWpConfigConsts", consts: defines }] } - } - - const existingSteps = Array.isArray(blueprint.steps) ? blueprint.steps : [] - return { - ...blueprint, - steps: [...existingSteps, { step: "defineWpConfigConsts", consts: defines }], - } + const base = isPlainObject(blueprint) ? blueprint : {} + const existingSteps = Array.isArray(base.steps) ? base.steps : [] + const defineStep = { step: "defineWpConfigConsts", consts: defines, ...(method ? { method } : {}) } + // PHPUnit loads its own test config, so don't predefine runtime constants in every PHP request. + return { ...base, steps: method ? [defineStep, ...existingSteps] : [...existingSteps, defineStep] } } diff --git a/packages/runtime-playground/src/blueprint.ts b/packages/runtime-playground/src/blueprint.ts index 18c31312..013d19ed 100644 --- a/packages/runtime-playground/src/blueprint.ts +++ b/packages/runtime-playground/src/blueprint.ts @@ -69,7 +69,9 @@ export function playgroundWpConfigConstants(blueprint: unknown): Record = {} for (const step of normalizeBlueprint(blueprint).steps) { if (!step || typeof step !== "object" || Array.isArray(step)) continue - const candidate = step as { step?: unknown; consts?: unknown } + const candidate = step as { step?: unknown; consts?: unknown; method?: unknown } + // Rewriting wp-config.php must not also predefine the constants in every PHP request. + if (candidate.method === "rewrite-wp-config") continue if (candidate.step !== "defineWpConfigConsts" || !candidate.consts || typeof candidate.consts !== "object" || Array.isArray(candidate.consts)) continue for (const [name, value] of Object.entries(candidate.consts)) { if (value === null || ["string", "number", "boolean"].includes(typeof value)) { diff --git a/tests/phpunit-project-autoload.test.ts b/tests/phpunit-project-autoload.test.ts index e38cf592..43b41deb 100644 --- a/tests/phpunit-project-autoload.test.ts +++ b/tests/phpunit-project-autoload.test.ts @@ -5,6 +5,7 @@ import { tmpdir } from "node:os" import { join } from "node:path" import { buildWordPressPhpunitRecipe } from "../packages/runtime-core/src/recipe-builders.js" +import { playgroundWpConfigConstants } from "../packages/runtime-playground/src/blueprint.js" import { corePhpunitRunCode, phpunitMultisitePreinstallCode, phpunitRunCode } from "../packages/runtime-playground/src/phpunit-command-handlers.js" import { runPhpunitCommand } from "../packages/runtime-playground/src/wordpress-command-runners.js" import { phpunitExecutionSemantics, requiresManagedMultisitePreinstall } from "../packages/runtime-playground/src/phpunit-command-semantics.js" @@ -29,6 +30,19 @@ assert.ok(managedRecipe.includes("autoload-file=/wp-codebox-vendor/autoload.php" assert.ok(managedRecipe.includes("autoload-file-role=harness")) assert.ok(managedRecipe.includes("phpunit-xml-default=1"), "default recipe XML retains implicit fallback semantics") +const wpConfigDefines = { FIXTURE_ENV: true, WPMU_PLUGIN_DIR: "/wordpress/wp-content/fixture-mu-plugins" } +const startupStep = { step: "setSiteOptions", options: { blogname: "Fixture" } } +const recipeWithDefines = buildWordPressPhpunitRecipe({ + pluginSlug: "demo-plugin", + blueprint: { steps: [startupStep] }, + wpConfigDefines, +}) +assert.deepEqual(recipeWithDefines.runtime?.blueprint, { + steps: [{ step: "defineWpConfigConsts", method: "rewrite-wp-config", consts: wpConfigDefines }, startupStep], +}, "apply PHPUnit constants before user blueprint steps can load WordPress") +assert.deepEqual(playgroundWpConfigConstants(recipeWithDefines.runtime?.blueprint), {}, "startup must not predefine constants that PHPUnit can override") +assert.ok(recipeWithDefines.workflow.steps[0].args.includes(`wp-config-defines-json=${JSON.stringify(wpConfigDefines)}`)) + const explicitAutoloadRecipe = phpunitRecipeArgs({ bootstrapMode: "project", autoloadFile: "/wp-codebox-vendor/autoload.php", @@ -732,7 +746,7 @@ assert.equal(requiresManagedMultisitePreinstall(["database-type=mdi-native", "mu const preinstallCode = phpunitMultisitePreinstallCode({ testsDir: "/wp-codebox-vendor/wp-phpunit/wp-phpunit", env: {}, - wpConfigDefines: { MULTISITE: true, DOMAIN_CURRENT_SITE: "example.org" }, + wpConfigDefines: { ...wpConfigDefines, MULTISITE: true, DOMAIN_CURRENT_SITE: "example.org" }, databaseType: "mysql", resultFile: "/tmp/preinstall-result.txt", }) @@ -743,6 +757,8 @@ assert.ok(preinstallCode.includes("pg_write_managed_test_config($wp_config_defin assert.equal(preinstallCode.includes("define('MULTISITE'"), false, "multisite preinstall must not bootstrap WordPress as an installed network") assert.equal(preinstallCode.includes("DOMAIN_CURRENT_SITE"), false, "multisite preinstall must not define current-network constants") assert.ok(preinstallCode.includes("/tmp/wp-codebox-preinstall-mu-plugins"), "multisite preinstall isolates generated and declared mu-plugins") +assert.ok(preinstallCode.includes("FIXTURE_ENV"), "multisite preinstall keeps other recipe constants") +assert.ok(!preinstallCode.includes(wpConfigDefines.WPMU_PLUGIN_DIR), "the preinstall MU plugin directory overrides the recipe value") assert.ok(preinstallCode.includes("@file_put_contents($result_file, '');"), "multisite preinstall clears stale diagnostics") assert.ok(preinstallCode.includes("STAGE_FAIL:preinstall:"), "multisite preinstall records throwable diagnostics") assert.ok(preinstallCode.includes("STAGE_FATAL:preinstall:"), "multisite preinstall records fatal diagnostics")