diff --git a/web/tests/CloudAutomaticBackupBillingTest.php b/web/tests/CloudAutomaticBackupBillingTest.php index 00d5b24d..d1b65c4e 100644 --- a/web/tests/CloudAutomaticBackupBillingTest.php +++ b/web/tests/CloudAutomaticBackupBillingTest.php @@ -156,6 +156,21 @@ $schema->create('cloud_invoice_payments', static function (Blueprint $table): vo $table->integer('user_id'); $table->string('status'); }); +$schema->create('cloud_mail_outbox', static function (Blueprint $table): void { + $table->increments('id'); + $table->integer('invoice_id'); + $table->string('kind'); + $table->string('status'); + $table->integer('attempts'); + $table->integer('available_at'); + $table->string('lease_token')->nullable(); + $table->integer('lease_until'); + $table->integer('sent_at')->nullable(); + $table->text('last_error'); + $table->integer('created_at'); + $table->integer('updated_at'); + $table->unique(['invoice_id', 'kind']); +}); $schema->create('cloud_jobs', static function (Blueprint $table): void { $table->increments('id'); $table->integer('invoice_id')->nullable(); @@ -384,6 +399,16 @@ $assert($capsule->table('cloud_backup_subscriptions') $assert($capsule->table('cloud_invoices')->where('id', $renewalInvoiceId) ->value('status') === 'void', 'Disabling did not void the stale unpaid backup renewal invoice.'); +$replacementRenewal = $capsule->table('cloud_invoices') + ->where('instance_id', 11) + ->where('purpose', 'renewal') + ->where('status', '<>', 'void') + ->orderByDesc('id') + ->first(); +$assert($replacementRenewal === null + || ((int) $replacementRenewal->renews_expires_at === $endsAt + && (int) $replacementRenewal->due_at === $endsAt), + 'Backup policy change moved the frozen renewal grace anchor.'); $invoiceCountBeforeReenable = $capsule->table('cloud_invoices')->count(); $reenableActor = [ @@ -598,4 +623,56 @@ $assert($rejected && (string) $poorUser->money === '4.99' ->where('instance_id', 13)->count() === 0, 'Insufficient balance left partial automatic-backup billing state.'); +// Once the service period has expired, the public disable flow is rejected by +// the final billing quote. Every earlier subscription/invoice mutation must +// therefore roll back; in particular it must not replace the fixed renewal +// deadline with a new three-day window starting at the request time. +$expiredAt = $now - 60; +$capsule->table('cloud_instances')->where('id', 11)->update([ + 'expires_at' => $expiredAt, +]); +$capsule->table('cloud_backup_subscriptions')->where('instance_id', 11)->update([ + 'enabled' => 1, + 'renew_next_period' => 1, + 'state_revision' => 5, + 'updated_at' => $now, +]); +$fixedRenewalId = $capsule->table('cloud_invoices')->insertGetId([ + 'invoice_no' => 'RENEW-BACKUP-EXPIRED-1', + 'purpose' => 'renewal', 'instance_id' => 11, 'order_id' => null, + 'user_id' => 7, 'plan_id' => null, 'offer_id' => 9, + 'renewal_id' => null, 'renews_expires_at' => $expiredAt, + 'due_at' => $expiredAt, 'amount' => '30.00', 'currency' => 'USD', + 'validity_days' => 30, 'traffic_quota_bytes' => 0, + 'status' => 'open', 'paid_via' => null, 'payment_reference' => null, + 'paid_at' => null, 'created_at' => $expiredAt, 'updated_at' => $now, +]); +$expiredDisableRejected = false; +try { + CloudAutomaticBackupBillingService::disable( + CloudInstance::find(11), + User::find(7), + ['user_id' => 7], + $now + ); +} catch (CloudError $error) { + $expiredDisableRejected = $error->httpStatus === 409; +} +$fixedRenewalAfter = $capsule->table('cloud_invoices') + ->where('instance_id', 11) + ->where('purpose', 'renewal') + ->where('status', '<>', 'void') + ->get(); +$assert($expiredDisableRejected + && $capsule->table('cloud_backup_subscriptions') + ->where('instance_id', 11) + ->where('enabled', 1) + ->where('renew_next_period', 1) + ->where('state_revision', 5) + ->count() === 1 + && $fixedRenewalAfter->count() === 1 + && (int) $fixedRenewalAfter->first()->id === $fixedRenewalId + && (int) $fixedRenewalAfter->first()->due_at === $expiredAt, + 'Rejected expired backup disable changed the frozen renewal deadline.'); + echo "Automatic backup mid-period billing tests passed.\n";