|
4 | 4 |
|
5 | 5 | use App\Interfaces\ContactServiceInterface; |
6 | 6 | use App\Interfaces\MessageServiceInterface; |
| 7 | +use App\Models\Message; |
7 | 8 | use App\Models\Participant; |
8 | 9 | use App\Models\Thread; |
9 | 10 | use App\Models\User; |
@@ -294,6 +295,26 @@ public function test_service_method_new_message() |
294 | 295 | $this->assertEquals($lastMessage, $lastInsertedMessage); |
295 | 296 | } |
296 | 297 |
|
| 298 | + /** |
| 299 | + * Sending a message must not leave it counted as unread for its own |
| 300 | + * sender — without marking the sender's own participant as read, |
| 301 | + * userUnreadMessagesCount() would count the sender's own message against |
| 302 | + * themselves. |
| 303 | + * |
| 304 | + * @return void |
| 305 | + */ |
| 306 | + public function test_new_message_does_not_count_as_unread_for_its_sender() |
| 307 | + { |
| 308 | + $sender = User::factory()->create(); |
| 309 | + $recipient = User::factory()->create(); |
| 310 | + |
| 311 | + $thread = $this->service->newThread('Own message', $sender, $this->envelope(), [$recipient->id]); |
| 312 | + $this->service->newMessage($thread, $sender, $this->envelope('meh')); |
| 313 | + |
| 314 | + $this->assertSame(0, $thread->userUnreadMessagesCount($sender->id)); |
| 315 | + $this->assertSame(2, $thread->userUnreadMessagesCount($recipient->id)); |
| 316 | + } |
| 317 | + |
297 | 318 | /** |
298 | 319 | * Check if markAsRead method updates last read attribute. |
299 | 320 | * |
@@ -482,4 +503,61 @@ public function test_thread_created_toarray_reloads_participant_user_after_seria |
482 | 503 |
|
483 | 504 | $this->assertCount(2, $participantUserNames); |
484 | 505 | } |
| 506 | + |
| 507 | + /** |
| 508 | + * Same serialization round-trip gap as ThreadCreated, but for the |
| 509 | + * message's "user" relation: MessageService::newMessage() only attaches |
| 510 | + * it via setRelation(), so MessageCreated::toArray() must reload it |
| 511 | + * itself rather than assume it survives dispatch. |
| 512 | + * |
| 513 | + * @return void |
| 514 | + */ |
| 515 | + public function test_message_created_toarray_reloads_user_after_serialization_round_trip() |
| 516 | + { |
| 517 | + Notification::fake(); |
| 518 | + |
| 519 | + $sender = User::factory()->create(['notify_via' => ['broadcast']]); |
| 520 | + $recipient = User::factory()->create(['notify_via' => ['broadcast']]); |
| 521 | + |
| 522 | + $thread = $this->service->newThread('Serialization Round Trip', $sender, $this->envelope(), [$recipient->id]); |
| 523 | + $message = $this->service->newMessage($thread, $sender, $this->envelope('meh')); |
| 524 | + |
| 525 | + // Fetch a bare copy with "user" NOT loaded, mirroring what |
| 526 | + // SerializesModels restores after unserialize(). |
| 527 | + $bareMessage = Message::findOrFail($message->id); |
| 528 | + |
| 529 | + $notification = new MessageCreated($bareMessage); |
| 530 | + $payload = $this->resolvedPayload($notification->toArray($recipient)); |
| 531 | + |
| 532 | + $this->assertSame($sender->name, Arr::get($payload, 'payload.attributes.user.attributes.name')); |
| 533 | + } |
| 534 | + |
| 535 | + /** |
| 536 | + * Same serialization round-trip gap as ThreadCreated, but for the |
| 537 | + * participant's "user" relation: MessageService::addParticipant() only |
| 538 | + * attaches it via setRelation(), so ParticipantCreated::toArray() must |
| 539 | + * reload it itself rather than assume it survives dispatch. |
| 540 | + * |
| 541 | + * @return void |
| 542 | + */ |
| 543 | + public function test_participant_created_toarray_reloads_user_after_serialization_round_trip() |
| 544 | + { |
| 545 | + Notification::fake(); |
| 546 | + |
| 547 | + $sender = User::factory()->create(['notify_via' => ['broadcast']]); |
| 548 | + $recipient = User::factory()->create(['notify_via' => ['broadcast']]); |
| 549 | + $newParticipant = User::factory()->create(['notify_via' => ['broadcast']]); |
| 550 | + |
| 551 | + $thread = $this->service->newThread('Serialization Round Trip', $sender, $this->envelope(), [$recipient->id]); |
| 552 | + $participant = $this->service->addParticipant($thread, $newParticipant); |
| 553 | + |
| 554 | + // Fetch a bare copy with "user" NOT loaded, mirroring what |
| 555 | + // SerializesModels restores after unserialize(). |
| 556 | + $bareParticipant = Participant::findOrFail($participant->id); |
| 557 | + |
| 558 | + $notification = new ParticipantCreated($bareParticipant); |
| 559 | + $payload = $this->resolvedPayload($notification->toArray($recipient)); |
| 560 | + |
| 561 | + $this->assertSame($newParticipant->name, Arr::get($payload, 'payload.attributes.user.attributes.name')); |
| 562 | + } |
485 | 563 | } |
0 commit comments