2017-08-05 22:20:07 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Tests\Unit\Services\Nodes;
|
|
|
|
|
|
|
|
use Mockery as m;
|
2017-08-05 22:26:30 +00:00
|
|
|
use Tests\TestCase;
|
2017-08-05 22:20:07 +00:00
|
|
|
use Pterodactyl\Models\Node;
|
2020-06-25 04:54:56 +00:00
|
|
|
use Pterodactyl\Exceptions\DisplayException;
|
2017-08-05 22:26:30 +00:00
|
|
|
use Illuminate\Contracts\Translation\Translator;
|
2017-08-31 02:14:20 +00:00
|
|
|
use Pterodactyl\Services\Nodes\NodeDeletionService;
|
2017-08-05 22:26:30 +00:00
|
|
|
use Pterodactyl\Contracts\Repository\NodeRepositoryInterface;
|
|
|
|
use Pterodactyl\Contracts\Repository\ServerRepositoryInterface;
|
2017-08-05 22:20:07 +00:00
|
|
|
|
2017-08-27 20:10:51 +00:00
|
|
|
class NodeDeletionServiceTest extends TestCase
|
2017-08-05 22:20:07 +00:00
|
|
|
{
|
|
|
|
/**
|
|
|
|
* @var \Pterodactyl\Contracts\Repository\NodeRepositoryInterface
|
|
|
|
*/
|
|
|
|
protected $repository;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var \Pterodactyl\Contracts\Repository\ServerRepositoryInterface
|
|
|
|
*/
|
|
|
|
protected $serverRepository;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var \Illuminate\Contracts\Translation\Translator
|
|
|
|
*/
|
|
|
|
protected $translator;
|
|
|
|
|
|
|
|
/**
|
2017-08-27 20:10:51 +00:00
|
|
|
* @var \Pterodactyl\Services\Nodes\NodeDeletionService
|
2017-08-05 22:20:07 +00:00
|
|
|
*/
|
|
|
|
protected $service;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Setup tests.
|
|
|
|
*/
|
2020-05-09 16:00:52 +00:00
|
|
|
public function setUp(): void
|
2017-08-05 22:20:07 +00:00
|
|
|
{
|
|
|
|
parent::setUp();
|
|
|
|
|
|
|
|
$this->repository = m::mock(NodeRepositoryInterface::class);
|
|
|
|
$this->serverRepository = m::mock(ServerRepositoryInterface::class);
|
|
|
|
$this->translator = m::mock(Translator::class);
|
|
|
|
|
2017-08-27 20:10:51 +00:00
|
|
|
$this->service = new NodeDeletionService(
|
2017-08-05 22:20:07 +00:00
|
|
|
$this->repository,
|
|
|
|
$this->serverRepository,
|
|
|
|
$this->translator
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test that a node is deleted if there are no servers attached to it.
|
|
|
|
*/
|
|
|
|
public function testNodeIsDeletedIfNoServersAreAttached()
|
|
|
|
{
|
2018-01-05 04:49:50 +00:00
|
|
|
$this->serverRepository->shouldReceive('setColumns')->with('id')->once()->andReturnSelf()
|
2017-08-05 22:20:07 +00:00
|
|
|
->shouldReceive('findCountWhere')->with([['node_id', '=', 1]])->once()->andReturn(0);
|
2018-01-05 04:49:50 +00:00
|
|
|
$this->repository->shouldReceive('delete')->with(1)->once()->andReturn(1);
|
2017-08-05 22:20:07 +00:00
|
|
|
|
2018-01-05 04:49:50 +00:00
|
|
|
$this->assertEquals(1, $this->service->handle(1));
|
2017-08-05 22:20:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test that an exception is thrown if servers are attached to the node.
|
|
|
|
*/
|
|
|
|
public function testExceptionIsThrownIfServersAreAttachedToNode()
|
|
|
|
{
|
2020-06-25 04:54:56 +00:00
|
|
|
$this->expectException(DisplayException::class);
|
|
|
|
|
2018-01-05 04:49:50 +00:00
|
|
|
$this->serverRepository->shouldReceive('setColumns')->with('id')->once()->andReturnSelf()
|
2017-08-05 22:20:07 +00:00
|
|
|
->shouldReceive('findCountWhere')->with([['node_id', '=', 1]])->once()->andReturn(1);
|
2017-09-03 21:32:52 +00:00
|
|
|
$this->translator->shouldReceive('trans')->with('exceptions.node.servers_attached')->once()->andReturnNull();
|
2017-08-05 22:20:07 +00:00
|
|
|
$this->repository->shouldNotReceive('delete');
|
|
|
|
|
|
|
|
$this->service->handle(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test that a model can be passed into the handle function rather than an ID.
|
|
|
|
*/
|
|
|
|
public function testModelCanBePassedToFunctionInPlaceOfNodeId()
|
|
|
|
{
|
2020-10-06 04:31:39 +00:00
|
|
|
$node = factory(Node::class)->make(['id' => 123]);
|
2017-08-05 22:20:07 +00:00
|
|
|
|
2018-01-05 04:49:50 +00:00
|
|
|
$this->serverRepository->shouldReceive('setColumns')->with('id')->once()->andReturnSelf()
|
2017-08-05 22:20:07 +00:00
|
|
|
->shouldReceive('findCountWhere')->with([['node_id', '=', $node->id]])->once()->andReturn(0);
|
2018-01-05 04:49:50 +00:00
|
|
|
$this->repository->shouldReceive('delete')->with($node->id)->once()->andReturn(1);
|
2017-08-05 22:20:07 +00:00
|
|
|
|
2018-01-05 04:49:50 +00:00
|
|
|
$this->assertEquals(1, $this->service->handle($node));
|
2017-08-05 22:20:07 +00:00
|
|
|
}
|
|
|
|
}
|