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 가 없어, 이후 모든 테스트 클래스가 중복 키로 깨졌다(단독 실행 시 미재현).
This commit is contained in:
1 parent
3044119b37
commit
d6e1f98a4f
17 files changed
+562
-66
No files matched your search
@@ -0,0 +1,216 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\Unit\Extension;
|
||||
|
||||
use App\Contracts\Repositories\ModuleRepositoryInterface;
|
||||
use App\Contracts\Repositories\PluginRepositoryInterface;
|
||||
use App\Extension\ExtensionManager;
|
||||
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||||
use Illuminate\Support\Facades\File;
|
||||
use Tests\TestCase;
|
||||
|
||||
/**
|
||||
* 확장 오토로드 캐시(bootstrap/cache/autoload-extensions.php) 파괴 방지 가드 테스트.
|
||||
*
|
||||
* 이 캐시는 `Modules\*` / `Plugins\*` PSR-4 매핑의 단일 소스라, 빈 배열로 덮어써지면
|
||||
* 모든 모듈/플러그인 컨트롤러가 "Target class does not exist" 로 사이트 전역 500 이 된다.
|
||||
*
|
||||
* 생성기는 "DB 설치 목록 × 디스크 활성 디렉토리" 교집합이므로, DB 쪽만 일시적으로
|
||||
* 비어도(테이블 부재 · 설치 중간 상태 · testing DB) 교집합이 공집합이 되어 정상 매핑이
|
||||
* 소실된다. 본 테스트는 그 소실을 차단하는 두 가드를 고정한다.
|
||||
*/
|
||||
class ExtensionAutoloadCacheGuardTest extends TestCase
|
||||
{
|
||||
use RefreshDatabase;
|
||||
|
||||
/**
|
||||
* 실 오토로드 캐시 파일 경로
|
||||
*/
|
||||
private string $realCachePath;
|
||||
|
||||
/**
|
||||
* 실 캐시 파일의 테스트 시작 시점 내용 (없으면 null)
|
||||
*/
|
||||
private ?string $realCacheBackup = null;
|
||||
|
||||
/**
|
||||
* 임시 오토로드 캐시 파일 경로
|
||||
*/
|
||||
private string $tmpCachePath;
|
||||
|
||||
protected function setUp(): void
|
||||
{
|
||||
parent::setUp();
|
||||
|
||||
$this->realCachePath = base_path('bootstrap/cache/autoload-extensions.php');
|
||||
$this->tmpCachePath = storage_path('framework/testing/autoload_guard_'.uniqid().'.php');
|
||||
|
||||
File::ensureDirectoryExists(dirname($this->tmpCachePath));
|
||||
|
||||
// 가드가 없는 상태(= 최초 fail 실행)에서도 개발 환경 캐시가 파괴되지 않도록 원본 보존.
|
||||
if (File::exists($this->realCachePath)) {
|
||||
$this->realCacheBackup = File::get($this->realCachePath);
|
||||
}
|
||||
}
|
||||
|
||||
protected function tearDown(): void
|
||||
{
|
||||
if ($this->realCacheBackup !== null) {
|
||||
File::put($this->realCachePath, $this->realCacheBackup);
|
||||
}
|
||||
|
||||
if (File::exists($this->tmpCachePath)) {
|
||||
File::delete($this->tmpCachePath);
|
||||
}
|
||||
|
||||
parent::tearDown();
|
||||
}
|
||||
|
||||
/**
|
||||
* 디스크에 활성 확장 디렉토리가 있는데 DB 조회 결과가 0건이면 빈 매핑을 쓰지 않아야 한다.
|
||||
*
|
||||
* DB 쪽이 신뢰 불가한 상태(테이블 부재 · 설치 중간 · 다른 DB)에서 산출된 공집합을
|
||||
* 그대로 기록하면 정상 매핑이 소실된다. 이 경우 기존 파일을 보존해야 한다.
|
||||
*/
|
||||
public function test_generate_autoload_file_preserves_existing_map_when_db_reports_none_but_directories_exist(): void
|
||||
{
|
||||
$this->assertNotEmpty(
|
||||
$this->installedLookingExtensionDirectories(),
|
||||
'이 테스트는 디스크에 활성 확장 디렉토리가 존재하는 저장소에서만 유효합니다.'
|
||||
);
|
||||
|
||||
// RefreshDatabase 로 modules/plugins 테이블은 비어 있다 (= DB 설치 목록 0건).
|
||||
$manager = $this->managerWritingTo($this->tmpCachePath);
|
||||
|
||||
// 정상 매핑이 이미 존재하는 상태를 재현
|
||||
File::put($this->tmpCachePath, $this->cacheFileWithPsr4([
|
||||
'Modules\\Sirsoft\\Board\\' => 'modules/sirsoft-board/src/',
|
||||
]));
|
||||
|
||||
$manager->generateAutoloadFile();
|
||||
|
||||
$written = require $this->tmpCachePath;
|
||||
|
||||
$this->assertNotEmpty(
|
||||
$written['psr4'],
|
||||
'DB 조회가 0건이어도 디스크에 활성 확장 디렉토리가 있으면 기존 PSR-4 매핑을 빈 배열로 덮어쓰면 안 됩니다. '
|
||||
.'(덮어쓰면 모든 모듈/플러그인 라우트가 500 — Target class does not exist)'
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* 테스트 환경에서는 실 bootstrap/cache 캐시 파일을 절대 건드리지 않아야 한다.
|
||||
*
|
||||
* testing DB 는 활성 확장 구성이 개발 DB 와 다르므로, 어떤 경로로든 실 캐시가
|
||||
* 재생성되면 개발 환경 매핑이 testing 기준으로 교체되어 사이트가 깨진다.
|
||||
*/
|
||||
public function test_generate_autoload_file_does_not_touch_real_cache_path_in_testing_env(): void
|
||||
{
|
||||
$this->assertNotNull(
|
||||
$this->realCacheBackup,
|
||||
'이 테스트는 실 오토로드 캐시가 생성된 환경에서만 유효합니다 (extension:update-autoload 선행 필요).'
|
||||
);
|
||||
|
||||
app(ExtensionManager::class)->generateAutoloadFile();
|
||||
|
||||
$this->assertSame(
|
||||
$this->realCacheBackup,
|
||||
File::get($this->realCachePath),
|
||||
'testing 환경에서는 실 bootstrap/cache/autoload-extensions.php 를 재생성하면 안 됩니다.'
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* extension:update-autoload 는 훅 캐시 재생성 전에 런타임 오토로드를 재등록해야 한다.
|
||||
*
|
||||
* 미재등록 시 현재 프로세스의 ClassLoader 는 (방금 교체된) 이전 매핑을 들고 있어
|
||||
* ModuleManager/PluginManager 가 확장 클래스를 로드하지 못하고, 훅 캐시가
|
||||
* modules/plugins 0건으로 생성된다 — 모든 확장 훅이 조용히 사라진다.
|
||||
*/
|
||||
public function test_update_autoload_command_reregisters_runtime_autoload_before_hook_cache(): void
|
||||
{
|
||||
$body = File::get(app_path('Console/Commands/Extension/UpdateAutoloadCommand.php'));
|
||||
|
||||
$reregisterPos = strpos($body, 'reregisterRuntimeAutoload(');
|
||||
$hookCachePos = strpos($body, '->generate($moduleManager, $pluginManager)');
|
||||
|
||||
$this->assertNotFalse(
|
||||
$reregisterPos,
|
||||
'extension:update-autoload 는 런타임 오토로드 재등록을 호출해야 합니다.'
|
||||
);
|
||||
$this->assertNotFalse($hookCachePos, '훅 캐시 재생성 호출을 찾을 수 없습니다.');
|
||||
|
||||
$this->assertLessThan(
|
||||
$hookCachePos,
|
||||
$reregisterPos,
|
||||
'런타임 오토로드 재등록은 훅 캐시 재생성보다 먼저 수행되어야 합니다 '
|
||||
.'(순서가 뒤바뀌면 훅 캐시가 modules/plugins 0건으로 생성됨).'
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* 지정 경로에 기록하는 ExtensionManager 인스턴스를 생성합니다.
|
||||
*
|
||||
* @param string $path 오토로드 캐시 파일 경로
|
||||
* @return ExtensionManager 경로가 치환된 매니저
|
||||
*/
|
||||
private function managerWritingTo(string $path): ExtensionManager
|
||||
{
|
||||
return new class(app(ModuleRepositoryInterface::class), app(PluginRepositoryInterface::class), $path) extends ExtensionManager
|
||||
{
|
||||
public function __construct(
|
||||
ModuleRepositoryInterface $moduleRepository,
|
||||
PluginRepositoryInterface $pluginRepository,
|
||||
string $path
|
||||
) {
|
||||
parent::__construct($moduleRepository, $pluginRepository);
|
||||
$this->autoloadFilePath = $path;
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* 디스크에 존재하는 "설치된 것으로 보이는" 확장 디렉토리 목록을 반환합니다.
|
||||
*
|
||||
* @return array<int, string> 디렉토리 절대경로 목록
|
||||
*/
|
||||
private function installedLookingExtensionDirectories(): array
|
||||
{
|
||||
$found = [];
|
||||
|
||||
foreach (['modules', 'plugins'] as $type) {
|
||||
$base = base_path($type);
|
||||
if (! File::isDirectory($base)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
foreach (File::directories($base) as $dir) {
|
||||
if (str_starts_with(basename($dir), '_')) {
|
||||
continue;
|
||||
}
|
||||
if (File::exists($dir.'/composer.json')) {
|
||||
$found[] = $dir;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return $found;
|
||||
}
|
||||
|
||||
/**
|
||||
* 주어진 PSR-4 매핑을 담은 캐시 파일 내용을 생성합니다.
|
||||
*
|
||||
* @param array<string, string> $psr4 PSR-4 매핑
|
||||
* @return string 캐시 파일 내용
|
||||
*/
|
||||
private function cacheFileWithPsr4(array $psr4): string
|
||||
{
|
||||
return '<?php return '.var_export([
|
||||
'psr4' => $psr4,
|
||||
'classmap' => [],
|
||||
'src_classmap' => [],
|
||||
'files' => [],
|
||||
'vendor_autoloads' => [],
|
||||
], true).';';
|
||||
}
|
||||
}
|
||||
@@ -80,6 +80,39 @@ class NoSourcemapArtifactsTest extends TestCase
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* 공개 docroot(`public/build`)에 소스맵 산출물이 남아 있지 않은지 테스트
|
||||
*
|
||||
* 확장 에셋과 달리 `public/` 은 웹서버가 직접 서빙하는 docroot 라, 확장자
|
||||
* 화이트리스트(AllowedModuleFileType 등)를 전혀 거치지 않는다. 즉 여기 남은
|
||||
* `.map` 은 URL 만 알면 그대로 내려받을 수 있고 `sourcesContent` 에 원본
|
||||
* 전문이 담겨 있다.
|
||||
*
|
||||
* `--production` 빌드는 `.map` 을 생성하지 않지만 **기존 파일을 지우지도 않는다**.
|
||||
* 따라서 한 번이라도 dev 빌드를 돌린 트리에는 stale 한 소스맵이 그대로 남는다
|
||||
* (실측: `GET /build/core/template-engine.min.js.map` → 200 + 원본 노출).
|
||||
*/
|
||||
public function test_public_build_dir_has_no_sourcemap_artifacts(): void
|
||||
{
|
||||
$offenders = [];
|
||||
|
||||
foreach (glob(base_path('public/build/**/*.map')) ?: [] as $path) {
|
||||
$offenders[] = str_replace(base_path().DIRECTORY_SEPARATOR, '', $path);
|
||||
}
|
||||
|
||||
foreach (glob(base_path('public/build/*.map')) ?: [] as $path) {
|
||||
$offenders[] = str_replace(base_path().DIRECTORY_SEPARATOR, '', $path);
|
||||
}
|
||||
|
||||
$this->assertSame(
|
||||
[],
|
||||
$offenders,
|
||||
"공개 docroot 에 소스맵이 남아 있습니다. URL 로 직접 내려받아 원본 코드가 노출됩니다.\n".
|
||||
"`--production` 재빌드는 기존 .map 을 지우지 않으므로 직접 삭제해야 합니다:\n ".
|
||||
implode("\n ", $offenders)
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* 활성(서빙) 확장 디렉토리에 소스맵 산출물이 남아 있지 않은지 테스트
|
||||
*
|
||||
|
||||
@@ -5,6 +5,7 @@ namespace Tests\Unit\Extension;
|
||||
use App\Enums\ExtensionStatus;
|
||||
use App\Enums\LayoutSourceType;
|
||||
use App\Extension\TemplateManager;
|
||||
use App\Extension\Traits\ClearsTemplateCaches;
|
||||
use App\Models\Template;
|
||||
use App\Models\TemplateLayout;
|
||||
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||||
@@ -46,7 +47,7 @@ class TemplateManagerOverrideTest extends TestCase
|
||||
private function createTestTemplateStructure(): void
|
||||
{
|
||||
// 기본 디렉토리 생성
|
||||
File::makeDirectory($this->testTemplatePath . '/layouts/overrides/sirsoft-sample', 0755, true);
|
||||
File::makeDirectory($this->testTemplatePath.'/layouts/overrides/sirsoft-sample', 0755, true);
|
||||
|
||||
// template.json 생성
|
||||
$templateJson = [
|
||||
@@ -57,7 +58,7 @@ class TemplateManagerOverrideTest extends TestCase
|
||||
'type' => 'admin',
|
||||
'description' => ['ko' => '테스트용 템플릿', 'en' => 'Test template'],
|
||||
];
|
||||
File::put($this->testTemplatePath . '/template.json', json_encode($templateJson, JSON_PRETTY_PRINT));
|
||||
File::put($this->testTemplatePath.'/template.json', json_encode($templateJson, JSON_PRETTY_PRINT));
|
||||
|
||||
// 기본 레이아웃 생성
|
||||
$baseLayout = [
|
||||
@@ -65,7 +66,7 @@ class TemplateManagerOverrideTest extends TestCase
|
||||
'meta' => ['title' => 'Admin Base'],
|
||||
'components' => [['component' => 'Container', 'children' => []]],
|
||||
];
|
||||
File::put($this->testTemplatePath . '/layouts/_admin_base.json', json_encode($baseLayout, JSON_PRETTY_PRINT));
|
||||
File::put($this->testTemplatePath.'/layouts/_admin_base.json', json_encode($baseLayout, JSON_PRETTY_PRINT));
|
||||
|
||||
// 오버라이드 레이아웃 생성
|
||||
$overrideLayout = [
|
||||
@@ -78,7 +79,7 @@ class TemplateManagerOverrideTest extends TestCase
|
||||
],
|
||||
];
|
||||
File::put(
|
||||
$this->testTemplatePath . '/layouts/overrides/sirsoft-sample/admin_index.json',
|
||||
$this->testTemplatePath.'/layouts/overrides/sirsoft-sample/admin_index.json',
|
||||
json_encode($overrideLayout, JSON_PRETTY_PRINT)
|
||||
);
|
||||
}
|
||||
@@ -154,8 +155,8 @@ class TemplateManagerOverrideTest extends TestCase
|
||||
*/
|
||||
public function test_generate_override_layout_name(): void
|
||||
{
|
||||
$basePath = $this->testTemplatePath . '/layouts/overrides/sirsoft-sample';
|
||||
$filePath = $basePath . '/admin_index.json';
|
||||
$basePath = $this->testTemplatePath.'/layouts/overrides/sirsoft-sample';
|
||||
$filePath = $basePath.'/admin_index.json';
|
||||
|
||||
// Windows/Unix 경로 정규화
|
||||
$normalizedBasePath = str_replace('/', DIRECTORY_SEPARATOR, $basePath);
|
||||
@@ -175,10 +176,10 @@ class TemplateManagerOverrideTest extends TestCase
|
||||
*/
|
||||
public function test_generate_override_layout_name_with_nested_path(): void
|
||||
{
|
||||
$basePath = $this->testTemplatePath . '/layouts/overrides/sirsoft-sample';
|
||||
File::makeDirectory($basePath . '/products', 0755, true);
|
||||
$basePath = $this->testTemplatePath.'/layouts/overrides/sirsoft-sample';
|
||||
File::makeDirectory($basePath.'/products', 0755, true);
|
||||
|
||||
$filePath = $basePath . '/products/list.json';
|
||||
$filePath = $basePath.'/products/list.json';
|
||||
|
||||
// Windows/Unix 경로 정규화
|
||||
$normalizedBasePath = str_replace('/', DIRECTORY_SEPARATOR, $basePath);
|
||||
@@ -199,13 +200,13 @@ class TemplateManagerOverrideTest extends TestCase
|
||||
public function test_scan_override_layout_files(): void
|
||||
{
|
||||
// 디렉토리 구조 생성
|
||||
$overridesPath = $this->testTemplatePath . '/layouts/overrides/sirsoft-sample';
|
||||
File::makeDirectory($overridesPath . '/nested', 0755, true);
|
||||
$overridesPath = $this->testTemplatePath.'/layouts/overrides/sirsoft-sample';
|
||||
File::makeDirectory($overridesPath.'/nested', 0755, true);
|
||||
|
||||
$testLayout = ['slots' => []];
|
||||
File::put($overridesPath . '/index.json', json_encode($testLayout));
|
||||
File::put($overridesPath . '/edit.json', json_encode($testLayout));
|
||||
File::put($overridesPath . '/nested/detail.json', json_encode($testLayout));
|
||||
File::put($overridesPath.'/index.json', json_encode($testLayout));
|
||||
File::put($overridesPath.'/edit.json', json_encode($testLayout));
|
||||
File::put($overridesPath.'/nested/detail.json', json_encode($testLayout));
|
||||
|
||||
$reflection = new \ReflectionClass($this->templateManager);
|
||||
$method = $reflection->getMethod('scanLayoutFilesRecursively');
|
||||
@@ -290,8 +291,8 @@ class TemplateManagerOverrideTest extends TestCase
|
||||
public function test_register_overrides_for_multiple_modules(): void
|
||||
{
|
||||
// 두 모듈의 오버라이드 디렉토리 생성
|
||||
$module1Path = $this->testTemplatePath . '/layouts/overrides/sirsoft-module1';
|
||||
$module2Path = $this->testTemplatePath . '/layouts/overrides/sirsoft-module2';
|
||||
$module1Path = $this->testTemplatePath.'/layouts/overrides/sirsoft-module1';
|
||||
$module2Path = $this->testTemplatePath.'/layouts/overrides/sirsoft-module2';
|
||||
File::makeDirectory($module1Path, 0755, true);
|
||||
File::makeDirectory($module2Path, 0755, true);
|
||||
|
||||
@@ -303,13 +304,13 @@ class TemplateManagerOverrideTest extends TestCase
|
||||
'version' => '1.0.0',
|
||||
'type' => 'admin',
|
||||
];
|
||||
File::put($this->testTemplatePath . '/template.json', json_encode($templateJson));
|
||||
File::put($this->testTemplatePath.'/template.json', json_encode($templateJson));
|
||||
|
||||
// 각 모듈의 오버라이드 레이아웃 생성
|
||||
$layout1 = ['layout_name' => 'sirsoft-module1_admin_index', 'slots' => []];
|
||||
$layout2 = ['layout_name' => 'sirsoft-module2_admin_index', 'slots' => []];
|
||||
File::put($module1Path . '/admin_index.json', json_encode($layout1));
|
||||
File::put($module2Path . '/admin_index.json', json_encode($layout2));
|
||||
File::put($module1Path.'/admin_index.json', json_encode($layout1));
|
||||
File::put($module2Path.'/admin_index.json', json_encode($layout2));
|
||||
|
||||
// 템플릿 로드
|
||||
$this->templateManager->loadTemplates();
|
||||
@@ -404,7 +405,7 @@ class TemplateManagerOverrideTest extends TestCase
|
||||
public function test_handles_missing_overrides_directory(): void
|
||||
{
|
||||
// 기본 템플릿 구조만 생성 (overrides 없음)
|
||||
File::makeDirectory($this->testTemplatePath . '/layouts', 0755, true);
|
||||
File::makeDirectory($this->testTemplatePath.'/layouts', 0755, true);
|
||||
|
||||
$templateJson = [
|
||||
'identifier' => 'test-admin',
|
||||
@@ -413,7 +414,7 @@ class TemplateManagerOverrideTest extends TestCase
|
||||
'version' => '1.0.0',
|
||||
'type' => 'admin',
|
||||
];
|
||||
File::put($this->testTemplatePath . '/template.json', json_encode($templateJson));
|
||||
File::put($this->testTemplatePath.'/template.json', json_encode($templateJson));
|
||||
|
||||
$this->templateManager->loadTemplates();
|
||||
|
||||
@@ -472,21 +473,35 @@ class TemplateManagerOverrideTest extends TestCase
|
||||
Cache::put('g7:core:ext.cache_version', $expectedVersion);
|
||||
|
||||
// ClearsTemplateCaches trait의 정적 메서드 호출
|
||||
$version = \App\Extension\Traits\ClearsTemplateCaches::getExtensionCacheVersion();
|
||||
$version = ClearsTemplateCaches::getExtensionCacheVersion();
|
||||
|
||||
$this->assertEquals($expectedVersion, $version);
|
||||
}
|
||||
|
||||
/**
|
||||
* 캐시 버전이 설정되지 않은 경우 0 반환
|
||||
* 캐시 버전 키가 없으면 유효한 새 버전을 생성·저장하는지 테스트
|
||||
*
|
||||
* 0 을 반환하면 버전 기반 캐시 무효화가 통째로 붕괴한다(모든 URL 이 `?v=0`
|
||||
* 으로 고정되어 갱신이 전파되지 않음). 키 부재는 캐시 플러시·TTL 만료로
|
||||
* 언제든 발생하므로, 그 자리에서 새 버전을 만들어 저장한다.
|
||||
*/
|
||||
public function test_get_extension_cache_version_returns_zero_when_not_set(): void
|
||||
public function test_get_extension_cache_version_regenerates_when_not_set(): void
|
||||
{
|
||||
Cache::forget('g7:core:ext.cache_version');
|
||||
|
||||
$version = \App\Extension\Traits\ClearsTemplateCaches::getExtensionCacheVersion();
|
||||
$beforeTime = time();
|
||||
$version = ClearsTemplateCaches::getExtensionCacheVersion();
|
||||
$afterTime = time();
|
||||
|
||||
$this->assertEquals(0, $version);
|
||||
$this->assertGreaterThan(0, $version, '키 부재 시 0 을 반환하면 캐시 무효화가 붕괴합니다.');
|
||||
$this->assertGreaterThanOrEqual($beforeTime, $version);
|
||||
$this->assertLessThanOrEqual($afterTime, $version);
|
||||
|
||||
$this->assertEquals(
|
||||
$version,
|
||||
Cache::get('g7:core:ext.cache_version'),
|
||||
'생성한 버전은 캐시에 저장되어 다음 조회에서 재사용되어야 합니다.'
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -0,0 +1,103 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\Unit\Rules;
|
||||
|
||||
use App\Rules\AllowedModuleFileType;
|
||||
use App\Rules\AllowedPluginFileType;
|
||||
use App\Rules\AllowedTemplateFileType;
|
||||
use Illuminate\Support\Facades\Validator;
|
||||
use PHPUnit\Framework\Attributes\DataProvider;
|
||||
use Tests\TestCase;
|
||||
|
||||
/**
|
||||
* 소스맵 에셋 서빙의 환경별 허용 계약 테스트.
|
||||
*
|
||||
* 소스맵에는 원본 TS/TSX 전문(`sourcesContent`)이 담기고, 확장 에셋 서빙에서
|
||||
* 이 확장자 화이트리스트가 유일한 방어선이다. 따라서 운영 환경에서는 어떤
|
||||
* 경우에도 `.map` 이 서빙되면 안 된다.
|
||||
*
|
||||
* 동시에 로컬 개발 환경의 dev 빌드 산출물은 `//# sourceMappingURL` 이 개별
|
||||
* 에셋 서빙 URL 을 가리키므로, 차단하면 브라우저 콘솔에 404 가 쌓인다.
|
||||
* 두 요구를 환경으로 가른다.
|
||||
*/
|
||||
class SourcemapServingEnvironmentTest extends TestCase
|
||||
{
|
||||
/**
|
||||
* 검증 대상 규칙 클래스 목록
|
||||
*
|
||||
* @return array<string, array{class-string, string}>
|
||||
*/
|
||||
public static function ruleProvider(): array
|
||||
{
|
||||
return [
|
||||
'module' => [AllowedModuleFileType::class, 'dist/js/module.iife.js.map'],
|
||||
'plugin' => [AllowedPluginFileType::class, 'dist/js/plugin.iife.js.map'],
|
||||
'template' => [AllowedTemplateFileType::class, 'dist/js/components.iife.js.map'],
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* 운영 환경에서 소스맵이 차단되는지 테스트
|
||||
*
|
||||
* @param class-string $ruleClass 규칙 클래스
|
||||
* @param string $sourcemapPath 소스맵 경로
|
||||
*/
|
||||
#[DataProvider('ruleProvider')]
|
||||
public function test_sourcemap_is_blocked_in_production(string $ruleClass, string $sourcemapPath): void
|
||||
{
|
||||
$this->app->detectEnvironment(fn () => 'production');
|
||||
|
||||
$this->assertNotContains('map', $ruleClass::getAllowedExtensions());
|
||||
|
||||
$validator = Validator::make(['path' => $sourcemapPath], ['path' => new $ruleClass]);
|
||||
|
||||
$this->assertFalse(
|
||||
$validator->passes(),
|
||||
$ruleClass.': 운영 환경에서 소스맵이 서빙되면 원본 코드 전문이 노출됩니다.'
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* 로컬 개발 환경에서 소스맵이 허용되는지 테스트
|
||||
*
|
||||
* @param class-string $ruleClass 규칙 클래스
|
||||
* @param string $sourcemapPath 소스맵 경로
|
||||
*/
|
||||
#[DataProvider('ruleProvider')]
|
||||
public function test_sourcemap_is_allowed_in_local(string $ruleClass, string $sourcemapPath): void
|
||||
{
|
||||
$this->app->detectEnvironment(fn () => 'local');
|
||||
|
||||
$this->assertContains('map', $ruleClass::getAllowedExtensions());
|
||||
|
||||
$validator = Validator::make(['path' => $sourcemapPath], ['path' => new $ruleClass]);
|
||||
|
||||
$this->assertTrue(
|
||||
$validator->passes(),
|
||||
$ruleClass.': 로컬 개발 환경에서는 dev 빌드의 소스맵 참조가 404 가 되지 않도록 허용해야 합니다.'
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* 소스맵 외 확장자는 환경과 무관하게 동일하게 허용되는지 테스트
|
||||
*
|
||||
* 환경 분기가 소스맵에만 적용되고 다른 확장자에는 영향을 주지 않아야 한다.
|
||||
*
|
||||
* @param class-string $ruleClass 규칙 클래스
|
||||
*/
|
||||
#[DataProvider('ruleProvider')]
|
||||
public function test_non_sourcemap_extensions_are_environment_independent(string $ruleClass): void
|
||||
{
|
||||
$this->app->detectEnvironment(fn () => 'production');
|
||||
$production = $ruleClass::getAllowedExtensions();
|
||||
|
||||
$this->app->detectEnvironment(fn () => 'local');
|
||||
$local = $ruleClass::getAllowedExtensions();
|
||||
|
||||
$this->assertSame(
|
||||
['map'],
|
||||
array_values(array_diff($local, $production)),
|
||||
$ruleClass.': 환경에 따라 달라지는 확장자는 소스맵 하나뿐이어야 합니다.'
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user