egg = Egg::query()->where('author', 'support@pterodactyl.io')->where('name', 'Bungeecord')->firstOrFail(); }); test('environment variables can be validated', function () { $egg = $this->cloneEggAndVariables($this->egg); try { getService()->validate($egg, ['BUNGEE_VERSION' => '1.2.3']); $this->fail('This statement should not be reached.'); } catch (ValidationException $exception) { $errors = $exception->errors(); expect($errors)->toHaveCount(2); expect($errors)->toHaveKey('environment.BUNGEE_VERSION'); expect($errors)->toHaveKey('environment.SERVER_JARFILE'); expect($errors['environment.BUNGEE_VERSION'][0])->toBe('The Bungeecord Version variable may only contain letters and numbers.'); expect($errors['environment.SERVER_JARFILE'][0])->toBe('The Bungeecord Jar File variable field is required.'); } $response = getService()->validate($egg, ['BUNGEE_VERSION' => '1234', 'SERVER_JARFILE' => 'server.jar']); expect($response)->toBeInstanceOf(Collection::class); expect($response)->toHaveCount(2); expect($response->get(0)->key)->toBe('BUNGEE_VERSION'); expect($response->get(0)->value)->toBe('1234'); expect($response->get(1)->key)->toBe('SERVER_JARFILE'); expect($response->get(1)->value)->toBe('server.jar'); }); test('normal user cannot validate non user editable variables', function () { $egg = $this->cloneEggAndVariables($this->egg); $egg->variables()->first()->update(['user_editable' => false]); $response = getService()->validate($egg, [ // This is an invalid value, but it shouldn't cause any issues since it should be skipped. 'BUNGEE_VERSION' => '1.2.3', 'SERVER_JARFILE' => 'server.jar', ]); expect($response)->toBeInstanceOf(Collection::class); expect($response)->toHaveCount(1); expect($response->get(0)->key)->toBe('SERVER_JARFILE'); expect($response->get(0)->value)->toBe('server.jar'); }); test('environment variables can be updated as admin', function () { $egg = $this->cloneEggAndVariables($this->egg); $egg->variables()->first()->update(['user_editable' => false]); try { getService()->validate($egg, ['BUNGEE_VERSION' => '1.2.3', 'SERVER_JARFILE' => 'server.jar'], User::USER_LEVEL_ADMIN); $this->fail('This statement should not be reached.'); } catch (ValidationException $exception) { expect($exception->errors())->toHaveCount(1); expect($exception->errors())->toHaveKey('environment.BUNGEE_VERSION'); } $response = getService()->validate($egg, ['BUNGEE_VERSION' => '123', 'SERVER_JARFILE' => 'server.jar'], User::USER_LEVEL_ADMIN); expect($response)->toBeInstanceOf(Collection::class); expect($response)->toHaveCount(2); expect($response->get(0)->key)->toBe('BUNGEE_VERSION'); expect($response->get(0)->value)->toBe('123'); expect($response->get(1)->key)->toBe('SERVER_JARFILE'); expect($response->get(1)->value)->toBe('server.jar'); }); test('nullable environment variables can be used correctly', function () { $egg = $this->cloneEggAndVariables($this->egg); $egg->variables()->where('env_variable', '!=', 'BUNGEE_VERSION')->delete(); $egg->variables()->update(['rules' => 'nullable|string']); $response = getService()->validate($egg, []); expect($response)->toHaveCount(1); expect($response->get(0)->value)->toBeNull(); $response = getService()->validate($egg, ['BUNGEE_VERSION' => null]); expect($response)->toHaveCount(1); expect($response->get(0)->value)->toBeNull(); $response = getService()->validate($egg, ['BUNGEE_VERSION' => '']); expect($response)->toHaveCount(1); expect($response->get(0)->value)->toBe(''); }); function getService(): VariableValidatorService { return (function () { return $this->app->make(VariableValidatorService::class); })->call(pterodactylTestCase()); }