#!/bin/bash ################################################################################ # Integration Test Suite for WordPress Cron Manager # Purpose: Validate script functionality and catch regressions ################################################################################ SCRIPT="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)/modules/website/wordpress/wordpress-cron-manager.sh" TEST_COUNT=0 PASSED_COUNT=0 FAILED_COUNT=0 # Color codes RED='\033[0;31m' GREEN='\033[0;32m' YELLOW='\033[1;33m' BLUE='\033[0;34m' NC='\033[0m' test_start() { TEST_COUNT=$((TEST_COUNT + 1)) echo -e "\n${BLUE}[TEST $TEST_COUNT]${NC} $1" } test_pass() { PASSED_COUNT=$((PASSED_COUNT + 1)) echo -e "${GREEN}✓ PASS${NC}: $1" } test_fail() { FAILED_COUNT=$((FAILED_COUNT + 1)) echo -e "${RED}✗ FAIL${NC}: $1" } test_info() { echo -e "${YELLOW}ℹ${NC} $1" } # Test 1: Script exists test_script_exists() { test_start "Script file exists" if [ -f "$SCRIPT" ]; then test_pass "Script found at: $SCRIPT" else test_fail "Script not found: $SCRIPT" fi } # Test 2: Script is executable test_script_executable() { test_start "Script is executable" if [ -x "$SCRIPT" ]; then test_pass "Script has execute permission" else test_fail "Script is not executable" return 1 fi } # Test 3: Bash syntax validation test_bash_syntax() { test_start "Bash syntax validation" if bash -n "$SCRIPT" 2>/dev/null; then test_pass "No syntax errors" else test_fail "Syntax errors detected" bash -n "$SCRIPT" 2>&1 | head -5 return 1 fi } # Test 4: Help flag works test_help_flag() { test_start "Help flag functionality (--help)" output=$($SCRIPT --help 2>&1 | head -5) if echo "$output" | grep -q "Usage"; then test_pass "Help output generated" else test_fail "Help flag not working properly" return 1 fi } # Test 5: Grep for key functions (non-execution test) test_functions_defined() { test_start "Key functions defined in script" local functions="initialize_wp_cache get_wp_search_paths validate_wordpress_site is_empty is_set" local missing=0 for func in $functions; do if grep -q "^$func()" "$SCRIPT"; then test_info "✓ Found function: $func" else test_fail "Missing function: $func" missing=$((missing + 1)) fi done if [ $missing -eq 0 ]; then test_pass "All key functions defined" else test_fail "$missing functions missing" return 1 fi } # Test 6: Check for helper functions test_helper_functions_defined() { test_start "Helper functions defined" local helpers="is_file_valid is_user_valid is_wp_configured is_cron_job_exists has_sufficient_disk_space" local count=0 for helper in $helpers; do if grep -q "^$helper()" "$SCRIPT"; then count=$((count + 1)) fi done if [ $count -ge 3 ]; then test_pass "Helper functions defined ($count/5)" else test_fail "Insufficient helper functions ($count/5)" fi } # Test 7: Error code constants test_error_codes_defined() { test_start "Error code constants defined" if grep -q "ERR_SUCCESS\|ERR_INVALID_USER\|ERR_FILE_NOT_FOUND" "$SCRIPT"; then test_pass "Error code constants found" else test_fail "Error code constants not found" return 1 fi } # Test 8: Report generation functions test_report_functions() { test_start "Report generation functions" local funcs="generate_json_report generate_csv_report report_save" local count=0 for func in $funcs; do if grep -q "^$func()" "$SCRIPT"; then count=$((count + 1)) fi done if [ $count -eq 3 ]; then test_pass "Report functions defined ($count/3)" else test_fail "Report functions incomplete ($count/3)" fi } # Test 9: Rollback functions test_rollback_functions() { test_start "Rollback support functions" local funcs="rollback_init rollback_create_checkpoint rollback_all" local count=0 for func in $funcs; do if grep -q "^$func()" "$SCRIPT"; then count=$((count + 1)) fi done if [ $count -eq 3 ]; then test_pass "Rollback functions defined ($count/3)" else test_fail "Rollback functions incomplete ($count/3)" fi } # Test 10: Configuration support test_config_support() { test_start "Configuration file support" if grep -q "load_config_file\|/etc/wordpress-cron-manager.conf" "$SCRIPT"; then test_pass "Configuration file support implemented" else test_fail "Configuration file support not found" return 1 fi } # Test 11: Progress tracking test_progress_tracking() { test_start "Progress tracking functions" local funcs="show_progress show_progress_bar show_spinner" local count=0 for func in $funcs; do if grep -q "^$func()" "$SCRIPT"; then count=$((count + 1)) fi done if [ $count -ge 2 ]; then test_pass "Progress tracking defined ($count/3)" else test_fail "Progress tracking incomplete ($count/3)" fi } # Test 12: Regex helpers test_regex_helpers() { test_start "Regex pattern helpers" local helpers="grep_wp_config_define grep_disabled_wp_cron grep_in_crontab" local count=0 for helper in $helpers; do if grep -q "^$helper()" "$SCRIPT"; then count=$((count + 1)) fi done if [ $count -eq 3 ]; then test_pass "Regex helpers defined ($count/3)" else test_fail "Regex helpers incomplete ($count/3)" fi } # Test 13: Function registry test_function_registry() { test_start "Function registry metadata" if grep -q "FUNCTION_REGISTRY" "$SCRIPT"; then test_pass "Function registry implemented" else test_fail "Function registry not found" return 1 fi } # Test 14: Line count (sanity check) test_script_size() { test_start "Script size sanity check" local lines=$(wc -l < "$SCRIPT") if [ "$lines" -gt 2000 ]; then test_pass "Script size reasonable: $lines lines" else test_fail "Script size too small: $lines lines (expected >2000)" fi } # Test 15: Git commits test_git_history() { test_start "Optimization commits in git history" local opt_commits=$(cd "$(dirname $SCRIPT)/../../../" && git log --oneline | grep -c "OPTIMIZE\|ADVANCED") if [ $opt_commits -gt 5 ]; then test_pass "Multiple optimization commits found: $opt_commits" else test_fail "Insufficient optimization commits: $opt_commits" fi } # Summary print_summary() { echo "" echo "================================================================================" echo "INTEGRATION TEST SUMMARY" echo "================================================================================" echo "Total Tests: $TEST_COUNT" echo -e "${GREEN}Passed: $PASSED_COUNT${NC}" echo -e "${RED}Failed: $FAILED_COUNT${NC}" if [ $FAILED_COUNT -eq 0 ]; then echo -e "\n${GREEN}✓ ALL TESTS PASSED${NC}" return 0 else echo -e "\n${RED}⚠ SOME TESTS FAILED (but script may still work)${NC}" return 1 fi } # Main execution echo "================================================================================" echo "WordPress Cron Manager - Integration Test Suite" echo "================================================================================" test_script_exists test_script_executable test_bash_syntax test_help_flag test_functions_defined test_helper_functions_defined test_error_codes_defined test_report_functions test_rollback_functions test_config_support test_progress_tracking test_regex_helpers test_function_registry test_script_size test_git_history print_summary