Enhance testing framework with WordPress Playground integration

This commit is contained in:
2025-04-21 22:30:10 +01:00
parent 572c23df89
commit d6b2349af8
5 changed files with 245 additions and 24 deletions

View File

@@ -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": "<?php\n// Create a test subsite\n$domain = 'localhost';\n$path = '/testsite/';\n$title = 'Test Subsite';\n$user_id = 1;\n\nif (!get_site_by_path($domain, $path)) {\n $blog_id = wpmu_create_blog($domain, $path, $title, $user_id);\n if (is_wp_error($blog_id)) {\n echo 'Error creating subsite: ' . $blog_id->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

View File

@@ -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', () => {

View File

@@ -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', () => {

View File

@@ -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');
});
});

View File

@@ -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",