addConnection([ 'driver' => 'sqlite', 'database' => ':memory:', ], 'default'); $capsule->setAsGlobal(); $capsule->bootEloquent(); $schema = $capsule->schema('default'); $schema->create('cloud_instances', static function (Blueprint $table): void { $table->increments('id'); $table->integer('user_id'); $table->integer('offer_id'); $table->string('name'); $table->string('status'); $table->string('service_status'); $table->string('renewal_price')->nullable(); $table->text('spec_json'); }); $schema->create('cloud_backup_subscriptions', static function (Blueprint $table): void { $table->increments('id'); $table->integer('instance_id')->unique(); $table->integer('user_id'); $table->integer('offer_id'); $table->boolean('enabled'); $table->boolean('renew_next_period'); $table->string('frequency'); $table->integer('weekday'); $table->integer('retention_count'); $table->string('timezone'); $table->decimal('price', 12, 2); $table->string('currency'); $table->integer('price_version'); $table->integer('next_run_at'); $table->integer('last_scheduled_at')->nullable(); $table->integer('last_backup_id')->nullable(); $table->integer('created_at'); $table->integer('updated_at'); }); $schema->create('cloud_backup_period_entitlements', static function (Blueprint $table): void { $table->increments('id'); $table->integer('subscription_id'); $table->integer('service_period_id')->unique(); $table->integer('instance_id'); $table->integer('user_id'); $table->integer('starts_at'); $table->integer('ends_at'); }); $schema->create('cloud_invoices', static function (Blueprint $table): void { $table->increments('id'); $table->string('purpose'); $table->string('amount'); }); $schema->create('cloud_invoice_items', static function (Blueprint $table): void { $table->increments('id'); $table->integer('invoice_id'); $table->integer('order_item_id')->nullable(); $table->string('code'); $table->string('label'); $table->string('amount'); $table->string('currency'); $table->text('snapshot_json'); $table->integer('created_at'); }); $schema->create('cloud_instance_backups', static function (Blueprint $table): void { $table->increments('id'); $table->integer('source_instance_id'); $table->integer('user_id'); $table->string('purpose'); $table->string('source'); $table->string('state'); $table->string('name'); $table->string('consistency'); $table->integer('object_size_bytes'); $table->integer('virtual_size_bytes'); $table->integer('scheduled_for')->nullable(); $table->integer('ready_at')->nullable(); $table->integer('created_at'); $table->integer('updated_at'); }); $schema->create('cloud_deployment_snapshots', static function (Blueprint $table): void { $table->increments('id'); $table->integer('backup_id')->unique(); $table->string('state'); }); $instance = new CloudInstance(); $instance->id = 1; $instance->user_id = 9; $instance->offer_id = 3; $instance->name = 'paid-backup-vm'; $instance->status = 'active'; $instance->service_status = 'active'; $instance->renewal_price = '10.00'; $instance->spec_json = json_encode([ 'automatic_backup' => true, 'instance_price' => '10.00', 'backup_price' => '2.50', 'price_version' => 4, 'currency' => 'USD', ], JSON_THROW_ON_ERROR); $instance->save(); $subscription = CloudAutomaticBackupService::activatePurchased($instance); $assert($subscription !== null && (bool) $subscription->enabled && (bool) $subscription->renew_next_period && (string) $subscription->frequency === 'daily' && (int) $subscription->retention_count === 2 && (string) $subscription->timezone === 'UTC' && (string) $subscription->price === '2.50', 'Paid provisioning did not freeze the default backup subscription.'); // The commercial spec is historical after provisioning. Current access is // proven by the immutable paid service-period entitlement. $instance->spec_json = json_encode([ 'automatic_backup' => false, 'instance_price' => '10.00', 'backup_price' => '2.50', 'price_version' => 4, 'currency' => 'USD', ], JSON_THROW_ON_ERROR); $instance->save(); $capsule->table('cloud_backup_period_entitlements')->insert([ 'subscription_id' => (int) $subscription->id, 'service_period_id' => 31, 'instance_id' => (int) $instance->id, 'user_id' => (int) $instance->user_id, 'starts_at' => time() - 3600, 'ends_at' => time() + 86400, ]); $updated = CloudAutomaticBackupService::update($instance, [ 'user_id' => 9, ], [ 'frequency' => 'weekly', 'weekday' => 3, 'retention_count' => 1, ]); $assert($updated['entitled'] === true && $updated['subscription']['enabled'] === true && $updated['subscription']['frequency'] === 'weekly' && $updated['subscription']['weekday'] === 3 && $updated['subscription']['retention_count'] === 1, 'Schedule-only PUT did not preserve the paid enabled state.'); foreach (['enabled' => false, 'renew_next_period' => false] as $field => $value) { $rejected = false; try { CloudAutomaticBackupService::update($instance, [ 'user_id' => 9, ], [ 'frequency' => 'weekly', 'weekday' => 3, 'retention_count' => 1, $field => $value, ]); } catch (CloudError $error) { $rejected = true; } $assert($rejected, 'User schedule API accepted hidden billing control: ' . $field); } $subscription->refresh(); $assert((bool) $subscription->enabled && (bool) $subscription->renew_next_period, 'Rejected hidden billing controls changed the paid subscription.'); $quote = CloudInvoiceService::renewalQuoteLocked($instance); $assert($quote['instance_cents'] === 1000 && $quote['backup_cents'] === 250 && $quote['total_cents'] === 1250 && $quote['backup_subscription_id'] === (int) $subscription->id, 'Renewal quote did not combine the base instance and backup price.'); $invoice = new CloudInvoice(); $invoice->purpose = 'renewal'; $invoice->amount = '12.50'; $invoice->save(); $capsule->table('cloud_invoice_items')->insert([ [ 'invoice_id' => (int) $invoice->id, 'order_item_id' => null, 'code' => 'instance', 'label' => '实例套餐续费', 'amount' => '10.00', 'currency' => 'USD', 'snapshot_json' => '{}', 'created_at' => time(), ], [ 'invoice_id' => (int) $invoice->id, 'order_item_id' => null, 'code' => 'automatic_backup', 'label' => '自动备份续费', 'amount' => '2.50', 'currency' => 'USD', 'snapshot_json' => '{}', 'created_at' => time(), ], ]); $assert(CloudInvoiceService::renewalInvoiceMatches($instance, $invoice), 'Renewal invoice did not accept the exact two-item backup quote.'); $subscription->enabled = false; $subscription->save(); $pausedRenewal = CloudInvoiceService::renewalQuoteLocked($instance); $assert($pausedRenewal['total_cents'] === 1250 && $pausedRenewal['backup_cents'] === 250, 'Pausing this-period scheduling incorrectly removed a retained next-period renewal.'); $subscription->renew_next_period = false; $subscription->save(); $withoutBackup = CloudInvoiceService::renewalQuoteLocked($instance); $assert($withoutBackup['instance_cents'] === 1000 && $withoutBackup['backup_cents'] === 0 && $withoutBackup['total_cents'] === 1000 && $withoutBackup['backup_subscription_id'] === null, 'Disabled next-period renewal still charged for automatic backup.'); echo "Automatic backup subscription activation and update tests passed.\n";