feat: notifications on comments

This commit is contained in:
2026-05-23 10:50:34 +02:00
parent cd38d88fe7
commit 49419c1e7b
5 changed files with 164 additions and 1 deletions
@@ -4,6 +4,8 @@ use App\Models\MealPosts;
use App\Models\PostReviews;
use App\Models\User;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Http\Client\Request;
use Illuminate\Support\Facades\Http;
use Laravel\Sanctum\Sanctum;
uses(RefreshDatabase::class);
@@ -40,6 +42,87 @@ it('creates one review per user for a meal post', function () {
expect(PostReviews::query()->count())->toBe(1);
});
it('sends a push notification to the meal owner when another user comments on their meal', function () {
Http::fake([
'https://exp.host/*' => Http::response([
'data' => [
['status' => 'ok', 'id' => 'ticket-one'],
],
]),
]);
$owner = User::factory()->create();
$owner->deviceTokens()->create([
'expo_push_token' => 'ExponentPushToken[owner]',
'platform' => 'ios',
]);
$reviewer = User::factory()->create(['name' => 'Alex']);
$mealPost = MealPosts::factory()
->for($owner, 'user')
->create(['title' => 'Recovery bowl']);
Sanctum::actingAs($reviewer);
$this->postJson("/api/meal-posts/{$mealPost->id}/reviews", [
'rating' => 5,
'comment' => 'Très bon repas.',
])->assertCreated();
Http::assertSent(fn (Request $request) => $request->url() === 'https://exp.host/--/api/v2/push/send'
&& $request->data() === [
[
'to' => 'ExponentPushToken[owner]',
'title' => 'New comment',
'body' => 'Alex commented on your meal "Recovery bowl".',
'sound' => 'default',
'channelId' => 'default',
'data' => [
'url' => "bowly://meals/{$mealPost->id}",
],
],
]);
});
it('does not send a push notification for rating-only reviews', function () {
Http::fake();
$owner = User::factory()->create();
$owner->deviceTokens()->create([
'expo_push_token' => 'ExponentPushToken[owner]',
'platform' => 'ios',
]);
$reviewer = User::factory()->create();
$mealPost = MealPosts::factory()->for($owner, 'user')->create();
Sanctum::actingAs($reviewer);
$this->postJson("/api/meal-posts/{$mealPost->id}/reviews", [
'rating' => 5,
])->assertCreated();
Http::assertNothingSent();
});
it('does not notify the owner when they comment on their own meal', function () {
Http::fake();
$owner = User::factory()->create();
$owner->deviceTokens()->create([
'expo_push_token' => 'ExponentPushToken[owner]',
'platform' => 'ios',
]);
$mealPost = MealPosts::factory()->for($owner, 'user')->create();
Sanctum::actingAs($owner);
$this->postJson("/api/meal-posts/{$mealPost->id}/reviews", [
'rating' => 5,
'comment' => 'Note personnelle.',
])->assertCreated();
Http::assertNothingSent();
});
it('lists reviews for a single meal post', function () {
$user = User::factory()->create();
$mealPost = MealPosts::factory()->create();