87 lines
3.5 KiB
PHP
87 lines
3.5 KiB
PHP
<?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 = ['conversations' => [['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 = ['conversations' => [['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";
|