style: align cloud product editor with Venix layout

dev
admin 2 months ago
parent eca57ab75f
commit 8759373324

@ -1,15 +1,85 @@
{assign var=admin_title value='编辑商品'}{assign var=admin_active value='shop'}{include file='admin/static_header.tpl'}
<div class="py-3"><h4>编辑云服务器套餐</h4><p class="text-muted">绑定宿主机后,用户可用余额购买开通。修改套餐不会改变已经开通的虚拟机。</p><a class="btn btn-light" href="/admin/shop">返回商品列表</a></div>
<form class="card" id="shopForm" data-method="PUT" data-url="/admin/shop/{$shop->id}"><div class="card-body"><div class="row g-3"><div class="col-12"><label class="form-label" for="name">商品名称</label><input class="form-control" id="name" name="name" type="text" maxlength="128" value="{$shop->name|escape:"html"}" required></div>
<div class="col-md-6"><label class="form-label" for="cpu_cores">CPU 核数</label><input class="form-control" id="cpu_cores" name="cpu_cores" type="number" min="1" max="64" step="1" value="{$shop->cpu_cores|escape:"html"}" required></div>
<div class="col-md-6"><label class="form-label" for="host_id">开通宿主机</label><select class="form-select" id="host_id" name="host_id" data-selected="{$shop->host_id|default:''|escape:'html'}"><option value="">未绑定(禁止购买)</option></select></div>
<div class="col-md-6"><label class="form-label" for="memory_gb">内存(GB)</label><input class="form-control" id="memory_gb" name="memory_gb" type="number" min="0.5" max="256" step="0.5" value="{$shop->memoryGB()|escape:"html"}" required></div>
<div class="col-md-6"><label class="form-label" for="bandwidth_mbps">带宽(Mbps)</label><input class="form-control" id="bandwidth_mbps" name="bandwidth_mbps" type="number" min="1" max="100000" step="1" value="{$shop->bandwidth_mbps|escape:"html"}" required></div>
<div class="col-md-6"><label class="form-label" for="disk_gib">硬盘(GB)</label><input class="form-control" id="disk_gib" name="disk_gib" type="number" min="1" max="4096" step="1" value="{$shop->disk_gib|escape:"html"}" required></div>
<div class="col-md-6"><label class="form-label" for="price">价格(USD)</label><div class="input-group"><span class="input-group-text">$</span><input class="form-control" id="price" name="price" type="number" min="0" max="100000000" step="0.01" value="{$shop->price|escape:"html"}" required></div></div>
<div class="col-md-6"><label class="form-label" for="validity_days">有效期(天)</label><input class="form-control" id="validity_days" name="validity_days" type="number" min="1" max="3650" step="1" value="{$shop->validity_days|default:30|escape:'html'}" required><div class="form-text">购买或续费一次增加的服务天数;修改后不追溯已有服务周期。</div></div>
<div class="col-md-6"><label class="form-label" for="traffic_tb">每期流量额度(TB)</label><input class="form-control" id="traffic_tb" name="traffic_tb" type="number" min="0" max="1024" step="0.0009765625" value="{$shop->trafficTB()|escape:'html'}" required><div class="form-text">每个账期独立计算下载与上传合计额度;填 0 表示不限流量,修改后不追溯已有实例。</div></div>
<div class="col-md-6"><label class="form-label" for="stock">库存</label><input class="form-control" id="stock" name="stock" type="number" min="-1" max="2147483647" step="1" value="{$shop->stock|escape:"html"}" required></div>
<div class="col-md-6"><label class="form-label" for="sort">排序</label><input class="form-control" id="sort" name="sort" type="number" min="-1000000" max="1000000" step="1" value="{$shop->sort|escape:"html"}" required></div>
<div class="col-md-6"><label class="form-label" for="status">状态</label><select class="form-select" id="status" name="status"><option value="1" {if $shop->status == 1}selected{/if}>上架</option><option value="0" {if $shop->status == 0}selected{/if}>下架</option></select></div></div><p id="shopFormStatus" class="admin-config-status">待保存</p><button class="btn btn-primary" id="shopFormSubmit" type="submit" {if !$admin_can_manage|default:false}disabled{/if}>保存商品</button></div></form><div class="admin-toast" id="shopFormToast"></div>
{include file='admin/shop/form_js.tpl'}{include file='admin/static_footer.tpl'}
{assign var=admin_title value='编辑商品'}
{assign var=admin_active value='shop'}
{include file='admin/static_header.tpl'}
<div class="py-3 d-flex align-items-xl-center flex-xl-row flex-column gap-3">
<div class="flex-grow-1">
<h4 class="fs-20 fw-semibold mb-1">编辑云服务器套餐</h4>
<p class="text-muted mb-0">绑定宿主机后,用户可用余额购买开通。修改套餐不会改变已经开通的虚拟机。</p>
</div>
<div class="admin-config-actions flex-shrink-0">
<span id="shopFormStatus" class="admin-config-status" role="status" aria-live="polite">待保存</span>
<a class="btn btn-light" href="/admin/shop">返回商品列表</a>
<button class="btn btn-primary" id="shopFormSubmit" type="submit" form="shopForm" {if !$admin_can_manage|default:false}disabled{/if}>保存商品</button>
</div>
</div>
<form class="card" id="shopForm" data-method="PUT" data-url="/admin/shop/{$shop->id}">
<div class="card-body">
<div class="row g-3">
<div class="col-md-6">
<label class="form-label" for="name">商品名称</label>
<input class="form-control" id="name" name="name" type="text" maxlength="128" value="{$shop->name|escape:'html'}" required>
</div>
<div class="col-md-6">
<label class="form-label" for="host_id">开通宿主机</label>
<select class="form-select" id="host_id" name="host_id" data-selected="{$shop->host_id|default:''|escape:'html'}">
<option value="">未绑定(禁止购买)</option>
</select>
</div>
<div class="col-md-6 col-xl-3">
<label class="form-label" for="cpu_cores">CPU 核数</label>
<input class="form-control" id="cpu_cores" name="cpu_cores" type="number" min="1" max="64" step="1" value="{$shop->cpu_cores|escape:'html'}" required>
</div>
<div class="col-md-6 col-xl-3">
<label class="form-label" for="memory_gb">内存(GB)</label>
<input class="form-control" id="memory_gb" name="memory_gb" type="number" min="0.5" max="256" step="0.5" value="{$shop->memoryGB()|escape:'html'}" required>
</div>
<div class="col-md-6 col-xl-3">
<label class="form-label" for="disk_gib">硬盘(GB)</label>
<input class="form-control" id="disk_gib" name="disk_gib" type="number" min="1" max="4096" step="1" value="{$shop->disk_gib|escape:'html'}" required>
</div>
<div class="col-md-6 col-xl-3">
<label class="form-label" for="bandwidth_mbps">带宽(Mbps)</label>
<input class="form-control" id="bandwidth_mbps" name="bandwidth_mbps" type="number" min="1" max="100000" step="1" value="{$shop->bandwidth_mbps|escape:'html'}" required>
</div>
<div class="col-md-6 col-xl-4">
<label class="form-label" for="price">价格(USD)</label>
<div class="input-group">
<span class="input-group-text">$</span>
<input class="form-control" id="price" name="price" type="number" min="0" max="100000000" step="0.01" value="{$shop->price|escape:'html'}" required>
</div>
</div>
<div class="col-md-6 col-xl-4">
<label class="form-label" for="validity_days">有效期(天)</label>
<input class="form-control" id="validity_days" name="validity_days" type="number" min="1" max="3650" step="1" value="{$shop->validity_days|default:30|escape:'html'}" required>
<div class="form-text">购买或续费一次增加的服务天数;修改后不追溯已有服务周期。</div>
</div>
<div class="col-md-6 col-xl-4">
<label class="form-label" for="traffic_tb">每期流量额度(TB)</label>
<input class="form-control" id="traffic_tb" name="traffic_tb" type="number" min="0" max="1024" step="0.0009765625" value="{$shop->trafficTB()|escape:'html'}" required>
<div class="form-text">每个账期独立计算下载与上传合计额度;填 0 表示不限流量,修改后不追溯已有实例。</div>
</div>
<div class="col-md-6 col-xl-4">
<label class="form-label" for="stock">库存</label>
<input class="form-control" id="stock" name="stock" type="number" min="-1" max="2147483647" step="1" value="{$shop->stock|escape:'html'}" required>
</div>
<div class="col-md-6 col-xl-4">
<label class="form-label" for="sort">排序</label>
<input class="form-control" id="sort" name="sort" type="number" min="-1000000" max="1000000" step="1" value="{$shop->sort|escape:'html'}" required>
</div>
<div class="col-md-6 col-xl-4">
<label class="form-label" for="status">状态</label>
<select class="form-select" id="status" name="status">
<option value="1" {if $shop->status == 1}selected{/if}>上架</option>
<option value="0" {if $shop->status == 0}selected{/if}>下架</option>
</select>
</div>
</div>
</div>
</form>
<div class="admin-toast" id="shopFormToast"></div>
{include file='admin/shop/form_js.tpl'}
{include file='admin/static_footer.tpl'}

