From 506004a479da50f6a456bb32a5e9143f16e992ea Mon Sep 17 00:00:00 2001 From: eliot-akira Date: Thu, 23 Oct 2025 20:49:33 +0200 Subject: [PATCH 1/3] Remove wp-now tests --- package.json | 4 +- tests/now/.wp-env.json | 6 - tests/now/index.ts | 57 -------- tests/now/plugin.php | 4 - tests/now/server.ts | 310 ----------------------------------------- 5 files changed, 1 insertion(+), 380 deletions(-) delete mode 100644 tests/now/.wp-env.json delete mode 100644 tests/now/index.ts delete mode 100644 tests/now/plugin.php delete mode 100644 tests/now/server.ts diff --git a/package.json b/package.json index 53ecd36..6e55ea0 100644 --- a/package.json +++ b/package.json @@ -27,12 +27,10 @@ "update": "roll update", "install:dev": "roll install --dev", "update:dev": "roll update --dev", - "archive": "roll archive -y && rm -rf publish/tangible-fields && unzip -q publish/tangible-fields.zip -d publish/ && npm run now:test:archive", + "archive": "roll archive -y", "jest:test": "jest --config=tests/jest/jest.config.js --silent=false test", "now": "wp-now start --blueprint=blueprint.dev.json", "now:dev": "concurrently --raw 'roll dev' 'npm run now'", - "now:test": "roll run tests/now/index.ts", - "now:test:archive": "roll run tests/now/index.ts TEST_ARCHIVE=1", "-- Local environment --": "https://developer.wordpress.org/block-editor/reference-guides/packages/packages-env", "env": "wp-env", "composer": "FOLDER=`basename $(realpath $PWD)`; wp-env run cli --env-cwd=wp-content/plugins/$FOLDER composer", diff --git a/tests/now/.wp-env.json b/tests/now/.wp-env.json deleted file mode 100644 index bb54e67..0000000 --- a/tests/now/.wp-env.json +++ /dev/null @@ -1,6 +0,0 @@ -{ - "extends": "../../.wp-env.json", - "mappings": { - "wp-content/plugins/tangible-fields": "../.." - } -} diff --git a/tests/now/index.ts b/tests/now/index.ts deleted file mode 100644 index c62778b..0000000 --- a/tests/now/index.ts +++ /dev/null @@ -1,57 +0,0 @@ -import { test, is, ok, run } from 'testra' -import { getServer } from './server.ts' - -async function ensurePlugin({ wpx }) { - return wpx/* php */ ` - -if (!function_exists('tangible_template')) { - if (!function_exists('activate_plugin')) { - require ABSPATH . 'wp-admin/includes/plugin.php'; - } - $result = activate_plugin(ABSPATH . 'wp-content/plugins/tangible-fields/plugin.php'); - if (is_wp_error($result)) return $result; -} - -if ( !get_option('site_init_done') ) { - - global $wp_rewrite; - $wp_rewrite->set_permalink_structure('/%postname%/'); - $wp_rewrite->flush_rules(); - - update_option('site_init_done', 1); -} - -return true; -` -} - -/** - * For syntax highlight of PHP in template strings, install: - * https://marketplace.visualstudio.com/items?itemName=bierner.comment-tagged-templates - */ -export default run(async () => { - // Set up server before running tests in Framework - const { - php, - request, - wpx, - documentRoot, - setSiteTemplate, - resetSiteTemplate, - } = await getServer({ - phpVersion: process.env.PHP_VERSION || '8.2', - mappings: process.env.TEST_ARCHIVE - ? { - 'wp-content/plugins/tangible-fields': '../../publish/tangible-fields', - } - : {}, - reset: true, - }) - - let result: any - - test('Plugin - Activate', async () => { - let result = await ensurePlugin({ wpx }) - is(true, result, 'activate plugin') - }) -}) diff --git a/tests/now/plugin.php b/tests/now/plugin.php deleted file mode 100644 index 579a86e..0000000 --- a/tests/now/plugin.php +++ /dev/null @@ -1,4 +0,0 @@ - - token?: string -} - -export function createRequest(siteUrl: string): Requester { - const request: Requester = async ({ - method = 'GET', - route, - /** - * Response format: arrayBuffer, formData, json, text - * @see https://developer.mozilla.org/en-US/docs/Web/API/Response#instance_methods - */ - format = 'json', - data = {}, - }) => { - const isJson = format === 'json' - const hasBody = !['GET', 'HEAD'].includes(method) - let url = `${siteUrl}${isJson ? '/wp-json' : ''}${route}` - - if (!hasBody) { - // Convert data to URL query - url += Object.keys(data).reduce( - (str, key) => str + `${!str ? '?' : '&'}${key}=${data[key]}`, - '', - ) - } - - const options: { - method: string - headers?: { - [key: string]: string - } - body?: string - } = { - method, - } - - if (isJson) { - options.headers = { - 'Content-Type': 'application/json', - } - if (request.token) { - options.headers.Authorization = `Bearer ${request.token}` - } - if (hasBody) { - options.body = JSON.stringify(data) - } - } - - return await (await fetch(url, options))[format]() - } - - request.token = undefined - - return request -} - -/** - * Console - */ -const silentConsole = { - log() {}, - warn() {}, - error() {}, - info() {}, - assert() {}, -} -export const originalConsole = globalThis.console - -export const disableConsole = () => { - // Silence console messages from NodePHP - globalThis.console = silentConsole as Console -} - -export const enableConsole = () => { - globalThis.console = originalConsole -} - -// let serverInstance: Server | null - -export type Server = { - php: PHP - port: number - siteUrl: string - documentRoot: string - options: {} - stopServer: () => Promise - request: Requester - console: Console - - // Template tag functions - phpx: (code: string | TemplateStringsArray, ...args: any[]) => Promise - wpx: (code: string | TemplateStringsArray, ...args: any[]) => Promise - - onMessage: (callback: Listener) => void -} - -export type Listener = (message: any) => void - -export async function getServer( - options: { - path?: string - blueprint?: string - env?: string - mappings?: { - [target: string]: string - } - phpVersion?: string - restart?: boolean - reset?: boolean - } = {}, -): Promise { - if (globalThis.serverInstance) { - if (!options.restart) return globalThis.serverInstance - await globalThis.serverInstance.stopServer() - } - - const { - path: projectPath = path.join(process.cwd(), 'tests', 'now'), - reset = true, - blueprint, - env, - mappings, - ...serverOptions - } = options - - const server: WPNowServer = await startServer({ - ...(await getWpNowConfig({ - path: projectPath, - blueprint, - env, - // @ts-ignore - mappings, - })), - - projectPath, - mode: 'plugin', - phpVersion: '8.2', - // silence: true, - reset, - ...serverOptions, - } as WPNowOptions) - - const { php, stopServer } = server - const { port = 3000, documentRoot = '/var/www/html' } = server.options - - /** - * PHP-WASM provides a function post_message_to_js() to send messages from PHP to JS. - * It expects a single global listener on JS side, with no way to unsubscribe. The following - * is a wrapper to enable multiple listeners with unsubscribe. - */ - - const phpListeners: Listener[] = [] - - // Subscriber - function onMessage(callback: Listener) { - if (phpListeners.indexOf(callback) === -1) { - phpListeners.push(callback) - } - // Unsubscriber - return function unsubscribe() { - phpListeners.splice(phpListeners.indexOf(callback), 1) - } - } - - php.onMessage(function globalListener(data: string) { - try { - const message = JSON.parse(data) - for (const listener of phpListeners) { - listener(message) - } - } catch (e) { - console.error(e) - } - }) - - const phpStart = ' { - if (Array.isArray(code)) { - code = code.reduce( - (prev, now, index) => prev + now + (args[index] ?? ''), - '', - ) - } - - disableConsole() - - let result - try { - result = await php.run({ - code: phpStart + (code as string).replace(phpStartRegex, ''), - }) - } catch (e: any) { - result = { errors: e.message } - } - const { text, errors } = result - - enableConsole() - - if (errors) throw new Error(errors) - else return text - } - - /** - * Run PHP code in WordPress context - * - * ```js - * const result = wpx`return 'hi';` - * ``` - */ - const wpx = async ( - code: string | TemplateStringsArray, - ...args: string[] - ) => { - if (Array.isArray(code)) { - code = code.reduce( - (prev, now, index) => prev + now + (args[index] ?? ''), - '', - ) - } - - // Extract lines to use namespace and put them at the top - let useNamespace = '' - let filteredCode = '' - - for (const line of (code as string).split('\n')) { - if (line.trim().startsWith('use ')) { - useNamespace += line + '\n' - } else { - filteredCode += line + '\n' - } - } - - const result = await phpx/* php */ ` -${useNamespace} -include 'wp-load.php'; -echo json_encode((function() { - try { - ${filteredCode} - } catch (Exception $e) { - return [ - 'error' => $e->getMessage() - ]; - } -})());` - - try { - return JSON.parse(result || '') - } catch (e) { - console.error(e) - } - } - - await wpx/* php */ ` -// Pretty permalinks -global $wp_rewrite; -$wp_rewrite->set_permalink_structure('/%postname%/'); -$wp_rewrite->flush_rules(); -` - - const siteUrl = `http://localhost:${port}` - - return (globalThis.serverInstance = { - php, - port, - console: originalConsole, - siteUrl, - documentRoot, - options: server.options, - request: createRequest(siteUrl), - phpx, - wpx, - async stopServer() { - globalThis.serverInstance = null - await stopServer() - }, - onMessage, - }) -} From c0ca6b31d653f0610d0bf2bb9bfa3db08a8cd754 Mon Sep 17 00:00:00 2001 From: eliot-akira Date: Thu, 23 Oct 2025 21:06:41 +0200 Subject: [PATCH 2/3] Remove wp-now --- package.json | 3 --- 1 file changed, 3 deletions(-) diff --git a/package.json b/package.json index 6e55ea0..9cac001 100644 --- a/package.json +++ b/package.json @@ -29,8 +29,6 @@ "update:dev": "roll update --dev", "archive": "roll archive -y", "jest:test": "jest --config=tests/jest/jest.config.js --silent=false test", - "now": "wp-now start --blueprint=blueprint.dev.json", - "now:dev": "concurrently --raw 'roll dev' 'npm run now'", "-- Local environment --": "https://developer.wordpress.org/block-editor/reference-guides/packages/packages-env", "env": "wp-env", "composer": "FOLDER=`basename $(realpath $PWD)`; wp-env run cli --env-cwd=wp-content/plugins/$FOLDER composer", @@ -80,7 +78,6 @@ "@storybook/addon-onboarding": "^9.0.5", "@storybook/builder-vite": "^9.0.17", "@storybook/react-vite": "^9.0.17", - "@tangible/now": "^3.0.2", "@tangible/roller": "^2.1.7", "@testing-library/jest-dom": "^6.6.3", "@testing-library/react": "^16.3.0", From 7d2cd2d5d95ac8fbfaa1037e233916cb437c55d9 Mon Sep 17 00:00:00 2001 From: eliot-akira Date: Sat, 25 Oct 2025 10:09:30 +0200 Subject: [PATCH 3/3] Update documentation to use wp-env for local dev and test site --- readme.md | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/readme.md b/readme.md index eb0d334..c649bfa 100644 --- a/readme.md +++ b/readme.md @@ -76,15 +76,15 @@ Format to code standard npm run format ``` -## Local test site +## Local dev site -Start a local test site using [`wp-now`](https://github.com/WordPress/playground-tools/blob/trunk/packages/wp-now/README.md). +Start a local dev site using [`wp-env`](https://developer.wordpress.org/block-editor/reference-guides/packages/packages-env/). ```sh npm run start ``` -For convenience, this also concurrently runs `npm run dev`. +For details see [Tests](#tests) below. ### Dev dependencies @@ -145,32 +145,32 @@ Alternatively, you can use the [wp-env](https://developer.wordpress.org/block-ed The test environment is started by running: ```sh -npm run env:start +npm run start ``` This uses [`wp-env`](https://developer.wordpress.org/block-editor/reference-guides/packages/packages-env/), which requires Docker to be installed. There are instructions available for installing Docker on [Windows](https://docs.docker.com/desktop/install/windows-install/), [macOS](https://docs.docker.com/desktop/install/mac-install/), and [Linux](https://docs.docker.com/desktop/install/linux-install/). -Visit [http://localhost:8888](http://localhost:8888) to see the dev site, and [http://localhost:8889](http://localhost:8880) for the test site, whose database is cleared on every run. +Visit [http://localhost:8888](http://localhost:8888) to see the dev site (default user `admin` with `password`); and [http://localhost:8889](http://localhost:8880) for the test site, whose database is cleared on every run. Before running tests, install PHPUnit as a dev dependency using Composer in the container. ```sh -npm run env:composer:install +npm run composer:install ``` To run the tests: ```sh -npm run env:test:8.2 -npm run env:test:7.4 +npm run test:8.2 +npm run test:7.4 ``` -The version-specific commands take a while to start, but afterwards you can run npm run env:test to re-run tests in the same environment. +The version-specific commands take a while to start, but afterwards you can run npm run test to re-run tests in the same environment. To stop the Docker process: ```sh -npm run env:stop +npm run stop ``` To “destroy” and remove cache: @@ -184,7 +184,7 @@ npm run env:destroy To run more than one instance of `wp-env`, set different ports for the dev and test sites: ```sh -WP_ENV_PORT=3333 WP_ENV_TESTS_PORT=3334 npm run env:start +WP_ENV_PORT=3333 WP_ENV_TESTS_PORT=3334 npm run start ``` ---