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/AdminOfferFormViewTest.php

152 lines
6.9 KiB
PHTML

<?php
require dirname(__DIR__) . '/vendor/autoload.php';
use App\Models\CloudOffer;
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;
});
$shop = new Shop();
$shop->setRawAttributes(['id' => 71, 'name' => '标准云服务器']);
$offer = new CloudOffer();
$offer->setRawAttributes([
'id' => 11, 'plan_id' => 71, 'resource_pool_id' => 8,
'code' => 'tokyo-standard', 'name' => '东京标准云服务器',
'price' => '19.90', 'price_version' => 3, 'revision' => 4,
'stock' => 25, 'status' => 1, 'sort' => 10,
]);
$pools = [
false => [
['id' => 8, 'label' => '日本东京 → 通用资源池', 'active' => true,
'availability_reason' => ''],
['id' => 9, 'label' => '中国香港 → 高防资源池', 'active' => false,
'availability_reason' => '位置已停用'],
],
true => [
['id' => 8, 'label' => '日本东京 → 默认线路 → 通用资源池', 'active' => true,
'availability_reason' => ''],
['id' => 9, 'label' => '中国香港 → 高防线路 → 高防资源池', 'active' => false,
'availability_reason' => '位置已停用'],
],
];
$smarty = new Smarty();
$compileDir = sys_get_temp_dir() . '/admin-offer-form-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 (?CloudOffer $item, bool $canManage,
bool $linesEnabled = false) use ($smarty, $shop, $pools): string {
$smarty->assign('shop', $shop);
$smarty->assign('offer', $item);
$smarty->assign('offer_pools', $pools[$linesEnabled]);
$smarty->assign('line_feature_enabled', $linesEnabled);
$smarty->assign('admin_can_manage', $canManage);
return $smarty->fetch('admin/shop/offer_form.tpl');
};
$createHtml = $render(null, true);
$editHtml = $render($offer, true);
$readOnlyHtml = $render($offer, false);
$linesEnabledHtml = $render(null, true, true);
$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);
};
$create = $parse($createHtml);
$edit = $parse($editHtml);
$readOnly = $parse($readOnlyHtml);
$createForm = $one($create, '//form[@id="offerForm"]');
$editForm = $one($edit, '//form[@id="offerForm"]');
$assert($createForm->getAttribute('data-method') === 'POST'
&& $createForm->getAttribute('data-url') === '/admin/shop/71/offers'
&& !$createForm->hasAttribute('data-plan-id'),
'Create form lost its POST endpoint or retained obsolete code-generation context.');
$assert($editForm->getAttribute('data-method') === 'PUT'
&& $editForm->getAttribute('data-url') === '/admin/shop/71/offers/11',
'Edit form lost its PUT endpoint.');
foreach (['resource_pool_id', 'name', 'price', 'stock', 'status', 'sort', 'revision'] as $field) {
$one($create, '//*[@name="' . $field . '"]');
$one($edit, '//*[@name="' . $field . '"]');
}
$namePosition = strpos($createHtml, 'id="name"');
$poolPosition = strpos($createHtml, 'id="resource_pool_id"');
$sortPosition = strpos($createHtml, 'id="sort"');
$assert($namePosition !== false && $poolPosition !== false && $sortPosition !== false
&& $namePosition < $poolPosition && $poolPosition < $sortPosition
&& strpos($createHtml, 'id="code"') === false,
'Create must lead with name/location and must not expose a system-identifier field.');
$assert(str_contains($createHtml, '销售名称与位置')
&& str_contains($createHtml, '价格与销售状态')
&& str_contains($createHtml, '高级设置')
&& !str_contains($createHtml, '方案代码'),
'Offer fields were not grouped under the simplified terminology.');
$poolOption = $one($create, '//select[@id="resource_pool_id"]/option[@value="8"]');
$assert(trim($poolOption->textContent) === '日本东京 → 通用资源池'
&& !str_contains($poolOption->textContent, 'tokyo'),
'Hidden-line mode must show the readable region-to-pool path without a code.');
$linesEnabled = $parse($linesEnabledHtml);
$enabledPoolOption = $one($linesEnabled,
'//select[@id="resource_pool_id"]/option[@value="8"]');
$assert(trim($enabledPoolOption->textContent)
=== '日本东京 → 默认线路 → 通用资源池'
&& str_contains($linesEnabledHtml, '地区 → 线路 → 资源池'),
'Enabled-line mode must retain the full readable location path.');
$inactivePool = $one($create, '//select[@id="resource_pool_id"]/option[@value="9"]');
$assert($inactivePool->hasAttribute('disabled') && str_contains($inactivePool->textContent, '位置已停用'),
'Inactive new-offer locations must remain visible but unavailable.');
$editCode = $one($edit, '//input[@id="code"]');
$assert($editCode->hasAttribute('readonly')
&& $editCode->getAttribute('aria-readonly') === 'true'
&& $editCode->getAttribute('value') === 'tokyo-standard'
&& !$editCode->hasAttribute('name')
&& $create->query('//*[@id="code" or @id="offerCodeSuggest"]')->length === 0,
'System identifiers must be absent on create and read-only/non-submitted on edit.');
foreach ($readOnly->query('//form[@id="offerForm"]//*[@name and (self::input or self::select or self::textarea)]') as $control) {
if ($control->getAttribute('type') !== 'hidden') {
$assert($control->hasAttribute('disabled'), 'Read-only offer controls must remain disabled.');
}
}
$scripts = [];
foreach ($create->query('//script[not(@src)]') as $script) {
if (str_contains($script->textContent, "var form = document.getElementById('offerForm')")) {
$scripts[] = $script->textContent;
}
}
$assert(count($scripts) === 1
&& !str_contains($scripts[0], 'suggestIdentifier')
&& !str_contains($scripts[0], "'code'")
&& str_contains($scripts[0], "method: form.getAttribute('data-method')"),
'Save wiring must exclude the system identifier and retain the form method contract.');
echo "Administrator offer form grouping, location path, identifier immutability and permissions passed.\n";