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.
130 lines
8.8 KiB
PHTML
130 lines
8.8 KiB
PHTML
|
3 weeks ago
|
<?php
|
||
|
|
|
||
|
|
require dirname(__DIR__) . '/vendor/autoload.php';
|
||
|
|
|
||
|
|
use App\Services\Cloud\CloudUserPageView;
|
||
|
|
|
||
|
|
set_error_handler(static function (int $severity, string $message, string $file, int $line): bool {
|
||
|
|
if (error_reporting() & $severity) throw new ErrorException($message, 0, $severity, $file, $line);
|
||
|
|
return false;
|
||
|
|
});
|
||
|
|
$assert = static function (bool $condition, string $message): void {
|
||
|
|
if (!$condition) throw new RuntimeException($message);
|
||
|
|
};
|
||
|
|
$smarty = new Smarty();
|
||
|
|
$compileDir = sys_get_temp_dir() . '/virt-user-first-paint-' . bin2hex(random_bytes(6));
|
||
|
|
mkdir($compileDir);
|
||
|
|
$smarty->setCompileDir($compileDir);
|
||
|
|
$smarty->setTemplateDir(dirname(__DIR__) . '/resources/views/venix');
|
||
|
|
$render = static function (string $page, array $values) use ($smarty): DOMXPath {
|
||
|
|
$smarty->clearAllAssign();
|
||
|
|
$smarty->assign('cloud_vm_bootstrap_json', '{}');
|
||
|
|
$smarty->assign($values);
|
||
|
|
$html = $smarty->fetch('cloud/pages/' . $page . '.tpl');
|
||
|
|
$dom = new DOMDocument();
|
||
|
|
$old = libxml_use_internal_errors(true);
|
||
|
|
$dom->loadHTML('<?xml encoding="UTF-8">' . $html);
|
||
|
|
libxml_clear_errors();
|
||
|
|
libxml_use_internal_errors($old);
|
||
|
|
return new DOMXPath($dom);
|
||
|
|
};
|
||
|
|
$one = static function (DOMXPath $xpath, string $id): DOMElement {
|
||
|
|
$node = $xpath->query('//*[@id="' . $id . '"]')->item(0);
|
||
|
|
if (!$node instanceof DOMElement) throw new RuntimeException('Missing first-paint element ' . $id);
|
||
|
|
return $node;
|
||
|
|
};
|
||
|
|
$unsafe = '<img src=x onerror=alert(1)>';
|
||
|
|
$backup = ['id' => 9, 'name' => $unsafe, 'state' => 'ready',
|
||
|
|
'source_instance' => ['id' => '7', 'name' => 'vm-seven', 'status' => 'active'],
|
||
|
|
'created_at' => 1793404800, 'consistency' => 'filesystem',
|
||
|
|
'object_size_bytes' => 1024, 'virtual_size_bytes' => 25 * 1073741824,
|
||
|
|
'can_restore_original' => false, 'can_promote' => true];
|
||
|
|
$payload = ['items' => [$backup], 'pagination' => ['page' => 2, 'total' => 41, 'total_pages' => 3]];
|
||
|
|
$xpath = $render('backups', ['cloud_backup_view' => CloudUserPageView::backups($payload)]);
|
||
|
|
$assert(str_contains($one($xpath, 'cloud-backups-list')->textContent, $unsafe)
|
||
|
|
&& $xpath->query('//img')->length === 0, 'Backup first paint is empty or contains unescaped names.');
|
||
|
|
$assert($one($xpath, 'cloud-backups-count')->textContent === '41 个'
|
||
|
|
&& $one($xpath, 'cloud-backups-page')->textContent === '第 2 / 3 页'
|
||
|
|
&& $one($xpath, 'cloud-backups-status')->hasAttribute('hidden'), 'Backup count, page or loaded state is incorrect.');
|
||
|
|
$assert($xpath->query('//a[contains(@href,"setting=automatic_backup")]')->length === 0,
|
||
|
|
'First paint invented original-restore permission for an ineligible backup.');
|
||
|
|
$backup['can_restore_original'] = true;
|
||
|
|
$xpath = $render('backups', ['cloud_backup_view' => CloudUserPageView::backups(['items' => [$backup]])]);
|
||
|
|
$assert($xpath->query('//a[contains(@href,"/instances/7?tab=settings")]')->length === 1, 'Eligible original restore link is missing.');
|
||
|
|
|
||
|
|
$key = str_repeat('a', 32);
|
||
|
|
$snapshot = ['snapshot_key' => $key, 'name' => 'snapshot-before-js', 'state' => 'ready',
|
||
|
|
'source_instance' => ['id' => '7', 'name' => 'vm-seven'], 'created_at' => 1793404800,
|
||
|
|
'can_create_instance' => true, 'can_delete' => false, 'active_invoice_count' => 1];
|
||
|
|
$xpath = $render('cloud-snapshots', ['cloud_snapshot_view' => CloudUserPageView::cloudSnapshots(['items' => [$snapshot]])]);
|
||
|
|
$assert(str_contains($one($xpath, 'cloud-snapshots-list')->textContent, 'snapshot-before-js')
|
||
|
|
&& $xpath->query('//a[@href="/user/cloud/deploy?cloud_snapshot=' . $key . '"]')->length === 1,
|
||
|
|
'Cloud snapshot first paint lost its name or deployment link.');
|
||
|
|
$assert($xpath->query('//button[@title="存在待支付账单,请先支付或等待账单失效后再删除。" and @disabled]')->length === 1,
|
||
|
|
'Snapshot first paint lost pending-invoice deletion protection.');
|
||
|
|
|
||
|
|
$vpc = ['vpc_key' => $key, 'name' => 'vpc-before-js', 'region_id' => 1,
|
||
|
|
'region_name' => '香港', 'cidr' => '10.0.1.0/24', 'gateway' => '10.0.1.1', 'state' => 'active', 'port_count' => 1];
|
||
|
|
$member = ['id' => '7', 'name' => 'vm-member', 'region_id' => 1, 'public_ipv4' => '192.0.2.7',
|
||
|
|
'port' => ['vpc_key' => $key, 'state' => 'attached', 'private_ip' => '10.0.1.2', 'mac_address' => '02:11:22:33:44:55']];
|
||
|
|
$candidate = ['id' => '8', 'name' => 'vm-candidate', 'region_id' => 1, 'public_ipv4' => '192.0.2.8', 'port' => null, 'can_attach' => true];
|
||
|
|
$elsewhere = $candidate; $elsewhere['id'] = '99'; $elsewhere['name'] = 'other-region'; $elsewhere['region_id'] = 2;
|
||
|
|
$library = ['items' => [$vpc], 'regions' => [['id' => 1, 'name' => '香港']],
|
||
|
|
'instances' => [$member, $candidate, $elsewhere], 'used' => 1, 'maximum' => 16];
|
||
|
|
foreach ([['query' => [], 'page' => 'home'], ['query' => ['view' => 'create'], 'page' => 'create'],
|
||
|
|
['query' => ['vpc' => $key], 'page' => 'members'],
|
||
|
|
['query' => ['vpc' => $key, 'view' => 'join'], 'page' => 'members'],
|
||
|
|
['query' => ['vpc' => str_repeat('b', 32)], 'page' => 'missing'],
|
||
|
|
['query' => ['vpc' => ['invalid']], 'page' => 'missing']] as $case) {
|
||
|
|
$view = CloudUserPageView::vpcs($library, $case['query']);
|
||
|
|
$xpath = $render('vpcs', ['cloud_vpc_view' => $view]);
|
||
|
|
$assert(!$one($xpath, 'cloud-vpcs-' . $case['page'])->hasAttribute('hidden'), 'VPC initial route is hidden before JavaScript.');
|
||
|
|
$assert($one($xpath, 'cloud-vpcs-quota')->textContent === '1 / 16 个', 'VPC first paint resets the quota to zero.');
|
||
|
|
if ($case['page'] === 'home') $assert(str_contains($one($xpath, 'cloud-vpcs-list')->textContent, 'vpc-before-js'), 'VPC list is empty.');
|
||
|
|
if ($case['page'] === 'create') $assert($xpath->query('//select[@id="cloud-vpc-region"]/option[@value="1"]')->length === 1, 'VPC regions wait for JavaScript.');
|
||
|
|
if ($case['page'] === 'members') {
|
||
|
|
$text = $one($xpath, 'cloud-vpc-instance-manager')->textContent;
|
||
|
|
$joining = ($case['query']['view'] ?? '') === 'join';
|
||
|
|
$assert(str_contains($text, $joining ? 'vm-candidate' : 'vm-member')
|
||
|
|
&& !str_contains($text, 'other-region')
|
||
|
|
&& str_contains($text, $joining ? '192.0.2.8' : '10.0.1.2'), 'VPC members or addresses do not match the requested view.');
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
$deploy = ['cloud_deploy_default_region_id' => 1, 'cloud_deploy_default_line_id' => 1,
|
||
|
|
'cloud_deploy_default_offer_id' => 1, 'cloud_deploy_lines_enabled' => false,
|
||
|
|
'cloud_deploy_regions' => [['id' => 1, 'name' => '香港']], 'cloud_deploy_lines' => [],
|
||
|
|
'cloud_deploy_offers' => [['id' => 1, 'name' => 'plan', 'region_id' => 1, 'line_id' => 1,
|
||
|
|
'sellable' => true, 'cpu_cores' => 1, 'memory_mib' => 1024, 'disk_gib' => 25,
|
||
|
|
'traffic_gib' => 1024, 'traffic_tib' => '1']], 'cloud_deploy_template_groups' => [],
|
||
|
|
'cloud_deploy_ssh_keys' => [], 'cloud_deploy_bootstrap_json' => '{}',
|
||
|
|
'cloud_deploy_price' => '$10.00 USD', 'cloud_deploy_backup_price' => '$2.00 USD', 'cloud_deploy_snapshot' => null];
|
||
|
|
$xpath = $render('deploy', $deploy);
|
||
|
|
$assert(!$one($xpath, 'vm-order-summary')->hasAttribute('hidden')
|
||
|
|
&& $one($xpath, 'vm-order-total-price')->textContent === '$10.00 USD'
|
||
|
|
&& !$one($xpath, 'vm-automatic-backup')->hasAttribute('disabled'), 'Selected plan pricing or backup availability waits for JavaScript.');
|
||
|
|
$deploy['cloud_deploy_snapshot'] = ['snapshot_key' => $key, 'name' => $unsafe];
|
||
|
|
$xpath = $render('deploy', $deploy);
|
||
|
|
$assert(!$one($xpath, 'vm-cloud-snapshot-source')->hasAttribute('hidden')
|
||
|
|
&& $one($xpath, 'vm-template-field')->hasAttribute('hidden')
|
||
|
|
&& $one($xpath, 'vm-cloud-fields')->hasAttribute('hidden')
|
||
|
|
&& $one($xpath, 'vm-root-password')->hasAttribute('disabled')
|
||
|
|
&& $one($xpath, 'vm-cloud-snapshot-key')->getAttribute('value') === $key
|
||
|
|
&& $xpath->query('//img')->length === 0, 'Snapshot deployment first paint shows normal installation controls or unsafe names.');
|
||
|
|
|
||
|
|
$xpath = $render('isos', ['cloud_iso_view' => CloudUserPageView::isos(['isos' => [
|
||
|
|
['id' => 'iso', 'name' => 'iso-before-js', 'size_bytes' => 1024, 'sha256' => str_repeat('a', 64), 'can_delete' => true],
|
||
|
|
]])]);
|
||
|
|
$assert(str_contains($one($xpath, 'user-iso-list')->textContent, 'iso-before-js'), 'Personal ISO library waits for JavaScript.');
|
||
|
|
|
||
|
|
$xpath = $render('detail', ['cloud_vm_view' => ['can_operate' => true, 'can_delete' => true],
|
||
|
|
'cloud_detail_has_ssh_keys' => false, 'cloud_detail_no_ssh_keys' => true]);
|
||
|
|
$assert($xpath->query('//*[@id="vm-detail-primary-actions"]/button[@disabled and @class="row-button"]')->length === 4,
|
||
|
|
'Instance daily actions wait for JavaScript or use different first-paint styling.');
|
||
|
|
$xpath = $render('detail', ['cloud_vm_view' => ['can_operate' => false, 'can_delete' => false],
|
||
|
|
'cloud_detail_has_ssh_keys' => false, 'cloud_detail_no_ssh_keys' => true]);
|
||
|
|
$assert($xpath->query('//*[@id="vm-detail-primary-actions"]/button')->length === 0,
|
||
|
|
'Read-only first paint exposes owner power controls.');
|
||
|
|
|
||
|
|
echo "User backup, cloud-snapshot, VPC routes, deployment pricing/source, ISO and instance-action first paints passed without JavaScript.\n";
|