From 7b8a253de6899fda1eb25288764c66296e604a79 Mon Sep 17 00:00:00 2001 From: Chri$ Date: Wed, 16 Oct 2024 17:52:42 +0200 Subject: [PATCH 1/6] feat:[LAR-32] Add article list wit action in cpanl --- app/Filament/Actions/ApprovedAction.php | 48 +++++++++++ app/Filament/Actions/DeclinedAction.php | 49 +++++++++++ app/Filament/Clusters/Articles.php | 10 +++ app/Filament/Resources/ArticleResource.php | 83 +++++++++++++++++++ .../ArticleResource/Pages/ListArticles.php | 19 +++++ app/Providers/Filament/AdminPanelProvider.php | 1 + tests/Feature/Filament/ArticleTest.php | 74 +++++++++++++++++ 7 files changed, 284 insertions(+) create mode 100644 app/Filament/Actions/ApprovedAction.php create mode 100644 app/Filament/Actions/DeclinedAction.php create mode 100644 app/Filament/Clusters/Articles.php create mode 100644 app/Filament/Resources/ArticleResource.php create mode 100644 app/Filament/Resources/ArticleResource/Pages/ListArticles.php create mode 100644 tests/Feature/Filament/ArticleTest.php diff --git a/app/Filament/Actions/ApprovedAction.php b/app/Filament/Actions/ApprovedAction.php new file mode 100644 index 00000000..e4710270 --- /dev/null +++ b/app/Filament/Actions/ApprovedAction.php @@ -0,0 +1,48 @@ +label(__('Approuver')); + + $this->modalHeading(fn (): string => __('Voulez vous approuver cet article', ['label' => $this->getRecordTitle()])); + + $this->modalSubmitActionLabel(__('Approuver')); + + $this->successNotificationTitle(__('Opération effectuée avec succès')); + + $this->color('success'); + + $this->icon( 'heroicon-s-check'); + + $this->requiresConfirmation(); + + $this->modalIcon('heroicon-s-check'); + + $this->action(function (): void { + $result = $this->process(static fn (Model $record) => $record->update(['approved_at' => now(), 'declined_at' => null])); + + if (! $result) { + $this->failure(); + + return; + } + + $this->success(); + }); + } +} diff --git a/app/Filament/Actions/DeclinedAction.php b/app/Filament/Actions/DeclinedAction.php new file mode 100644 index 00000000..20c5235d --- /dev/null +++ b/app/Filament/Actions/DeclinedAction.php @@ -0,0 +1,49 @@ +label(__('Décliner')); + + $this->modalHeading(fn (): string => __('Voulez vous décliner cet article', ['label' => $this->getRecordTitle()])); + + $this->modalSubmitActionLabel(__('Décliner')); + + $this->successNotificationTitle(__('Opération effectuée avec succès')); + + $this->color('warning'); + + $this->icon( 'heroicon-s-x-mark'); + + $this->requiresConfirmation(); + + $this->modalIcon('heroicon-s-x-mark'); + + $this->action(function (): void { + $result = $this->process(static fn (Model $record) => $record->update(['declined_at' => now(), 'approved_at' => null])); + + if (! $result) { + $this->failure(); + + return; + } + + $this->success(); + }); + } +} diff --git a/app/Filament/Clusters/Articles.php b/app/Filament/Clusters/Articles.php new file mode 100644 index 00000000..c1500920 --- /dev/null +++ b/app/Filament/Clusters/Articles.php @@ -0,0 +1,10 @@ +columns([ + TextColumn::make('title') + ->label('Titre') + ->sortable(), + TextColumn::make('status') + ->label('Status') + ->getStateUsing(function ($record) { + if ($record->approved_at) { + return 'Approuver'; + } elseif ($record->declined_at) { + return 'Décliner'; + } elseif($record->submitted_at) { + return 'Soumis'; + }else{ + return 'Brouillon'; + } + }) + ->colors([ + 'success' => 'Approuver', + 'danger' => 'Décliner', + 'warning' => 'Soumis', + 'default' => 'Brouillon', + ]) + ->badge(), + TextColumn::make('submitted_at') + ->label('Date de soumission') + ->dateTime(), + TextColumn::make('user.name') + ->label('Auteur') + ->sortable() + ]) + ->filters([ + Filter::make('submitted_at')->query( fn (Builder $query) => $query->whereNotNull('submitted_at'))->label('Soumis'), + Filter::make('declined_at')->query( fn (Builder $query) => $query->whereNotNull('declined_at'))->label('Décliner'), + Filter::make('approved_at')->query( fn (Builder $query) => $query->whereNotNull('approved_at'))->label('Approuver') + ]) + + ->actions([ + ActionGroup::make([ + ApprovedAction::make('approved'), + DeclinedAction::make('declined'), + Tables\Actions\DeleteAction::make('delete'), + ]), + + ]) + ->bulkActions([ + Tables\Actions\BulkActionGroup::make([ + Tables\Actions\DeleteBulkAction::make(), + ]), + ]); + } + + public static function getPages(): array + { + return [ + 'index' => Pages\ListArticles::route('/'), + ]; + } +} diff --git a/app/Filament/Resources/ArticleResource/Pages/ListArticles.php b/app/Filament/Resources/ArticleResource/Pages/ListArticles.php new file mode 100644 index 00000000..13cef1d2 --- /dev/null +++ b/app/Filament/Resources/ArticleResource/Pages/ListArticles.php @@ -0,0 +1,19 @@ +discoverResources(in: app_path('Filament/Resources'), for: 'App\\Filament\\Resources') ->discoverPages(in: app_path('Filament/Pages'), for: 'App\\Filament\\Pages') ->discoverWidgets(in: app_path('Filament/Widgets'), for: 'App\\Filament\\Widgets') + ->discoverClusters(in: app_path('Filament/Clusters'), for: 'App\\Filament\\Clusters') ->pages([ Pages\Dashboard::class, ]) diff --git a/tests/Feature/Filament/ArticleTest.php b/tests/Feature/Filament/ArticleTest.php new file mode 100644 index 00000000..b6660e41 --- /dev/null +++ b/tests/Feature/Filament/ArticleTest.php @@ -0,0 +1,74 @@ +user = $this->login(); + $this->articles = Article::factory()->count(10)->create([ + 'submitted_at' => now(), + ]); +}); + +describe(ArticleResource::class, function (): void { + + it('page can display table with records', function (): void { + Livewire::test(ArticleResource\Pages\ListArticles::class) + ->assertCanSeeTableRecords($this->articles); + }); + + it('table can render columns', function (): void { + Livewire::test(ArticleResource\Pages\ListArticles::class) + ->assertCanRenderTableColumn('title') + ->assertCanRenderTableColumn('status') + ->assertCanRenderTableColumn('status') + ->assertCanRenderTableColumn('submitted_at'); + }); + + it('admin user can approved article', function (): void { + $article = Article::factory()->create(['submitted_at' => now()]); + + Livewire::test(ArticleResource\Pages\ListArticles::class) + ->callTableAction('approved', $article); + + $article->refresh(); + + expect($article->approved_at) + ->not + ->toBe(null); + + expect($article->declined_at) + ->toBe(null); + }); + + it('admin user can declined article', function (): void { + $article = Article::factory()->create(['submitted_at' => now()]); + + Livewire::test(ArticleResource\Pages\ListArticles::class) + ->callTableAction('declined', $article); + + $article->refresh(); + + expect($article->declined_at) + ->not + ->toBe(null); + + expect($article->approved_at) + ->toBe(null); + }); + + // it('admin user can deleted article', function () { + // $article = Article::factory()->create(['submitted_at' => now()]); + + // Livewire::test(ArticleResource\Pages\ListArticles::class) + // ->callTableAction('delete', $article); + // //->callAction(DeleteAction::class); + + // expect(Article::find($article->id)) + // ->toBe(null); + // }); +})->group('articles'); From 63e70df8fc1d085a1949a9fedcb6de7bd7676564 Mon Sep 17 00:00:00 2001 From: Chri$ Date: Wed, 16 Oct 2024 18:44:49 +0200 Subject: [PATCH 2/6] refactor:[LAR-32] integrate review comment --- app/Filament/Actions/ApprovedAction.php | 7 +- app/Filament/Actions/DeclinedAction.php | 8 ++- app/Filament/Clusters/Articles.php | 6 +- app/Filament/Resources/ArticleResource.php | 66 ++++++++++--------- .../ArticleResource/Pages/ListArticles.php | 12 +--- tests/Feature/Filament/ArticleTest.php | 21 ++---- 6 files changed, 55 insertions(+), 65 deletions(-) diff --git a/app/Filament/Actions/ApprovedAction.php b/app/Filament/Actions/ApprovedAction.php index e4710270..9b1b87ab 100644 --- a/app/Filament/Actions/ApprovedAction.php +++ b/app/Filament/Actions/ApprovedAction.php @@ -1,13 +1,16 @@ color('success'); - $this->icon( 'heroicon-s-check'); + $this->icon('heroicon-s-check'); $this->requiresConfirmation(); diff --git a/app/Filament/Actions/DeclinedAction.php b/app/Filament/Actions/DeclinedAction.php index 20c5235d..1fede632 100644 --- a/app/Filament/Actions/DeclinedAction.php +++ b/app/Filament/Actions/DeclinedAction.php @@ -1,14 +1,16 @@ color('warning'); - $this->icon( 'heroicon-s-x-mark'); + $this->icon('heroicon-s-x-mark'); $this->requiresConfirmation(); diff --git a/app/Filament/Clusters/Articles.php b/app/Filament/Clusters/Articles.php index c1500920..bdc8b64d 100644 --- a/app/Filament/Clusters/Articles.php +++ b/app/Filament/Clusters/Articles.php @@ -1,10 +1,12 @@ columns([ TextColumn::make('title') - ->label('Titre') - ->sortable(), + ->label('Titre') + ->sortable(), TextColumn::make('status') - ->label('Status') - ->getStateUsing(function ($record) { - if ($record->approved_at) { - return 'Approuver'; - } elseif ($record->declined_at) { - return 'Décliner'; - } elseif($record->submitted_at) { - return 'Soumis'; - }else{ - return 'Brouillon'; - } - }) - ->colors([ - 'success' => 'Approuver', - 'danger' => 'Décliner', - 'warning' => 'Soumis', - 'default' => 'Brouillon', - ]) - ->badge(), + ->getStateUsing(function ($record) { + if ($record->approved_at) { + return 'Approuver'; + } elseif ($record->declined_at) { + return 'Décliner'; + } elseif ($record->submitted_at) { + return 'Soumis'; + } else { + return 'Brouillon'; + } + }) + ->colors([ + 'success' => 'Approuver', + 'danger' => 'Décliner', + 'warning' => 'Soumis', + 'default' => 'Brouillon', + ]) + ->badge(), TextColumn::make('submitted_at') - ->label('Date de soumission') - ->dateTime(), + ->label('Date de soumission') + ->dateTime(), TextColumn::make('user.name') - ->label('Auteur') - ->sortable() + ->label('Auteur') + ->sortable(), ]) ->filters([ - Filter::make('submitted_at')->query( fn (Builder $query) => $query->whereNotNull('submitted_at'))->label('Soumis'), - Filter::make('declined_at')->query( fn (Builder $query) => $query->whereNotNull('declined_at'))->label('Décliner'), - Filter::make('approved_at')->query( fn (Builder $query) => $query->whereNotNull('approved_at'))->label('Approuver') + Filter::make('submitted_at')->query(fn (Builder $query) => $query->whereNotNull('submitted_at'))->label('Soumis'), + Filter::make('declined_at')->query(fn (Builder $query) => $query->whereNotNull('declined_at'))->label('Décliner'), + Filter::make('approved_at')->query(fn (Builder $query) => $query->whereNotNull('approved_at'))->label('Approuver'), ]) ->actions([ diff --git a/app/Filament/Resources/ArticleResource/Pages/ListArticles.php b/app/Filament/Resources/ArticleResource/Pages/ListArticles.php index 13cef1d2..0d007448 100644 --- a/app/Filament/Resources/ArticleResource/Pages/ListArticles.php +++ b/app/Filament/Resources/ArticleResource/Pages/ListArticles.php @@ -1,19 +1,13 @@ approved_at) ->not - ->toBe(null); - - expect($article->declined_at) + ->toBe(null) + ->and($article->declined_at) ->toBe(null); }); @@ -55,20 +53,9 @@ expect($article->declined_at) ->not - ->toBe(null); - - expect($article->approved_at) + ->toBe(null) + ->and($article->approved_at) ->toBe(null); }); - // it('admin user can deleted article', function () { - // $article = Article::factory()->create(['submitted_at' => now()]); - - // Livewire::test(ArticleResource\Pages\ListArticles::class) - // ->callTableAction('delete', $article); - // //->callAction(DeleteAction::class); - - // expect(Article::find($article->id)) - // ->toBe(null); - // }); })->group('articles'); From ae7cff7dc7fff74373559a5729d241a63eadaaee Mon Sep 17 00:00:00 2001 From: Chri$ Date: Wed, 16 Oct 2024 21:35:58 +0200 Subject: [PATCH 3/6] refactor:[LAR-32] Remove action class(Approved and Declined) , Add filament builk acion and Feature test for approved and declined builk action --- app/Filament/Actions/ApprovedAction.php | 51 ----------------- app/Filament/Actions/DeclinedAction.php | 51 ----------------- app/Filament/Clusters/Articles.php | 12 ---- app/Filament/Resources/ArticleResource.php | 57 +++++++++++++++++-- app/Providers/Filament/AdminPanelProvider.php | 1 - tests/Feature/Filament/ArticleTest.php | 20 +++++++ 6 files changed, 73 insertions(+), 119 deletions(-) delete mode 100644 app/Filament/Actions/ApprovedAction.php delete mode 100644 app/Filament/Actions/DeclinedAction.php delete mode 100644 app/Filament/Clusters/Articles.php diff --git a/app/Filament/Actions/ApprovedAction.php b/app/Filament/Actions/ApprovedAction.php deleted file mode 100644 index 9b1b87ab..00000000 --- a/app/Filament/Actions/ApprovedAction.php +++ /dev/null @@ -1,51 +0,0 @@ -label(__('Approuver')); - - $this->modalHeading(fn (): string => __('Voulez vous approuver cet article', ['label' => $this->getRecordTitle()])); - - $this->modalSubmitActionLabel(__('Approuver')); - - $this->successNotificationTitle(__('Opération effectuée avec succès')); - - $this->color('success'); - - $this->icon('heroicon-s-check'); - - $this->requiresConfirmation(); - - $this->modalIcon('heroicon-s-check'); - - $this->action(function (): void { - $result = $this->process(static fn (Model $record) => $record->update(['approved_at' => now(), 'declined_at' => null])); - - if (! $result) { - $this->failure(); - - return; - } - - $this->success(); - }); - } -} diff --git a/app/Filament/Actions/DeclinedAction.php b/app/Filament/Actions/DeclinedAction.php deleted file mode 100644 index 1fede632..00000000 --- a/app/Filament/Actions/DeclinedAction.php +++ /dev/null @@ -1,51 +0,0 @@ -label(__('Décliner')); - - $this->modalHeading(fn (): string => __('Voulez vous décliner cet article', ['label' => $this->getRecordTitle()])); - - $this->modalSubmitActionLabel(__('Décliner')); - - $this->successNotificationTitle(__('Opération effectuée avec succès')); - - $this->color('warning'); - - $this->icon('heroicon-s-x-mark'); - - $this->requiresConfirmation(); - - $this->modalIcon('heroicon-s-x-mark'); - - $this->action(function (): void { - $result = $this->process(static fn (Model $record) => $record->update(['declined_at' => now(), 'approved_at' => null])); - - if (! $result) { - $this->failure(); - - return; - } - - $this->success(); - }); - } -} diff --git a/app/Filament/Clusters/Articles.php b/app/Filament/Clusters/Articles.php deleted file mode 100644 index bdc8b64d..00000000 --- a/app/Filament/Clusters/Articles.php +++ /dev/null @@ -1,12 +0,0 @@ -actions([ ActionGroup::make([ - ApprovedAction::make('approved'), - DeclinedAction::make('declined'), + Action::make('approved') + ->label('Approuver') + ->icon('heroicon-s-check') + ->color('success') + ->modalHeading(__('Voulez vous approuver cet article')) + ->successNotificationTitle(__('Opération effectuée avec succès')) + ->requiresConfirmation() + ->modalIcon('heroicon-s-check') + ->action(function ($record): void { + $record->approved_at = now(); + $record->declined_at = null; + $record->save(); + }), + Action::make('declined') + ->label('Décliner') + ->icon('heroicon-s-x-mark') + ->color('warning') + ->modalHeading(__('Voulez vous décliner cet article')) + ->successNotificationTitle(__('Opération effectuée avec succès')) + ->requiresConfirmation() + ->modalIcon('heroicon-s-x-mark') + ->action(function ($record): void { + $record->declined_at = now(); + $record->approved_at = null; + $record->save(); + }), + // DeclinedAction::make('declined'), Tables\Actions\DeleteAction::make('delete'), ]), ]) ->bulkActions([ Tables\Actions\BulkActionGroup::make([ + BulkAction::make('approved') + ->label('Approuver la sélection') + ->icon('heroicon-s-check') + ->color('success') + ->action(fn (Collection $records) => $records->each->update(['approved_at' => now(), 'declined_at' => null])) + ->deselectRecordsAfterCompletion() + ->requiresConfirmation() + ->modalIcon('heroicon-s-check') + ->modalHeading('Approuver') + ->modalSubheading('Voulez-vous vraiment approuver ces articles ?') + ->modalButton('Confirmer'), + BulkAction::make('declined') + ->label('Décliner la sélection') + ->icon('heroicon-s-x-mark') + ->color('warning') + ->action(fn (Collection $records) => $records->each->update(['declined_at' => now(), 'approved_at' => null])) + ->deselectRecordsAfterCompletion() + ->requiresConfirmation() + ->modalIcon('heroicon-s-x-mark') + ->modalHeading('Décliner') + ->modalSubheading('Voulez-vous vraiment décliner ces articles ?') + ->modalButton('Confirmer'), + Tables\Actions\DeleteBulkAction::make(), ]), ]); diff --git a/app/Providers/Filament/AdminPanelProvider.php b/app/Providers/Filament/AdminPanelProvider.php index 1bc60ea1..a668ad2f 100644 --- a/app/Providers/Filament/AdminPanelProvider.php +++ b/app/Providers/Filament/AdminPanelProvider.php @@ -39,7 +39,6 @@ public function panel(Panel $panel): Panel ->discoverResources(in: app_path('Filament/Resources'), for: 'App\\Filament\\Resources') ->discoverPages(in: app_path('Filament/Pages'), for: 'App\\Filament\\Pages') ->discoverWidgets(in: app_path('Filament/Widgets'), for: 'App\\Filament\\Widgets') - ->discoverClusters(in: app_path('Filament/Clusters'), for: 'App\\Filament\\Clusters') ->pages([ Pages\Dashboard::class, ]) diff --git a/tests/Feature/Filament/ArticleTest.php b/tests/Feature/Filament/ArticleTest.php index 5c071ca5..b9e9cbc6 100644 --- a/tests/Feature/Filament/ArticleTest.php +++ b/tests/Feature/Filament/ArticleTest.php @@ -58,4 +58,24 @@ ->toBe(null); }); + it('admin user can bulk approved articles', function (): void { + $articles = Article::factory()->count(5)->create(['submitted_at' => now()]); + + Livewire::test(ArticleResource\Pages\ListArticles::class) + ->callTableBulkAction('approved', $articles); + + expect(Article::whereNotNull('approved_at')->count()) + ->toBe(5); + }); + + it('admin user can bulk declined articles', function (): void { + $articles = Article::factory()->count(5)->create(['submitted_at' => now()]); + + Livewire::test(ArticleResource\Pages\ListArticles::class) + ->callTableBulkAction('declined', $articles); + + expect(Article::whereNotNull('declined_at')->count()) + ->toBe(5); + }); + })->group('articles'); From 7a651c15323604be1211517cfd838d9d981435b0 Mon Sep 17 00:00:00 2001 From: Chri$ Date: Thu, 17 Oct 2024 11:03:31 +0200 Subject: [PATCH 4/6] feat:[LAR-32] add visible control in make action --- app/Filament/Resources/ArticleResource.php | 11 ++++++----- tests/Feature/Filament/ArticleTest.php | 21 --------------------- 2 files changed, 6 insertions(+), 26 deletions(-) diff --git a/app/Filament/Resources/ArticleResource.php b/app/Filament/Resources/ArticleResource.php index f8869ecb..fec2c04d 100644 --- a/app/Filament/Resources/ArticleResource.php +++ b/app/Filament/Resources/ArticleResource.php @@ -65,6 +65,7 @@ public static function table(Table $table): Table ->actions([ ActionGroup::make([ Action::make('approved') + ->visible(fn ($record) => $record->submitted_at && (! $record->declined_at && ! $record->approved_at)) ->label('Approuver') ->icon('heroicon-s-check') ->color('success') @@ -74,10 +75,10 @@ public static function table(Table $table): Table ->modalIcon('heroicon-s-check') ->action(function ($record): void { $record->approved_at = now(); - $record->declined_at = null; $record->save(); }), Action::make('declined') + ->visible(fn ($record) => $record->submitted_at && (! $record->declined_at && ! $record->approved_at)) ->label('Décliner') ->icon('heroicon-s-x-mark') ->color('warning') @@ -87,10 +88,8 @@ public static function table(Table $table): Table ->modalIcon('heroicon-s-x-mark') ->action(function ($record): void { $record->declined_at = now(); - $record->approved_at = null; $record->save(); }), - // DeclinedAction::make('declined'), Tables\Actions\DeleteAction::make('delete'), ]), @@ -98,10 +97,11 @@ public static function table(Table $table): Table ->bulkActions([ Tables\Actions\BulkActionGroup::make([ BulkAction::make('approved') + ->visible(fn (?Collection $records) => ! empty($records) || $records?->contains(fn ($record) => $record->submitted_at)) ->label('Approuver la sélection') ->icon('heroicon-s-check') ->color('success') - ->action(fn (Collection $records) => $records->each->update(['approved_at' => now(), 'declined_at' => null])) + ->action(fn (Collection $records) => $records->each->update(['approved_at' => now()])) ->deselectRecordsAfterCompletion() ->requiresConfirmation() ->modalIcon('heroicon-s-check') @@ -109,10 +109,11 @@ public static function table(Table $table): Table ->modalSubheading('Voulez-vous vraiment approuver ces articles ?') ->modalButton('Confirmer'), BulkAction::make('declined') + ->visible(fn (?Collection $records) => ! empty($records) || $records?->contains(fn ($record) => $record->submitted_at)) ->label('Décliner la sélection') ->icon('heroicon-s-x-mark') ->color('warning') - ->action(fn (Collection $records) => $records->each->update(['declined_at' => now(), 'approved_at' => null])) + ->action(fn (Collection $records) => $records->each->update(['declined_at' => now()])) ->deselectRecordsAfterCompletion() ->requiresConfirmation() ->modalIcon('heroicon-s-x-mark') diff --git a/tests/Feature/Filament/ArticleTest.php b/tests/Feature/Filament/ArticleTest.php index b9e9cbc6..176a3771 100644 --- a/tests/Feature/Filament/ArticleTest.php +++ b/tests/Feature/Filament/ArticleTest.php @@ -57,25 +57,4 @@ ->and($article->approved_at) ->toBe(null); }); - - it('admin user can bulk approved articles', function (): void { - $articles = Article::factory()->count(5)->create(['submitted_at' => now()]); - - Livewire::test(ArticleResource\Pages\ListArticles::class) - ->callTableBulkAction('approved', $articles); - - expect(Article::whereNotNull('approved_at')->count()) - ->toBe(5); - }); - - it('admin user can bulk declined articles', function (): void { - $articles = Article::factory()->count(5)->create(['submitted_at' => now()]); - - Livewire::test(ArticleResource\Pages\ListArticles::class) - ->callTableBulkAction('declined', $articles); - - expect(Article::whereNotNull('declined_at')->count()) - ->toBe(5); - }); - })->group('articles'); From fbbeec4e61e35713693acddec89b5b5f16dc2029 Mon Sep 17 00:00:00 2001 From: Chri$ Date: Thu, 17 Oct 2024 11:13:52 +0200 Subject: [PATCH 5/6] fix:[LAR-32] remove unwork function to fix phpstan error --- app/Filament/Resources/ArticleResource.php | 2 -- 1 file changed, 2 deletions(-) diff --git a/app/Filament/Resources/ArticleResource.php b/app/Filament/Resources/ArticleResource.php index fec2c04d..db6b40dc 100644 --- a/app/Filament/Resources/ArticleResource.php +++ b/app/Filament/Resources/ArticleResource.php @@ -97,7 +97,6 @@ public static function table(Table $table): Table ->bulkActions([ Tables\Actions\BulkActionGroup::make([ BulkAction::make('approved') - ->visible(fn (?Collection $records) => ! empty($records) || $records?->contains(fn ($record) => $record->submitted_at)) ->label('Approuver la sélection') ->icon('heroicon-s-check') ->color('success') @@ -109,7 +108,6 @@ public static function table(Table $table): Table ->modalSubheading('Voulez-vous vraiment approuver ces articles ?') ->modalButton('Confirmer'), BulkAction::make('declined') - ->visible(fn (?Collection $records) => ! empty($records) || $records?->contains(fn ($record) => $record->submitted_at)) ->label('Décliner la sélection') ->icon('heroicon-s-x-mark') ->color('warning') From 197d69319d2414b18499b83c690f6866cf3b05dd Mon Sep 17 00:00:00 2001 From: Chri$ Date: Mon, 21 Oct 2024 10:00:06 +0200 Subject: [PATCH 6/6] feat:[LAR-32]replace inline condition with model function , delete unnecessary test and add test to check if action it is hidden after approved or declined article --- app/Filament/Resources/ArticleResource.php | 4 ++-- tests/Feature/Filament/ArticleTest.php | 23 +++++++++++----------- 2 files changed, 14 insertions(+), 13 deletions(-) diff --git a/app/Filament/Resources/ArticleResource.php b/app/Filament/Resources/ArticleResource.php index db6b40dc..86027d11 100644 --- a/app/Filament/Resources/ArticleResource.php +++ b/app/Filament/Resources/ArticleResource.php @@ -65,7 +65,7 @@ public static function table(Table $table): Table ->actions([ ActionGroup::make([ Action::make('approved') - ->visible(fn ($record) => $record->submitted_at && (! $record->declined_at && ! $record->approved_at)) + ->visible(fn (Article $record) => $record->isAwaitingApproval()) ->label('Approuver') ->icon('heroicon-s-check') ->color('success') @@ -78,7 +78,7 @@ public static function table(Table $table): Table $record->save(); }), Action::make('declined') - ->visible(fn ($record) => $record->submitted_at && (! $record->declined_at && ! $record->approved_at)) + ->visible(fn (Article $record) => $record->isAwaitingApproval()) ->label('Décliner') ->icon('heroicon-s-x-mark') ->color('warning') diff --git a/tests/Feature/Filament/ArticleTest.php b/tests/Feature/Filament/ArticleTest.php index 176a3771..40318132 100644 --- a/tests/Feature/Filament/ArticleTest.php +++ b/tests/Feature/Filament/ArticleTest.php @@ -20,16 +20,8 @@ ->assertCanSeeTableRecords($this->articles); }); - it('table can render columns', function (): void { - Livewire::test(ArticleResource\Pages\ListArticles::class) - ->assertCanRenderTableColumn('title') - ->assertCanRenderTableColumn('status') - ->assertCanRenderTableColumn('status') - ->assertCanRenderTableColumn('submitted_at'); - }); - - it('admin user can approved article', function (): void { - $article = Article::factory()->create(['submitted_at' => now()]); + it('admin user can approved article', function (): void { + $article = $this->articles->first(); Livewire::test(ArticleResource\Pages\ListArticles::class) ->callTableAction('approved', $article); @@ -41,10 +33,14 @@ ->toBe(null) ->and($article->declined_at) ->toBe(null); + + Livewire::test(ArticleResource\Pages\ListArticles::class) + ->assertTableActionHidden('approved', $article) + ->assertTableActionHidden('declined', $article); }); it('admin user can declined article', function (): void { - $article = Article::factory()->create(['submitted_at' => now()]); + $article = $this->articles->first(); Livewire::test(ArticleResource\Pages\ListArticles::class) ->callTableAction('declined', $article); @@ -56,5 +52,10 @@ ->toBe(null) ->and($article->approved_at) ->toBe(null); + + Livewire::test(ArticleResource\Pages\ListArticles::class) + ->assertTableActionHidden('approved', $article) + ->assertTableActionHidden('declined', $article); + }); })->group('articles');