1
0
Files
breadcrumb-the-shire/tests/Service/Settings/SettingCacheServiceTest.php

81 lines
2.7 KiB
PHP
Raw Normal View History

2026-02-23 12:58:19 +01:00
<?php
namespace MintyPHP\Tests\Service\Settings;
use MintyPHP\Service\Settings\SettingCacheService;
use MintyPHP\Service\Settings\SettingKeys;
use MintyPHP\Service\Settings\SettingsMetadataGateway;
2026-02-23 12:58:19 +01:00
use PHPUnit\Framework\TestCase;
class SettingCacheServiceTest extends TestCase
{
public function testGetReturnsNullForMissingKey(): void
{
$file = $this->tempFilePath();
file_put_contents($file, "<?php\nreturn ['app_title' => 'Demo'];\n");
$service = new SettingCacheService($file);
$this->assertNull($service->get('missing'));
}
public function testUpdateWritesAndNormalizesValues(): void
{
$file = $this->tempFilePath();
file_put_contents($file, "<?php\nreturn ['app_title' => 'Old'];\n");
$service = new SettingCacheService($file);
$this->assertTrue($service->update([
'app_title' => 'New',
'app_locale' => null,
]));
$this->assertSame('New', $service->get('app_title'));
$this->assertNull($service->get('app_locale'));
}
public function testDefaultFilePathUsesStorageRuntimeSettingsFile(): void
{
$service = new SettingCacheService();
$this->assertStringEndsWith('/storage/runtime/settings.php', $service->filePath());
}
public function testColdStartHydratesHotPathKeysFromDatabaseAndWritesCacheFile(): void
{
$file = $this->tempFilePath();
$metadataGateway = $this->createMock(SettingsMetadataGateway::class);
refactor(settings): remove global appearance settings — tenant is sole source Removes the global app_theme, app_theme_user and app_primary_color settings from the admin/settings area and the underlying service/cache/API/i18n/docs layers. The tenant columns (tenants.primary_color, default_theme, allow_user_theme) become the single source of truth for appearance. Branding helpers are tenant-only and fall back to a hardcoded 'light' theme with no brand color when no tenant context is available (login, error, pre-session pages). The APP_THEME env var is removed. Scope: - UI: Appearance tab gone from /admin/settings; tenant edit form drops the global-fallback color preview. - Service: SettingsAppGateway no longer exposes setting-backed accessors for theme/user-theme/primary-color. Theme catalog utilities (listThemes, isAllowedTheme, normalizeTheme, resolveDefaultTheme) stay and are used across Tenant / Auth / User flows; resolveDefaultTheme now hardcodes 'light' with a catalog fallback (no global/env lookup). - AdminSettingsService: buildPageData, sanitization, audit snapshot, apply step and cache payload are all stripped of the three keys. Dead helper applyAppPrimaryColor + normalizePrimaryColor removed. - Cache: SettingCacheService HOT_PATH_KEYS drops the three keys. - API: /settings (GET/PUT) and /settings/public (GET) no longer expose appearance fields; OpenAPI schemas pruned accordingly. - Audit redaction list shortened. - DB: db/init/init.sql seed rows removed. No migration for existing installs — legacy rows stay inert. - i18n + docs: setting.app_theme, setting.app_theme_user, setting.app_primary_color removed from de+en locales; reference and explanation docs updated. - Tests: covering tests for the removed methods pruned; ThemeResolutionTest rewritten for tenant-only semantics. One unrelated PHP-CS-Fixer issue in UserRegistrar.php cleaned up to keep QG-006 green. Workflow: .agents/runs/SETTINGS-APPEARANCE-TENANT-ONLY/ (gitignored). Gates: QG-001..003, QG-006, QG-008 all pass (1985 tests, 28764 assertions). Reviews: code, security, acceptance all pass. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-04-22 22:40:15 +02:00
$metadataGateway->expects($this->exactly(3))
->method('getValue')
->willReturnCallback(static function (string $key): ?string {
return match ($key) {
SettingKeys::APP_TITLE_KEY => 'Demo App',
SettingKeys::APP_LOCALE_KEY => 'de',
SettingKeys::APP_REGISTRATION_KEY => '0',
default => null,
};
});
$service = new SettingCacheService($file, $metadataGateway);
$this->assertSame('Demo App', $service->get('app_title'));
$this->assertFileExists($file);
/** @var array<string, string> $cache */
$cache = include $file;
$this->assertSame('de', $cache[SettingKeys::APP_LOCALE_KEY] ?? null);
refactor(settings): remove global appearance settings — tenant is sole source Removes the global app_theme, app_theme_user and app_primary_color settings from the admin/settings area and the underlying service/cache/API/i18n/docs layers. The tenant columns (tenants.primary_color, default_theme, allow_user_theme) become the single source of truth for appearance. Branding helpers are tenant-only and fall back to a hardcoded 'light' theme with no brand color when no tenant context is available (login, error, pre-session pages). The APP_THEME env var is removed. Scope: - UI: Appearance tab gone from /admin/settings; tenant edit form drops the global-fallback color preview. - Service: SettingsAppGateway no longer exposes setting-backed accessors for theme/user-theme/primary-color. Theme catalog utilities (listThemes, isAllowedTheme, normalizeTheme, resolveDefaultTheme) stay and are used across Tenant / Auth / User flows; resolveDefaultTheme now hardcodes 'light' with a catalog fallback (no global/env lookup). - AdminSettingsService: buildPageData, sanitization, audit snapshot, apply step and cache payload are all stripped of the three keys. Dead helper applyAppPrimaryColor + normalizePrimaryColor removed. - Cache: SettingCacheService HOT_PATH_KEYS drops the three keys. - API: /settings (GET/PUT) and /settings/public (GET) no longer expose appearance fields; OpenAPI schemas pruned accordingly. - Audit redaction list shortened. - DB: db/init/init.sql seed rows removed. No migration for existing installs — legacy rows stay inert. - i18n + docs: setting.app_theme, setting.app_theme_user, setting.app_primary_color removed from de+en locales; reference and explanation docs updated. - Tests: covering tests for the removed methods pruned; ThemeResolutionTest rewritten for tenant-only semantics. One unrelated PHP-CS-Fixer issue in UserRegistrar.php cleaned up to keep QG-006 green. Workflow: .agents/runs/SETTINGS-APPEARANCE-TENANT-ONLY/ (gitignored). Gates: QG-001..003, QG-006, QG-008 all pass (1985 tests, 28764 assertions). Reviews: code, security, acceptance all pass. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-04-22 22:40:15 +02:00
$this->assertSame('0', $cache[SettingKeys::APP_REGISTRATION_KEY] ?? null);
}
2026-02-23 12:58:19 +01:00
private function tempFilePath(): string
{
$dir = sys_get_temp_dir() . '/mintyphp_settings_cache_test_' . bin2hex(random_bytes(6));
mkdir($dir, 0775, true);
$file = $dir . '/settings.php';
$this->addToAssertionCount(1);
$this->assertDirectoryExists($dir);
return $file;
}
}