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.
46 lines
2.4 KiB
PHP
46 lines
2.4 KiB
PHP
<?php
|
|
|
|
// Static host-location contract: no application boot, database or engine connection.
|
|
$root = dirname(__DIR__);
|
|
$schema = file_get_contents($root . '/sql/cloud.sql');
|
|
$controller = file_get_contents($root . '/app/Controllers/CloudAdminController.php');
|
|
$template = file_get_contents($root . '/resources/views/venix/admin/hosts/index.tpl');
|
|
$client = file_get_contents($root . '/public/cloud/admin.js');
|
|
|
|
$assert = static function ($condition, string $message): void {
|
|
if (!$condition) {
|
|
throw new RuntimeException($message);
|
|
}
|
|
};
|
|
|
|
$assert(str_contains($schema, "country_code CHAR(2) NOT NULL DEFAULT ''")
|
|
&& str_contains($schema,
|
|
"ALTER TABLE cloud_hosts ADD COLUMN IF NOT EXISTS\n country_code CHAR(2) NOT NULL DEFAULT '' AFTER region"),
|
|
'Fresh and upgraded cloud_hosts schemas must contain the structured country code.');
|
|
$assert(str_contains($controller,
|
|
"private const HOST_COUNTRY_CODES = ['CN', 'JP', 'US', 'SG'];")
|
|
&& str_contains($controller, "'region', 'country_code'")
|
|
&& str_contains($controller,
|
|
"unset(\$body['name'], \$body['enabled'], \$body['country_code']);")
|
|
&& str_contains($controller, '$host->country_code = $countryCode;'),
|
|
'Host writes must validate and persist country_code without forwarding it to the engine.');
|
|
$unsetAt = strpos($controller,
|
|
"unset(\$body['name'], \$body['enabled'], \$body['country_code']);");
|
|
$enginePutAt = strpos($controller, "'PUT', 'host', \$actor, \$body");
|
|
$assert($unsetAt !== false && $enginePutAt !== false && $unsetAt < $enginePutAt,
|
|
'country_code must be removed before the strict engine HostBody receives the request.');
|
|
foreach (['CN', 'JP', 'US', 'SG'] as $code) {
|
|
$assert(str_contains($template, '<option value="' . $code . '">'),
|
|
'Host country selector is missing ' . $code . '.');
|
|
}
|
|
$assert(str_contains($template, 'id="host-country-code"')
|
|
&& preg_match('/id="host-region"[^>]*required[^>]*maxlength="64"/', $template) === 1
|
|
&& str_contains($controller, '具体地区需为 1 至 64 字符'),
|
|
'Host editor must require a country and an exact region within the engine limit.');
|
|
$assert(str_contains($client, "'host-country-code': h.country_code || ''")
|
|
&& str_contains($client,
|
|
"country_code: byId('host-country-code').value"),
|
|
'Host editor must round-trip country_code through the admin API.');
|
|
|
|
echo "Structured cloud host country and region contracts passed.\n";
|