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 Mockery as m;
|
2017-08-16 04:21:01 +00:00
|
|
|
use Tests\TestCase;
|
2018-03-17 20:09:09 +00:00
|
|
|
use BadMethodCallException;
|
2017-10-07 04:57:53 +00:00
|
|
|
use Pterodactyl\Models\EggVariable;
|
2018-03-17 20:09:09 +00:00
|
|
|
use Illuminate\Contracts\Validation\Factory;
|
2017-10-08 04:29:08 +00:00
|
|
|
use Pterodactyl\Services\Eggs\Variables\VariableCreationService;
|
|
|
|
use Pterodactyl\Contracts\Repository\EggVariableRepositoryInterface;
|
2017-08-13 19:55:09 +00:00
|
|
|
|
|
|
|
class VariableCreationServiceTest extends TestCase
|
|
|
|
{
|
|
|
|
/**
|
2017-10-08 04:29:08 +00:00
|
|
|
* @var \Pterodactyl\Contracts\Repository\EggVariableRepositoryInterface|\Mockery\Mock
|
2017-08-13 19:55:09 +00:00
|
|
|
*/
|
2018-03-17 20:09:09 +00:00
|
|
|
private $repository;
|
2017-08-13 19:55:09 +00:00
|
|
|
|
|
|
|
/**
|
2018-03-17 20:09:09 +00:00
|
|
|
* @var \Illuminate\Contracts\Validation\Factory|\Mockery\Mock
|
2017-08-13 19:55:09 +00:00
|
|
|
*/
|
2018-03-17 20:09:09 +00:00
|
|
|
private $validator;
|
2017-08-13 19:55:09 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Setup tests.
|
|
|
|
*/
|
|
|
|
public function setUp()
|
|
|
|
{
|
|
|
|
parent::setUp();
|
|
|
|
|
2017-10-08 04:29:08 +00:00
|
|
|
$this->repository = m::mock(EggVariableRepositoryInterface::class);
|
2018-03-17 20:09:09 +00:00
|
|
|
$this->validator = m::mock(Factory::class);
|
2017-08-13 19:55:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test basic functionality, data should be stored in the database.
|
|
|
|
*/
|
|
|
|
public function testVariableIsCreatedAndStored()
|
|
|
|
{
|
2018-02-17 19:37:53 +00:00
|
|
|
$data = ['env_variable' => 'TEST_VAR_123', 'default_value' => 'test'];
|
|
|
|
$this->repository->shouldReceive('create')->with(m::subset([
|
2017-10-08 04:29:08 +00:00
|
|
|
'egg_id' => 1,
|
2018-02-17 19:37:53 +00:00
|
|
|
'default_value' => 'test',
|
2017-10-08 04:29:08 +00:00
|
|
|
'user_viewable' => false,
|
|
|
|
'user_editable' => false,
|
|
|
|
'env_variable' => 'TEST_VAR_123',
|
2018-02-17 19:37:53 +00:00
|
|
|
]))->once()->andReturn(new EggVariable);
|
2017-08-13 19:55:09 +00:00
|
|
|
|
2018-03-17 20:09:09 +00:00
|
|
|
$this->assertInstanceOf(EggVariable::class, $this->getService()->handle(1, $data));
|
2017-08-13 19:55:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test that the option key in the data array is properly parsed.
|
|
|
|
*/
|
|
|
|
public function testOptionsPassedInArrayKeyAreParsedProperly()
|
|
|
|
{
|
|
|
|
$data = ['env_variable' => 'TEST_VAR_123', 'options' => ['user_viewable', 'user_editable']];
|
2018-02-17 19:37:53 +00:00
|
|
|
$this->repository->shouldReceive('create')->with(m::subset([
|
|
|
|
'default_value' => '',
|
2017-08-13 19:55:09 +00:00
|
|
|
'user_viewable' => true,
|
|
|
|
'user_editable' => true,
|
|
|
|
'env_variable' => 'TEST_VAR_123',
|
2018-02-17 19:37:53 +00:00
|
|
|
]))->once()->andReturn(new EggVariable);
|
2017-08-13 19:55:09 +00:00
|
|
|
|
2018-03-17 20:09:09 +00:00
|
|
|
$this->assertInstanceOf(EggVariable::class, $this->getService()->handle(1, $data));
|
2017-08-13 19:55:09 +00:00
|
|
|
}
|
|
|
|
|
2017-12-31 01:56:42 +00:00
|
|
|
/**
|
|
|
|
* Test that an empty (null) value passed in the option key is handled
|
2018-05-13 14:50:56 +00:00
|
|
|
* properly as an array. Also tests the same case against the default_value.
|
2017-12-31 01:56:42 +00:00
|
|
|
*
|
|
|
|
* @see https://github.com/Pterodactyl/Panel/issues/841
|
2018-02-17 19:37:53 +00:00
|
|
|
* @see https://github.com/Pterodactyl/Panel/issues/943
|
2017-12-31 01:56:42 +00:00
|
|
|
*/
|
|
|
|
public function testNullOptionValueIsPassedAsArray()
|
|
|
|
{
|
2018-02-17 19:37:53 +00:00
|
|
|
$data = ['env_variable' => 'TEST_VAR_123', 'options' => null, 'default_value' => null];
|
|
|
|
$this->repository->shouldReceive('create')->with(m::subset([
|
|
|
|
'default_value' => '',
|
2017-12-31 01:56:42 +00:00
|
|
|
'user_viewable' => false,
|
|
|
|
'user_editable' => false,
|
2018-02-17 19:37:53 +00:00
|
|
|
]))->once()->andReturn(new EggVariable);
|
2017-12-31 01:56:42 +00:00
|
|
|
|
2018-03-17 20:09:09 +00:00
|
|
|
$this->assertInstanceOf(EggVariable::class, $this->getService()->handle(1, $data));
|
2017-12-31 01:56:42 +00:00
|
|
|
}
|
|
|
|
|
2017-08-13 19:55:09 +00:00
|
|
|
/**
|
|
|
|
* Test that all of the reserved variables defined in the model trigger an exception.
|
|
|
|
*
|
2018-03-17 20:09:09 +00:00
|
|
|
* @param string $variable
|
|
|
|
*
|
2017-08-13 19:55:09 +00:00
|
|
|
* @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
|
|
|
{
|
2018-03-17 20:09:09 +00:00
|
|
|
$this->getService()->handle(1, ['env_variable' => $variable]);
|
2017-08-13 19:55:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-10-08 04:29:08 +00:00
|
|
|
* Test that the egg ID applied in the function takes higher priority than an
|
|
|
|
* ID passed into the handler.
|
2017-08-13 19:55:09 +00:00
|
|
|
*/
|
2017-10-08 04:29:08 +00:00
|
|
|
public function testEggIdPassedInDataIsNotApplied()
|
2017-08-13 19:55:09 +00:00
|
|
|
{
|
2017-10-08 04:29:08 +00:00
|
|
|
$data = ['egg_id' => 123456, 'env_variable' => 'TEST_VAR_123'];
|
2018-02-17 19:37:53 +00:00
|
|
|
$this->repository->shouldReceive('create')->with(m::subset([
|
2017-10-08 04:29:08 +00:00
|
|
|
'egg_id' => 1,
|
2018-02-17 19:37:53 +00:00
|
|
|
]))->once()->andReturn(new EggVariable);
|
2017-08-13 19:55:09 +00:00
|
|
|
|
2018-03-17 20:09:09 +00:00
|
|
|
$this->assertInstanceOf(EggVariable::class, $this->getService()->handle(1, $data));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test that validation errors due to invalid rules are caught and handled properly.
|
|
|
|
*
|
|
|
|
* @expectedException \Pterodactyl\Exceptions\Service\Egg\Variable\BadValidationRuleException
|
|
|
|
* @expectedExceptionMessage The validation rule "hodor_door" is not a valid rule for this application.
|
|
|
|
*/
|
|
|
|
public function testInvalidValidationRulesResultInException()
|
|
|
|
{
|
|
|
|
$data = ['env_variable' => 'TEST_VAR_123', 'rules' => 'string|hodorDoor'];
|
|
|
|
|
|
|
|
$this->validator->shouldReceive('make')->once()
|
|
|
|
->with(['__TEST' => 'test'], ['__TEST' => 'string|hodorDoor'])
|
|
|
|
->andReturnSelf();
|
|
|
|
|
|
|
|
$this->validator->shouldReceive('fails')->once()
|
|
|
|
->withNoArgs()
|
|
|
|
->andThrow(new BadMethodCallException('Method [validateHodorDoor] does not exist.'));
|
|
|
|
|
|
|
|
$this->getService()->handle(1, $data);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test that an exception not stemming from a bad rule is not caught.
|
|
|
|
*
|
|
|
|
* @expectedException \BadMethodCallException
|
|
|
|
* @expectedExceptionMessage Received something, but no expectations were specified.
|
|
|
|
*/
|
|
|
|
public function testExceptionNotCausedByBadRuleIsNotCaught()
|
|
|
|
{
|
|
|
|
$data = ['env_variable' => 'TEST_VAR_123', 'rules' => 'string'];
|
|
|
|
|
|
|
|
$this->validator->shouldReceive('make')->once()
|
|
|
|
->with(['__TEST' => 'test'], ['__TEST' => 'string'])
|
|
|
|
->andReturnSelf();
|
|
|
|
|
|
|
|
$this->validator->shouldReceive('fails')->once()
|
|
|
|
->withNoArgs()
|
|
|
|
->andThrow(new BadMethodCallException('Received something, but no expectations were specified.'));
|
|
|
|
|
|
|
|
$this->getService()->handle(1, $data);
|
2017-08-13 19:55:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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;
|
|
|
|
}
|
2018-03-17 20:09:09 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Return an instance of the service with mocked dependencies for testing.
|
|
|
|
*
|
|
|
|
* @return \Pterodactyl\Services\Eggs\Variables\VariableCreationService
|
|
|
|
*/
|
|
|
|
private function getService(): VariableCreationService
|
|
|
|
{
|
|
|
|
return new VariableCreationService($this->repository, $this->validator);
|
|
|
|
}
|
2017-08-13 19:55:09 +00:00
|
|
|
}
|