Skip to content

Commit 3b8c3eb

Browse files
committed
feat: (LAR-87) Update spamReportsTest, ReportSpamAction and ReportSpam
1 parent 1ca5625 commit 3b8c3eb

File tree

4 files changed

+75
-59
lines changed

4 files changed

+75
-59
lines changed

app/Actions/SpamReport/ReportSpamAction.php

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -12,31 +12,33 @@
1212

1313
class ReportSpamAction
1414
{
15-
public function execute(int $contentId, string $contentType, string $reason): SpamReport
15+
public function execute(array $data): void
1616
{
17-
$user = Auth::user();
17+
$user = $data['user'];
18+
$recordId = $data['recordId'];
19+
$recordType = $data['recordType'];
20+
$reason = $data['reason'];
1821

19-
if (! $user->hasVerifiedEmail()) {
20-
throw new \Exception('Vous devez avoir un email vérifié pour signaler du contenu.');
21-
}
22+
$alreadyReported = SpamReport::where([
23+
'user_id' => $user->id,
24+
'reportable_id' => $recordId,
25+
'reportable_type' => $recordType,
26+
])->exists();
2227

23-
if (SpamReport::where('user_id', $user->id)
24-
->where('reportable_id', $contentId)
25-
->where('reportable_type', $contentType)
26-
->exists()) {
27-
throw new \Exception('Vous avez déjà signalé ce contenu.');
28+
if ($alreadyReported) {
29+
throw new \Exception('already_reported');
2830
}
2931

30-
$report = SpamReport::create([
32+
SpamReport::create([
3133
'user_id' => $user->id,
32-
'reportable_id' => $contentId,
33-
'reportable_type' => $contentType,
34+
'reportable_id' => $recordId,
35+
'reportable_type' => $recordType,
3436
'reason' => $reason,
3537
]);
3638

37-
$adminsAndModerators = User::whereIn('role', ['admin', 'moderator'])->get();
38-
Notification::send($adminsAndModerators, new ContentReportedSpamNotification($contentId, $contentType));
39-
40-
return $report;
39+
Notification::send(
40+
User::whereIn('role', ['admin', 'moderator'])->get(),
41+
new ContentReportedSpamNotification($recordId, $recordType)
42+
);
4143
}
4244
}

app/Livewire/ReportSpam.php

Lines changed: 20 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,10 @@
44

55
namespace App\Livewire;
66

7-
use App\Models\User;
8-
use Livewire\Component;
7+
use App\Actions\SpamReport\ReportSpamAction;
98
use App\Models\SpamReport;
109
use Illuminate\Support\Facades\Auth;
11-
use Illuminate\Support\Facades\Notification;
12-
use App\Notifications\ContentReportedSpamNotification;
10+
use Livewire\Component;
1311

1412
final class ReportSpam extends Component
1513
{
@@ -48,33 +46,33 @@ public function report(): void
4846
{
4947
$user = Auth::user();
5048

51-
if (!$user) {
49+
if (! $user) {
5250
abort(403, 'Vous devez être connecté pour signaler du contenu.');
5351
}
5452

55-
if (!$user->hasVerifiedEmail()) {
53+
if (! $user->hasVerifiedEmail()) {
5654
abort(403, 'Vous devez avoir un email vérifié pour signaler du contenu');
5755
}
5856

59-
if ($this->alreadyReported) {
60-
$this->addError('alreadyReported', 'Vous avez déjà signalé ce contenu.');
61-
62-
return;
63-
}
57+
try {
58+
app(ReportSpamAction::class)->execute([
59+
'user' => $user,
60+
'recordId' => $this->recordId,
61+
'recordType' => $this->recordType,
62+
'reason' => $this->reason,
63+
]);
6464

65-
SpamReport::create([
66-
'user_id' => Auth::id(),
67-
'reportable_id' => $this->recordId,
68-
'reportable_type' => $this->recordType,
69-
'reason' => $this->reason,
70-
]);
65+
$this->reset('reason');
7166

72-
Notification::send(User::whereIn('role', ['admin', 'moderator'])->get(), new ContentReportedSpamNotification($this->recordId, $this->recordType));
67+
session()->flash('message', 'Signalement créé avec succès');
7368

74-
$this->alreadyReported = true;
75-
$this->reset('reason');
76-
77-
session()->flash('message', 'Signalement créé avec succès');
69+
} catch (\Exception $e) {
70+
if ($e->getMessage() === 'already_reported') {
71+
$this->addError('alreadyReported', 'Vous avez déjà signalé ce contenu.');
72+
} else {
73+
session()->flash('error', 'Une erreur est survenue lors du signalement.');
74+
}
75+
}
7876
}
7977

8078
public function render()

resources/views/livewire/components/spam-report.blade.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,12 @@
88
<button class="btn btn-link text-danger" wire:click="openModal">Signaler</button>
99
@endif
1010
@endcan
11+
12+
@if ($errors->has('alreadyReported'))
13+
<div class="text-danger">
14+
{{ $errors->first('alreadyReported') }}
15+
</div>
16+
@endif
1117

1218
<!-- Modal -->
1319
@if ($isModalOpen)
@@ -39,7 +45,6 @@ class="form-control"
3945
</div>
4046
@endif
4147

42-
<!-- Messages de feedback -->
4348
@if (session()->has('message'))
4449
<div class="alert alert-success mt-2">
4550
{{ session('message') }}

tests/Feature/Actions/SpamReports/SpamReportsTest.php

Lines changed: 30 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,10 @@
55
use App\Models\User;
66
use App\Models\Thread;
77
use Livewire\Livewire;
8+
use App\Models\SpamReport;
89
use App\Livewire\ReportSpam;
910
use Spatie\Permission\Models\Role;
11+
use App\Actions\SpamReport\ReportSpamAction;
1012
use Illuminate\Support\Facades\Notification;
1113
use Illuminate\Foundation\Testing\RefreshDatabase;
1214
use App\Notifications\ContentReportedSpamNotification;
@@ -45,39 +47,48 @@
4547

4648
$thread = Thread::factory()->create();
4749

50+
$this->mock(ReportSpamAction::class)
51+
->shouldReceive('execute')
52+
->once()
53+
->with([
54+
'user' => $user,
55+
'recordId' => $thread->id,
56+
'recordType' => Thread::class,
57+
'reason' => 'Contenu inapproprié',
58+
]);
59+
4860
Livewire::test(ReportSpam::class, [
4961
'recordId' => $thread->id,
5062
'recordType' => Thread::class,
5163
])
5264
->set('reason', 'Contenu inapproprié')
5365
->call('report');
66+
});
5467

55-
$this->assertDatabaseHas('spam_reports', [
68+
it('does not allow a user to report the same content multiple times', function (): void {
69+
$user = User::factory()->create();
70+
$this->actingAs($user);
71+
72+
$thread = Thread::factory()->create();
73+
74+
SpamReport::create([
5675
'user_id' => $user->id,
5776
'reportable_id' => $thread->id,
5877
'reportable_type' => Thread::class,
5978
'reason' => 'Contenu inapproprié',
6079
]);
61-
});
6280

63-
// it('does not allow a user to report the same content multiple times', function (): void {
64-
// $user = User::factory()->create();
65-
// $this->actingAs($user);
81+
$this->mock(ReportSpamAction::class)
82+
->shouldNotReceive('execute');
6683

67-
// $thread = Thread::factory()->create();
68-
69-
// Livewire::test(ReportSpam::class, [
70-
// 'recordId' => $thread->id,
71-
// 'recordType' => Thread::class,
72-
// ])
73-
// ->set('reason', 'Contenu inapproprié')
74-
// ->call('report')
75-
// ->assertSessionHas('message', 'Signalement créé avec succès');
76-
77-
// Livewire::test('spam-report-content', ['contentId' => $thread->id, 'contentType' => Thread::class])
78-
// ->call('spamReport', 'Contenu toujours inapproprié')
79-
// ->assertSee('Vous avez déjà signalé ce contenu.');
80-
// });
84+
Livewire::test(ReportSpam::class, [
85+
'recordId' => $thread->id,
86+
'recordType' => Thread::class,
87+
])
88+
->set('reason', 'Même Contenu inapproprié')
89+
->call('report')
90+
->assertSee('Vous avez déjà signalé ce contenu.');
91+
});
8192

8293
// it('shows a reported badge only to the user who reported the content', function (): void {
8394
// $reporter = User::factory()->create();

0 commit comments

Comments
 (0)