From 8759373324fa3c34d9f32920fc2efce4a2f010d8 Mon Sep 17 00:00:00 2001 From: admin Date: Wed, 30 Sep 2026 03:37:04 +0800 Subject: [PATCH] style: align cloud product editor with Venix layout --- web/resources/views/venix/admin/shop/edit.tpl | 100 +++++++-- web/tests/AdminShopEditViewTest.php | 189 ++++++++++++++++++ 2 files changed, 274 insertions(+), 15 deletions(-) create mode 100644 web/tests/AdminShopEditViewTest.php diff --git a/web/resources/views/venix/admin/shop/edit.tpl b/web/resources/views/venix/admin/shop/edit.tpl index 5c2af02d..2b6b8f66 100755 --- a/web/resources/views/venix/admin/shop/edit.tpl +++ b/web/resources/views/venix/admin/shop/edit.tpl @@ -1,15 +1,85 @@ -{assign var=admin_title value='编辑商品'}{assign var=admin_active value='shop'}{include file='admin/static_header.tpl'} -

编辑云服务器套餐

绑定宿主机后,用户可用余额购买开通。修改套餐不会改变已经开通的虚拟机。

返回商品列表
-
-
-
-
-
-
-
$
-
购买或续费一次增加的服务天数;修改后不追溯已有服务周期。
-
每个账期独立计算下载与上传合计额度;填 0 表示不限流量,修改后不追溯已有实例。
-
-
-

待保存

-{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'} + +
+
+

编辑云服务器套餐

+

绑定宿主机后,用户可用余额购买开通。修改套餐不会改变已经开通的虚拟机。

+
+
+ 待保存 + 返回商品列表 + +
+
+ +
+
+
+
+ + +
+
+ + +
+
+ + +
+
+ + +
+
+ + +
+
+ + +
+
+ +
+ $ + +
+
+
+ + +
购买或续费一次增加的服务天数;修改后不追溯已有服务周期。
+
+
+ + +
每个账期独立计算下载与上传合计额度;填 0 表示不限流量,修改后不追溯已有实例。
+
+
+ + +
+
+ + +
+
+ + +
+
+
+
+ +
+ +{include file='admin/shop/form_js.tpl'} +{include file='admin/static_footer.tpl'} diff --git a/web/tests/AdminShopEditViewTest.php b/web/tests/AdminShopEditViewTest.php new file mode 100644 index 00000000..1eaf4b82 --- /dev/null +++ b/web/tests/AdminShopEditViewTest.php @@ -0,0 +1,189 @@ + 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('' . $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 = '\"> & 套餐'; +$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, ''), + '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";