# Staging Environment Restoration Checklist

**Last Updated**: August 27, 2025  
**Purpose**: Complete checklist for restoring staging environment from production and re-establishing testing data

## 🚨 When to Use This Checklist

This checklist should be used when:
- Staging environment has critical WordPress errors
- Tests fail with "WordPress site has critical errors that block testing"
- Database corruption or plugin conflicts on staging
- Need to reset staging to known good state from production

## 📋 Pre-Restoration Steps

### 1. Confirm Issues
```bash
# Test current staging health
DISPLAY=:0 XAUTHORITY=/run/user/1000/.mutter-Xwaylandauth.U8VEB3 node -e "
const { chromium } = require('playwright');
const WordPressErrorDetector = require('./tests/framework/utils/WordPressErrorDetector');
(async () => {
  const browser = await chromium.launch({ headless: false });
  const page = await browser.newPage();
  await page.goto('https://upskill-staging.measurequick.com');
  const detector = new WordPressErrorDetector(page);
  const report = await detector.getErrorReport();
  console.log(JSON.stringify(report, null, 2));
  await browser.close();
})();
"

# Document current error state
curl -s https://upskill-staging.measurequick.com | head -100 > staging-error-$(date +%s).html
```

### 2. Backup Current Staging (if needed)
```bash
# Only if staging has important data that production doesn't
# Usually skip this step as staging should be disposable
```

## 🔄 Restoration Process

### 3. **[MANUAL]** Production Backup to Staging
⚠️ **This step must be performed by user with server access**

1. Access hosting control panel (Cloudways/etc)
2. Create fresh backup of production database
3. Restore production database to staging environment
4. Copy production files to staging (if needed)
5. Update staging WordPress configuration:
   - Site URL: `https://upskill-staging.measurequick.com`
   - Database credentials for staging
   - Any staging-specific configurations

### 4. Verify Restoration Success
```bash
# Test basic WordPress functionality
curl -s -o /dev/null -w "%{http_code}" https://upskill-staging.measurequick.com
# Should return: 200

# Test admin access
curl -s https://upskill-staging.measurequick.com/wp-admin/ | grep -i "wordpress"
# Should find WordPress admin page
```

## 🌱 Post-Restoration Data Seeding

### 5. Re-seed Test Data
```bash
# Comprehensive event seeding (creates test events, users, data)
bin/seed-comprehensive-events.sh

# Wait for seeding to complete (usually 2-3 minutes)
echo "Seeding completed at: $(date)"
```

### 6. Verify Test Users Exist
```bash
# Check test user accounts
UPSKILL_STAGING_URL="https://upskill-staging.measurequick.com" wp-cli.phar --url=$UPSKILL_STAGING_URL --ssh=root@upskill-staging.measurequick.com user list --role=hvac_trainer --fields=ID,user_login,user_email

UPSKILL_STAGING_URL="https://upskill-staging.measurequick.com" wp-cli.phar --url=$UPSKILL_STAGING_URL --ssh=root@upskill-staging.measurequick.com user list --role=hvac_master_trainer --fields=ID,user_login,user_email

# Expected accounts:
# - test_trainer / TestTrainer123!
# - test_master / TestMaster123!
# - JoeMedosch@gmail.com / JoeTrainer2025@
```

### 7. Verify Plugin Status
```bash
# Check HVAC plugin is active
UPSKILL_STAGING_URL="https://upskill-staging.measurequick.com" wp-cli.phar --url=$UPSKILL_STAGING_URL --ssh=root@upskill-staging.measurequick.com plugin list --status=active | grep hvac

# Should show: hvac-community-events
```

### 8. Test WordPress Health
```bash
# Run automated health check
DISPLAY=:0 XAUTHORITY=/run/user/1000/.mutter-Xwaylandauth.U8VEB3 node -e "
const { chromium } = require('playwright');
const WordPressErrorDetector = require('./tests/framework/utils/WordPressErrorDetector');
(async () => {
  console.log('🔍 Testing WordPress Health Post-Restoration...');
  const browser = await chromium.launch({ headless: false });
  const page = await browser.newPage();
  await page.goto('https://upskill-staging.measurequick.com');
  await page.waitForLoadState('networkidle');
  
  const detector = new WordPressErrorDetector(page);
  const report = await detector.getErrorReport();
  
  if (report.hasErrors && report.blockingErrors.length > 0) {
    console.log('❌ RESTORATION FAILED - Critical errors still detected:');
    report.blockingErrors.forEach(error => {
      console.log(\`   - \${error.type}: \${error.message}\`);
    });
    process.exit(1);
  } else {
    console.log('✅ WordPress health check PASSED - Ready for testing');
  }
  
  await browser.close();
})();
"
```

