142 lines
4.0 KiB
PHP
142 lines
4.0 KiB
PHP
<?php
|
|
|
|
app_require('modules/selfserve/classes/selfserve_config_versioning.php');
|
|
app_require('objects/selfserve_config_versions_o.php');
|
|
|
|
use modules\selfserve\classes\selfserve_config_versioning;
|
|
use objects\selfserve_config_versions_o;
|
|
|
|
function selfserve_config_versioning_without_constructor(): selfserve_config_versioning
|
|
{
|
|
$reflection = new ReflectionClass(selfserve_config_versioning::class);
|
|
return $reflection->newInstanceWithoutConstructor();
|
|
}
|
|
|
|
it('validates typed task gates for known references', function (): void {
|
|
$service = selfserve_config_versioning_without_constructor();
|
|
|
|
$validation = $service->validateConfig([
|
|
'questions' => [
|
|
['id' => 10],
|
|
],
|
|
'conditions' => [
|
|
['id' => 20],
|
|
],
|
|
'rules' => [],
|
|
'tasks' => [
|
|
[
|
|
'id' => 100,
|
|
'gate_type' => 'ALWAYS',
|
|
'gate_ref_id' => null,
|
|
],
|
|
[
|
|
'id' => 101,
|
|
'gate_type' => 'CONDITION',
|
|
'gate_ref_id' => 20,
|
|
],
|
|
[
|
|
'id' => 102,
|
|
'gate_type' => 'QUESTION',
|
|
'gate_ref_id' => 10,
|
|
],
|
|
],
|
|
]);
|
|
|
|
expect($validation['valid'])->toBeTrue();
|
|
expect($validation['errors'])->toBe([]);
|
|
});
|
|
|
|
it('fails validation when typed task gates reference unknown entities', function (): void {
|
|
$service = selfserve_config_versioning_without_constructor();
|
|
|
|
$validation = $service->validateConfig([
|
|
'questions' => [
|
|
['id' => 1],
|
|
],
|
|
'conditions' => [
|
|
['id' => 2],
|
|
],
|
|
'rules' => [],
|
|
'tasks' => [
|
|
[
|
|
'id' => 200,
|
|
'gate_type' => 'CONDITION',
|
|
'gate_ref_id' => 999,
|
|
],
|
|
[
|
|
'id' => 201,
|
|
'gate_type' => 'QUESTION',
|
|
'gate_ref_id' => 998,
|
|
],
|
|
[
|
|
'id' => 202,
|
|
'gate_type' => 'QUESTION',
|
|
'gate_ref_id' => null,
|
|
],
|
|
],
|
|
]);
|
|
|
|
expect($validation['valid'])->toBeFalse();
|
|
expect(implode("\n", $validation['errors']))->toContain('unknown condition gate_ref_id 999');
|
|
expect(implode("\n", $validation['errors']))->toContain('unknown question gate_ref_id 998');
|
|
expect(implode("\n", $validation['errors']))->toContain('requires gate_ref_id');
|
|
});
|
|
|
|
it('encodes config json payloads with apostrophes before persistence', function (): void {
|
|
$version = new class extends selfserve_config_versions_o {
|
|
/** @var array<string,mixed> */
|
|
public array $capturedPayload = [];
|
|
|
|
public function __construct()
|
|
{
|
|
// Skip db bootstrap for this unit test.
|
|
}
|
|
|
|
public function add_object(array $data): int
|
|
{
|
|
$this->capturedPayload = $data;
|
|
return 123;
|
|
}
|
|
|
|
public function getObjectProperties(): void
|
|
{
|
|
// No-op for this unit test.
|
|
}
|
|
|
|
public function objectChanged(): void
|
|
{
|
|
// No-op for this unit test.
|
|
}
|
|
};
|
|
|
|
$version->add(
|
|
42,
|
|
selfserve_config_versioning::STATUS_DRAFT,
|
|
1,
|
|
[
|
|
'questions' => [
|
|
[
|
|
'id' => 1,
|
|
'question' => "Driver's side check",
|
|
],
|
|
],
|
|
'conditions' => [],
|
|
'rules' => [],
|
|
'tasks' => [],
|
|
],
|
|
[
|
|
'valid' => true,
|
|
'errors' => [],
|
|
'warnings' => [],
|
|
],
|
|
);
|
|
|
|
$configJson = $version->capturedPayload['config_json'] ?? null;
|
|
$validationJson = $version->capturedPayload['validation_result_json'] ?? null;
|
|
|
|
expect($configJson)->toBeString();
|
|
expect($validationJson)->toBeString();
|
|
expect(json_decode($configJson, true)['questions'][0]['question'] ?? null)->toBe("Driver's side check");
|
|
expect(json_decode($validationJson, true)['valid'] ?? null)->toBeTrue();
|
|
});
|