Admin subscriptions: batch mark expired with safety guards

This commit is contained in:
萝卜
2026-03-17 00:27:04 +08:00
parent 0e8a9797b9
commit 7b143e1a11
6 changed files with 271 additions and 1 deletions

View File

@@ -0,0 +1,99 @@
<?php
namespace Tests\Feature;
use App\Models\Merchant;
use App\Models\Plan;
use App\Models\SiteSubscription;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Tests\TestCase;
class AdminSiteSubscriptionsBatchMarkExpiredShouldMarkExpiredOnlyWhenEndsAtPastTest extends TestCase
{
use RefreshDatabase;
protected function loginAsPlatformAdmin(): void
{
$this->seed();
$this->post('/admin/login', [
'email' => 'platform.admin@demo.local',
'password' => 'Platform@123456',
])->assertRedirect('/admin');
}
public function test_batch_mark_expired_should_only_update_subscriptions_with_ends_at_past(): void
{
$this->loginAsPlatformAdmin();
$merchant = Merchant::query()->firstOrFail();
$plan = Plan::query()->create([
'code' => 'sub_batch_mark_expired_success_plan',
'name' => '订阅批量标记过期(成功)测试套餐',
'billing_cycle' => 'monthly',
'price' => 1,
'list_price' => 1,
'status' => 'active',
'sort' => 10,
'published_at' => now(),
]);
$expiredA = SiteSubscription::query()->create([
'merchant_id' => $merchant->id,
'plan_id' => $plan->id,
'status' => 'activated',
'source' => 'manual',
'subscription_no' => 'SUB_BATCH_EXPIRED_OK_0001',
'plan_name' => $plan->name,
'billing_cycle' => $plan->billing_cycle,
'period_months' => 1,
'amount' => 1,
'starts_at' => now()->subDays(40),
'ends_at' => now()->subDays(1),
'activated_at' => now()->subDays(40),
]);
$expiredAlready = SiteSubscription::query()->create([
'merchant_id' => $merchant->id,
'plan_id' => $plan->id,
'status' => 'expired',
'source' => 'manual',
'subscription_no' => 'SUB_BATCH_EXPIRED_ALREADY_0001',
'plan_name' => $plan->name,
'billing_cycle' => $plan->billing_cycle,
'period_months' => 1,
'amount' => 1,
'starts_at' => now()->subDays(70),
'ends_at' => now()->subDays(10),
'activated_at' => now()->subDays(70),
]);
$notExpired = SiteSubscription::query()->create([
'merchant_id' => $merchant->id,
'plan_id' => $plan->id,
'status' => 'activated',
'source' => 'manual',
'subscription_no' => 'SUB_BATCH_EXPIRED_SKIP_0001',
'plan_name' => $plan->name,
'billing_cycle' => $plan->billing_cycle,
'period_months' => 1,
'amount' => 1,
'starts_at' => now()->subDays(5),
'ends_at' => now()->addDays(5),
'activated_at' => now()->subDays(5),
]);
$this->post('/admin/site-subscriptions/batch-mark-expired', [
'expiry' => 'expired',
'confirm' => 'YES',
])->assertRedirect()->assertSessionHas('success');
$expiredA->refresh();
$expiredAlready->refresh();
$notExpired->refresh();
$this->assertSame('expired', (string) $expiredA->status);
$this->assertSame('expired', (string) $expiredAlready->status);
$this->assertSame('activated', (string) $notExpired->status);
}
}