misc_pterodactyl-panel/tests/Unit/Services/Users/UserDeletionServiceTest.php

104 lines
3.2 KiB
PHP
Raw Normal View History

<?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
*/
namespace Tests\Unit\Services\Users;
use Mockery as m;
2017-08-05 22:26:30 +00:00
use Tests\TestCase;
use Pterodactyl\Models\User;
2017-08-05 22:26:30 +00:00
use Illuminate\Contracts\Translation\Translator;
2017-08-31 02:14:20 +00:00
use Pterodactyl\Services\Users\UserDeletionService;
2017-08-05 22:26:30 +00:00
use Pterodactyl\Contracts\Repository\UserRepositoryInterface;
use Pterodactyl\Contracts\Repository\ServerRepositoryInterface;
class UserDeletionServiceTest extends TestCase
{
/**
* @var \Pterodactyl\Contracts\Repository\UserRepositoryInterface
*/
protected $repository;
/**
* @var \Illuminate\Contracts\Translation\Translator
*/
protected $translator;
/**
* @var \Pterodactyl\Contracts\Repository\ServerRepositoryInterface
*/
protected $serverRepository;
/**
* @var \Pterodactyl\Services\Users\UserDeletionService
*/
protected $service;
/**
* @var User
*/
protected $user;
/**
* Setup tests.
*/
public function setUp(): void
{
parent::setUp();
$this->user = factory(User::class)->make();
$this->repository = m::mock(UserRepositoryInterface::class);
$this->translator = m::mock(Translator::class);
$this->serverRepository = m::mock(ServerRepositoryInterface::class);
$this->service = new UserDeletionService(
2017-08-22 03:10:48 +00:00
$this->serverRepository,
$this->translator,
$this->repository
);
}
/**
* Test that a user is deleted if they have no servers.
*/
public function testUserIsDeletedIfNoServersAreAttachedToAccount()
{
$this->serverRepository->shouldReceive('setColumns')->with('id')->once()->andReturnSelf()
->shouldReceive('findCountWhere')->with([['owner_id', '=', $this->user->id]])->once()->andReturn(0);
$this->repository->shouldReceive('delete')->with($this->user->id)->once()->andReturn(1);
$this->assertEquals(1, $this->service->handle($this->user->id));
}
/**
* Test that an exception is thrown if trying to delete a user with servers.
*
* @expectedException \Pterodactyl\Exceptions\DisplayException
*/
public function testExceptionIsThrownIfServersAreAttachedToAccount()
{
$this->serverRepository->shouldReceive('setColumns')->with('id')->once()->andReturnSelf()
->shouldReceive('findCountWhere')->with([['owner_id', '=', $this->user->id]])->once()->andReturn(1);
$this->translator->shouldReceive('trans')->with('admin/user.exceptions.user_has_servers')->once()->andReturnNull();
$this->service->handle($this->user->id);
}
/**
* Test that the function supports passing in a model or an ID.
*/
public function testModelCanBePassedInPlaceOfUserId()
{
$this->serverRepository->shouldReceive('setColumns')->with('id')->once()->andReturnSelf()
->shouldReceive('findCountWhere')->with([['owner_id', '=', $this->user->id]])->once()->andReturn(0);
$this->repository->shouldReceive('delete')->with($this->user->id)->once()->andReturn(1);
$this->assertEquals(1, $this->service->handle($this->user));
}
}