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

102 lines
4.3 KiB
PHP

<?php
require dirname(__DIR__) . '/vendor/autoload.php';
set_error_handler(static function (int $severity, string $message, string $file, int $line): bool {
if ((error_reporting() & $severity) && ($severity & (E_WARNING | E_USER_WARNING | E_DEPRECATED))) {
throw new ErrorException($message, 0, $severity, $file, $line);
}
return false;
});
$smarty = new Smarty();
$compileDir = sys_get_temp_dir() . '/object-storage-view-' . bin2hex(random_bytes(6));
mkdir($compileDir);
$smarty->setCompileDir($compileDir);
$smarty->setTemplateDir(dirname(__DIR__) . '/resources/views/venix');
$smarty->assign('config', ['appName' => 'Virt Test']);
$smarty->assign('cdn', '');
$smarty->assign('user', (object) ['isLogin' => false]);
$smarty->assign('admin_csrf_token', str_repeat('c', 64));
$smarty->assign('admin_display_user_name', 'admin');
$state = [
'name' => '"><script>window.__objectStorageXss=1</script>测试存储',
'driver' => 's3',
'endpoint' => 'https://minio.example.com:9000',
'region' => 'us-east-1',
'bucket' => 'test-bucket',
'prefix' => 'virt-cloud/backups',
'path_style' => true,
'enabled' => true,
'ready' => true,
'configured' => true,
'credentials_configured' => true,
'revision' => 7,
'tested_revision' => 7,
'last_test_status' => 'healthy',
'last_error_code' => '',
'last_error' => '',
'last_tested_at' => 1792876800,
'last_tested_at_text' => '2026-10-25 00:00:00',
'status_label' => '已启用',
'status_class' => 'success',
];
$render = static function (bool $canManage) use ($smarty, $state): string {
$smarty->assign('admin_can_manage', $canManage);
$smarty->assign('object_storage', $state);
$smarty->assign('object_storage_error', '');
return $smarty->fetch('admin/object_storage.tpl');
};
$html = $render(true);
$readonlyHtml = $render(false);
$assert = static function (bool $condition, string $message): void {
if (!$condition) {
throw new RuntimeException($message);
}
};
$parse = static function (string $document): DOMXPath {
$dom = new DOMDocument();
libxml_use_internal_errors(true);
$dom->loadHTML('<?xml encoding="UTF-8">' . $document);
return new DOMXPath($dom);
};
$one = static function (DOMXPath $xpath, string $query) use ($assert): DOMElement {
$nodes = $xpath->query($query);
$assert($nodes->length === 1 && $nodes->item(0) instanceof DOMElement,
'Expected one element matching ' . $query);
return $nodes->item(0);
};
$xpath = $parse($html);
$form = $one($xpath, '//form[@id="objectStorageForm"]');
$assert($form->getAttribute('action') === '/admin/object-storage/config'
&& $form->getAttribute('method') === 'post'
&& $form->getAttribute('data-can-manage') === '1',
'Object-storage form method, endpoint, or permission marker changed.');
$assert($one($xpath, '//input[@id="osName"]')->getAttribute('value') === $state['name']
&& $xpath->query('//script[contains(.,"__objectStorageXss")]')->length === 0,
'Object-storage application name escaped its value attribute.');
foreach (['osAccessKey', 'osSecretKey'] as $id) {
$secret = $one($xpath, '//input[@id="' . $id . '"]');
$assert($secret->getAttribute('type') === 'password'
&& $secret->getAttribute('value') === '',
'A stored object-storage credential can be rendered into the browser.');
}
$assert($one($xpath, '//input[@name="expected_revision"]')->getAttribute('value') === '7'
&& !$one($xpath, '//button[@id="osSave"]')->hasAttribute('disabled')
&& !$one($xpath, '//button[@id="osTest"]')->hasAttribute('disabled')
&& !$one($xpath, '//button[@id="osDisable"]')->hasAttribute('disabled'),
'Writable object-storage state did not render its current revision and actions.');
$readonly = $parse($readonlyHtml);
$assert($one($readonly, '//fieldset')->hasAttribute('disabled')
&& $one($readonly, '//button[@id="osSave"]')->hasAttribute('disabled')
&& $one($readonly, '//button[@id="osTest"]')->hasAttribute('disabled')
&& $one($readonly, '//button[@id="osDisable"]')->hasAttribute('disabled'),
'Ordinary administrator object-storage controls are not read-only.');
$assert(str_contains($html, 'window.ADMIN_CSRF_TOKEN ='),
'Shared administrator CSRF bootstrap is missing from object-storage page.');
echo "Object-storage administrator template rendering passed.\n";