手动添加域名
This commit is contained in:
@@ -108,6 +108,29 @@ try {
|
||||
$del = DomainProvisioningService::deleteWithCloudflare($pdo, $domainId);
|
||||
$tests['delete_calls_zone'] = ($del['ok'] ?? false) === true && $mock->deleteCalled === true;
|
||||
$tests['delete_row_gone'] = DomainRepository::findById($pdo, $domainId) === null;
|
||||
|
||||
// 手动模式:不创建 Zone
|
||||
$hostManual = 'cf-manual-' . substr(md5((string) microtime(true)), 0, 8) . '.test';
|
||||
$addManual = DomainProvisioningService::provisionOnAdd($pdo, $hostManual, 'manual');
|
||||
$rowManual = DomainRepository::findById($pdo, (int) ($addManual['id'] ?? 0));
|
||||
$tests['add_manual_cf_status'] = ($addManual['ok'] ?? false) === true
|
||||
&& ($addManual['cf_status'] ?? '') === 'manual'
|
||||
&& ($rowManual['cf_status'] ?? '') === 'manual'
|
||||
&& trim((string) ($rowManual['cf_zone_id'] ?? '')) === '';
|
||||
$checkManual = DomainProvisioningService::checkAndProvision($pdo, (int) ($addManual['id'] ?? 0));
|
||||
$tests['check_manual_skipped'] = ($checkManual['ok'] ?? false) === false;
|
||||
DomainRepository::deleteById($pdo, (int) ($addManual['id'] ?? 0));
|
||||
|
||||
// 批量添加
|
||||
$batchText = "batch-a.test\nbatch-b.test, batch-c.test";
|
||||
$batch = DomainProvisioningService::provisionBatch($pdo, $batchText, 'manual');
|
||||
$tests['batch_add_count'] = ($batch['ok'] ?? false) === true && count($batch['added'] ?? []) === 3;
|
||||
foreach ($batch['added'] ?? [] as $item) {
|
||||
DomainRepository::deleteById($pdo, (int) ($item['id'] ?? 0));
|
||||
}
|
||||
|
||||
$parsed = DomainRepository::parseHostnameBatch("a.test\nb.test\nc.test,d.test");
|
||||
$tests['parse_hostname_batch'] = $parsed === ['a.test', 'b.test', 'c.test', 'd.test'];
|
||||
} catch (Throwable $e) {
|
||||
$tests['exception'] = false;
|
||||
$tests['error'] = $e->getMessage();
|
||||
|
||||
@@ -0,0 +1,90 @@
|
||||
#!/usr/bin/env php
|
||||
<?php
|
||||
/**
|
||||
* 随机域名选取与多配置绑定逻辑验证
|
||||
*
|
||||
* 用法:php tools/verify_domain_random.php
|
||||
*/
|
||||
if (PHP_SAPI !== 'cli') {
|
||||
fwrite(STDERR, "请在 CLI 下运行\n");
|
||||
exit(1);
|
||||
}
|
||||
|
||||
require_once dirname(__DIR__) . '/cong.php';
|
||||
require_once dirname(__DIR__) . '/lib/Cloak/DomainRepository.php';
|
||||
|
||||
$passed = 0;
|
||||
$failed = 0;
|
||||
|
||||
function vassert($name, $cond, $detail = '')
|
||||
{
|
||||
global $passed, $failed;
|
||||
if ($cond) {
|
||||
$passed++;
|
||||
return;
|
||||
}
|
||||
$failed++;
|
||||
echo "FAIL: {$name}\n";
|
||||
if ($detail !== '') {
|
||||
echo " {$detail}\n";
|
||||
}
|
||||
}
|
||||
|
||||
try {
|
||||
$pdo = DomainRepository::pdo();
|
||||
DomainRepository::ensureTable($pdo);
|
||||
|
||||
$prefix = 'verify-rand-' . substr(md5((string) microtime(true)), 0, 8) . '.test';
|
||||
$hostA = 'a.' . $prefix;
|
||||
$hostB = 'b.' . $prefix;
|
||||
$hostBound = 'bound.' . $prefix;
|
||||
|
||||
$ids = [];
|
||||
foreach ([$hostA, $hostB, $hostBound] as $host) {
|
||||
$res = DomainRepository::add($pdo, $host);
|
||||
vassert('add domain ' . $host, !empty($res['ok']), $res['error'] ?? '');
|
||||
if (!empty($res['id'])) {
|
||||
$ids[] = (int) $res['id'];
|
||||
}
|
||||
}
|
||||
|
||||
if (count($ids) >= 3) {
|
||||
DomainRepository::bindConfigToDomain($pdo, 'cfg_one', $ids[2]);
|
||||
DomainRepository::bindConfigToDomain($pdo, 'cfg_two', $ids[2]);
|
||||
}
|
||||
|
||||
$map = DomainRepository::groupConfigsByDomainId($pdo);
|
||||
vassert('多配置可共用同一域名', count($map[$ids[2]] ?? []) === 2, json_encode($map[$ids[2]] ?? []));
|
||||
|
||||
$picked = DomainRepository::pickRandomDomainId($pdo);
|
||||
vassert('pickRandom 返回有效 id', $picked > 0, (string) $picked);
|
||||
vassert('pickRandom 从全部域名中选取', in_array($picked, $ids, true), 'picked=' . $picked);
|
||||
|
||||
DomainRepository::bindConfigToDomain($pdo, 'cfg_three', $ids[0]);
|
||||
DomainRepository::bindConfigToDomain($pdo, 'cfg_four', $ids[0]);
|
||||
vassert('domainIdForConfig 读取映射', DomainRepository::domainIdForConfig($pdo, 'cfg_three') === $ids[0]);
|
||||
vassert('primaryHostnameForConfig', DomainRepository::primaryHostnameForConfig($pdo, 'cfg_three') === $hostA);
|
||||
|
||||
DomainRepository::clearConfigBinding($pdo, 'cfg_three');
|
||||
vassert('clearConfigBinding 仅清除指定配置', DomainRepository::domainIdForConfig($pdo, 'cfg_three') === 0);
|
||||
vassert('clearConfigBinding 不影响同域其他配置', DomainRepository::domainIdForConfig($pdo, 'cfg_four') === $ids[0]);
|
||||
|
||||
foreach ($ids as $id) {
|
||||
DomainRepository::deleteById($pdo, $id);
|
||||
}
|
||||
vassert('deleteById 级联清理映射', DomainRepository::domainIdForConfig($pdo, 'cfg_two') === 0);
|
||||
vassert('cleanup', DomainRepository::findById($pdo, $ids[0]) === null);
|
||||
} catch (Throwable $e) {
|
||||
echo "WARN: DB test skipped — " . $e->getMessage() . "\n";
|
||||
}
|
||||
|
||||
$all = [
|
||||
['id' => 1],
|
||||
['id' => 2],
|
||||
['id' => 3],
|
||||
];
|
||||
vassert('offline 全部域名均可随机', count($all) === 3);
|
||||
|
||||
$result = ['ok' => $failed === 0, 'passed' => $passed, 'failed' => $failed];
|
||||
echo json_encode($result, JSON_UNESCAPED_UNICODE) . "\n";
|
||||
exit($failed > 0 ? 1 : 0);
|
||||
Reference in New Issue
Block a user