Fix code quality issues and improve test framework

This commit is contained in:
2025-04-21 21:23:23 +01:00
parent ed160ed51b
commit 051bc763f8
11 changed files with 155 additions and 75 deletions

View File

@@ -10,59 +10,59 @@ jobs:
single-site-test:
name: Single Site Tests
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/checkout@v4
- name: Setup Node.js
uses: actions/setup-node@v3
uses: actions/setup-node@v4
with:
node-version: '16'
node-version: '20'
cache: 'npm'
- name: Install dependencies
run: npm ci
- name: Install wp-env
run: npm install -g @wordpress/env
- name: Setup WordPress Single Site
run: |
chmod +x bin/setup-test-env.sh
bash bin/setup-test-env.sh single
- name: Install Cypress
run: npm install cypress
- name: Run Cypress tests
run: npm run test:single:headless
multisite-test:
name: Multisite Tests
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/checkout@v4
- name: Setup Node.js
uses: actions/setup-node@v3
uses: actions/setup-node@v4
with:
node-version: '16'
node-version: '20'
cache: 'npm'
- name: Install dependencies
run: npm ci
- name: Install wp-env
run: npm install -g @wordpress/env
- name: Setup WordPress Multisite
run: |
chmod +x bin/setup-test-env.sh
bash bin/setup-test-env.sh multisite
- name: Install Cypress
run: npm install cypress
- name: Run Cypress tests
run: npm run test:multisite:headless

View File

