up
This commit is contained in:
+120
-1
@@ -306,6 +306,117 @@ class WebhookTestSuite {
|
||||
|
||||
return "Conversación guardada correctamente";
|
||||
});
|
||||
|
||||
$this->webhookTest("Procesamiento de reacción entrante", function() {
|
||||
$testPhone = '573007777000';
|
||||
// Crear usuario y mensaje referenciado
|
||||
$userId = $this->db->insert('users', ['phone_number' => $testPhone, 'name' => 'Reactor', 'status' => 'active']);
|
||||
$origMsgId = '1001';
|
||||
$this->db->insert('conversations', [
|
||||
'user_id' => $userId,
|
||||
'message_id' => $origMsgId,
|
||||
'direction' => 'incoming',
|
||||
'message_type' => 'text',
|
||||
'content' => 'Original msg',
|
||||
'status' => 'received',
|
||||
'created_at' => date('Y-m-d H:i:s')
|
||||
]);
|
||||
|
||||
$payload = [
|
||||
'object' => 'whatsapp_business_account',
|
||||
'entry' => [
|
||||
[
|
||||
'changes' => [
|
||||
[
|
||||
'field' => 'messages',
|
||||
'value' => [
|
||||
'messages' => [
|
||||
[
|
||||
'from' => $testPhone,
|
||||
'id' => '2001',
|
||||
'timestamp' => time(),
|
||||
'type' => 'reaction',
|
||||
'reaction' => ['message_id' => $origMsgId, 'emoji' => '👍']
|
||||
]
|
||||
]
|
||||
]
|
||||
]
|
||||
]
|
||||
]
|
||||
]
|
||||
];
|
||||
|
||||
$webhook = new WhatsAppWebhook();
|
||||
$result = $webhook->processPayload($payload);
|
||||
|
||||
$conv = $this->db->fetch("SELECT * FROM conversations WHERE message_id = :mid", ['mid' => '2001']);
|
||||
|
||||
// Limpiar
|
||||
$this->db->execute("DELETE FROM conversations WHERE message_id = :mid", ['mid' => '2001']);
|
||||
$this->db->execute("DELETE FROM conversations WHERE message_id = :mid", ['mid' => $origMsgId]);
|
||||
$this->db->execute("DELETE FROM users WHERE id = :id", ['id' => $userId]);
|
||||
|
||||
if (!$conv) throw new Exception("Reacción no se guardó");
|
||||
if (intval($conv['reaction_to_message_id']) !== intval($origMsgId)) throw new Exception("reaction_to_message_id no guardado correctamente");
|
||||
if ($conv['reaction_emoji'] !== '👍') throw new Exception("reaction_emoji incorrecto");
|
||||
|
||||
return "Reacción procesada y guardada correctamente";
|
||||
});
|
||||
|
||||
$this->webhookTest("Procesamiento de reply/context entrante", function() {
|
||||
$testPhone = '573007777111';
|
||||
$userId = $this->db->insert('users', ['phone_number' => $testPhone, 'name' => 'Replier', 'status' => 'active']);
|
||||
$origMsgId = '1002';
|
||||
$this->db->insert('conversations', [
|
||||
'user_id' => $userId,
|
||||
'message_id' => $origMsgId,
|
||||
'direction' => 'incoming',
|
||||
'message_type' => 'text',
|
||||
'content' => 'Original for reply',
|
||||
'status' => 'received',
|
||||
'created_at' => date('Y-m-d H:i:s')
|
||||
]);
|
||||
|
||||
$payload = [
|
||||
'object' => 'whatsapp_business_account',
|
||||
'entry' => [
|
||||
[
|
||||
'changes' => [
|
||||
[
|
||||
'field' => 'messages',
|
||||
'value' => [
|
||||
'messages' => [
|
||||
[
|
||||
'from' => $testPhone,
|
||||
'id' => '2002',
|
||||
'timestamp' => time(),
|
||||
'type' => 'text',
|
||||
'text' => ['body' => 'Reply message'],
|
||||
'context' => ['id' => $origMsgId]
|
||||
]
|
||||
]
|
||||
]
|
||||
]
|
||||
]
|
||||
]
|
||||
]
|
||||
];
|
||||
|
||||
$webhook = new WhatsAppWebhook();
|
||||
$result = $webhook->processPayload($payload);
|
||||
|
||||
$conv = $this->db->fetch("SELECT * FROM conversations WHERE message_id = :mid", ['mid' => '2002']);
|
||||
|
||||
// Limpiar
|
||||
$this->db->execute("DELETE FROM conversations WHERE message_id = :mid", ['mid' => '2002']);
|
||||
$this->db->execute("DELETE FROM conversations WHERE message_id = :mid", ['mid' => $origMsgId]);
|
||||
$this->db->execute("DELETE FROM users WHERE id = :id", ['id' => $userId]);
|
||||
|
||||
if (!$conv) throw new Exception("Reply no se guardó");
|
||||
if ($conv['reply_to_message_id'] != $origMsgId) throw new Exception("reply_to_message_id no guardado correctamente");
|
||||
|
||||
return "Reply/context procesado y guardado correctamente";
|
||||
});
|
||||
}
|
||||
|
||||
private function testWebhookSecurity() {
|
||||
@@ -524,12 +635,20 @@ class WebhookTestSuite {
|
||||
}
|
||||
|
||||
// No ejecutar si es incluido por el runner principal
|
||||
if (basename($_SERVER['PHP_SELF']) === 'webhook_tests.php') {
|
||||
if (basename($_SERVER['PHP_SELF']) === 'webhook_tests.php' || php_sapi_name() === 'cli') {
|
||||
$configPath = __DIR__ . '/../config/config.php';
|
||||
if (file_exists($configPath)) {
|
||||
require_once $configPath;
|
||||
}
|
||||
|
||||
// Asegurar que la clase WhatsAppWebhook esté disponible cuando se ejecutan tests desde CLI
|
||||
$webhookPath = __DIR__ . '/../api/webhook.php';
|
||||
if (file_exists($webhookPath)) {
|
||||
require_once $webhookPath;
|
||||
}
|
||||
|
||||
$suite = new WebhookTestSuite();
|
||||
echo "RUNNING WEBHOOK TESTS\n";
|
||||
$suite->runAllWebhookTests();
|
||||
}
|
||||
?>
|
||||
Binary file not shown.
@@ -0,0 +1,86 @@
|
||||
<?php
|
||||
/**
|
||||
* Tests para WhatsAppService saveOutgoingMessage
|
||||
*/
|
||||
require_once __DIR__ . '/../config/config.php';
|
||||
require_once __DIR__ . '/../services/WhatsAppService.php';
|
||||
|
||||
$db = Database::getInstance();
|
||||
$service = new WhatsAppService();
|
||||
$ref = new ReflectionClass($service);
|
||||
$method = $ref->getMethod('saveOutgoingMessage');
|
||||
$method->setAccessible(true);
|
||||
|
||||
// Test reaction save
|
||||
$phone = '573001234999';
|
||||
$db->execute('DELETE FROM users WHERE phone_number = :p', ['p' => $phone]);
|
||||
$userId = $db->insert('users', ['phone_number' => $phone, 'name' => 'Outgoing Reactor', 'status' => 'active']);
|
||||
|
||||
$data = [
|
||||
'to' => $phone,
|
||||
'type' => 'reaction',
|
||||
'reaction' => ['message_id' => '5001', 'emoji' => '❤️']
|
||||
];
|
||||
$response = ['messages' => [['id' => 'out_msg_1']]];
|
||||
|
||||
$foundUser = $db->fetch('SELECT * FROM users WHERE phone_number = :p', ['p' => $phone]);
|
||||
echo "User found for outgoing reaction? " . ($foundUser ? 'yes' : 'no') . "\n";
|
||||
|
||||
$method->invokeArgs($service, [$data, $response]);
|
||||
$conv = $db->fetch('SELECT * FROM conversations WHERE message_id = :mid', ['mid' => 'out_msg_1']);
|
||||
if ($conv) {
|
||||
echo "Outgoing reaction saved: id={$conv['id']} reaction_to={$conv['reaction_to_message_id']} emoji={$conv['reaction_emoji']}\n";
|
||||
} else {
|
||||
echo "Outgoing reaction NOT saved\n";
|
||||
$recent = $db->fetchAll('SELECT id,message_id,message_type,direction,content,created_at FROM conversations WHERE user_id = :uid ORDER BY id DESC LIMIT 5', ['uid' => $userId]);
|
||||
echo "Recent rows for user:\n";
|
||||
foreach ($recent as $r) echo json_encode($r) . "\n";
|
||||
|
||||
echo "Trying direct DB insert to see if constraints block it...\n";
|
||||
$msg = [
|
||||
'user_id' => $userId,
|
||||
'message_id' => 'direct_out_msg_1',
|
||||
'direction' => 'outgoing',
|
||||
'message_type' => 'reaction',
|
||||
'content' => '❤️',
|
||||
'status' => 'sent',
|
||||
'created_at' => date('Y-m-d H:i:s')
|
||||
];
|
||||
try {
|
||||
$res = $db->insert('conversations', $msg);
|
||||
echo "Direct insert result: " . ($res ? 'ok id=' . $res : 'failed') . "\n";
|
||||
$r2 = $db->fetch('SELECT * FROM conversations WHERE message_id = :mid', ['mid' => 'direct_out_msg_1']);
|
||||
echo "Fetched direct row: " . json_encode($r2) . "\n";
|
||||
$db->execute('DELETE FROM conversations WHERE message_id = :mid', ['mid' => 'direct_out_msg_1']);
|
||||
} catch (Exception $e) {
|
||||
echo "Direct insert threw: " . $e->getMessage() . "\n";
|
||||
}
|
||||
}
|
||||
// cleanup
|
||||
$db->execute('DELETE FROM conversations WHERE message_id = :mid', ['mid' => 'out_msg_1']);
|
||||
$db->execute('DELETE FROM users WHERE id = :id', ['id' => $userId]);
|
||||
|
||||
// Test text reply save
|
||||
$phone2 = '573001234998';
|
||||
$db->execute('DELETE FROM users WHERE phone_number = :p', ['p' => $phone2]);
|
||||
$userId2 = $db->insert('users', ['phone_number' => $phone2, 'name' => 'Outgoing Replier', 'status' => 'active']);
|
||||
|
||||
$data2 = [
|
||||
'to' => $phone2,
|
||||
'type' => 'text',
|
||||
'context' => ['message_id' => '5002'],
|
||||
'text' => ['body' => 'Gracias']
|
||||
];
|
||||
$response2 = ['messages' => [['id' => 'out_msg_2']]];
|
||||
$method->invokeArgs($service, [$data2, $response2]);
|
||||
$conv2 = $db->fetch('SELECT * FROM conversations WHERE message_id = :mid', ['mid' => 'out_msg_2']);
|
||||
if ($conv2) {
|
||||
echo "Outgoing reply saved: id={$conv2['id']} reply_to={$conv2['reply_to_message_id']}\n";
|
||||
} else {
|
||||
echo "Outgoing reply NOT saved\n";
|
||||
}
|
||||
// cleanup
|
||||
$db->execute('DELETE FROM conversations WHERE message_id = :mid', ['mid' => 'out_msg_2']);
|
||||
$db->execute('DELETE FROM users WHERE id = :id', ['id' => $userId2]);
|
||||
|
||||
echo "Done tests\n";
|
||||
Binary file not shown.
Reference in New Issue
Block a user