2026-03-25 16:39:28 +01:00
|
|
|
<?php
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
namespace rabe\Util\tests;
|
|
|
|
|
|
|
|
|
|
use PHPUnit\Framework\TestCase;
|
|
|
|
|
|
|
|
|
|
class CfgTest extends TestCase
|
|
|
|
|
{
|
|
|
|
|
private string $tmpDir;
|
|
|
|
|
|
|
|
|
|
protected function setUp(): void
|
|
|
|
|
{
|
|
|
|
|
$this->tmpDir = rtrim(sys_get_temp_dir(), '/').'/util-settings-cfg-'.bin2hex(random_bytes(8));
|
|
|
|
|
mkdir($this->tmpDir, 0775, true);
|
|
|
|
|
mkdir($this->tmpDir.'/config', 0775, true);
|
|
|
|
|
|
|
|
|
|
file_put_contents(
|
2026-03-26 11:28:58 +01:00
|
|
|
$this->tmpDir.'/config/Extension.default.conf.php',
|
|
|
|
|
"<?php return [\n\t'mode' => 'prod',\n\t'module' => [\n\t\t'code' => '',\n\t\t'label' => '',\n\t\t'flags' => [\n\t\t\t'enabled' => false,\n\t\t],\n\t],\n\t'feature' => [\n\t\t'endpoint' => '',\n\t],\n];\n"
|
2026-03-25 16:39:28 +01:00
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected function tearDown(): void
|
|
|
|
|
{
|
|
|
|
|
$this->removeDir($this->tmpDir);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function testWriteWithoutDirectoryNameUsesLocalConfigFile(): void
|
|
|
|
|
{
|
|
|
|
|
$result = $this->runCfg([
|
2026-03-26 11:28:58 +01:00
|
|
|
'-a',
|
|
|
|
|
$this->tmpDir,
|
|
|
|
|
'write',
|
|
|
|
|
'Extension',
|
|
|
|
|
'module:code="X100"',
|
|
|
|
|
]);
|
2026-03-25 16:39:28 +01:00
|
|
|
|
|
|
|
|
$this->assertSame(0, $result['code'], $result['output']);
|
|
|
|
|
|
2026-03-26 11:28:58 +01:00
|
|
|
$localFile = $this->tmpDir.'/config/Extension.conf.php';
|
2026-03-25 16:39:28 +01:00
|
|
|
$this->assertFileExists($localFile);
|
|
|
|
|
|
|
|
|
|
$cfg = require $localFile;
|
2026-03-26 11:28:58 +01:00
|
|
|
$this->assertSame('X100', $cfg['module']['code']);
|
2026-03-25 16:39:28 +01:00
|
|
|
}
|
|
|
|
|
|
2026-03-26 09:31:07 +01:00
|
|
|
public function testWriteWithoutModeFallsBackToDefaultMode(): void
|
|
|
|
|
{
|
|
|
|
|
file_put_contents(
|
2026-03-26 11:28:58 +01:00
|
|
|
$this->tmpDir.'/config/Extension.default.conf.php',
|
|
|
|
|
"<?php return [\n\t'module' => [\n\t\t'code' => '',\n\t],\n];\n"
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
$result = $this->runCfg([
|
|
|
|
|
'-a',
|
|
|
|
|
$this->tmpDir,
|
|
|
|
|
'write',
|
|
|
|
|
'Extension',
|
|
|
|
|
'module:code="X100"',
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
$this->assertSame(0, $result['code'], $result['output']);
|
|
|
|
|
$this->assertFileExists($this->tmpDir.'/config/Extension.conf.php');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function testWriteWithInputFileWritesMultipleSettings(): void
|
|
|
|
|
{
|
|
|
|
|
$inFile = $this->tmpDir.'/extension-in.json';
|
|
|
|
|
file_put_contents(
|
|
|
|
|
$inFile,
|
|
|
|
|
json_encode([
|
|
|
|
|
'module' => [
|
|
|
|
|
'code' => 'X100',
|
|
|
|
|
'label' => 'demo-module',
|
|
|
|
|
],
|
|
|
|
|
'feature' => [
|
|
|
|
|
'endpoint' => 'https://example.invalid/v1/resource',
|
|
|
|
|
],
|
|
|
|
|
], JSON_PRETTY_PRINT)
|
2026-03-26 09:31:07 +01:00
|
|
|
);
|
|
|
|
|
|
|
|
|
|
$result = $this->runCfg([
|
|
|
|
|
'-a',
|
|
|
|
|
$this->tmpDir,
|
|
|
|
|
'write',
|
2026-03-26 11:28:58 +01:00
|
|
|
'Extension',
|
|
|
|
|
'--siteDir=owner_xyz',
|
|
|
|
|
'-i',
|
|
|
|
|
$inFile,
|
2026-03-26 09:31:07 +01:00
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
$this->assertSame(0, $result['code'], $result['output']);
|
2026-03-26 11:28:58 +01:00
|
|
|
$cfg = require $this->tmpDir.'/config/owner_xyz/Extension.conf.php';
|
|
|
|
|
$this->assertSame('X100', $cfg['module']['code']);
|
|
|
|
|
$this->assertSame('demo-module', $cfg['module']['label']);
|
|
|
|
|
$this->assertSame('https://example.invalid/v1/resource', $cfg['feature']['endpoint']);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function testWriteAcceptsJsonStringValueInSetting(): void
|
|
|
|
|
{
|
|
|
|
|
$result = $this->runCfg([
|
|
|
|
|
'-a',
|
|
|
|
|
$this->tmpDir,
|
|
|
|
|
'write',
|
|
|
|
|
'Extension',
|
|
|
|
|
'module={"code":"X100","label":"demo-module"}',
|
|
|
|
|
'--siteDir=owner_xyz',
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
$this->assertSame(0, $result['code'], $result['output']);
|
|
|
|
|
$cfg = require $this->tmpDir.'/config/owner_xyz/Extension.conf.php';
|
|
|
|
|
$this->assertSame('X100', $cfg['module']['code']);
|
|
|
|
|
$this->assertSame('demo-module', $cfg['module']['label']);
|
2026-03-26 09:31:07 +01:00
|
|
|
}
|
|
|
|
|
|
2026-03-26 11:28:58 +01:00
|
|
|
public function testWriteSupportsNestedPathWithColonNotation(): void
|
2026-03-25 16:39:28 +01:00
|
|
|
{
|
|
|
|
|
$result = $this->runCfg([
|
|
|
|
|
'-a',
|
|
|
|
|
$this->tmpDir,
|
|
|
|
|
'write',
|
2026-03-26 11:28:58 +01:00
|
|
|
'Extension',
|
|
|
|
|
'module:flags:enabled=true',
|
|
|
|
|
'--siteDir=owner_xyz',
|
2026-03-25 16:39:28 +01:00
|
|
|
]);
|
|
|
|
|
|
2026-03-26 11:28:58 +01:00
|
|
|
$this->assertSame(0, $result['code'], $result['output']);
|
|
|
|
|
$cfg = require $this->tmpDir.'/config/owner_xyz/Extension.conf.php';
|
|
|
|
|
$this->assertTrue($cfg['module']['flags']['enabled']);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function testWriteWithInputFileAndSettingReturnsError(): void
|
|
|
|
|
{
|
|
|
|
|
$inFile = $this->tmpDir.'/extension-in.json';
|
|
|
|
|
file_put_contents($inFile, json_encode(['module' => ['code' => 'X100']]));
|
|
|
|
|
|
|
|
|
|
$result = $this->runCfg([
|
|
|
|
|
'-a',
|
|
|
|
|
$this->tmpDir,
|
|
|
|
|
'write',
|
|
|
|
|
'Extension',
|
|
|
|
|
'module:code="x"',
|
|
|
|
|
'-i',
|
|
|
|
|
$inFile,
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
$this->assertSame(1, $result['code']);
|
|
|
|
|
$this->assertStringContainsString('Please use either SETTING or --in, not both.', $result['output']);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function testWriteWithSiteDirCreatesAndWritesSiteConfig(): void
|
|
|
|
|
{
|
|
|
|
|
$result = $this->runCfg([
|
|
|
|
|
'-a',
|
|
|
|
|
$this->tmpDir,
|
|
|
|
|
'write',
|
|
|
|
|
'Extension',
|
|
|
|
|
'module:code="X100"',
|
|
|
|
|
'--siteDir',
|
|
|
|
|
'owner_xyz',
|
|
|
|
|
]);
|
|
|
|
|
|
2026-03-25 16:39:28 +01:00
|
|
|
$this->assertSame(0, $result['code'], $result['output']);
|
|
|
|
|
|
2026-03-26 11:28:58 +01:00
|
|
|
$siteFile = $this->tmpDir.'/config/owner_xyz/Extension.conf.php';
|
2026-03-25 16:39:28 +01:00
|
|
|
$this->assertFileExists($siteFile);
|
2026-03-26 11:28:58 +01:00
|
|
|
$this->assertFileDoesNotExist($this->tmpDir.'/config/Extension.conf.php');
|
2026-03-25 16:39:28 +01:00
|
|
|
|
|
|
|
|
$cfg = require $siteFile;
|
2026-03-26 11:28:58 +01:00
|
|
|
$this->assertSame('X100', $cfg['module']['code']);
|
2026-03-25 16:39:28 +01:00
|
|
|
}
|
|
|
|
|
|
2026-03-26 11:28:58 +01:00
|
|
|
public function testWriteWithSiteDirMergesIntoExistingSiteConfig(): void
|
2026-03-25 16:39:28 +01:00
|
|
|
{
|
|
|
|
|
$firstWrite = $this->runCfg([
|
2026-03-26 11:28:58 +01:00
|
|
|
'-a',
|
|
|
|
|
$this->tmpDir,
|
|
|
|
|
'write',
|
|
|
|
|
'Extension',
|
|
|
|
|
'module:code="X100"',
|
|
|
|
|
'--siteDir',
|
|
|
|
|
'owner_xyz',
|
|
|
|
|
]);
|
2026-03-25 16:39:28 +01:00
|
|
|
$this->assertSame(0, $firstWrite['code'], $firstWrite['output']);
|
|
|
|
|
|
|
|
|
|
$secondWrite = $this->runCfg([
|
2026-03-26 11:28:58 +01:00
|
|
|
'-a',
|
|
|
|
|
$this->tmpDir,
|
|
|
|
|
'write',
|
|
|
|
|
'Extension',
|
|
|
|
|
'module:label="demo-module"',
|
|
|
|
|
'--siteDir',
|
|
|
|
|
'owner_xyz',
|
|
|
|
|
]);
|
2026-03-25 16:39:28 +01:00
|
|
|
$this->assertSame(0, $secondWrite['code'], $secondWrite['output']);
|
|
|
|
|
|
2026-03-26 11:28:58 +01:00
|
|
|
$siteFile = $this->tmpDir.'/config/owner_xyz/Extension.conf.php';
|
2026-03-25 16:39:28 +01:00
|
|
|
$cfg = require $siteFile;
|
|
|
|
|
|
2026-03-26 11:28:58 +01:00
|
|
|
$this->assertSame('X100', $cfg['module']['code']);
|
|
|
|
|
$this->assertSame('demo-module', $cfg['module']['label']);
|
2026-03-25 16:39:28 +01:00
|
|
|
}
|
|
|
|
|
|
2026-03-26 11:28:58 +01:00
|
|
|
public function testWriteWithSiteDirUsingConfigPrefixIsNormalized(): void
|
2026-03-25 16:39:28 +01:00
|
|
|
{
|
|
|
|
|
$result = $this->runCfg([
|
2026-03-26 11:28:58 +01:00
|
|
|
'-a',
|
|
|
|
|
$this->tmpDir,
|
|
|
|
|
'write',
|
|
|
|
|
'Extension',
|
|
|
|
|
'module:code="X100"',
|
|
|
|
|
'--siteDir=config/owner_xyz',
|
|
|
|
|
]);
|
2026-03-25 16:39:28 +01:00
|
|
|
|
|
|
|
|
$this->assertSame(0, $result['code'], $result['output']);
|
2026-03-26 11:28:58 +01:00
|
|
|
$this->assertFileExists($this->tmpDir.'/config/owner_xyz/Extension.conf.php');
|
|
|
|
|
$this->assertFileDoesNotExist($this->tmpDir.'/config/config/owner_xyz/Extension.conf.php');
|
2026-03-25 16:39:28 +01:00
|
|
|
}
|
|
|
|
|
|
2026-03-26 11:28:58 +01:00
|
|
|
public function testWriteWithSiteDirWritesToSiteConfig(): void
|
2026-03-25 16:39:28 +01:00
|
|
|
{
|
|
|
|
|
$result = $this->runCfg([
|
2026-03-26 11:28:58 +01:00
|
|
|
'-a',
|
|
|
|
|
$this->tmpDir,
|
|
|
|
|
'write',
|
|
|
|
|
'Extension',
|
|
|
|
|
'module:code="X100"',
|
|
|
|
|
'--siteDir=owner_xyz',
|
|
|
|
|
]);
|
2026-03-25 16:39:28 +01:00
|
|
|
|
|
|
|
|
$this->assertSame(0, $result['code'], $result['output']);
|
2026-03-26 11:28:58 +01:00
|
|
|
$this->assertFileExists($this->tmpDir.'/config/owner_xyz/Extension.conf.php');
|
2026-03-25 16:39:28 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function testWriteToExistingSiteConfigCreatesBackup(): void
|
|
|
|
|
{
|
|
|
|
|
$firstWrite = $this->runCfg([
|
2026-03-26 11:28:58 +01:00
|
|
|
'-a',
|
|
|
|
|
$this->tmpDir,
|
|
|
|
|
'write',
|
|
|
|
|
'Extension',
|
|
|
|
|
'module:code="first"',
|
|
|
|
|
'--siteDir=owner_xyz',
|
|
|
|
|
]);
|
2026-03-25 16:39:28 +01:00
|
|
|
$this->assertSame(0, $firstWrite['code'], $firstWrite['output']);
|
|
|
|
|
|
|
|
|
|
$secondWrite = $this->runCfg([
|
2026-03-26 11:28:58 +01:00
|
|
|
'-a',
|
|
|
|
|
$this->tmpDir,
|
|
|
|
|
'write',
|
|
|
|
|
'Extension',
|
|
|
|
|
'module:code="second"',
|
|
|
|
|
'--siteDir=owner_xyz',
|
|
|
|
|
]);
|
2026-03-25 16:39:28 +01:00
|
|
|
$this->assertSame(0, $secondWrite['code'], $secondWrite['output']);
|
|
|
|
|
|
2026-03-26 11:28:58 +01:00
|
|
|
$siteFile = $this->tmpDir.'/config/owner_xyz/Extension.conf.php';
|
2026-03-25 16:39:28 +01:00
|
|
|
$backupFile = $siteFile.'.bak';
|
|
|
|
|
$this->assertFileExists($backupFile);
|
|
|
|
|
|
|
|
|
|
$current = require $siteFile;
|
|
|
|
|
$backup = require $backupFile;
|
2026-03-26 11:28:58 +01:00
|
|
|
$this->assertSame('second', $current['module']['code']);
|
|
|
|
|
$this->assertSame('first', $backup['module']['code']);
|
2026-03-25 16:39:28 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function testShowWithSiteReturnsSiteSpecificValue(): void
|
|
|
|
|
{
|
|
|
|
|
$this->runCfg([
|
2026-03-26 11:28:58 +01:00
|
|
|
'-a',
|
|
|
|
|
$this->tmpDir,
|
|
|
|
|
'write',
|
|
|
|
|
'Extension',
|
|
|
|
|
'module:code="X100"',
|
|
|
|
|
'--siteDir=owner_xyz',
|
|
|
|
|
]);
|
2026-03-25 16:39:28 +01:00
|
|
|
|
|
|
|
|
$show = $this->runCfg([
|
2026-03-26 11:28:58 +01:00
|
|
|
'-a',
|
|
|
|
|
$this->tmpDir,
|
|
|
|
|
'show',
|
|
|
|
|
'Extension',
|
|
|
|
|
'module:code',
|
|
|
|
|
'--siteDir=owner_xyz',
|
|
|
|
|
]);
|
2026-03-25 16:39:28 +01:00
|
|
|
|
|
|
|
|
$this->assertSame(0, $show['code'], $show['output']);
|
2026-03-26 11:28:58 +01:00
|
|
|
$this->assertStringContainsString('X100', $show['output']);
|
2026-03-25 16:39:28 +01:00
|
|
|
}
|
|
|
|
|
|
2026-03-26 11:28:58 +01:00
|
|
|
public function testSiteDirRejectsTraversal(): void
|
2026-03-25 16:39:28 +01:00
|
|
|
{
|
|
|
|
|
$result = $this->runCfg([
|
2026-03-26 11:28:58 +01:00
|
|
|
'-a',
|
|
|
|
|
$this->tmpDir,
|
|
|
|
|
'write',
|
|
|
|
|
'Extension',
|
|
|
|
|
'module:code="X100"',
|
|
|
|
|
'--siteDir=../owner_xyz',
|
|
|
|
|
]);
|
2026-03-25 16:39:28 +01:00
|
|
|
|
|
|
|
|
$this->assertSame(1, $result['code']);
|
2026-03-26 11:28:58 +01:00
|
|
|
$this->assertStringContainsString('Invalid directory in --siteDir', $result['output']);
|
2026-03-25 16:39:28 +01:00
|
|
|
}
|
|
|
|
|
|
2026-03-26 11:28:58 +01:00
|
|
|
public function testSiteDirRejectsInvalidSegment(): void
|
2026-03-25 16:39:28 +01:00
|
|
|
{
|
|
|
|
|
$result = $this->runCfg([
|
2026-03-26 11:28:58 +01:00
|
|
|
'-a',
|
|
|
|
|
$this->tmpDir,
|
|
|
|
|
'write',
|
|
|
|
|
'Extension',
|
|
|
|
|
'module:code="X100"',
|
|
|
|
|
'--siteDir=owner xyz',
|
|
|
|
|
]);
|
2026-03-25 16:39:28 +01:00
|
|
|
|
|
|
|
|
$this->assertSame(1, $result['code']);
|
2026-03-26 11:28:58 +01:00
|
|
|
$this->assertStringContainsString("Invalid directory name segment 'owner xyz' in --siteDir.", $result['output']);
|
2026-03-25 16:39:28 +01:00
|
|
|
}
|
|
|
|
|
|
2026-03-26 11:28:58 +01:00
|
|
|
public function testSiteDirRejectsEmptyValue(): void
|
2026-03-25 16:39:28 +01:00
|
|
|
{
|
|
|
|
|
$result = $this->runCfg([
|
2026-03-26 11:28:58 +01:00
|
|
|
'-a',
|
|
|
|
|
$this->tmpDir,
|
|
|
|
|
'write',
|
|
|
|
|
'Extension',
|
|
|
|
|
'module:code="X100"',
|
|
|
|
|
'--siteDir=',
|
|
|
|
|
]);
|
2026-03-25 16:39:28 +01:00
|
|
|
|
|
|
|
|
$this->assertSame(1, $result['code']);
|
2026-03-26 11:28:58 +01:00
|
|
|
$this->assertStringContainsString('Option --siteDir is empty.', $result['output']);
|
2026-03-25 16:39:28 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private function runCfg(array $args): array
|
|
|
|
|
{
|
|
|
|
|
$script = realpath(__DIR__.'/../bin/cfg');
|
|
|
|
|
$this->assertNotFalse($script);
|
|
|
|
|
|
|
|
|
|
$command = escapeshellarg(PHP_BINARY).' '.escapeshellarg($script);
|
|
|
|
|
foreach ($args as $arg) {
|
|
|
|
|
$command .= ' '.escapeshellarg($arg);
|
|
|
|
|
}
|
|
|
|
|
$command .= ' 2>&1';
|
|
|
|
|
|
|
|
|
|
$outputLines = [];
|
|
|
|
|
$exitCode = 0;
|
|
|
|
|
exec($command, $outputLines, $exitCode);
|
|
|
|
|
|
|
|
|
|
return [
|
|
|
|
|
'code' => $exitCode,
|
|
|
|
|
'output' => implode(PHP_EOL, $outputLines),
|
|
|
|
|
];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private function removeDir(string $dir): void
|
|
|
|
|
{
|
|
|
|
|
if (!is_dir($dir)) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
foreach (scandir($dir) ?: [] as $entry) {
|
|
|
|
|
if ($entry === '.' || $entry === '..') {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$path = $dir.'/'.$entry;
|
|
|
|
|
if (is_dir($path)) {
|
|
|
|
|
$this->removeDir($path);
|
|
|
|
|
} else {
|
|
|
|
|
unlink($path);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
rmdir($dir);
|
|
|
|
|
}
|
|
|
|
|
}
|