2017-08-16 04:16:00 +00:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* Pterodactyl - Panel
|
|
|
|
* Copyright (c) 2015 - 2017 Dane Everitt <dane@daneeveritt.com>.
|
|
|
|
*
|
2017-09-26 02:43:01 +00:00
|
|
|
* This software is licensed under the terms of the MIT license.
|
|
|
|
* https://opensource.org/licenses/MIT
|
2017-08-16 04:16:00 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
namespace Tests\Unit\Http\Controllers\Admin;
|
|
|
|
|
|
|
|
use Mockery as m;
|
2017-08-16 04:21:01 +00:00
|
|
|
use Tests\TestCase;
|
2018-01-05 04:49:50 +00:00
|
|
|
use Pterodactyl\Models\DatabaseHost;
|
2017-08-16 04:16:00 +00:00
|
|
|
use Prologue\Alerts\AlertsMessageBag;
|
2018-03-04 00:24:40 +00:00
|
|
|
use Illuminate\Pagination\LengthAwarePaginator;
|
2017-08-16 04:16:00 +00:00
|
|
|
use Tests\Assertions\ControllerAssertionsTrait;
|
2017-08-16 04:21:01 +00:00
|
|
|
use Pterodactyl\Http\Controllers\Admin\DatabaseController;
|
2017-10-24 01:12:15 +00:00
|
|
|
use Pterodactyl\Services\Databases\Hosts\HostUpdateService;
|
|
|
|
use Pterodactyl\Services\Databases\Hosts\HostCreationService;
|
|
|
|
use Pterodactyl\Services\Databases\Hosts\HostDeletionService;
|
2018-03-04 00:24:40 +00:00
|
|
|
use Pterodactyl\Contracts\Repository\DatabaseRepositoryInterface;
|
2017-08-16 04:21:01 +00:00
|
|
|
use Pterodactyl\Contracts\Repository\LocationRepositoryInterface;
|
|
|
|
use Pterodactyl\Contracts\Repository\DatabaseHostRepositoryInterface;
|
2017-08-16 04:16:00 +00:00
|
|
|
|
|
|
|
class DatabaseControllerTest extends TestCase
|
|
|
|
{
|
|
|
|
use ControllerAssertionsTrait;
|
|
|
|
|
|
|
|
/**
|
2017-10-24 01:12:15 +00:00
|
|
|
* @var \Prologue\Alerts\AlertsMessageBag|\Mockery\Mock
|
2017-08-16 04:16:00 +00:00
|
|
|
*/
|
2017-10-24 01:12:15 +00:00
|
|
|
private $alert;
|
2017-08-16 04:16:00 +00:00
|
|
|
|
|
|
|
/**
|
2017-10-24 01:12:15 +00:00
|
|
|
* @var \Pterodactyl\Services\Databases\Hosts\HostCreationService|\Mockery\Mock
|
2017-08-16 04:16:00 +00:00
|
|
|
*/
|
2017-10-24 01:12:15 +00:00
|
|
|
private $creationService;
|
2017-08-16 04:16:00 +00:00
|
|
|
|
2018-03-04 00:24:40 +00:00
|
|
|
/**
|
|
|
|
* @var \Pterodactyl\Contracts\Repository\DatabaseRepositoryInterface|\Mockery\Mock
|
|
|
|
*/
|
|
|
|
private $databaseRepository;
|
|
|
|
|
2017-08-16 04:16:00 +00:00
|
|
|
/**
|
2017-10-24 01:12:15 +00:00
|
|
|
* @var \Pterodactyl\Services\Databases\Hosts\HostDeletionService|\Mockery\Mock
|
2017-08-16 04:16:00 +00:00
|
|
|
*/
|
2017-10-24 01:12:15 +00:00
|
|
|
private $deletionService;
|
2017-08-16 04:16:00 +00:00
|
|
|
|
|
|
|
/**
|
2017-10-24 01:12:15 +00:00
|
|
|
* @var \Pterodactyl\Contracts\Repository\LocationRepositoryInterface|\Mockery\Mock
|
2017-08-16 04:16:00 +00:00
|
|
|
*/
|
2017-10-24 01:12:15 +00:00
|
|
|
private $locationRepository;
|
2017-08-16 04:16:00 +00:00
|
|
|
|
|
|
|
/**
|
2017-10-24 01:12:15 +00:00
|
|
|
* @var \Pterodactyl\Contracts\Repository\DatabaseHostRepositoryInterface|\Mockery\Mock
|
2017-08-16 04:16:00 +00:00
|
|
|
*/
|
2017-10-24 01:12:15 +00:00
|
|
|
private $repository;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var \Pterodactyl\Services\Databases\Hosts\HostUpdateService|\Mockery\Mock
|
|
|
|
*/
|
|
|
|
private $updateService;
|
2017-08-16 04:16:00 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Setup tests.
|
|
|
|
*/
|
|
|
|
public function setUp()
|
|
|
|
{
|
|
|
|
parent::setUp();
|
|
|
|
|
|
|
|
$this->alert = m::mock(AlertsMessageBag::class);
|
2017-10-24 01:12:15 +00:00
|
|
|
$this->creationService = m::mock(HostCreationService::class);
|
2018-03-04 00:24:40 +00:00
|
|
|
$this->databaseRepository = m::mock(DatabaseRepositoryInterface::class);
|
2017-10-24 01:12:15 +00:00
|
|
|
$this->deletionService = m::mock(HostDeletionService::class);
|
2017-08-16 04:16:00 +00:00
|
|
|
$this->locationRepository = m::mock(LocationRepositoryInterface::class);
|
|
|
|
$this->repository = m::mock(DatabaseHostRepositoryInterface::class);
|
2017-10-24 01:12:15 +00:00
|
|
|
$this->updateService = m::mock(HostUpdateService::class);
|
2017-08-16 04:16:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test the index controller.
|
|
|
|
*/
|
|
|
|
public function testIndexController()
|
|
|
|
{
|
2018-01-05 04:49:50 +00:00
|
|
|
$this->locationRepository->shouldReceive('getAllWithNodes')->withNoArgs()->once()->andReturn(collect(['getAllWithNodes']));
|
|
|
|
$this->repository->shouldReceive('getWithViewDetails')->withNoArgs()->once()->andReturn(collect(['getWithViewDetails']));
|
2017-08-16 04:16:00 +00:00
|
|
|
|
2017-10-24 01:12:15 +00:00
|
|
|
$response = $this->getController()->index();
|
2017-09-02 23:56:15 +00:00
|
|
|
|
|
|
|
$this->assertIsViewResponse($response);
|
|
|
|
$this->assertViewNameEquals('admin.databases.index', $response);
|
|
|
|
$this->assertViewHasKey('locations', $response);
|
|
|
|
$this->assertViewHasKey('hosts', $response);
|
2018-01-05 04:49:50 +00:00
|
|
|
$this->assertViewKeyEquals('locations', collect(['getAllWithNodes']), $response);
|
|
|
|
$this->assertViewKeyEquals('hosts', collect(['getWithViewDetails']), $response);
|
2017-08-16 04:16:00 +00:00
|
|
|
}
|
|
|
|
|
2017-08-19 03:19:06 +00:00
|
|
|
/**
|
|
|
|
* Test the view controller for displaying a specific database host.
|
|
|
|
*/
|
2017-08-16 04:16:00 +00:00
|
|
|
public function testViewController()
|
|
|
|
{
|
2018-01-05 04:49:50 +00:00
|
|
|
$model = factory(DatabaseHost::class)->make();
|
2018-03-04 00:24:40 +00:00
|
|
|
$paginator = new LengthAwarePaginator([], 1, 1);
|
2018-01-05 04:49:50 +00:00
|
|
|
|
|
|
|
$this->locationRepository->shouldReceive('getAllWithNodes')->withNoArgs()->once()->andReturn(collect(['getAllWithNodes']));
|
2018-03-04 00:24:40 +00:00
|
|
|
$this->repository->shouldReceive('find')->with(1)->once()->andReturn($model);
|
|
|
|
$this->databaseRepository->shouldReceive('getDatabasesForHost')
|
|
|
|
->once()
|
|
|
|
->with(1)
|
|
|
|
->andReturn($paginator);
|
2017-08-16 04:16:00 +00:00
|
|
|
|
2017-10-24 01:12:15 +00:00
|
|
|
$response = $this->getController()->view(1);
|
2017-08-16 04:16:00 +00:00
|
|
|
|
2017-09-02 23:56:15 +00:00
|
|
|
$this->assertIsViewResponse($response);
|
|
|
|
$this->assertViewNameEquals('admin.databases.view', $response);
|
|
|
|
$this->assertViewHasKey('locations', $response);
|
|
|
|
$this->assertViewHasKey('host', $response);
|
2018-03-04 00:24:40 +00:00
|
|
|
$this->assertViewHasKey('databases', $response);
|
2018-01-05 04:49:50 +00:00
|
|
|
$this->assertViewKeyEquals('locations', collect(['getAllWithNodes']), $response);
|
|
|
|
$this->assertViewKeyEquals('host', $model, $response);
|
2018-03-04 00:24:40 +00:00
|
|
|
$this->assertViewKeyEquals('databases', $paginator, $response);
|
2017-08-16 04:16:00 +00:00
|
|
|
}
|
2017-10-24 01:12:15 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Return an instance of the DatabaseController with mock dependencies.
|
|
|
|
*
|
|
|
|
* @return \Pterodactyl\Http\Controllers\Admin\DatabaseController
|
|
|
|
*/
|
|
|
|
private function getController(): DatabaseController
|
|
|
|
{
|
|
|
|
return new DatabaseController(
|
|
|
|
$this->alert,
|
|
|
|
$this->repository,
|
2018-03-04 00:24:40 +00:00
|
|
|
$this->databaseRepository,
|
2017-10-24 01:12:15 +00:00
|
|
|
$this->creationService,
|
|
|
|
$this->deletionService,
|
|
|
|
$this->updateService,
|
|
|
|
$this->locationRepository
|
|
|
|
);
|
|
|
|
}
|
2017-08-16 04:16:00 +00:00
|
|
|
}
|