Files
Gnuboard7/tests/Feature/Template/TemplateLanguageServingTest.php
T
HeuJung e819db9d42 fix(core): 공개 템플릿 config 캐시 무효화 누락과 확장 캐시 삭제 경로 동종 결함 정비
공개 config.json 의 고정 캐시 키(template.config.{identifier})가 어떤 갱신
경로에서도 forget 되지 않아 template:update/cache-clear/비활성화/삭제 후에도
최대 1시간(TTL) 이전 manifest 로 응답되던 결함(공개 )을 수정한다.

- TemplateManager::clearTemplateCache 공개화 — config/components_manifest(두
 변종)/현재 버전 routes·language 키 능동 forget, updateTemplate cleanup 호출
- forgetLayoutCacheKeys 가 편집기용 .with_source_meta 변종까지 삭제
- ext.cache_version 쓰기를 incrementExtensionCacheVersion 단일 지점으로 통일
- module/plugin:cache-clear 의 유령 키 forget 을 실재 캐시(레이아웃 서빙/해석
 캐시, 상태 키, 미들웨어 인덱스, 버전 bump) 무효화로 교체
- 공개 routes/language/layout API 의 ?v 생략 폴백을 0 → 현재 버전으로 교정
2026-08-17 13:22:48 +09:00

187 lines
6.0 KiB
PHP

<?php
namespace Tests\Feature\Template;
use App\Contracts\Extension\CacheInterface;
use App\Enums\ExtensionStatus;
use App\Models\Template;
use App\Services\TemplateService;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\Cache;
use Tests\TestCase;
class TemplateLanguageServingTest extends TestCase
{
use RefreshDatabase;
private Template $activeTemplate;
private Template $inactiveTemplate;
private TemplateService $templateService;
protected function setUp(): void
{
parent::setUp();
$this->templateService = app(TemplateService::class);
// sirsoft-admin_basic 템플릿 설치 및 활성화
$this->templateService->installTemplate('sirsoft-admin_basic');
$this->activeTemplate = Template::where('identifier', 'sirsoft-admin_basic')->first();
$this->templateService->activateTemplate($this->activeTemplate->id);
// 비활성화된 템플릿 생성 (테스트용)
$this->inactiveTemplate = Template::create([
'identifier' => 'sirsoft-user_basic',
'vendor' => 'sirsoft',
'name' => ['ko' => 'User Basic', 'en' => 'User Basic'],
'version' => '1.0.0',
'description' => ['ko' => '기본 사용자 템플릿', 'en' => 'Basic user template'],
'type' => 'user',
'status' => ExtensionStatus::Inactive->value,
]);
// 캐시 초기화
Cache::flush();
}
/**
* 한국어 언어 파일 조회 성공 테스트
*/
public function test_can_retrieve_korean_language_file(): void
{
$response = $this->getJson('/api/templates/sirsoft-admin_basic/lang/ko.json');
$response->assertStatus(200)
->assertJsonStructure([
'common',
'auth',
]);
}
/**
* 영어 언어 파일 조회 성공 테스트
*/
public function test_can_retrieve_english_language_file(): void
{
$response = $this->getJson('/api/templates/sirsoft-admin_basic/lang/en.json');
$response->assertStatus(200)
->assertJsonStructure([
'common',
'auth',
]);
}
/**
* 지원하지 않는 로케일 요청 시 404 반환
*/
public function test_unsupported_locale_returns_404(): void
{
$response = $this->getJson('/api/templates/sirsoft-admin_basic/lang/fr.json');
$response->assertStatus(404)
->assertJson([
'success' => false,
])
->assertJsonFragment([
'message' => __('templates.errors.locale_not_supported', [
'template' => 'sirsoft-admin_basic',
'locale' => 'fr',
]),
]);
}
/**
* 존재하지 않는 템플릿 요청 시 404 반환
*/
public function test_nonexistent_template_returns_404(): void
{
$response = $this->getJson('/api/templates/nonexistent-template/lang/ko.json');
$response->assertStatus(404)
->assertJson([
'success' => false,
])
->assertJsonFragment([
'message' => __('templates.errors.not_found', ['template' => 'nonexistent-template']),
]);
}
/**
* 비활성화된 템플릿 요청 시 404 반환
*/
public function test_inactive_template_returns_404(): void
{
$response = $this->getJson('/api/templates/sirsoft-user_basic/lang/ko.json');
$response->assertStatus(404)
->assertJson([
'success' => false,
])
->assertJsonFragment([
'message' => __('templates.errors.not_found', ['template' => 'sirsoft-user_basic']),
]);
}
/**
* 언어 파일이 캐싱되는지 확인
*/
public function test_language_file_is_cached(): void
{
// `?v` 생략 시 현재 확장 캐시 버전으로 폴백하므로 (#588 — `.v0` 사각 키 방지)
// 버전을 시드해 결정적 키로 검증한다
Cache::put('g7:core:ext.cache_version', 1234);
$cache = app(CacheInterface::class);
$cacheKey = 'template.language.sirsoft-admin_basic.ko.v1234';
$cache->forget($cacheKey);
// 첫 번째 요청
$this->getJson('/api/templates/sirsoft-admin_basic/lang/ko.json');
// 캐시 확인
$cachedData = $cache->get($cacheKey);
$this->assertNotNull($cachedData);
$this->assertIsArray($cachedData);
$this->assertArrayHasKey('success', $cachedData);
$this->assertTrue($cachedData['success']);
$this->assertArrayHasKey('data', $cachedData);
}
/**
* Cache-Control 헤더가 설정되는지 확인
*/
public function test_cache_control_header_is_set(): void
{
$response = $this->getJson('/api/templates/sirsoft-admin_basic/lang/ko.json');
$response->assertStatus(200);
// Cache-Control 헤더 존재 여부 및 max-age 값 확인
$this->assertTrue($response->headers->has('Cache-Control'));
$cacheControl = $response->headers->get('Cache-Control');
$this->assertStringContainsString('max-age=3600', $cacheControl);
$this->assertStringContainsString('public', $cacheControl);
}
/**
* 잘못된 로케일 형식 요청 시 404 반환
*/
public function test_invalid_locale_format_returns_404(): void
{
// 3자리 로케일 - 라우트 매칭 실패로 404
$response = $this->getJson('/api/templates/sirsoft-admin_basic/lang/kor.json');
$response->assertStatus(404);
// 숫자 포함 로케일 - 라우트 매칭 실패로 404
$response = $this->getJson('/api/templates/sirsoft-admin_basic/lang/k1.json');
$response->assertStatus(404);
// 대문자 로케일 - 라우트 매칭 실패로 404
$response = $this->getJson('/api/templates/sirsoft-admin_basic/lang/KO.json');
$response->assertStatus(404);
}
}