From 1c4ef537ffa1edc04fcf819ae5197ed0a05dcc79 Mon Sep 17 00:00:00 2001 From: Travis Sheldon Date: Mon, 17 Jun 2024 12:38:44 -0400 Subject: [PATCH 1/2] fixed an issue where menu item was attempting to render in panels where the plugin was not enabled --- ...FilamentDeveloperLoginsServiceProvider.php | 14 ++++++-- tests/Feature/MenuLoginsTest.php | 36 +++++++++++++++++++ 2 files changed, 47 insertions(+), 3 deletions(-) diff --git a/src/FilamentDeveloperLoginsServiceProvider.php b/src/FilamentDeveloperLoginsServiceProvider.php index 8192db2..0bb7ac0 100644 --- a/src/FilamentDeveloperLoginsServiceProvider.php +++ b/src/FilamentDeveloperLoginsServiceProvider.php @@ -4,6 +4,7 @@ use DutchCodingCompany\FilamentDeveloperLogins\Livewire\MenuLogins; use DutchCodingCompany\FilamentDeveloperLogins\View\Components\DeveloperLogins; +use Filament\Panel; use Filament\Facades\Filament; use Filament\Support\Concerns\EvaluatesClosures; use Filament\Support\Facades\FilamentView; @@ -45,7 +46,8 @@ public function packageBooted(): void protected static function registerRenderHooks(): void { $panel = Filament::getCurrentPanel(); - if (is_null($panel) || ! $panel->hasPlugin('filament-developer-logins')) { + + if(! self::enabledForPanel($panel)) { return; } @@ -55,7 +57,8 @@ protected static function registerRenderHooks(): void FilamentView::registerRenderHook( PanelsRenderHook::AUTH_LOGIN_FORM_AFTER, static function () use ($plugin) : ?string { - if (! $plugin->getEnabled()) { + + if (! $plugin->getEnabled() || ! self::enabledForPanel(Filament::getCurrentPanel())) { return null; } @@ -66,7 +69,7 @@ static function () use ($plugin) : ?string { FilamentView::registerRenderHook( PanelsRenderHook::GLOBAL_SEARCH_AFTER, static function () use ($plugin) : ?string { - if (! $plugin->getEnabled() || ! $plugin->getSwitchable()) { + if (! $plugin->getEnabled() || ! $plugin->getSwitchable() || ! self::enabledForPanel(Filament::getCurrentPanel())) { return null; } @@ -74,4 +77,9 @@ static function () use ($plugin) : ?string { }, ); } + + protected static function enabledForPanel(?Panel $panel) + { + return ! is_null($panel) && $panel->hasPlugin('filament-developer-logins'); + } } diff --git a/tests/Feature/MenuLoginsTest.php b/tests/Feature/MenuLoginsTest.php index 501762c..2365153 100644 --- a/tests/Feature/MenuLoginsTest.php +++ b/tests/Feature/MenuLoginsTest.php @@ -8,6 +8,7 @@ use DutchCodingCompany\FilamentDeveloperLogins\Tests\TestCase; use Filament\Facades\Filament; use Filament\Pages\Dashboard; +use Filament\Panel; use Livewire\Livewire; final class MenuLoginsTest extends TestCase @@ -80,4 +81,39 @@ public function test_forbidden_is_returned_when_enabled_or_switchable_is_false() ->call('loginAs', 'developer@dutchcodingcompany.com') ->assertForbidden(); } + + public function test_component_is_only_rendered_in_panel_with_plugin(): void + { + + $user = TestUser::factory()->create(); + + $this->actingAs($user) + ->get(Dashboard::getUrl(panel: $this->panelName)) + ->assertSuccessful() + ->assertSeeLivewire(MenuLogins::class); + + + $this->actingAs($user) + ->get(Dashboard::getUrl(panel: $this->panelName . '-other' )) + ->assertSuccessful() + ->assertDontSeeLivewire(MenuLogins::class); + } + + protected function registerTestPanel(): void + { + parent::registerTestPanel(); + Filament::registerPanel(fn() => Panel::make() + ->darkMode(false) + ->id($this->panelName . '-other') + ->path($this->panelName . '-other') + ->login() + ->pages([ + Dashboard::class, + ]) + ->plugins([ + ]), + ); + } + + } From 3041739cd96a11437ed3f8596e75dbc0c9ef063d Mon Sep 17 00:00:00 2001 From: Bram Raaijmakers Date: Wed, 19 Jun 2024 10:44:46 +0200 Subject: [PATCH 2/2] Clean up code --- ...FilamentDeveloperLoginsServiceProvider.php | 34 +++++++++++-------- tests/Feature/MenuLoginsTest.php | 22 +----------- tests/TestCase.php | 16 +++++++-- 3 files changed, 34 insertions(+), 38 deletions(-) diff --git a/src/FilamentDeveloperLoginsServiceProvider.php b/src/FilamentDeveloperLoginsServiceProvider.php index 0bb7ac0..5297631 100644 --- a/src/FilamentDeveloperLoginsServiceProvider.php +++ b/src/FilamentDeveloperLoginsServiceProvider.php @@ -4,8 +4,8 @@ use DutchCodingCompany\FilamentDeveloperLogins\Livewire\MenuLogins; use DutchCodingCompany\FilamentDeveloperLogins\View\Components\DeveloperLogins; -use Filament\Panel; use Filament\Facades\Filament; +use Filament\Panel; use Filament\Support\Concerns\EvaluatesClosures; use Filament\Support\Facades\FilamentView; use Filament\View\PanelsRenderHook; @@ -45,20 +45,17 @@ public function packageBooted(): void protected static function registerRenderHooks(): void { - $panel = Filament::getCurrentPanel(); - - if(! self::enabledForPanel($panel)) { - return; - } - - /** @var FilamentDeveloperLoginsPlugin $plugin */ - $plugin = $panel->getPlugin('filament-developer-logins'); - FilamentView::registerRenderHook( PanelsRenderHook::AUTH_LOGIN_FORM_AFTER, - static function () use ($plugin) : ?string { + static function (): ?string { + $panel = Filament::getCurrentPanel(); + if (! self::panelHasPlugin($panel)) { + return null; + } - if (! $plugin->getEnabled() || ! self::enabledForPanel(Filament::getCurrentPanel())) { + /** @var FilamentDeveloperLoginsPlugin $plugin */ + $plugin = $panel->getPlugin('filament-developer-logins'); + if (! $plugin->getEnabled()) { return null; } @@ -68,8 +65,15 @@ static function () use ($plugin) : ?string { FilamentView::registerRenderHook( PanelsRenderHook::GLOBAL_SEARCH_AFTER, - static function () use ($plugin) : ?string { - if (! $plugin->getEnabled() || ! $plugin->getSwitchable() || ! self::enabledForPanel(Filament::getCurrentPanel())) { + static function (): ?string { + $panel = Filament::getCurrentPanel(); + if (! self::panelHasPlugin($panel)) { + return null; + } + + /** @var FilamentDeveloperLoginsPlugin $plugin */ + $plugin = $panel->getPlugin('filament-developer-logins'); + if (! $plugin->getEnabled() || ! $plugin->getSwitchable()) { return null; } @@ -78,7 +82,7 @@ static function () use ($plugin) : ?string { ); } - protected static function enabledForPanel(?Panel $panel) + protected static function panelHasPlugin(?Panel $panel): bool { return ! is_null($panel) && $panel->hasPlugin('filament-developer-logins'); } diff --git a/tests/Feature/MenuLoginsTest.php b/tests/Feature/MenuLoginsTest.php index 2365153..d15d7fd 100644 --- a/tests/Feature/MenuLoginsTest.php +++ b/tests/Feature/MenuLoginsTest.php @@ -84,7 +84,6 @@ public function test_forbidden_is_returned_when_enabled_or_switchable_is_false() public function test_component_is_only_rendered_in_panel_with_plugin(): void { - $user = TestUser::factory()->create(); $this->actingAs($user) @@ -92,28 +91,9 @@ public function test_component_is_only_rendered_in_panel_with_plugin(): void ->assertSuccessful() ->assertSeeLivewire(MenuLogins::class); - $this->actingAs($user) - ->get(Dashboard::getUrl(panel: $this->panelName . '-other' )) + ->get(Dashboard::getUrl(panel: $this->panelName.'-other')) ->assertSuccessful() ->assertDontSeeLivewire(MenuLogins::class); } - - protected function registerTestPanel(): void - { - parent::registerTestPanel(); - Filament::registerPanel(fn() => Panel::make() - ->darkMode(false) - ->id($this->panelName . '-other') - ->path($this->panelName . '-other') - ->login() - ->pages([ - Dashboard::class, - ]) - ->plugins([ - ]), - ); - } - - } diff --git a/tests/TestCase.php b/tests/TestCase.php index 79a8c86..c4f3004 100644 --- a/tests/TestCase.php +++ b/tests/TestCase.php @@ -36,7 +36,7 @@ protected function setUp(): void ); } - protected function registerTestPanel(): void + protected function registerTestPanels(): void { Filament::registerPanel( fn (): Panel => Panel::make() @@ -57,6 +57,18 @@ protected function registerTestPanel(): void ->modelClass(TestUser::class), ]), ); + + Filament::registerPanel(fn () => Panel::make() + ->darkMode(false) + ->id($this->panelName.'-other') + ->path($this->panelName.'-other') + ->login() + ->pages([ + Dashboard::class, + ]) + ->plugins([ + ]), + ); } protected function defineRoutes($router): void @@ -80,7 +92,7 @@ protected function getEnvironmentSetUp($app): void protected function getPackageProviders($app): array { - $this->registerTestPanel(); + $this->registerTestPanels(); return [ FilamentServiceProvider::class,