Files
nabeh/backend/public/test_woocommerce_limits.php
2026-05-22 23:55:19 +03:00

142 lines
6.4 KiB
PHP

<?php
/**
* WooCommerce Integration & SaaS Limit Verification Simulation
* Run this on the server: php backend/public/test_woocommerce_limits.php
*/
require_once dirname(__DIR__) . '/app/bootstrap.php';
use App\Core\Database;
use App\Models\CompanySubscription;
use App\Models\CompanySubscriptionUsage;
use App\Models\WooCommerceStore;
use App\Services\WooCommerceService;
echo "=== Starting SaaS & WooCommerce Integration Tests ===\n\n";
$companyId = 999; // Mock company for testing limits
$phone = "966555555555"; // Mock Saudi number
// Ensure clean database state for this mock company
Database::execute("DELETE FROM company_subscriptions WHERE company_id = ?", [$companyId]);
Database::execute("DELETE FROM company_subscription_usage WHERE company_id = ?", [$companyId]);
Database::execute("DELETE FROM woocommerce_stores WHERE company_id = ?", [$companyId]);
Database::execute("DELETE FROM companies WHERE id = ?", [$companyId]);
// 1. Create Mock Company
echo "1. Creating mock company...\n";
Database::execute("INSERT INTO companies (id, name, created_at) VALUES (?, 'Mock Merchant Co', NOW())", [$companyId]);
// 2. Add WooCommerce Mock Connection details
echo "2. Saving mock WooCommerce store credentials...\n";
$mockStoreUrl = "https://mock-woo-store.com";
$mockConsumerKey = "ck_1234567890123456789012345678901234567890";
$mockConsumerSecret = "cs_1234567890123456789012345678901234567890";
$mockWebhookSecret = "webhook_secret_xyz";
$storeId = WooCommerceStore::saveStore(
$companyId,
$mockStoreUrl,
$mockConsumerKey,
$mockConsumerSecret,
$mockWebhookSecret
);
echo " WooCommerce Store saved. ID: $storeId\n";
// Verify decryption
$store = WooCommerceStore::findByCompany($companyId);
$decrypted = WooCommerceStore::getDecryptedCredentials($store);
if ($decrypted['consumer_key'] === $mockConsumerKey && $decrypted['consumer_secret'] === $mockConsumerSecret) {
echo " ✅ Credentials decryption verified successfully.\n";
} else {
echo " ❌ Credentials decryption FAILED.\n";
}
// 3. Test Phone Trailing Digit Matching
echo "3. Testing phone trailing digit comparison helper...\n";
$testCases = [
['+966555555555', '0555555555', true],
['00966555555555', '966555555555', true],
['0555555555', '555555555', true],
['962799999999', '0799999999', true],
['12345', '12345', true],
['12345', '54321', false],
];
foreach ($testCases as $case) {
$res = WooCommerceService::comparePhones($case[0], $case[1]);
$status = ($res === $case[2]) ? "PASS" : "FAIL";
echo " Compare '{$case[0]}' with '{$case[1]}': " . ($res ? "MATCH" : "NO MATCH") . " ($status)\n";
}
// 4. Test Subscription Limits and Dynamic Reset (تصفير ديناميكي)
echo "\n4. Testing Subscription Limits and Usage...\n";
// Insert a Custom Plan for testing
// ID 999 Plan: Max 3 requests, 1 voice note, 1 ocr
Database::execute("INSERT INTO subscription_plans (id, name, price, max_sessions, max_requests, max_voice_requests, max_ocr_requests, features) VALUES (999, 'Test Plan', 0.00, 1, 3, 1, 1, '{}') ON DUPLICATE KEY UPDATE max_requests=3, max_voice_requests=1, max_ocr_requests=1");
// Create active subscription starting 5 days ago and ending 25 days from now
$startsAt = date('Y-m-d H:i:s', strtotime('-5 days'));
$endsAt = date('Y-m-d H:i:s', strtotime('+25 days'));
Database::execute(
"INSERT INTO company_subscriptions (company_id, plan_id, status, starts_at, ends_at) VALUES (?, 999, 'active', ?, ?)",
[$companyId, $startsAt, $endsAt]
);
$activeSub = CompanySubscription::findActiveByCompany($companyId);
echo " Active Subscription found: Plan ID {$activeSub['plan_id']}\n";
echo " Billing cycle starts: {$activeSub['starts_at']}, ends: {$activeSub['ends_at']}\n";
// Test dynamic usage record initialization
$usage = CompanySubscriptionUsage::getOrCreateCurrentUsage($companyId, $activeSub);
echo " Initialized usage: Requests={$usage['request_count']}, Voice={$usage['voice_count']}, OCR={$usage['ocr_count']}\n";
// Check limits
echo " Checking initial limits:\n";
echo " - Has request limit? " . (CompanySubscriptionUsage::hasRemainingLimit($companyId, 'request') ? "YES" : "NO") . "\n";
echo " - Has voice limit? " . (CompanySubscriptionUsage::hasRemainingLimit($companyId, 'voice') ? "YES" : "NO") . "\n";
echo " - Has OCR limit? " . (CompanySubscriptionUsage::hasRemainingLimit($companyId, 'ocr') ? "YES" : "NO") . "\n";
// Increment and check limits
echo " Incrementing request and voice usage...\n";
CompanySubscriptionUsage::incrementUsage($companyId, 'request');
CompanySubscriptionUsage::incrementUsage($companyId, 'voice');
$usage = CompanySubscriptionUsage::getOrCreateCurrentUsage($companyId, $activeSub);
echo " Current usage: Requests={$usage['request_count']}, Voice={$usage['voice_count']}, OCR={$usage['ocr_count']}\n";
echo " - Has remaining voice limit? " . (CompanySubscriptionUsage::hasRemainingLimit($companyId, 'voice') ? "YES" : "NO") . "\n";
// Exceed request limits
echo " Exceeding request limits...\n";
CompanySubscriptionUsage::incrementUsage($companyId, 'request'); // Request 2
CompanySubscriptionUsage::incrementUsage($companyId, 'request'); // Request 3
$usage = CompanySubscriptionUsage::getOrCreateCurrentUsage($companyId, $activeSub);
echo " Current usage: Requests={$usage['request_count']}\n";
echo " - Has remaining request limit? " . (CompanySubscriptionUsage::hasRemainingLimit($companyId, 'request') ? "YES" : "NO") . "\n";
// 5. Test Webhook Signature verification logic
echo "\n5. Testing Webhook Signature verification...\n";
$mockPayload = json_encode(['id' => 1025, 'status' => 'completed', 'total' => '150.00']);
$signature = base64_encode(hash_hmac('sha256', $mockPayload, $mockWebhookSecret, true));
echo " Calculated Signature: $signature\n";
// Verify matching logic
$calculatedSig = base64_encode(hash_hmac('sha256', $mockPayload, $mockWebhookSecret, true));
if (hash_equals($calculatedSig, $signature)) {
echo " ✅ Signature verification logic PASSED.\n";
} else {
echo " ❌ Signature verification logic FAILED.\n";
}
// Clean up mock company after tests
Database::execute("DELETE FROM company_subscriptions WHERE company_id = ?", [$companyId]);
Database::execute("DELETE FROM company_subscription_usage WHERE company_id = ?", [$companyId]);
Database::execute("DELETE FROM woocommerce_stores WHERE company_id = ?", [$companyId]);
Database::execute("DELETE FROM companies WHERE id = ?", [$companyId]);
echo "\n=== Tests Completed successfully! ===\n";