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.
54 lines
2.9 KiB
PHP
54 lines
2.9 KiB
PHP
<?php
|
|
|
|
namespace App\Models {
|
|
// This focused test intentionally has no database or application bootstrap.
|
|
class Model {}
|
|
require dirname(__DIR__) . '/app/Models/Shop.php';
|
|
}
|
|
|
|
namespace {
|
|
use App\Models\Shop;
|
|
|
|
$assert = static function ($condition, $message) {
|
|
if (!$condition) {
|
|
throw new \RuntimeException($message);
|
|
}
|
|
};
|
|
$input = ['name' => 'Cloud starter', 'price' => '19.90', 'cpu_cores' => '2', 'memory_mib' => '2048',
|
|
'bandwidth_mbps' => '100', 'disk_gib' => '20', 'validity_days' => '30', 'traffic_gib' => '1024',
|
|
'status' => '1', 'stock' => '-1', 'sort' => '0'];
|
|
$data = Shop::validatePlan($input);
|
|
$assert($data['cpu_cores'] === 2 && $data['disk_gib'] === 20 && $data['price'] === '19.90'
|
|
&& $data['validity_days'] === 30 && $data['traffic_quota_bytes'] === 1024 * Shop::GIB,
|
|
'Plan values were not normalized.');
|
|
$assert(!array_key_exists('class', $data) && !array_key_exists('expire', $data)
|
|
&& !array_key_exists('traffic_gib', $data), 'A cloud plan contains legacy or noncanonical entitlements.');
|
|
foreach (['cpu_cores' => 0, 'memory_mib' => 511, 'disk_gib' => 4097, 'bandwidth_mbps' => 0,
|
|
'validity_days' => 0, 'traffic_gib' => -1, 'price' => '1.999', 'status' => 2,
|
|
'stock' => -2, 'name' => ''] as $key => $invalid) {
|
|
try {
|
|
Shop::validatePlan(array_replace($input, [$key => $invalid]));
|
|
throw new \RuntimeException('Invalid plan value accepted: ' . $key);
|
|
} catch (\InvalidArgumentException $exception) {
|
|
// Expected.
|
|
}
|
|
}
|
|
$source = file_get_contents(dirname(__DIR__) . '/app/Controllers/UserController.php');
|
|
$start = strpos($source, 'public function buy(');
|
|
$end = strpos($source, 'public function bought(', $start);
|
|
$body = substr($source, $start, $end - $start);
|
|
$assert(strpos($body, '409') !== false && strpos($body, 'save(') === false && strpos($body, 'transaction(') === false,
|
|
'Purchase must not debit or create an order before virtualization integration.');
|
|
$controller = file_get_contents(dirname(__DIR__) . '/app/Controllers/CloudApiController.php');
|
|
$client = file_get_contents(dirname(__DIR__) . '/public/cloud/app.js');
|
|
$assert(str_contains($controller, '\'can_mutate\' => $owner || $global')
|
|
&& str_contains($controller, '\'can_view_credential\' => $owner || $global')
|
|
&& str_contains($client, '!vm.can_view_credential')
|
|
&& str_contains($client, 'danger.closest(\'section\').hidden = !vm.can_mutate'),
|
|
'Readonly administrators were offered another tenant\'s credentials or VM mutation controls.');
|
|
$assert(str_contains($controller, '$suffix === \'/rescue/exit\'')
|
|
&& str_contains($controller, "['blocking', 'blocked_traffic', 'block_failed']"),
|
|
'Rescue exit does not preserve the authoritative traffic suspension target.');
|
|
echo 'Cloud plan definition checks passed.' . PHP_EOL;
|
|
}
|