Files
XC_VM/tests/Unit/SettingsRepositoryTest.php
T
Divarion_D 6964e99f35 test(core): cover the DB layer — ModuleMigrator, SettingsRepository, AuthRepository
Fifth coverage batch, all driven against the in-memory SQLite TestDb:

- ModuleMigratorTest: install runs master database.sql (or falls back to
  replaying deltas <= target when there is none); up() applies only the
  (from, to] range ascending; uninstall runs database_drop.sql or no-ops; has()
  and discover() semver rules (non-semver files ignored, comments stripped);
  and failure propagation from a bad statement.
- SettingsRepositoryTest: getAll() JSON/CSV field normalisation (allow_countries,
  allowed_stb_types lowercase/trim/blank-drop, bouquet_name spaces, api_ips,
  shared_mount_prefixes incl. the legacy-CSV self-heal) and empty-list collapse.
  Points CACHE_TMP_PATH at a temp dir for the trailing FileCache write.
- AuthRepositoryTest: access-code and HMAC-key reads (getAllCodes with/without
  type filter, getCodeById, getCurrentCode via XC_CODE, getAllHMAC, getHMACById)
  and deleteHMAC existing/missing. getGroupPermissions (JSON_CONTAINS) and
  deleteCode (nginx config regen) are left for integration.

+20 tests. Suite: 678 tests, 0 errors.
2026-09-13 14:51:45 +03:00

94 lines
3.2 KiB
PHP

<?php
use XcVm\Core\Config\SettingsRepository;
use PHPUnit\Framework\TestCase;
/**
* SettingsRepository::getAll — loads the single settings row and normalises its
* JSON/CSV fields. Driven with force = true (bypassing the read cache) against a
* SQLite settings table; CACHE_TMP_PATH is pointed at a temp dir so the trailing
* FileCache write has somewhere to go.
*/
final class SettingsRepositoryTest extends TestCase {
private TestDb $db;
protected function setUp(): void {
if (!defined('CACHE_TMP_PATH')) {
$dir = sys_get_temp_dir() . '/xcvm_settings_cache_test/';
@mkdir($dir, 0755, true);
define('CACHE_TMP_PATH', $dir);
}
@mkdir(CACHE_TMP_PATH, 0755, true);
$this->db = new TestDb();
$this->db->exec('CREATE TABLE settings (id INTEGER PRIMARY KEY, allow_countries TEXT, allowed_stb_types TEXT, stalker_lock_images TEXT, bouquet_name TEXT, api_ips TEXT, shared_mount_prefixes TEXT);');
$GLOBALS['db'] = $this->db;
}
protected function tearDown(): void {
unset($GLOBALS['db']);
foreach (glob(rtrim(CACHE_TMP_PATH, '/') . '/*') ?: [] as $f) {
if (is_file($f)) {
unlink($f);
}
}
}
private function seed(array $row): void {
// Production settings columns are NOT NULL DEFAULT '', so unseeded fields
// default to '' here (not null) to mirror the real row shape.
$this->db->query(
'INSERT INTO settings (id, allow_countries, allowed_stb_types, stalker_lock_images, bouquet_name, api_ips, shared_mount_prefixes) VALUES (1, ?, ?, ?, ?, ?, ?);',
$row['allow_countries'] ?? '',
$row['allowed_stb_types'] ?? '',
$row['stalker_lock_images'] ?? '',
$row['bouquet_name'] ?? '',
$row['api_ips'] ?? '',
$row['shared_mount_prefixes'] ?? ''
);
}
public function testDecodesJsonAndCsvFields(): void {
$this->seed([
'allow_countries' => '["US","GB"]',
'allowed_stb_types' => '["MAG250"," ",""]',
'stalker_lock_images' => 'null',
'bouquet_name' => 'My Bouquet',
'api_ips' => '1.2.3.4,5.6.7.8',
'shared_mount_prefixes' => '["/mnt/a"," /mnt/b "]',
]);
$out = SettingsRepository::getAll(true);
$this->assertSame(['US', 'GB'], $out['allow_countries']);
$this->assertSame(['mag250'], $out['allowed_stb_types'], 'lowercased, trimmed, blanks dropped');
$this->assertNull($out['stalker_lock_images']);
$this->assertSame('My_Bouquet', $out['bouquet_name'], 'spaces become underscores');
$this->assertSame(['1.2.3.4', '5.6.7.8'], $out['api_ips']);
$this->assertSame(['/mnt/a', '/mnt/b'], $out['shared_mount_prefixes']);
}
public function testEmptyListFieldsCollapseToEmptyArrays(): void {
$this->seed([
'allowed_stb_types' => '[""]',
'api_ips' => '',
'shared_mount_prefixes' => '',
]);
$out = SettingsRepository::getAll(true);
$this->assertSame([], $out['allowed_stb_types'], 'an "empty" multiselect ([""]) collapses');
$this->assertSame([], $out['api_ips']);
$this->assertSame([], $out['shared_mount_prefixes']);
}
public function testSharedMountPrefixesFallsBackToLegacyCsv(): void {
$this->seed(['shared_mount_prefixes' => 'legacy1, legacy2']);
$out = SettingsRepository::getAll(true);
$this->assertSame(['legacy1', 'legacy2'], $out['shared_mount_prefixes'], 'non-JSON CSV self-heals');
}
}