Path: blob/1.0-develop/tests/Integration/Services/Servers/VariableValidatorServiceTest.php
7460 views
<?php12namespace Pterodactyl\Tests\Integration\Services\Servers;34use Pterodactyl\Models\Egg;5use Pterodactyl\Models\User;6use Illuminate\Support\Collection;7use Illuminate\Validation\ValidationException;8use Pterodactyl\Tests\Integration\IntegrationTestCase;9use Pterodactyl\Services\Servers\VariableValidatorService;1011class VariableValidatorServiceTest extends IntegrationTestCase12{13protected Egg $egg;1415public function setUp(): void16{17parent::setUp();1819/* @noinspection PhpFieldAssignmentTypeMismatchInspection */20$this->egg = Egg::query()21->where('author', '[email protected]')22->where('name', 'Bungeecord')23->firstOrFail();24}2526/**27* Test that environment variables for a server are validated as expected.28*/29public function testEnvironmentVariablesCanBeValidated()30{31$egg = $this->cloneEggAndVariables($this->egg);3233try {34$this->getService()->handle($egg->id, [35'BUNGEE_VERSION' => '1.2.3',36]);3738$this->fail('This statement should not be reached.');39} catch (ValidationException $exception) {40$errors = $exception->errors();4142$this->assertCount(2, $errors);43$this->assertArrayHasKey('environment.BUNGEE_VERSION', $errors);44$this->assertArrayHasKey('environment.SERVER_JARFILE', $errors);45$this->assertSame('The Bungeecord Version variable may only contain letters and numbers.', $errors['environment.BUNGEE_VERSION'][0]);46$this->assertSame('The Bungeecord Jar File variable field is required.', $errors['environment.SERVER_JARFILE'][0]);47}4849$response = $this->getService()->handle($egg->id, [50'BUNGEE_VERSION' => '1234',51'SERVER_JARFILE' => 'server.jar',52]);5354$this->assertInstanceOf(Collection::class, $response);55$this->assertCount(2, $response);56$this->assertSame('BUNGEE_VERSION', $response->get(0)->key);57$this->assertSame('1234', $response->get(0)->value);58$this->assertSame('SERVER_JARFILE', $response->get(1)->key);59$this->assertSame('server.jar', $response->get(1)->value);60}6162/**63* Test that variables that are user_editable=false do not get validated (or returned) by64* the handler.65*/66public function testNormalUserCannotValidateNonUserEditableVariables()67{68$egg = $this->cloneEggAndVariables($this->egg);69$egg->variables()->first()->update([70'user_editable' => false,71]);7273$response = $this->getService()->handle($egg->id, [74// This is an invalid value, but it shouldn't cause any issues since it should be skipped.75'BUNGEE_VERSION' => '1.2.3',76'SERVER_JARFILE' => 'server.jar',77]);7879$this->assertInstanceOf(Collection::class, $response);80$this->assertCount(1, $response);81$this->assertSame('SERVER_JARFILE', $response->get(0)->key);82$this->assertSame('server.jar', $response->get(0)->value);83}8485public function testEnvironmentVariablesCanBeUpdatedAsAdmin()86{87$egg = $this->cloneEggAndVariables($this->egg);88$egg->variables()->first()->update([89'user_editable' => false,90]);9192try {93$this->getService()->setUserLevel(User::USER_LEVEL_ADMIN)->handle($egg->id, [94'BUNGEE_VERSION' => '1.2.3',95'SERVER_JARFILE' => 'server.jar',96]);9798$this->fail('This statement should not be reached.');99} catch (ValidationException $exception) {100$this->assertCount(1, $exception->errors());101$this->assertArrayHasKey('environment.BUNGEE_VERSION', $exception->errors());102}103104$response = $this->getService()->setUserLevel(User::USER_LEVEL_ADMIN)->handle($egg->id, [105'BUNGEE_VERSION' => '123',106'SERVER_JARFILE' => 'server.jar',107]);108109$this->assertInstanceOf(Collection::class, $response);110$this->assertCount(2, $response);111$this->assertSame('BUNGEE_VERSION', $response->get(0)->key);112$this->assertSame('123', $response->get(0)->value);113$this->assertSame('SERVER_JARFILE', $response->get(1)->key);114$this->assertSame('server.jar', $response->get(1)->value);115}116117public function testNullableEnvironmentVariablesCanBeUsedCorrectly()118{119$egg = $this->cloneEggAndVariables($this->egg);120$egg->variables()->where('env_variable', '!=', 'BUNGEE_VERSION')->delete();121122$egg->variables()->update(['rules' => 'nullable|string']);123124$response = $this->getService()->handle($egg->id, []);125$this->assertCount(1, $response);126$this->assertNull($response->get(0)->value);127128$response = $this->getService()->handle($egg->id, ['BUNGEE_VERSION' => null]);129$this->assertCount(1, $response);130$this->assertNull($response->get(0)->value);131132$response = $this->getService()->handle($egg->id, ['BUNGEE_VERSION' => '']);133$this->assertCount(1, $response);134$this->assertSame('', $response->get(0)->value);135}136137private function getService(): VariableValidatorService138{139return $this->app->make(VariableValidatorService::class);140}141}142143144