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

172 lines
10 KiB
PHP

<?php
// Source contracts, without application boot or a database connection.
$root = dirname(__DIR__);
$assert = static function ($condition, $message) {
if (!$condition) {
throw new RuntimeException($message);
}
};
$routes = file_get_contents($root . '/config/routes.php');
$apiRoutes = file_get_contents($root . '/config/routes_cloud.php');
$assert(!str_contains($routes, "get('/node'") && !str_contains($routes, 'NodeController:'), 'Old user/admin node routes remain');
$assert(str_contains($routes, "get('/hosts', 'App\\Controllers\\Admin\\CloudPageController:hosts')"), 'Admin host page missing');
foreach (['/community', '/paid-registration', '/trafficlog', '/tcplog', '/tcpmonitor', '/redis-online', '/inactive-', '/abnormal-users', '/apple-', '/detect', '/edge-config', '/node-access-api', '/sublink', '/sub_server', '/checkin', '/cleanup', '/doc'] as $retired) {
$assert(!str_contains($routes, "'" . $retired), 'Retired route remains: ' . $retired);
}
$assert(!is_dir($root . '/resources/views/material') && !is_dir($root . '/resources/views/codex'), 'Retired templates remain');
$assert(is_file($root . '/resources/views/venix/admin/hosts/index.tpl'), 'Host administration template missing');
$assert(!str_contains($routes, "get('/telegram'"), 'Standalone administrator Telegram page route remains');
$assert(!is_file($root . '/resources/views/venix/admin/telegram/index.tpl'), 'Standalone administrator Telegram page template remains');
$ticketSystem = file_get_contents($root . '/resources/views/venix/admin/ticket/index.tpl');
$ticketTelegram = $root . '/resources/views/venix/admin/ticket/telegram_settings.tpl';
$adminSidebar = file_get_contents($root . '/resources/views/venix/admin/static_header.tpl');
$adminPageController = file_get_contents($root . '/app/Controllers/Admin/CloudPageController.php');
$systemJournal = file_get_contents($root . '/app/Services/Cloud/CloudSystemJournalService.php');
$repository = dirname($root);
$assert(is_file($ticketTelegram) && str_contains($ticketSystem, "admin/ticket/telegram_settings.tpl"), 'Ticket system Telegram settings were not integrated');
$assert(str_contains($ticketSystem, "value='工单系统'") && str_contains($adminSidebar, '<span>工单系统</span>'), 'Ticket system label was not updated');
$assert(!str_contains($adminSidebar, 'href="/admin/telegram"'), 'Standalone administrator Telegram navigation remains');
$config = file_get_contents($root . '/config/.config.php.example');
$assert(!str_contains($routes, "get('/update'")
&& !str_contains($apiRoutes, "'/admin/update")
&& !str_contains($adminSidebar, 'href="/admin/update"')
&& !str_contains($adminPageController, 'public function update('),
'Retired online-update route, navigation, or page action remains');
foreach ([
'app/Controllers/AdminUpdateController.php',
'app/Services/Cloud/CloudUpdateHelperClient.php',
'resources/views/venix/admin/update.tpl',
'public/cloud/update.js',
] as $retiredUpdateSource) {
$assert(!is_file($root . '/' . $retiredUpdateSource),
'Retired online-update source remains: ' . $retiredUpdateSource);
}
foreach ([
'gateway/cmd/virt-cloud-update',
'gateway/update',
'deploy/virt-cloud-update.json.example',
'deploy/virt-cloud-update.service',
'release/virt-cloud-update-linux-amd64',
] as $retiredUpdateSource) {
$assert(!file_exists($repository . '/' . $retiredUpdateSource),
'Retired online-update repository source remains: '
. $retiredUpdateSource);
}
$assert(!str_contains($config, 'cloud_update_')
&& !str_contains($systemJournal, 'virt-cloud-update.service'),
'Retired online-update configuration or journal unit remains');
foreach (['user_try', 'reg_method', 'user_class_default', 'enable_class_expire_reset', 'auto_clean_', 'auto_backup_webroot', 'mu_suffix', 'mu_regex', 'checkinMin', 'enable_documents', 'subUrl', 'tgUrl', 'enable_telegram', 'telegram_token', 'telegram_request_token', 'chatgpt_api'] as $key) {
$assert(!str_contains($config, $key), 'Retired configuration remains: ' . $key);
}
foreach (['db_password', 'smtp_passsword', 'super_admin', 'auto_backup_passwd', 'enable_login_bind_ip', 'notify_limit_mode'] as $key) {
$assert(str_contains($config, "['" . $key . "']"), 'Preserved configuration missing: ' . $key);
}
$userController = file_get_contents($root . '/app/Controllers/UserController.php');
$cloudPageController = file_get_contents($root . '/app/Controllers/CloudPageController.php');
$assert(!str_contains($routes, "post('/buy'")
&& !str_contains($userController, 'public function buy('),
'Retired legacy purchase endpoint remains');
$assert(!str_contains($routes,
"get('/shop', 'App\\Controllers\\CloudPageController:legacyShop')")
&& !str_contains($routes,
"post('/coupon_check', 'App\\Controllers\\UserController:CouponCheck')")
&& !str_contains($cloudPageController, 'function legacyShop(')
&& !str_contains($userController, 'function shop(')
&& !str_contains($userController, 'function CouponCheck(')
&& !is_file($root . '/resources/views/venix/user/shop.tpl')
&& !is_file($root . '/public/theme/venix/js/shop-page.js'),
'Retired user storefront or coupon-check endpoint remains');
$assert(str_contains($routes,
"get('/coupon', 'App\\Controllers\\AdminController:coupon')")
&& str_contains($routes,
"post('/coupon', 'App\\Controllers\\AdminController:addCoupon')")
&& is_file($root . '/app/Models/Coupon.php')
&& is_file($root . '/resources/views/venix/admin/coupon.tpl'),
'Administrator coupon management was removed with the user storefront');
$layoutScripts = file_get_contents($root . '/resources/views/venix/user/layout_scripts.tpl');
$accountStyles = file_get_contents($root . '/public/cloud/account.css');
$assert(!str_contains($layoutScripts, 'vu_shop_page')
&& !str_contains($layoutScripts, 'shop-page.js')
&& !str_contains($accountStyles, '.vu-shop-')
&& !str_contains($accountStyles, '.vu-plan-badge'),
'Retired user storefront assets are still referenced');
$assert(str_contains($routes, '/password/reset') === false && str_contains($routes, "group('/password'"), 'Password recovery routes missing');
$schema = file_get_contents($root . '/sql/ss.sql');
$shopStart = strpos($schema, 'CREATE TABLE `shop` (');
$shopEnd = strpos($schema, ') ENGINE=InnoDB', $shopStart);
$shopSchema = $shopStart === false || $shopEnd === false ? ''
: substr($schema, $shopStart, $shopEnd - $shopStart);
$assert($shopSchema !== ''
&& !str_contains($shopSchema, '`host_id`')
&& !str_contains($shopSchema, '`price`')
&& !str_contains($shopSchema, '`stock`'),
'Fresh base-plan schema retains legacy storefront fields');
$assert(!str_contains($schema, 'CREATE TABLE `bought`')
&& !str_contains($schema, 'ALTER TABLE `bought`'),
'Retired bought table remains in the base schema');
$cloudSchema = file_get_contents($root . '/sql/cloud.sql');
$cloudSetup = file_get_contents($root . '/app/Command/CloudSetup.php');
$assert(!str_contains($cloudSetup, "hasColumn('shop', 'host_id')")
&& !str_contains($cloudSetup, "unsignedBigInteger('host_id')"),
'cloudInit still recreates the retired base-plan host field');
$adminShopController = file_get_contents($root . '/app/Controllers/Admin/ShopController.php');
$assert(!str_contains($cloudSchema, 'DROP TABLE IF EXISTS bought')
&& !str_contains($cloudSetup, 'retireLegacyBought')
&& !str_contains($cloudSetup, "hasTable('bought')")
&& !str_contains($cloudSetup, "drop('bought')")
&& !is_file($root . '/app/Models/Bought.php')
&& !is_file($root . '/resources/views/venix/admin/shop/bought.tpl')
&& !str_contains($routes, "'/bought'")
&& !str_contains($adminShopController, 'function bought('),
'Legacy bought compatibility, storage, model or administrator page remains reachable');
preg_match('/CREATE TABLE `user` \(([\s\S]+?)\) ENGINE/', $schema, $userTable);
$assert(isset($userTable[1]), 'Clean account table missing');
foreach (['class', 'class_expire', 'expire_in', 'transfer_enable', 'passwd', 'method', 'obfs', 'port', 'protocol', 'node_group'] as $column) {
$assert(!str_contains($userTable[1], '`' . $column . '`'), 'Retired account column remains: ' . $column);
}
$logSchema = file_get_contents($root . '/sql/user_log.sql');
preg_match_all('/CREATE\s+TABLE\s+`([^`]+)`/i', $logSchema, $logTableMatches);
$logTables = $logTableMatches[1];
sort($logTables);
$assert($logTables === ['setting', 'site_security_events', 'site_visit_daily_stats', 'site_visit_events', 'user_email_log'], 'Retained log table set changed');
foreach (['access_log', 'dns_rule', 'node_access_api_log', 'node_network_issues', 'node_network_samples', 'node_network_status', 'online_user', 'ss_node_backend', 'ss_node_info', 'ss_node_online_log', 'user_apple_copy_log', 'user_subscribe_log', 'user_tcp_daily_stats', 'user_tcp_log', 'user_traffic_daily_stats', 'user_traffic_log'] as $table) {
$assert(!str_contains($logSchema, '`' . $table . '`'), 'Retired log table is still referenced: ' . $table);
}
preg_match_all('/ALTER\s+TABLE\s+`([^`]+)`/i', $logSchema, $logAlterMatches);
foreach ($logAlterMatches[1] as $table) {
$assert(in_array($table, $logTables, true), 'Log schema alters an undefined table: ' . $table);
}
preg_match_all('/CREATE\s+TRIGGER\s+`[^`]+`\s+(?:BEFORE|AFTER)\s+(?:INSERT|UPDATE|DELETE)\s+ON\s+`([^`]+)`/i', $logSchema, $logTriggerMatches);
foreach ($logTriggerMatches[1] as $table) {
$assert(in_array($table, $logTables, true), 'Log schema trigger targets an undefined table: ' . $table);
}
$manualFixture = $root . '/tests/manual/admin_sidebar_fixture.php';
$assert(is_file($manualFixture)
&& !is_file($root . '/tests/manual/admin_sidebar_fixture.py'),
'The administrator sidebar regression fixture was not migrated from Python to PHP.');
$pythonSources = [];
$iterator = new RecursiveIteratorIterator(
new RecursiveCallbackFilterIterator(
new RecursiveDirectoryIterator($repository,
FilesystemIterator::SKIP_DOTS),
static function (SplFileInfo $entry): bool {
if (!$entry->isDir()) {
return true;
}
return !in_array($entry->getFilename(), [
'.git', 'vendor', '__pycache__',
], true);
}
)
);
foreach ($iterator as $entry) {
if ($entry->isFile() && strtolower($entry->getExtension()) === 'py') {
$pythonSources[] = substr($entry->getPathname(), strlen($repository) + 1);
}
}
$assert($pythonSources === [], 'Python source remains in the clean-break project: '
. implode(', ', $pythonSources));
echo "Pruned project routes/configuration/schema contracts passed.\n";