## 🧪 Post-Restoration Testing

### 9. Run Quick Login Test
```bash
# Test basic authentication
DISPLAY=:0 XAUTHORITY=/run/user/1000/.mutter-Xwaylandauth.U8VEB3 node -e "
const { chromium } = require('playwright');
(async () => {
  console.log('🔐 Testing Basic Authentication...');
  const browser = await chromium.launch({ headless: false });
  const page = await browser.newPage();
  
  await page.goto('https://upskill-staging.measurequick.com/training-login/');
  await page.waitForLoadState('networkidle');
  
  await page.fill('#username', 'test_trainer');
  await page.fill('#password', 'TestTrainer123!');
  await page.click('button[type=\"submit\"]');
  
  try {
    await page.waitForURL('**/trainer/dashboard/**', { timeout: 10000 });
    console.log('✅ Authentication test PASSED');
  } catch (e) {
    console.log('❌ Authentication test FAILED:', e.message);
    process.exit(1);
  }
  
  await browser.close();
})();
"
```

### 10. Run Full Test Suite
```bash
# Execute comprehensive test suite
echo "🏁 Starting full E2E test suite..."
DISPLAY=:0 XAUTHORITY=/run/user/1000/.mutter-Xwaylandauth.U8VEB3 node test-master-trainer-e2e.js

# If above passes, run comprehensive validation
DISPLAY=:0 XAUTHORITY=/run/user/1000/.mutter-Xwaylandauth.U8VEB3 node test-comprehensive-validation.js
```

## ✅ Success Criteria

### Restoration is considered successful when:
- [ ] WordPress site loads without fatal errors
- [ ] WordPress admin access works
- [ ] HVAC plugin is active and functional
- [ ] Test user accounts exist and can log in
- [ ] Test data is seeded (events, trainers, etc.)
- [ ] WordPress error detector reports no blocking errors
- [ ] Basic authentication test passes
- [ ] At least 1 comprehensive E2E test passes

## 🚨 Troubleshooting

### Common Issues After Restoration

**Issue: Site still shows errors**
```bash
# Check WordPress debug log
UPSKILL_STAGING_URL="https://upskill-staging.measurequick.com" wp-cli.phar --url=$UPSKILL_STAGING_URL --ssh=root@upskill-staging.measurequick.com eval 'error_log("Test log entry");' && echo "Debug logging working"

# Check for plugin conflicts
UPSKILL_STAGING_URL="https://upskill-staging.measurequick.com" wp-cli.phar --url=$UPSKILL_STAGING_URL --ssh=root@upskill-staging.measurequick.com plugin deactivate --all
UPSKILL_STAGING_URL="https://upskill-staging.measurequick.com" wp-cli.phar --url=$UPSKILL_STAGING_URL --ssh=root@upskill-staging.measurequick.com plugin activate hvac-community-events
```

**Issue: Test users don't exist**
```bash
# Recreate test users manually
UPSKILL_STAGING_URL="https://upskill-staging.measurequick.com" wp-cli.phar --url=$UPSKILL_STAGING_URL --ssh=root@upskill-staging.measurequick.com user create test_trainer test.trainer@example.com --role=hvac_trainer --user_pass=TestTrainer123! --display_name="Test Trainer"

UPSKILL_STAGING_URL="https://upskill-staging.measurequick.com" wp-cli.phar --url=$UPSKILL_STAGING_URL --ssh=root@upskill-staging.measurequick.com user create test_master test.master@example.com --role=hvac_master_trainer --user_pass=TestMaster123! --display_name="Test Master Trainer"
```

**Issue: HVAC plugin not working**
```bash
# Reactivate and flush rewrite rules
UPSKILL_STAGING_URL="https://upskill-staging.measurequick.com" wp-cli.phar --url=$UPSKILL_STAGING_URL --ssh=root@upskill-staging.measurequick.com plugin deactivate hvac-community-events
UPSKILL_STAGING_URL="https://upskill-staging.measurequick.com" wp-cli.phar --url=$UPSKILL_STAGING_URL --ssh=root@upskill-staging.measurequick.com plugin activate hvac-community-events
UPSKILL_STAGING_URL="https://upskill-staging.measurequick.com" wp-cli.phar --url=$UPSKILL_STAGING_URL --ssh=root@upskill-staging.measurequick.com rewrite flush
```

## 📝 Post-Restoration Documentation

### Record Completion
```bash
# Update status documentation
echo "Staging restored from production on: $(date)" >> Status.md
echo "Test data seeded on: $(date)" >> Status.md
echo "WordPress health verified on: $(date)" >> Status.md
```

---

**Next Steps**: Once all checklist items are ✅, proceed with full E2E testing and continue development work.