102 lines
3.7 KiB
PHP
102 lines
3.7 KiB
PHP
<?php
|
|
|
|
// Secure token check to prevent unauthorized execution on production
|
|
if (($_GET['token'] ?? '') !== 'nabeh_test_token_1298') {
|
|
http_response_code(403);
|
|
die('Unauthorized access');
|
|
}
|
|
|
|
require_once dirname(__DIR__) . '/app/bootstrap.php';
|
|
|
|
use App\Core\Database;
|
|
use App\Models\ConversationState;
|
|
use App\Core\Flows\ConversationFlowEngine;
|
|
|
|
header('Content-Type: text/plain; charset=utf-8');
|
|
|
|
echo "=== Starting Conversation Flow Engine Mock Test ===\n";
|
|
|
|
// 1. Ensure table exists
|
|
echo "Ensuring conversation_states table exists...\n";
|
|
ConversationState::ensureTableExists();
|
|
echo "Table ensured.\n";
|
|
|
|
// 2. Fetch a company and session to mock with
|
|
$company = Database::selectOne("SELECT * FROM companies LIMIT 1");
|
|
if (!$company) {
|
|
echo "No company found in database. Creating a mock company...\n";
|
|
$companyId = Database::insert("INSERT INTO companies (name) VALUES (?)", ["Mock Test Company"]);
|
|
$company = Database::selectOne("SELECT * FROM companies WHERE id = ?", [$companyId]);
|
|
}
|
|
echo "Using Company ID: {$company['id']} ({$company['name']})\n";
|
|
|
|
$session = Database::selectOne("SELECT * FROM whatsapp_sessions WHERE company_id = ? LIMIT 1", [$company['id']]);
|
|
if (!$session) {
|
|
echo "No WhatsApp session found for company. Creating a mock session...\n";
|
|
$sessionId = Database::insert(
|
|
"INSERT INTO whatsapp_sessions (company_id, name, session_key, status) VALUES (?, ?, ?, ?)",
|
|
[$company['id'], "Test Session", "mock_test_session_" . rand(100, 999), "connected"]
|
|
);
|
|
$session = Database::selectOne("SELECT * FROM whatsapp_sessions WHERE id = ?", [$sessionId]);
|
|
}
|
|
echo "Using WhatsApp Session Key: {$session['session_key']}\n";
|
|
|
|
// Clean existing states for test phone
|
|
$testPhone = "+962799999999";
|
|
$hash = \App\Core\Security::blindIndex($testPhone);
|
|
Database::execute("DELETE FROM conversation_states WHERE company_id = ? AND contact_phone_hash = ?", [$company['id'], $hash]);
|
|
echo "Cleaned any old state for test phone: {$testPhone}\n";
|
|
|
|
// Helper to simulate incoming message
|
|
function simulateIncoming(array $session, string $phone, string $body) {
|
|
echo "\n--- SIMULATING INCOMING: '$body' from $phone ---\n";
|
|
|
|
$msgData = [
|
|
'phone' => $phone,
|
|
'body' => $body,
|
|
'id' => 'msg_' . uniqid()
|
|
];
|
|
|
|
// Temporarily point gateway to a local dummy or mock so curl doesn't fail
|
|
$oldGateway = getenv('WHATSAPP_GATEWAY_URL');
|
|
putenv('WHATSAPP_GATEWAY_URL=http://localhost:9999'); // Invalid/mock port to prevent actual gateway call or we can let it fail gracefully
|
|
|
|
$handled = ConversationFlowEngine::processMessage($session, $msgData);
|
|
echo "Handled by Engine: " . ($handled ? "YES" : "NO") . "\n";
|
|
|
|
// Restore gateway
|
|
if ($oldGateway) {
|
|
putenv("WHATSAPP_GATEWAY_URL={$oldGateway}");
|
|
}
|
|
|
|
// Print current state in DB
|
|
$state = ConversationState::findActive($session['company_id'], $phone);
|
|
if ($state) {
|
|
echo "Current DB State: Step = '{$state['current_step']}', Context = '{$state['context_data']}'\n";
|
|
} else {
|
|
echo "Current DB State: [No Active Flow / Finished]\n";
|
|
}
|
|
}
|
|
|
|
// 3. Run test cases
|
|
// Case A: Message not triggering anything
|
|
simulateIncoming($session, $testPhone, "مرحبا");
|
|
|
|
// Case B: Trigger keyword "اختبار"
|
|
simulateIncoming($session, $testPhone, "اختبار");
|
|
|
|
// Case C: Provide name "احمد"
|
|
simulateIncoming($session, $testPhone, "احمد");
|
|
|
|
// Case D: Provide invalid rating "10"
|
|
simulateIncoming($session, $testPhone, "10");
|
|
|
|
// Case E: Provide valid rating "5"
|
|
simulateIncoming($session, $testPhone, "5");
|
|
|
|
// Case F: Start again and test cancellation
|
|
simulateIncoming($session, $testPhone, "اختبار");
|
|
simulateIncoming($session, $testPhone, "إلغاء");
|
|
|
|
echo "\n=== Mock Test Complete ===\n";
|