| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061 |
- <?php
- namespace Tests\Feature;
- use App\Models\User;
- use App\Models\UserNotification;
- use Illuminate\Foundation\Testing\RefreshDatabase;
- use Tests\TestCase;
- class UserNotificationControllerTest extends TestCase
- {
- use RefreshDatabase;
- public function test_user_can_mark_all_own_notifications_as_read(): void
- {
- $user = User::factory()->create();
- $otherUser = User::factory()->create();
- $unreadNotifications = collect([
- $this->createNotification($user->id),
- $this->createNotification($user->id),
- ]);
- $alreadyReadAt = now()->subDay();
- $alreadyReadNotification = $this->createNotification($user->id, $alreadyReadAt);
- $otherUserNotification = $this->createNotification($otherUser->id);
- $response = $this->actingAs($user)->postJson(route('notifications.read-all'));
- $response
- ->assertOk()
- ->assertJson([
- 'ok' => true,
- 'updated' => 2,
- 'unread' => 0,
- ]);
- foreach ($unreadNotifications as $notification) {
- $this->assertNotNull($notification->fresh()->read_at);
- }
- $this->assertSame(
- $alreadyReadAt->format('Y-m-d H:i:s'),
- $alreadyReadNotification->fresh()->read_at?->format('Y-m-d H:i:s')
- );
- $this->assertNull($otherUserNotification->fresh()->read_at);
- }
- private function createNotification(int $userId, $readAt = null): UserNotification
- {
- return UserNotification::query()->create([
- 'user_id' => $userId,
- 'type' => UserNotification::TYPE_PLATFORM,
- 'event' => UserNotification::EVENT_CREATED,
- 'title' => 'Тестовое уведомление',
- 'message' => 'Тестовое сообщение',
- 'message_html' => '<p>Тестовое сообщение</p>',
- 'read_at' => $readAt,
- ]);
- }
- }
|