From 535dbda6b48171f8115b455e54790de94d97d42a Mon Sep 17 00:00:00 2001 From: Murilo Ijanc Date: Sun, 26 Jul 2026 16:41:48 -0300 Subject: Initial import --- test/wp.js | 88 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 88 insertions(+) create mode 100644 test/wp.js (limited to 'test/wp.js') diff --git a/test/wp.js b/test/wp.js new file mode 100644 index 0000000..ab2d2a7 --- /dev/null +++ b/test/wp.js @@ -0,0 +1,88 @@ +/** + * Talking to the WordPress under test. + * + * Everything goes through WP-CLI in the `cli` container of `compose.yml`, so + * the tests configure the store exactly the way a store owner would. + * + * @module + */ + +import { spawnSync } from "node:child_process"; +import { dirname } from "node:path"; +import { fileURLToPath } from "node:url"; + +const HERE = dirname(fileURLToPath(import.meta.url)); + +/** Where the site answers, from the host. */ +export const SITE = "http://127.0.0.1:8090"; + +/** Hostname the fake API is reached by, from both the browser and PHP. */ +export const FAKE_HOST = "fakecep"; + +/** Port the fake API listens on. */ +export const FAKE_PORT = 8899; + +/** Base URL of the fake API, as both sides must spell it. */ +export const FAKE_URL = `http://${FAKE_HOST}:${FAKE_PORT}`; + +/** + * Run a WP-CLI command in the container. + * + * @param {string[]} args Arguments after `wp`. + * @returns {string} Trimmed stdout. + */ +export function wp(args) { + const result = spawnSync("docker", ["compose", "exec", "-T", "cli", "wp", ...args], { + cwd: HERE, + encoding: "utf8", + }); + + if (result.status !== 0) { + throw new Error(`wp ${args.join(" ")} failed:\n${result.stdout}\n${result.stderr}`); + } + + return String(result.stdout).trim(); +} + +/** + * Run PHP inside WordPress. + * + * @param {string} code PHP to evaluate. + * @returns {string} Trimmed stdout. + */ +export function wpEval(code) { + return wp(["eval", code]); +} + +/** + * Set one option. + * + * @param {string} name Option name. + * @param {string} value Option value. + */ +export function setOption(name, value) { + wp(["option", "update", name, value]); +} + +/** + * Read the WordPress debug log and clear it. + * + * @returns {string} Whatever had been logged. + */ +export function drainDebugLog() { + const result = spawnSync( + "docker", + [ + "compose", + "exec", + "-T", + "cli", + "sh", + "-c", + "cat /var/www/html/wp-content/debug.log 2>/dev/null; : > /var/www/html/wp-content/debug.log 2>/dev/null || true", + ], + { cwd: HERE, encoding: "utf8" }, + ); + + return String(result.stdout || "").trim(); +} -- cgit v1.3