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/CloudInvoiceDetailContractT...

115 lines
6.4 KiB
PHP

<?php
// Static public-contract checks: keep invoice identifiers tenant-scoped and
// prevent the internal invoice number from leaking into browser responses.
$root = dirname(__DIR__);
$assert = static function ($condition, string $message): void {
if (!$condition) {
throw new RuntimeException($message);
}
};
$section = static function (string $source, string $start, string $end): string {
$offset = strpos($source, $start);
if ($offset === false) {
return '';
}
$limit = strpos($source, $end, $offset + strlen($start));
return $limit === false ? substr($source, $offset)
: substr($source, $offset, $limit - $offset);
};
$pageRoutes = file_get_contents($root . '/config/routes.php');
$apiRoutes = file_get_contents($root . '/config/routes_cloud.php');
$pageController = file_get_contents($root . '/app/Controllers/CloudPageController.php');
$apiController = file_get_contents($root . '/app/Controllers/CloudApiController.php');
$invoiceService = file_get_contents($root . '/app/Services/Cloud/CloudInvoiceService.php');
$jobService = file_get_contents($root . '/app/Services/Cloud/CloudJobService.php');
$invoiceModel = file_get_contents($root . '/app/Models/CloudInvoice.php');
$invoiceEmail = file_get_contents($root . '/resources/email/cloud/invoice.tpl');
$assert(str_contains($pageRoutes,
"'/cloud/invoices/{id:[0-9]+}', 'App\\Controllers\\CloudPageController:invoiceDetail'"),
'The authenticated invoice-detail page route is missing.');
$assert(str_contains($apiRoutes,
"'/invoices/{id:[0-9]+}', 'App\\Controllers\\CloudApiController:invoice'"),
'The numeric invoice-detail API route is missing.');
$pageMethod = $section($pageController, 'public function invoiceDetail(',
'public function reinstall(');
$assert(str_contains($pageMethod, "'invoice-detail'")
&& str_contains($pageMethod, "'/user/cloud/invoices'")
&& str_contains($pageController, "assign('cloud_invoice_id', \$invoiceId)"),
'The detail page does not validate and pass its numeric invoice id independently.');
$listMethod = $section($apiController, 'public function invoices(',
'private function createProvisionInvoice(');
$assert(str_contains($listMethod, '$perPage = 10;')
&& str_contains($listMethod, "\$query['page'] ?? '1'")
&& str_contains($listMethod, "'pagination' => [")
&& str_contains($listMethod, "'total_pages' => \$totalPages")
&& str_contains($listMethod, 'CloudInvoiceService::listSummaries($records)'),
'The invoice list is not using its fixed ten-row server-side pagination contract.');
$assert(!str_contains($listMethod, 'CloudInvoiceService::display(')
&& !str_contains($listMethod, 'CloudInvoicePaymentService::methods(')
&& !str_contains($listMethod, "'payment_methods' =>"),
'The list still expands full invoice details or exposes detail-only payment methods.');
$summaryMethod = $section($invoiceService, 'public static function listSummaries(',
'private static function listKey(');
$assert(substr_count($summaryMethod, '::whereIn(') === 3
&& str_contains($summaryMethod, "->get(['id', 'user_id', 'status'])")
&& str_contains($summaryMethod, "->get(['id', 'order_id', 'user_id', 'status'])"),
'Invoice list associations are not loaded with fixed-size batch queries.');
foreach (['id', 'title', 'type_label', 'purpose', 'amount', 'status', 'created_at',
'order_status', 'fulfillment_status', 'job_status'] as $field) {
$assert(str_contains($summaryMethod, "'{$field}' =>"),
'Invoice list summary is missing field: ' . $field);
}
foreach (['payment_methods', 'payment_reference', 'paid_via', 'specification',
'traffic_quota_bytes', 'payment_deadline_at'] as $detailOnlyField) {
$assert(!str_contains($summaryMethod, "'{$detailOnlyField}' =>"),
'Invoice list summary leaked detail-only field: ' . $detailOnlyField);
}
$apiMethod = $section($apiController, 'public function invoice(',
'public function payInvoice(');
$assert(str_contains($apiMethod, "CloudInvoice::where('id', \$args['id'])")
&& str_contains($apiMethod, "->where('user_id', \$actor['user_id'])")
&& str_contains($apiMethod, "'payment_methods' => CloudInvoicePaymentService::methods(\$user)")
&& str_contains($apiMethod, "'balance' => (string) \$user->money"),
'The invoice-detail API is not strictly owner-scoped or lacks payment options.');
$assert(!str_contains($apiMethod, "\$actor['is_admin']"),
'Administrator scope must not bypass invoice ownership in the user detail API.');
$displayMethod = $section($invoiceService, 'public static function display(',
'public static function payBalance(');
$assert(str_contains($displayMethod, "'recharge' => '余额充值'")
&& str_contains($displayMethod, "'provision' => '实例开通'")
&& str_contains($displayMethod, "'renewal' => '实例续费'")
&& str_contains($displayMethod, "'title' => \$title")
&& str_contains($displayMethod, "'type_label' => \$title"),
'Invoice purpose labels are incomplete.');
$assert(!str_contains($displayMethod, "'invoice_no' =>"),
'The internal invoice number is still exposed to users.');
$assert(substr_count($displayMethod, "->where('user_id', \$invoice->user_id)") >= 3,
'Invoice associations are not constrained to the invoice owner.');
$assert(str_contains($invoiceModel, "protected \$hidden = ['invoice_no', 'payment_reference']"),
'Sensitive invoice internals are not protected from accidental model serialization.');
foreach (['created_at', 'amount', 'status', 'plan_name', 'cpu_cores', 'memory_mib',
'disk_gib', 'bandwidth_mbps', 'validity_days', 'traffic_quota_bytes',
'order_id', 'job_id', 'fulfillment'] as $field) {
$assert(str_contains($displayMethod, "'{$field}' =>"),
'Invoice detail is missing field: ' . $field);
}
$assert(str_contains($jobService, "'updated_at' =>")
&& str_contains($jobService, "'attempts' =>")
&& str_contains($jobService, 'CloudInput::safeResult($result)'),
'Provisioning progress lacks safe timestamps or attempt metadata.');
$assert(str_contains($invoiceEmail, '#{$invoice_id|escape}')
&& str_contains($invoiceEmail, '&#36;{$amount|escape}')
&& !str_contains($invoiceEmail, 'invoice_no')
&& str_contains($invoiceService,
"'invoice_url' => \$base . '/user/cloud/invoices/' . (int) \$invoice->id"),
'Renewal email must use the public numeric id, USD and its detail-page link.');
echo "Cloud invoice detail routing, ownership and public fields passed.\n";