#!/usr/bin/env php
<?php
// Verifies parseCpanelZone() defaults a priority-less MX to 10 instead of dropping it.
require_once(__DIR__ . '/../web-files/libs/CpanelBackupImporter.php');

$tmp = sys_get_temp_dir() . '/whp-mxtest-' . getmypid();
@mkdir($tmp . '/dnszones', 0777, true);
file_put_contents($tmp . '/dnszones/example.com.db', implode("\n", [
    '$TTL 3600',
    '$ORIGIN example.com.',
    '@ IN MX 10 mail1.example.com.',
    '@ IN MX mail2.example.com.',   // <-- malformed: no priority
    '',
]));

$ref = new ReflectionClass('CpanelBackupImporter');
$imp = $ref->newInstanceWithoutConstructor();
$bp = $ref->getProperty('backupRoot'); $bp->setAccessible(true); $bp->setValue($imp, $tmp);
$m = $ref->getMethod('parseCpanelZone'); $m->setAccessible(true);
$records = $m->invoke($imp, 'example.com');

$mx = array_values(array_filter($records, fn($r) => $r['type'] === 'MX'));
$ok = true;
if (count($mx) !== 2) { $ok = false; fprintf(STDERR, "FAIL: expected 2 MX, got %d\n", count($mx)); }
$priorities = array_map(fn($r) => $r['priority'], $mx);
if (!in_array(10, $priorities, true) || in_array(null, $priorities, true)) {
    $ok = false; fprintf(STDERR, "FAIL: priorities=%s (expected both set, missing->10)\n", json_encode($priorities));
}
@unlink($tmp . '/dnszones/example.com.db'); @rmdir($tmp . '/dnszones'); @rmdir($tmp);
echo $ok ? "cpanel-mx-default OK\n" : "cpanel-mx-default FAIL\n";
exit($ok ? 0 : 1);
