Files
compras/tests/Unit/SgfWorkflowInventoryTest.php
T
nala c9bd6db90b
tests / ci (push) Canceled after 0s
commit compras2 3011
2026-11-30 23:04:25 +02:00

75 lines
4.2 KiB
PHP

<?php
require_once dirname(__DIR__, 2).'/scripts/lib/SgfWorkflowInventory.php';
beforeEach(function () {
$this->inventoryRoot = sys_get_temp_dir().'/sgf-inventory-'.bin2hex(random_bytes(8));
mkdir($this->inventoryRoot.'/source/app', 0700, true);
mkdir($this->inventoryRoot.'/bundle/app', 0700, true);
file_put_contents($this->inventoryRoot.'/source/app/Workflow.php', '<?php // current');
file_put_contents($this->inventoryRoot.'/bundle/app/Workflow.php', '<?php // embedded');
$this->manifest = ['files' => ['app/Workflow.php' => hash_file('sha256', $this->inventoryRoot.'/bundle/app/Workflow.php')],
'originals' => ['app/Workflow.php' => hash_file('sha256', $this->inventoryRoot.'/source/app/Workflow.php')]];
$this->inspect = function (?array $manifest = null, array $paths = ['app/Workflow.php']) {
file_put_contents($this->inventoryRoot.'/bundle/manifest.json', json_encode($manifest ?? $this->manifest));
return SgfWorkflowInventory::inspect($this->inventoryRoot.'/source', $this->inventoryRoot.'/bundle',
hash_file('sha256', $this->inventoryRoot.'/bundle/manifest.json'), ['process' => $paths]);
};
});
afterEach(function () {
$iterator = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($this->inventoryRoot, FilesystemIterator::SKIP_DOTS), RecursiveIteratorIterator::CHILD_FIRST);
foreach ($iterator as $file) {
$file->isDir() && ! $file->isLink() ? rmdir($file->getPathname()) : unlink($file->getPathname());
}
rmdir($this->inventoryRoot);
});
test('recognises preserved source without claiming functional acceptance', function () {
$result = ($this->inspect)();
expect($result['processes']['process']['source_equivalent'])->toBeTrue()
->and($result['functional_acceptance'])->toBeFalse()->and($result['read_only'])->toBeTrue();
});
test('reports source drift rather than treating old acceptance as current', function () {
file_put_contents($this->inventoryRoot.'/source/app/Workflow.php', '<?php // changed');
$result = ($this->inspect)();
expect($result['processes']['process']['source_equivalent'])->toBeFalse()
->and($result['processes']['process']['components']['app/Workflow.php']['status'])->toBe('source_changed');
});
test('distinguishes missing source from untransferred source', function () {
file_put_contents($this->inventoryRoot.'/source/app/New.php', '<?php // new');
$result = ($this->inspect)(null, ['app/New.php', 'app/Missing.php']);
expect($result['processes']['process']['components']['app/New.php']['status'])->toBe('not_transferred')
->and($result['processes']['process']['components']['app/Missing.php']['status'])->toBe('source_missing');
});
test('refuses tampering anywhere in the distributed bundle', function () {
file_put_contents($this->inventoryRoot.'/bundle/app/Workflow.php', '<?php // tampered');
($this->inspect)();
})->throws(RuntimeException::class, 'Workflow bundle integrity failure.');
test('refuses unsafe manifest paths', function (string $path) {
($this->inspect)(['files' => [$path => str_repeat('a', 64)], 'originals' => []]);
})->with(['../outside.php', '/absolute.php', 'C:/outside.php', 'app/../outside.php', 'app\\outside.php'])
->throws(RuntimeException::class, 'Invalid workflow path.');
test('refuses an unpinned manifest', function () {
file_put_contents($this->inventoryRoot.'/bundle/manifest.json', json_encode($this->manifest));
SgfWorkflowInventory::inspect($this->inventoryRoot.'/source', $this->inventoryRoot.'/bundle', str_repeat('0', 64), []);
})->throws(RuntimeException::class, 'Unrecognised workflow manifest.');
test('does not accept an original hash without its distributed file', function () {
$manifest = $this->manifest;
$manifest['files'] = [];
$result = ($this->inspect)($manifest);
expect($result['processes']['process']['source_equivalent'])->toBeFalse()
->and($result['processes']['process']['components']['app/Workflow.php']['status'])->toBe('not_transferred');
});
test('requires both source and distributed inventories', function () {
($this->inspect)(['files' => $this->manifest['files']]);
})->throws(RuntimeException::class, 'Original and distributed hashes required.');