upskill-event-manager/wordpress-dev/bin/run-tests.sh
bengizmo fd79b22c9b feat: Successfully create test user on staging and update test setup
- Troubleshooted and fixed issues with the plugin deployment script (`deploy-plugin.sh`) to ensure all necessary plugin files, including the main plugin file, are correctly transferred to the staging environment.
- Corrected a role name mismatch in the test user creation script (`setup-staging-test-users.sh`) to successfully create a test user with the `hvac_trainer` role on staging.
- Updated the E2E test runner script (`run-tests.sh`) to replace deprecated Docker commands with SSH commands targeting the staging environment and explicitly pass the staging URL to Playwright.
- Increased the global timeout and enabled retries in the Playwright configuration (`playwright.config.ts`) to aid in debugging test failures on the staging environment.
- Updated documentation files (`docs/mvp-integration-testing-plan.md`, `wordpress-dev/README.md`, and `wordpress-dev/MIGRATION_GUIDE.md`) to include instructions on setting up the test user for the staging environment and corrected section numbering in the testing plan.
2025-04-24 04:15:43 -03:00

158 lines
5.2 KiB
Bash
Executable file

#!/bin/bash
# Load environment variables
if [ ! -f ./.env ]; then
echo "Error: .env file not found!"
exit 1
fi
echo "Sourcing .env file from: $(pwd)/.env"
source ./.env
# Change to the script's directory parent (wordpress-dev)
SCRIPT_DIR=$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd ) # Corrected: &> and &&
cd "$SCRIPT_DIR/.." || exit 1
echo "Changed working directory to: $(pwd)"
# Colors for output
GREEN='\033[0;32m'
RED='\033[0;31m'
YELLOW='\033[1;33m'
NC='\033[0m'
# Default values
RUN_UNIT=false
RUN_INTEGRATION=false
RUN_E2E=false
DEBUG=false
TEST_SUITE=""
PHPUNIT_FILTER=""
# Parse arguments
while [[ $# -gt 0 ]]; do
case $1 in
--unit)
RUN_UNIT=true
shift
;;
--integration)
RUN_INTEGRATION=true
shift
;;
--e2e)
RUN_E2E=true
shift
;;
--login)
RUN_E2E=true
TEST_SUITE="login"
shift
;;
--debug)
DEBUG=true
shift
;;
--filter)
if [[ -z "$2" || "$2" == --* ]]; then
echo "Error: --filter option requires a value."
exit 1
fi
PHPUNIT_FILTER="$2"
shift 2 # Consume both --filter and its value
;;
*)
echo "Unknown option: $1"
exit 1
;;
esac
done
# If no test type specified, run all
if ! $RUN_UNIT && ! $RUN_INTEGRATION && ! $RUN_E2E; then # Corrected: &&
RUN_UNIT=true
RUN_INTEGRATION=true
RUN_E2E=true
fi
# Function to run tests
run_tests() {
local test_type=$1
local test_command=$2
echo -e "${YELLOW}Running $test_type tests...${NC}"
if $DEBUG; then
echo "Test command: $test_command"
fi
if eval $test_command; then
echo -e "${GREEN}$test_type tests passed${NC}"
return 0
else
echo -e "${RED}$test_type tests failed. Exiting.${NC}"
exit 1 # Exit script immediately on failure
fi
}
# Create results directory
mkdir -p ../test-results
# Composer dependencies are managed on the host and mounted via volume.
# Run unit tests using relative path via docker-compose exec
if $RUN_UNIT; then
# Base command
UNIT_CMD="vendor/bin/phpunit --verbose --testsuite unit --log-junit ../test-results/unit.xml"
# Add filter if provided, ensuring proper quoting for the value
if [ -n "$PHPUNIT_FILTER" ]; then
# Escape potential special characters within the filter value for sh -c
FILTER_ESCAPED=$(printf '%s\n' "$PHPUNIT_FILTER" | sed "s/'/'\\\\''/g")
UNIT_CMD="$UNIT_CMD --filter '$FILTER_ESCAPED'"
fi
# Add command to capture exit status
UNIT_CMD="$UNIT_CMD; exit \$?"
# Execute the command via sh -c, passing the constructed command in single quotes
run_tests "Unit" "docker-compose exec -T wordpress sh -c '$UNIT_CMD'"
fi
# Run integration tests using relative path via docker-compose exec
if $RUN_INTEGRATION; then
# Base command
INTEGRATION_CMD="vendor/bin/phpunit --testsuite integration --log-junit ../test-results/integration.xml"
# Add filter if provided
if [ -n "$PHPUNIT_FILTER" ]; then
FILTER_ESCAPED=$(printf '%s\n' "$PHPUNIT_FILTER" | sed "s/'/'\\\\''/g")
INTEGRATION_CMD="$INTEGRATION_CMD --filter '$FILTER_ESCAPED'"
fi
# Add command to capture exit status
INTEGRATION_CMD="$INTEGRATION_CMD; exit \$?"
# Execute the command via sh -c
run_tests "Integration" "docker-compose exec -T wordpress sh -c '$INTEGRATION_CMD'"
fi
# Run E2E tests
if $RUN_E2E; then
# Ensure plugin activation hooks run and permalinks are fresh for E2E context
echo -e "${YELLOW}Deactivating/Reactivating plugin to ensure hooks fire...${NC}"
sshpass -p "$UPSKILL_STAGING_PASS" ssh -o StrictHostKeyChecking=no "$UPSKILL_STAGING_SSH_USER@$UPSKILL_STAGING_IP" "cd $UPSKILL_STAGING_PATH && wp plugin deactivate hvac-community-events --allow-root" || echo -e "${YELLOW}Note: Plugin already inactive or not found (continuing).${NC}" # Allow failure if already inactive
sshpass -p "$UPSKILL_STAGING_PASS" ssh -o StrictHostKeyChecking=no "$UPSKILL_STAGING_SSH_USER@$UPSKILL_STAGING_IP" "cd $UPSKILL_STAGING_PATH && wp plugin activate hvac-community-events --allow-root"
if [ $? -ne 0 ]; then
echo -e "${RED}✗ Failed to activate hvac-community-events plugin. Exiting.${NC}"
exit 1
fi
echo -e "${GREEN}✓ Plugin reactivated.${NC}"
# Flush rewrite rules after activation
echo -e "${YELLOW}Flushing rewrite rules...${NC}"
if ! sshpass -p "$UPSKILL_STAGING_PASS" ssh -o StrictHostKeyChecking=no "$UPSKILL_STAGING_SSH_USER@$UPSKILL_STAGING_IP" "cd $UPSKILL_STAGING_PATH && wp rewrite flush --hard --allow-root"; then
echo -e "${RED}✗ Failed to flush rewrite rules. Exiting.${NC}"
exit 1
fi
echo -e "${GREEN}✓ Rewrite rules flushed.${NC}"
# Now run the tests
if [ -n "$TEST_SUITE" ]; then
run_tests "E2E" "UPSKILL_STAGING_URL=\"$UPSKILL_STAGING_URL\" npx playwright test --config=tests/e2e/playwright.config.ts --grep @$TEST_SUITE"
else
run_tests "E2E" "UPSKILL_STAGING_URL=\"$UPSKILL_STAGING_URL\" npx playwright test --config=tests/e2e/playwright.config.ts"
fi
fi