@@ -62,6 +62,59 @@ public function it_can_mark_a_conversation_as_read()
6262 $ this ->assertEquals (1 , $ conversation ->unReadNotifications ($ this ->bravo )->count ());
6363 }
6464
65+ /** @test */
66+ public function it_only_marks_received_messages_as_read_not_sent_messages ()
67+ {
68+ $ conversation = Chat::createConversation ([
69+ $ this ->alpha ,
70+ $ this ->bravo ,
71+ ])->makeDirect ();
72+
73+ // Alpha sends 2 messages
74+ Chat::message ('Message from Alpha 1 ' )->from ($ this ->alpha )->to ($ conversation )->send ();
75+ Chat::message ('Message from Alpha 2 ' )->from ($ this ->alpha )->to ($ conversation )->send ();
76+
77+ // Bravo sends 1 message
78+ Chat::message ('Message from Bravo ' )->from ($ this ->bravo )->to ($ conversation )->send ();
79+
80+ // Get Alpha's notifications before readAll
81+ $ alphaNotificationsBefore = \Musonza \Chat \Models \MessageNotification::where ('messageable_id ' , $ this ->alpha ->getKey ())
82+ ->where ('messageable_type ' , $ this ->alpha ->getMorphClass ())
83+ ->where ('conversation_id ' , $ conversation ->id )
84+ ->get ();
85+
86+ // Alpha has 3 notifications: 2 as sender (is_seen=1), 1 as receiver (is_seen=0)
87+ $ this ->assertEquals (3 , $ alphaNotificationsBefore ->count ());
88+ $ this ->assertEquals (2 , $ alphaNotificationsBefore ->where ('is_sender ' , 1 )->count ());
89+ $ this ->assertEquals (1 , $ alphaNotificationsBefore ->where ('is_sender ' , 0 )->count ());
90+ $ this ->assertEquals (1 , $ alphaNotificationsBefore ->where ('is_sender ' , 0 )->where ('is_seen ' , 0 )->count ());
91+
92+ // Manually set is_seen to a special value (2) on sent messages to detect if readAll updates them
93+ \Musonza \Chat \Models \MessageNotification::where ('messageable_id ' , $ this ->alpha ->getKey ())
94+ ->where ('messageable_type ' , $ this ->alpha ->getMorphClass ())
95+ ->where ('conversation_id ' , $ conversation ->id )
96+ ->where ('is_sender ' , 1 )
97+ ->update (['is_seen ' => 2 ]);
98+
99+ // Alpha marks conversation as read
100+ Chat::conversation ($ conversation )->setParticipant ($ this ->alpha )->readAll ();
101+
102+ // Get Alpha's notifications after readAll
103+ $ alphaNotificationsAfter = \Musonza \Chat \Models \MessageNotification::where ('messageable_id ' , $ this ->alpha ->getKey ())
104+ ->where ('messageable_type ' , $ this ->alpha ->getMorphClass ())
105+ ->where ('conversation_id ' , $ conversation ->id )
106+ ->get ();
107+
108+ // The received message should now be marked as read
109+ $ this ->assertEquals (0 , $ alphaNotificationsAfter ->where ('is_sender ' , 0 )->where ('is_seen ' , 0 )->count ());
110+ $ this ->assertEquals (1 , $ alphaNotificationsAfter ->where ('is_sender ' , 0 )->where ('is_seen ' , 1 )->count ());
111+
112+ // The sent messages should NOT have been updated - they should still have is_seen=2
113+ // If readAll() incorrectly updates all notifications, they would have is_seen=1
114+ $ sentMessagesWithOriginalValue = $ alphaNotificationsAfter ->where ('is_sender ' , 1 )->where ('is_seen ' , 2 )->count ();
115+ $ this ->assertEquals (2 , $ sentMessagesWithOriginalValue , 'readAll() should not update sent message notifications ' );
116+ }
117+
65118 /** @test */
66119 public function it_can_update_conversation_details ()
67120 {
0 commit comments