|
|
|
|
<?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 GB/TB conversions without a database.
|
|
|
|
|
$attributes = [
|
|
|
|
|
'id' => 71, 'name' => '香港标准云服务器 · 月付',
|
|
|
|
|
'cpu_cores' => 4, 'memory_mib' => 2560, 'disk_gib' => 80,
|
|
|
|
|
'bandwidth_mbps' => 200, 'validity_days' => 45,
|
|
|
|
|
'traffic_quota_bytes' => 3 * Shop::TIB / 2, '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,
|
|
|
|
|
bool $linesEnabled = false) use ($smarty): string {
|
|
|
|
|
$smarty->assign('shop', $shop);
|
|
|
|
|
$smarty->assign('offers', [[
|
|
|
|
|
'id' => 11, 'code' => 'hk-standard', 'name' => '香港标准线路',
|
|
|
|
|
'pool' => $linesEnabled
|
|
|
|
|
? '香港 → 默认线路 → 通用资源池'
|
|
|
|
|
: '香港 → 通用资源池',
|
|
|
|
|
'price' => '$12.34',
|
|
|
|
|
'price_version' => 2, 'stock' => 25, 'status' => true,
|
|
|
|
|
'location_active' => true, 'availability_reason' => '', 'sort' => 0,
|
|
|
|
|
]]);
|
|
|
|
|
$smarty->assign('line_feature_enabled', $linesEnabled);
|
|
|
|
|
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,
|
|
|
|
|
'offers' => [['id' => 11, 'code' => 'hk-standard']],
|
|
|
|
|
], 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', 'cpu_cores', 'memory_gb', 'disk_gib', 'bandwidth_mbps',
|
|
|
|
|
'validity_days', 'traffic_tb', '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'],
|
|
|
|
|
'validity_days' => ['1', '3650', '1', '45'],
|
|
|
|
|
'traffic_tb' => ['0', '1024', '0.0009765625', '1.5'],
|
|
|
|
|
'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);
|
|
|
|
|
}
|
|
|
|
|
$assert($xpath->query('//*[@id="host_id" or @id="price" or @id="stock"]')->length === 0,
|
|
|
|
|
'The base-plan editor still exposes host placement, offer pricing, or offer stock.');
|
|
|
|
|
$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.');
|
|
|
|
|
foreach ($restricted->query('//form[@id="shopForm"]//*[@name]') as $control) {
|
|
|
|
|
$assert($control->hasAttribute('disabled'), 'Read-only plan fields must be 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,
|
|
|
|
|
'memory_mib' => 512, 'traffic_quota_bytes' => 0, 'validity_days' => null,
|
|
|
|
|
])));
|
|
|
|
|
$assert($one($off, '//select[@id="status"]/option[@selected]')->getAttribute('value') === '0'
|
|
|
|
|
&& $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',
|
|
|
|
|
'Disabled, unlimited-traffic, or default-period plan 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($xpath->query('//a[@href="/admin/shop/71/offers/11/edit"]')->length === 1
|
|
|
|
|
&& str_contains($html, '香港标准线路')
|
|
|
|
|
&& str_contains($html, '$12.34'),
|
|
|
|
|
'The base-plan editor did not render its independently priced offer list.');
|
|
|
|
|
$offerTechnicalDetails = $one($xpath,
|
|
|
|
|
'//table[caption[contains(., "基础套餐的销售方案")]]//details[summary[contains(., "技术信息")]]');
|
|
|
|
|
$assert(str_contains($offerTechnicalDetails->textContent, '系统标识')
|
|
|
|
|
&& str_contains($offerTechnicalDetails->textContent, 'hk-standard')
|
|
|
|
|
&& str_contains($offerTechnicalDetails->textContent, '记录 #11')
|
|
|
|
|
&& str_contains($html, '地区 → 资源池决定可调度范围')
|
|
|
|
|
&& str_contains($html, '香港 → 通用资源池')
|
|
|
|
|
&& !str_contains($html, '香港 → 默认线路 → 通用资源池')
|
|
|
|
|
&& str_contains($html, '<th>销售名称</th><th>销售位置</th>'),
|
|
|
|
|
'Offer technical identifiers must stay collapsed while the list leads with name and location.');
|
|
|
|
|
$linesEnabledHtml = $render($makeShop(), true, true);
|
|
|
|
|
$assert(str_contains($linesEnabledHtml, '地区 → 线路 → 资源池决定可调度范围')
|
|
|
|
|
&& str_contains($linesEnabledHtml, '香港 → 默认线路 → 通用资源池'),
|
|
|
|
|
'Offer list must show the line segment only when differentiated lines are enabled.');
|
|
|
|
|
|
|
|
|
|
// 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";
|