mirror of
				https://github.com/pelican-dev/panel.git
				synced 2025-11-04 04:16:52 +01:00 
			
		
		
		
	
		
			
				
	
	
		
			58 lines
		
	
	
		
			2.2 KiB
		
	
	
	
		
			PHP
		
	
	
	
	
	
			
		
		
	
	
			58 lines
		
	
	
		
			2.2 KiB
		
	
	
	
		
			PHP
		
	
	
	
	
	
<?php
 | 
						|
 | 
						|
namespace App\Tests\Integration\Http\Controllers\Admin;
 | 
						|
 | 
						|
use Illuminate\Support\Str;
 | 
						|
use Illuminate\Http\Request;
 | 
						|
use App\Models\User;
 | 
						|
use App\Models\Subuser;
 | 
						|
use Illuminate\Pagination\LengthAwarePaginator;
 | 
						|
use App\Http\Controllers\Admin\UserController;
 | 
						|
use App\Tests\Integration\IntegrationTestCase;
 | 
						|
 | 
						|
class UserControllerTest extends IntegrationTestCase
 | 
						|
{
 | 
						|
    /**
 | 
						|
     * Test that the index route controller for the user listing returns the expected user
 | 
						|
     * data with the number of servers they are assigned to, and the number of servers they
 | 
						|
     * are a subuser of.
 | 
						|
     */
 | 
						|
    public function testIndexReturnsExpectedData(): void
 | 
						|
    {
 | 
						|
        $unique = Str::random();
 | 
						|
        $users = [
 | 
						|
            User::factory()->create(['username' => $unique . '_1']),
 | 
						|
            User::factory()->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 \App\Http\Controllers\Admin\UserController $controller */
 | 
						|
        $controller = $this->app->make(UserController::class);
 | 
						|
 | 
						|
        $request = Request::create('/admin/users?filter[username]=' . $unique);
 | 
						|
        $this->app->instance(Request::class, $request);
 | 
						|
 | 
						|
        $data = $controller->index($request)->getData();
 | 
						|
        $this->assertArrayHasKey('users', $data);
 | 
						|
        $this->assertInstanceOf(LengthAwarePaginator::class, $data['users']);
 | 
						|
 | 
						|
        /** @var \App\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);
 | 
						|
    }
 | 
						|
}
 |