|
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
use App\Controllers\Admin\ShopController;
|
|
|
|
|
use App\Services\AdminPermission;
|
|
|
|
|
use Illuminate\Database\Capsule\Manager as Capsule;
|
|
|
|
|
use Illuminate\Database\Schema\Blueprint;
|
|
|
|
|
|
|
|
|
|
require dirname(__DIR__) . '/vendor/autoload.php';
|
|
|
|
|
|
|
|
|
|
final class AdminOfferWorkflowRequest
|
|
|
|
|
{
|
|
|
|
|
public function __construct(private array $parameters)
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function getParam(string $name, $default = null)
|
|
|
|
|
{
|
|
|
|
|
return array_key_exists($name, $this->parameters)
|
|
|
|
|
? $this->parameters[$name] : $default;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
final class AdminOfferWorkflowResponse
|
|
|
|
|
{
|
|
|
|
|
public array $payload = [];
|
|
|
|
|
public int $status = 200;
|
|
|
|
|
|
|
|
|
|
public function withJson(array $payload, int $status = 200): self
|
|
|
|
|
{
|
|
|
|
|
$copy = clone $this;
|
|
|
|
|
$copy->payload = $payload;
|
|
|
|
|
$copy->status = $status;
|
|
|
|
|
return $copy;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$assert = static function (bool $condition, string $message): void {
|
|
|
|
|
if (!$condition) {
|
|
|
|
|
throw new RuntimeException($message);
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
$capsule = new Capsule();
|
|
|
|
|
$capsule->addConnection([
|
|
|
|
|
'driver' => 'sqlite',
|
|
|
|
|
'database' => ':memory:',
|
|
|
|
|
], 'default');
|
|
|
|
|
$capsule->setAsGlobal();
|
|
|
|
|
$capsule->bootEloquent();
|
|
|
|
|
$schema = $capsule->schema('default');
|
|
|
|
|
|
|
|
|
|
$schema->create('configurations', function (Blueprint $table): void {
|
|
|
|
|
$table->increments('id');
|
|
|
|
|
$table->string('name')->unique();
|
|
|
|
|
$table->text('value');
|
|
|
|
|
});
|
|
|
|
|
$schema->create('shop', function (Blueprint $table): void {
|
|
|
|
|
$table->increments('id');
|
|
|
|
|
$table->string('name');
|
|
|
|
|
$table->integer('status');
|
|
|
|
|
});
|
|
|
|
|
$schema->create('cloud_regions', function (Blueprint $table): void {
|
|
|
|
|
$table->increments('id');
|
|
|
|
|
$table->string('name');
|
|
|
|
|
$table->string('status');
|
|
|
|
|
$table->integer('sort')->default(0);
|
|
|
|
|
});
|
|
|
|
|
$schema->create('cloud_network_lines', function (Blueprint $table): void {
|
|
|
|
|
$table->increments('id');
|
|
|
|
|
$table->integer('region_id');
|
|
|
|
|
$table->string('name');
|
|
|
|
|
$table->boolean('is_default');
|
|
|
|
|
$table->string('status');
|
|
|
|
|
$table->integer('sort')->default(0);
|
|
|
|
|
});
|
|
|
|
|
$schema->create('cloud_resource_pools', function (Blueprint $table): void {
|
|
|
|
|
$table->increments('id');
|
|
|
|
|
$table->integer('line_id');
|
|
|
|
|
$table->string('name');
|
|
|
|
|
$table->string('status');
|
|
|
|
|
$table->integer('sort')->default(0);
|
|
|
|
|
});
|
|
|
|
|
$schema->create('cloud_offers', function (Blueprint $table): void {
|
|
|
|
|
$table->increments('id');
|
|
|
|
|
$table->integer('plan_id');
|
|
|
|
|
$table->integer('resource_pool_id');
|
|
|
|
|
$table->string('code')->unique();
|
|
|
|
|
$table->string('name');
|
|
|
|
|
$table->decimal('price', 12, 2);
|
|
|
|
|
$table->decimal('quarterly_price', 12, 2);
|
|
|
|
|
$table->decimal('annual_price', 12, 2);
|
|
|
|
|
$table->decimal('backup_price', 12, 2)->default(0);
|
|
|
|
|
$table->decimal('backup_quarterly_price', 12, 2)->default(0);
|
|
|
|
|
$table->decimal('backup_annual_price', 12, 2)->default(0);
|
|
|
|
|
$table->string('currency');
|
|
|
|
|
$table->integer('price_version');
|
|
|
|
|
$table->integer('revision');
|
|
|
|
|
$table->boolean('status');
|
|
|
|
|
$table->integer('stock');
|
|
|
|
|
$table->integer('sort');
|
|
|
|
|
$table->integer('created_at');
|
|
|
|
|
$table->integer('updated_at');
|
|
|
|
|
$table->unique(['plan_id', 'resource_pool_id']);
|
|
|
|
|
});
|
|
|
|
|
$schema->create('cloud_instances', function (Blueprint $table): void {
|
|
|
|
|
$table->increments('id');
|
|
|
|
|
$table->integer('offer_id')->nullable();
|
|
|
|
|
});
|
|
|
|
|
$schema->create('cloud_invoices', function (Blueprint $table): void {
|
|
|
|
|
$table->increments('id');
|
|
|
|
|
$table->string('purpose');
|
|
|
|
|
$table->integer('offer_id')->nullable();
|
|
|
|
|
});
|
|
|
|
|
$schema->create('cloud_jobs', function (Blueprint $table): void {
|
|
|
|
|
$table->increments('id');
|
|
|
|
|
$table->integer('instance_id')->nullable();
|
|
|
|
|
$table->integer('invoice_id')->nullable();
|
|
|
|
|
$table->string('kind');
|
|
|
|
|
$table->string('execution_mode')->default('agent');
|
|
|
|
|
$table->string('phase');
|
|
|
|
|
$table->string('outcome')->nullable();
|
|
|
|
|
$table->string('status')->default('active');
|
|
|
|
|
$table->integer('dispatch_attempts')->default(0);
|
|
|
|
|
$table->string('error_code')->nullable();
|
|
|
|
|
$table->boolean('effect_may_have_applied')->nullable();
|
|
|
|
|
$table->boolean('safe_to_resubmit')->nullable();
|
|
|
|
|
$table->string('effect_resolution')->default('not_applicable');
|
|
|
|
|
$table->boolean('finalization_incident')->default(false);
|
|
|
|
|
$table->string('finalization_error_code')->nullable();
|
|
|
|
|
$table->integer('host_id')->nullable();
|
|
|
|
|
$table->integer('attempts')->default(0);
|
|
|
|
|
$table->string('operation_id')->nullable();
|
|
|
|
|
$table->string('request_fingerprint')->nullable();
|
|
|
|
|
$table->string('request_idempotency_key')->nullable();
|
|
|
|
|
$table->integer('dispatch_started_at')->nullable();
|
|
|
|
|
$table->integer('agent_accepted_at')->nullable();
|
|
|
|
|
$table->integer('dispatched_at')->nullable();
|
|
|
|
|
$table->integer('dispatch_deadline_at')->nullable();
|
|
|
|
|
$table->integer('receipt_deadline_at')->nullable();
|
|
|
|
|
$table->string('completion_event_id')->nullable();
|
|
|
|
|
$table->text('result_json')->nullable();
|
|
|
|
|
$table->integer('terminal_at')->nullable();
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
$connection = $capsule->connection('default');
|
|
|
|
|
$connection->table('configurations')->insert([
|
|
|
|
|
'name' => 'cloud_network_lines_enabled',
|
|
|
|
|
'value' => 'false',
|
|
|
|
|
]);
|
|
|
|
|
$connection->table('shop')->insert([
|
|
|
|
|
['id' => 1, 'name' => '标准云服务器', 'status' => 1],
|
|
|
|
|
['id' => 2, 'name' => '高防云服务器', 'status' => 1],
|
|
|
|
|
]);
|
|
|
|
|
$connection->table('cloud_regions')->insert([
|
|
|
|
|
'id' => 1, 'name' => '东京', 'status' => 'active',
|
|
|
|
|
]);
|
|
|
|
|
$connection->table('cloud_network_lines')->insert([
|
|
|
|
|
['id' => 1, 'region_id' => 1, 'name' => '默认线路',
|
|
|
|
|
'is_default' => 1, 'status' => 'active'],
|
|
|
|
|
['id' => 2, 'region_id' => 1, 'name' => '高防线路',
|
|
|
|
|
'is_default' => 0, 'status' => 'active'],
|
|
|
|
|
]);
|
|
|
|
|
$connection->table('cloud_resource_pools')->insert([
|
|
|
|
|
['id' => 1, 'line_id' => 1, 'name' => '通用资源池', 'status' => 'active'],
|
|
|
|
|
['id' => 2, 'line_id' => 2, 'name' => '高防资源池', 'status' => 'active'],
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
$permission = new ReflectionClass(AdminPermission::class);
|
|
|
|
|
foreach (['resolved' => true, 'canManage' => true] as $property => $value) {
|
|
|
|
|
$reflectionProperty = $permission->getProperty($property);
|
|
|
|
|
$reflectionProperty->setValue(null, $value);
|
|
|
|
|
}
|
|
|
|
|
$controller = (new ReflectionClass(ShopController::class))
|
|
|
|
|
->newInstanceWithoutConstructor();
|
|
|
|
|
$response = new AdminOfferWorkflowResponse();
|
|
|
|
|
$poolOptions = (new ReflectionClass(ShopController::class))
|
|
|
|
|
->getMethod('poolOptions');
|
|
|
|
|
$hiddenLineOptions = $poolOptions->invoke($controller, false, null);
|
|
|
|
|
$currentHiddenLineOptions = $poolOptions->invoke($controller, false, 2);
|
|
|
|
|
$visibleLineOptions = $poolOptions->invoke($controller, true, null);
|
|
|
|
|
$assert(count($hiddenLineOptions) === 1
|
|
|
|
|
&& $hiddenLineOptions[0]['id'] === 1
|
|
|
|
|
&& $hiddenLineOptions[0]['label'] === '东京 → 通用资源池'
|
|
|
|
|
&& count($currentHiddenLineOptions) === 2
|
|
|
|
|
&& $currentHiddenLineOptions[1]['active'] === false
|
|
|
|
|
&& $currentHiddenLineOptions[1]['availability_reason'] === '线路选择已关闭'
|
|
|
|
|
&& count($visibleLineOptions) === 2
|
|
|
|
|
&& $visibleLineOptions[1]['label'] === '东京 → 高防线路 → 高防资源池',
|
|
|
|
|
'Offer location options did not follow the line-feature visibility and eligibility contract.');
|
|
|
|
|
$baseInput = [
|
|
|
|
|
'resource_pool_id' => '1',
|
|
|
|
|
'name' => '东京标准云服务器',
|
|
|
|
|
'price' => '19.90',
|
|
|
|
|
'quarterly_price' => '56.70',
|
|
|
|
|
'annual_price' => '199.00',
|
|
|
|
|
'backup_price' => '3.50',
|
|
|
|
|
'backup_quarterly_price' => '9.50',
|
|
|
|
|
'backup_annual_price' => '35.00',
|
|
|
|
|
'stock' => '-1',
|
|
|
|
|
'status' => '1',
|
|
|
|
|
'sort' => '0',
|
|
|
|
|
'revision' => '0',
|
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
$created = $controller->offerAdd(
|
|
|
|
|
new AdminOfferWorkflowRequest($baseInput), $response, ['id' => 1]
|
|
|
|
|
);
|
|
|
|
|
$stored = $connection->table('cloud_offers')->where('id', 1)->first();
|
|
|
|
|
$assert($created->status === 200 && ($created->payload['ret'] ?? 0) === 1
|
|
|
|
|
&& $stored !== null
|
|
|
|
|
&& (string) $stored->backup_price === '3.5'
|
|
|
|
|
&& preg_match('/^offer-[a-f0-9]{32}$/D', (string) $stored->code) === 1,
|
|
|
|
|
'Creating an offer without a code did not persist an opaque system identifier.');
|
|
|
|
|
$originalCode = (string) $stored->code;
|
|
|
|
|
|
|
|
|
|
$customCode = $controller->offerAdd(
|
|
|
|
|
new AdminOfferWorkflowRequest($baseInput + ['code' => 'operator-code']),
|
|
|
|
|
$response,
|
|
|
|
|
['id' => 2]
|
|
|
|
|
);
|
|
|
|
|
$assert($customCode->status === 422
|
|
|
|
|
&& ($customCode->payload['ret'] ?? 1) === 0
|
|
|
|
|
&& $connection->table('cloud_offers')->count() === 1,
|
|
|
|
|
'Create accepted an operator-supplied system identifier.');
|
|
|
|
|
|
|
|
|
|
$tampered = $controller->offerUpdate(
|
|
|
|
|
new AdminOfferWorkflowRequest(array_replace($baseInput, [
|
|
|
|
|
'name' => '被篡改的名称',
|
|
|
|
|
'revision' => '1',
|
|
|
|
|
'code' => 'replacement',
|
|
|
|
|
])),
|
|
|
|
|
$response,
|
|
|
|
|
['id' => 1, 'offer_id' => 1]
|
|
|
|
|
);
|
|
|
|
|
$afterTamper = $connection->table('cloud_offers')->where('id', 1)->first();
|
|
|
|
|
$assert($tampered->status === 409
|
|
|
|
|
&& ($tampered->payload['ret'] ?? 1) === 0
|
|
|
|
|
&& (string) $afterTamper->code === $originalCode
|
|
|
|
|
&& (string) $afterTamper->name === '东京标准云服务器',
|
|
|
|
|
'PUT accepted a system-identifier field or partially mutated the offer.');
|
|
|
|
|
|
|
|
|
|
$updated = $controller->offerUpdate(
|
|
|
|
|
new AdminOfferWorkflowRequest(array_replace($baseInput, [
|
|
|
|
|
'name' => '东京标准云服务器新名称',
|
|
|
|
|
'revision' => '1',
|
|
|
|
|
])),
|
|
|
|
|
$response,
|
|
|
|
|
['id' => 1, 'offer_id' => 1]
|
|
|
|
|
);
|
|
|
|
|
$afterUpdate = $connection->table('cloud_offers')->where('id', 1)->first();
|
|
|
|
|
$assert($updated->status === 200 && ($updated->payload['ret'] ?? 0) === 1
|
|
|
|
|
&& (string) $afterUpdate->code === $originalCode
|
|
|
|
|
&& (string) $afterUpdate->name === '东京标准云服务器新名称'
|
|
|
|
|
&& (int) $afterUpdate->price_version === 1
|
|
|
|
|
&& (int) $afterUpdate->revision === 2,
|
|
|
|
|
'Updating without a code did not preserve the immutable identifier.');
|
|
|
|
|
|
|
|
|
|
$backupPriceUpdated = $controller->offerUpdate(
|
|
|
|
|
new AdminOfferWorkflowRequest(array_replace($baseInput, [
|
|
|
|
|
'name' => '东京标准云服务器新名称',
|
|
|
|
|
'backup_price' => '4.25',
|
|
|
|
|
'revision' => '2',
|
|
|
|
|
])),
|
|
|
|
|
$response,
|
|
|
|
|
['id' => 1, 'offer_id' => 1]
|
|
|
|
|
);
|
|
|
|
|
$afterBackupPriceUpdate = $connection->table('cloud_offers')
|
|
|
|
|
->where('id', 1)->first();
|
|
|
|
|
$assert($backupPriceUpdated->status === 200
|
|
|
|
|
&& (int) $afterBackupPriceUpdate->price_version === 2
|
|
|
|
|
&& (int) $afterBackupPriceUpdate->revision === 3
|
|
|
|
|
&& (string) $afterBackupPriceUpdate->backup_price === '4.25',
|
|
|
|
|
'Changing the automatic-backup price did not advance the shared price version.');
|
|
|
|
|
|
|
|
|
|
$connection->table('cloud_invoices')->insert([
|
|
|
|
|
'id' => 1, 'purpose' => 'upgrade', 'offer_id' => 1,
|
|
|
|
|
]);
|
|
|
|
|
$connection->table('cloud_jobs')->insert([
|
|
|
|
|
'invoice_id' => 1, 'kind' => 'instance_upgrade',
|
|
|
|
|
'phase' => 'accepted', 'status' => 'accepted',
|
|
|
|
|
]);
|
|
|
|
|
$blockedModeSwitch = $controller->offerUpdate(
|
|
|
|
|
new AdminOfferWorkflowRequest(array_replace($baseInput, [
|
|
|
|
|
'name' => '不应保存的有限库存',
|
|
|
|
|
'stock' => '5',
|
|
|
|
|
'backup_price' => '4.25',
|
|
|
|
|
'revision' => '3',
|
|
|
|
|
])),
|
|
|
|
|
$response,
|
|
|
|
|
['id' => 1, 'offer_id' => 1]
|
|
|
|
|
);
|
|
|
|
|
$afterBlockedModeSwitch = $connection->table('cloud_offers')
|
|
|
|
|
->where('id', 1)->first();
|
|
|
|
|
$assert($blockedModeSwitch->status === 409
|
|
|
|
|
&& ($blockedModeSwitch->payload['ret'] ?? 1) === 0
|
|
|
|
|
&& (int) $afterBlockedModeSwitch->stock === -1
|
|
|
|
|
&& (int) $afterBlockedModeSwitch->revision === 3,
|
|
|
|
|
'An active upgrade allowed its target offer stock mode to change.');
|
|
|
|
|
|
|
|
|
|
$nonDefault = $controller->offerAdd(
|
|
|
|
|
new AdminOfferWorkflowRequest(array_replace($baseInput, [
|
|
|
|
|
'resource_pool_id' => '2',
|
|
|
|
|
'name' => '东京高防云服务器',
|
|
|
|
|
])),
|
|
|
|
|
$response,
|
|
|
|
|
['id' => 2]
|
|
|
|
|
);
|
|
|
|
|
$assert($nonDefault->status === 422
|
|
|
|
|
&& ($nonDefault->payload['ret'] ?? 1) === 0
|
|
|
|
|
&& $connection->table('cloud_offers')->count() === 1,
|
|
|
|
|
'Hidden-line mode allowed an offer on a non-default line.');
|
|
|
|
|
|
|
|
|
|
echo "Administrator offer workflow passed.\n";
|