Skip to content

Commit ffbc8e2

Browse files
committed
refact(test): refactoring test and logic of action
1 parent 614bd11 commit ffbc8e2

File tree

13 files changed

+56
-29
lines changed

13 files changed

+56
-29
lines changed

app/Actions/Discussion/ConvertDiscussionToThreadAction.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,9 @@
1010

1111
final class ConvertDiscussionToThreadAction
1212
{
13-
public function execute(Discussion $discussion, bool $isAdmin = false): Thread
13+
public function execute(Discussion $discussion): Thread
1414
{
15-
return DB::transaction(function () use ($discussion, $isAdmin) {
15+
return DB::transaction(function () use ($discussion) {
1616
$thread = Thread::create([
1717
'title' => $discussion->title,
1818
'slug' => $discussion->slug,
@@ -28,7 +28,7 @@ public function execute(Discussion $discussion, bool $isAdmin = false): Thread
2828

2929
$discussion->delete();
3030

31-
app(NotifyUsersOfThreadConversion::class)->execute($thread, $isAdmin);
31+
app(NotifyUsersOfThreadConversion::class)->execute($thread);
3232

3333
return $thread;
3434
});

app/Actions/Discussion/NotifyUsersOfThreadConversion.php

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,19 +8,18 @@
88
use App\Models\User;
99
use App\Notifications\ThreadConvertedByAdmin;
1010
use App\Notifications\ThreadConvertedByCreator;
11+
use Illuminate\Support\Facades\Auth;
1112

1213
final class NotifyUsersOfThreadConversion
1314
{
14-
public function execute(Thread $thread, bool $isAdmin = false): void
15+
public function execute(Thread $thread): void
1516
{
1617
$usersToNotify = $thread->replies()->pluck('user_id')->unique()->toArray();
1718

1819
User::whereIn('id', $usersToNotify)->get()->each->notify(new ThreadConvertedByCreator($thread));
1920

20-
if ($isAdmin) {
21-
$creator = $thread->user;
22-
23-
$creator->notify(new ThreadConvertedByAdmin($thread));
21+
if (Auth::check() && Auth::user()->isAdmin()) {
22+
$thread->user->notify(new ThreadConvertedByAdmin($thread));
2423
}
2524
}
2625
}

app/Livewire/Modals/ConvertDiscussion.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,7 @@
66

77
use App\Actions\Discussion\ConvertDiscussionToThreadAction;
88
use App\Models\Discussion;
9-
use App\Policies\DiscussionPolicy;
109
use Illuminate\Contracts\View\View;
11-
use Illuminate\Support\Facades\Auth;
1210
use LivewireUI\Modal\ModalComponent;
1311

1412
final class ConvertDiscussion extends ModalComponent
@@ -21,7 +19,7 @@ public function save(): void
2119

2220
$this->authorize('convertedToThread', $discussion);
2321

24-
$thread = app(ConvertDiscussionToThreadAction::class)->execute($discussion, Auth::user()->isAdmin());
22+
$thread = app(ConvertDiscussionToThreadAction::class)->execute($discussion);
2523

2624
$this->redirectRoute('forum.show', $thread, navigate: true);
2725
}

app/Notifications/ThreadConvertedByAdmin.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ public function via(object $notifiable): array
3131
public function toMail(object $notifiable): MailMessage
3232
{
3333
return (new MailMessage)
34-
->subject('Discussion Converted by Admin')
34+
->subject('pages/discussion.converted_by_admin')
3535
->greeting('Hello!')
3636
->line('An admin has converted a discussion to a thread.')
3737
->line('Thread Title: '.$this->thread->title)

app/Notifications/ThreadConvertedByCreator.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ public function via(object $notifiable): array
3131
public function toMail(object $notifiable): MailMessage
3232
{
3333
return (new MailMessage)
34-
->subject(__('Discussion Converted to Thread'))
34+
->subject(__('pages/discussion.converted_by_creator'))
3535
->line('A discussion you participated in has been converted to a thread.')
3636
->line('Thread Title: '.$this->thread->title)
3737
->action('View Thread', route('forum.show', $this->thread))

lang/en/actions.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,5 +12,6 @@
1212
'save' => 'Save',
1313
'ban' => 'Ban',
1414
'unban' => 'Cancel ban',
15+
'confirm' => 'Confirm',
1516

1617
];

lang/en/pages/discutssion.php renamed to lang/en/pages/discussion.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,5 +19,10 @@
1919
'active' => 'Actif',
2020
],
2121
'comments_count' => 'Commentaires (:count)',
22+
'convert_to_thread' => 'Convert to thread',
23+
'confirm_conversion' => 'Confirm conversion',
24+
'text_confirmation' => 'Do you really want to turn this discussion into a topic?',
25+
'converted_by_creator' => 'Discussion Converted to Thread',
26+
'converted_by_admin' => 'Discussion Converted to Thread by the administrator',
2227

2328
];

lang/fr/actions.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,5 +12,6 @@
1212
'save' => 'Enregistrer',
1313
'ban' => 'Bannir',
1414
'unban' => 'Dé-bannir',
15+
'confirm' => 'Confirmer',
1516

1617
];

lang/fr/pages/discussion.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,5 +19,10 @@
1919
'active' => 'Actif',
2020
],
2121
'comments_count' => 'Commentaires (:count)',
22+
'convert_to_thread' => 'Convertir en sujet',
23+
'confirm_conversion' => 'Confirmez la conversion',
24+
'text_confirmation' => 'Voulez-vous vraiment transformer cette discussion en sujet de forum?',
25+
'converted_by_creator' => 'Discussion convertie en sujet par le createur',
26+
'converted_by_admin' => 'Discussion convertie en sujet par l\'administrateur',
2227

2328
];

resources/views/livewire/modals/convert-discussion.blade.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,22 +5,22 @@
55
form-action="save"
66
>
77
<x-slot name="title">
8-
{{ __("Confirmez la convertion") }}
8+
{{ __("pages/discussion.confirm_conversion") }}
99
</x-slot>
1010

1111
<div class="space-y-4 pb-5">
1212
<div class="grid grid-cols-2 gap-4">
1313
<div class="space-y-2">
1414
<x-slot name="content">
15-
{{ __("Voulez-vous vraiment convertir cette discussion en thread ?") }}
15+
{{ __("pages/discussion.text_confirmation") }}
1616
</x-slot>
1717
</div>
1818
</div>
1919
</div>
2020

2121
<x-slot name="buttons">
2222
<x-buttons.submit
23-
:title="__('Confirmer')"
23+
:title="__('action.confirm')"
2424
wire:loading.attr="data-loading"
2525
class="w-full sm:ml-3 sm:w-auto"
2626
/>
@@ -29,7 +29,7 @@ class="w-full sm:ml-3 sm:w-auto"
2929
wire:click="$dispatch('closeModal')"
3030
class="w-full px-4 py-2 mt-3 text-sm sm:mt-0 sm:w-auto"
3131
>
32-
{{ __('Annuler') }}
32+
{{ __('action.cancel') }}
3333
</x-buttons.default>
3434
</x-slot>
3535
</x-modal>

resources/views/livewire/pages/discussions/single-discussion.blade.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -97,23 +97,24 @@ class="mx-auto mt-6 text-sm prose prose-sm prose-green max-w-none dark:prose-inv
9797
href="{{ route('discussions.edit', $discussion) }}"
9898
class="text-sm leading-5 text-gray-500 dark:text-gray-400 hover:underline focus:outline-none"
9999
>
100-
{{ __('Éditer') }}
100+
{{ __('action.edit') }}
101101
</x-link>
102102
<span class="font-medium text-gray-500 dark:text-gray-400">·</span>
103103
<button
104104
onclick="Livewire.dispatch('openModal', {component: 'modals.delete-discussion', arguments: {{ json_encode([$discussion->id]) }}})"
105105
type="button"
106106
class="text-sm leading-5 text-red-500 hover:underline focus:outline-none"
107107
>
108-
{{ __('Supprimer') }}
108+
{{ __('actions.delete') }}
109109
</button>
110110
@can('convertedToThread', $discussion)
111111
<span class="font-medium text-gray-500 dark:text-gray-400">·</span>
112112
<button
113113
onclick="Livewire.dispatch('openModal', {component: 'modals.convert-discussion', arguments: { discussionId: {{ $discussion->id }} }})"
114114
class="text-sm leading-5 text-gray-500 dark:text-gray-400 hover:underline focus:outline-none"
115115
>
116-
{{ __('Convertir en Thread') }}
116+
{{ __('pages/discussion.convert_to_thread') }}
117+
{{ __('pages/home.description') }}
117118
</button>
118119
@endcan
119120
</div>

tests/Feature/Actions/Discussion/ConvertDiscussionToThreadActionTest.php

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,18 @@
66
use App\Models\Discussion;
77
use App\Models\Reply;
88
use App\Models\Thread;
9+
use Illuminate\Support\Facades\Notification;
10+
use Spatie\Permission\Models\Role;
911

1012
beforeEach(function (): void {
11-
$this->discussion = Discussion::factory()->create();
13+
$this->user = $this->login();
14+
$this->discussion = Discussion::factory()->create(['user_id' => $this->user->id]);
15+
Role::create(['name' => 'admin']);
16+
Notification::fake();
1217
});
1318

1419
describe(ConvertDiscussionToThreadAction::class, function (): void {
15-
it('can converts a discussion to a thread', function (): void {
20+
it('allows discussion author to convert his discussion to a forum topic', function (): void {
1621
$replies = Reply::factory()->count(3)->create([
1722
'replyable_type' => 'discussion',
1823
'replyable_id' => $this->discussion->id,
@@ -29,11 +34,22 @@
2934
expect($updatedReply->replyable_type)->toBe('thread')
3035
->and($updatedReply->replyable_id)->toBe($thread->id);
3136
});
37+
38+
Notification::assertCount(3);
3239
});
3340

34-
it('can handles admin conversion', function (): void {
35-
$thread = app(ConvertDiscussionToThreadAction::class)->execute(discussion: $this->discussion, isAdmin: true);
41+
it('allows admin users to convert any discussion to a forum topic', function (): void {
42+
$this->user->assignRole('admin');
43+
44+
Reply::factory()->count(3)->create([
45+
'replyable_type' => 'discussion',
46+
'replyable_id' => $this->discussion->id,
47+
]);
48+
49+
$thread = app(ConvertDiscussionToThreadAction::class)->execute(discussion: $this->discussion);
3650

3751
expect($thread)->toBeInstanceOf(Thread::class);
52+
53+
Notification::assertCount(4);
3854
});
3955
});

tests/Feature/Livewire/Modal/ConvertDiscussionTest.php

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,16 @@
88
use Livewire\Livewire;
99

1010
beforeEach(function (): void {
11-
$user = User::factory()->create();
12-
$this->user = $this->actingAs($user);
13-
$this->discussion = Discussion::factory()->create();
11+
$this->user = $this->login();
1412
});
1513

1614
describe(ConvertDiscussion::class, function (): void {
17-
it('requires authorization to convert discussion', function (): void {
15+
it('requires authorization for user to convert discussion', function (): void {
16+
$user = User::factory()->create();
17+
$discussion = Discussion::factory()->create(['user_id' => $user->id]);
18+
1819
Livewire::test(ConvertDiscussion::class)
19-
->set('discussionId', $this->discussion->id)
20+
->set('discussionId', $discussion->id)
2021
->call('save')
2122
->assertForbidden();
2223
});

0 commit comments

Comments
 (0)