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($html, 'window.ADMIN_CSRF_TOKEN ='), 'The shared administrator CSRF token bootstrap was removed.'); $assert(!str_contains($html, 'window.ADMIN_CAN_MANAGE'), 'A duplicate client-side administrator permission flag survived.'); $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";