Files
saasshop/tests/Feature/AdminPlanShowTest.php

423 lines
14 KiB
PHP

<?php
namespace Tests\Feature;
use App\Models\Merchant;
use App\Models\Plan;
use App\Models\PlatformOrder;
use App\Models\SiteSubscription;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Arr;
use Tests\TestCase;
class AdminPlanShowTest 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_platform_admin_can_open_plan_show_page(): void
{
$this->loginAsPlatformAdmin();
$merchant = Merchant::query()->firstOrFail();
$plan = Plan::query()->create([
'code' => 'plan_show_test',
'name' => '套餐详情测试套餐',
'billing_cycle' => 'monthly',
'price' => 99,
'list_price' => 199,
'status' => 'active',
'sort' => 10,
'description' => '用于验证套餐详情页',
'published_at' => now(),
]);
$subscription = SiteSubscription::query()->create([
'merchant_id' => $merchant->id,
'plan_id' => $plan->id,
'status' => 'activated',
'source' => 'manual',
'subscription_no' => 'SUB_PLAN_SHOW_0001',
'plan_name' => $plan->name,
'billing_cycle' => $plan->billing_cycle,
'period_months' => 1,
'amount' => 99,
'starts_at' => now()->subDay(),
'ends_at' => now()->addDays(5),
'activated_at' => now()->subDay(),
]);
PlatformOrder::query()->create([
'merchant_id' => $merchant->id,
'plan_id' => $plan->id,
'site_subscription_id' => $subscription->id,
'order_no' => 'PO_PLAN_SHOW_0001',
'order_type' => 'renewal',
'status' => 'activated',
'payment_status' => 'paid',
'plan_name' => $plan->name,
'billing_cycle' => $plan->billing_cycle,
'period_months' => 1,
'quantity' => 1,
'list_amount' => 99,
'discount_amount' => 0,
'payable_amount' => 99,
'paid_amount' => 99,
'placed_at' => now()->subHour(),
'paid_at' => now()->subMinutes(50),
'activated_at' => now()->subMinutes(40),
'meta' => [],
]);
$res = $this->get('/admin/plans/' . $plan->id);
$res->assertOk()
->assertSee('套餐详情')
->assertSee('套餐详情测试套餐')
->assertSee('查看已付无回执订单')
->assertSee('查看续费缺订阅订单');
}
public function test_guest_cannot_open_plan_show_page(): void
{
$plan = Plan::query()->create([
'code' => 'plan_show_guest_test',
'name' => '游客不可见套餐',
'billing_cycle' => 'monthly',
'price' => 10,
'list_price' => 10,
'status' => 'active',
'sort' => 10,
]);
$this->get('/admin/plans/' . $plan->id)->assertRedirect('/admin/login');
}
public function test_plan_show_links_to_subscriptions_and_orders_should_contain_back_to_plan_show(): void
{
$this->loginAsPlatformAdmin();
$plan = Plan::query()->create([
'code' => 'plan_show_back_test',
'name' => '套餐详情 back 测试套餐',
'billing_cycle' => 'monthly',
'price' => 88,
'list_price' => 108,
'status' => 'active',
'sort' => 10,
'published_at' => now(),
]);
$res = $this->get('/admin/plans/' . $plan->id);
$res->assertOk();
$back = '/admin/plans/' . $plan->id;
$expectedSubscriptionUrl = '/admin/site-subscriptions?' . Arr::query([
'plan_id' => $plan->id,
'back' => $back,
]);
$expectedOrderUrl = '/admin/platform-orders?' . Arr::query([
'plan_id' => $plan->id,
'back' => $back,
]);
$expectedPaidNoReceiptUrl = '/admin/platform-orders?' . Arr::query([
'plan_id' => $plan->id,
'payment_status' => 'paid',
'receipt_status' => 'none',
'back' => $back,
]);
$expectedRenewalMissingUrl = '/admin/platform-orders?' . Arr::query([
'plan_id' => $plan->id,
'renewal_missing_subscription' => '1',
'back' => $back,
]);
$res->assertSee($expectedSubscriptionUrl, false);
$res->assertSee($expectedOrderUrl, false);
$res->assertSee($expectedPaidNoReceiptUrl, false);
$res->assertSee($expectedRenewalMissingUrl, false);
}
public function test_plan_index_show_link_should_carry_back_to_index_self_without_back(): void
{
$this->loginAsPlatformAdmin();
$plan = Plan::query()->create([
'code' => 'plan_index_show_link_test',
'name' => '套餐列表详情入口测试套餐',
'billing_cycle' => 'monthly',
'price' => 18,
'list_price' => 18,
'status' => 'active',
'sort' => 10,
]);
$res = $this->get('/admin/plans?status=active&back=' . urlencode('/admin/platform-orders'));
$res->assertOk();
$expectedBack = '/admin/plans?' . Arr::query([
'status' => 'active',
]);
$expectedShowUrl = '/admin/plans/' . $plan->id . '?' . Arr::query([
'back' => $expectedBack,
]);
$res->assertSee($expectedShowUrl, false);
$res->assertSee('查看详情');
}
public function test_plan_show_should_drop_unsafe_back_and_not_render_return_to_previous_link(): void
{
$this->loginAsPlatformAdmin();
$plan = Plan::query()->create([
'code' => 'plan_show_unsafe_back_test',
'name' => '套餐详情 unsafe back 测试套餐',
'billing_cycle' => 'monthly',
'price' => 28,
'list_price' => 38,
'status' => 'active',
'sort' => 10,
]);
$unsafeBack = '/admin/plans?status=active&back=/admin/platform-orders';
$res = $this->get('/admin/plans/' . $plan->id . '?back=' . urlencode($unsafeBack));
$res->assertOk();
$res->assertDontSee('返回上一页(保留上下文)');
$res->assertSee('/admin/plans', false);
$res->assertDontSee('back=' . $unsafeBack, false);
}
public function test_plan_show_should_render_safe_back_but_governance_links_should_still_use_plan_show_self_back(): void
{
$this->loginAsPlatformAdmin();
$plan = Plan::query()->create([
'code' => 'plan_show_safe_back_test',
'name' => '套餐详情 safe back 测试套餐',
'billing_cycle' => 'monthly',
'price' => 58,
'list_price' => 68,
'status' => 'active',
'sort' => 10,
]);
$safeBack = '/admin/plans?' . Arr::query([
'status' => 'active',
'keyword' => '治理',
]);
$res = $this->get('/admin/plans/' . $plan->id . '?back=' . urlencode($safeBack));
$res->assertOk();
$res->assertSee('href="' . $safeBack . '"', false);
$res->assertSee('返回上一页(保留上下文)');
$planShowSelf = '/admin/plans/' . $plan->id;
$expectedOrdersUrl = '/admin/platform-orders?' . Arr::query([
'plan_id' => $plan->id,
'back' => $planShowSelf,
]);
$expectedPaidNoReceiptUrl = '/admin/platform-orders?' . Arr::query([
'plan_id' => $plan->id,
'payment_status' => 'paid',
'receipt_status' => 'none',
'back' => $planShowSelf,
]);
$expectedRenewalMissingUrl = '/admin/platform-orders?' . Arr::query([
'plan_id' => $plan->id,
'renewal_missing_subscription' => '1',
'back' => $planShowSelf,
]);
$res->assertSee($expectedOrdersUrl, false);
$res->assertSee($expectedPaidNoReceiptUrl, false);
$res->assertSee($expectedRenewalMissingUrl, false);
$res->assertDontSee('back=' . $safeBack, false);
$res->assertDontSee('back%3D', false);
}
public function test_plan_show_header_actions_should_use_plan_show_self_back_even_when_safe_back_is_present(): void
{
$this->loginAsPlatformAdmin();
$plan = Plan::query()->create([
'code' => 'plan_show_header_cta_back_test',
'name' => '套餐详情顶部动作回链测试套餐',
'billing_cycle' => 'monthly',
'price' => 66,
'list_price' => 88,
'status' => 'active',
'sort' => 10,
]);
$safeBack = '/admin/plans?' . Arr::query([
'status' => 'active',
'billing_cycle' => 'monthly',
]);
$res = $this->get('/admin/plans/' . $plan->id . '?back=' . urlencode($safeBack));
$res->assertOk();
$planShowSelf = '/admin/plans/' . $plan->id;
$expectedEditUrl = '/admin/plans/' . $plan->id . '/edit?' . Arr::query([
'back' => $planShowSelf,
]);
$expectedCreateOrderUrl = '/admin/platform-orders/create?' . Arr::query([
'plan_id' => $plan->id,
'order_type' => 'new_purchase',
'back' => $planShowSelf,
]);
$res->assertSee($expectedEditUrl, false);
$res->assertSee($expectedCreateOrderUrl, false);
$res->assertDontSee('back=' . $safeBack, false);
}
public function test_plan_show_platform_order_governance_links_should_use_self_back_when_outer_back_is_unsafe(): void
{
$this->loginAsPlatformAdmin();
$plan = Plan::query()->create([
'code' => 'plan_show_governance_unsafe_back_test',
'name' => '套餐详情治理入口 unsafe back 测试套餐',
'billing_cycle' => 'monthly',
'price' => 76,
'list_price' => 96,
'status' => 'active',
'sort' => 10,
]);
$unsafeBack = '/admin/plans?status=active&back=/admin/platform-orders';
$res = $this->get('/admin/plans/' . $plan->id . '?back=' . urlencode($unsafeBack));
$res->assertOk();
$planShowSelf = '/admin/plans/' . $plan->id;
$expectedOrdersUrl = '/admin/platform-orders?' . Arr::query([
'plan_id' => $plan->id,
'back' => $planShowSelf,
]);
$expectedPaidNoReceiptUrl = '/admin/platform-orders?' . Arr::query([
'plan_id' => $plan->id,
'payment_status' => 'paid',
'receipt_status' => 'none',
'back' => $planShowSelf,
]);
$expectedRenewalMissingUrl = '/admin/platform-orders?' . Arr::query([
'plan_id' => $plan->id,
'renewal_missing_subscription' => '1',
'back' => $planShowSelf,
]);
$res->assertSee($expectedOrdersUrl, false);
$res->assertSee($expectedPaidNoReceiptUrl, false);
$res->assertSee($expectedRenewalMissingUrl, false);
$res->assertDontSee('back=' . $unsafeBack, false);
$res->assertDontSee('back%3D', false);
}
public function test_plan_show_subscription_governance_links_should_use_self_back_when_outer_back_is_unsafe(): void
{
$this->loginAsPlatformAdmin();
$plan = Plan::query()->create([
'code' => 'plan_show_subscription_governance_unsafe_back_test',
'name' => '套餐详情订阅治理入口 unsafe back 测试套餐',
'billing_cycle' => 'monthly',
'price' => 86,
'list_price' => 106,
'status' => 'active',
'sort' => 10,
]);
$unsafeBack = '/admin/plans?status=active&back=/admin/site-subscriptions';
$res = $this->get('/admin/plans/' . $plan->id . '?back=' . urlencode($unsafeBack));
$res->assertOk();
$planShowSelf = '/admin/plans/' . $plan->id;
$expectedSubscriptionsUrl = '/admin/site-subscriptions?' . Arr::query([
'plan_id' => $plan->id,
'back' => $planShowSelf,
]);
$expectedActivatedSubscriptionsUrl = '/admin/site-subscriptions?' . Arr::query([
'plan_id' => $plan->id,
'status' => 'activated',
'back' => $planShowSelf,
]);
$expectedExpiringSubscriptionsUrl = '/admin/site-subscriptions?' . Arr::query([
'plan_id' => $plan->id,
'expiry' => 'expiring_7d',
'back' => $planShowSelf,
]);
$res->assertSee($expectedSubscriptionsUrl, false);
$res->assertSee($expectedActivatedSubscriptionsUrl, false);
$res->assertSee($expectedExpiringSubscriptionsUrl, false);
$res->assertDontSee('back=' . $unsafeBack, false);
$res->assertDontSee('back%3D', false);
}
public function test_plan_show_should_render_safe_back_but_subscription_governance_links_should_still_use_plan_show_self_back(): void
{
$this->loginAsPlatformAdmin();
$plan = Plan::query()->create([
'code' => 'plan_show_subscription_governance_safe_back_test',
'name' => '套餐详情订阅治理入口 safe back 测试套餐',
'billing_cycle' => 'monthly',
'price' => 96,
'list_price' => 126,
'status' => 'active',
'sort' => 10,
]);
$safeBack = '/admin/plans?' . Arr::query([
'status' => 'active',
'keyword' => '订阅治理',
]);
$res = $this->get('/admin/plans/' . $plan->id . '?back=' . urlencode($safeBack));
$res->assertOk();
$res->assertSee('href="' . $safeBack . '"', false);
$res->assertSee('返回上一页(保留上下文)');
$planShowSelf = '/admin/plans/' . $plan->id;
$expectedSubscriptionsUrl = '/admin/site-subscriptions?' . Arr::query([
'plan_id' => $plan->id,
'back' => $planShowSelf,
]);
$expectedExpiringSubscriptionsUrl = '/admin/site-subscriptions?' . Arr::query([
'plan_id' => $plan->id,
'expiry' => 'expiring_7d',
'back' => $planShowSelf,
]);
$res->assertSee($expectedSubscriptionsUrl, false);
$res->assertSee($expectedExpiringSubscriptionsUrl, false);
$res->assertDontSee('back=' . $safeBack, false);
}
}