Files
Gnuboard7/tests/Feature/Console/HooksCacheCommandTest.php
HeuJung d6e1f98a4f fix(core): 확장 오토로드 캐시 공집합 덮어쓰기로 인한 전역 500 + 소스맵 노출 차단 복원
오토로드 캐시(psr4)가 공집합으로 덮어써져 모든 모듈/플러그인 컨트롤러가
해석 불가능해지고 확장 화면 전체가 500 이 됐다. 이 매핑은 "DB 설치 목록 ×
디스크 활성 디렉토리" 교집합이라 DB 쪽만 일시적으로 비어도(테이블 부재·
설치 중간 실패·testing DB) 정상 매핑이 통째로 소실된다. 코어 라우트는 200
인데 확장 라우트만 전부 500 인 비대칭이 이 결함의 진단 단서다.

- 디스크에 확장 디렉토리가 남아 있는데 매핑이 0건이면 기록하지 않고 보존.
 정상적인 0건(신규 설치·마지막 확장 삭제)은 디스크도 함께 비어 통과한다
- generateAutoloadFile 에 testing 실 캐시 경로 쓰기 차단. updateComposerAutoload
 에만 있던 가드를 생성기 자체로 옮겨 어느 경로로 들어와도 막히게 했다
- extension:update-autoload 가 훅 캐시 재생성 전에 런타임 오토로드를 재등록.
 미재등록 시 깨진 매핑으로 훅 캐시가 modules/plugins 0건으로 생성되어
 확장 훅이 조용히 사라지고, 복구에 커맨드 2회 실행이 필요했다

소스맵 서빙은 에서 차단했으나 이번 브랜치에서 화이트리스트에 재추가되어
운영에서 원본 코드 전문(sourcesContent)이 노출될 수 있었다. 이 화이트리스트가
확장 에셋 서빙의 유일한 방어선이다. dev 빌드의 sourceMappingURL 404 를 피하면서
운영을 막도록 local 환경에서만 허용하도록 분기하고, 산출물을 production 으로
재빌드해 docroot 에 남아 있던 소스맵을 제거했다. --production 빌드는 소스맵을
생성하지 않을 뿐 기존 파일을 지우지 않는다.

CoreUpdateCommandSpawnFailureTest 는 in-process fallback 이 실제 업그레이드
스텝을 돌려 코어 역할·권한·메뉴를 커밋하는데 RefreshDatabase 가 없어, 이후
모든 테스트 클래스가 중복 키로 깨졌다(단독 실행 시 미재현).
2026-07-23 13:04:59 +09:00

135 lines
5.5 KiB
PHP

