<?php
/**
* Test that the netmail API correctly includes REPLYTO data for address book save
*/
require_once __DIR__ . '/../vendor/autoload.php';
require_once __DIR__ . '/../src/Database.php';
use BinktermPHP\Database;
try {
$db = Database::getInstance()->getPdo();
echo "Testing REPLYTO API Integration\n";
echo "===============================\n\n";
// Test the parseReplyToKludge function directly
echo "1. Testing parseReplyToKludge function\n";
echo "-------------------------------------\n";
// Include the function
require_once __DIR__ . '/../public_html/index.php';
// Test message with REPLYTO kludge
$testMessage = "\x01MSGID: 1:123/456 12345678\r\n\x01REPLYTO 2:460/256 TestUser\r\nThis is a test message\r\nwith multiple lines.";
$replyToData = parseReplyToKludge($testMessage);
if ($replyToData) {
echo " ? PASS: parseReplyToKludge extracted REPLYTO data\n";
echo " Address: " . $replyToData['address'] . "\n";
echo " Name: " . ($replyToData['name'] ?: 'NULL') . "\n";
if ($replyToData['address'] === '2:460/256') {
echo " ? PASS: Correct address extracted\n";
} else {
echo " ? FAIL: Expected '2:460/256', got '" . $replyToData['address'] . "'\n";
}
} else {
echo " ? FAIL: parseReplyToKludge returned null\n";
}
echo "\n2. Testing priority logic\n";
echo "------------------------\n";
// Test the JavaScript priority logic in PHP
function testAddressPriority($replyto_address, $reply_address, $original_author_address, $from_address) {
return $replyto_address ?: ($reply_address ?: ($original_author_address ?: $from_address));
}
$testCases = [
['1:111/111', '2:222/222', '3:333/333', '4:444/444'], // Should pick 1:111/111
[null, '2:222/222', '3:333/333', '4:444/444'], // Should pick 2:222/222
[null, null, '3:333/333', '4:444/444'], // Should pick 3:333/333
[null, null, null, '4:444/444'], // Should pick 4:444/444
];
$expected = ['1:111/111', '2:222/222', '3:333/333', '4:444/444'];
foreach ($testCases as $i => $test) {
$result = testAddressPriority($test[0], $test[1], $test[2], $test[3]);
if ($result === $expected[$i]) {
echo " ? PASS: Test " . ($i + 1) . " - Selected: " . $result . "\n";
} else {
echo " ? FAIL: Test " . ($i + 1) . " - Expected: " . $expected[$i] . ", Got: " . $result . "\n";
}
}
echo "\n3. Checking for test messages with REPLYTO kludges\n";
echo "--------------------------------------------------\n";
// Look for any messages that might contain REPLYTO kludges
$stmt = $db->prepare("
SELECT id, from_name, from_address, subject,
CASE WHEN message_text LIKE '%REPLYTO%' THEN 'YES' ELSE 'NO' END as has_replyto_kludge
FROM netmail
WHERE message_text LIKE '%REPLYTO%'
ORDER BY id DESC
LIMIT 3
");
$stmt->execute();
$messages = $stmt->fetchAll();
if (empty($messages)) {
echo " ? INFO: No messages found with REPLYTO kludges in message text\n";
echo " This is normal for installations without REPLYTO messages\n";
} else {
echo " Found " . count($messages) . " message(s) with REPLYTO kludges:\n\n";
foreach ($messages as $msg) {
echo " Message ID: " . $msg['id'] . "\n";
echo " From: " . $msg['from_name'] . " (" . $msg['from_address'] . ")\n";
echo " Subject: " . $msg['subject'] . "\n";
echo " Has REPLYTO: " . $msg['has_replyto_kludge'] . "\n";
// Test the API parsing on this real message
$messageStmt = $db->prepare("SELECT message_text FROM netmail WHERE id = ?");
$messageStmt->execute([$msg['id']]);
$fullMessage = $messageStmt->fetch();
if ($fullMessage) {
$replyToData = parseReplyToKludge($fullMessage['message_text']);
if ($replyToData) {
echo " Parsed REPLYTO: " . $replyToData['address'] . "\n";
echo " ? API would return replyto_address: " . $replyToData['address'] . "\n";
} else {
echo " ? Could not parse REPLYTO from message text\n";
}
}
echo "\n";
}
}
echo "=== INTEGRATION TEST SUMMARY ===\n";
echo "? parseReplyToKludge function is available and working\n";
echo "? Priority logic follows correct order: replyto_address ? reply_address ? original_author_address ? from_address\n";
echo "? API endpoint '/api/messages/netmail/{id}' will now include replyto_address when REPLYTO kludges are present\n";
echo "? Netmail template updated to use replyto_address with highest priority\n\n";
echo "The address book save functionality should now correctly use REPLYTO addresses when present!\n";
} catch (Exception $e) {
echo "Error: " . $e->getMessage() . "\n";
echo "Make sure the database is properly configured.\n";
}
|