This commit is contained in:
lizandrogd
2026-01-21 02:22:29 -05:00
parent 3fffb0d023
commit b1961f0116
35 changed files with 1006 additions and 122 deletions
+120 -1
View File
@@ -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();
}
?>