2017-08-05 22:20:07 +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-05 22:20:07 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
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;
|
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.
|
|
|
|
*/
|
|
|
|
public function setUp()
|
|
|
|
{
|
|
|
|
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()
|
|
|
|
{
|
|
|
|
$this->serverRepository->shouldReceive('withColumns')->with('id')->once()->andReturnSelf()
|
|
|
|
->shouldReceive('findCountWhere')->with([['node_id', '=', 1]])->once()->andReturn(0);
|
|
|
|
$this->repository->shouldReceive('delete')->with(1)->once()->andReturn(true);
|
|
|
|
|
|
|
|
$this->assertTrue(
|
|
|
|
$this->service->handle(1),
|
|
|
|
'Assert that deletion returns a positive boolean value.'
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test that an exception is thrown if servers are attached to the node.
|
|
|
|
*
|
|
|
|
* @expectedException \Pterodactyl\Exceptions\DisplayException
|
|
|
|
*/
|
|
|
|
public function testExceptionIsThrownIfServersAreAttachedToNode()
|
|
|
|
{
|
|
|
|
$this->serverRepository->shouldReceive('withColumns')->with('id')->once()->andReturnSelf()
|
|
|
|
->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()
|
|
|
|
{
|
|
|
|
$node = factory(Node::class)->make();
|
|
|
|
|
|
|
|
$this->serverRepository->shouldReceive('withColumns')->with('id')->once()->andReturnSelf()
|
|
|
|
->shouldReceive('findCountWhere')->with([['node_id', '=', $node->id]])->once()->andReturn(0);
|
|
|
|
$this->repository->shouldReceive('delete')->with($node->id)->once()->andReturn(true);
|
|
|
|
|
|
|
|
$this->assertTrue(
|
2017-09-27 03:23:19 +00:00
|
|
|
$this->service->handle($node),
|
2017-08-05 22:20:07 +00:00
|
|
|
'Assert that deletion returns a positive boolean value.'
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|