feat: report reviews etc ...

This commit is contained in:
2026-05-23 12:15:11 +02:00
parent 49419c1e7b
commit 4af28f3828
15 changed files with 146 additions and 0 deletions
+35
View File
@@ -4,6 +4,7 @@ use App\Enums\ReportReason;
use App\Enums\UserRole;
use App\Models\MealPosts;
use App\Models\ModerationCase;
use App\Models\PostReviews;
use App\Models\Report;
use App\Models\User;
use Illuminate\Foundation\Testing\RefreshDatabase;
@@ -96,6 +97,40 @@ it('reports users and prevents reporting own profile', function () {
->assertJsonPath('message', __('api.reports.cannot_report_self'));
});
it('reports post reviews and prevents reporting own review', function () {
$reviewAuthor = User::factory()->create();
$reporter = User::factory()->create();
$review = PostReviews::query()->create([
'meal_post_id' => MealPosts::factory()->create()->id,
'user_id' => $reviewAuthor->id,
'rating' => 2,
'comment' => 'Commentaire problématique.',
]);
Sanctum::actingAs($reporter);
$this->postJson("/api/post-reviews/{$review->id}/reports", [
'reason' => ReportReason::INAPPROPRIATE->value,
])
->assertCreated()
->assertJsonPath('data.reportableType', 'post_review')
->assertJsonPath('data.reportableId', $review->id);
$this->assertDatabaseHas('moderation_cases', [
'caseable_type' => PostReviews::class,
'caseable_id' => $review->id,
'threshold' => 3,
]);
Sanctum::actingAs($reviewAuthor);
$this->postJson("/api/post-reviews/{$review->id}/reports", [
'reason' => ReportReason::OTHER->value,
])
->assertUnprocessable()
->assertJsonPath('message', __('api.reports.cannot_report_own_review'));
});
it('notifies moderators once when a report threshold is reached', function () {
$admin = User::factory()->create(['role' => UserRole::ADMIN]);
$moderator = User::factory()->create(['role' => UserRole::MODERATOR]);
@@ -156,6 +156,41 @@ it('lists reviews for a single meal post', function () {
->assertJsonMissing(['id' => $otherReview->id]);
});
it('shows the authenticated users review for a meal post', function () {
$user = User::factory()->create();
$mealPost = MealPosts::factory()->create();
$review = PostReviews::query()->create([
'meal_post_id' => $mealPost->id,
'user_id' => $user->id,
'rating' => 4,
'comment' => 'Mon avis.',
]);
PostReviews::query()->create([
'meal_post_id' => $mealPost->id,
'user_id' => User::factory()->create()->id,
'rating' => 5,
'comment' => 'Un autre avis.',
]);
Sanctum::actingAs($user);
$this->getJson("/api/meal-posts/{$mealPost->id}/my-review")
->assertOk()
->assertJsonPath('data.id', $review->id)
->assertJsonPath('data.user_id', $user->id);
});
it('returns null when the authenticated user has not reviewed a meal post', function () {
$user = User::factory()->create();
$mealPost = MealPosts::factory()->create();
Sanctum::actingAs($user);
$this->getJson("/api/meal-posts/{$mealPost->id}/my-review")
->assertOk()
->assertJsonPath('data', null);
});
it('lets the review author update and delete their review', function () {
$user = User::factory()->create();
$review = PostReviews::query()->create([