Skip to content

Commit 3ecab82

Browse files
committed
Fix exception when empty default value is passed for an egg variable, closes pterodactyl#934
1 parent bf53792 commit 3ecab82

File tree

3 files changed

+19
-20
lines changed

3 files changed

+19
-20
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ This project follows [Semantic Versioning](http://semver.org) guidelines.
1010
* `[rc.2]` — Fixes bug with server creation API endpoint that would fail to validate `allocation.default` correctly.
1111
* `[rc.2]` — Fix data integrity exception occuring due to invalid data being passed to server creation service on the API.
1212
* `[rc.2]` — Fix data integrity exception that could occur when an email containing non-username characters was passed.
13+
* `[rc.2]` — Fix data integrity exception occurring when no default value is provided for an egg variable.
1314

1415
### Added
1516
* Added ability to search the following API endpoints: list users, list servers, and list locations.

app/Services/Eggs/Variables/VariableUpdateService.php

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -27,21 +27,17 @@ public function __construct(EggVariableRepositoryInterface $repository)
2727
/**
2828
* Update a specific egg variable.
2929
*
30-
* @param int|\Pterodactyl\Models\EggVariable $variable
31-
* @param array $data
30+
* @param \Pterodactyl\Models\EggVariable $variable
31+
* @param array $data
3232
* @return mixed
3333
*
3434
* @throws \Pterodactyl\Exceptions\DisplayException
3535
* @throws \Pterodactyl\Exceptions\Model\DataValidationException
3636
* @throws \Pterodactyl\Exceptions\Repository\RecordNotFoundException
3737
* @throws \Pterodactyl\Exceptions\Service\Egg\Variable\ReservedVariableNameException
3838
*/
39-
public function handle($variable, array $data)
39+
public function handle(EggVariable $variable, array $data)
4040
{
41-
if (! $variable instanceof EggVariable) {
42-
$variable = $this->repository->find($variable);
43-
}
44-
4541
if (! is_null(array_get($data, 'env_variable'))) {
4642
if (in_array(strtoupper(array_get($data, 'env_variable')), explode(',', EggVariable::RESERVED_ENV_NAMES))) {
4743
throw new ReservedVariableNameException(trans('exceptions.service.variables.reserved_name', [
@@ -65,6 +61,7 @@ public function handle($variable, array $data)
6561
$options = array_get($data, 'options') ?? [];
6662

6763
return $this->repository->withoutFreshModel()->update($variable->id, array_merge($data, [
64+
'default_value' => array_get($data, 'default_value') ?? '',
6865
'user_viewable' => in_array('user_viewable', $options),
6966
'user_editable' => in_array('user_editable', $options),
7067
]));

tests/Unit/Services/Eggs/Variables/VariableUpdateServiceTest.php

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -46,29 +46,30 @@ public function setUp()
4646
public function testVariableIsUpdatedWhenNoEnvironmentVariableIsPassed()
4747
{
4848
$this->repository->shouldReceive('withoutFreshModel')->withNoArgs()->once()->andReturnSelf()
49-
->shouldReceive('update')->with($this->model->id, [
49+
->shouldReceive('update')->with($this->model->id, m::subset([
5050
'user_viewable' => false,
5151
'user_editable' => false,
5252
'test-data' => 'test-value',
53-
])->once()->andReturn(true);
53+
]))->once()->andReturn(true);
5454

5555
$this->assertTrue($this->service->handle($this->model, ['test-data' => 'test-value']));
5656
}
5757

5858
/**
59-
* Test that a service variable ID can be passed in place of the model.
59+
* Test that a null value passed in for the default is converted to a string.
60+
*
61+
* @see https://github.com/Pterodactyl/Panel/issues/934
6062
*/
61-
public function testVariableIdCanBePassedInPlaceOfModel()
63+
public function testNullDefaultValue()
6264
{
63-
$this->repository->shouldReceive('find')->with($this->model->id)->once()->andReturn($this->model);
6465
$this->repository->shouldReceive('withoutFreshModel')->withNoArgs()->once()->andReturnSelf()
6566
->shouldReceive('update')->with($this->model->id, [
6667
'user_viewable' => false,
6768
'user_editable' => false,
68-
'test-data' => 'test-value',
69+
'default_value' => '',
6970
])->once()->andReturn(true);
7071

71-
$this->assertTrue($this->service->handle($this->model->id, ['test-data' => 'test-value']));
72+
$this->assertTrue($this->service->handle($this->model, ['default_value' => null]));
7273
}
7374

7475
/**
@@ -84,11 +85,11 @@ public function testVariableIsUpdatedWhenValidEnvironmentVariableIsPassed()
8485
])->once()->andReturn(0);
8586

8687
$this->repository->shouldReceive('withoutFreshModel')->withNoArgs()->once()->andReturnSelf()
87-
->shouldReceive('update')->with($this->model->id, [
88+
->shouldReceive('update')->with($this->model->id, m::subset([
8889
'user_viewable' => false,
8990
'user_editable' => false,
9091
'env_variable' => 'TEST_VAR_123',
91-
])->once()->andReturn(true);
92+
]))->once()->andReturn(true);
9293

9394
$this->assertTrue($this->service->handle($this->model, ['env_variable' => 'TEST_VAR_123']));
9495
}
@@ -102,11 +103,11 @@ public function testVariableIsUpdatedWhenValidEnvironmentVariableIsPassed()
102103
public function testNullOptionValueIsPassedAsArray()
103104
{
104105
$this->repository->shouldReceive('withoutFreshModel')->withNoArgs()->once()->andReturnSelf()
105-
->shouldReceive('update')->with($this->model->id, [
106+
->shouldReceive('update')->with($this->model->id, m::subset([
106107
'user_viewable' => false,
107108
'user_editable' => false,
108109
'options' => null,
109-
])->once()->andReturn(true);
110+
]))->once()->andReturn(true);
110111

111112
$this->assertTrue($this->service->handle($this->model, ['options' => null]));
112113
}
@@ -124,11 +125,11 @@ public function testDataPassedIntoHandlerTakesLowerPriorityThanDataSet()
124125
])->once()->andReturn(0);
125126

126127
$this->repository->shouldReceive('withoutFreshModel')->withNoArgs()->once()->andReturnSelf()
127-
->shouldReceive('update')->with($this->model->id, [
128+
->shouldReceive('update')->with($this->model->id, m::subset([
128129
'user_viewable' => false,
129130
'user_editable' => false,
130131
'env_variable' => 'TEST_VAR_123',
131-
])->once()->andReturn(true);
132+
]))->once()->andReturn(true);
132133

133134
$this->assertTrue($this->service->handle($this->model, ['user_viewable' => 123456, 'env_variable' => 'TEST_VAR_123']));
134135
}

0 commit comments

Comments
 (0)