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

108 lines
5.8 KiB
PHP

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

<?php
namespace App\Models {
class Model
{
}
require dirname(__DIR__) . '/app/Models/CloudOffer.php';
}
namespace {
use App\Models\CloudOffer;
$assert = static function ($condition, string $message): void {
if (!$condition) {
throw new \RuntimeException($message);
}
};
$valid = [
'resource_pool_id' => '8', 'code' => 'tokyo-premium', 'name' => '东京高级线路',
'price' => '019.9', 'stock' => '-1', 'status' => '1', 'sort' => '-20',
];
$normalized = CloudOffer::validateOffer($valid);
$assert($normalized === [
'resource_pool_id' => 8, 'code' => 'tokyo-premium', 'name' => '东京高级线路',
'price' => '19.90', 'currency' => 'USD', 'status' => 1, 'stock' => -1, 'sort' => -20,
], 'Offer input was not normalized into the expected pool, USD price, inventory and status fields.');
foreach ([
['resource_pool_id' => 0], ['code' => 'Tokyo Premium'], ['code' => ''],
['name' => ''], ['price' => '-1'], ['price' => '1.999'], ['stock' => -2],
['status' => 2], ['sort' => 1000001],
] as $invalid) {
try {
CloudOffer::validateOffer(array_replace($valid, $invalid));
throw new \RuntimeException('Invalid offer input was accepted: ' . json_encode($invalid));
} catch (\InvalidArgumentException $exception) {
// Expected.
}
}
$root = dirname(__DIR__);
$controller = file_get_contents($root . '/app/Controllers/Admin/ShopController.php');
$shopModel = file_get_contents($root . '/app/Models/Shop.php');
$routes = file_get_contents($root . '/config/routes.php');
$planViews = file_get_contents($root . '/resources/views/venix/admin/shop/create.tpl')
. file_get_contents($root . '/resources/views/venix/admin/shop/edit.tpl');
$offerView = file_get_contents($root . '/resources/views/venix/admin/shop/offer_form.tpl');
$offerScript = file_get_contents($root . '/resources/views/venix/admin/shop/offer_form_js.tpl');
$assert(!str_contains($planViews, 'name="host_id"')
&& !str_contains($planViews, 'name="price"')
&& !str_contains($planViews, 'name="stock"')
&& !str_contains($controller, "foreach (['name', 'host_id'")
&& str_contains($controller, "'name', 'cpu_cores', 'memory_gb'")
&& str_contains($controller, 'CloudOffer::validateOffer($input)')
&& !str_contains($shopModel, "'host_id' => 'int'")
&& !str_contains($shopModel, "'price' => 'decimal:2'")
&& !str_contains($shopModel, "'stock' => 'int'"),
'Base-plan administration still owns host placement, price or stock.');
$assert(str_contains($routes,
"get('/shop/{id}/edit', 'App\\Controllers\\Admin\\ShopController:edit');")
&& str_contains($routes,
"get('/shop/{id}/offers/{offer_id}/edit', 'App\\Controllers\\Admin\\ShopController:offerEdit');")
&& str_contains($routes,
"get('/shop/{id}/offers/create', 'App\\Controllers\\Admin\\ShopController:offerCreate')->add(new SuperAdmin())")
&& str_contains($routes,
"post('/shop/{id}/offers', 'App\\Controllers\\Admin\\ShopController:offerAdd')->add(new SuperAdmin())")
&& str_contains($routes,
"put('/shop/{id}/offers/{offer_id}', 'App\\Controllers\\Admin\\ShopController:offerUpdate')->add(new SuperAdmin())")
&& str_contains($routes,
"delete('/shop/{id}/offers/{offer_id}', 'App\\Controllers\\Admin\\ShopController:offerDelete')->add(new SuperAdmin())"),
'Offer read routes must remain admin-readable while every mutation stays behind SuperAdmin middleware.');
$assert(str_contains($controller, 'if (!AdminPermission::isSuperAdmin())')
&& str_contains($controller, "(int) \$values['status'] === 1 && (int) \$shop->status !== 1")
&& str_contains($controller, 'CloudResourcePool::find($poolId)')
&& str_contains($controller, "(string) \$pool->status === 'active'")
&& str_contains($controller, "CloudOrder::where('offer_id', \$offerId)->exists()")
&& str_contains($controller, "CloudInvoice::where('offer_id', \$offerId)->exists()")
&& str_contains($controller, "CloudInstance::where('offer_id', \$offerId)->exists()")
&& str_contains($controller, "CloudResourceReservation::where('offer_id', \$offerId)->exists()")
&& str_contains($controller, '(int) $locked->revision !== (int) $expectedRevision')
&& str_contains($controller, 'lockAndAssertPoolAvailable(')
&& str_contains($controller, '$this->offerReferenced((int) $locked->id)')
&& str_contains($controller, '$priceChanged = $locked->exists')
&& str_contains($controller, '} elseif ($priceChanged) {')
&& str_contains($controller, '$locked->price_version = max(1, (int) $locked->price_version) + 1;')
&& str_contains($controller, '$locked->delete()')
&& str_contains($controller, '$locked->status = 0;'),
'Offer mutation lacks server authorization, price versioning, or reference-aware deletion.');
foreach (['resource_pool_id', 'code', 'name', 'price', 'stock', 'status', 'sort', 'revision'] as $field) {
$assert(str_contains($offerView, 'name="' . $field . '"'),
'Offer editor is missing field: ' . $field);
}
$assert(str_contains($offerView, '每期价格(USD)')
&& str_contains($offerView, '当前价格版本')
&& str_contains($offerView, '{if !$admin_can_manage|default:false}disabled{/if}')
&& str_contains($offerScript, "method: form.getAttribute('data-method')")
&& str_contains($offerScript, "method: 'DELETE'")
&& !str_contains($offerView, '|nofilter'),
'Offer UI lost USD/version/read-only controls, mutation wiring, or output escaping.');
echo "Administrator base-plan and offer catalog contracts passed.\n";
}