create([ 'is_admin' => true, ]); $customer = User::factory()->create(); $conversation = ChatConversation::query()->create([ 'user_id' => $customer->id, 'visitor_token' => (string) Str::uuid(), 'status' => ChatConversation::STATUS_OPEN, 'last_message_at' => now(), ]); $response = $this ->actingAs($admin) ->patch(route('admin.chats.status', $conversation), [ 'status' => ChatConversation::STATUS_CLOSED, ]); $response->assertRedirect(route('admin.chats.index', ['conversation' => $conversation->id])); $this->assertSame(ChatConversation::STATUS_CLOSED, $conversation->fresh()->status); } public function test_customer_message_after_chat_closure_starts_new_conversation(): void { $customer = User::factory()->create(); $closedConversation = ChatConversation::query()->create([ 'user_id' => $customer->id, 'visitor_token' => (string) Str::uuid(), 'status' => ChatConversation::STATUS_CLOSED, 'last_message_at' => now(), ]); $closedConversation->messages()->create([ 'sender' => 'customer', 'body' => 'Старый вопрос', 'is_read' => true, ]); $this ->actingAs($customer) ->getJson(route('chat.messages')) ->assertOk() ->assertJsonPath('conversation.id', $closedConversation->id) ->assertJsonPath('conversation.is_closed', true); $this ->actingAs($customer) ->postJson(route('chat.send'), [ 'message' => 'Новый вопрос', ]) ->assertOk() ->assertJsonPath('conversation.is_closed', false); $this->assertSame(ChatConversation::STATUS_CLOSED, $closedConversation->fresh()->status); $this->assertSame(2, ChatConversation::query()->where('user_id', $customer->id)->count()); $newConversation = ChatConversation::query() ->where('user_id', $customer->id) ->latest('id') ->firstOrFail(); $this->assertNotSame($closedConversation->id, $newConversation->id); $this->assertSame(ChatConversation::STATUS_OPEN, $newConversation->status); $this->assertSame('Новый вопрос', $newConversation->messages()->firstOrFail()->body); } public function test_admin_can_delete_chat_conversation_with_messages(): void { $admin = User::factory()->create([ 'is_admin' => true, ]); $customer = User::factory()->create(); $conversation = ChatConversation::query()->create([ 'user_id' => $customer->id, 'visitor_token' => (string) Str::uuid(), 'status' => ChatConversation::STATUS_OPEN, 'last_message_at' => now(), ]); $message = $conversation->messages()->create([ 'sender' => 'customer', 'body' => 'Нужно удалить этот чат', 'is_read' => false, ]); $response = $this ->actingAs($admin) ->delete(route('admin.chats.destroy', $conversation)); $response->assertRedirect(route('admin.chats.index')); $this->assertDatabaseMissing('chat_conversations', ['id' => $conversation->id]); $this->assertDatabaseMissing('chat_messages', ['id' => $message->id]); $this->assertSame(0, ChatConversation::query()->count()); $this->assertSame(0, ChatMessage::query()->count()); } }