Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 10 additions & 11 deletions packages/runtime-core/src/recipe-builders.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 } : {}),
Expand Down Expand Up @@ -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] }
}


Expand Down
4 changes: 3 additions & 1 deletion packages/runtime-playground/src/blueprint.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,9 @@ export function playgroundWpConfigConstants(blueprint: unknown): Record<string,
const constants: Record<string, string | number | boolean | null> = {}
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)) {
Expand Down
18 changes: 17 additions & 1 deletion tests/phpunit-project-autoload.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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",
Expand Down Expand Up @@ -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",
})
Expand All @@ -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")
Expand Down
Loading