@@ -188,6 +188,26 @@ public function testModelFindByIdWithGroups(): void
188188 );
189189 }
190190
191+ public function testModelFindByIdWithGroupsWhenUserHasNoGroups (): void
192+ {
193+ // User has no groups in the database
194+ $ user = model (UserModel::class)->where ('active ' , 1 )->withGroups ()->findById (1 );
195+
196+ $ this ->assertInstanceOf (User::class, $ user );
197+
198+ // Verify groups cache is set to empty array (not null)
199+ $ this ->assertSame ([], $ user ->getGroups ());
200+ $ this ->assertFalse ($ user ->inGroup ('admin ' ));
201+
202+ // Verify the last query was the one with WHERE IN
203+ $ query = (string ) model (UserModel::class)->getLastQuery ();
204+ $ this ->assertMatchesRegularExpression (
205+ '/WHERE\s+.*\s+IN\s+\([^)]+\)/i ' ,
206+ $ query ,
207+ 'Groups were not obtained with the single query (missing "WHERE ... IN" condition) ' ,
208+ );
209+ }
210+
191211 public function testModelFindAllWithPermissionsUserNotExists (): void
192212 {
193213 $ users = model (UserModel::class)->where ('active ' , 0 )->withPermissions ()->findAll ();
@@ -244,6 +264,28 @@ public function testModelFindByIdWithPermissions(): void
244264 );
245265 }
246266
267+ public function testModelFindByIdWithPermissionsWhenUserHasNoPermissions (): void
268+ {
269+ // Load both groups and permissions to ensure can() doesn't trigger queries
270+ $ user = model (UserModel::class)->where ('active ' , 1 )->withGroups ()->withPermissions ()->findById (1 );
271+
272+ $ this ->assertInstanceOf (User::class, $ user );
273+
274+ // Verify permissions cache is set to empty array (not null)
275+ $ this ->assertSame ([], $ user ->getPermissions ());
276+ $ this ->assertSame ([], $ user ->getGroups ());
277+ $ this ->assertFalse ($ user ->hasPermission ('users.delete ' ));
278+ $ this ->assertFalse ($ user ->can ('users.delete ' ));
279+
280+ // Verify the last query was the one with WHERE IN
281+ $ query = (string ) model (UserModel::class)->getLastQuery ();
282+ $ this ->assertMatchesRegularExpression (
283+ '/WHERE\s+.*\s+IN\s+\([^)]+\)/i ' ,
284+ $ query ,
285+ 'Groups and Permissions were not obtained with the single query (missing "WHERE ... IN" condition) ' ,
286+ );
287+ }
288+
247289 public function testModelFindByIdWithGroupsAndPermissions (): void
248290 {
249291 fake (GroupModel::class, ['user_id ' => $ this ->user ->id , 'group ' => 'superadmin ' ]);
0 commit comments