You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
virt/web/tests/CloudInvoicePresentationMod...

225 lines
8.4 KiB
PHTML

<?php
require dirname(__DIR__) . '/vendor/autoload.php';
use App\Services\Cloud\CloudInvoicePresentation;
$assert = static function (bool $condition, string $message): void {
if (!$condition) {
throw new RuntimeException($message);
}
};
$renewal = [
'id' => '17',
'purpose' => 'renewal',
'title' => '实例续费',
'currency' => 'USD',
'created_at' => 2_000_000_000,
'amount' => '12.51',
'status' => 'overdue',
'display_status' => 'renewal_grace',
'renews_expires_at' => 2_000_100_000,
'payment_deadline_at' => 2_000_359_200,
'deletion_at' => 2_000_359_200,
'payable' => true,
'validity_days' => 30,
'traffic_quota_bytes' => 0,
'items' => [
[
'id' => '31',
'code' => 'instance',
'label' => '实例套餐续费',
'amount' => '10.00',
'currency' => 'USD',
],
[
'id' => '32',
'code' => 'automatic_backup',
'label' => '<img src=x onerror=alert(1)>自动备份',
'amount' => '2.50',
'currency' => 'USD',
],
],
];
$rows = CloudInvoicePresentation::summaries([$renewal]);
$assert(count($rows) === 1
&& $rows[0]['status_key'] === 'billing.status.financial.renewal_grace'
&& $rows[0]['status_class'] === 'warning'
&& $rows[0]['amount_display'] === '$12.51'
&& $rows[0]['polling_required'] === true
&& ($rows[0]['context_segments'][0]['key'] ?? null)
=== 'billing.context.renewal_expiry'
&& ($rows[0]['context_segments'][1]['value'] ?? null) === 2_000_100_000
&& ($rows[0]['context_segments'][2]['key'] ?? null)
=== 'billing.context.latest_payment'
&& ($rows[0]['context_segments'][3]['value'] ?? null) === 2_000_359_200,
'Renewal grace summary did not preserve its canonical status and deadline.');
$wrongLegacyTitle = $renewal;
$wrongLegacyTitle['title'] = '旧调用方错误标题';
$assert(CloudInvoicePresentation::summaries([$wrongLegacyTitle])[0]['title_key']
=== 'billing.purpose.renewal',
'Known invoice title was still controlled by a legacy caller mapping.');
$detail = CloudInvoicePresentation::detail([
'invoice' => $renewal,
'balance_payment_allowed' => false,
'payment_methods' => [],
]);
$assert($detail['show_line_items'] === true
&& $detail['line_items'] === [
[
'label' => '实例套餐续费',
'label_key' => 'billing.line_items.renewal_plan',
'amount_display' => '$10.00',
],
[
'label' => '<img src=x onerror=alert(1)>自动备份',
'label_key' => 'billing.line_items.backup_renewal',
'amount_display' => '$2.50',
],
]
&& $detail['amount_display'] === '$12.51'
&& $detail['number_parameters'] === ['id' => '17']
&& $detail['created_at'] === 2_000_000_000
&& ($detail['facts'][1]['timestamp'] ?? null) === 2_000_000_000
&& ($detail['facts'][2]['timestamp'] ?? null) === 2_000_100_000
&& $detail['polling_required'] === false,
'Detail projection changed frozen line-item labels or authoritative total.');
$switchable = CloudInvoicePresentation::detail([
'invoice' => $renewal,
'balance_payment_allowed' => true,
'balance' => '20.00',
'payment_methods' => [[
'id' => 'stripe_card',
'name' => 'Stripe · 银行卡',
]],
'active_payment' => [
'status' => 'review',
'task_id' => '91',
'payment' => null,
'can_submit' => true,
],
]);
$assert($switchable['show_payment'] === true
&& $switchable['payment_title_key'] === 'billing.payment.select_title'
&& $switchable['active_payment_key'] === 'billing.payment.active.review'
&& count($switchable['payment_methods']) === 2
&& $switchable['payment_methods'][0]['id'] === 'balance'
&& $switchable['payment_methods'][0]['selected'] === true
&& $switchable['polling_required'] === true,
'Unfinished payment did not remain switchable through the public module.');
$ipChange = [
'id' => '81',
'purpose' => 'ip_change',
'title' => '更换公网 IPv4',
'currency' => 'USD',
'created_at' => 2_000_000_000,
'amount' => '5.00',
'status' => 'paid',
'paid_at' => 2_000_000_000,
'paid_via' => 'balance',
'instance_id' => '41',
'instance_name' => 'vm-audit',
'task_id' => '82',
'job_status' => 'review',
'fulfillment_status' => 'review',
'error' => '操作效果待人工核对,请勿重复提交。',
'items' => [],
];
$ipView = CloudInvoicePresentation::detail(['invoice' => $ipChange]);
$ipRow = CloudInvoicePresentation::summaries([$ipChange])[0];
$assert($ipView['show_fulfillment'] === true
&& $ipView['title_key'] === 'billing.purpose.ip_change'
&& $ipView['fulfillment_title_key']
=== 'billing.fulfillment.title.ip_change'
&& $ipView['fulfillment_key'] === 'billing.status.fulfillment.review'
&& $ipView['fulfillment_class'] === 'warning'
&& $ipView['instance_id'] === '41'
&& $ipRow['show_fulfillment'] === true
&& $ipRow['fulfillment_key'] === 'billing.status.fulfillment.review'
&& ($ipRow['context_segments'][0]['key'] ?? null)
=== 'billing.context.target_instance'
&& ($ipRow['context_segments'][0]['parameters']['name'] ?? null)
=== 'vm-audit'
&& ($ipView['completion_notice']['copy_key'] ?? null)
=== 'billing.notice.ip_change_review',
'IP-change presentation lost its target or review-only fulfillment state.');
$unknown = CloudInvoicePresentation::summaries([[
'id' => '99',
'purpose' => 'custom',
'currency' => 'XTS',
'amount' => '7.25',
'status' => 'provider_unknown',
'created_at' => 0,
]])[0];
$assert($unknown['title_key'] === 'billing.purpose.invoice'
&& $unknown['status_key'] === 'billing.status.financial.unknown'
&& $unknown['status_class'] === 'other'
&& $unknown['amount_display'] === '7.25 XTS'
&& $unknown['created_display'] === '',
'Unknown status or currency did not degrade to a safe display.');
$patch = CloudInvoicePresentation::statusPatch([
'invoice' => $ipChange + [
'display_status' => 'paid',
'job_completed' => false,
'job_attempts' => 2,
'job_updated_at' => 2_000_000_001,
],
'active_payment' => [
'status' => 'pending',
'payment' => ['payment_id' => 'pay-1'],
'can_submit' => true,
],
]);
$assert($patch['status']['code'] === 'paid'
&& $patch['status']['copy_key'] === 'billing.status.financial.paid'
&& $patch['status']['class'] === 'running'
&& $patch['payment']['message_key'] === 'billing.payment.active.pending'
&& $patch['fulfillment']['fulfillment_key']
=== 'billing.status.fulfillment.review'
&& $patch['polling']['task_id'] === '82'
&& $patch['polling']['required'] === false
&& $patch['polling']['job_attempts'] === 2
&& !array_key_exists('amount', $patch['invoice']),
'Sparse status projection did not isolate mutable payment and polling fields.');
$source = file_get_contents(
dirname(__DIR__) . '/app/Services/Cloud/CloudInvoicePresentation.php'
);
$controllerSource = file_get_contents(
dirname(__DIR__) . '/app/Controllers/CloudPageController.php'
);
$pageServiceSource = file_get_contents(
dirname(__DIR__) . '/app/Services/Cloud/CloudInvoicePageService.php'
);
$detailServiceSource = file_get_contents(
dirname(__DIR__) . '/app/Services/Cloud/CloudInvoiceDetailPageService.php'
);
$assert(is_string($source)
&& !str_contains($source, 'App\\Models')
&& !preg_match('/\\btime\\s*\\(/', $source)
&& !str_contains($source, 'CloudInvoicePaymentService')
&& !str_contains($source, 'CloudInvoiceService::'),
'Presentation module acquired an ORM, clock, payment-provider or domain-service dependency.');
$assert(is_string($controllerSource) && is_string($pageServiceSource)
&& is_string($detailServiceSource)
&& str_contains($pageServiceSource,
'CloudInvoicePresentation::summaries(')
&& str_contains($controllerSource, "\$rows = \$payload['invoices'];")
&& str_contains($detailServiceSource,
'CloudInvoicePresentation::detail($payload)')
&& str_contains($detailServiceSource,
'CloudInvoicePresentation::statusPatch($payload)')
&& str_contains($controllerSource,
"'cloud_invoice_detail_view' => \$payload['presentation']"),
'Invoice SSR adapters do not consume the unified presentation module.');
echo "Cloud invoice presentation module checks passed.\n";