pelican-panel-mirror/tests/Integration/Services/Servers/VariableValidatorServiceTest.php
Lance Pioch 8261184b57
Officially support PostgreSQL database (#1066)
* Just skip this table because it no longer exists

* Add postgresql

* This no longer needs to be there

* These are the same output in mysql, but different in postgresql

* Fix these migrations for postgresql

* This table no longer exists

* This is expected to be a json column for json operations, required for postgresql

* Shoot for the stars

* Fix pint

* Why was this missing

* Updates

* Restore this

* This needs to be explicit

* Don’t like strings

* Fix these classes

* Use different method to compare dates

* Apparently postgresql doesn’t like case insensitivity

* Postgresql orders it backwards

* Ordered different by postgresql

* Unnecessary and breaking

* Make sure the order is correct for postresql

* Fix this with the order too

* Remove this

* Force email to be lowercased

* Update app/Models/User.php
2025-03-30 14:44:03 -04:00

146 lines
5.4 KiB
PHP

<?php
namespace App\Tests\Integration\Services\Servers;
use App\Models\Egg;
use App\Models\User;
use Illuminate\Support\Collection;
use Illuminate\Validation\ValidationException;
use App\Tests\Integration\IntegrationTestCase;
use App\Services\Servers\VariableValidatorService;
class VariableValidatorServiceTest extends IntegrationTestCase
{
protected Egg $egg;
protected function setUp(): void
{
parent::setUp();
/* @noinspection PhpFieldAssignmentTypeMismatchInspection */
$this->egg = Egg::query()
->where('author', 'panel@example.com')
->where('name', 'Bungeecord')
->firstOrFail();
}
/**
* Test that environment variables for a server are validated as expected.
*/
public function test_environment_variables_can_be_validated(): void
{
$egg = $this->cloneEggAndVariables($this->egg);
try {
$this->getService()->handle($egg->id, [
'BUNGEE_VERSION' => '1.2.3',
]);
$this->fail('This statement should not be reached.');
} catch (ValidationException $exception) {
$errors = $exception->errors();
$this->assertCount(2, $errors);
$this->assertArrayHasKey('environment.BUNGEE_VERSION', $errors);
$this->assertArrayHasKey('environment.SERVER_JARFILE', $errors);
$this->assertSame('The Bungeecord Version variable may only contain letters and numbers.', $errors['environment.BUNGEE_VERSION'][0]);
$this->assertSame('The Bungeecord Jar File variable field is required.', $errors['environment.SERVER_JARFILE'][0]);
}
$response = $this->getService()->handle($egg->id, [
'BUNGEE_VERSION' => '1234',
'SERVER_JARFILE' => 'server.jar',
]);
$this->assertInstanceOf(Collection::class, $response);
$this->assertCount(2, $response);
$this->assertSame('BUNGEE_VERSION', $response->get(0)->key);
$this->assertSame('1234', $response->get(0)->value);
$this->assertSame('SERVER_JARFILE', $response->get(1)->key);
$this->assertSame('server.jar', $response->get(1)->value);
}
/**
* Test that variables that are user_editable=false do not get validated (or returned) by
* the handler.
*/
public function test_normal_user_cannot_validate_non_user_editable_variables(): void
{
$egg = $this->cloneEggAndVariables($this->egg);
$egg->variables()->first()->update([
'user_editable' => false,
]);
$response = $this->getService()->handle($egg->id, [
// 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',
]);
$this->assertInstanceOf(Collection::class, $response);
$this->assertCount(1, $response);
$this->assertSame('SERVER_JARFILE', $response->get(0)->key);
$this->assertSame('server.jar', $response->get(0)->value);
}
public function test_environment_variables_can_be_updated_as_admin(): void
{
$egg = $this->cloneEggAndVariables($this->egg);
$egg->variables()->first()->update([
'user_editable' => false,
]);
try {
$this->getService()->setUserLevel(User::USER_LEVEL_ADMIN)->handle($egg->id, [
'BUNGEE_VERSION' => '1.2.3',
'SERVER_JARFILE' => 'server.jar',
]);
$this->fail('This statement should not be reached.');
} catch (ValidationException $exception) {
$this->assertCount(1, $exception->errors());
$this->assertArrayHasKey('environment.BUNGEE_VERSION', $exception->errors());
}
$response = $this->getService()->setUserLevel(User::USER_LEVEL_ADMIN)->handle($egg->id, [
'BUNGEE_VERSION' => '123',
'SERVER_JARFILE' => 'server.jar',
]);
$bungeeVersion = $response->firstWhere('key', 'BUNGEE_VERSION');
$serverJarfile = $response->firstWhere('key', 'SERVER_JARFILE');
$this->assertInstanceOf(Collection::class, $response);
$this->assertCount(2, $response);
$this->assertSame('BUNGEE_VERSION', $bungeeVersion->key);
$this->assertSame('123', $bungeeVersion->value);
$this->assertSame('SERVER_JARFILE', $serverJarfile->key);
$this->assertSame('server.jar', $serverJarfile->value);
}
public function test_nullable_environment_variables_can_be_used_correctly(): void
{
$egg = $this->cloneEggAndVariables($this->egg);
$egg->variables()->where('env_variable', '!=', 'BUNGEE_VERSION')->delete();
$egg->variables()->update(['rules' => ['nullable', 'string']]);
$response = $this->getService()->handle($egg->id, []);
$this->assertCount(1, $response);
$this->assertNull($response->get(0)->value);
$response = $this->getService()->handle($egg->id, ['BUNGEE_VERSION' => null]);
$this->assertCount(1, $response);
$this->assertNull($response->get(0)->value);
$response = $this->getService()->handle($egg->id, ['BUNGEE_VERSION' => '']);
$this->assertCount(1, $response);
$this->assertSame('', $response->get(0)->value);
}
private function getService(): VariableValidatorService
{
return $this->app->make(VariableValidatorService::class);
}
}