diff --git a/app/Http/Controllers/Admin/UserController.php b/app/Http/Controllers/Admin/UserController.php index e0b53c343..b5126c766 100644 --- a/app/Http/Controllers/Admin/UserController.php +++ b/app/Http/Controllers/Admin/UserController.php @@ -86,8 +86,8 @@ class UserController extends Controller { $users = QueryBuilder::for( User::query()->select('users.*') - ->selectRaw('COUNT(subusers.id) as subuser_of_count') - ->selectRaw('COUNT(servers.id) as servers_count') + ->selectRaw('COUNT(DISTINCT(subusers.id)) as subuser_of_count') + ->selectRaw('COUNT(DISTINCT(servers.id)) as servers_count') ->leftJoin('subusers', 'subusers.user_id', '=', 'users.id') ->leftJoin('servers', 'servers.owner_id', '=', 'users.id') ->groupBy('users.id') diff --git a/tests/Integration/Http/Controllers/Admin/UserControllerTest.php b/tests/Integration/Http/Controllers/Admin/UserControllerTest.php new file mode 100644 index 000000000..a29844ca5 --- /dev/null +++ b/tests/Integration/Http/Controllers/Admin/UserControllerTest.php @@ -0,0 +1,59 @@ +create(['username' => $unique . '_1']), + factory(User::class)->create(['username' => $unique . '_2']), + ]; + + $servers = [ + $this->createServerModel(['owner_id' => $users[0]->id]), + $this->createServerModel(['owner_id' => $users[0]->id]), + $this->createServerModel(['owner_id' => $users[0]->id]), + $this->createServerModel(['owner_id' => $users[1]->id]), + ]; + + Subuser::query()->forceCreate(['server_id' => $servers[0]->id, 'user_id' => $users[1]->id]); + Subuser::query()->forceCreate(['server_id' => $servers[1]->id, 'user_id' => $users[1]->id]); + + /** @var \Pterodactyl\Http\Controllers\Admin\UserController $controller */ + $controller = $this->app->make(UserController::class); + + $request = Request::create('/admin/users?filter[username]=' . $unique, 'GET'); + $this->app->instance(Request::class, $request); + + $data = $controller->index($request)->getData(); + $this->assertArrayHasKey('users', $data); + $this->assertInstanceOf(LengthAwarePaginator::class, $data['users']); + + /** @var \Pterodactyl\Models\User[] $response */ + $response = $data['users']->items(); + $this->assertCount(2, $response); + $this->assertInstanceOf(User::class, $response[0]); + $this->assertSame(3, (int)$response[0]->servers_count); + $this->assertSame(0, (int)$response[0]->subuser_of_count); + $this->assertSame(1, (int)$response[1]->servers_count); + $this->assertSame(2, (int)$response[1]->subuser_of_count); + } +}