From a75329679943566de24aff06283d93dd2ee41742 Mon Sep 17 00:00:00 2001 From: Simon Hamp Date: Tue, 19 May 2026 17:38:16 +0100 Subject: [PATCH] Fix "selected user is invalid" when granting bundle to user with null name Filament's Select uses getOptionLabelUsing to validate the chosen value; returning a blank label causes the "in" rule to reject it. The bundle grant action returned `User->name`, which is null for some users. Use `getFilamentName()` so the label falls back through display_name, name, then email, ensuring a non-blank result for any existing user. Co-Authored-By: Claude Opus 4.7 (1M context) --- .../Resources/PluginBundleResource.php | 4 ++-- .../Filament/GrantBundleToUserActionTest.php | 21 +++++++++++++++++++ 2 files changed, 23 insertions(+), 2 deletions(-) diff --git a/app/Filament/Resources/PluginBundleResource.php b/app/Filament/Resources/PluginBundleResource.php index a0e2781f..ace52b5b 100644 --- a/app/Filament/Resources/PluginBundleResource.php +++ b/app/Filament/Resources/PluginBundleResource.php @@ -189,10 +189,10 @@ public static function table(Table $table): Table ->orWhere('email', 'like', "%{$search}%") ->limit(50) ->get() - ->mapWithKeys(fn (User $user) => [$user->id => "{$user->name} ({$user->email})"]) + ->mapWithKeys(fn (User $user) => [$user->id => "{$user->getFilamentName()} ({$user->email})"]) ->toArray(); }) - ->getOptionLabelUsing(fn ($value): ?string => User::find($value)?->name) + ->getOptionLabelUsing(fn ($value): ?string => User::find($value)?->getFilamentName()) ->required(), ]) ->action(function (PluginBundle $record, array $data): void { diff --git a/tests/Feature/Filament/GrantBundleToUserActionTest.php b/tests/Feature/Filament/GrantBundleToUserActionTest.php index 43d2237c..4e494d16 100644 --- a/tests/Feature/Filament/GrantBundleToUserActionTest.php +++ b/tests/Feature/Filament/GrantBundleToUserActionTest.php @@ -51,4 +51,25 @@ public function test_grant_to_user_action_can_be_called_with_user_id(): void ]); } } + + public function test_grant_to_user_action_works_for_user_with_null_name(): void + { + $recipient = User::factory()->create(['name' => null]); + $plugins = Plugin::factory()->count(1)->approved()->create(); + $this->bundle->plugins()->attach($plugins->pluck('id')); + + Livewire::actingAs($this->admin) + ->test(ListPluginBundles::class) + ->callAction( + TestAction::make('grantToUser')->table($this->bundle), + data: ['user_id' => $recipient->id], + ) + ->assertHasNoFormErrors(); + + $this->assertDatabaseHas('plugin_licenses', [ + 'user_id' => $recipient->id, + 'plugin_id' => $plugins->first()->id, + 'plugin_bundle_id' => $this->bundle->id, + ]); + } }