修复一些错误

This commit is contained in:
liu
2026-08-14 21:28:55 +08:00
parent 398bb98356
commit 228212f8e3
20 changed files with 489 additions and 100 deletions
+127
View File
@@ -0,0 +1,127 @@
<?php
namespace Tests\Feature;
use App\Models\NoticeModel;
use App\Models\StoreModel;
use App\Models\UserModel;
/**
* 小程序消息通知:本人通知 + 全员广播;广播已读复制本人副本(data.broadcast_from 记来源),
* 列表排除已读广播。回归:列表曾报 Undefined property: stdClass::$data->broadcast_from
* pluck JSON 路径被 Query Builder 当作结果列名读取 stdClass 属性)
*/
class MiniNoticeTest extends ProcurementTestCase
{
/**
* 造一个绑定门店的小程序用户
*
* @return array{0: StoreModel, 1: UserModel}
*/
private function makeStoreWithUser(): array
{
$store = StoreModel::factory()->create();
return [$store, UserModel::factory()->forStore($store->id)->create()];
}
/** 通知列表:本人通知 + 未读广播,unread_count 正确 */
public function test_notice_list_includes_personal_and_broadcast(): void
{
[, $user] = $this->makeStoreWithUser();
$this->actingAsMiniUser($user);
NoticeModel::create([
'user_id' => NoticeModel::BROADCAST_USER_ID,
'type' => NoticeModel::TYPE_SYSTEM,
'title' => '系统公告',
'content' => '全员可见',
'is_read' => NoticeModel::UNREAD,
]);
NoticeModel::create([
'user_id' => $user->id,
'type' => NoticeModel::TYPE_ORDER,
'title' => '订单状态更新',
'content' => '您的订单已接单',
'is_read' => NoticeModel::UNREAD,
]);
$data = $this->getJson('/mini/notice')->assertOk()->json('data');
$this->assertSame(2, $data['total']);
$this->assertSame(2, $data['unread_count']);
}
/** 广播标记已读:复制本人已读副本,列表排除原广播(回归:列表不再报错) */
public function test_broadcast_read_creates_personal_copy(): void
{
[, $user] = $this->makeStoreWithUser();
$this->actingAsMiniUser($user);
$broadcast = NoticeModel::create([
'user_id' => NoticeModel::BROADCAST_USER_ID,
'type' => NoticeModel::TYPE_SYSTEM,
'title' => '系统公告',
'content' => '全员可见',
'data' => ['foo' => 'bar'],
'is_read' => NoticeModel::UNREAD,
]);
$this->putJson("/mini/notice/{$broadcast->id}/read")->assertJsonPath('success', true);
// 已读副本:本人记录、已读、broadcast_from 记来源且保留原 data
$copy = NoticeModel::where('user_id', $user->id)->first();
$this->assertNotNull($copy);
$this->assertSame(NoticeModel::READ, $copy->is_read);
$this->assertSame($broadcast->id, $copy->data['broadcast_from']);
$this->assertSame('bar', $copy->data['foo']);
// 列表:原广播被排除,仅剩已读副本,未读数归零
$data = $this->getJson('/mini/notice')->assertOk()->json('data');
$this->assertSame(1, $data['total']);
$this->assertSame(0, $data['unread_count']);
$this->assertSame($copy->id, $data['data'][0]['id']);
// 重复标记不重复复制
$this->putJson("/mini/notice/{$broadcast->id}/read")->assertJsonPath('success', true);
$this->assertSame(1, NoticeModel::where('user_id', $user->id)->count());
}
/** 个人通知标记已读:直接更新原记录,不产生副本 */
public function test_personal_notice_read_marks_directly(): void
{
[, $user] = $this->makeStoreWithUser();
$this->actingAsMiniUser($user);
$notice = NoticeModel::create([
'user_id' => $user->id,
'type' => NoticeModel::TYPE_ORDER,
'title' => '订单状态更新',
'content' => '您的订单已接单',
'is_read' => NoticeModel::UNREAD,
]);
$this->putJson("/mini/notice/{$notice->id}/read")->assertJsonPath('success', true);
$this->assertSame(NoticeModel::READ, $notice->fresh()->is_read);
$this->assertSame(1, NoticeModel::count(), '个人通知直接更新,不产生副本');
}
/** 他人通知不可标记(仅本人通知与广播可读) */
public function test_other_users_notice_not_found(): void
{
[, $user] = $this->makeStoreWithUser();
$this->actingAsMiniUser($user);
[, $other] = $this->makeStoreWithUser();
$notice = NoticeModel::create([
'user_id' => $other->id,
'type' => NoticeModel::TYPE_ORDER,
'title' => '订单状态更新',
'content' => '他人通知',
'is_read' => NoticeModel::UNREAD,
]);
$this->putJson("/mini/notice/{$notice->id}/read")->assertJsonPath('success', false);
$this->assertSame(NoticeModel::UNREAD, $notice->fresh()->is_read);
}
}