新增聊天室状态与功能快捷菜单
This commit is contained in:
@@ -10,6 +10,8 @@ namespace Tests\Feature;
|
||||
use App\Models\Room;
|
||||
use App\Models\Sysparam;
|
||||
use App\Models\User;
|
||||
use App\Services\ChatUserPresenceService;
|
||||
use Carbon\Carbon;
|
||||
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||||
use Illuminate\Support\Facades\Cache;
|
||||
use Illuminate\Support\Facades\Hash;
|
||||
@@ -31,6 +33,9 @@ class UserControllerTest extends TestCase
|
||||
{
|
||||
parent::setUp();
|
||||
|
||||
// 清空 Redis 在线状态,避免当日状态与房间在线缓存互相污染。
|
||||
Redis::flushall();
|
||||
|
||||
Sysparam::updateOrCreate(['alias' => 'superlevel'], ['body' => '100']);
|
||||
Sysparam::updateOrCreate(['alias' => 'level_kick'], ['body' => '15']);
|
||||
Sysparam::updateOrCreate(['alias' => 'level_mute'], ['body' => '15']);
|
||||
@@ -163,6 +168,95 @@ class UserControllerTest extends TestCase
|
||||
], $user->chat_preferences);
|
||||
}
|
||||
|
||||
/**
|
||||
* 测试合法聊天室当日状态可以保存,并在当天结束后自动失效。
|
||||
*/
|
||||
public function test_can_update_daily_status_until_end_of_day(): void
|
||||
{
|
||||
Carbon::setTestNow('2026-04-24 10:36:00');
|
||||
|
||||
try {
|
||||
$user = User::factory()->create([
|
||||
'username' => 'status-user',
|
||||
]);
|
||||
$room = $this->enterRoomForUser($user);
|
||||
|
||||
$response = $this->actingAs($user)->putJson(route('user.update_daily_status'), [
|
||||
'room_id' => $room->id,
|
||||
'action' => 'set',
|
||||
'status_key' => 'working_hard',
|
||||
]);
|
||||
|
||||
$expectedExpiry = now()->endOfDay();
|
||||
|
||||
$response->assertOk()
|
||||
->assertJsonPath('status', 'success')
|
||||
->assertJsonPath('data.status.key', 'working_hard')
|
||||
->assertJsonPath('data.status.label', '搬砖')
|
||||
->assertJsonPath('data.status.group', '工作学习');
|
||||
|
||||
$user->refresh();
|
||||
$this->assertSame('working_hard', $user->daily_status_key);
|
||||
$this->assertSame(
|
||||
$expectedExpiry->toDateTimeString(),
|
||||
$user->daily_status_expires_at?->toDateTimeString()
|
||||
);
|
||||
|
||||
// 时间推进到次日,验证在线名单服务不再返回已过期状态。
|
||||
Carbon::setTestNow($expectedExpiry->copy()->addSecond());
|
||||
$this->assertNull(app(ChatUserPresenceService::class)->currentDailyStatus($user->fresh()));
|
||||
} finally {
|
||||
Carbon::setTestNow();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 测试非法聊天室当日状态键会返回 422 校验错误。
|
||||
*/
|
||||
public function test_invalid_daily_status_key_returns_validation_error(): void
|
||||
{
|
||||
$user = User::factory()->create();
|
||||
$room = Room::create([
|
||||
'room_name' => 'dsinvalid',
|
||||
'room_owner' => 'someone',
|
||||
]);
|
||||
|
||||
$response = $this->actingAs($user)->putJson(route('user.update_daily_status'), [
|
||||
'room_id' => $room->id,
|
||||
'action' => 'set',
|
||||
'status_key' => 'not-a-real-status',
|
||||
]);
|
||||
|
||||
$response->assertStatus(422)
|
||||
->assertJsonValidationErrors('status_key');
|
||||
}
|
||||
|
||||
/**
|
||||
* 测试清除聊天室当日状态后会把状态字段置空。
|
||||
*/
|
||||
public function test_clear_daily_status_resets_status_fields(): void
|
||||
{
|
||||
$user = User::factory()->create([
|
||||
'daily_status_key' => 'working_hard',
|
||||
'daily_status_expires_at' => now()->endOfDay(),
|
||||
]);
|
||||
$room = $this->enterRoomForUser($user);
|
||||
|
||||
$response = $this->actingAs($user)->putJson(route('user.update_daily_status'), [
|
||||
'room_id' => $room->id,
|
||||
'action' => 'clear',
|
||||
]);
|
||||
|
||||
$response->assertOk()
|
||||
->assertJsonPath('status', 'success')
|
||||
->assertJsonPath('message', '状态已清除。')
|
||||
->assertJsonPath('data.status', null);
|
||||
|
||||
$user->refresh();
|
||||
$this->assertNull($user->daily_status_key);
|
||||
$this->assertNull($user->daily_status_expires_at);
|
||||
}
|
||||
|
||||
public function test_can_change_password()
|
||||
{
|
||||
$user = User::factory()->create([
|
||||
@@ -304,4 +398,21 @@ class UserControllerTest extends TestCase
|
||||
$target->refresh();
|
||||
$this->assertEquals(-1, $target->user_level);
|
||||
}
|
||||
|
||||
/**
|
||||
* 让指定用户先进入聊天室,满足“仅在线用户可设置状态”的前置条件。
|
||||
*/
|
||||
private function enterRoomForUser(User $user): Room
|
||||
{
|
||||
$room = Room::create([
|
||||
'room_name' => 'dsr'.$user->id,
|
||||
'room_owner' => 'someone',
|
||||
]);
|
||||
|
||||
$this->actingAs($user)
|
||||
->get(route('chat.room', $room->id))
|
||||
->assertOk();
|
||||
|
||||
return $room;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user