https://github.com/gnuboard/g7/issues/125 — 상품 상세설명을 HTML 로 저장할 때 HTMLPurifier 가 모듈 vendor 폴더 안에 정의 캐시를 만들려다 실패해 저장이 매번 500 으로 끝나던 문제를 고친다. vendor 를 읽기 전용으로 두는 표준 배포에서 그 쓰기는 예외가 아니라 PHP 경고로 나오고 Laravel 이 이를 ErrorException 으로 승격시킨다. 캐시는 설정 해시당 1회만 기록되므로 캐시가 영영 생기지 않아 재시도해도 같은 결과였다. 캐시 경로를 storage 아래로 옮기고, 그 경로마저 확보하지 못하면 캐시만 끄고 정화는 그대로 수행한다 — 캐시는 성능 장치이고 정화는 보안 장치라, 전자의 실패가 후자를 건너뛰게 만들면 안 된다. 저장은 성공하므로 운영자에게 도달하는 흔적이 로그 하나뿐이라 error 수준으로 남긴다 (출하 기본 로그 수준이 error 라 warning 은 기본 설치 상태에서 파일에 남지 않는다). 그 과정에서 갈라져 있던 두 축을 코어 한 곳으로 모은다. - 쓰기 디렉토리 확보: 억제 생성·chmod·setgid·소유권 상속·쓰기 판정 절차가 정적 게시와 정의 캐시 두 곳에 서로 다른 하드닝으로 복제돼 있었다(억제 mkdir·setgid·clearstatcache 가 사본마다 한쪽씩 빠져 있었다). FilePermissionHelper 의 ensureWritableDirectory 와 hardenDirectory 로 통합하고, 실패 사유는 out 파라미터로 올려 정책(조용한 성능 저하 대 시끄러운 실패)은 호출부가 정하게 둔다. - 확장 저장 경로: storage_path('app/modules/…') 손조립이 30곳에 흩어져 있어 테스트 격리 분기를 넣으려면 사본마다 복제해야 했고, 한 곳만 빠뜨려도 그 확장의 테스트가 운영 설정 파일을 덮어쓴다. 디스크 root 를 단일 출처로 읽는 ExtensionStoragePath 로 전환하고 테스트 분기는 config/filesystems.php 한 줄에서 끝낸다. 함께 고친 것 - 테스트가 운영 라우트 캐시로 부팅해 확장 allowlist 가 라우트 축에서 통째로 무력화되던 문제. 삭제가 아니라 경로를 돌린다 — 라우트 캐시는 확장 작업 전까지 재생성되지 않아, 삭제하면 운영 사이트가 그때까지 라우트 파일 스캔 경로로 떨어진다. - PHPUnit 프로세스가 확장 vendor 의 제3자 composer 패키지를 오토로드하지 않아 그 패키지를 쓰는 코드 경로가 통째로 테스트 불가였던 문제. 확장 자신의 오토로더를 그대로 쓰면 활성 디렉토리가 _bundled 를 이기고 base path 유추까지 깨지므로, 생성된 맵에서 제3자 항목만 골라 별도 로더로 등록한다. - 게시 폴더가 setgid 를 갖지 않아, 명령줄과 웹이 번갈아 만든 하위 폴더를 다른 쪽이 쓰지 못하던 문제. - 관리자 템플릿이 HTML 정화 라이브러리를 직접 지정하지 않아 전이 의존으로 딸려온 구버전이 쓰이던 문제. 동반 산출물 - 규정 표(·AGENTS.md) 6행 + storage-driver/service-repository/testing-guide 문서 - audit 룰 2종 + coverage 6항목. 저장소가 이미 전량 전환돼 전수 실행이 공허 통과하므로 판정식은 픽스처 36건이 잠근다 - INSTALL.md 에 설치 후 파일 권한 절 추가 (vendor 쓰기 권한 불요를 명시)
177 lines
5.9 KiB
PHP
177 lines
5.9 KiB
PHP
<?php
|
|
|
|
namespace Tests\Unit\Extension;
|
|
|
|
use App\Extension\PluginManager;
|
|
use App\Support\ExtensionStoragePath;
|
|
use Illuminate\Support\Facades\File;
|
|
use Illuminate\Support\Facades\Log;
|
|
use Mockery;
|
|
use Tests\Helpers\MocksExtensions;
|
|
use Tests\TestCase;
|
|
|
|
/**
|
|
* PluginManager 설정 초기화 테스트
|
|
*
|
|
* initializePluginSettings()의 defaults.json 우선순위 로직을 테스트합니다.
|
|
*/
|
|
class PluginManagerSettingsTest extends TestCase
|
|
{
|
|
use MocksExtensions;
|
|
|
|
private string $testSettingsDir;
|
|
|
|
private string $testDefaultsDir;
|
|
|
|
protected function setUp(): void
|
|
{
|
|
parent::setUp();
|
|
|
|
$this->testSettingsDir = ExtensionStoragePath::plugin('test-init-plugin', 'settings');
|
|
$this->testDefaultsDir = sys_get_temp_dir().'/g7_test_defaults_'.uniqid();
|
|
|
|
// 테스트용 디렉토리 정리
|
|
if (File::isDirectory($this->testSettingsDir)) {
|
|
File::deleteDirectory($this->testSettingsDir);
|
|
}
|
|
|
|
if (! File::isDirectory($this->testDefaultsDir)) {
|
|
File::makeDirectory($this->testDefaultsDir, 0755, true);
|
|
}
|
|
}
|
|
|
|
protected function tearDown(): void
|
|
{
|
|
// 테스트 후 정리
|
|
if (File::isDirectory($this->testSettingsDir)) {
|
|
File::deleteDirectory($this->testSettingsDir);
|
|
}
|
|
|
|
$parentDir = dirname($this->testSettingsDir);
|
|
if (File::isDirectory($parentDir) && empty(File::directories($parentDir)) && empty(File::files($parentDir))) {
|
|
File::deleteDirectory($parentDir);
|
|
}
|
|
|
|
if (File::isDirectory($this->testDefaultsDir)) {
|
|
File::deleteDirectory($this->testDefaultsDir);
|
|
}
|
|
|
|
Mockery::close();
|
|
parent::tearDown();
|
|
}
|
|
|
|
/**
|
|
* defaults.json이 있으면 defaults 섹션으로 setting.json 초기화
|
|
*/
|
|
public function test_initialize_plugin_settings_uses_defaults_json_when_available(): void
|
|
{
|
|
// defaults.json 생성
|
|
$defaultsPath = $this->testDefaultsDir.'/defaults.json';
|
|
File::put($defaultsPath, json_encode([
|
|
'defaults' => [
|
|
'display_mode' => 'layer',
|
|
'popup_width' => 500,
|
|
],
|
|
'frontend_schema' => [
|
|
'display_mode' => ['expose' => true],
|
|
],
|
|
]));
|
|
|
|
$mockPlugin = $this->createMockPlugin([
|
|
'identifier' => 'test-init-plugin',
|
|
'configValues' => [
|
|
'display_mode' => 'popup',
|
|
'popup_width' => 400,
|
|
],
|
|
'settingsDefaultsPath' => $defaultsPath,
|
|
]);
|
|
|
|
// PluginManager의 protected 메서드 호출을 위해 리플렉션 사용
|
|
$pluginManager = app(PluginManager::class);
|
|
$method = new \ReflectionMethod($pluginManager, 'initializePluginSettings');
|
|
$method->setAccessible(true);
|
|
|
|
$method->invoke($pluginManager, $mockPlugin);
|
|
|
|
// setting.json이 defaults.json의 defaults 섹션 값으로 생성되었는지 확인
|
|
$settingsPath = $this->testSettingsDir.'/setting.json';
|
|
$this->assertFileExists($settingsPath);
|
|
|
|
$savedSettings = json_decode(File::get($settingsPath), true);
|
|
$this->assertSame('layer', $savedSettings['display_mode']);
|
|
$this->assertSame(500, $savedSettings['popup_width']);
|
|
}
|
|
|
|
/**
|
|
* defaults.json이 없으면 getConfigValues()로 폴백 (하위 호환성)
|
|
*/
|
|
public function test_initialize_plugin_settings_falls_back_to_get_config_values(): void
|
|
{
|
|
$mockPlugin = $this->createMockPlugin([
|
|
'identifier' => 'test-init-plugin',
|
|
'configValues' => [
|
|
'api_key' => 'default-key',
|
|
'enabled' => true,
|
|
],
|
|
'settingsDefaultsPath' => null,
|
|
]);
|
|
|
|
$pluginManager = app(PluginManager::class);
|
|
$method = new \ReflectionMethod($pluginManager, 'initializePluginSettings');
|
|
$method->setAccessible(true);
|
|
|
|
$method->invoke($pluginManager, $mockPlugin);
|
|
|
|
// setting.json이 getConfigValues() 값으로 생성되었는지 확인
|
|
$settingsPath = $this->testSettingsDir.'/setting.json';
|
|
$this->assertFileExists($settingsPath);
|
|
|
|
$savedSettings = json_decode(File::get($settingsPath), true);
|
|
$this->assertSame('default-key', $savedSettings['api_key']);
|
|
$this->assertTrue($savedSettings['enabled']);
|
|
}
|
|
|
|
/**
|
|
* 이미 setting.json이 존재하면 덮어쓰기 방지 (재설치 시 기존 설정 유지)
|
|
*/
|
|
public function test_initialize_plugin_settings_skips_when_settings_file_exists(): void
|
|
{
|
|
// 기존 설정 파일 생성
|
|
if (! File::isDirectory($this->testSettingsDir)) {
|
|
File::makeDirectory($this->testSettingsDir, 0755, true);
|
|
}
|
|
|
|
$existingSettings = ['display_mode' => 'custom', 'custom_value' => 'preserve'];
|
|
File::put(
|
|
$this->testSettingsDir.'/setting.json',
|
|
json_encode($existingSettings)
|
|
);
|
|
|
|
$mockPlugin = $this->createMockPlugin([
|
|
'identifier' => 'test-init-plugin',
|
|
'configValues' => [
|
|
'display_mode' => 'new_value',
|
|
],
|
|
'settingsDefaultsPath' => null,
|
|
]);
|
|
|
|
$pluginManager = app(PluginManager::class);
|
|
$method = new \ReflectionMethod($pluginManager, 'initializePluginSettings');
|
|
$method->setAccessible(true);
|
|
|
|
Log::shouldReceive('info')
|
|
->once()
|
|
->withArgs(function ($message, $context) {
|
|
return str_contains($message, '이미 존재');
|
|
});
|
|
|
|
$method->invoke($pluginManager, $mockPlugin);
|
|
|
|
// 기존 설정이 유지되는지 확인
|
|
$settingsPath = $this->testSettingsDir.'/setting.json';
|
|
$savedSettings = json_decode(File::get($settingsPath), true);
|
|
$this->assertSame('custom', $savedSettings['display_mode']);
|
|
$this->assertSame('preserve', $savedSettings['custom_value']);
|
|
}
|
|
}
|