From 051bc763f876c096afa92c76a280f47113aee827 Mon Sep 17 00:00:00 2001 From: marcusquinn <6428977+marcusquinn@users.noreply.github.com> Date: Mon, 21 Apr 2025 21:23:23 +0100 Subject: [PATCH] Fix code quality issues and improve test framework --- .github/workflows/wordpress-tests.yml | 40 +++++++------- .wiki/Architecture-Overview.md | 4 ++ README.md | 1 + bin/setup-test-env.sh | 73 +++++++++++++++++++------- cypress/e2e/multisite.cy.js | 38 ++++++++------ cypress/e2e/single-site.cy.js | 26 +++++---- cypress/support/commands.js | 20 +++++++ cypress/support/e2e.js | 17 ++++++ includes/Admin/class-admin.php | 3 -- includes/Multisite/README.md | 2 +- includes/Multisite/class-multisite.php | 6 +-- 11 files changed, 155 insertions(+), 75 deletions(-) create mode 100644 cypress/support/commands.js create mode 100644 cypress/support/e2e.js diff --git a/.github/workflows/wordpress-tests.yml b/.github/workflows/wordpress-tests.yml index cde5f28..ab84450 100644 --- a/.github/workflows/wordpress-tests.yml +++ b/.github/workflows/wordpress-tests.yml @@ -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 diff --git a/.wiki/Architecture-Overview.md b/.wiki/Architecture-Overview.md index 6bc8df4..86aba8d 100644 --- a/.wiki/Architecture-Overview.md +++ b/.wiki/Architecture-Overview.md @@ -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). diff --git a/README.md b/README.md index 19d9416..dfd4528 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/bin/setup-test-env.sh b/bin/setup-test-env.sh index 14150d5..904f7e7 100644 --- a/bin/setup-test-env.sh +++ b/bin/setup-test-env.sh @@ -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 diff --git a/cypress/e2e/multisite.cy.js b/cypress/e2e/multisite.cy.js index ee68f29..66f8117 100644 --- a/cypress/e2e/multisite.cy.js +++ b/cypress/e2e/multisite.cy.js @@ -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'); + }); }); diff --git a/cypress/e2e/single-site.cy.js b/cypress/e2e/single-site.cy.js index f016ed9..603593f 100644 --- a/cypress/e2e/single-site.cy.js +++ b/cypress/e2e/single-site.cy.js @@ -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'); + }); }); diff --git a/cypress/support/commands.js b/cypress/support/commands.js new file mode 100644 index 0000000..8aef097 --- /dev/null +++ b/cypress/support/commands.js @@ -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'); +}); diff --git a/cypress/support/e2e.js b/cypress/support/e2e.js new file mode 100644 index 0000000..b026563 --- /dev/null +++ b/cypress/support/e2e.js @@ -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'; diff --git a/includes/Admin/class-admin.php b/includes/Admin/class-admin.php index 0fca2f5..4a12d90 100644 --- a/includes/Admin/class-admin.php +++ b/includes/Admin/class-admin.php @@ -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 { diff --git a/includes/Multisite/README.md b/includes/Multisite/README.md index 6f7e359..e4f8a66 100644 --- a/includes/Multisite/README.md +++ b/includes/Multisite/README.md @@ -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. diff --git a/includes/Multisite/class-multisite.php b/includes/Multisite/class-multisite.php index f45730e..839533d 100644 --- a/includes/Multisite/class-multisite.php +++ b/includes/Multisite/class-multisite.php @@ -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(); } }