2017-08-13 19:55:09 +00:00
|
|
|
<?php
|
|
|
|
|
2017-10-08 04:29:08 +00:00
|
|
|
namespace Tests\Unit\Services\Eggs\Variables;
|
2017-08-13 19:55:09 +00:00
|
|
|
|
|
|
|
use Exception;
|
|
|
|
use Mockery as m;
|
2017-08-16 04:21:01 +00:00
|
|
|
use Tests\TestCase;
|
2017-10-07 04:57:53 +00:00
|
|
|
use Pterodactyl\Models\EggVariable;
|
2017-08-16 04:21:01 +00:00
|
|
|
use Pterodactyl\Exceptions\DisplayException;
|
2017-10-08 04:29:08 +00:00
|
|
|
use Pterodactyl\Services\Eggs\Variables\VariableUpdateService;
|
|
|
|
use Pterodactyl\Contracts\Repository\EggVariableRepositoryInterface;
|
2017-08-13 19:55:09 +00:00
|
|
|
|
|
|
|
class VariableUpdateServiceTest extends TestCase
|
|
|
|
{
|
|
|
|
/**
|
2017-10-07 04:57:53 +00:00
|
|
|
* @var \Pterodactyl\Models\EggVariable|\Mockery\Mock
|
2017-08-13 19:55:09 +00:00
|
|
|
*/
|
|
|
|
protected $model;
|
|
|
|
|
|
|
|
/**
|
2017-10-08 04:29:08 +00:00
|
|
|
* @var \Pterodactyl\Contracts\Repository\EggVariableRepositoryInterface|\Mockery\Mock
|
2017-08-13 19:55:09 +00:00
|
|
|
*/
|
|
|
|
protected $repository;
|
|
|
|
|
|
|
|
/**
|
2017-10-08 04:29:08 +00:00
|
|
|
* @var \Pterodactyl\Services\Eggs\Variables\VariableUpdateService
|
2017-08-13 19:55:09 +00:00
|
|
|
*/
|
|
|
|
protected $service;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Setup tests.
|
|
|
|
*/
|
|
|
|
public function setUp()
|
|
|
|
{
|
|
|
|
parent::setUp();
|
|
|
|
|
2017-10-07 04:57:53 +00:00
|
|
|
$this->model = factory(EggVariable::class)->make();
|
2017-10-08 04:29:08 +00:00
|
|
|
$this->repository = m::mock(EggVariableRepositoryInterface::class);
|
2017-08-13 19:55:09 +00:00
|
|
|
|
|
|
|
$this->service = new VariableUpdateService($this->repository);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test the function when no env_variable key is passed into the function.
|
|
|
|
*/
|
|
|
|
public function testVariableIsUpdatedWhenNoEnvironmentVariableIsPassed()
|
|
|
|
{
|
2018-01-05 22:33:50 +00:00
|
|
|
$this->repository->shouldReceive('withoutFreshModel')->withNoArgs()->once()->andReturnSelf()
|
2018-02-11 22:47:50 +00:00
|
|
|
->shouldReceive('update')->with($this->model->id, m::subset([
|
2017-08-13 19:55:09 +00:00
|
|
|
'user_viewable' => false,
|
|
|
|
'user_editable' => false,
|
2018-02-11 22:47:50 +00:00
|
|
|
]))->once()->andReturn(true);
|
2017-08-13 19:55:09 +00:00
|
|
|
|
2018-02-17 19:37:53 +00:00
|
|
|
$this->assertTrue($this->service->handle($this->model, []));
|
2017-08-13 19:55:09 +00:00
|
|
|
}
|
|
|
|
|
2017-09-27 03:16:26 +00:00
|
|
|
/**
|
2018-02-11 22:47:50 +00:00
|
|
|
* Test that a null value passed in for the default is converted to a string.
|
|
|
|
*
|
|
|
|
* @see https://github.com/Pterodactyl/Panel/issues/934
|
2017-09-27 03:16:26 +00:00
|
|
|
*/
|
2018-02-11 22:47:50 +00:00
|
|
|
public function testNullDefaultValue()
|
2017-09-27 03:16:26 +00:00
|
|
|
{
|
2018-02-17 19:37:53 +00:00
|
|
|
$this->repository->shouldReceive('withoutFreshModel->update')->with($this->model->id, m::subset([
|
|
|
|
'user_viewable' => false,
|
|
|
|
'user_editable' => false,
|
|
|
|
'default_value' => '',
|
|
|
|
]))->once()->andReturn(true);
|
2017-09-27 03:16:26 +00:00
|
|
|
|
2018-02-11 22:47:50 +00:00
|
|
|
$this->assertTrue($this->service->handle($this->model, ['default_value' => null]));
|
2017-09-27 03:16:26 +00:00
|
|
|
}
|
|
|
|
|
2017-08-13 19:55:09 +00:00
|
|
|
/**
|
|
|
|
* Test the function when a valid env_variable key is passed into the function.
|
|
|
|
*/
|
|
|
|
public function testVariableIsUpdatedWhenValidEnvironmentVariableIsPassed()
|
|
|
|
{
|
2018-01-05 04:49:50 +00:00
|
|
|
$this->repository->shouldReceive('setColumns')->with('id')->once()->andReturnSelf()
|
2017-08-13 19:55:09 +00:00
|
|
|
->shouldReceive('findCountWhere')->with([
|
|
|
|
['env_variable', '=', 'TEST_VAR_123'],
|
2017-10-08 04:29:08 +00:00
|
|
|
['egg_id', '=', $this->model->option_id],
|
2017-08-16 04:21:01 +00:00
|
|
|
['id', '!=', $this->model->id],
|
2017-08-13 19:55:09 +00:00
|
|
|
])->once()->andReturn(0);
|
|
|
|
|
2018-01-05 22:33:50 +00:00
|
|
|
$this->repository->shouldReceive('withoutFreshModel')->withNoArgs()->once()->andReturnSelf()
|
2018-02-11 22:47:50 +00:00
|
|
|
->shouldReceive('update')->with($this->model->id, m::subset([
|
2017-08-13 19:55:09 +00:00
|
|
|
'user_viewable' => false,
|
|
|
|
'user_editable' => false,
|
|
|
|
'env_variable' => 'TEST_VAR_123',
|
2018-02-11 22:47:50 +00:00
|
|
|
]))->once()->andReturn(true);
|
2017-08-13 19:55:09 +00:00
|
|
|
|
|
|
|
$this->assertTrue($this->service->handle($this->model, ['env_variable' => 'TEST_VAR_123']));
|
|
|
|
}
|
|
|
|
|
2017-12-31 01:56:42 +00:00
|
|
|
/**
|
|
|
|
* Test that an empty (null) value passed in the option key is handled
|
2018-02-17 19:37:53 +00:00
|
|
|
* properly as an array. Also tests that a null description is handled.
|
2017-12-31 01:56:42 +00:00
|
|
|
*
|
|
|
|
* @see https://github.com/Pterodactyl/Panel/issues/841
|
|
|
|
*/
|
|
|
|
public function testNullOptionValueIsPassedAsArray()
|
|
|
|
{
|
2018-01-05 22:33:50 +00:00
|
|
|
$this->repository->shouldReceive('withoutFreshModel')->withNoArgs()->once()->andReturnSelf()
|
2018-02-11 22:47:50 +00:00
|
|
|
->shouldReceive('update')->with($this->model->id, m::subset([
|
2017-12-31 01:56:42 +00:00
|
|
|
'user_viewable' => false,
|
|
|
|
'user_editable' => false,
|
2018-02-17 19:37:53 +00:00
|
|
|
'description' => '',
|
2018-02-11 22:47:50 +00:00
|
|
|
]))->once()->andReturn(true);
|
2017-12-31 01:56:42 +00:00
|
|
|
|
2018-02-17 19:37:53 +00:00
|
|
|
$this->assertTrue($this->service->handle($this->model, ['options' => null, 'description' => null]));
|
2017-12-31 01:56:42 +00:00
|
|
|
}
|
|
|
|
|
2017-10-08 04:29:08 +00:00
|
|
|
/**
|
|
|
|
* Test that data passed into the handler is overwritten inside the handler.
|
|
|
|
*/
|
|
|
|
public function testDataPassedIntoHandlerTakesLowerPriorityThanDataSet()
|
|
|
|
{
|
2018-01-05 04:49:50 +00:00
|
|
|
$this->repository->shouldReceive('setColumns')->with('id')->once()->andReturnSelf()
|
2017-10-08 04:29:08 +00:00
|
|
|
->shouldReceive('findCountWhere')->with([
|
|
|
|
['env_variable', '=', 'TEST_VAR_123'],
|
|
|
|
['egg_id', '=', $this->model->option_id],
|
|
|
|
['id', '!=', $this->model->id],
|
|
|
|
])->once()->andReturn(0);
|
|
|
|
|
2018-01-05 22:33:50 +00:00
|
|
|
$this->repository->shouldReceive('withoutFreshModel')->withNoArgs()->once()->andReturnSelf()
|
2018-02-11 22:47:50 +00:00
|
|
|
->shouldReceive('update')->with($this->model->id, m::subset([
|
2017-10-08 04:29:08 +00:00
|
|
|
'user_viewable' => false,
|
|
|
|
'user_editable' => false,
|
|
|
|
'env_variable' => 'TEST_VAR_123',
|
2018-02-11 22:47:50 +00:00
|
|
|
]))->once()->andReturn(true);
|
2017-10-08 04:29:08 +00:00
|
|
|
|
|
|
|
$this->assertTrue($this->service->handle($this->model, ['user_viewable' => 123456, 'env_variable' => 'TEST_VAR_123']));
|
|
|
|
}
|
|
|
|
|
2017-08-13 19:55:09 +00:00
|
|
|
/**
|
|
|
|
* Test that a non-unique environment variable triggers an exception.
|
|
|
|
*/
|
|
|
|
public function testExceptionIsThrownIfEnvironmentVariableIsNotUnique()
|
|
|
|
{
|
2018-01-05 04:49:50 +00:00
|
|
|
$this->repository->shouldReceive('setColumns')->with('id')->once()->andReturnSelf()
|
2017-08-13 19:55:09 +00:00
|
|
|
->shouldReceive('findCountWhere')->with([
|
|
|
|
['env_variable', '=', 'TEST_VAR_123'],
|
2017-10-08 04:29:08 +00:00
|
|
|
['egg_id', '=', $this->model->option_id],
|
2017-08-16 04:21:01 +00:00
|
|
|
['id', '!=', $this->model->id],
|
2017-08-13 19:55:09 +00:00
|
|
|
])->once()->andReturn(1);
|
|
|
|
|
|
|
|
try {
|
|
|
|
$this->service->handle($this->model, ['env_variable' => 'TEST_VAR_123']);
|
|
|
|
} catch (Exception $exception) {
|
|
|
|
$this->assertInstanceOf(DisplayException::class, $exception);
|
2017-09-03 21:32:52 +00:00
|
|
|
$this->assertEquals(trans('exceptions.service.variables.env_not_unique', [
|
2017-08-13 19:55:09 +00:00
|
|
|
'name' => 'TEST_VAR_123',
|
|
|
|
]), $exception->getMessage());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test that all of the reserved variables defined in the model trigger an exception.
|
|
|
|
*
|
|
|
|
* @dataProvider reservedNamesProvider
|
2017-10-08 04:29:08 +00:00
|
|
|
* @expectedException \Pterodactyl\Exceptions\Service\Egg\Variable\ReservedVariableNameException
|
2017-08-13 19:55:09 +00:00
|
|
|
*/
|
2017-10-08 04:29:08 +00:00
|
|
|
public function testExceptionIsThrownIfEnvironmentVariableIsInListOfReservedNames(string $variable)
|
2017-08-13 19:55:09 +00:00
|
|
|
{
|
|
|
|
$this->service->handle($this->model, ['env_variable' => $variable]);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Provides the data to be used in the tests.
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function reservedNamesProvider()
|
|
|
|
{
|
|
|
|
$data = [];
|
2017-10-07 04:57:53 +00:00
|
|
|
$exploded = explode(',', EggVariable::RESERVED_ENV_NAMES);
|
2017-08-13 19:55:09 +00:00
|
|
|
foreach ($exploded as $e) {
|
|
|
|
$data[] = [$e];
|
|
|
|
}
|
|
|
|
|
|
|
|
return $data;
|
|
|
|
}
|
|
|
|
}
|