Commit 1fb053a1643 for woocommerce
commit 1fb053a1643918d6add08b5c45de3844feec313f
Author: Ján Mikláš <neosinner@gmail.com>
Date: Wed Dec 2 10:07:41 2026 +0100
Fix extension wp-env setup on WP-CLI 3 (#71695)
* Fix extension wp-env setup on WP-CLI 3
* Add changelog entry for WP-CLI 3 compatibility
* Document direct wp-env startup hooks
* Retarget chained wp-env activation checks
* Reject activation checks against another site
* Pass browser config explicitly to wp-env hooks
* Document shell for wp-env activation hooks
diff --git a/packages/js/extension-tooling/README.md b/packages/js/extension-tooling/README.md
index 6a9e05b0563..0f1cb08489a 100644
--- a/packages/js/extension-tooling/README.md
+++ b/packages/js/extension-tooling/README.md
@@ -24,6 +24,7 @@ Run `pnpm exec woo-extension-tooling <command> [args]` from the extension root.
| `build-zip [commit]` | Run the configured build and write `deploy/<slug>.zip`. A commit argument selects committed files when the build step is empty. |
| `makepot` | Generate `languages/<slug>.pot` from the extension's translation settings with WP-CLI. |
| `e2e <mode> [args]` | Keep the existing `prepare`, `start`, `setup`, `reset`, `run`, and `with-env` modes. `prepare` extracts the ZIP and calls the extension's `bin/link_plugins.js`. |
+| `check-wp-env-plugins <wp-env-config>` | Check that WooCommerce, WooCommerce Gateway Dummy, and the extension are active on a single-site wp-env installation. |
| `phpunit [setup\|run\|help] [args]` | Provision the wp-env test site or run its PHPUnit suite. Use `phpunit direct <config> [args]` for an extension that already provisions the site separately. |
| `phplint [paths]`, `phpcs [args]`, `phpcs-baseline [args]` | Check PHP syntax, report PHPCS findings outside the baseline, or generate a baseline with the extension's Composer tools. |
| `phpcompat [archive]` | Check the supplied production ZIP, or run the extension's `build:zip` script first. |
@@ -134,6 +135,26 @@ Add this mapping alongside the extension's existing `wp-content/bin` mapping in
Use `wp-env run cli bash wp-content/woo-extension-tooling/seed-dev.sh wp-content/bin/seed_dev_plugin.sh` for `env:dev:seed`. Pass the fixture hook's path inside the container. Include WooCommerce Gateway Dummy in the development site's plugin list to use the seeded dummy payment method.
+### Set up disposable sites with WP-CLI 3
+
+Add the compatibility mu-plugin to the mappings in `.wp-env.json` and `.wp-env.test.json`. It restores unlimited PHP memory for WP-CLI and skips the WooCommerce setup redirect during CLI commands. Browser requests still get the normal setup redirect.
+
+```json
+{
+ "mappings": {
+ "wp-content/mu-plugins/wp-cli-compatibility.php": "./node_modules/@woocommerce/extension-tooling/bin/wp-cli-compatibility.php"
+ },
+ "lifecycleScripts": {
+ "afterStart": "pnpm exec woo-extension-tooling check-wp-env-plugins \"${WOO_EXTENSION_TOOLING_WP_ENV_CONFIG:-.wp-env.test.json}\"",
+ "afterReset": "pnpm exec woo-extension-tooling check-wp-env-plugins \"${WOO_EXTENSION_TOOLING_WP_ENV_CONFIG:-.wp-env.test.json}\""
+ }
+}
+```
+
+For single-site setups with WooCommerce, WooCommerce Gateway Dummy and the extension, add the lifecycle scripts above to catch commands that exit successfully without activating a plugin. The hooks use a POSIX shell. Pass `.wp-env.json` as the argument in the development config. The browser test site generator carries over the mapping and sets `WOO_EXTENSION_TOOLING_WP_ENV_CONFIG` in the generated hooks so they check `.wp-env.e2e.json`.
+
+If the test config also mounts a repository directory at `wp-content/mu-plugins`, ignore `wp-cli-compatibility.php` in that directory. Docker creates an empty mount target there and loads the package's file in the container.
+
### Provision a browser test store
For suites that switch between shortcode and block checkout, add the same `./wp-content/woo-extension-tooling` mapping to the browser test site. After resetting the site, call the provisioner in place of `wp_env_config.sh`. Keep the extension-specific `wp_env_config_plugin.sh` as the next step.
diff --git a/packages/js/extension-tooling/bin/check-wp-env-plugins.sh b/packages/js/extension-tooling/bin/check-wp-env-plugins.sh
new file mode 100644
index 00000000000..00ee6c6593f
--- /dev/null
+++ b/packages/js/extension-tooling/bin/check-wp-env-plugins.sh
@@ -0,0 +1,27 @@
+#!/usr/bin/env bash
+set -euo pipefail
+
+if [[ $# -ne 1 ]]; then
+ echo 'Usage: woo-extension-tooling check-wp-env-plugins <wp-env-config>' >&2
+ exit 1
+fi
+
+main_file=$(bash "$(dirname "$0")/woo-config.sh" '.mainFile | split("/") | last')
+output=$(wp-env --config "$1" run cli wp option get active_plugins -- --format=json --skip-plugins --skip-themes)
+
+# Match filenames since wp-env may name mounts after a ZIP or worktree directory.
+if ! active_plugins=$(jq -ces 'if length == 1 then .[0] | [.[] | split("/") | last] else error("Expected one JSON value") end' <<< "$output"); then
+ printf 'Could not read the active plugins from %s.\n' "$1" >&2
+ exit 1
+fi
+
+status=0
+
+for plugin in woocommerce.php woocommerce-gateway-dummy.php "$main_file"; do
+ if ! jq -e --arg plugin "$plugin" 'index($plugin) != null' >/dev/null <<< "$active_plugins"; then
+ printf 'Plugin not active: %s\n' "$plugin" >&2
+ status=1
+ fi
+done
+
+exit "$status"
diff --git a/packages/js/extension-tooling/bin/woo-extension-tooling.js b/packages/js/extension-tooling/bin/woo-extension-tooling.js
index 399b5eca969..7162856633b 100755
--- a/packages/js/extension-tooling/bin/woo-extension-tooling.js
+++ b/packages/js/extension-tooling/bin/woo-extension-tooling.js
@@ -9,6 +9,7 @@ const commands = [
'install',
'makepot',
'e2e',
+ 'check-wp-env-plugins',
'phpunit',
'phplint',
'phpcs',
diff --git a/packages/js/extension-tooling/bin/wp-cli-compatibility.php b/packages/js/extension-tooling/bin/wp-cli-compatibility.php
new file mode 100644
index 00000000000..295e02a75cc
--- /dev/null
+++ b/packages/js/extension-tooling/bin/wp-cli-compatibility.php
@@ -0,0 +1,17 @@
+<?php
+/**
+ * Plugin Name: Woo Extension Tooling WP-CLI Compatibility
+ * Description: Keeps WooCommerce extension wp-env setup working with WP-CLI 3.
+ *
+ * @package WooCommerce\ExtensionTooling
+ */
+
+defined( 'ABSPATH' ) || exit;
+
+if ( defined( 'WP_CLI' ) && WP_CLI ) {
+ // phpcs:ignore WordPress.PHP.IniSet.memory_limit_Disallowed -- Restore the WP-CLI 2 memory limit on disposable sites.
+ ini_set( 'memory_limit', '-1' );
+
+ // WP-CLI 3 runs plugin and theme commands in admin context, where the redirect can abort them.
+ add_filter( 'woocommerce_enable_setup_wizard', '__return_false' );
+}
diff --git a/packages/js/extension-tooling/changelog/add-wp-cli-3-wp-env b/packages/js/extension-tooling/changelog/add-wp-cli-3-wp-env
new file mode 100644
index 00000000000..e50e22e38bf
--- /dev/null
+++ b/packages/js/extension-tooling/changelog/add-wp-cli-3-wp-env
@@ -0,0 +1,4 @@
+Significance: minor
+Type: add
+
+Keep WooCommerce extension wp-env setup working with WP-CLI 3 and check that required plugins activate.
diff --git a/packages/js/extension-tooling/src/e2e-site-config.js b/packages/js/extension-tooling/src/e2e-site-config.js
index 6f9ac60bb95..a8f4400b6b5 100644
--- a/packages/js/extension-tooling/src/e2e-site-config.js
+++ b/packages/js/extension-tooling/src/e2e-site-config.js
@@ -143,6 +143,19 @@ function writeE2ESiteConfig( {
delete developmentMappings?.[ debugLog ];
delete developmentMappings?.[ debugLogMount ];
config.mappings[ debugLogMount ] = './tests/_output/e2e-debug.log';
+
+ const cliConfigVariable = 'WOO_EXTENSION_TOOLING_WP_ENV_CONFIG';
+ for ( const hook of [ 'afterStart', 'afterReset' ] ) {
+ const script = config.lifecycleScripts?.[ hook ];
+ if (
+ typeof script === 'string' &&
+ script.includes( cliConfigVariable )
+ ) {
+ config.lifecycleScripts[ hook ] =
+ `export ${ cliConfigVariable }=.wp-env.e2e.json\n${ script }`;
+ }
+ }
+
fs.writeFileSync(
path.join( root, '.wp-env.e2e.json' ),
JSON.stringify( config )
diff --git a/packages/js/extension-tooling/test/check-wp-env-plugins.test.js b/packages/js/extension-tooling/test/check-wp-env-plugins.test.js
new file mode 100644
index 00000000000..618219a1ae4
--- /dev/null
+++ b/packages/js/extension-tooling/test/check-wp-env-plugins.test.js
@@ -0,0 +1,72 @@
+/**
+ * External dependencies
+ */
+const assert = require( 'node:assert/strict' );
+const fs = require( 'node:fs' );
+const os = require( 'node:os' );
+const path = require( 'node:path' );
+const { spawnSync } = require( 'node:child_process' );
+const { test } = require( 'node:test' );
+
+const tooling = path.join( __dirname, '../bin/woo-extension-tooling.js' );
+const wooCommerce = 'woocommerce.latest-stable/woocommerce.php';
+const extension = 'another-worktree/extension-main.php';
+const dummyGateway = 'woocommerce-gateway-dummy/woocommerce-gateway-dummy.php';
+
+function runCheck( context, output ) {
+ const root = fs.mkdtempSync(
+ path.join( os.tmpdir(), 'woo-plugin-check-' )
+ );
+ context.after( () => fs.rmSync( root, { recursive: true, force: true } ) );
+ fs.writeFileSync(
+ path.join( root, 'package.json' ),
+ JSON.stringify( {
+ name: 'extension',
+ config: { woo: { mainFile: 'extension-main.php' } },
+ } )
+ );
+ fs.writeFileSync(
+ path.join( root, 'wp-env' ),
+ '#!/bin/sh\ntest "$*" = "--config .wp-env.test.json run cli wp option get active_plugins -- --format=json --skip-plugins --skip-themes" || exit 99\nprintf "%s\\n" "$ACTIVE_PLUGINS"\n',
+ { mode: 0o755 }
+ );
+
+ return spawnSync(
+ process.execPath,
+ [ tooling, 'check-wp-env-plugins', '.wp-env.test.json' ],
+ {
+ cwd: root,
+ env: {
+ ...process.env,
+ PATH: root + path.delimiter + process.env.PATH,
+ ACTIVE_PLUGINS:
+ typeof output === 'string'
+ ? output
+ : JSON.stringify( output ),
+ },
+ encoding: 'utf8',
+ }
+ );
+}
+
+test( 'accepts active plugins mounted under different directory names', ( context ) => {
+ const result = runCheck( context, [
+ wooCommerce,
+ extension,
+ dummyGateway,
+ ] );
+ assert.equal( result.status, 0, result.stderr );
+} );
+
+test( 'fails when wp-env silently skips the gateway activation', ( context ) => {
+ const result = runCheck( context, [ wooCommerce, extension ] );
+ assert.equal( result.status, 1 );
+ assert.match( result.stderr, /not active: woocommerce-gateway-dummy\.php/ );
+} );
+
+test( 'distinguishes malformed output from missing plugins', ( context ) => {
+ const result = runCheck( context, 'Notice: unexpected output' );
+ assert.equal( result.status, 1 );
+ assert.match( result.stderr, /Could not read the active plugins/ );
+ assert.doesNotMatch( result.stderr, /Plugin not active/ );
+} );
diff --git a/packages/js/extension-tooling/test/e2e-site-config.test.js b/packages/js/extension-tooling/test/e2e-site-config.test.js
index c2f3f4c8899..15d7f33d9a1 100644
--- a/packages/js/extension-tooling/test/e2e-site-config.test.js
+++ b/packages/js/extension-tooling/test/e2e-site-config.test.js
@@ -1,4 +1,5 @@
const assert = require( 'node:assert/strict' );
+const { execFileSync } = require( 'node:child_process' );
const fs = require( 'node:fs' );
const os = require( 'node:os' );
const path = require( 'node:path' );
@@ -45,6 +46,51 @@ test( 'mounts the packaged plugin and adds a separate debug log', ( context ) =>
);
} );
+test( 'runs the activation check on the generated browser test site', ( context ) => {
+ const { root, pluginPath, defaults, input, output } = site( context );
+ const argument =
+ '"${WOO_EXTENSION_TOOLING_WP_ENV_CONFIG:-.wp-env.test.json}"';
+ defaults.lifecycleScripts = {
+ afterStart: `echo ready && printf '%s' ${ argument }`,
+ afterReset: `printf '%s' ${ argument }`,
+ afterClean: 'echo unchanged',
+ };
+ fs.writeFileSync( input, JSON.stringify( defaults ) );
+
+ writeE2ESiteConfig( { root, pluginPath, env: {} } );
+
+ const { lifecycleScripts } = JSON.parse(
+ fs.readFileSync( output, 'utf8' )
+ );
+ const run = ( script ) =>
+ execFileSync( 'sh', [ '-c', script ], { encoding: 'utf8', env: {} } );
+ assert.equal(
+ run( defaults.lifecycleScripts.afterStart ),
+ 'ready\n.wp-env.test.json'
+ );
+ assert.equal(
+ run( lifecycleScripts.afterStart ),
+ 'ready\n.wp-env.e2e.json'
+ );
+ assert.equal( run( lifecycleScripts.afterReset ), '.wp-env.e2e.json' );
+ assert.equal( lifecycleScripts.afterClean, 'echo unchanged' );
+} );
+
+test( 'leaves hooks that do not opt into browser config selection unchanged', ( context ) => {
+ const { root, pluginPath, defaults, input, output } = site( context );
+ defaults.lifecycleScripts = {
+ afterStart:
+ "echo 'Run woo-extension-tooling check-wp-env-plugins .wp-env.test.json separately'",
+ };
+ fs.writeFileSync( input, JSON.stringify( defaults ) );
+
+ writeE2ESiteConfig( { root, pluginPath, env: {} } );
+ assert.deepEqual(
+ JSON.parse( fs.readFileSync( output, 'utf8' ) ).lifecycleScripts,
+ defaults.lifecycleScripts
+ );
+} );
+
test( 'sets pinned WordPress, WooCommerce and PHP versions', ( context ) => {
const { root, pluginPath, input, defaults, output } = site( context );
defaults.env = {
diff --git a/packages/js/extension-tooling/test/wp-cli-compatibility.test.js b/packages/js/extension-tooling/test/wp-cli-compatibility.test.js
new file mode 100644
index 00000000000..b52f33b9ef4
--- /dev/null
+++ b/packages/js/extension-tooling/test/wp-cli-compatibility.test.js
@@ -0,0 +1,53 @@
+/**
+ * External dependencies
+ */
+const assert = require( 'node:assert/strict' );
+const { spawnSync } = require( 'node:child_process' );
+const path = require( 'node:path' );
+const { test } = require( 'node:test' );
+
+const shim = path.join( __dirname, '../bin/wp-cli-compatibility.php' );
+
+const cases = [
+ { context: 'WP-CLI', value: 'true', memory: '-1', wizard: false },
+ { context: 'browser', value: null, memory: '128M', wizard: true },
+ {
+ context: 'disabled WP-CLI',
+ value: 'false',
+ memory: '128M',
+ wizard: true,
+ },
+];
+
+for ( const { context, value, memory, wizard } of cases ) {
+ test( `preserves the expected setup behavior for ${ context }`, () => {
+ const result = spawnSync(
+ 'php',
+ [
+ '-d',
+ 'memory_limit=128M',
+ '-r',
+ `
+ define( 'ABSPATH', __DIR__ );
+ ${ value === null ? '' : `define( 'WP_CLI', ${ value } );` }
+ function __return_false() { return false; }
+ function add_filter( $hook, $callback ) {
+ $GLOBALS['filters'][ $hook ] = $callback;
+ }
+ require $argv[1];
+ $wizard = $GLOBALS['filters']['woocommerce_enable_setup_wizard'] ?? null;
+ echo json_encode( [ ini_get( 'memory_limit' ), $wizard ? $wizard() : true ] );
+ `,
+ shim,
+ ],
+ { encoding: 'utf8' }
+ );
+
+ assert.equal(
+ result.status,
+ 0,
+ result.error?.message ?? result.stderr
+ );
+ assert.deepEqual( JSON.parse( result.stdout ), [ memory, wizard ] );
+ } );
+}