feat(core,extensions): 구동 에셋 자체 제공 · 자산 실패 폴백 · 운영자 추가 에셋(custom/)
공개 저장소 이슈 gnuboard/g7 (@bigmsg) 제보에서 출발한 작업이다. 브라우저가 화면을 그리려고 제3자 CDN 에 도달해야 하면, 그 도달 실패는 예외도 로그도 남기지 않고 화면 기능만 조용히 사라진다. 폐쇄망·방화벽·광고차단기에서 재현되는데 자체 서버 로그에는 흔적이 없어 운영자가 원인을 특정할 수 없다. 제보된 것은 편집기 하나였지만 같은 구조가 아이콘·글꼴·코드편집기·압축 라이브러리·설치 마법사·개발 대시보드에 똑같이 있었으므로, 번들 확장과 템플릿 전체를 자체 제공으로 옮겼다. 런타임에 외부로 나가는 것은 주소 검색 서비스 하나만 남았다. 자체 제공만으로는 부족하다 — 자기 서버에서 받는 파일도 실패할 수 있고, 종전에는 그 실패가 무음이었다. CSS 경로에 재시도 계층을 세우고(스크립트 경로와 동형), 서버가 HTML 에 직접 심는 externals 까지 실패를 붙잡아 안내 배너와 [다시 시도]로 표면화했다. 편집기·코드편집기는 확보 실패 시 평문 입력으로 내려앉되 저장 계약을 유지한다. 두 번째 축은 운영자가 CSS 를 덧붙일 자리가 없던 문제다(sir.kr 문의). 확장 디렉토리의 custom/ 을 운영자 소유로 정해, 확장 교체가 그 디렉토리만은 보존하게 했다. 출처에 의존하지 않는 서술자로 해석하므로 나중에 다른 출처가 붙어도 소비자는 바뀌지 않는다. 확장 자산과 같은 메커니즘으로 정적 게시되어 CSS 내부 상대 url 도 해석되고, 파일을 고치면 그 변경을 감지해 재게시까지 예약된다. FTP 접근이 없는 운영자에게는 그 자리도 없는 것과 같으므로 레이아웃 편집기에서 직접 넣고 고칠 수 있게 했다. 모듈·플러그인·템플릿이 한 엔드포인트를 공유한다 — 타입별로 나누면 같은 검증이 세 벌로 갈리고 그중 약한 하나가 우회로가 된다. 여기서 올린 스크립트는 그 레이아웃 한 장이 아니라 사이트 전 화면에서 실행되므로 레이아웃 편집과 분리된 전용 권한으로 연다. 운영자 CSS 가 화면을 조작 불능으로 만들면 그것을 고칠 화면에도 같은 CSS 가 실려 스스로 갇히므로, 서버가 목록을 비우는 탈출구(?custom=off)를 함께 뒀다. 동봉 자산은 재생성 경로에 버전 대조 가드를 붙였다. 선언과 다른 버전을 버전 디렉토리에 써 넣는 조용한 거짓말은 배포된 뒤에는 드러나지 않는다.
This commit is contained in:
1 parent
72f2e94c1a
commit
3945b6f1b3
1042 files changed
+107435
-947
No files matched your search
@@ -0,0 +1,391 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\Feature\Api\Admin;
|
||||
|
||||
use App\Contracts\Extension\CacheInterface;
|
||||
use App\Models\Permission;
|
||||
use App\Models\Role;
|
||||
use App\Models\User;
|
||||
use App\Services\CustomAssetService;
|
||||
use App\Services\ExtensionStaticCacheService;
|
||||
use App\Support\CustomAssets;
|
||||
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||||
use Illuminate\Http\UploadedFile;
|
||||
use Tests\TestCase;
|
||||
|
||||
/**
|
||||
* 사용자 추가 에셋(`custom/`) 관리 API 테스트
|
||||
*
|
||||
* 운영자가 화면에서 자기 CSS·JS·폰트·이미지를 넣고 고치는 경로를 잠근다. 특히
|
||||
* **권한 경계**와 **경로 탈출** 두 축은 실패해도 오류가 남지 않는 종류라(정상 200 이
|
||||
* 나가는 것이 유일한 증상) 테스트가 유일한 방어선이다.
|
||||
*/
|
||||
class AdminExtensionCustomAssetControllerTest extends TestCase
|
||||
{
|
||||
use RefreshDatabase;
|
||||
|
||||
private const TEMPLATE = 'sirsoft-admin_basic';
|
||||
|
||||
private User $manager;
|
||||
|
||||
private string $managerToken;
|
||||
|
||||
private User $outsider;
|
||||
|
||||
private string $outsiderToken;
|
||||
|
||||
private string $customDir;
|
||||
|
||||
protected function setUp(): void
|
||||
{
|
||||
parent::setUp();
|
||||
|
||||
$managePermission = Permission::firstOrCreate([
|
||||
'identifier' => 'core.extensions.custom_assets.manage',
|
||||
], [
|
||||
'name' => '커스텀 자산 관리',
|
||||
'display_name' => '커스텀 자산 관리',
|
||||
'type' => 'admin',
|
||||
]);
|
||||
|
||||
// 레이아웃 편집 권한만 가진 역할 — "레이아웃을 고칠 수 있다" 가 곧 "사이트 전역
|
||||
// 스크립트를 올릴 수 있다" 가 되지 않는지 확인하는 대조군이다.
|
||||
$layoutPermission = Permission::firstOrCreate([
|
||||
'identifier' => 'core.templates.layouts.edit',
|
||||
], [
|
||||
'name' => '레이아웃 편집',
|
||||
'display_name' => '레이아웃 편집',
|
||||
'type' => 'admin',
|
||||
]);
|
||||
|
||||
$managerRole = Role::firstOrCreate(['identifier' => 'custom-asset-manager'], [
|
||||
'name' => 'Custom Asset Manager',
|
||||
'display_name' => 'Custom Asset Manager',
|
||||
'is_default' => false,
|
||||
]);
|
||||
$managerRole->permissions()->syncWithoutDetaching([$managePermission->id]);
|
||||
|
||||
$layoutRole = Role::firstOrCreate(['identifier' => 'layout-only'], [
|
||||
'name' => 'Layout Only',
|
||||
'display_name' => 'Layout Only',
|
||||
'is_default' => false,
|
||||
]);
|
||||
$layoutRole->permissions()->syncWithoutDetaching([$layoutPermission->id]);
|
||||
|
||||
$this->manager = User::factory()->create();
|
||||
$this->manager->roles()->syncWithoutDetaching([$managerRole->id]);
|
||||
$this->managerToken = $this->manager->createToken('manager')->plainTextToken;
|
||||
|
||||
$this->outsider = User::factory()->create();
|
||||
$this->outsider->roles()->syncWithoutDetaching([$layoutRole->id]);
|
||||
$this->outsiderToken = $this->outsider->createToken('outsider')->plainTextToken;
|
||||
|
||||
$this->customDir = (string) CustomAssets::directory('templates', self::TEMPLATE);
|
||||
$this->cleanupCustomDir();
|
||||
}
|
||||
|
||||
protected function tearDown(): void
|
||||
{
|
||||
$this->cleanupCustomDir();
|
||||
|
||||
parent::tearDown();
|
||||
}
|
||||
|
||||
/**
|
||||
* 테스트가 만든 파일만 지웁니다 (운영자 파일 보존).
|
||||
*/
|
||||
private function cleanupCustomDir(): void
|
||||
{
|
||||
foreach (glob($this->customDir.DIRECTORY_SEPARATOR.'__t123-*') ?: [] as $path) {
|
||||
@unlink($path);
|
||||
}
|
||||
|
||||
@rmdir($this->customDir.DIRECTORY_SEPARATOR.'__t123-dir');
|
||||
}
|
||||
|
||||
/**
|
||||
* 관리 API URL 을 만듭니다.
|
||||
*
|
||||
* 세 타입이 한 엔드포인트를 공유하므로 테스트도 타입을 인자로 받는다 — URL 을 케이스마다
|
||||
* 손으로 조립하면 한 타입만 고쳐 놓고 나머지는 옛 경로를 두드리게 된다.
|
||||
*
|
||||
* @param string $suffix `/custom-assets…` 이후 경로
|
||||
* @param string $type 확장 타입 (`template` | `module` | `plugin`)
|
||||
* @param string|null $identifier 확장 식별자 (기본: 대상 템플릿)
|
||||
* @return string URL
|
||||
*/
|
||||
private function url(string $suffix, string $type = 'template', ?string $identifier = null): string
|
||||
{
|
||||
return '/api/admin/extensions/'.$type.'/'.($identifier ?? self::TEMPLATE).$suffix;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array<string, string>
|
||||
*/
|
||||
private function headers(string $token): array
|
||||
{
|
||||
return ['Authorization' => 'Bearer '.$token, 'Accept' => 'application/json'];
|
||||
}
|
||||
|
||||
public function test_저장_조회_목록_삭제_왕복(): void
|
||||
{
|
||||
// @scenario manage_actor=with_permission, manage_action=save
|
||||
// @effects custom_asset_editor_save_invalidates_published_copy
|
||||
// @scenario manage_actor=with_permission, manage_action=read
|
||||
// @effects custom_asset_manage_requires_dedicated_permission
|
||||
// @scenario manage_actor=with_permission, manage_action=delete
|
||||
// @effects custom_asset_manage_requires_dedicated_permission
|
||||
$path = '__t123-overrides.css';
|
||||
|
||||
$save = $this->putJson(
|
||||
$this->url('/custom-assets/content'),
|
||||
['path' => $path, 'content' => 'body { color: red; }'],
|
||||
$this->headers($this->managerToken)
|
||||
);
|
||||
$save->assertStatus(200);
|
||||
$this->assertFileExists($this->customDir.DIRECTORY_SEPARATOR.$path);
|
||||
|
||||
$index = $this->getJson(
|
||||
$this->url('/custom-assets'),
|
||||
$this->headers($this->managerToken)
|
||||
);
|
||||
$index->assertStatus(200);
|
||||
$files = $index->json('data.files');
|
||||
$paths = array_column($files, 'path');
|
||||
$this->assertContains($path, $paths);
|
||||
|
||||
// `loaded` 는 그 파일이 실제로 페이지에 실리는지다. 서술자에 상대 경로 필드가
|
||||
// 없어 한때 언제나 false 였고, 그러면 화면이 모든 파일을 "실리지 않음" 으로
|
||||
// 표시해 운영자가 규약 스캔이 고장났다고 읽는다 (E2E 가 먼저 잡았다).
|
||||
$row = collect($files)->firstWhere('path', $path);
|
||||
$this->assertTrue($row['loaded'], '규약 스캔이 자동으로 싣는 css 인데 loaded 가 false 입니다.');
|
||||
|
||||
$show = $this->getJson(
|
||||
$this->url('/custom-assets/content?path='.urlencode($path)),
|
||||
$this->headers($this->managerToken)
|
||||
);
|
||||
$show->assertStatus(200);
|
||||
$this->assertSame('body { color: red; }', $show->json('data.content'));
|
||||
|
||||
$delete = $this->deleteJson(
|
||||
$this->url('/custom-assets?path='.urlencode($path)),
|
||||
[],
|
||||
$this->headers($this->managerToken)
|
||||
);
|
||||
$delete->assertStatus(200);
|
||||
$this->assertFileDoesNotExist($this->customDir.DIRECTORY_SEPARATOR.$path);
|
||||
}
|
||||
|
||||
public function test_레이아웃_편집_권한만으로는_접근할_수_없다(): void
|
||||
{
|
||||
// 여기서 올린 스크립트는 그 레이아웃 한 장이 아니라 사이트 전 화면에서 실행된다.
|
||||
// 레이아웃 편집 권한이 곧 이 권한이 되면 그 확대는 조용히 일어난다.
|
||||
// @scenario manage_actor=layout_edit_only, manage_action=list
|
||||
// @effects custom_asset_manage_requires_dedicated_permission
|
||||
// @scenario manage_actor=layout_edit_only, manage_action=read
|
||||
// @effects custom_asset_manage_requires_dedicated_permission
|
||||
// @scenario manage_actor=layout_edit_only, manage_action=save
|
||||
// @effects custom_asset_manage_requires_dedicated_permission
|
||||
foreach ([
|
||||
['getJson', $this->url('/custom-assets'), []],
|
||||
['getJson', $this->url('/custom-assets/content?path=a.css'), []],
|
||||
] as [$method, $url, $payload]) {
|
||||
$response = $this->{$method}($url, $this->headers($this->outsiderToken));
|
||||
$response->assertStatus(403);
|
||||
}
|
||||
|
||||
$this->putJson(
|
||||
$this->url('/custom-assets/content'),
|
||||
['path' => '__t123-evil.css', 'content' => 'x'],
|
||||
$this->headers($this->outsiderToken)
|
||||
)->assertStatus(403);
|
||||
|
||||
$this->assertFileDoesNotExist($this->customDir.DIRECTORY_SEPARATOR.'__t123-evil.css');
|
||||
}
|
||||
|
||||
public function test_비인증_요청은_거부된다(): void
|
||||
{
|
||||
// @scenario manage_actor=unauthenticated, manage_action=list
|
||||
// @effects custom_asset_manage_requires_dedicated_permission
|
||||
$this->getJson($this->url('/custom-assets'), ['Accept' => 'application/json'])
|
||||
->assertStatus(401);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array<string, array{0: string}>
|
||||
*/
|
||||
public static function 탈출_경로_제공자(): array
|
||||
{
|
||||
return [
|
||||
'상위 디렉토리' => ['../evil.css'],
|
||||
'중간 상위' => ['a/../../evil.css'],
|
||||
'절대 경로' => ['/etc/passwd.css'],
|
||||
'역슬래시 상위' => ['..\\evil.css'],
|
||||
'빈 세그먼트' => ['a//b.css'],
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider 탈출_경로_제공자
|
||||
*/
|
||||
public function test_경로_탈출은_422_로_차단된다(string $path): void
|
||||
{
|
||||
// @scenario manage_actor=with_permission, manage_action=save
|
||||
// @effects custom_asset_manage_path_traversal_blocked
|
||||
$this->putJson(
|
||||
$this->url('/custom-assets/content'),
|
||||
['path' => $path, 'content' => 'x'],
|
||||
$this->headers($this->managerToken)
|
||||
)->assertStatus(422);
|
||||
}
|
||||
|
||||
public function test_편집_불가_확장자는_본문_저장이_거부된다(): void
|
||||
{
|
||||
// @scenario manage_actor=with_permission, manage_action=save
|
||||
// @effects custom_asset_manage_binary_rejects_text_save
|
||||
// 바이너리를 텍스트 편집기에 열어 저장하면 내용이 손상된다 — 그 경로를 막는다.
|
||||
$this->putJson(
|
||||
$this->url('/custom-assets/content'),
|
||||
['path' => '__t123-font.woff2', 'content' => 'x'],
|
||||
$this->headers($this->managerToken)
|
||||
)->assertStatus(422);
|
||||
}
|
||||
|
||||
public function test_업로드는_허용_확장자만_받는다(): void
|
||||
{
|
||||
// @scenario manage_actor=with_permission, manage_action=upload
|
||||
// @effects custom_asset_manage_upload_extension_whitelist
|
||||
$rejected = $this->post(
|
||||
$this->url('/custom-assets/upload'),
|
||||
['file' => UploadedFile::fake()->create('__t123-shell.php', 4)],
|
||||
$this->headers($this->managerToken)
|
||||
);
|
||||
$rejected->assertStatus(422);
|
||||
$this->assertFileDoesNotExist($this->customDir.DIRECTORY_SEPARATOR.'__t123-shell.php');
|
||||
|
||||
$accepted = $this->post(
|
||||
$this->url('/custom-assets/upload'),
|
||||
['file' => UploadedFile::fake()->image('__t123-logo.png')],
|
||||
$this->headers($this->managerToken)
|
||||
);
|
||||
$accepted->assertStatus(200);
|
||||
$this->assertFileExists($this->customDir.DIRECTORY_SEPARATOR.'__t123-logo.png');
|
||||
}
|
||||
|
||||
public function test_목록_응답이_편집기_메타를_함께_싣는다(): void
|
||||
{
|
||||
// @scenario manage_actor=with_permission, manage_action=list
|
||||
// @effects custom_asset_manage_requires_dedicated_permission
|
||||
$response = $this->getJson(
|
||||
$this->url('/custom-assets'),
|
||||
$this->headers($this->managerToken)
|
||||
);
|
||||
|
||||
$response->assertStatus(200);
|
||||
$this->assertSame(CustomAssetService::EDITABLE_EXTENSIONS, $response->json('data.editable_extensions'));
|
||||
$this->assertSame(CustomAssetService::MAX_TEXT_BYTES, $response->json('data.max_text_bytes'));
|
||||
$this->assertSame(CustomAssetService::MAX_UPLOAD_BYTES, $response->json('data.max_upload_bytes'));
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array<string, array{0: string, 1: string}>
|
||||
*/
|
||||
public static function 확장_타입_제공자(): array
|
||||
{
|
||||
return [
|
||||
'템플릿' => ['template', self::TEMPLATE],
|
||||
'모듈' => ['module', 'sirsoft-page'],
|
||||
'플러그인' => ['plugin', 'sirsoft-gdpr'],
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* 세 타입이 같은 강도로 동작해야 한다.
|
||||
*
|
||||
* 타입별로 경로·검증이 갈리면 그중 약한 쪽이 조용한 우회로가 되고, 반대로 한 타입만
|
||||
* 동작하면 운영자는 "모듈에서는 왜 안 되는지" 를 알 수 없다.
|
||||
*
|
||||
* @dataProvider 확장_타입_제공자
|
||||
*/
|
||||
public function test_세_타입_모두_같은_왕복을_지원한다(string $type, string $identifier): void
|
||||
{
|
||||
// @scenario manage_actor=with_permission, manage_action=save
|
||||
// @effects custom_asset_manage_all_extension_types_parity
|
||||
$dir = (string) CustomAssets::directory($type.'s', $identifier);
|
||||
$path = '__t123-parity.css';
|
||||
|
||||
try {
|
||||
$this->putJson(
|
||||
$this->url('/custom-assets/content', $type, $identifier),
|
||||
['path' => $path, 'content' => 'body { color: blue; }'],
|
||||
$this->headers($this->managerToken)
|
||||
)->assertStatus(200);
|
||||
|
||||
$this->assertFileExists($dir.DIRECTORY_SEPARATOR.$path);
|
||||
|
||||
$index = $this->getJson(
|
||||
$this->url('/custom-assets', $type, $identifier),
|
||||
$this->headers($this->managerToken)
|
||||
);
|
||||
$index->assertStatus(200);
|
||||
$this->assertContains($path, array_column($index->json('data.files'), 'path'));
|
||||
|
||||
// 권한 경계도 타입과 무관하게 같아야 한다.
|
||||
//
|
||||
// 한 테스트 안에서 토큰을 바꿔 요청하려면 가드를 먼저 잊어야 한다 — 앱이
|
||||
// 요청 사이에 재부팅되지 않아 `auth:sanctum` 가드가 앞 요청에서 해석한
|
||||
// 사용자를 그대로 재사용하고, 그러면 권한이 없는 토큰이 200 을 받는다
|
||||
// (제품 결함이 아니라 테스트 하네스의 성질이다).
|
||||
$this->app['auth']->forgetGuards();
|
||||
|
||||
$this->getJson(
|
||||
$this->url('/custom-assets', $type, $identifier),
|
||||
$this->headers($this->outsiderToken)
|
||||
)->assertStatus(403);
|
||||
|
||||
$this->app['auth']->forgetGuards();
|
||||
|
||||
$this->deleteJson(
|
||||
$this->url('/custom-assets?path='.urlencode($path), $type, $identifier),
|
||||
[],
|
||||
$this->headers($this->managerToken)
|
||||
)->assertStatus(200);
|
||||
|
||||
$this->assertFileDoesNotExist($dir.DIRECTORY_SEPARATOR.$path);
|
||||
} finally {
|
||||
@unlink($dir.DIRECTORY_SEPARATOR.$path);
|
||||
@rmdir($dir);
|
||||
}
|
||||
}
|
||||
|
||||
public function test_알_수_없는_확장_타입은_라우트가_받지_않는다(): void
|
||||
{
|
||||
// 정규식이 세 값으로 제한하므로 404 다 — 통과시켜 빈 목록으로 응답하면 오타가
|
||||
// "그 확장에는 파일이 없다" 로 보인다.
|
||||
$this->getJson(
|
||||
'/api/admin/extensions/theme/'.self::TEMPLATE.'/custom-assets',
|
||||
$this->headers($this->managerToken)
|
||||
)->assertStatus(404);
|
||||
}
|
||||
|
||||
public function test_저장이_확장_캐시_버전을_올린다(): void
|
||||
{
|
||||
// 올리지 않으면 편집한 파일이 게시본에 반영되지 않아, 운영자에게는
|
||||
// "고쳤는데 화면이 그대로" 로만 나타난다.
|
||||
$before = ExtensionStaticCacheService::getExtensionCacheVersion();
|
||||
|
||||
// 버전은 초 단위 timestamp 라 같은 초에 저장하면 값이 같을 수 있다 — 값 비교가
|
||||
// 아니라 "덮어써졌는지" 를 보기 위해 과거 값을 심어 둔다.
|
||||
app(CacheInterface::class)->put('ext.cache_version', 1);
|
||||
|
||||
$this->putJson(
|
||||
$this->url('/custom-assets/content'),
|
||||
['path' => '__t123-bump.css', 'content' => 'body{}'],
|
||||
$this->headers($this->managerToken)
|
||||
)->assertStatus(200);
|
||||
|
||||
$after = (int) app(CacheInterface::class)->get('ext.cache_version');
|
||||
$this->assertGreaterThan(1, $after);
|
||||
$this->assertGreaterThan(0, $before);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,318 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\Feature\Extension;
|
||||
|
||||
use PHPUnit\Framework\Attributes\Test;
|
||||
use Tests\TestCase;
|
||||
|
||||
/**
|
||||
* 구동 에셋의 외부 CDN 의존 전수 차단
|
||||
*
|
||||
* 브라우저가 화면을 그리기 위해 제3자 origin 에 도달해야 하면, 그 도달 실패는 예외도
|
||||
* 로그도 남기지 않고 **화면 기능만 조용히 사라진다.** 폐쇄망·방화벽·광고차단기에서
|
||||
* 재현되며 자체 서버 로그에 흔적이 없어 운영자가 원인을 특정할 수 없다.
|
||||
*
|
||||
* 판정 모집단을 한 확장 안에 두지 않고 **코어까지 넓힌다** — 판정기를 한 확장에 두면
|
||||
* 그 확장 밖의 동형 결함이 red 가 되지 않는다(`GenericCatchStatusCodeContractTest` 선례).
|
||||
*
|
||||
* 예외는 두 가지뿐이다:
|
||||
* 1. 확장 manifest 가 `trusted_script_hosts` + `trusted_script_hosts_reason` 을 **함께**
|
||||
* 선언한 호스트 (자체 호스팅이 불가능한 서비스 SDK 등)
|
||||
* 2. 이 테스트가 사유와 함께 명시한 코어 예외 (운영자가 켜야만 출력되는 외부 분석 등)
|
||||
*
|
||||
* DB 를 쓰지 않는다 — 저장소 소스 스캔이다.
|
||||
*/
|
||||
class BundledExtensionNoExternalAssetTest extends TestCase
|
||||
{
|
||||
/**
|
||||
* 자산으로 간주하는 확장자
|
||||
*
|
||||
* API 엔드포인트·문서 링크를 결함으로 오인하지 않도록 **브라우저가 실행/적용하는
|
||||
* 파일**만 본다.
|
||||
*/
|
||||
private const ASSET_EXTENSIONS = 'js|mjs|css|woff2?|ttf|otf|eot';
|
||||
|
||||
/**
|
||||
* 코어의 명시 예외 (사유 필수)
|
||||
*
|
||||
* @var array<string, string>
|
||||
*/
|
||||
private const CORE_EXEMPTIONS = [
|
||||
// 운영자가 seo.google_analytics_id 를 설정했을 때만 출력되는 외부 분석 서비스다.
|
||||
// 자체 제공 대상이 아니며 gdpr 차단 카탈로그가 다루는 영역이다.
|
||||
'googletagmanager.com' => '운영자 설정 시에만 출력되는 외부 분석 서비스',
|
||||
'google-analytics.com' => '운영자 설정 시에만 출력되는 외부 분석 서비스',
|
||||
];
|
||||
|
||||
/**
|
||||
* 스캔 대상 파일 목록을 만듭니다.
|
||||
*
|
||||
* @return array<int, string> 절대 경로 목록
|
||||
*/
|
||||
private function runtimeSources(): array
|
||||
{
|
||||
$patterns = [
|
||||
// 번들 확장 런타임 소스
|
||||
'modules/_bundled/*/resources/**/*.{ts,tsx,json}',
|
||||
'modules/_bundled/*/src/**/*.{ts,tsx}',
|
||||
'plugins/_bundled/*/resources/**/*.{ts,tsx,json}',
|
||||
'plugins/_bundled/*/src/**/*.{ts,tsx}',
|
||||
'templates/_bundled/*/src/**/*.{ts,tsx}',
|
||||
'templates/_bundled/*/layouts/**/*.json',
|
||||
'templates/_bundled/*/template.json',
|
||||
'templates/_bundled/*/seo-config.json',
|
||||
// 코어 런타임 소스
|
||||
'resources/js/core/**/*.{ts,tsx}',
|
||||
'resources/layouts/**/*.json',
|
||||
'resources/views/*.blade.php',
|
||||
];
|
||||
|
||||
$files = [];
|
||||
|
||||
foreach ($patterns as $pattern) {
|
||||
foreach (glob(base_path($pattern), GLOB_BRACE) as $path) {
|
||||
$files[] = $path;
|
||||
}
|
||||
|
||||
// glob 은 `**` 를 재귀로 해석하지 않으므로 디렉토리 재귀를 따로 돈다
|
||||
if (str_contains($pattern, '**')) {
|
||||
$files = array_merge($files, $this->globRecursive($pattern));
|
||||
}
|
||||
}
|
||||
|
||||
// 설치 마법사 (SPA 부팅 전 화면)
|
||||
$files[] = base_path('public/install/index.php');
|
||||
|
||||
// 테스트 파일은 제외한다 — 차단 대상 URL 을 픽스처로 담는 것이 그 테스트의 일이다
|
||||
$files = array_filter($files, function (string $path): bool {
|
||||
$normalized = str_replace('\\', '/', $path);
|
||||
|
||||
return is_file($path)
|
||||
&& ! str_contains($normalized, '/__tests__/')
|
||||
&& ! str_contains($normalized, '/tests/');
|
||||
});
|
||||
|
||||
return array_values(array_unique($files));
|
||||
}
|
||||
|
||||
/**
|
||||
* `**` 를 포함한 패턴을 재귀적으로 해석합니다.
|
||||
*
|
||||
* @param string $pattern 상대 패턴
|
||||
* @return array<int, string> 절대 경로 목록
|
||||
*/
|
||||
private function globRecursive(string $pattern): array
|
||||
{
|
||||
[$prefix, $suffix] = explode('**', $pattern, 2);
|
||||
$suffix = ltrim($suffix, '/');
|
||||
$results = [];
|
||||
|
||||
foreach (glob(base_path($prefix), GLOB_ONLYDIR | GLOB_BRACE) as $root) {
|
||||
$iterator = new \RecursiveIteratorIterator(
|
||||
new \RecursiveDirectoryIterator($root, \FilesystemIterator::SKIP_DOTS)
|
||||
);
|
||||
|
||||
foreach ($iterator as $file) {
|
||||
if (! $file->isFile()) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (fnmatch($suffix, $file->getBasename(), FNM_CASEFOLD)
|
||||
|| fnmatch(basename($suffix), $file->getBasename(), FNM_CASEFOLD)) {
|
||||
$results[] = $file->getPathname();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return $results;
|
||||
}
|
||||
|
||||
/**
|
||||
* 파일이 속한 확장의 manifest 에서 사유가 선언된 신뢰 호스트를 읽습니다.
|
||||
*
|
||||
* @param string $path 파일 절대 경로
|
||||
* @return array<int, string> 사유가 함께 선언된 호스트 목록
|
||||
*/
|
||||
private function declaredHostsWithReason(string $path): array
|
||||
{
|
||||
$relative = str_replace('\\', '/', substr($path, strlen(base_path()) + 1));
|
||||
|
||||
if (! preg_match('#^(modules|plugins|templates)/_bundled/([^/]+)/#', $relative, $m)) {
|
||||
return [];
|
||||
}
|
||||
|
||||
$manifestName = match ($m[1]) {
|
||||
'modules' => 'module.json',
|
||||
'plugins' => 'plugin.json',
|
||||
default => 'template.json',
|
||||
};
|
||||
|
||||
$manifestPath = base_path("{$m[1]}/_bundled/{$m[2]}/{$manifestName}");
|
||||
|
||||
if (! is_file($manifestPath)) {
|
||||
return [];
|
||||
}
|
||||
|
||||
$manifest = json_decode((string) file_get_contents($manifestPath), true) ?: [];
|
||||
$hosts = $manifest['trusted_script_hosts'] ?? [];
|
||||
$reasons = $manifest['trusted_script_hosts_reason'] ?? [];
|
||||
|
||||
if (! is_array($hosts) || ! is_array($reasons)) {
|
||||
return [];
|
||||
}
|
||||
|
||||
// 사유 없이 선언된 호스트는 예외로 인정하지 않는다 — 왜 외부로 나가는지가
|
||||
// 코드에 남아야 "자체 제공이 원칙, 예외는 근거와 함께" 가 강제된다.
|
||||
return array_values(array_filter(
|
||||
$hosts,
|
||||
fn ($host) => is_string($host) && ! empty($reasons[$host])
|
||||
));
|
||||
}
|
||||
|
||||
/**
|
||||
* @scenario asset_class=vendored, outcome=loaded
|
||||
* @scenario asset_class=translation, outcome=loaded
|
||||
* @scenario asset_class=service_sdk, outcome=loaded
|
||||
*
|
||||
* @effects no_third_party_request_on_page_load, runtime_asset_served_same_origin
|
||||
*/
|
||||
#[Test]
|
||||
public function 런타임_소스에_외부_자산_ur_l_이_없다(): void
|
||||
{
|
||||
// 구분자로 `~` 를 쓴다 — 패턴 안에 URL 프래그먼트(`#`)가 들어가 `#` 구분자와 충돌한다
|
||||
// 확장자 뒤에 경계를 둔다 — 새 탭으로 여는 외부 페이지(`.jsp`)가 `.js` 로 오탐되면
|
||||
// 자산이 아닌 링크를 결함으로 세게 된다 (KG이니시스 영수증 링크가 그 예다).
|
||||
$pattern = '~(?:https?:)?//([A-Za-z0-9._-]+)/[^"\'\s)]*\.(?:'.self::ASSET_EXTENSIONS.')(?![A-Za-z0-9])(?:[?\#][^"\'\s)]*)?~i';
|
||||
$violations = [];
|
||||
|
||||
foreach ($this->runtimeSources() as $path) {
|
||||
$contents = (string) file_get_contents($path);
|
||||
|
||||
if (! preg_match_all($pattern, $contents, $matches, PREG_SET_ORDER)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$allowedHosts = array_merge(
|
||||
$this->declaredHostsWithReason($path),
|
||||
array_keys(self::CORE_EXEMPTIONS)
|
||||
);
|
||||
|
||||
foreach ($matches as $match) {
|
||||
[$url, $host] = $match;
|
||||
|
||||
if (in_array($host, $allowedHosts, true)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
// 주석에 남은 설명 문구는 대상이 아니다 — 실제 로드 경로만 본다
|
||||
if ($this->isInsideComment($contents, $url)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$violations[] = str_replace('\\', '/', substr($path, strlen(base_path()) + 1)).' → '.$url;
|
||||
}
|
||||
}
|
||||
|
||||
$this->assertSame(
|
||||
[],
|
||||
array_values(array_unique($violations)),
|
||||
"구동 자산을 외부 origin 에서 불러오는 지점이 남아 있습니다.\n"
|
||||
."자체 제공으로 바꾸거나, 자체 제공이 불가능하면 manifest 에\n"
|
||||
."`trusted_script_hosts` + `trusted_script_hosts_reason` 을 함께 선언하세요.\n"
|
||||
.implode("\n", array_unique($violations))
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* URL 이 주석 안에 있는지 판정합니다.
|
||||
*
|
||||
* @param string $contents 파일 내용
|
||||
* @param string $url 검출된 URL
|
||||
* @return bool 주석 안이면 true
|
||||
*/
|
||||
private function isInsideComment(string $contents, string $url): bool
|
||||
{
|
||||
foreach (explode("\n", $contents) as $line) {
|
||||
if (! str_contains($line, $url)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$trimmed = ltrim($line);
|
||||
|
||||
if (str_starts_with($trimmed, '//')
|
||||
|| str_starts_with($trimmed, '*')
|
||||
|| str_starts_with($trimmed, '/*')
|
||||
|| str_starts_with($trimmed, '#')) {
|
||||
continue;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function 신뢰_호스트를_선언한_확장은_사유도_함께_선언한다(): void
|
||||
{
|
||||
$missing = [];
|
||||
|
||||
foreach (['modules' => 'module.json', 'plugins' => 'plugin.json', 'templates' => 'template.json'] as $type => $name) {
|
||||
foreach (glob(base_path("{$type}/_bundled/*/{$name}")) as $manifestPath) {
|
||||
$manifest = json_decode((string) file_get_contents($manifestPath), true) ?: [];
|
||||
$hosts = $manifest['trusted_script_hosts'] ?? [];
|
||||
|
||||
if (! is_array($hosts) || $hosts === []) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$reasons = $manifest['trusted_script_hosts_reason'] ?? [];
|
||||
|
||||
foreach ($hosts as $host) {
|
||||
if (empty($reasons[$host])) {
|
||||
$missing[] = str_replace('\\', '/', substr($manifestPath, strlen(base_path()) + 1)).' → '.$host;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
$this->assertSame(
|
||||
[],
|
||||
$missing,
|
||||
"외부 호스트를 신뢰 출처로 선언했으면 그 사유도 manifest 에 남겨야 합니다.\n"
|
||||
.'`trusted_script_hosts_reason` 에 호스트별 사유를 적으세요.'."\n"
|
||||
.implode("\n", $missing)
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @effects template_external_asset_field_resolves_to_asset_url
|
||||
*/
|
||||
#[Test]
|
||||
public function 템플릿_externals_는_자체_제공_경로를_쓴다(): void
|
||||
{
|
||||
$violations = [];
|
||||
|
||||
foreach (glob(base_path('templates/_bundled/*/template.json')) as $manifestPath) {
|
||||
$manifest = json_decode((string) file_get_contents($manifestPath), true) ?: [];
|
||||
|
||||
foreach ($manifest['externals'] ?? [] as $external) {
|
||||
if (! is_array($external) || ! isset($external['url'])) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (preg_match('#^(https?:)?//#i', (string) $external['url']) === 1) {
|
||||
$violations[] = str_replace('\\', '/', substr($manifestPath, strlen(base_path()) + 1))
|
||||
.' → '.$external['id'].' ('.$external['url'].')';
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
$this->assertSame(
|
||||
[],
|
||||
$violations,
|
||||
"템플릿 externals 는 `asset`(자체 제공 경로) 으로 선언해야 합니다.\n"
|
||||
.implode("\n", $violations)
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,215 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\Feature\Extension;
|
||||
|
||||
use App\Extension\Helpers\ExtensionPendingHelper;
|
||||
use Illuminate\Support\Facades\File;
|
||||
use PHPUnit\Framework\Attributes\Test;
|
||||
use ReflectionMethod;
|
||||
use Tests\TestCase;
|
||||
|
||||
/**
|
||||
* 사용자 추가 에셋(`custom/`)의 확장 교체 생존 테스트
|
||||
*
|
||||
* `custom/` 은 운영자 소유 자리다. 확장 업데이트가 그것을 덮어쓰면 업데이트할 때마다
|
||||
* 운영자가 넣은 파일이 사라지고, 그 사실이 어디에도 남지 않는다 — 파일이 조용히
|
||||
* 없어질 뿐이다. 포럼 질문("custom.css 를 어디에 두나요")의 커뮤니티 답변대로
|
||||
* `src/styles/` 에 넣으면 정확히 그 일이 벌어진다.
|
||||
*
|
||||
* 교체 경로가 **둘**이라는 점이 핵심이다:
|
||||
* 1. 디렉토리 rename (정상 경로)
|
||||
* 2. 제자리 파일 동기화 (Windows 에서 하위 트리에 열린 핸들이 있어 rename 이 막힐 때)
|
||||
*
|
||||
* 한쪽만 보존하면 잠금 상황에서만 조용히 사라진다 — 재현이 어려운 결함이 된다.
|
||||
*
|
||||
* DB 를 쓰지 않는다 — 파일 교체 동작만 검증한다.
|
||||
*/
|
||||
class CustomAssetPreservationTest extends TestCase
|
||||
{
|
||||
/** 테스트 작업 디렉토리 */
|
||||
private string $workspace;
|
||||
|
||||
/** 기존 활성 디렉토리 */
|
||||
private string $activePath;
|
||||
|
||||
/** 새 배포본 소스 */
|
||||
private string $sourcePath;
|
||||
|
||||
protected function setUp(): void
|
||||
{
|
||||
parent::setUp();
|
||||
|
||||
$this->workspace = storage_path('framework/testing/custom-preserve-'.uniqid());
|
||||
$this->activePath = $this->workspace.'/plugins/g7test-preserve';
|
||||
$this->sourcePath = $this->workspace.'/plugins/_bundled/g7test-preserve';
|
||||
|
||||
File::ensureDirectoryExists($this->activePath.'/custom');
|
||||
File::ensureDirectoryExists($this->activePath.'/dist/js');
|
||||
File::ensureDirectoryExists($this->sourcePath.'/dist/js');
|
||||
|
||||
// 운영자가 넣은 파일
|
||||
File::put($this->activePath.'/custom/custom.css', 'body{--operator:1}');
|
||||
File::put($this->activePath.'/custom/assets.json', '{"assets":[]}');
|
||||
|
||||
// 구버전 산출물 (교체 시 정리되어야 한다)
|
||||
File::put($this->activePath.'/dist/js/old-hash.js', '// old');
|
||||
|
||||
// 새 배포본
|
||||
File::put($this->sourcePath.'/dist/js/new-hash.js', '// new');
|
||||
File::put($this->sourcePath.'/plugin.json', '{"identifier":"g7test-preserve"}');
|
||||
}
|
||||
|
||||
protected function tearDown(): void
|
||||
{
|
||||
File::deleteDirectory($this->workspace);
|
||||
|
||||
parent::tearDown();
|
||||
}
|
||||
|
||||
/**
|
||||
* 보존 결과를 단언합니다.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
private function assertPreserved(): void
|
||||
{
|
||||
$this->assertFileExists(
|
||||
$this->activePath.'/custom/custom.css',
|
||||
'운영자가 넣은 파일이 확장 교체에 사라졌습니다.'
|
||||
);
|
||||
$this->assertSame(
|
||||
'body{--operator:1}',
|
||||
File::get($this->activePath.'/custom/custom.css'),
|
||||
'운영자 파일의 내용이 새 배포본으로 덮어써졌습니다.'
|
||||
);
|
||||
$this->assertFileExists($this->activePath.'/custom/assets.json');
|
||||
}
|
||||
|
||||
/**
|
||||
* @scenario custom_source=convention_scan, custom_asset=css
|
||||
*
|
||||
* @effects custom_dir_survives_extension_update
|
||||
*/
|
||||
#[Test]
|
||||
public function rename_경로에서_운영자_파일이_보존된다(): void
|
||||
{
|
||||
ExtensionPendingHelper::copyToActive($this->sourcePath, $this->activePath);
|
||||
|
||||
$this->assertPreserved();
|
||||
$this->assertFileExists($this->activePath.'/dist/js/new-hash.js');
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function rename_경로에서도_구버전_산출물은_정리된다(): void
|
||||
{
|
||||
// 보존 계층이 stale 산출물 청소를 무력화하면 배포본이 계속 부푼다
|
||||
ExtensionPendingHelper::copyToActive($this->sourcePath, $this->activePath);
|
||||
|
||||
$this->assertFileDoesNotExist($this->activePath.'/dist/js/old-hash.js');
|
||||
}
|
||||
|
||||
/**
|
||||
* @scenario custom_source=convention_scan, custom_asset=css
|
||||
*
|
||||
* @effects custom_dir_survives_inplace_sync_fallback
|
||||
*/
|
||||
#[Test]
|
||||
public function 제자리_동기화_폴백_경로에서도_운영자_파일이_보존된다(): void
|
||||
{
|
||||
// rename 이 막히는 상황(Windows 잠금)의 폴백 경로를 직접 태운다
|
||||
$sync = new ReflectionMethod(ExtensionPendingHelper::class, 'syncDirectoryContents');
|
||||
$sync->setAccessible(true);
|
||||
|
||||
// 스테이징에는 새 배포본 + 운영자 파일이 함께 있어야 한다 (copyToActive 가 그렇게 만든다)
|
||||
$staging = $this->workspace.'/staging';
|
||||
File::copyDirectory($this->sourcePath, $staging);
|
||||
File::copyDirectory($this->activePath.'/custom', $staging.'/custom');
|
||||
|
||||
$sync->invoke(null, $staging, $this->activePath, null);
|
||||
|
||||
$this->assertPreserved();
|
||||
$this->assertFileExists($this->activePath.'/dist/js/new-hash.js');
|
||||
$this->assertFileDoesNotExist($this->activePath.'/dist/js/old-hash.js');
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function 스테이징에_운영자_디렉토리가_없어도_정리_대상이_되지_않는다(): void
|
||||
{
|
||||
// 소스에 없는 항목을 지우는 정리 로직이 custom/ 을 지우면 안 된다
|
||||
$stale = new ReflectionMethod(ExtensionPendingHelper::class, 'removeStaleEntries');
|
||||
$stale->setAccessible(true);
|
||||
|
||||
$failures = [];
|
||||
$stale->invokeArgs(null, [$this->sourcePath, $this->activePath, &$failures, true]);
|
||||
|
||||
$this->assertPreserved();
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function 새_배포본이_같은_이름의_디렉토리를_담아도_운영자_파일이_우선한다(): void
|
||||
{
|
||||
File::ensureDirectoryExists($this->sourcePath.'/custom');
|
||||
File::put($this->sourcePath.'/custom/custom.css', 'body{--from-vendor:1}');
|
||||
|
||||
ExtensionPendingHelper::copyToActive($this->sourcePath, $this->activePath);
|
||||
|
||||
$this->assertPreserved();
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function 대상_디렉토리가_없으면_그냥_복사한다(): void
|
||||
{
|
||||
$fresh = $this->workspace.'/plugins/g7test-fresh';
|
||||
|
||||
ExtensionPendingHelper::copyToActive($this->sourcePath, $fresh);
|
||||
|
||||
$this->assertFileExists($fresh.'/dist/js/new-hash.js');
|
||||
$this->assertDirectoryDoesNotExist($fresh.'/custom');
|
||||
}
|
||||
|
||||
/**
|
||||
* 삭제는 보존이 아니라 보관이다 — 그리고 보관 사실은 호출자에게 도달해야 한다.
|
||||
*
|
||||
* 사본 경로가 로그에만 남으면 운영자는 로그를 뒤지지 않는 한 사본의 존재를 모른다.
|
||||
* 그러면 "확장을 지웠더니 내 CSS 가 사라졌다" 와 구분되지 않는다.
|
||||
*
|
||||
* @effects custom_dir_archived_on_uninstall
|
||||
*/
|
||||
#[Test]
|
||||
public function 삭제는_운영자_파일을_보관하고_그_경로를_반환한다(): void
|
||||
{
|
||||
$archived = ExtensionPendingHelper::deleteExtensionDirectory(
|
||||
$this->workspace.'/plugins',
|
||||
'g7test-preserve'
|
||||
);
|
||||
|
||||
// 확장은 실제로 지워진다 — 보존이 아니라 보관이다.
|
||||
$this->assertDirectoryDoesNotExist($this->activePath);
|
||||
|
||||
// 보관 경로가 반환값으로 올라온다.
|
||||
$this->assertCount(1, $archived, '보관된 디렉토리 1건이 반환되어야 한다');
|
||||
$this->assertSame('custom', $archived[0]['directory']);
|
||||
|
||||
// 반환된 경로에 운영자 파일이 실제로 있다 — 경로만 돌려주고 사본이 없으면 무의미하다.
|
||||
$this->assertFileExists($archived[0]['archive'].'/custom.css');
|
||||
$this->assertSame('body{--operator:1}', File::get($archived[0]['archive'].'/custom.css'));
|
||||
|
||||
File::deleteDirectory(dirname($archived[0]['archive']));
|
||||
}
|
||||
|
||||
/**
|
||||
* 보관할 것이 없으면 빈 배열이다 — 호출부가 "사본이 있다" 고 오인해선 안 된다.
|
||||
*/
|
||||
#[Test]
|
||||
public function 운영자_파일이_없으면_보관_결과가_비어_있다(): void
|
||||
{
|
||||
File::deleteDirectory($this->activePath.'/custom');
|
||||
|
||||
$archived = ExtensionPendingHelper::deleteExtensionDirectory(
|
||||
$this->workspace.'/plugins',
|
||||
'g7test-preserve'
|
||||
);
|
||||
|
||||
$this->assertSame([], $archived);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,159 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\Feature\Extension;
|
||||
|
||||
use App\Extension\Helpers\ExtensionInstallRollbackHelper;
|
||||
use Illuminate\Support\Facades\File;
|
||||
use Tests\TestCase;
|
||||
|
||||
/**
|
||||
* 회귀: 실패한 설치가 활성 디렉토리를 고아로 남기지 않는다
|
||||
*
|
||||
* 설치 흐름은 원본(`_pending`/`_bundled`)을 활성 디렉토리로 **먼저 복사한 뒤** 확장을
|
||||
* 로드해 코어 버전·의존성·언어 경로를 검증한다. 검증에 로드된 인스턴스가 필요해 순서를
|
||||
* 뒤집을 수 없는데, 검증이 실패하면 방금 만든 디렉토리가 그대로 남았다.
|
||||
*
|
||||
* 남은 디렉토리는 DB 행이 없어 `plugin:list` 에도 뜨지 않고 번들 병합에도 참여하지 않는다.
|
||||
* 오류도 경고도 없이 디스크만 점유하는 고아이고, 다음 설치 시도가 그것을 "이미 있는
|
||||
* 설치본" 으로 보고 원본 복사를 건너뛸 수 있다.
|
||||
*/
|
||||
class FailedInstallRollsBackActiveDirectoryTest extends TestCase
|
||||
{
|
||||
private string $sandbox;
|
||||
|
||||
protected function setUp(): void
|
||||
{
|
||||
parent::setUp();
|
||||
|
||||
$this->sandbox = storage_path('framework/testing/install-rollback-'.uniqid());
|
||||
File::ensureDirectoryExists($this->sandbox);
|
||||
}
|
||||
|
||||
protected function tearDown(): void
|
||||
{
|
||||
File::deleteDirectory($this->sandbox);
|
||||
|
||||
parent::tearDown();
|
||||
}
|
||||
|
||||
/**
|
||||
* 이번 설치가 만든 디렉토리는 제거한다.
|
||||
*/
|
||||
public function test_directory_created_by_this_install_is_removed(): void
|
||||
{
|
||||
$active = $this->sandbox.DIRECTORY_SEPARATOR.'vendor-sample';
|
||||
File::ensureDirectoryExists($active);
|
||||
File::put($active.DIRECTORY_SEPARATOR.'plugin.json', '{}');
|
||||
|
||||
$removed = ExtensionInstallRollbackHelper::removeIfCreatedByThisInstall(
|
||||
$active,
|
||||
existedBefore: false,
|
||||
identifier: 'vendor-sample',
|
||||
type: 'plugin',
|
||||
);
|
||||
|
||||
$this->assertTrue($removed);
|
||||
$this->assertDirectoryDoesNotExist($active);
|
||||
}
|
||||
|
||||
/**
|
||||
* 이미 있던 설치본은 건드리지 않는다.
|
||||
*
|
||||
* `--force` 재설치가 검증에서 실패한 경우다. 그 디렉토리는 운영자의 기존 설치본이므로
|
||||
* 여기서 지우면 실패한 재설치가 멀쩡히 돌던 확장을 없애버린다.
|
||||
*/
|
||||
public function test_preexisting_directory_is_left_untouched(): void
|
||||
{
|
||||
$active = $this->sandbox.DIRECTORY_SEPARATOR.'vendor-existing';
|
||||
File::ensureDirectoryExists($active);
|
||||
File::put($active.DIRECTORY_SEPARATOR.'plugin.json', '{"keep":true}');
|
||||
|
||||
$removed = ExtensionInstallRollbackHelper::removeIfCreatedByThisInstall(
|
||||
$active,
|
||||
existedBefore: true,
|
||||
identifier: 'vendor-existing',
|
||||
type: 'plugin',
|
||||
);
|
||||
|
||||
$this->assertFalse($removed);
|
||||
$this->assertDirectoryExists($active);
|
||||
$this->assertStringContainsString('keep', File::get($active.DIRECTORY_SEPARATOR.'plugin.json'));
|
||||
}
|
||||
|
||||
/**
|
||||
* 디렉토리가 아예 없으면 조용히 넘어간다 (복사 이전에 실패한 경우).
|
||||
*/
|
||||
public function test_absent_directory_is_a_no_op(): void
|
||||
{
|
||||
$this->assertFalse(ExtensionInstallRollbackHelper::removeIfCreatedByThisInstall(
|
||||
$this->sandbox.DIRECTORY_SEPARATOR.'never-created',
|
||||
existedBefore: false,
|
||||
identifier: 'never-created',
|
||||
type: 'module',
|
||||
));
|
||||
}
|
||||
|
||||
/**
|
||||
* 세 관리자의 설치 경로가 모두 이 되돌리기를 경유한다.
|
||||
*
|
||||
* 한 곳만 배선하면 나머지 두 유형에서 같은 고아가 계속 생기고, 그 사실은 오류로
|
||||
* 드러나지 않는다. 배선이 나중에 조용히 빠지는 것도 여기서 막는다.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function test_all_three_managers_wire_the_rollback(): void
|
||||
{
|
||||
$targets = [
|
||||
[app_path('Extension/PluginManager.php'), 'installPlugin'],
|
||||
[app_path('Extension/ModuleManager.php'), 'installModule'],
|
||||
[app_path('Extension/TemplateManager.php'), 'installTemplate'],
|
||||
];
|
||||
|
||||
foreach ($targets as [$path, $method]) {
|
||||
$source = File::get($path);
|
||||
$body = $this->methodBody($source, $method);
|
||||
|
||||
$this->assertNotSame('', $body, "{$method} 본문을 찾지 못했습니다.");
|
||||
$this->assertStringContainsString(
|
||||
'ExtensionInstallRollbackHelper::removeIfCreatedByThisInstall',
|
||||
$body,
|
||||
"{$method} 이 실패 시 활성 디렉토리를 되돌리지 않습니다."
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 소스에서 메서드 본문을 잘라냅니다 (중괄호 균형).
|
||||
*
|
||||
* @param string $source PHP 소스 전문
|
||||
* @param string $method 메서드명
|
||||
* @return string 본문 (못 찾으면 빈 문자열)
|
||||
*/
|
||||
private function methodBody(string $source, string $method): string
|
||||
{
|
||||
$start = strpos($source, 'public function '.$method.'(');
|
||||
|
||||
if ($start === false) {
|
||||
return '';
|
||||
}
|
||||
|
||||
$depth = 0;
|
||||
$began = false;
|
||||
$length = strlen($source);
|
||||
|
||||
for ($i = $start; $i < $length; $i++) {
|
||||
if ($source[$i] === '{') {
|
||||
$depth++;
|
||||
$began = true;
|
||||
} elseif ($source[$i] === '}') {
|
||||
$depth--;
|
||||
|
||||
if ($began && $depth === 0) {
|
||||
return substr($source, $start, $i - $start + 1);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return '';
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,438 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\Feature\Extension;
|
||||
|
||||
use App\Console\Commands\Concerns\PrunesBuildOutput;
|
||||
use App\Contracts\Repositories\PluginRepositoryInterface;
|
||||
use App\Contracts\Repositories\TemplateRepositoryInterface;
|
||||
use App\Enums\ExtensionStatus;
|
||||
use App\Http\Requests\Public\Template\ServeTemplateAssetRequest;
|
||||
use App\Models\Plugin;
|
||||
use App\Models\Template;
|
||||
use App\Rules\SafeTemplatePath;
|
||||
use App\Seo\SeoRenderer;
|
||||
use App\Services\ExtensionStaticCacheService;
|
||||
use App\Services\PluginService;
|
||||
use App\Services\TemplateService;
|
||||
use App\Support\AssetUrl;
|
||||
use App\Support\CustomAssets;
|
||||
use Illuminate\Routing\Route;
|
||||
use Illuminate\Support\Facades\File;
|
||||
use Mockery;
|
||||
use PHPUnit\Framework\Attributes\Test;
|
||||
use Tests\TestCase;
|
||||
|
||||
/**
|
||||
* 동봉 자산 · 사용자 추가 자산의 서빙 테스트
|
||||
*
|
||||
* 확장이 담은 구동 자산(`dist/vendor/**`)과 운영자가 넣은 자산(`custom/**`)이 실제로
|
||||
* 서빙되는지, 그리고 **비활성 확장은 서빙되지 않는지**를 본다. 자산 404 는 예외를
|
||||
* 남기지 않고 화면 기능만 사라지므로, 경로 규약이 어긋나면 배포본에서만 드러난다.
|
||||
*
|
||||
* DB 대신 저장소를 모킹한다 — 검증 대상은 경로 해석과 활성 게이트이지 DB 스키마가 아니다.
|
||||
*/
|
||||
class VendoredAssetServingTest extends TestCase
|
||||
{
|
||||
/** 테스트용 가짜 템플릿 식별자 */
|
||||
private const FAKE_TEMPLATE = 'g7test-serving';
|
||||
|
||||
/** 테스트용 가짜 플러그인 식별자 */
|
||||
private const FAKE_PLUGIN = 'g7test-serving-plugin';
|
||||
|
||||
protected function setUp(): void
|
||||
{
|
||||
parent::setUp();
|
||||
|
||||
CustomAssets::flushCache();
|
||||
|
||||
// 템플릿: dist/vendor(동봉) + custom(운영자)
|
||||
File::ensureDirectoryExists(base_path('templates/'.self::FAKE_TEMPLATE.'/dist/vendor/lib/1.0.0/css'));
|
||||
File::put(base_path('templates/'.self::FAKE_TEMPLATE.'/dist/vendor/lib/1.0.0/css/lib.css'), '/* vendored */');
|
||||
File::ensureDirectoryExists(base_path('templates/'.self::FAKE_TEMPLATE.'/custom'));
|
||||
File::put(base_path('templates/'.self::FAKE_TEMPLATE.'/custom/custom.css'), '/* operator */');
|
||||
File::put(base_path('templates/'.self::FAKE_TEMPLATE.'/custom/notes.txt'), 'not an asset');
|
||||
|
||||
// 플러그인: 확장 루트 기준
|
||||
File::ensureDirectoryExists(base_path('plugins/'.self::FAKE_PLUGIN.'/dist/vendor/lib/1.0.0'));
|
||||
File::put(base_path('plugins/'.self::FAKE_PLUGIN.'/dist/vendor/lib/1.0.0/lib.js'), '// vendored');
|
||||
}
|
||||
|
||||
protected function tearDown(): void
|
||||
{
|
||||
File::deleteDirectory(base_path('templates/'.self::FAKE_TEMPLATE));
|
||||
File::deleteDirectory(base_path('plugins/'.self::FAKE_PLUGIN));
|
||||
CustomAssets::flushCache();
|
||||
AssetUrl::forceMode(null);
|
||||
Mockery::close();
|
||||
|
||||
parent::tearDown();
|
||||
}
|
||||
|
||||
/**
|
||||
* 저장되지 않은 템플릿 모델을 만듭니다.
|
||||
*
|
||||
* @param string $status 확장 상태 값
|
||||
* @return Template 모델 인스턴스
|
||||
*/
|
||||
private function makeTemplate(string $status): Template
|
||||
{
|
||||
$template = new Template;
|
||||
$template->identifier = self::FAKE_TEMPLATE;
|
||||
$template->status = $status;
|
||||
|
||||
return $template;
|
||||
}
|
||||
|
||||
/**
|
||||
* 저장되지 않은 플러그인 모델을 만듭니다.
|
||||
*
|
||||
* @param string $status 확장 상태 값
|
||||
* @return Plugin 모델 인스턴스
|
||||
*/
|
||||
private function makePlugin(string $status): Plugin
|
||||
{
|
||||
$plugin = new Plugin;
|
||||
$plugin->identifier = self::FAKE_PLUGIN;
|
||||
$plugin->status = $status;
|
||||
|
||||
return $plugin;
|
||||
}
|
||||
|
||||
/**
|
||||
* 지정 상태의 템플릿을 돌려주는 서비스를 만듭니다.
|
||||
*
|
||||
* @param string $status 확장 상태 값
|
||||
* @return TemplateService 모킹된 저장소를 물린 서비스
|
||||
*/
|
||||
private function templateService(string $status): TemplateService
|
||||
{
|
||||
$repository = Mockery::mock(TemplateRepositoryInterface::class);
|
||||
$repository->shouldReceive('findByIdentifier')
|
||||
->andReturn($this->makeTemplate($status));
|
||||
|
||||
$this->app->instance(TemplateRepositoryInterface::class, $repository);
|
||||
|
||||
return $this->app->make(TemplateService::class);
|
||||
}
|
||||
|
||||
/**
|
||||
* 지정 상태의 플러그인을 돌려주는 서비스를 만듭니다.
|
||||
*
|
||||
* @param string $status 확장 상태 값
|
||||
* @return PluginService 모킹된 저장소를 물린 서비스
|
||||
*/
|
||||
private function pluginService(string $status): PluginService
|
||||
{
|
||||
$repository = Mockery::mock(PluginRepositoryInterface::class);
|
||||
$repository->shouldReceive('findByIdentifier')
|
||||
->andReturn($this->makePlugin($status));
|
||||
|
||||
$this->app->instance(PluginRepositoryInterface::class, $repository);
|
||||
|
||||
return $this->app->make(PluginService::class);
|
||||
}
|
||||
|
||||
/**
|
||||
* @scenario asset_class=vendored, outcome=loaded
|
||||
*
|
||||
* @effects runtime_asset_served_same_origin
|
||||
*/
|
||||
#[Test]
|
||||
public function 템플릿_동봉_자산은_dist_이하로_해석된다(): void
|
||||
{
|
||||
$result = $this->templateService(ExtensionStatus::Active->value)
|
||||
->getAssetFilePath(self::FAKE_TEMPLATE, 'vendor/lib/1.0.0/css/lib.css');
|
||||
|
||||
$this->assertTrue($result['success'], (string) ($result['error'] ?? ''));
|
||||
$this->assertSame('text/css', $result['mimeType']);
|
||||
$this->assertStringContainsString('/dist/vendor/', str_replace('\\', '/', $result['filePath']));
|
||||
}
|
||||
|
||||
/**
|
||||
* @scenario custom_source=convention_scan, custom_asset=css
|
||||
*
|
||||
* @effects custom_asset_loaded_after_extension_bundles
|
||||
*/
|
||||
#[Test]
|
||||
public function 템플릿_운영자_자산은_dist_밖의_custom_으로_해석된다(): void
|
||||
{
|
||||
// custom/ 은 빌드 산출물이 아니라 사람이 넣은 파일이고 확장 교체가 보존하는
|
||||
// 대상이라 dist/ 안에 둘 수 없다 — 서빙도 그 비대칭을 알아야 한다.
|
||||
$result = $this->templateService(ExtensionStatus::Active->value)
|
||||
->getAssetFilePath(self::FAKE_TEMPLATE, 'custom/custom.css');
|
||||
|
||||
$this->assertTrue($result['success'], (string) ($result['error'] ?? ''));
|
||||
$normalized = str_replace('\\', '/', $result['filePath']);
|
||||
$this->assertStringContainsString('/'.self::FAKE_TEMPLATE.'/custom/custom.css', $normalized);
|
||||
$this->assertStringNotContainsString('/dist/', $normalized);
|
||||
}
|
||||
|
||||
/**
|
||||
* @scenario custom_source=convention_scan
|
||||
*
|
||||
* @effects inactive_extension_custom_not_served
|
||||
*/
|
||||
#[Test]
|
||||
public function 비활성_템플릿의_자산은_서빙되지_않는다(): void
|
||||
{
|
||||
$result = $this->templateService(ExtensionStatus::Inactive->value)
|
||||
->getAssetFilePath(self::FAKE_TEMPLATE, 'vendor/lib/1.0.0/css/lib.css');
|
||||
|
||||
$this->assertFalse($result['success']);
|
||||
$this->assertSame('template_not_found', $result['error']);
|
||||
}
|
||||
|
||||
/**
|
||||
* @scenario custom_source=convention_scan, custom_asset=static_file
|
||||
*
|
||||
* @effects custom_disallowed_extension_blocked
|
||||
*/
|
||||
#[Test]
|
||||
public function 허용되지_않은_확장자는_서빙되지_않는다(): void
|
||||
{
|
||||
$result = $this->templateService(ExtensionStatus::Active->value)
|
||||
->getAssetFilePath(self::FAKE_TEMPLATE, 'custom/notes.txt');
|
||||
|
||||
$this->assertFalse($result['success']);
|
||||
$this->assertSame('file_type_not_allowed', $result['error']);
|
||||
}
|
||||
|
||||
/**
|
||||
* @effects custom_path_traversal_blocked
|
||||
*/
|
||||
#[Test]
|
||||
public function 경로_이탈은_차단된다(): void
|
||||
{
|
||||
$result = $this->templateService(ExtensionStatus::Active->value)
|
||||
->getAssetFilePath(self::FAKE_TEMPLATE, '../../../.env');
|
||||
|
||||
$this->assertFalse($result['success']);
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function 플러그인_동봉_자산은_확장_루트_기준으로_해석된다(): void
|
||||
{
|
||||
$result = $this->pluginService(ExtensionStatus::Active->value)
|
||||
->getAssetFilePath(self::FAKE_PLUGIN, 'dist/vendor/lib/1.0.0/lib.js');
|
||||
|
||||
$this->assertTrue($result['success'], (string) ($result['error'] ?? ''));
|
||||
$this->assertSame('application/javascript', $result['mimeType']);
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function 비활성_플러그인의_자산은_서빙되지_않는다(): void
|
||||
{
|
||||
$result = $this->pluginService(ExtensionStatus::Inactive->value)
|
||||
->getAssetFilePath(self::FAKE_PLUGIN, 'dist/vendor/lib/1.0.0/lib.js');
|
||||
|
||||
$this->assertFalse($result['success']);
|
||||
$this->assertSame('plugin_not_found', $result['error']);
|
||||
}
|
||||
|
||||
/**
|
||||
* @effects runtime_asset_served_same_origin
|
||||
*/
|
||||
#[Test]
|
||||
public function 자산_url_은_두_모드_모두_same_origin_이다(): void
|
||||
{
|
||||
foreach (['extension', 'extensionless'] as $mode) {
|
||||
AssetUrl::forceMode($mode);
|
||||
|
||||
$url = AssetUrl::templateAsset(self::FAKE_TEMPLATE, 'vendor/lib/1.0.0/css/lib.css', null, false);
|
||||
|
||||
$this->assertStringStartsWith('/api/templates/assets/', $url, "모드: {$mode}");
|
||||
$this->assertDoesNotMatchRegularExpression('#^(https?:)?//#', $url, "모드: {$mode}");
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 빌드 산출물 정리는 동봉 자산을 지우지 않는다.
|
||||
*
|
||||
* vite 의 `emptyOutDir` 를 끄고 정리 책임을 빌드 커맨드로 옮긴 이유가 이것이다.
|
||||
* 정리 규칙이 vendor 를 함께 지우면 매 빌드마다 동봉 자산이 사라지고, 그 사실은
|
||||
* 배포본을 열어보기 전까지 드러나지 않는다.
|
||||
*
|
||||
*
|
||||
* @effects vendored_asset_survives_rebuild
|
||||
*/
|
||||
#[Test]
|
||||
public function 빌드_산출물_정리가_동봉_자산을_보존한다(): void
|
||||
{
|
||||
$workspace = storage_path('framework/testing/prune-'.uniqid());
|
||||
File::ensureDirectoryExists($workspace.'/dist/js');
|
||||
File::ensureDirectoryExists($workspace.'/dist/vendor/lib/1.0.0');
|
||||
File::put($workspace.'/dist/js/old.js', '// old');
|
||||
File::put($workspace.'/dist/index.d.ts', 'export {};');
|
||||
File::put($workspace.'/dist/vendor/lib/1.0.0/lib.js', '// vendored');
|
||||
|
||||
$command = new class
|
||||
{
|
||||
use PrunesBuildOutput;
|
||||
|
||||
/**
|
||||
* 정리를 실행합니다.
|
||||
*
|
||||
* @param string $path 확장 루트 경로
|
||||
* @return array<int, string> 삭제된 항목
|
||||
*/
|
||||
public function run(string $path): array
|
||||
{
|
||||
return $this->pruneBuildOutput($path);
|
||||
}
|
||||
};
|
||||
|
||||
$removed = $command->run($workspace);
|
||||
|
||||
$this->assertFileExists($workspace.'/dist/vendor/lib/1.0.0/lib.js', '동봉 자산이 빌드 정리에 삭제됐습니다.');
|
||||
$this->assertFileDoesNotExist($workspace.'/dist/js/old.js');
|
||||
$this->assertFileDoesNotExist($workspace.'/dist/index.d.ts');
|
||||
$this->assertNotContains('vendor', $removed);
|
||||
|
||||
File::deleteDirectory($workspace);
|
||||
}
|
||||
|
||||
/**
|
||||
* 봇 렌더 경로의 스타일시트도 same-origin 자산으로 해석된다.
|
||||
*
|
||||
* 검색엔진이 보는 화면이 사용자 화면과 다른 자산을 쓰면, 자체 제공 전환이 그 경로에서만
|
||||
* 무력화된다 — 그리고 그 사실은 봇 화면을 따로 열어보기 전까지 드러나지 않는다.
|
||||
*
|
||||
*
|
||||
* @effects bot_render_path_uses_same_origin_asset, runtime_asset_served_same_origin
|
||||
*/
|
||||
#[Test]
|
||||
public function 봇_렌더_경로의_스타일시트가_same_origin_으로_해석된다(): void
|
||||
{
|
||||
$renderer = $this->app->make(SeoRenderer::class);
|
||||
$method = new \ReflectionMethod($renderer, 'resolveConfigStylesheets');
|
||||
$method->setAccessible(true);
|
||||
|
||||
$resolved = $method->invoke($renderer, [
|
||||
'vendor/font-awesome/6.4.0/css/all.inlined.css',
|
||||
'https://cdn.example.com/keep-as-is.css',
|
||||
'/api/templates/assets/t/already-absolute.css',
|
||||
], self::FAKE_TEMPLATE);
|
||||
|
||||
$this->assertStringStartsWith('/api/templates/assets/', $resolved[0]);
|
||||
$this->assertStringNotContainsString('/dist/', $resolved[0]);
|
||||
|
||||
// 절대 URL 과 이미 절대 경로인 항목은 그대로 둔다 (판정 범위를 넘지 않는다)
|
||||
$this->assertSame('https://cdn.example.com/keep-as-is.css', $resolved[1]);
|
||||
$this->assertSame('/api/templates/assets/t/already-absolute.css', $resolved[2]);
|
||||
}
|
||||
|
||||
/**
|
||||
* 자산 요청 검증의 컨테인먼트 기준이 **실제로 읽는 디렉토리**와 일치한다.
|
||||
*
|
||||
* 템플릿 자산은 `dist/` 이하가 기본이지만 `custom/` 만은 그 밖에 있다. 검증 기준을
|
||||
* `dist` 로 고정하면 `custom/**` 은 realpath 가 실패해 문자열 접두 비교로만 통과하므로,
|
||||
* 검증한 경로와 읽는 경로가 서로 다른 상태가 된다. 그 상태는 오류를 남기지 않는다 —
|
||||
* 룰이 realpath 존재를 필수로 바꾸는 순간 custom 서빙만 조용히 404 가 된다.
|
||||
*
|
||||
* @effects custom_asset_containment_base_matches_serving_root
|
||||
*/
|
||||
#[Test]
|
||||
public function 자산_요청_검증_기준_경로가_서빙_경로와_일치한다(): void
|
||||
{
|
||||
$base = fn (string $path): string => (function () {
|
||||
$rules = $this->rules();
|
||||
|
||||
foreach ($rules['path'] as $rule) {
|
||||
if ($rule instanceof SafeTemplatePath) {
|
||||
$property = new \ReflectionProperty($rule, 'basePath');
|
||||
$property->setAccessible(true);
|
||||
|
||||
return $property->getValue($rule);
|
||||
}
|
||||
}
|
||||
|
||||
return '';
|
||||
})->call($this->makeAssetRequest($path));
|
||||
|
||||
// custom/ 은 확장 루트 기준 — TemplateService::getAssetFilePath 와 같은 분기
|
||||
$this->assertSame(
|
||||
base_path('templates/'.self::FAKE_TEMPLATE),
|
||||
$base('custom/custom.css')
|
||||
);
|
||||
|
||||
// 그 외에는 종전대로 dist 기준
|
||||
$this->assertSame(
|
||||
base_path('templates/'.self::FAKE_TEMPLATE.'/dist'),
|
||||
$base('vendor/font-awesome/6.4.0/css/all.inlined.css')
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* 정적 게시가 운영자 소유 자산(`custom/`)을 자기 원본 루트로 싣는다.
|
||||
*
|
||||
* 게시하지 않으면 이 자산만 API 경로에 남는데, 그 경로에서는 CSS 내부 상대 `url()`
|
||||
* 이 해석되지 않는다 — 문서가 안내하는 "폰트·이미지를 custom/ 에 두고 상대 경로로
|
||||
* 참조" 가 성립하지 않는다. 정적 확장자 URL 은 public 아래 실제 파일일 때만 200 이
|
||||
* 되므로 게시가 유일한 성립 조건이다.
|
||||
*
|
||||
* @effects custom_asset_published_with_extension_assets
|
||||
*/
|
||||
#[Test]
|
||||
public function 정적_게시가_운영자_자산을_자기_루트로_싣는다(): void
|
||||
{
|
||||
$service = $this->app->make(ExtensionStaticCacheService::class);
|
||||
$method = new \ReflectionMethod($service, 'publishTemplate');
|
||||
$method->setAccessible(true);
|
||||
|
||||
// publishTemplate 이 custom 원본을 **자기 호출로** 게시하는지 — 소스에 두 번째
|
||||
// publishDistAssets 호출이 있고 그 대상이 custom 루트임을 구조로 확인한다.
|
||||
$source = file_get_contents(app_path('Services/ExtensionStaticCacheService.php'));
|
||||
|
||||
$this->assertStringContainsString(
|
||||
'base_path("templates/{$identifier}/".CustomAssets::DIRECTORY)',
|
||||
$source,
|
||||
'custom 원본을 게시하는 호출이 없습니다 — 상대 url() 이 다시 깨집니다.'
|
||||
);
|
||||
$this->assertStringContainsString(
|
||||
'excludeCustom: false',
|
||||
$source,
|
||||
'custom 원본 게시에서 custom 제외 가드가 꺼져 있지 않습니다 — 하위가 조용히 누락됩니다.'
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* dist 원본으로 들어온 `custom/` 하위는 여전히 건너뛴다.
|
||||
*
|
||||
* 운영자 파일은 자기 원본 루트로 게시되므로, dist 를 통해 한 번 더 실리면 같은
|
||||
* 파일이 두 경로에 놓여 어느 쪽이 유효한지가 갈린다.
|
||||
*
|
||||
* @effects custom_asset_published_with_extension_assets
|
||||
*/
|
||||
#[Test]
|
||||
public function dist_원본에서는_운영자_디렉토리를_건너뛴다(): void
|
||||
{
|
||||
$service = $this->app->make(ExtensionStaticCacheService::class);
|
||||
$method = new \ReflectionMethod($service, 'isCustomAssetPath');
|
||||
$method->setAccessible(true);
|
||||
|
||||
$this->assertTrue($method->invoke($service, CustomAssets::DIRECTORY.'/custom.css'));
|
||||
$this->assertTrue($method->invoke($service, CustomAssets::DIRECTORY.'/fonts/MyFont.woff2'));
|
||||
$this->assertTrue($method->invoke($service, CustomAssets::DIRECTORY));
|
||||
|
||||
// 이름이 겹치는 정상 산출물까지 막지 않는다
|
||||
$this->assertFalse($method->invoke($service, 'css/custom-theme.css'));
|
||||
$this->assertFalse($method->invoke($service, 'vendor/font-awesome/6.4.0/css/all.inlined.css'));
|
||||
}
|
||||
|
||||
/**
|
||||
* 테스트용 자산 요청을 만든다 (라우트 파라미터 해석까지 재현).
|
||||
*/
|
||||
private function makeAssetRequest(string $path): ServeTemplateAssetRequest
|
||||
{
|
||||
$request = ServeTemplateAssetRequest::create(
|
||||
'/api/templates/assets/'.self::FAKE_TEMPLATE.'/'.$path
|
||||
);
|
||||
|
||||
$route = new Route(['GET'], '/api/templates/assets/{identifier}/{path}', []);
|
||||
$route->parameters = ['identifier' => self::FAKE_TEMPLATE, 'path' => $path];
|
||||
$request->setRouteResolver(fn () => $route);
|
||||
$request->merge(['identifier' => self::FAKE_TEMPLATE, 'path' => $path]);
|
||||
|
||||
return $request;
|
||||
}
|
||||
}
|
||||
@@ -433,4 +433,116 @@ class ModuleAssetServingTest extends TestCase
|
||||
// Assert: 200 서빙이 아니어야 한다 (검증 실패 → 리다이렉트)
|
||||
$this->assertNotSame(200, $browser->getStatusCode());
|
||||
}
|
||||
|
||||
/**
|
||||
* 운영자가 넣은 `custom/` 자산도 서빙된다.
|
||||
*
|
||||
* 모듈 자산 경로는 확장 루트 기준이라 `custom/` 이 별도 분기 없이 해석된다
|
||||
* (템플릿만 `dist/` 하드코딩이라 분기가 필요했다). 그 사실을 실제 HTTP 로 고정한다 —
|
||||
* 서비스 반환값만 보면 라우트·FormRequest 게이트를 통과하는지가 남는다.
|
||||
*
|
||||
* @scenario custom_source=convention_scan, custom_asset=css
|
||||
*
|
||||
* @effects custom_asset_loaded_after_extension_bundles
|
||||
*/
|
||||
public function test_serves_operator_custom_css_from_active_module(): void
|
||||
{
|
||||
// Arrange
|
||||
mkdir($this->testModulePath.'/custom', 0755, true);
|
||||
file_put_contents($this->testModulePath.'/custom/custom.css', '.operator { color: red; }');
|
||||
|
||||
// Act
|
||||
$response = $this->get('/api/modules/assets/test-module/custom/custom.css');
|
||||
|
||||
// Assert
|
||||
$response->assertStatus(200);
|
||||
$this->assertTrue(
|
||||
str_starts_with($response->headers->get('Content-Type'), 'text/css'),
|
||||
'Content-Type should start with text/css'
|
||||
);
|
||||
|
||||
// BinaryFileResponse 는 스트리밍이라 본문이 버퍼에 없다 — 어떤 파일을 서빙했는지로
|
||||
// 확인한다. dist/ 의 동명 파일이 아니라 custom/ 의 파일이어야 한다.
|
||||
$served = $response->baseResponse->getFile()->getRealPath();
|
||||
$this->assertSame(
|
||||
realpath($this->testModulePath.'/custom/custom.css'),
|
||||
$served,
|
||||
'custom/ 의 파일이 서빙되어야 한다'
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* 비활성 모듈의 `custom/` 은 서빙되지 않는다.
|
||||
*
|
||||
* 비활성 확장의 자산이 계속 응답하면 "껐는데 스타일이 남아 있다" 가 된다.
|
||||
*
|
||||
* @scenario custom_source=convention_scan
|
||||
*
|
||||
* @effects inactive_extension_custom_not_served
|
||||
*/
|
||||
public function test_returns_404_for_custom_asset_of_inactive_module(): void
|
||||
{
|
||||
// Arrange
|
||||
mkdir($this->testModulePath.'/custom', 0755, true);
|
||||
file_put_contents($this->testModulePath.'/custom/custom.css', '.operator { color: red; }');
|
||||
|
||||
$this->activeModule->update(['status' => ExtensionStatus::Inactive->value]);
|
||||
|
||||
// Act
|
||||
$response = $this->get('/api/modules/assets/test-module/custom/custom.css');
|
||||
|
||||
// Assert
|
||||
$response->assertStatus(404);
|
||||
}
|
||||
|
||||
/**
|
||||
* `custom/` 도 확장자 화이트리스트를 그대로 받는다 — 신규 확장자를 열지 않는다.
|
||||
*
|
||||
* @scenario custom_source=convention_scan, custom_asset=static_file
|
||||
*
|
||||
* @effects custom_disallowed_extension_blocked
|
||||
*/
|
||||
public function test_blocks_disallowed_file_type_under_custom(): void
|
||||
{
|
||||
// Arrange
|
||||
mkdir($this->testModulePath.'/custom', 0755, true);
|
||||
file_put_contents($this->testModulePath.'/custom/notes.txt', 'not an asset');
|
||||
|
||||
// Act
|
||||
$response = $this->getJson('/api/modules/assets/test-module/custom/notes.txt');
|
||||
|
||||
// Assert: FormRequest 의 확장자 게이트가 거부한다 (422)
|
||||
$response->assertStatus(422);
|
||||
$response->assertJsonValidationErrors('path');
|
||||
$this->assertStringNotContainsString('not an asset', $response->getContent());
|
||||
}
|
||||
|
||||
/**
|
||||
* `custom/` 자산도 ETag 로 304 를 돌려준다 — 운영자 파일이라고 캐시 계약이 다르지 않다.
|
||||
*
|
||||
* @scenario custom_source=convention_scan, custom_asset=css
|
||||
*
|
||||
* @effects custom_asset_url_busts_on_file_change
|
||||
*/
|
||||
public function test_returns_304_for_custom_asset_when_etag_matches(): void
|
||||
{
|
||||
// Arrange
|
||||
mkdir($this->testModulePath.'/custom', 0755, true);
|
||||
file_put_contents($this->testModulePath.'/custom/custom.css', '.operator { color: red; }');
|
||||
|
||||
$first = $this->get('/api/modules/assets/test-module/custom/custom.css');
|
||||
$first->assertStatus(200);
|
||||
|
||||
$etag = $first->headers->get('ETag');
|
||||
$this->assertNotNull($etag, 'custom 자산에도 ETag 가 있어야 한다');
|
||||
|
||||
// Act
|
||||
$second = $this->get(
|
||||
'/api/modules/assets/test-module/custom/custom.css',
|
||||
['If-None-Match' => $etag]
|
||||
);
|
||||
|
||||
// Assert
|
||||
$second->assertStatus(304);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,92 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\Feature\Permission;
|
||||
|
||||
use App\Enums\ExtensionOwnerType;
|
||||
use App\Models\Permission;
|
||||
use App\Models\Role;
|
||||
use App\Services\CoreUpdateService;
|
||||
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||||
use PHPUnit\Framework\Attributes\Test;
|
||||
use Tests\TestCase;
|
||||
|
||||
/**
|
||||
* 커스텀 자산 관리 권한(D34)의 정의·전파 테스트
|
||||
*
|
||||
* 신규 권한은 별도 업그레이드 스텝 없이 코어 업데이트의 표준 동기화
|
||||
* (`CoreUpdateService::syncCoreRolesAndPermissions`)로 기설치 사이트에 도달한다.
|
||||
* 그 전제가 사실인지를 여기서 잠근다 — 사실이 아니면 신규 권한이 정의만 되고
|
||||
* 아무에게도 부여되지 않아, 화면은 정상인데 **모두 403** 이 된다.
|
||||
*/
|
||||
class CustomAssetPermissionSyncTest extends TestCase
|
||||
{
|
||||
use RefreshDatabase;
|
||||
|
||||
private const IDENTIFIER = 'core.extensions.custom_assets.manage';
|
||||
|
||||
#[Test]
|
||||
public function 권한이_확장_공통_그룹에_정의되어_있다(): void
|
||||
{
|
||||
// 확장 타입을 가리지 않는 단일 권한이므로 특정 타입 그룹(core.templates 등)에
|
||||
// 두지 않는다. 한 타입 그룹에 있으면 나머지 두 타입에도 적용된다는 사실이
|
||||
// 화면 어디에도 드러나지 않는다.
|
||||
$categories = config('core.permissions.categories', []);
|
||||
$group = null;
|
||||
|
||||
foreach ($categories as $category) {
|
||||
if (($category['identifier'] ?? null) === 'core.extensions') {
|
||||
$group = $category;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
$this->assertNotNull($group, 'core.extensions 권한 그룹이 없습니다.');
|
||||
$this->assertContains(self::IDENTIFIER, array_column($group['permissions'] ?? [], 'identifier'));
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function 타입별_권한으로_쪼개지_않는다(): void
|
||||
{
|
||||
// 쪼개면 운영자가 셋을 다 부여해야 하고, "모듈 CSS 는 되는데 템플릿 CSS 는 안 되는"
|
||||
// 상태가 실질적 의미 없이 생긴다.
|
||||
$all = [];
|
||||
|
||||
foreach (config('core.permissions.categories', []) as $category) {
|
||||
foreach ($category['permissions'] ?? [] as $permission) {
|
||||
$all[] = $permission['identifier'];
|
||||
}
|
||||
}
|
||||
|
||||
foreach (['core.templates', 'core.modules', 'core.plugins'] as $prefix) {
|
||||
$this->assertNotContains($prefix.'.custom_assets.manage', $all);
|
||||
}
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function 레이아웃_편집_권한과_별개의_식별자다(): void
|
||||
{
|
||||
// 같은 권한을 재사용하면 "레이아웃을 고칠 수 있다" 가 곧 "사이트 전역 스크립트를
|
||||
// 올릴 수 있다" 가 된다. 그 확대는 오류 없이 일어난다.
|
||||
$this->assertNotSame('core.templates.layouts.edit', self::IDENTIFIER);
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function 표준_동기화가_권한을_만들고_관리자_역할에_부여한다(): void
|
||||
{
|
||||
// @scenario manage_actor=with_permission, manage_action=list
|
||||
// @effects custom_asset_permission_reaches_existing_sites_via_core_sync
|
||||
app(CoreUpdateService::class)->syncCoreRolesAndPermissions();
|
||||
|
||||
$permission = Permission::where('identifier', self::IDENTIFIER)->first();
|
||||
|
||||
$this->assertNotNull($permission, '표준 동기화가 신규 권한을 만들지 않았습니다.');
|
||||
$this->assertSame(ExtensionOwnerType::Core, $permission->extension_type);
|
||||
|
||||
$admin = Role::where('identifier', 'admin')->first();
|
||||
$this->assertNotNull($admin);
|
||||
$this->assertTrue(
|
||||
$admin->permissions()->where('identifier', self::IDENTIFIER)->exists(),
|
||||
'관리자 역할(all_leaf)에 신규 권한이 부여되지 않았습니다.'
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -2,8 +2,12 @@
|
||||
|
||||
namespace Tests\Feature\Services;
|
||||
|
||||
use App\Contracts\Repositories\ModuleRepositoryInterface;
|
||||
use App\Contracts\Repositories\PluginRepositoryInterface;
|
||||
use App\Contracts\Repositories\TemplateRepositoryInterface;
|
||||
use App\Enums\ExtensionStatus;
|
||||
use App\Models\Module;
|
||||
use App\Models\Plugin;
|
||||
use App\Models\Template;
|
||||
use App\Services\ExtensionBundleService;
|
||||
use App\Services\ExtensionStaticCacheService;
|
||||
@@ -71,11 +75,73 @@ class ExtensionStaticCacheServiceTest extends TestCase
|
||||
File::deleteDirectory(public_path('build/ext'));
|
||||
}
|
||||
|
||||
/**
|
||||
* 게시 실패 시 어느 분기에서 멈췄는지 보여 줍니다.
|
||||
*
|
||||
* `publishCurrent()` 는 여러 사유로 **조용히 false** 를 돌려준다(kill-switch·락 미획득·
|
||||
* 게시 중 예외). 단언 메시지가 "false is true" 뿐이면 그중 무엇인지 알 수 없어, 간헐적
|
||||
* 실패를 만났을 때 재현부터 다시 해야 한다.
|
||||
*
|
||||
* @param ExtensionStaticCacheService $svc 대상 서비스
|
||||
* @return string 진단 문자열
|
||||
*/
|
||||
private function publishDiagnostics(ExtensionStaticCacheService $svc): string
|
||||
{
|
||||
$v = ExtensionStaticCacheService::getExtensionCacheVersion();
|
||||
$lock = Cache::lock('ext-static.publish.'.$v, 1);
|
||||
$free = $lock->get();
|
||||
if ($free) {
|
||||
$lock->release();
|
||||
}
|
||||
$store = config('cache.default');
|
||||
|
||||
return sprintf(
|
||||
'enabled=%s version=%d expected=%d lockFree=%s store=%s published=%s baseDir=%s',
|
||||
var_export($svc->isEnabled(), true), $v, self::VERSION, var_export($free, true),
|
||||
$store, var_export($svc->isPublished($v), true), $svc->baseDir()
|
||||
);
|
||||
}
|
||||
|
||||
private function service(): ExtensionStaticCacheService
|
||||
{
|
||||
return app(ExtensionStaticCacheService::class);
|
||||
}
|
||||
|
||||
/**
|
||||
* 활성 모듈·플러그인 행을 만듭니다.
|
||||
*
|
||||
* 레포지토리를 모킹하지 않는다 — 게시에는 번들 서비스 등 같은 레포지토리를 쓰는 다른
|
||||
* 소비자가 있어, 인터페이스를 통째로 대체하면 그쪽이 끊겨 게시 자체가 실패한다.
|
||||
* `RefreshDatabase` 라 행은 테스트마다 비어 있으므로 실제 설치본에 좌우되지도 않는다.
|
||||
*
|
||||
* @param array<int, string> $modules 활성 모듈 식별자
|
||||
* @param array<int, string> $plugins 활성 플러그인 식별자
|
||||
*/
|
||||
private function createActiveExtensions(array $modules, array $plugins): void
|
||||
{
|
||||
foreach ($modules as $identifier) {
|
||||
Module::create([
|
||||
'identifier' => $identifier,
|
||||
'vendor' => explode('-', $identifier)[0],
|
||||
'name' => ['ko' => '테스트 모듈', 'en' => 'Test Module'],
|
||||
'version' => '1.0.0',
|
||||
'status' => ExtensionStatus::Active->value,
|
||||
'description' => ['ko' => '테스트', 'en' => 'Test'],
|
||||
]);
|
||||
}
|
||||
|
||||
foreach ($plugins as $identifier) {
|
||||
Plugin::create([
|
||||
'identifier' => $identifier,
|
||||
'vendor' => explode('-', $identifier)[0],
|
||||
'name' => ['ko' => '테스트 플러그인', 'en' => 'Test Plugin'],
|
||||
'version' => '1.0.0',
|
||||
'status' => ExtensionStatus::Active->value,
|
||||
'description' => ['ko' => '테스트', 'en' => 'Test'],
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
||||
private function createActiveTemplate(string $identifier = 'sirsoft-admin_basic', string $type = 'admin'): Template
|
||||
{
|
||||
return Template::create([
|
||||
@@ -98,7 +164,7 @@ class ExtensionStaticCacheServiceTest extends TestCase
|
||||
{
|
||||
$service = $this->service();
|
||||
|
||||
$this->assertTrue($service->publishCurrent());
|
||||
$this->assertTrue($service->publishCurrent(), $this->publishDiagnostics($service));
|
||||
$this->assertTrue($service->isPublished(self::VERSION));
|
||||
$this->assertFileExists($service->versionDir(self::VERSION).'/manifest.json');
|
||||
$this->assertFileExists($service->versionDir(self::VERSION).'/.htaccess');
|
||||
@@ -114,7 +180,7 @@ class ExtensionStaticCacheServiceTest extends TestCase
|
||||
$this->createActiveTemplate();
|
||||
|
||||
$service = $this->service();
|
||||
$this->assertTrue($service->publishCurrent());
|
||||
$this->assertTrue($service->publishCurrent(), $this->publishDiagnostics($service));
|
||||
|
||||
$publishedPath = $service->versionDir(self::VERSION).'/templates/sirsoft-admin_basic/lang/ko.json';
|
||||
$this->assertFileExists($publishedPath);
|
||||
@@ -140,7 +206,7 @@ class ExtensionStaticCacheServiceTest extends TestCase
|
||||
$this->createActiveTemplate();
|
||||
|
||||
$service = $this->service();
|
||||
$this->assertTrue($service->publishCurrent());
|
||||
$this->assertTrue($service->publishCurrent(), $this->publishDiagnostics($service));
|
||||
|
||||
$publishedPath = $service->versionDir(self::VERSION).'/templates/sirsoft-admin_basic/routes.json';
|
||||
$this->assertFileExists($publishedPath);
|
||||
@@ -155,6 +221,89 @@ class ExtensionStaticCacheServiceTest extends TestCase
|
||||
$this->assertSame($apiResponse->json('data'), $published['data']);
|
||||
}
|
||||
|
||||
/**
|
||||
* 활성 모듈·플러그인의 운영자 소유 디렉토리(`custom/`)도 게시된다.
|
||||
*
|
||||
* 모듈·플러그인 빌드 산출물은 **병합 번들**로만 게시되는데 `custom/` 은 그 번들에
|
||||
* 들어가지 않는다. 게시하지 않으면 확장 자산 중 이것만 요청마다 PHP 를 거치고,
|
||||
* CSS 내부 상대 `url()` 이 해석되지 않는다 — 이 축이 빠지면 같은 기능이 확장 타입에
|
||||
* 따라 다르게 동작하고 그 이유가 화면에 드러나지 않는다.
|
||||
*
|
||||
* @effects custom_asset_published_for_all_extension_types
|
||||
*/
|
||||
public function test_publishes_active_module_and_plugin_custom_assets(): void
|
||||
{
|
||||
$this->createActiveTemplate();
|
||||
|
||||
$module = 'g7test-publish_module';
|
||||
$plugin = 'g7test-publish_plugin';
|
||||
|
||||
$fixtures = [
|
||||
base_path("modules/{$module}/custom") => 'modules/'.$module,
|
||||
base_path("plugins/{$plugin}/custom") => 'plugins/'.$plugin,
|
||||
];
|
||||
|
||||
foreach (array_keys($fixtures) as $dir) {
|
||||
File::ensureDirectoryExists($dir.'/fonts');
|
||||
File::put($dir.'/custom.css', "body { background-image: url('./fonts/x.woff2'); }");
|
||||
File::put($dir.'/fonts/x.woff2', 'font');
|
||||
File::put($dir.'/notes.txt', 'not an asset');
|
||||
}
|
||||
|
||||
// 활성 목록은 레포지토리가 판정한다 — 실제 설치본에 의존하면 이 저장소 상태에 따라
|
||||
// 단언이 공허하게 통과한다.
|
||||
$this->createActiveExtensions([$module], [$plugin]);
|
||||
|
||||
try {
|
||||
$service = $this->service();
|
||||
$this->assertTrue($service->publishCurrent(), $this->publishDiagnostics($service));
|
||||
|
||||
$versionDir = $service->versionDir(self::VERSION);
|
||||
|
||||
foreach ($fixtures as $root) {
|
||||
$this->assertFileExists($versionDir."/{$root}/assets/custom/custom.css");
|
||||
$this->assertFileExists($versionDir."/{$root}/assets/custom/fonts/x.woff2");
|
||||
|
||||
// 허용 확장자 화이트리스트는 타입과 무관하게 같아야 한다
|
||||
$this->assertFileDoesNotExist($versionDir."/{$root}/assets/custom/notes.txt");
|
||||
}
|
||||
} finally {
|
||||
File::deleteDirectory(base_path("modules/{$module}"));
|
||||
File::deleteDirectory(base_path("plugins/{$plugin}"));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 비활성 확장의 custom 은 게시하지 않는다.
|
||||
*
|
||||
* 자산 서빙이 활성 확장에만 응답하므로, 게시해 봐야 아무도 참조하지 않는 사본이
|
||||
* 버전 디렉토리마다 쌓인다.
|
||||
*
|
||||
* @effects custom_asset_published_for_all_extension_types
|
||||
*/
|
||||
public function test_inactive_module_custom_is_absent_from_published_tree(): void
|
||||
{
|
||||
$this->createActiveTemplate();
|
||||
|
||||
$module = 'g7test-inactive_module';
|
||||
$dir = base_path("modules/{$module}/custom");
|
||||
File::ensureDirectoryExists($dir);
|
||||
File::put($dir.'/custom.css', '/* operator */');
|
||||
|
||||
// 활성 행을 만들지 않는다 — 비활성(미설치) 상태 그대로.
|
||||
|
||||
try {
|
||||
$service = $this->service();
|
||||
$this->assertTrue($service->publishCurrent(), $this->publishDiagnostics($service));
|
||||
|
||||
$this->assertFileDoesNotExist(
|
||||
$service->versionDir(self::VERSION)."/modules/{$module}/assets/custom/custom.css"
|
||||
);
|
||||
} finally {
|
||||
File::deleteDirectory(base_path("modules/{$module}"));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* components.json 사본과 dist 에셋이 게시되고 `*.map` 은 제외된다.
|
||||
*
|
||||
@@ -165,7 +314,7 @@ class ExtensionStaticCacheServiceTest extends TestCase
|
||||
$this->createActiveTemplate();
|
||||
|
||||
$service = $this->service();
|
||||
$this->assertTrue($service->publishCurrent());
|
||||
$this->assertTrue($service->publishCurrent(), $this->publishDiagnostics($service));
|
||||
|
||||
$templateDir = $service->versionDir(self::VERSION).'/templates/sirsoft-admin_basic';
|
||||
|
||||
@@ -201,7 +350,7 @@ class ExtensionStaticCacheServiceTest extends TestCase
|
||||
]);
|
||||
|
||||
$service = $this->service();
|
||||
$this->assertTrue($service->publishCurrent());
|
||||
$this->assertTrue($service->publishCurrent(), $this->publishDiagnostics($service));
|
||||
|
||||
$this->assertDirectoryExists($service->versionDir(self::VERSION).'/templates/sirsoft-admin_basic');
|
||||
$this->assertDirectoryDoesNotExist($service->versionDir(self::VERSION).'/templates/sirsoft-basic');
|
||||
@@ -228,7 +377,7 @@ class ExtensionStaticCacheServiceTest extends TestCase
|
||||
]);
|
||||
|
||||
$service = $this->service();
|
||||
$this->assertTrue($service->publishCurrent());
|
||||
$this->assertTrue($service->publishCurrent(), $this->publishDiagnostics($service));
|
||||
|
||||
$templatesDir = $service->versionDir(self::VERSION).'/templates';
|
||||
$this->assertDirectoryExists($templatesDir.'/sirsoft-admin_basic');
|
||||
@@ -246,13 +395,13 @@ class ExtensionStaticCacheServiceTest extends TestCase
|
||||
public function test_publish_is_idempotent_and_force_republishes(): void
|
||||
{
|
||||
$service = $this->service();
|
||||
$this->assertTrue($service->publishCurrent());
|
||||
$this->assertTrue($service->publishCurrent(), $this->publishDiagnostics($service));
|
||||
|
||||
// 게시 후 심은 마커가 skip 재호출에서 살아남으면 재게시가 없었다는 증거
|
||||
$marker = $service->versionDir(self::VERSION).'/idempotency-marker';
|
||||
File::put($marker, 'x');
|
||||
|
||||
$this->assertTrue($service->publishCurrent());
|
||||
$this->assertTrue($service->publishCurrent(), $this->publishDiagnostics($service));
|
||||
$this->assertFileExists($marker);
|
||||
|
||||
// force 재게시는 디렉토리를 새로 만든다 → 마커 소실
|
||||
@@ -281,6 +430,8 @@ class ExtensionStaticCacheServiceTest extends TestCase
|
||||
$service = new ExtensionStaticCacheService(
|
||||
$mock,
|
||||
app(TemplateRepositoryInterface::class),
|
||||
app(ModuleRepositoryInterface::class),
|
||||
app(PluginRepositoryInterface::class),
|
||||
app(ExtensionBundleService::class),
|
||||
app(LanguagePackService::class),
|
||||
);
|
||||
@@ -303,7 +454,7 @@ class ExtensionStaticCacheServiceTest extends TestCase
|
||||
public function test_cleanup_keeps_current_and_previous_only(): void
|
||||
{
|
||||
$service = $this->service();
|
||||
$this->assertTrue($service->publishCurrent());
|
||||
$this->assertTrue($service->publishCurrent(), $this->publishDiagnostics($service));
|
||||
|
||||
// 과거 버전 3개 + 고아 tmp 시뮬레이션
|
||||
foreach ([100, 200, 300] as $old) {
|
||||
@@ -445,7 +596,7 @@ class ExtensionStaticCacheServiceTest extends TestCase
|
||||
{
|
||||
$service = $this->service();
|
||||
|
||||
$this->assertTrue($service->publishCurrent());
|
||||
$this->assertTrue($service->publishCurrent(), $this->publishDiagnostics($service));
|
||||
|
||||
$htaccess = File::get($service->versionDir(self::VERSION).'/.htaccess');
|
||||
|
||||
|
||||
@@ -0,0 +1,359 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\Feature\View;
|
||||
|
||||
use App\Contracts\Extension\CacheInterface;
|
||||
use App\Extension\HookManager;
|
||||
use App\Extension\ModuleManager;
|
||||
use App\Extension\PluginManager;
|
||||
use App\Http\View\Composers\TemplateComposer;
|
||||
use App\Support\CustomAssets;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Facades\File;
|
||||
use PHPUnit\Framework\Attributes\Test;
|
||||
use ReflectionMethod;
|
||||
use Tests\TestCase;
|
||||
|
||||
/**
|
||||
* 사용자 추가 에셋의 프론트 주입 테스트
|
||||
*
|
||||
* 서버가 해석한 목록이 프론트까지 도달하지 않으면 `custom/` 에 파일을 넣어도 아무 일도
|
||||
* 일어나지 않는다 — 오류도 로그도 없이 "안 먹는다" 로만 나타난다. 배선의 존재와
|
||||
* **순서**를 함께 본다: 확장 간 순서는 모듈 → 플러그인 → 템플릿이어야 한다.
|
||||
*/
|
||||
class CustomAssetInjectionTest extends TestCase
|
||||
{
|
||||
/** 테스트용 가짜 플러그인 식별자 */
|
||||
private const FAKE_PLUGIN = 'g7test-injection';
|
||||
|
||||
protected function setUp(): void
|
||||
{
|
||||
parent::setUp();
|
||||
|
||||
CustomAssets::flushCache();
|
||||
|
||||
File::ensureDirectoryExists(base_path('plugins/'.self::FAKE_PLUGIN.'/custom'));
|
||||
File::put(base_path('plugins/'.self::FAKE_PLUGIN.'/custom/custom.css'), '/* operator */');
|
||||
}
|
||||
|
||||
protected function tearDown(): void
|
||||
{
|
||||
File::deleteDirectory(base_path('plugins/'.self::FAKE_PLUGIN));
|
||||
CustomAssets::flushCache();
|
||||
|
||||
parent::tearDown();
|
||||
}
|
||||
|
||||
/**
|
||||
* 탈출구(D33) — `?custom=off` 요청은 목록을 비운다.
|
||||
*
|
||||
* 운영자가 넣은 CSS 한 줄이 관리자 화면을 조작 불능으로 만들면, 그것을 고칠 화면에도
|
||||
* 그 CSS 가 실려 있어 스스로 갇힌다. 서버가 목록을 비우면 자산이 페이지에 **도달하지
|
||||
* 않으므로**, 이미 깨진 화면에서 자바스크립트가 돌기를 기대할 필요가 없다.
|
||||
*
|
||||
* @scenario custom_source=convention_scan, custom_asset=css
|
||||
*
|
||||
* @effects custom_assets_disabled_by_request_parameter
|
||||
*/
|
||||
#[Test]
|
||||
public function custom_off_요청은_수집_결과를_비운다(): void
|
||||
{
|
||||
$this->mock(PluginManager::class, function ($mock) {
|
||||
$mock->shouldReceive('getActivePlugins')->andReturn([self::FAKE_PLUGIN => []]);
|
||||
$mock->shouldReceive('loadPlugins')->andReturnNull();
|
||||
});
|
||||
|
||||
CustomAssets::flushCache();
|
||||
|
||||
$composer = app(TemplateComposer::class);
|
||||
$collect = new ReflectionMethod(TemplateComposer::class, 'collectCustomAssets');
|
||||
|
||||
// 평소에는 실린다 (대조군이 없으면 빈 결과가 "원래 없었다" 와 구분되지 않는다)
|
||||
$this->assertNotEmpty($collect->invoke($composer, null));
|
||||
|
||||
// `?custom=off` 이면 비운다
|
||||
$this->app['request'] = Request::create('/admin/layout-editor/x', 'GET', ['custom' => 'off']);
|
||||
CustomAssets::flushCache();
|
||||
|
||||
$this->assertSame([], $collect->invoke($composer, null));
|
||||
}
|
||||
|
||||
/**
|
||||
* 화면이 "지금 꺼져 있다" 를 알 수 있어야 한다.
|
||||
*
|
||||
* SPA 부팅이 주소를 다시 쓰면서 `?custom=off` 를 지우므로, 화면이 URL 로 판정하면
|
||||
* 자산은 꺼졌는데 버튼은 "켜져 있음" 으로 표시되고 되돌릴 방법이 사라진다.
|
||||
* 서버가 자기가 한 일을 그대로 심어야 한다.
|
||||
*
|
||||
* @effects custom_assets_disabled_by_request_parameter
|
||||
*/
|
||||
#[Test]
|
||||
public function 서버가_custom_off_상태를_화면에_알린다(): void
|
||||
{
|
||||
$composer = app(TemplateComposer::class);
|
||||
$judge = new ReflectionMethod(TemplateComposer::class, 'customAssetsDisabledByRequest');
|
||||
|
||||
$this->app['request'] = Request::create('/admin', 'GET');
|
||||
$this->assertFalse($judge->invoke($composer));
|
||||
|
||||
$this->app['request'] = Request::create('/admin', 'GET', ['custom' => 'off']);
|
||||
$this->assertTrue($judge->invoke($composer));
|
||||
}
|
||||
|
||||
/**
|
||||
* 판정이 변경 감지보다 **앞**에 있어야 한다.
|
||||
*
|
||||
* 뒤에 두면 빈 목록이 "전부 사라짐" 으로 읽혀 캐시 버전이 오르고, 다음 정상 요청이
|
||||
* 다시 "전부 생김" 으로 읽어 또 오른다 — 요청마다 버전이 오르내리며 재게시가 끝없이
|
||||
* 돈다.
|
||||
*
|
||||
* @effects custom_assets_disabled_by_request_parameter
|
||||
*/
|
||||
#[Test]
|
||||
public function custom_off_요청은_캐시_버전을_건드리지_않는다(): void
|
||||
{
|
||||
$this->mock(PluginManager::class, function ($mock) {
|
||||
$mock->shouldReceive('getActivePlugins')->andReturn([self::FAKE_PLUGIN => []]);
|
||||
$mock->shouldReceive('loadPlugins')->andReturnNull();
|
||||
});
|
||||
|
||||
$cache = app(CacheInterface::class);
|
||||
$cache->put('ext.cache_version', 1);
|
||||
$cache->forget(CustomAssets::SIGNATURE_CACHE_KEY);
|
||||
|
||||
$this->app['request'] = Request::create('/admin', 'GET', ['custom' => 'off']);
|
||||
CustomAssets::flushCache();
|
||||
|
||||
$composer = app(TemplateComposer::class);
|
||||
$collect = new ReflectionMethod(TemplateComposer::class, 'collectCustomAssets');
|
||||
$collect->invoke($composer, null);
|
||||
$collect->invoke($composer, null);
|
||||
|
||||
$this->assertSame(1, (int) $cache->get('ext.cache_version'));
|
||||
$this->assertNull($cache->get(CustomAssets::SIGNATURE_CACHE_KEY));
|
||||
}
|
||||
|
||||
/**
|
||||
* @scenario custom_source=assets_json, custom_asset=css
|
||||
*
|
||||
* @effects custom_asset_loaded_after_extension_bundles
|
||||
*/
|
||||
#[Test]
|
||||
public function 필터_훅으로_다른_출처의_항목을_더할_수_있다(): void
|
||||
{
|
||||
// 7.1.0 템플릿 환경설정이 화면에서 입력한 CSS 를 실어 보낼 통로다.
|
||||
// 이 훅이 없으면 그때 코어 해석기를 다시 열어야 하고, 그 순간 "운영자 CSS 가
|
||||
// 어디서 오는가" 의 SSoT 가 둘로 갈린다.
|
||||
HookManager::addFilter(
|
||||
'core.assets.custom_assets',
|
||||
function (array $assets, string $type, string $identifier): array {
|
||||
$assets[] = [
|
||||
'id' => 'custom:settings:'.$identifier,
|
||||
'type' => 'style',
|
||||
'url' => '/api/'.$type.'/settings-css',
|
||||
'version' => 42,
|
||||
'source' => 'settings',
|
||||
];
|
||||
|
||||
return $assets;
|
||||
},
|
||||
10,
|
||||
3
|
||||
);
|
||||
|
||||
CustomAssets::flushCache();
|
||||
|
||||
$assets = CustomAssets::forExtension('plugins', self::FAKE_PLUGIN);
|
||||
|
||||
$sources = array_column($assets, 'source');
|
||||
|
||||
$this->assertContains('file', $sources, '파일 출처 항목이 사라졌습니다.');
|
||||
$this->assertContains('settings', $sources, '필터 훅으로 더한 항목이 반영되지 않았습니다.');
|
||||
|
||||
// 파일 출처가 먼저 온다 — 화면에서 방금 입력한 것이 파일보다 뒤여야
|
||||
// "고쳤는데 안 먹는다" 가 생기지 않는다
|
||||
$this->assertSame('file', $sources[0]);
|
||||
$this->assertSame('settings', $sources[count($sources) - 1]);
|
||||
}
|
||||
|
||||
/**
|
||||
* @scenario custom_source=convention_scan, custom_asset=css
|
||||
*
|
||||
* @effects custom_asset_loaded_after_extension_bundles
|
||||
*/
|
||||
#[Test]
|
||||
public function 서술자는_프론트가_읽는_형태를_그대로_갖춘다(): void
|
||||
{
|
||||
// 프론트 로더(parseCustomAssetsFromConfig)는 id·type·url 셋으로 항목을 판정한다.
|
||||
// 하나라도 빠지면 그 항목은 조용히 버려진다.
|
||||
$asset = CustomAssets::forExtension('plugins', self::FAKE_PLUGIN)[0];
|
||||
|
||||
$this->assertIsString($asset['id']);
|
||||
$this->assertContains($asset['type'], ['style', 'script']);
|
||||
$this->assertIsString($asset['url']);
|
||||
$this->assertNotSame('', $asset['url']);
|
||||
}
|
||||
|
||||
/**
|
||||
* 확장 간 순서: 모듈 → 플러그인 → 템플릿.
|
||||
*
|
||||
* CSS 는 나중에 온 규칙이 이긴다. 템플릿이 화면 외관의 최종 책임을 지므로 템플릿
|
||||
* 운영자의 재정의가 가장 뒤에 와야 한다. 순서가 뒤집히면 오류 없이 "재정의가
|
||||
* 안 먹는다" 로만 나타난다.
|
||||
*
|
||||
* @scenario custom_source=convention_scan, custom_asset=css
|
||||
*
|
||||
* @effects custom_asset_loaded_after_extension_bundles
|
||||
*/
|
||||
#[Test]
|
||||
public function 확장_간_순서는_모듈_플러그인_템플릿이다(): void
|
||||
{
|
||||
// 활성 목록을 고정한다 — 테스트 DB 에 무엇이 설치되어 있는지에 순서 검증이
|
||||
// 좌우되면, 설치본이 비었을 때 수집 결과가 비어 단언이 공허하게 통과한다.
|
||||
$module = 'g7test-order-module';
|
||||
$plugin = 'g7test-order-plugin';
|
||||
$template = 'g7test-order-template';
|
||||
|
||||
$this->mock(ModuleManager::class, function ($mock) use ($module) {
|
||||
$mock->shouldReceive('getActiveModules')->andReturn([$module => []]);
|
||||
$mock->shouldReceive('loadModules')->andReturnNull();
|
||||
});
|
||||
|
||||
$this->mock(PluginManager::class, function ($mock) use ($plugin) {
|
||||
$mock->shouldReceive('getActivePlugins')->andReturn([$plugin => []]);
|
||||
$mock->shouldReceive('loadPlugins')->andReturnNull();
|
||||
});
|
||||
|
||||
$created = [
|
||||
'modules' => base_path('modules/'.$module.'/custom'),
|
||||
'plugins' => base_path('plugins/'.$plugin.'/custom'),
|
||||
'templates' => base_path('templates/'.$template.'/custom'),
|
||||
];
|
||||
|
||||
try {
|
||||
foreach ($created as $type => $dir) {
|
||||
File::ensureDirectoryExists($dir);
|
||||
File::put($dir.'/zz-order-probe.css', '/* '.$type.' */');
|
||||
}
|
||||
|
||||
CustomAssets::flushCache();
|
||||
|
||||
$composer = app(TemplateComposer::class);
|
||||
$collect = new ReflectionMethod(TemplateComposer::class, 'collectCustomAssets');
|
||||
$assets = $collect->invoke($composer, $template);
|
||||
|
||||
// 이 테스트가 놓은 프로브만 골라 순서를 본다 — 다른 운영자 파일이 있어도 무관.
|
||||
$observed = [];
|
||||
|
||||
foreach ($assets as $asset) {
|
||||
if (! str_contains($asset['id'], 'zz-order-probe.css')) {
|
||||
continue;
|
||||
}
|
||||
|
||||
// id 형식: custom:{type}:{identifier}:{file}
|
||||
$observed[] = explode(':', $asset['id'])[1];
|
||||
}
|
||||
|
||||
$this->assertSame(
|
||||
['modules', 'plugins', 'templates'],
|
||||
$observed,
|
||||
'확장 간 순서가 모듈 → 플러그인 → 템플릿이 아닙니다. '
|
||||
.'CSS 는 나중에 온 규칙이 이기므로 템플릿 재정의가 가장 뒤여야 합니다.'
|
||||
);
|
||||
} finally {
|
||||
foreach ($created as $dir) {
|
||||
File::deleteDirectory($dir);
|
||||
}
|
||||
|
||||
CustomAssets::flushCache();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 관리자 렌더와 사용자 렌더가 **하나의 서명 키를 서로 덮어쓰지 않는다.**
|
||||
*
|
||||
* 서명은 수집된 서술자로 만드는데, 그 수집은 모듈·플러그인(양쪽 동일)에 더해
|
||||
* **그 렌더의 템플릿 하나**만 싣는다(`resolveCustomAssets`). 관리자 렌더는 admin
|
||||
* 템플릿, 사용자 렌더는 user 템플릿이라 두 서명은 정상적으로 다르다. 그런데 저장
|
||||
* 키가 하나뿐이면 두 렌더가 번갈아 덮어쓰며 매번 "파일이 바뀌었다" 로 읽힌다.
|
||||
*
|
||||
* 그 오독의 대가는 `incrementExtensionCacheVersion()` 이다 — 확장 캐시 버전이 오르고
|
||||
* 정적 재게시가 예약된다. 운영자가 아무것도 건드리지 않았는데 관리자·사용자 페이지를
|
||||
* 오갈 때마다 모든 자산 URL 이 바뀌어 브라우저 캐시가 상시 무효가 되고 전체 재게시가
|
||||
* 반복된다. 예외도 화면 이상도 없어 로그 외에는 드러나지 않는다.
|
||||
*
|
||||
* `?custom=off` 진동은 `customAssetsDisabledByRequest` 가 이미 막고 있다 — 같은
|
||||
* 진동의 다른 원인인 admin/user 템플릿 비대칭만 열려 있었다.
|
||||
*
|
||||
* @scenario custom_source=convention_scan, custom_ext_type=template
|
||||
*
|
||||
* @effects custom_asset_signature_is_scoped_per_render
|
||||
*/
|
||||
#[Test]
|
||||
public function 관리자_사용자_렌더가_서명_키를_서로_덮어쓰지_않는다(): void
|
||||
{
|
||||
$adminTemplate = 'g7test-admin_tpl';
|
||||
$userTemplate = 'g7test-user_tpl';
|
||||
|
||||
foreach ([$adminTemplate, $userTemplate] as $identifier) {
|
||||
File::ensureDirectoryExists(base_path('templates/'.$identifier.'/custom'));
|
||||
File::put(base_path('templates/'.$identifier.'/custom/custom.css'), '/* '.$identifier.' */');
|
||||
}
|
||||
|
||||
try {
|
||||
$composer = app(TemplateComposer::class);
|
||||
$resolve = new ReflectionMethod(TemplateComposer::class, 'resolveCustomAssets');
|
||||
$sync = new ReflectionMethod(TemplateComposer::class, 'syncCustomAssetCacheVersion');
|
||||
$collect = new ReflectionMethod(TemplateComposer::class, 'collectCustomAssets');
|
||||
$cache = app(CacheInterface::class);
|
||||
|
||||
CustomAssets::flushCache();
|
||||
$adminAssets = $resolve->invoke($composer, $adminTemplate);
|
||||
$userAssets = $resolve->invoke($composer, $userTemplate);
|
||||
|
||||
// 두 렌더의 수집 결과가 실제로 다르다 — 같으면 이 테스트는 공허하다
|
||||
$this->assertNotSame($adminAssets, $userAssets);
|
||||
|
||||
$cache->forget(CustomAssets::SIGNATURE_CACHE_KEY);
|
||||
|
||||
// 실제 진입점으로 두 렌더를 번갈아 태운다 (스코프 전달 누락도 함께 잡는다)
|
||||
foreach ([$adminTemplate, $userTemplate, $adminTemplate, $userTemplate] as $identifier) {
|
||||
CustomAssets::flushCache();
|
||||
$collect->invoke($composer, $identifier);
|
||||
}
|
||||
|
||||
// 두 렌더의 서명이 **각자** 기억된다 — 하나의 자리를 서로 덮어쓰지 않는다
|
||||
$stored = $cache->get(CustomAssets::SIGNATURE_CACHE_KEY);
|
||||
|
||||
$this->assertIsArray($stored, '서명이 스코프별로 기억되지 않는다 (단일 값으로 덮어쓰기)');
|
||||
$this->assertArrayHasKey($adminTemplate, $stored);
|
||||
$this->assertArrayHasKey($userTemplate, $stored);
|
||||
$this->assertNotSame($stored[$adminTemplate], $stored[$userTemplate]);
|
||||
|
||||
// 파일은 그대로다 — 렌더를 오간다고 버전이 올라서는 안 된다
|
||||
$this->assertFalse(
|
||||
$sync->invoke($composer, $adminAssets, $adminTemplate),
|
||||
'관리자 렌더가 사용자 렌더의 서명을 덮어써 파일 변경으로 오독했다'
|
||||
);
|
||||
$this->assertFalse(
|
||||
$sync->invoke($composer, $userAssets, $userTemplate),
|
||||
'사용자 렌더가 관리자 렌더의 서명을 덮어써 파일 변경으로 오독했다'
|
||||
);
|
||||
|
||||
// 대조군 — 실제로 파일이 바뀌면 그 스코프에서는 감지된다 (가드가 사문이 아니다)
|
||||
File::put(base_path('templates/'.$userTemplate.'/custom/custom.css'), '/* changed */');
|
||||
touch(base_path('templates/'.$userTemplate.'/custom/custom.css'), time() + 10);
|
||||
CustomAssets::flushCache();
|
||||
|
||||
$this->assertTrue(
|
||||
$sync->invoke($composer, $resolve->invoke($composer, $userTemplate), $userTemplate),
|
||||
'파일을 고쳤는데 변경 감지가 발화하지 않았다'
|
||||
);
|
||||
} finally {
|
||||
foreach ([$adminTemplate, $userTemplate] as $identifier) {
|
||||
File::deleteDirectory(base_path('templates/'.$identifier));
|
||||
}
|
||||
CustomAssets::flushCache();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -5,6 +5,7 @@ namespace Tests\Feature\View;
|
||||
use App\Contracts\Extension\TemplateManagerInterface;
|
||||
use App\Models\Template;
|
||||
use App\Services\TemplateService;
|
||||
use App\Support\AssetUrl;
|
||||
use App\Support\TemplateExternals;
|
||||
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||||
use Illuminate\Support\Facades\File;
|
||||
@@ -35,6 +36,11 @@ class TemplateExternalsRenderingTest extends TestCase
|
||||
parent::tearDown();
|
||||
}
|
||||
|
||||
/**
|
||||
* @scenario asset_class=vendored, outcome=loaded
|
||||
*
|
||||
* @effects template_external_asset_field_resolves_to_asset_url
|
||||
*/
|
||||
public function test_blade_partials_render_all_external_types_attributes_and_positions(): void
|
||||
{
|
||||
$externals = TemplateExternals::normalize($this->externalsFixture());
|
||||
@@ -77,6 +83,9 @@ class TemplateExternalsRenderingTest extends TestCase
|
||||
$this->assertStringContainsString('src="https://cdn.example.com/body-end.js"', $bodyEnd);
|
||||
}
|
||||
|
||||
/**
|
||||
* @effects runtime_asset_served_same_origin, no_third_party_request_on_page_load
|
||||
*/
|
||||
public function test_admin_response_renders_sirsoft_admin_basic_externals_before_template_css(): void
|
||||
{
|
||||
$templateService = app(TemplateService::class);
|
||||
@@ -89,13 +98,31 @@ class TemplateExternalsRenderingTest extends TestCase
|
||||
|
||||
$html = $response->getContent();
|
||||
|
||||
$this->assertStringContainsString('href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css"', $html);
|
||||
$this->assertStringContainsString('href="https://cdn.jsdelivr.net/gh/orioncactus/pretendard@v1.3.9/dist/web/static/pretendard.min.css"', $html);
|
||||
$this->assertStringContainsString('href="https://cdn.jsdelivr.net/npm/flag-icons@7.2.3/css/flag-icons.min.css"', $html);
|
||||
$this->assertBefore($html, 'href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css"', '/api/templates/assets/sirsoft-admin_basic/css/components.css?v=');
|
||||
$this->assertBefore($html, 'href="https://cdn.jsdelivr.net"', 'href="https://cdn.jsdelivr.net/gh/orioncactus/pretendard@v1.3.9/dist/web/static/pretendard.min.css"');
|
||||
// 자체 제공으로 전환됐다 (공개 #123) — 종전에는 이 세 자산이 각각 다른 외부 CDN 에서 왔다.
|
||||
$fontAwesome = 'vendor/font-awesome/6.4.0/css/all.inlined.css';
|
||||
$pretendard = 'vendor/pretendard/1.3.9/pretendard-variable.css';
|
||||
$flagIcons = 'vendor/flag-icons/7.2.3/css/flag-icons.min.css';
|
||||
|
||||
foreach ([$fontAwesome, $pretendard, $flagIcons] as $asset) {
|
||||
$this->assertStringContainsString('/api/templates/assets/sirsoft-admin_basic/'.$asset, $html);
|
||||
}
|
||||
|
||||
// externals 는 템플릿 자체 CSS 보다 먼저 온다 (종전 순서 계약 유지)
|
||||
$this->assertBefore(
|
||||
$html,
|
||||
'/api/templates/assets/sirsoft-admin_basic/'.$fontAwesome,
|
||||
'/api/templates/assets/sirsoft-admin_basic/css/components.css?v='
|
||||
);
|
||||
|
||||
// 자체 제공 항목에는 외부 전용 힌트가 붙지 않는다 (자기 origin 에 preconnect 는 무의미)
|
||||
$this->assertStringNotContainsString('rel="preconnect"', $html);
|
||||
$this->assertStringNotContainsString('cdnjs.cloudflare.com', $html);
|
||||
$this->assertStringNotContainsString('cdn.jsdelivr.net', $html);
|
||||
}
|
||||
|
||||
/**
|
||||
* @effects runtime_asset_served_same_origin
|
||||
*/
|
||||
public function test_user_response_renders_template_externals_with_same_manifest_syntax(): void
|
||||
{
|
||||
$this->createFixtureTemplate('test-userexternals', 'user', [
|
||||
@@ -129,6 +156,72 @@ class TemplateExternalsRenderingTest extends TestCase
|
||||
$this->assertBefore($html, 'href="https://cdn.example.com"', 'href="https://cdn.example.com/user.css"');
|
||||
}
|
||||
|
||||
/**
|
||||
* `asset` 필드가 만드는 URL 은 자산 URL 모드 × 정적 게시 4조합 전부에서 same-origin 이다.
|
||||
*
|
||||
* 이 축이 이 작업의 잔여 리스크다 — 확장자를 정적 location 이 가로채는 서버에서는
|
||||
* `extensionless` 모드가 쓰이고, 그 조합에서 URL 형태가 달라진다. 어느 조합에서든
|
||||
* 외부 origin 이 새어 나가면 자체 제공 전환이 그 조합에서만 무효가 되는데,
|
||||
* 화면에는 "아이콘이 안 보인다" 로만 나타난다.
|
||||
*
|
||||
* 한계: 정적 게시 게이트(`AssetUrl::staticExtBase`)는 `production` 환경도 함께 요구하므로
|
||||
* 테스트 환경에서 `static=on` 다리는 실제 게시 경로(`/build/ext/{v}/…`)를 타지 않는다.
|
||||
* 여기서 고정하는 것은 **설정 축에서 URL 이 외부로 새지 않는다** 는 것까지이며,
|
||||
* 게시 경로 자체의 실제 형태는 브라우저 점검(T10-f/T10-g)이 맡는다.
|
||||
*
|
||||
* @scenario asset_class=vendored, outcome=loaded
|
||||
*
|
||||
* @effects runtime_asset_served_same_origin
|
||||
*/
|
||||
public function test_asset_field_stays_same_origin_across_url_mode_and_static_cache(): void
|
||||
{
|
||||
$combinations = [
|
||||
['mode' => 'extension', 'static' => false],
|
||||
['mode' => 'extension', 'static' => true],
|
||||
['mode' => 'extensionless', 'static' => false],
|
||||
['mode' => 'extensionless', 'static' => true],
|
||||
];
|
||||
|
||||
try {
|
||||
foreach ($combinations as $combination) {
|
||||
AssetUrl::forceMode($combination['mode']);
|
||||
config(['core.static_cache.enabled' => $combination['static']]);
|
||||
AssetUrl::resetStaticExtBaseMemo();
|
||||
|
||||
$normalized = TemplateExternals::normalize([
|
||||
[
|
||||
'id' => 'style-vendored',
|
||||
'type' => 'style',
|
||||
'asset' => 'vendor/font-awesome/6.4.0/css/all.inlined.css',
|
||||
],
|
||||
], 'test-userexternals', 7);
|
||||
|
||||
$label = "mode={$combination['mode']} static=".($combination['static'] ? 'on' : 'off');
|
||||
|
||||
$this->assertCount(1, $normalized, "항목이 사라졌습니다 ({$label})");
|
||||
|
||||
$url = $normalized[0]['url'];
|
||||
|
||||
$this->assertDoesNotMatchRegularExpression(
|
||||
'#^(https?:)?//#',
|
||||
$url,
|
||||
"외부 origin 으로 새어 나갔습니다 ({$label}): {$url}"
|
||||
);
|
||||
$this->assertStringStartsWith('/', $url, "same-origin 경로가 아닙니다 ({$label}): {$url}");
|
||||
$this->assertStringContainsString('all.inlined.css', rawurldecode($url), $label);
|
||||
|
||||
// 외부 전용 키는 same-origin 항목에서 붙지 않는다 — preconnect 는 외부 origin
|
||||
// 을 미리 여는 장치라 자기 자신에게는 의미가 없다.
|
||||
$this->assertArrayNotHasKey('preconnect', $normalized[0], $label);
|
||||
$this->assertArrayNotHasKey('crossorigin', $normalized[0], $label);
|
||||
}
|
||||
} finally {
|
||||
AssetUrl::forceMode(null);
|
||||
config(['core.static_cache.enabled' => true]);
|
||||
AssetUrl::resetStaticExtBaseMemo();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array<int, array<string, mixed>>
|
||||
*/
|
||||
|
||||
Reference in new issue
Block a user