From d6b2349af8312509f8e1f4be3f49c6de0f4644c8 Mon Sep 17 00:00:00 2001 From: marcusquinn <6428977+marcusquinn@users.noreply.github.com> Date: Mon, 21 Apr 2025 22:30:10 +0100 Subject: [PATCH] Enhance testing framework with WordPress Playground integration --- bin/setup-test-env.sh | 184 ++++++++++++++++++++++++++++++++-- cypress/e2e/multisite.cy.js | 8 +- cypress/e2e/single-site.cy.js | 8 +- cypress/support/commands.js | 61 ++++++++++- package.json | 8 +- 5 files changed, 245 insertions(+), 24 deletions(-) diff --git a/bin/setup-test-env.sh b/bin/setup-test-env.sh index 904f7e7..5d98dbf 100644 --- a/bin/setup-test-env.sh +++ b/bin/setup-test-env.sh @@ -3,23 +3,41 @@ # Make this script executable chmod +x "$0" -# Check if wp-env is installed -if ! command -v wp-env &> /dev/null; then - echo "wp-env is not installed. Installing..." - npm install -g @wordpress/env -fi - # Check if environment type is provided if [ -z "$1" ]; then - echo "Usage: $0 [single|multisite]" + echo "Usage: $0 [single|multisite|playground-single|playground-multisite]" exit 1 fi ENV_TYPE=$1 +# Function to check if a command exists +command_exists() { + command -v "$1" &> /dev/null +} + +# Function to install wp-env if needed +install_wp_env() { + if ! command_exists wp-env; then + echo "wp-env is not installed. Installing..." + npm install -g @wordpress/env + fi +} + +# Function to install wp-playground if needed +install_wp_playground() { + if ! command_exists wp-playground; then + echo "wp-playground is not installed. Installing..." + npm install -g @wordpress/playground-tools + fi +} + if [ "$ENV_TYPE" == "single" ]; then echo "Setting up single site environment..." + # Install wp-env if needed + install_wp_env + # Start the environment wp-env start @@ -51,6 +69,9 @@ if [ "$ENV_TYPE" == "single" ]; then elif [ "$ENV_TYPE" == "multisite" ]; then echo "Setting up multisite environment..." + # Install wp-env if needed + install_wp_env + # Start the environment with multisite configuration wp-env start --config=.wp-env.multisite.json @@ -86,7 +107,154 @@ elif [ "$ENV_TYPE" == "multisite" ]; then echo "Test site: http://localhost:8888/testsite" echo "Admin login: admin / password" +elif [ "$ENV_TYPE" == "playground-single" ]; then + echo "Setting up WordPress Playground single site environment..." + + # Install wp-playground if needed + install_wp_playground + + # Create plugin zip + echo "Creating plugin zip..." + mkdir -p dist + zip -r dist/plugin.zip . -x "node_modules/*" "dist/*" ".git/*" + + # Update blueprint to use local plugin + cat > playground/blueprint.json << EOF +{ + "landingPage": "/wp-admin/", + "preferredVersions": { + "php": "8.0", + "wp": "latest" + }, + "steps": [ + { + "step": "login", + "username": "admin", + "password": "password" + }, + { + "step": "installPlugin", + "pluginZipFile": { + "resource": "local", + "path": "dist/plugin.zip" + } + }, + { + "step": "activatePlugin", + "pluginSlug": "wp-plugin-starter-template-for-ai-coding" + } + ] +} +EOF + + # Start WordPress Playground + echo "Starting WordPress Playground..." + wp-playground start --blueprint playground/blueprint.json --port 8888 & + + # Wait for WordPress Playground to be ready + echo "Waiting for WordPress Playground to be ready..." + sleep 5 + + echo "WordPress Playground Single Site environment is ready!" + echo "Site: http://localhost:8888" + echo "Admin login: admin / password" + echo "Press Ctrl+C to stop the server when done." + +elif [ "$ENV_TYPE" == "playground-multisite" ]; then + echo "Setting up WordPress Playground multisite environment..." + + # Install wp-playground if needed + install_wp_playground + + # Create plugin zip + echo "Creating plugin zip..." + mkdir -p dist + zip -r dist/plugin.zip . -x "node_modules/*" "dist/*" ".git/*" + + # Update blueprint to use local plugin + cat > playground/multisite-blueprint.json << EOF +{ + "landingPage": "/wp-admin/network/", + "preferredVersions": { + "php": "8.0", + "wp": "latest" + }, + "steps": [ + { + "step": "defineWpConfig", + "name": "WP_ALLOW_MULTISITE", + "value": true + }, + { + "step": "defineWpConfig", + "name": "MULTISITE", + "value": true + }, + { + "step": "defineWpConfig", + "name": "SUBDOMAIN_INSTALL", + "value": false + }, + { + "step": "defineWpConfig", + "name": "DOMAIN_CURRENT_SITE", + "value": "localhost" + }, + { + "step": "defineWpConfig", + "name": "PATH_CURRENT_SITE", + "value": "/" + }, + { + "step": "defineWpConfig", + "name": "SITE_ID_CURRENT_SITE", + "value": 1 + }, + { + "step": "defineWpConfig", + "name": "BLOG_ID_CURRENT_SITE", + "value": 1 + }, + { + "step": "login", + "username": "admin", + "password": "password" + }, + { + "step": "installPlugin", + "pluginZipFile": { + "resource": "local", + "path": "dist/plugin.zip" + } + }, + { + "step": "activatePlugin", + "pluginSlug": "wp-plugin-starter-template-for-ai-coding", + "networkWide": true + }, + { + "step": "runPHP", + "code": "get_error_message();\n } else {\n echo 'Created subsite with ID: ' . $blog_id;\n }\n} else {\n echo 'Subsite already exists';\n}\n" + } + ] +} +EOF + + # Start WordPress Playground + echo "Starting WordPress Playground..." + wp-playground start --blueprint playground/multisite-blueprint.json --port 8888 & + + # Wait for WordPress Playground to be ready + echo "Waiting for WordPress Playground to be ready..." + sleep 5 + + echo "WordPress Playground Multisite environment is ready!" + echo "Main site: http://localhost:8888" + echo "Test site: http://localhost:8888/testsite" + echo "Admin login: admin / password" + echo "Press Ctrl+C to stop the server when done." + else - echo "Invalid environment type. Use 'single' or 'multisite'." + echo "Invalid environment type. Use 'single', 'multisite', 'playground-single', or 'playground-multisite'." exit 1 fi diff --git a/cypress/e2e/multisite.cy.js b/cypress/e2e/multisite.cy.js index 66f8117..2da6b82 100644 --- a/cypress/e2e/multisite.cy.js +++ b/cypress/e2e/multisite.cy.js @@ -28,11 +28,11 @@ describe('WordPress Multisite Tests', () => { }); it('Plugin is network activated', () => { - cy.loginAsAdmin(); + // Use our custom command to check and network activate the plugin if needed + cy.networkActivatePlugin('wp-plugin-starter-template-for-ai-coding'); - // Check plugins page - cy.visit('/wp-admin/network/plugins.php'); - cy.contains('tr', 'WP Plugin Starter Template').should('contain', 'Network Active'); + // Verify it's network active + cy.get('tr[data-slug="wp-plugin-starter-template-for-ai-coding"] .network_active').should('exist'); }); it('Network settings page loads correctly', () => { diff --git a/cypress/e2e/single-site.cy.js b/cypress/e2e/single-site.cy.js index 603593f..b4eee4a 100644 --- a/cypress/e2e/single-site.cy.js +++ b/cypress/e2e/single-site.cy.js @@ -11,11 +11,11 @@ describe('WordPress Single Site Tests', () => { }); it('Plugin is activated', () => { - cy.loginAsAdmin(); + // Use our custom command to check and activate the plugin if needed + cy.activatePlugin('wp-plugin-starter-template-for-ai-coding'); - // Check plugins page - cy.visit('/wp-admin/plugins.php'); - cy.contains('tr', 'WP Plugin Starter Template').should('contain', 'Deactivate'); + // Verify it's active + cy.get('tr[data-slug="wp-plugin-starter-template-for-ai-coding"] .deactivate').should('exist'); }); it('Plugin settings page loads correctly', () => { diff --git a/cypress/support/commands.js b/cypress/support/commands.js index 8aef097..5e00960 100644 --- a/cypress/support/commands.js +++ b/cypress/support/commands.js @@ -13,8 +13,61 @@ */ Cypress.Commands.add('loginAsAdmin', () => { cy.visit('/wp-admin'); - cy.get('#user_login').type('admin'); - cy.get('#user_pass').type('password'); - cy.get('#wp-submit').click(); - cy.get('body.wp-admin').should('exist'); + + // Check if we're already logged in + cy.get('body').then(($body) => { + if ($body.find('body.wp-admin').length > 0) { + // Already logged in + cy.log('Already logged in as admin'); + return; + } + + // Need to log in + cy.get('#user_login').type('admin'); + cy.get('#user_pass').type('password'); + cy.get('#wp-submit').click(); + cy.get('body.wp-admin').should('exist'); + }); +}); + +/** + * Custom command to activate plugin + */ +Cypress.Commands.add('activatePlugin', (pluginSlug) => { + cy.loginAsAdmin(); + cy.visit('/wp-admin/plugins.php'); + + // Check if plugin is already active + cy.get(`tr[data-slug="${pluginSlug}"]`).then(($tr) => { + if ($tr.find('.deactivate').length > 0) { + // Plugin is already active + cy.log(`Plugin ${pluginSlug} is already active`); + return; + } + + // Activate the plugin + cy.get(`tr[data-slug="${pluginSlug}"] .activate a`).click(); + cy.get(`tr[data-slug="${pluginSlug}"] .deactivate`).should('exist'); + }); +}); + +/** + * Custom command to network activate plugin + */ +Cypress.Commands.add('networkActivatePlugin', (pluginSlug) => { + cy.loginAsAdmin(); + cy.visit('/wp-admin/network/plugins.php'); + + // Check if plugin is already network active + cy.get(`tr[data-slug="${pluginSlug}"]`).then(($tr) => { + if ($tr.find('.network_active').length > 0) { + // Plugin is already network active + cy.log(`Plugin ${pluginSlug} is already network active`); + return; + } + + // Network activate the plugin + cy.get(`tr[data-slug="${pluginSlug}"] .activate a`).click(); + cy.get(`tr[data-slug="${pluginSlug}"] .network_active`).should('exist'); + }); }); diff --git a/package.json b/package.json index 870f171..d8ab102 100644 --- a/package.json +++ b/package.json @@ -13,10 +13,10 @@ "test:single:headless": "cypress run --config specPattern=cypress/e2e/single-site.cy.js", "test:multisite": "cypress open --config specPattern=cypress/e2e/multisite.cy.js", "test:multisite:headless": "cypress run --config specPattern=cypress/e2e/multisite.cy.js", - "playground:single": "wp-playground start --blueprint playground/blueprint.json --port 8888", - "playground:multisite": "wp-playground start --blueprint playground/multisite-blueprint.json --port 8888", - "test:playground:single": "npm run playground:single & sleep 10 && npm run test:single:headless", - "test:playground:multisite": "npm run playground:multisite & sleep 10 && npm run test:multisite:headless", + "setup:playground:single": "bash bin/setup-test-env.sh playground-single", + "setup:playground:multisite": "bash bin/setup-test-env.sh playground-multisite", + "test:playground:single": "npm run setup:playground:single && sleep 10 && npm run test:single:headless", + "test:playground:multisite": "npm run setup:playground:multisite && sleep 10 && npm run test:multisite:headless", "build": "./build.sh", "lint:php": "composer run-script phpcs", "lint:php:simple": "composer run-script phpcs:simple",