新增职务权限管理与聊天室管理权限控制

This commit is contained in:
2026-04-21 16:43:17 +08:00
parent cfdbf387af
commit 281315d1cf
19 changed files with 1243 additions and 87 deletions
+137 -6
View File
@@ -9,8 +9,12 @@
namespace Tests\Feature;
use App\Events\MessageSent;
use App\Models\Department;
use App\Models\Position;
use App\Models\Room;
use App\Models\User;
use App\Models\UserPosition;
use App\Support\PositionPermissionRegistry;
use Illuminate\Broadcasting\PresenceChannel;
use Illuminate\Broadcasting\PrivateChannel;
use Illuminate\Foundation\Testing\RefreshDatabase;
@@ -157,6 +161,60 @@ class ChatControllerTest extends TestCase
$response->assertSee('toggleBlockedSystemSender');
}
/**
* 测试无聊天室权限的职务用户看不到顶部管理按钮。
*/
public function test_room_view_hides_admin_menu_for_position_without_room_permissions(): void
{
$room = Room::create(['room_name' => 'nomenu']);
$user = $this->createUserWithPositionPermissions([]);
$response = $this->actingAs($user)->get(route('chat.room', $room->id));
$response->assertOk();
$response->assertDontSee('🛠 管理', false);
$response->assertDontSee('🪧 设公告', false);
}
/**
* 测试只授予公告权限时,顶部管理菜单仅显示对应按钮。
*/
public function test_room_view_renders_only_granted_room_management_buttons(): void
{
$room = Room::create(['room_name' => 'annmenu']);
$user = $this->createUserWithPositionPermissions([
PositionPermissionRegistry::ROOM_ANNOUNCEMENT,
]);
$response = $this->actingAs($user)->get(route('chat.room', $room->id));
$response->assertOk();
$response->assertSee('🛠 管理', false);
$response->assertSee('🪧 设公告', false);
$response->assertDontSee("runAdminAction('announce-message')", false);
$response->assertDontSee("selectEffect('fireworks')", false);
}
/**
* 测试仅有全屏特效权限时,只显示特效分组。
*/
public function test_room_view_renders_fullscreen_effect_group_only_when_permission_exists(): void
{
$room = Room::create(['room_name' => 'effectmenu']);
$user = $this->createUserWithPositionPermissions([
PositionPermissionRegistry::ROOM_FULLSCREEN_EFFECT,
]);
$response = $this->actingAs($user)->get(route('chat.room', $room->id));
$response->assertOk();
$response->assertSee('🛠 管理', false);
$response->assertSee('全屏特效');
$response->assertSee('🎆 烟花', false);
$response->assertDontSee("runAdminAction('announcement')", false);
$response->assertDontSee("runAdminAction('announce-message')", false);
}
/**
* 测试用户可以发送普通文本消息。
*/
@@ -593,9 +651,9 @@ class ChatControllerTest extends TestCase
/**
* 测试管理员可以设置房间公告。
*/
public function test_can_set_announcement()
public function test_site_owner_can_set_announcement()
{
$user = User::factory()->create(['user_level' => 100]); // superadmin
$user = User::factory()->create(['id' => 1, 'user_level' => 100]);
$room = Room::create(['room_name' => 'test_ann', 'room_owner' => 'someone']);
$response = $this->actingAs($user)->postJson(route('chat.announcement', $room->id), [
@@ -609,12 +667,29 @@ class ChatControllerTest extends TestCase
}
/**
* 测试无权限用户不能设置房间公告。
* 测试拥有公告权限的职务用户可以设置房间公告。
*/
public function test_cannot_set_announcement_without_permission()
public function test_position_user_with_room_announcement_permission_can_set_announcement(): void
{
$user = User::factory()->create(['user_level' => 0]);
$room = Room::create(['room_name' => 'test_ann2', 'room_owner' => 'someone']);
$user = $this->createUserWithPositionPermissions([
PositionPermissionRegistry::ROOM_ANNOUNCEMENT,
]);
$room = Room::create(['room_name' => 'test_ann2', 'room_owner' => 'other']);
$response = $this->actingAs($user)->postJson(route('chat.announcement', $room->id), [
'announcement' => 'This is a new test announcement',
]);
$response->assertStatus(200);
}
/**
* 测试房主但无公告权限时也不能设置房间公告。
*/
public function test_room_owner_without_announcement_permission_cannot_set_announcement(): void
{
$user = $this->createUserWithPositionPermissions([]);
$room = Room::create(['room_name' => 'test_ann3', 'room_owner' => $user->username]);
$response = $this->actingAs($user)->postJson(route('chat.announcement', $room->id), [
'announcement' => 'This is a new test announcement',
@@ -622,4 +697,60 @@ class ChatControllerTest extends TestCase
$response->assertStatus(403);
}
/**
* 测试无权限用户不能设置房间公告。
*/
public function test_cannot_set_announcement_without_permission()
{
$user = User::factory()->create(['user_level' => 0]);
$room = Room::create(['room_name' => 'test_ann4', 'room_owner' => 'someone']);
$response = $this->actingAs($user)->postJson(route('chat.announcement', $room->id), [
'announcement' => 'This is a new test announcement',
]);
$response->assertStatus(403);
}
/**
* 创建带指定聊天室权限的在职职务用户。
*
* @param list<string> $permissions
*/
private function createUserWithPositionPermissions(array $permissions): User
{
$user = User::factory()->create([
'user_level' => 70,
]);
$department = Department::create([
'name' => '聊天室测试部门'.$user->id,
'rank' => 70,
'color' => '#1d4ed8',
'sort_order' => 1,
'description' => '聊天室权限测试',
]);
$position = Position::create([
'department_id' => $department->id,
'name' => '聊天室测试职务'.$user->id,
'icon' => '🛡️',
'rank' => 70,
'level' => 70,
'sort_order' => 1,
'permissions' => $permissions,
]);
UserPosition::create([
'user_id' => $user->id,
'position_id' => $position->id,
'appointed_by_user_id' => null,
'appointed_at' => now(),
'remark' => '聊天室权限测试',
'is_active' => true,
]);
return $user->fresh();
}
}