@ -0,0 +1,189 @@
<?php
require dirname(__DIR__) . '/vendor/autoload.php';
use App\Models\Shop;
set_error_handler(static function (int $severity, string $message, string $file, int $line): bool {
if ((error_reporting() & $severity) && ($severity & (E_WARNING | E_USER_WARNING))) {
throw new ErrorException($message, 0, $severity, $file, $line);
}
return false;
});
// The real model supplies price casts and GB/TB conversions without a database.
$attributes = [
'id' => 71, 'name' => '香港标准云服务器 · 月付', 'host_id' => 7,
'cpu_cores' => 4, 'memory_mib' => 2560, 'disk_gib' => 80,
'bandwidth_mbps' => 200, 'price' => '12.34', 'validity_days' => 45,
'traffic_quota_bytes' => 3 * Shop::TIB / 2, 'stock' => 25, 'sort' => -2, 'status' => 1,
];
$makeShop = static function (array $overrides = []) use ($attributes): Shop {
$shop = new Shop();
$shop->setRawAttributes(array_replace($attributes, $overrides));
return $shop;
};
$smarty = new Smarty();
$compileDir = sys_get_temp_dir() . '/admin-shop-edit-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_csrf_token', 'fixture-not-a-real-token');
$render = static function (Shop $shop, ?bool $canManage = true) use ($smarty): string {
$smarty->assign('shop', $shop);
if ($canManage === null) {
$smarty->clearAssign('admin_can_manage');
} else {
$smarty->assign('admin_can_manage', $canManage);
}
return $smarty->fetch('admin/shop/edit.tpl');
};
$html = $render($makeShop());
$readOnlyHtml = $render($makeShop(), false);
if (in_array('--fixture', $argv ?? [], true)) {
echo json_encode([
'html' => $html,
'readonly_html' => $readOnlyHtml,
'hosts' => [
['id' => 7, 'name' => '香港宿主机 A', 'region' => 'HongKong', 'enabled' => true],
['id' => 8, 'name' => '东京宿主机 B', 'region' => 'Tokyo', 'enabled' => true],
],
], JSON_UNESCAPED_UNICODE | JSON_INVALID_UTF8_SUBSTITUTE | JSON_THROW_ON_ERROR);
exit;
}
$assert = static function (bool $condition, string $message): void {
if (!$condition) throw new RuntimeException($message);
};
$parse = static function (string $html): DOMXPath {
$dom = new DOMDocument();
libxml_use_internal_errors(true);
$dom->loadHTML('<?xml encoding="UTF-8">' . $html);
return new DOMXPath($dom);
};
$one = static function (DOMXPath $xpath, string $query) use ($assert): DOMElement {
$matches = $xpath->query($query);
$assert($matches->length === 1 && $matches->item(0) instanceof DOMElement,
'Expected one element matching ' . $query);
return $matches->item(0);
};
$xpath = $parse($html);
$form = $one($xpath, '//form[@id="shopForm"]');
$assert($form->getAttribute('data-method') === 'PUT'
&& $form->getAttribute('data-url') === '/admin/shop/71',
'Restyling changed the existing update method or resource URL.');
$expectedNames = ['name', 'host_id', 'cpu_cores', 'memory_gb', 'disk_gib', 'bandwidth_mbps',
'price', 'validity_days', 'traffic_tb', 'stock', 'sort', 'status'];
$actualNames = [];
foreach ($xpath->query('//form[@id="shopForm"]//*[@name and (self::input or self::select or self::textarea)]') as $control) {
$actualNames[] = $control->getAttribute('name');
}
sort($actualNames);
$sortedNames = $expectedNames;
sort($sortedNames);
$assert($actualNames === $sortedNames, 'The edit form added, removed, or duplicated a product field.');
foreach ($expectedNames as $name) {
$control = $one($xpath, '//form[@id="shopForm"]//*[@id="' . $name . '"]');
$assert($control->getAttribute('name') === $name, 'Existing form field names and IDs diverged: ' . $name);
$assert($xpath->query('//label[@for="' . $name . '"]')->length >= 1,
'An input lost its associated label: ' . $name);
}
$name = $one($xpath, '//input[@id="name"]');
$assert($name->getAttribute('type') === 'text' && $name->getAttribute('maxlength') === '128'
&& $name->getAttribute('value') === $attributes['name'] && $name->hasAttribute('required'),
'Name field type, length, required state, or existing value changed.');
$numbers = [
'cpu_cores' => ['1', '64', '1', '4'],
'memory_gb' => ['0.5', '256', '0.5', '2.5'],
'disk_gib' => ['1', '4096', '1', '80'],
'bandwidth_mbps' => ['1', '100000', '1', '200'],
'price' => ['0', '100000000', '0.01', '12.34'],
'validity_days' => ['1', '3650', '1', '45'],
'traffic_tb' => ['0', '1024', '0.0009765625', '1.5'],
'stock' => ['-1', '2147483647', '1', '25'],
'sort' => ['-1000000', '1000000', '1', '-2'],
];
foreach ($numbers as $id => [$minimum, $maximum, $step, $value]) {
$control = $one($xpath, '//input[@id="' . $id . '"]');
$assert($control->getAttribute('type') === 'number' && $control->hasAttribute('required')
&& $control->getAttribute('min') === $minimum
&& $control->getAttribute('max') === $maximum
&& $control->getAttribute('step') === $step
&& $control->getAttribute('value') === $value,
'Existing numeric field semantics changed: ' . $id);
}
$host = $one($xpath, '//select[@id="host_id"]');
$assert($host->getAttribute('data-selected') === '7'
&& $xpath->query('//select[@id="host_id"]/option[@value=""]')->length === 1,
'Host selection lost its current binding or unbound option before asynchronous loading.');
$one($xpath, '//select[@id="status"]');
$statusOptions = [];
foreach ($xpath->query('//select[@id="status"]/option') as $option) {
$statusOptions[] = $option->getAttribute('value');
}
$assert($statusOptions === ['1', '0']
&& $one($xpath, '//select[@id="status"]/option[@selected]')->getAttribute('value') === '1',
'Product availability option values or selected state changed.');
$submit = $one($xpath, '//button[@id="shopFormSubmit"]');
$assert($submit->getAttribute('type') === 'submit' && $submit->getAttribute('form') === 'shopForm'
&& !$submit->hasAttribute('disabled')
&& $xpath->query('//form[@id="shopForm"]//button[@id="shopFormSubmit"]')->length === 0,
'The external save button must remain associated with the existing form and enabled for managers.');
$one($xpath, '//*[@id="shopFormStatus"]');
$one($xpath, '//*[@id="shopFormToast"]');
foreach ([$readOnlyHtml, $render($makeShop(), null)] as $restrictedHtml) {
$restricted = $parse($restrictedHtml);
$restrictedSubmit = $one($restricted, '//button[@id="shopFormSubmit"]');
$assert($restrictedSubmit->hasAttribute('disabled')
&& $restrictedSubmit->getAttribute('form') === 'shopForm',
'Read-only or missing management permission must keep the save button disabled.');
$assert(str_contains($restrictedHtml, 'window.ADMIN_CAN_MANAGE = false;'),
'The existing save script received incorrect management permission.');
}
$assert(str_contains($html, 'window.ADMIN_CAN_MANAGE = true;'),
'Managers lost the existing save script permission flag.');
$off = $parse($render($makeShop([
'status' => 0, 'host_id' => null, 'stock' => -1, 'price' => '0.00',
'memory_mib' => 512, 'traffic_quota_bytes' => 0, 'validity_days' => null,
])));
$assert($one($off, '//select[@id="status"]/option[@selected]')->getAttribute('value') === '0'
&& $one($off, '//select[@id="host_id"]')->getAttribute('data-selected') === ''
&& $one($off, '//input[@id="stock"]')->getAttribute('value') === '-1'
&& $one($off, '//input[@id="price"]')->getAttribute('value') === '0.00'
&& $one($off, '//input[@id="memory_gb"]')->getAttribute('value') === '0.5'
&& $one($off, '//input[@id="traffic_tb"]')->getAttribute('value') === '0'
&& $one($off, '//input[@id="validity_days"]')->getAttribute('value') === '30',
'Unbound, off-sale, unlimited-stock/traffic, zero-price, or default-period values were lost.');
$smallQuota = $parse($render($makeShop(['traffic_quota_bytes' => Shop::GIB])));
$assert($one($smallQuota, '//input[@id="traffic_tb"]')->getAttribute('value') === '0.0009765625',
'Rendering rounded away the supported one-GiB traffic increment.');
$unsafeName = '\"><img src=x onerror=alert(1)></script><script>window.__shopUnsafe=1</script> & 套餐';
$unsafeHtml = $render($makeShop(['name' => $unsafeName]));
$unsafe = $parse($unsafeHtml);
$assert($one($unsafe, '//input[@id="name"]')->getAttribute('value') === $unsafeName
&& $unsafe->query('//*[@onerror]')->length === 0
&& !str_contains($unsafeHtml, '<script>window.__shopUnsafe=1</script>'),
'An existing product name escaped its attribute or became executable markup.');
// Assert the actual shared script still renders, preserving its one submit
// handler and its existing form-driven URL/method contract after moving save.
$saveScripts = [];
foreach ($xpath->query('//script[not(@src)]') as $script) {
if (str_contains($script->textContent, "var form = document.getElementById('shopForm');")) {
$saveScripts[] = $script->textContent;
}
}
$assert(count($saveScripts) === 1
&& str_contains($saveScripts[0], "form.addEventListener('submit', save)")
&& str_contains($saveScripts[0], "fetch(form.getAttribute('data-url')")
&& str_contains($saveScripts[0], "method: form.getAttribute('data-method')"),
'The shared save script was removed, duplicated, or disconnected from the edit form.');
echo "Administrator shop edit Smarty fields, constraints, values, form ownership, permissions and escaping passed.\n";
Loading…
Cancel
Save