完善职务礼包红包默认配置

This commit is contained in:
2026-04-24 23:09:32 +08:00
parent 4486a87326
commit 5273b4ee4b
12 changed files with 394 additions and 93 deletions
+13 -12
View File
@@ -336,15 +336,14 @@ class ChatControllerTest extends TestCase
}
/**
* 测试站长即使没有在职职务,也能看到管理菜单中的刷新全员按钮。
* 测试站长有在职职务权限时能看到管理菜单中的刷新全员按钮。
*/
public function test_room_view_shows_refresh_all_button_for_site_owner(): void
public function test_room_view_shows_refresh_all_button_for_positioned_site_owner(): void
{
$room = Room::create(['room_name' => 'owner-rf']);
$user = User::factory()->create([
'id' => 1,
'user_level' => 100,
]);
$user = $this->createUserWithPositionPermissions([
PositionPermissionRegistry::ROOM_CLEAR_SCREEN,
], ['id' => 1, 'user_level' => 100]);
$response = $this->actingAs($user)->get(route('chat.room', $room->id));
@@ -804,11 +803,13 @@ class ChatControllerTest extends TestCase
}
/**
* 测试管理员可以设置房间公告。
* 测试拥有公告权限的职务用户可以设置房间公告。
*/
public function test_site_owner_can_set_announcement()
public function test_position_user_can_set_announcement()
{
$user = User::factory()->create(['id' => 1, 'user_level' => 100]);
$user = $this->createUserWithPositionPermissions([
PositionPermissionRegistry::ROOM_ANNOUNCEMENT,
]);
$room = Room::create(['room_name' => 'test_ann', 'room_owner' => 'someone']);
$response = $this->actingAs($user)->postJson(route('chat.announcement', $room->id), [
@@ -873,11 +874,11 @@ class ChatControllerTest extends TestCase
*
* @param list<string> $permissions
*/
private function createUserWithPositionPermissions(array $permissions): User
private function createUserWithPositionPermissions(array $permissions, array $attributes = []): User
{
$user = User::factory()->create([
$user = User::factory()->create(array_merge([
'user_level' => 70,
]);
], $attributes));
$department = Department::create([
'name' => '聊天室测试部门'.$user->id,
@@ -39,13 +39,12 @@ class AdminCommandControllerTest extends TestCase
}
/**
* 测试站长可以触发全部新增全屏特效。
* 测试拥有全屏特效权限的职务用户可以触发全部新增全屏特效。
*/
public function test_super_admin_can_trigger_all_new_effect_types(): void
public function test_position_user_can_trigger_all_new_effect_types(): void
{
$admin = User::factory()->create([
'id' => 1,
'user_level' => 100,
$admin = $this->createPositionedManager([
PositionPermissionRegistry::ROOM_FULLSCREEN_EFFECT,
]);
$room = Room::create([
'room_name' => '特效房',
@@ -297,7 +296,8 @@ class AdminCommandControllerTest extends TestCase
'message' => "已警告 {$target->username}",
]);
$privateMessage = $this->findPrivateSystemMessage($room->id, $target->username, '站长</b> <b>'.$admin->username.'</b> 警告了你');
$identityText = $admin->activePosition->position->department->name.'·'.$admin->activePosition->position->name;
$privateMessage = $this->findPrivateSystemMessage($room->id, $target->username, "{$identityText}</b> <b>{$admin->username}</b> 警告了你");
$this->assertNotNull($privateMessage);
$this->assertSame('⚠️ 收到警告', $privateMessage['toast_notification']['title'] ?? null);
@@ -521,10 +521,13 @@ class AdminCommandControllerTest extends TestCase
*/
private function createAdminCommandActors(): array
{
$admin = User::factory()->create([
'id' => 1,
'user_level' => 100,
$admin = $this->createPositionedManager([
PositionPermissionRegistry::USER_WARN,
PositionPermissionRegistry::USER_MUTE,
PositionPermissionRegistry::USER_KICK,
PositionPermissionRegistry::USER_FREEZE,
]);
$admin->load('activePosition.position.department');
$target = User::factory()->create([
'user_level' => 1,
]);
@@ -41,6 +41,8 @@ class AdminPositionPermissionTest extends TestCase
'max_reward' => 100,
'daily_reward_limit' => 300,
'recipient_daily_limit' => 2,
'red_packet_amount' => 12000,
'red_packet_count' => 12,
'sort_order' => 8,
'permissions' => [
PositionPermissionRegistry::ROOM_ANNOUNCEMENT,
@@ -52,6 +54,8 @@ class AdminPositionPermissionTest extends TestCase
$position = Position::query()->where('name', '值班主持')->firstOrFail();
$this->assertSame(12000, (int) $position->red_packet_amount);
$this->assertSame(12, (int) $position->red_packet_count);
$this->assertSame([
PositionPermissionRegistry::ROOM_ANNOUNCEMENT,
PositionPermissionRegistry::ROOM_PUBLIC_BROADCAST,
@@ -113,6 +117,8 @@ class AdminPositionPermissionTest extends TestCase
'max_reward' => null,
'daily_reward_limit' => null,
'recipient_daily_limit' => null,
'red_packet_amount' => 6600,
'red_packet_count' => 6,
'sort_order' => 1,
'permissions' => [
PositionPermissionRegistry::ROOM_RED_PACKET,
@@ -124,12 +130,41 @@ class AdminPositionPermissionTest extends TestCase
$position->refresh();
$this->assertSame(6600, (int) $position->red_packet_amount);
$this->assertSame(6, (int) $position->red_packet_count);
$this->assertSame([
PositionPermissionRegistry::ROOM_RED_PACKET,
PositionPermissionRegistry::ROOM_FULLSCREEN_EFFECT,
], $position->permissions);
}
/**
* 验证礼包总量不能小于礼包份数。
*/
public function test_red_packet_amount_must_cover_packet_count(): void
{
$owner = $this->createSiteOwner();
$department = $this->createDepartment();
$response = $this->from(route('admin.positions.index'))->actingAs($owner)->post(route('admin.positions.store'), [
'department_id' => $department->id,
'name' => '礼包主持',
'icon' => '🧧',
'rank' => 55,
'level' => 55,
'max_persons' => 1,
'red_packet_amount' => 5,
'red_packet_count' => 10,
'sort_order' => 2,
'permissions' => [
PositionPermissionRegistry::ROOM_RED_PACKET,
],
]);
$response->assertRedirect(route('admin.positions.index'));
$response->assertSessionHasErrors('red_packet_amount');
}
/**
* 验证职务管理页面会渲染权限配置与摘要文案。
*/
@@ -157,6 +192,8 @@ class AdminPositionPermissionTest extends TestCase
$response->assertSee('权限管理');
$response->assertSee('设置公告');
$response->assertSee('礼包红包');
$response->assertSee('默认礼包总量');
$response->assertSee('默认礼包份数');
$response->assertSee('警告用户');
$response->assertSee('冻结用户');
}
+87 -15
View File
@@ -64,9 +64,9 @@ class RedPacketControllerTest extends TestCase
}
/**
* 方法功能:验证站长可以成功发出礼包并写入 Redis 拆包结果
* 方法功能:验证站长没有在职职务时也不能绕过礼包权限
*/
public function test_superadmin_can_send_red_packet(): void
public function test_site_owner_without_position_cannot_send_red_packet(): void
{
$admin = $this->createSiteOwner();
@@ -75,19 +75,72 @@ class RedPacketControllerTest extends TestCase
'type' => 'gold',
]);
$response->assertStatus(200);
$response->assertJson(['status' => 'success']);
$response->assertStatus(403);
}
$this->assertDatabaseHas('red_packet_envelopes', [
'sender_id' => $admin->id,
/**
* 方法功能:验证礼包弹窗配置接口会读取当前职务的数据库配置。
*/
public function test_red_packet_config_uses_position_packet_config(): void
{
$user = $this->createUserWithPermissions([
PositionPermissionRegistry::ROOM_RED_PACKET,
], redPacketAmount: 2468, redPacketCount: 8);
$response = $this->actingAs($user)->getJson(route('command.red_packet.config'));
$response->assertOk()
->assertJson([
'status' => 'success',
'amount' => 2468,
'count' => 8,
'expire_seconds' => 300,
]);
}
/**
* 方法功能:验证没有礼包权限时不能读取发包弹窗配置。
*/
public function test_user_without_red_packet_permission_cannot_read_packet_config(): void
{
$user = $this->createUserWithPermissions([]);
$response = $this->actingAs($user)->getJson(route('command.red_packet.config'));
$response->assertStatus(403)
->assertJson(['status' => 'error']);
}
/**
* 方法功能:验证拥有礼包权限的职务用户会按职务配置发出礼包。
*/
public function test_position_user_with_red_packet_permission_uses_position_packet_config(): void
{
$user = $this->createUserWithPermissions([
PositionPermissionRegistry::ROOM_RED_PACKET,
], redPacketAmount: 1234, redPacketCount: 7);
$response = $this->actingAs($user)->postJson(route('command.red_packet.send'), [
'room_id' => 1,
'type' => 'gold',
]);
$response->assertOk()
->assertJsonPath('status', 'success');
$this->assertDatabaseHas('red_packet_envelopes', [
'sender_id' => $user->id,
'room_id' => 1,
'type' => 'gold',
'total_amount' => 1234,
'total_count' => 7,
'status' => 'active',
]);
$envelope = RedPacketEnvelope::first();
// Check Redis for parts
$this->assertEquals(10, Redis::llen("red_packet:{$envelope->id}:amounts"));
$amounts = array_map('intval', Redis::lrange("red_packet:{$envelope->id}:amounts", 0, -1));
$this->assertCount(7, $amounts);
$this->assertSame(1234, array_sum($amounts));
}
/**
@@ -97,7 +150,7 @@ class RedPacketControllerTest extends TestCase
{
$user = $this->createUserWithPermissions([
PositionPermissionRegistry::ROOM_RED_PACKET,
]);
], redPacketAmount: 4321, redPacketCount: 6);
$response = $this->actingAs($user)->postJson(route('command.red_packet.send'), [
'room_id' => 1,
@@ -107,6 +160,13 @@ class RedPacketControllerTest extends TestCase
$response->assertOk()->assertJson([
'status' => 'success',
]);
$this->assertDatabaseHas('red_packet_envelopes', [
'sender_id' => $user->id,
'type' => 'exp',
'total_amount' => 4321,
'total_count' => 6,
]);
}
/**
@@ -114,7 +174,9 @@ class RedPacketControllerTest extends TestCase
*/
public function test_cannot_send_multiple_active_packets_in_same_room(): void
{
$admin = $this->createSiteOwner();
$admin = $this->createUserWithPermissions([
PositionPermissionRegistry::ROOM_RED_PACKET,
]);
$this->actingAs($admin)->postJson(route('command.red_packet.send'), [
'room_id' => 1,
@@ -134,7 +196,9 @@ class RedPacketControllerTest extends TestCase
*/
public function test_user_can_claim_red_packet(): void
{
$admin = $this->createSiteOwner();
$admin = $this->createUserWithPermissions([
PositionPermissionRegistry::ROOM_RED_PACKET,
]);
$user = User::factory()->create(['jjb' => 100]);
// Send packet
@@ -169,7 +233,9 @@ class RedPacketControllerTest extends TestCase
{
Event::fake([RedPacketClaimed::class]);
$admin = $this->createSiteOwner();
$admin = $this->createUserWithPermissions([
PositionPermissionRegistry::ROOM_RED_PACKET,
]);
$user = User::factory()->create(['jjb' => 100]);
$this->actingAs($admin)->postJson(route('command.red_packet.send'), [
@@ -203,7 +269,9 @@ class RedPacketControllerTest extends TestCase
*/
public function test_user_cannot_claim_same_packet_twice(): void
{
$admin = $this->createSiteOwner();
$admin = $this->createUserWithPermissions([
PositionPermissionRegistry::ROOM_RED_PACKET,
]);
$user = User::factory()->create();
$this->actingAs($admin)->postJson(route('command.red_packet.send'), [
@@ -232,7 +300,9 @@ class RedPacketControllerTest extends TestCase
*/
public function test_can_check_packet_status(): void
{
$admin = $this->createSiteOwner();
$admin = $this->createUserWithPermissions([
PositionPermissionRegistry::ROOM_RED_PACKET,
]);
$user = User::factory()->create();
$this->actingAs($admin)->postJson(route('command.red_packet.send'), [
@@ -279,7 +349,7 @@ class RedPacketControllerTest extends TestCase
*
* @param list<string> $permissions
*/
private function createUserWithPermissions(array $permissions): User
private function createUserWithPermissions(array $permissions, int $redPacketAmount = 8888, int $redPacketCount = 10): User
{
$user = User::factory()->create([
'user_level' => 80,
@@ -301,6 +371,8 @@ class RedPacketControllerTest extends TestCase
'level' => 80,
'sort_order' => 1,
'permissions' => $permissions,
'red_packet_amount' => $redPacketAmount,
'red_packet_count' => $redPacketCount,
]);
UserPosition::create([