对账明细导出:增加 download=1 安全阀并补护栏测试

This commit is contained in:
萝卜
2026-03-13 21:05:23 +00:00
parent c4c1eedce1
commit dcf1e3d226
4 changed files with 72 additions and 6 deletions

View File

@@ -421,6 +421,11 @@ class PlatformOrderController extends Controller
{ {
$this->ensurePlatformAdmin($request); $this->ensurePlatformAdmin($request);
// 安全阀:必须显式声明 download=1避免浏览器预取/误触发导致频繁导出
if ((string) $request->query('download', '') !== '1') {
abort(400, 'download=1 required');
}
$order->loadMissing(['merchant', 'plan', 'siteSubscription']); $order->loadMissing(['merchant', 'plan', 'siteSubscription']);
$paymentReceipts = (array) (data_get($order->meta, 'payment_receipts', []) ?? []); $paymentReceipts = (array) (data_get($order->meta, 'payment_receipts', []) ?? []);

View File

@@ -327,8 +327,8 @@
<div class="flex-between" style="align-items:center;"> <div class="flex-between" style="align-items:center;">
<h3 style="margin:0;">支付回执(对账留痕)</h3> <h3 style="margin:0;">支付回执(对账留痕)</h3>
<div class="muted" style="display:flex; gap:10px;"> <div class="muted" style="display:flex; gap:10px;">
<a class="muted" href="/admin/platform-orders/{{ $order->id }}/export-ledger">导出对账明细CSV</a> <a class="muted" href="/admin/platform-orders/{{ $order->id }}/export-ledger?download=1">导出对账明细CSV</a>
<a class="muted" href="/admin/platform-orders/{{ $order->id }}/export-ledger?include_order_snapshot=1">导出含订单摘要CSV</a> <a class="muted" href="/admin/platform-orders/{{ $order->id }}/export-ledger?download=1&include_order_snapshot=1">导出含订单摘要CSV</a>
</div> </div>
</div> </div>
<p class="muted muted-tight">用于“线下收款/转账/人工核对”的留痕记录(当前阶段先落 meta不引入独立表</p> <p class="muted muted-tight">用于“线下收款/转账/人工核对”的留痕记录(当前阶段先落 meta不引入独立表</p>

View File

@@ -0,0 +1,61 @@
<?php
namespace Tests\Feature;
use App\Models\Merchant;
use App\Models\Plan;
use App\Models\PlatformOrder;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Tests\TestCase;
class AdminPlatformOrderExportLedgerDownloadSafetyValveTest 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_export_ledger_should_require_download_flag(): void
{
$this->loginAsPlatformAdmin();
$merchant = Merchant::query()->firstOrFail();
$plan = Plan::query()->create([
'code' => 'po_export_ledger_safety_valve_plan',
'name' => '平台订单导出对账明细 download 安全阀测试套餐',
'billing_cycle' => 'monthly',
'price' => 10,
'list_price' => 10,
'status' => 'active',
'sort' => 10,
'published_at' => now(),
]);
$order = PlatformOrder::query()->create([
'merchant_id' => $merchant->id,
'plan_id' => $plan->id,
'order_no' => 'PO_EXPORT_LEDGER_SAFETY_0001',
'order_type' => 'new_purchase',
'status' => 'pending',
'payment_status' => 'unpaid',
'plan_name' => $plan->name,
'billing_cycle' => $plan->billing_cycle,
'period_months' => 1,
'quantity' => 1,
'payable_amount' => 10,
'paid_amount' => 0,
'placed_at' => now(),
'meta' => [],
]);
$this->get('/admin/platform-orders/' . $order->id . '/export-ledger')->assertStatus(400);
$this->get('/admin/platform-orders/' . $order->id . '/export-ledger?download=1')->assertOk();
}
}

View File

@@ -78,12 +78,12 @@ class AdminPlatformOrderExportLedgerTest extends TestCase
], ],
]); ]);
$res = $this->get('/admin/platform-orders/' . $order->id . '/export-ledger'); $res = $this->get('/admin/platform-orders/' . $order->id . '/export-ledger?download=1');
$res->assertOk(); $res->assertOk();
$content = $res->streamedContent(); $content = $res->streamedContent();
$res2 = $this->get('/admin/platform-orders/' . $order->id . '/export-ledger?include_order_snapshot=1'); $res2 = $this->get('/admin/platform-orders/' . $order->id . '/export-ledger?download=1&include_order_snapshot=1');
$res2->assertOk(); $res2->assertOk();
$content2 = $res2->streamedContent(); $content2 = $res2->streamedContent();
@@ -146,8 +146,8 @@ class AdminPlatformOrderExportLedgerTest extends TestCase
$res = $this->get('/admin/platform-orders/' . $order->id); $res = $this->get('/admin/platform-orders/' . $order->id);
$res->assertOk(); $res->assertOk();
$res->assertSee('/admin/platform-orders/' . $order->id . '/export-ledger', false); $res->assertSee('/admin/platform-orders/' . $order->id . '/export-ledger?download=1', false);
$res->assertSee('/admin/platform-orders/' . $order->id . '/export-ledger?include_order_snapshot=1', false); $res->assertSee('/admin/platform-orders/' . $order->id . '/export-ledger?download=1&include_order_snapshot=1', false);
$res->assertSee('导出对账明细CSV', false); $res->assertSee('导出对账明细CSV', false);
$res->assertSee('导出含订单摘要CSV', false); $res->assertSee('导出含订单摘要CSV', false);
} }