@@ -35,6 +35,8 @@ wp-plugin-starter-template/
├── cypress.config.js # Cypress configuration
├── .ai-workflows/ # AI workflow documentation
├── .wiki/ # Wiki documentation
│ ├── Testing-Framework.md # Testing framework documentation
│ └── Multisite-Development.md # Multisite development guide
└── wp-plugin-starter-template.php # Main plugin file
```
@@ -129,3 +131,5 @@ The plugin includes a comprehensive testing framework:
## Conclusion
This architecture provides a solid foundation for WordPress plugin development, following best practices and modern coding standards. It's designed to be maintainable, extensible, and easy to understand.
For more details on using the testing framework, see [Testing Framework](Testing-Framework.md). For multisite development guidelines, refer to [Multisite Development](Multisite-Development.md).

View File

@@ -86,6 +86,7 @@ This template includes configuration for WordPress Environment (wp-env) to make
```
3. For testing in different WordPress environments:
```bash
# For single site testing
npm run setup:single

View File

@@ -1,7 +1,7 @@
#!/bin/bash
# Make the bin directory executable
chmod +x bin
# Make this script executable
chmod +x "$0"
# Check if wp-env is installed
if ! command -v wp-env &> /dev/null; then
@@ -19,40 +19,73 @@ ENV_TYPE=$1
if [ "$ENV_TYPE" == "single" ]; then
echo "Setting up single site environment..."
# Start the environment
wp-env start
# Wait for WordPress to be ready
sleep 5
# Wait for WordPress to be ready with a timeout
MAX_ATTEMPTS=30
ATTEMPT=0
echo "Waiting for WordPress to be ready..."
until wp-env run cli wp core is-installed || [ $ATTEMPT -ge $MAX_ATTEMPTS ]; do
ATTEMPT=$((ATTEMPT+1))
echo "Attempt $ATTEMPT/$MAX_ATTEMPTS..."
sleep 2
done
if [ $ATTEMPT -ge $MAX_ATTEMPTS ]; then
echo "Timed out waiting for WordPress to be ready."
exit 1
fi
# Activate our plugin
wp-env run cli wp plugin activate wp-plugin-starter-template-for-ai-coding
if ! wp-env run cli wp plugin activate wp-plugin-starter-template-for-ai-coding; then
echo "Failed to activate plugin. Exiting."
exit 1
fi
echo "WordPress Single Site environment is ready!"
echo "Site: http://localhost:8888"
echo "Admin login: admin / password"
elif [ "$ENV_TYPE" == "multisite" ]; then
echo "Setting up multisite environment..."
# Start the environment with multisite configuration
wp-env start --config=.wp-env.multisite.json
# Wait for WordPress to be ready
sleep 5
# Wait for WordPress to be ready with a timeout
MAX_ATTEMPTS=30
ATTEMPT=0
echo "Waiting for WordPress to be ready..."
until wp-env run cli wp core is-installed || [ $ATTEMPT -ge $MAX_ATTEMPTS ]; do
ATTEMPT=$((ATTEMPT+1))
echo "Attempt $ATTEMPT/$MAX_ATTEMPTS..."
sleep 2
done
if [ $ATTEMPT -ge $MAX_ATTEMPTS ]; then
echo "Timed out waiting for WordPress to be ready."
exit 1
fi
# Create a test site
wp-env run cli wp site create --slug=testsite --title="Test Site" --email=admin@example.com
if ! wp-env run cli wp site create --slug=testsite --title="Test Site" --email=admin@example.com; then
echo "Failed to create test site. Exiting."
exit 1
fi
# Network activate our plugin
wp-env run cli wp plugin activate wp-plugin-starter-template-for-ai-coding --network
if ! wp-env run cli wp plugin activate wp-plugin-starter-template-for-ai-coding --network; then
echo "Failed to activate plugin. Exiting."
exit 1
fi
echo "WordPress Multisite environment is ready!"
echo "Main site: http://localhost:8888"
echo "Test site: http://localhost:8888/testsite"
echo "Admin login: admin / password"
else
echo "Invalid environment type. Use 'single' or 'multisite'."
exit 1

View File

@@ -2,42 +2,46 @@ describe('WordPress Multisite Tests', () => {
it('Can access the main site', () => {
cy.visit('/');
cy.get('body').should('exist');
cy.get('h1').should('exist');
cy.title().should('include', 'WordPress');
});
it('Can access the test subsite', () => {
cy.visit('/testsite');
cy.get('body').should('exist');
cy.get('h1').should('exist');
cy.title().should('include', 'Test Site');
});
it('Can login to the admin area', () => {
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');
cy.loginAsAdmin();
cy.get('#wpadminbar').should('exist');
cy.get('#dashboard-widgets').should('exist');
});
it('Can access network admin', () => {
// Login first
cy.visit('/wp-admin');
cy.get('#user_login').type('admin');
cy.get('#user_pass').type('password');
cy.get('#wp-submit').click();
cy.loginAsAdmin();
// Go to network admin
cy.visit('/wp-admin/network/');
cy.get('body.network-admin').should('exist');
});
it('Plugin is network activated', () => {
// Login first
cy.visit('/wp-admin');
cy.get('#user_login').type('admin');
cy.get('#user_pass').type('password');
cy.get('#wp-submit').click();
cy.loginAsAdmin();
// Check plugins page
cy.visit('/wp-admin/network/plugins.php');
cy.contains('tr', 'WP Plugin Starter Template').should('contain', 'Network Active');
});
it('Network settings page loads correctly', () => {
cy.loginAsAdmin();
// Navigate to the network settings page (if it exists)
cy.visit('/wp-admin/network/settings.php');
// This is a basic check for the network settings page
cy.get('h1').should('contain', 'Network Settings');
});
});

View File

@@ -5,22 +5,26 @@ describe('WordPress Single Site Tests', () => {
});
it('Can login to the admin area', () => {
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');
cy.loginAsAdmin();
cy.get('#wpadminbar').should('exist');
cy.get('#dashboard-widgets').should('exist');
});
it('Plugin is activated', () => {
// Login first
cy.visit('/wp-admin');
cy.get('#user_login').type('admin');
cy.get('#user_pass').type('password');
cy.get('#wp-submit').click();
cy.loginAsAdmin();
// Check plugins page
cy.visit('/wp-admin/plugins.php');
cy.contains('tr', 'WP Plugin Starter Template').should('contain', 'Deactivate');
});
it('Plugin settings page loads correctly', () => {
cy.loginAsAdmin();
// Navigate to the plugin settings page (if it exists)
cy.visit('/wp-admin/options-general.php?page=wp-plugin-starter-template');
// This is a basic check - adjust based on your actual plugin's settings page
cy.get('h1').should('contain', 'WP Plugin Starter Template');
});
});

View File

@@ -0,0 +1,20 @@
// ***********************************************
// This example commands.js shows you how to
// create various custom commands and overwrite
// existing commands.
//
// For more comprehensive examples of custom
// commands please read more here:
// https://on.cypress.io/custom-commands
// ***********************************************
/**
* Custom command to login as admin
*/
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');
});

17
cypress/support/e2e.js Normal file
View File

@@ -0,0 +1,17 @@
// ***********************************************************
// This example support/e2e.js is processed and
// loaded automatically before your test files.
//
// This is a great place to put global configuration and
// behavior that modifies Cypress.
//
// You can change the location of this file or turn off
// automatically serving support files with the
// 'supportFile' configuration option.
//
// You can read more here:
// https://on.cypress.io/configuration
// ***********************************************************
// Import commands.js using ES2015 syntax:
import './commands';

View File

@@ -43,9 +43,6 @@ class Admin {
*
* This method is hooked into 'admin_enqueue_scripts'. It checks if the current
* screen is relevant to the plugin before enqueueing assets.
*
*/
public function enqueue_admin_assets(): void {

View File

@@ -21,4 +21,4 @@ if ( is_multisite() ) {
## Testing
For information on testing your plugin in a multisite environment, see the [Testing Framework](.wiki/Testing-Framework.md) documentation.
For information on testing your plugin in a multisite environment, see the [Testing Framework](../../.wiki/Testing-Framework.md) documentation.

View File

@@ -36,7 +36,7 @@ class Multisite {
*
* @return bool Always returns true.
*/
public function is_multisite_compatible() {
public function isMultisiteCompatible() {
return true;
}
@@ -45,9 +45,9 @@ class Multisite {
*
* @return array An empty array as this is just a placeholder.
*/
public function get_network_sites() {
public function getNetworkSites() {
// This is just a placeholder method.
// In a real implementation, you might use get_sites() or a custom query.
return array();
return function_exists('get_sites') ? get_sites(['public' => 1]) : array();
}
}