'42', 'user_name' => '演示客户', 'user_email' => 'demo@example.test', 'plan_id' => '3', 'plan_name' => '标准云服务器', 'subject_name' => 'vm-production', 'amount' => '10.00', 'payment_method' => 'balance', 'due_at' => $now + 86400, 'paid_at' => $now - 3500, 'created_at' => $now - 3600, 'updated_at' => $now - 3400, 'order_id' => '301', 'task_id' => '501', 'instance_id' => '101', 'renewal_id' => null]; $invoices = [ array_replace($common, ['id' => '201', 'purpose' => 'provision', 'financial_status' => 'paid', 'fulfillment_status' => 'succeeded']), array_replace($common, ['id' => '202', 'purpose' => 'provision', 'financial_status' => 'refunded', 'fulfillment_status' => 'failed', 'instance_id' => null]), array_replace($common, ['id' => '203', 'purpose' => 'recharge', 'financial_status' => 'paid', 'fulfillment_status' => null, 'amount' => '100.00', 'plan_id' => null, 'plan_name' => '', 'subject_name' => '', 'order_id' => null, 'instance_id' => null, 'task_id' => null, 'payment_method' => 'yipay_alipay']), array_replace($common, ['id' => '204', 'purpose' => 'renewal', 'financial_status' => 'paid', 'fulfillment_status' => 'succeeded', 'renewal_id' => '1', 'order_id' => null, 'task_id' => null]), array_replace($common, ['id' => '205', 'purpose' => 'provision', 'financial_status' => 'expired', 'fulfillment_status' => 'expired', 'amount' => '100.00', 'paid_at' => null, 'payment_method' => null, 'order_id' => '305', 'task_id' => null, 'instance_id' => null]), ]; $orders = [ [ 'id' => '301', 'user_id' => '42', 'user_name' => '演示客户', 'user_email' => 'demo@example.test', 'plan_id' => '3', 'plan_name' => '标准云服务器', 'host_id' => '7', 'amount' => '10.00', 'financial_status' => 'paid', 'fulfillment_status' => 'active', 'invoice_id' => '201', 'task_id' => '501', 'job_status' => 'active', 'job_completed' => false, 'instance_id' => '101', 'attempts' => 1, 'error' => null, 'refunded_at' => null, 'created_at' => $now - 3600, 'updated_at' => $now - 60, ], [ 'id' => '302', 'user_id' => '42', 'user_name' => '演示客户', 'user_email' => 'demo@example.test', 'plan_id' => '3', 'plan_name' => '标准云服务器', 'host_id' => '7', 'amount' => '10.00', 'financial_status' => 'paid', 'fulfillment_status' => 'failed', 'invoice_id' => '206', 'task_id' => '502', 'job_status' => 'failed', 'job_completed' => true, 'instance_id' => '102', 'attempts' => 1, 'error' => '任务已失败,不会自动重试', 'refunded_at' => null, 'created_at' => $now - 7200, 'updated_at' => $now - 120, ], ]; $paymentBase = ['user_id' => '42', 'user_name' => '演示客户', 'user_email' => 'demo@example.test', 'provider' => 'yipay_alipay', 'purpose' => 'provision', 'created_at' => $now - 3500, 'updated_at' => $now - 3400]; $payments = [ array_replace($paymentBase, ['id' => '701', 'invoice_id' => '201', 'amount' => '10.00', 'payment_status' => 'paid_invoice', 'invoice_financial_status' => 'paid']), array_replace($paymentBase, ['id' => '702', 'invoice_id' => '205', 'amount' => '100.00', 'payment_status' => 'credited_balance', 'invoice_financial_status' => 'expired']), ]; $filters = ['page' => 1, 'per_page' => 30, 'q' => '', 'user_id' => null, 'created_from' => null, 'created_to' => null]; $viewFilters = [ 'invoices' => ['financial_status' => '', 'fulfillment_status' => '', 'payment_method' => '', 'purpose' => ''], 'orders' => ['status' => '', 'financial_status' => ''], 'payments' => ['status' => '', 'provider' => '', 'invoice_id' => null], ]; $pages = []; foreach (['invoices' => $invoices, 'orders' => $orders, 'payments' => $payments] as $view => $items) { $pages[$view] = ['view' => $view, 'items' => $items, 'filters' => $filters + $viewFilters[$view], 'pagination' => ['page' => 1, 'per_page' => 30, 'total' => count($items), 'total_pages' => 1, 'has_previous' => false, 'has_next' => false], 'generated_at' => $now]; } $pages['invoices']['summary'] = ['total_records' => 5, 'total_amount' => '230.00', 'outstanding_count' => 0, 'outstanding_amount' => '0.00', 'paid_count' => 3, 'paid_amount' => '120.00', 'refunded_count' => 1, 'refunded_amount' => '10.00']; $pages['orders']['summary'] = ['total_records' => 2, 'total_amount' => '20.00', 'status_counts' => ['active' => 1, 'failed' => 1]]; $pages['payments']['summary'] = ['total_records' => 2, 'total_amount' => '110.00', 'paid_invoice_amount' => '10.00', 'credited_balance_amount' => '100.00', 'status_counts' => ['paid_invoice' => 1, 'credited_balance' => 1]]; $details = []; foreach ($invoices as $row) { $recharge = $row['purpose'] === 'recharge'; $refunded = $row['financial_status'] === 'refunded'; $expired = $row['financial_status'] === 'expired'; $detailInvoice = $row + ['validity_days' => $recharge ? 0 : 30, 'traffic_quota_bytes' => $recharge ? 0 : 107374182400, 'renews_expires_at' => $row['purpose'] === 'renewal' ? $now + 30 * 86400 : null, 'payment_deadline_at' => $now + 86400, 'specification' => $recharge ? [] : ['cpu_cores' => 2, 'memory_mib' => 2048, 'disk_gib' => 40, 'bandwidth_mbps' => 100]]; $details[$row['id']] = ['invoice' => $detailInvoice, 'order' => $row['order_id'] ? array_replace($orders[0], ['id' => $row['order_id'], 'invoice_id' => $row['id'], 'fulfillment_status' => $row['fulfillment_status'], 'refunded_at' => $refunded ? $now - 3300 : null]) : null, 'job' => $row['task_id'] ? ['id' => '501', 'kind' => 'create', 'completed' => true, 'status' => $row['fulfillment_status'], 'attempts' => 1, 'phase' => 'complete', 'error' => $refunded ? '实例创建失败,已确认清理' : null, 'created_at' => $now - 3500, 'updated_at' => $now - 3400] : null, 'instance' => $row['instance_id'] ? ['id' => '101', 'name' => 'vm-production', 'status' => 'active', 'service_status' => 'active', 'network_status' => 'active', 'expires_at' => $now + 30 * 86400] : null, 'payment_attempts' => $expired ? [$payments[1]] : [], 'payment_attempts_truncated' => false, 'funds' => ['title' => $refunded ? '已退回账户余额' : ($expired ? '款项保留在账户余额' : '账单已结清'), 'description' => $refunded ? '开通失败退款记入账户余额,不是原支付渠道原路退款。' : ($expired ? '支付记录标记为已入余额,该次款项未结清目标账单。' : '款项已用于结清账单。'), 'settled_amount' => $expired ? null : $row['amount'], 'refund_amount' => $refunded ? $row['amount'] : null, 'refund_destination' => $refunded ? 'balance' : null, 'refunded_at' => $refunded ? $now - 3300 : null, 'balance_retained_amount' => $expired ? '100.00' : '0.00', 'balance_retained_count' => $expired ? 1 : 0, 'payment_records_truncated' => false, 'notes' => ['仅根据现有业务记录展示,不是完整钱包变动账本。']], 'timeline' => [['kind' => 'created', 'title' => '账单创建', 'description' => '已记录账单创建时间', 'at' => $row['created_at']]], 'links' => ['user' => '/admin/user/42/edit', 'instance' => $row['instance_id'] ? '/admin/cloud/instances?q=11111111-1111-4111-8111-111111111111' : null, 'payments' => '/admin/billing?view=payments&invoice_id=' . $row['id']], ]; if ($row['paid_at']) $details[$row['id']]['timeline'][] = ['kind' => 'paid', 'title' => '账单结清', 'description' => '账户余额支付', 'at' => $row['paid_at']]; if ($refunded) $details[$row['id']]['timeline'][] = ['kind' => 'refunded', 'title' => '退款退回余额', 'description' => '开通失败后退回账户余额', 'at' => $now - 3300]; } $smarty = new Smarty(); $compileDir = sys_get_temp_dir() . '/admin-billing-view-' . bin2hex(random_bytes(6)); mkdir($compileDir); $smarty->setCompileDir($compileDir); $smarty->setTemplateDir(dirname(__DIR__) . '/resources/views/venix'); $smarty->assign('config', ['appName' => 'Venix Test']); $smarty->assign('cdn', ''); $smarty->assign('user', (object) ['isLogin' => false]); $smarty->assign('admin_can_manage', true); $smarty->assign('admin_csrf_token', 'fixture-not-a-real-token'); $render = static function (?array $payload, string $error = '', string $view = 'invoices') use ($smarty): string { $smarty->assign('billing_page', $payload); $smarty->assign('billing_view', $payload['view'] ?? $view); $smarty->assign('billing_error', $error); $smarty->assign('billing_bootstrap_json', json_encode($payload, JSON_HEX_TAG | JSON_HEX_AMP | JSON_HEX_APOS | JSON_HEX_QUOT | JSON_UNESCAPED_UNICODE | JSON_INVALID_UTF8_SUBSTITUTE)); return $smarty->fetch('admin/billing.tpl'); }; $assert = static function (bool $ok, string $message): void { if (!$ok) throw new RuntimeException($message); }; // Test-only input used by the local preview to exercise refresh/deep-link SSR. foreach ($argv ?? [] as $arg) { if (str_starts_with($arg, '--render-page=')) { $payload = json_decode(base64_decode(substr($arg, 14), true), true, 512, JSON_THROW_ON_ERROR); echo $render($payload); exit; } } $html = []; foreach ($pages as $view => $page) $html[$view] = $render($page); $failedHtml = $render(null, '账单数据暂不可用,请稍后重试'); if (in_array('--fixture', $argv ?? [], true)) { echo json_encode(['html' => $html, 'failed_html' => $failedHtml, 'pages' => $pages, 'details' => $details], JSON_UNESCAPED_UNICODE | JSON_INVALID_UTF8_SUBSTITUTE); exit; } $dom = new DOMDocument(); libxml_use_internal_errors(true); foreach ($pages as $view => $page) { $dom->loadHTML('' . $html[$view]); $xpath = new DOMXPath($dom); $assert($xpath->query('//tbody[@id="billing-table-body"]/tr')->length === count($page['items']), 'Server-rendered rows missing for billing view: ' . $view); $assert(str_contains($dom->textContent, '演示客户'), 'The first paint must contain meaningful account data.'); $assert($xpath->query('//dialog[@id="billing-detail-dialog" and @data-admin-instance-drawer]')->length === 1, 'Billing details must reuse the existing administrator instance drawer.'); } $assert(str_contains($html['invoices'], '已续费'), 'Renewal completion must not be labelled instance provisioning.'); $assert(str_contains($html['orders'], '执行中') && str_contains($html['orders'], '已失败'), 'Active and terminal task receipts must be visible in the first billing paint and summary.'); $dom->loadHTML('' . $html['orders']); $xpath = new DOMXPath($dom); $assert(trim($xpath->query('//*[@id="billing-summary-total"]')->item(0)->textContent) === '2' && trim($xpath->query('//*[@id="billing-summary-focus"]')->item(0)->textContent) === '1 笔', 'Order summary must count one active task without fabricating a waiting state.'); $dom->loadHTML('' . $html['invoices']); $xpath = new DOMXPath($dom); $assert(trim($xpath->query('//*[@id="billing-summary-result"]')->item(0)->textContent) === '3 笔 · $120.00' && trim($xpath->query('//*[@id="billing-summary-refunded"]')->item(0)->textContent) === '1 笔 · $10.00', 'SSR must show paid and refunded totals separately, without subtracting refunds a second time.'); $unsafe = $pages['invoices']; $unsafe['items'][0]['subject_name'] = ''; $unsafe['items'][0]['user_name'] = ''; $unsafe['filters']['q'] = '" autofocus onfocus="alert(2)'; $unsafeHtml = $render($unsafe); $dom->loadHTML('' . $unsafeHtml); $xpath = new DOMXPath($dom); $assert($xpath->query('//*[@onerror or @onfocus]')->length === 0 && !str_contains($unsafeHtml, ''), 'Untrusted billing fields escaped into executable markup.'); $assert(str_contains($dom->textContent, ''), 'Escaped names must remain visible text.'); $bootstrap = json_decode($xpath->query('//*[@id="admin-billing-bootstrap"]')->item(0)->textContent, true, 512, JSON_THROW_ON_ERROR); $assert($bootstrap['items'][0]['subject_name'] === $unsafe['items'][0]['subject_name'] && $bootstrap['filters']['q'] === $unsafe['filters']['q'], 'Safe bootstrap encoding changed the original data.'); foreach (['invoices' => ['financial_status', 'paid'], 'orders' => ['status', 'failed'], 'payments' => ['status', 'credited_balance']] as $view => [$key, $value]) { $filtered = $pages[$view]; $filtered['filters'][$key] = $value; $filtered['filters']['user_id'] = '42'; $filtered['pagination'] = ['page' => 2, 'per_page' => 50, 'total' => 80, 'total_pages' => 2, 'has_previous' => true, 'has_next' => false]; $dom->loadHTML('' . $render($filtered)); $xpath = new DOMXPath($dom); $assert($xpath->query('//*[@id="billing-status-primary"]/option[@selected and @value="' . $value . '"]')->length === 1 && $xpath->query('//*[@id="billing-per-page"]/option[@selected and @value="50"]')->length === 1 && $xpath->query('//*[@id="billing-user-id" and @value="42"]')->length === 1 && str_contains($xpath->query('//*[@id="billing-page-label"]')->item(0)->textContent, '第 2 / 2 页'), 'SSR discarded the active filter or page: ' . $view); } $empty = $pages['invoices']; $empty['items'] = []; $empty['pagination']['total'] = 0; $empty['summary'] = array_fill_keys(array_keys($empty['summary']), 0); $emptyHtml = $render($empty); $assert(!str_contains($emptyHtml, 'vm-production'), 'Empty billing response retained an old record.'); $assert(str_contains($failedHtml, '账单数据暂不可用,请稍后重试') && !str_contains($failedHtml, 'vm-production'), 'Failed read was rendered as a successful old list.'); foreach (array_keys($pages) as $view) { $failure = $render(null, '账单数据暂不可用,请稍后重试', $view); $dom->loadHTML('' . $failure); $xpath = new DOMXPath($dom); $assert($xpath->query('//*[@data-billing-view="' . $view . '" and @aria-current="page"]')->length === 1, 'A failed snapshot must keep the requested view selected: ' . $view); $assert(trim($xpath->query('//*[@id="billing-summary-total"]')->item(0)->textContent) === '—', 'A failed snapshot must not appear as zero financial records.'); $assert(json_decode($xpath->query('//*[@id="admin-billing-bootstrap"]')->item(0)->textContent, true, 512, JSON_THROW_ON_ERROR) === null, 'A failed snapshot must not bootstrap a successful list.'); } echo "Administrator billing three-view SSR, shared drawer, escaping and empty/error rendering passed.\n";