Skip to content

Commit 7b0f998

Browse files
committed
Return the correct server & subuser counts for user listing; closes pterodactyl#2469
1 parent 1f7fe09 commit 7b0f998

File tree

2 files changed

+61
-2
lines changed

2 files changed

+61
-2
lines changed

app/Http/Controllers/Admin/UserController.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,8 +86,8 @@ public function index(Request $request)
8686
{
8787
$users = QueryBuilder::for(
8888
User::query()->select('users.*')
89-
->selectRaw('COUNT(subusers.id) as subuser_of_count')
90-
->selectRaw('COUNT(servers.id) as servers_count')
89+
->selectRaw('COUNT(DISTINCT(subusers.id)) as subuser_of_count')
90+
->selectRaw('COUNT(DISTINCT(servers.id)) as servers_count')
9191
->leftJoin('subusers', 'subusers.user_id', '=', 'users.id')
9292
->leftJoin('servers', 'servers.owner_id', '=', 'users.id')
9393
->groupBy('users.id')
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
<?php
2+
3+
namespace Pterodactyl\Tests\Integration\Http\Controllers\Admin;
4+
5+
use Illuminate\Support\Str;
6+
use Illuminate\Http\Request;
7+
use Pterodactyl\Models\User;
8+
use Pterodactyl\Models\Subuser;
9+
use Illuminate\Pagination\LengthAwarePaginator;
10+
use Pterodactyl\Tests\Integration\IntegrationTestCase;
11+
use Pterodactyl\Http\Controllers\Admin\UserController;
12+
13+
class UserControllerTest extends IntegrationTestCase
14+
{
15+
/**
16+
* Test that the index route controller for the user listing returns the expected user
17+
* data with the number of servers they are assigned to, and the number of servers they
18+
* are a subuser of.
19+
*
20+
* @see https://github.com/pterodactyl/panel/issues/2469
21+
*/
22+
public function testIndexReturnsExpectedData()
23+
{
24+
$unique = Str::random(16);
25+
$users = [
26+
factory(User::class)->create(['username' => $unique . '_1']),
27+
factory(User::class)->create(['username' => $unique . '_2']),
28+
];
29+
30+
$servers = [
31+
$this->createServerModel(['owner_id' => $users[0]->id]),
32+
$this->createServerModel(['owner_id' => $users[0]->id]),
33+
$this->createServerModel(['owner_id' => $users[0]->id]),
34+
$this->createServerModel(['owner_id' => $users[1]->id]),
35+
];
36+
37+
Subuser::query()->forceCreate(['server_id' => $servers[0]->id, 'user_id' => $users[1]->id]);
38+
Subuser::query()->forceCreate(['server_id' => $servers[1]->id, 'user_id' => $users[1]->id]);
39+
40+
/** @var \Pterodactyl\Http\Controllers\Admin\UserController $controller */
41+
$controller = $this->app->make(UserController::class);
42+
43+
$request = Request::create('/admin/users?filter[username]=' . $unique, 'GET');
44+
$this->app->instance(Request::class, $request);
45+
46+
$data = $controller->index($request)->getData();
47+
$this->assertArrayHasKey('users', $data);
48+
$this->assertInstanceOf(LengthAwarePaginator::class, $data['users']);
49+
50+
/** @var \Pterodactyl\Models\User[] $response */
51+
$response = $data['users']->items();
52+
$this->assertCount(2, $response);
53+
$this->assertInstanceOf(User::class, $response[0]);
54+
$this->assertSame(3, (int)$response[0]->servers_count);
55+
$this->assertSame(0, (int)$response[0]->subuser_of_count);
56+
$this->assertSame(1, (int)$response[1]->servers_count);
57+
$this->assertSame(2, (int)$response[1]->subuser_of_count);
58+
}
59+
}

0 commit comments

Comments
 (0)