<?php
namespace Tests\Feature\Console;
use App\Extension\ExtensionManager;
use App\Extension\HookCacheManager;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\File;
use Mockery;
use Tests\TestCase;
/**
* hooks:cache / hooks:clear Artisan 커맨드 Feature 테스트.
*
* 정적 훅 매핑 캐시 생성/삭제 명령이 캐시 파일을 실제로 생성·삭제하는지 검증한다.
*/
class HooksCacheCommandTest extends TestCase
{
use RefreshDatabase;
private string $tmpCachePath;
protected function setUp(): void
{
parent::setUp();
$this->tmpCachePath = storage_path('framework/testing/hooks_cmd_'.uniqid().'.php');
// 실제 bootstrap/cache/hooks.php 를 건드리지 않도록 임시 경로 매니저를 컨테이너에 바인딩
$this->app->instance(HookCacheManager::class, new class($this->tmpCachePath) extends HookCacheManager
{
public function __construct(string $path)
{
$this->cacheFilePath = $path;
}
});
}
protected function tearDown(): void
{
if (File::exists($this->tmpCachePath)) {
File::delete($this->tmpCachePath);
}
parent::tearDown();
}
/**
* @scenario cache_state=absent, listener_type=action_sync, regeneration_trigger=hooks_cache_command, registration_source=module
*
* @effects hooks_cache_command_generates_cache_file, removed_core_listener_dropped_from_cache_after_regeneration
*/
public function test_hooks_cache_command_generates_cache_file(): void
{
$this->assertFalse(File::exists($this->tmpCachePath));
$this->artisan('hooks:cache')
->assertSuccessful();
$this->assertTrue(File::exists($this->tmpCachePath), 'hooks:cache 는 캐시 파일을 생성해야 합니다');
$cache = require $this->tmpCachePath;
$this->assertArrayHasKey('core', $cache);
$this->assertArrayHasKey('modules', $cache);
$this->assertArrayHasKey('plugins', $cache);
$this->assertGreaterThan(0, count($cache['core']));
}
/**
* @scenario cache_state=present_valid, listener_type=action_queued, regeneration_trigger=hooks_clear_command, registration_source=plugin
*
* @effects hooks_clear_command_removes_cache_file
*/
public function test_hooks_clear_command_removes_cache_file(): void
{
// 먼저 생성
$this->artisan('hooks:cache')->assertSuccessful();
$this->assertTrue(File::exists($this->tmpCachePath));
// 삭제
$this->artisan('hooks:clear')->assertSuccessful();
$this->assertFalse(File::exists($this->tmpCachePath), 'hooks:clear 는 캐시 파일을 삭제해야 합니다');
}
/**
* @scenario cache_state=present_but_testing_env, listener_type=action_queued, regeneration_trigger=hooks_cache_command, registration_source=module
*
* @effects hooks_clear_is_safe_when_cache_absent
*/
public function test_hooks_clear_command_is_safe_when_cache_absent(): void
{
$this->assertFalse(File::exists($this->tmpCachePath));
// 캐시 부재 상태에서도 성공 종료 (route:clear 동형)
$this->artisan('hooks:clear')->assertSuccessful();
}
/**
* extension:update-autoload 가 오토로드 캐시와 함께 정적 훅 매핑 캐시를 재생성하는지 검증합니다.
*
* 코어 업데이트(CoreUpdateService::clearAllCaches → extension:update-autoload)의
* 훅 캐시 재생성 choke point. 코어 리스너 추가/변경/삭제가 이 경로로 캐시에 반영된다.
*
* 격리: ExtensionManager 의 generateAutoloadFile() / reregisterRuntimeAutoload() 은 mock 으로 no-op 처리한다.
* 실제 커맨드를 그대로 실행하면 testing DB(활성 확장 0건) 기준으로 실 경로
* `bootstrap/cache/autoload-extensions.php` 를 빈 배열로 덮어써 dev 환경을 오염시킨다
* (feedback_test_must_protect_dev_directories). 훅 캐시는 setUp 의 임시 경로 매니저로 격리.
*
* @scenario cache_state=corrupted_broken_structure, listener_type=action_queued, regeneration_trigger=core_update, registration_source=plugin
*
* @effects core_update_regenerates_hook_cache_via_clearAllCaches_extension_update_autoload, extension_update_regenerates_hook_cache_via_updateComposerAutoload
*/
public function test_extension_update_autoload_regenerates_hook_cache(): void
{
// 실 오토로드 캐시 파일과 현재 프로세스의 ClassLoader 를 건드리지 않도록
// ExtensionManager 의 부수효과 메서드를 no-op 으로 대체 (생성자 미실행 partial mock).
$extensionManager = Mockery::mock(ExtensionManager::class)->makePartial();
$extensionManager->shouldReceive('generateAutoloadFile')->once();
$extensionManager->shouldReceive('reregisterRuntimeAutoload')->once();
$this->app->instance(ExtensionManager::class, $extensionManager);
$this->assertFalse(File::exists($this->tmpCachePath));
$this->artisan('extension:update-autoload')->assertSuccessful();
$this->assertTrue(
File::exists($this->tmpCachePath),
'extension:update-autoload 는 훅 매핑 캐시를 함께 재생성해야 합니다 (코어 업데이트 choke point)'
);
$cache = require $this->tmpCachePath;
$this->assertArrayHasKey('core', $cache);
$this->assertGreaterThan(0, count($cache['core']));
}
}