fix: 付费块文章解锁无响应——unlockPost 守卫误用 canReadPost

付费块文章的整篇是公开的,canReadPost 恒为 true,导致解锁请求被"已有权限"守卫拦截直接跳回文章页,不创建订单。改为按 canReadPaidContent 判断(能否读付费内容),并补回归测试:付费块解锁→创建订单→沙箱支付→[paid] 块可见
This commit is contained in:
ak
2026-08-12 01:12:45 +08:00
parent 8ef250ba80
commit 7228a4f14a
2 changed files with 32 additions and 2 deletions
+29
View File
@@ -186,6 +186,35 @@ class MembershipFlowTest extends TestCase
$this->assertSame(0, Payment::query()->count());
}
public function test_unlock_paid_block_creates_payment_for_non_member(): void
{
$post = Post::create([
'title' => '付费块文章',
'slug' => 'paid-block-unlock',
'content' => "开头\n\n[paid]\n隐藏付费块\n[/paid]\n\n结尾",
'content_format' => 'markdown',
'status' => 'published',
'published_at' => now(),
'meta' => ['price' => 500],
]);
// 付费块文章的整篇公开,但 [paid] 块不可读——解锁必须创建订单,不能误判为"已有权限"
$this->actingAs($this->user)
->post('/posts/'.$post->id.'/unlock')
->assertRedirect(route('pay.checkout', Payment::first()));
$payment = Payment::query()->where('user_id', $this->user->id)->first();
$this->assertNotNull($payment);
$this->assertTrue($payment->payable->is($post));
$this->assertSame('pending', $payment->status);
// 沙箱支付后 [paid] 块可见
$this->actingAs($this->user)->post('/pay/sandbox/'.$payment->order_no.'/confirm');
$this->actingAs($this->user)->get('/posts/paid-block-unlock.shtml')
->assertOk()
->assertSee('隐藏付费块');
}
public function test_members_only_post_shows_paywall_to_guest(): void
{
$post = Post::create([