확장이 훅으로 등록한 카탈로그에서 고른 값은 그 확장이 사라져도 저장값으로 남는다. 그 상태에서 값만 보고 판정하면 이미 제공 불가한 항목이 사용자 화면에 그대로 노출된다. 예외도 경고도 로그도 남지 않아 관리자 화면과 나란히 보지 않으면 드러나지 않는다. - 결제수단에 지정한 PG, 현금영수증 발급사, 검색엔진 드라이버 세 축에 같은 판정을 적용. 공개 응답에서는 제거하고 관리자 응답에는 표시를 남긴다 — 운영자가 고쳐야 할 대상이라 감추면 복구 경로가 사라진다. 서버 검증도 같은 판정을 공유해 화면 우회 제출을 막는다. - 설정 단건 저장이 벌크 저장과 다른 경로를 타던 문제를 코어·이커머스·게시판에서 정리. 저장은 성공하고 파생 값만 갱신되지 않아 어느 화면에서 저장했느냐로 결과가 갈렸다. - 단건 저장의 값 형태 계약 신설. 빈 값으로 되돌릴 수 있게 하고, 타입은 defaults.json 의 기본값을 SSoT 로 삼는다. 해석 불가한 입력은 조용히 캐스팅하지 않고 거절한다. - 드라이버 셀렉트를 서버 카탈로그에 바인딩하고, 저장값이 카탈로그에 없으면 그 값을 드러내는 안내를 8곳 전수에 붙인다. 종전에는 빈 칸으로만 보여 무엇이 저장됐는지 알 수 없었다. 시나리오 매니페스트 8종을 면제 없이 신설했다. `audit:allow` 는 cross product 와 effects 검사를 함께 끄므로, 면제가 걸린 매니페스트에 선언을 추가하면 검증이 회복되지 않는다. 그 아래에서 대응 테스트가 없는 effect 이름 2건이 살아남아 있었고, 함께 정리했다.
306 lines
11 KiB
PHP
306 lines
11 KiB
PHP
<?php
|
|
|
|
namespace Tests\Feature\Services;
|
|
|
|
use App\Contracts\Extension\ModuleManagerInterface;
|
|
use App\Contracts\Extension\PluginManagerInterface;
|
|
use App\Contracts\Extension\TemplateManagerInterface;
|
|
use App\Contracts\Repositories\LayoutVersionRepositoryInterface;
|
|
use App\Enums\ExtensionStatus;
|
|
use App\Exceptions\TemplateFileCopyException;
|
|
use App\Extension\HookManager;
|
|
use App\Models\Template;
|
|
use App\Repositories\TemplateRepository;
|
|
use App\Services\TemplateService;
|
|
use Illuminate\Foundation\Testing\RefreshDatabase;
|
|
use Illuminate\Validation\ValidationException;
|
|
use Mockery;
|
|
use Tests\TestCase;
|
|
|
|
class TemplateServiceTest extends TestCase
|
|
{
|
|
use RefreshDatabase;
|
|
|
|
private TemplateService $templateService;
|
|
|
|
private TemplateRepository $templateRepository;
|
|
|
|
private TemplateManagerInterface $templateManager;
|
|
|
|
private ModuleManagerInterface $moduleManager;
|
|
|
|
private PluginManagerInterface $pluginManager;
|
|
|
|
protected function setUp(): void
|
|
{
|
|
parent::setUp();
|
|
|
|
// TemplateManager Mock 생성
|
|
$this->templateManager = Mockery::mock(TemplateManagerInterface::class);
|
|
|
|
// loadTemplates() 호출 허용 (생성자에서 호출됨)
|
|
$this->templateManager->shouldReceive('loadTemplates')->andReturn(null);
|
|
$this->templateManager->shouldReceive('ensureLoaded')->andReturn(null);
|
|
|
|
// ModuleManager Mock 생성
|
|
$this->moduleManager = Mockery::mock(ModuleManagerInterface::class);
|
|
|
|
// PluginManager Mock 생성
|
|
$this->pluginManager = Mockery::mock(PluginManagerInterface::class);
|
|
|
|
// TemplateRepository 인스턴스 생성
|
|
$this->templateRepository = new TemplateRepository;
|
|
|
|
// TemplateService 인스턴스 생성 (Mock Manager 주입)
|
|
$this->templateService = new TemplateService(
|
|
$this->templateRepository,
|
|
$this->templateManager,
|
|
$this->moduleManager,
|
|
$this->pluginManager,
|
|
app(LayoutVersionRepositoryInterface::class)
|
|
);
|
|
}
|
|
|
|
protected function tearDown(): void
|
|
{
|
|
Mockery::close();
|
|
parent::tearDown();
|
|
}
|
|
|
|
/**
|
|
* 템플릿 활성화 시 훅 시스템이 올바른 순서로 실행되는지 검증
|
|
*/
|
|
public function test_activates_template_with_hook_system(): void
|
|
{
|
|
// Arrange - 고유 identifier 사용하여 트랜잭션 락 충돌 방지
|
|
$identifier = 'test-template-'.uniqid();
|
|
$template = Template::create([
|
|
'identifier' => $identifier,
|
|
'vendor' => 'test-vendor',
|
|
'name' => 'Test Template',
|
|
'type' => 'admin',
|
|
'version' => '1.0.0',
|
|
'status' => ExtensionStatus::Inactive->value,
|
|
]);
|
|
|
|
$templateInfo = [
|
|
'identifier' => $identifier,
|
|
'vendor' => 'test-vendor',
|
|
'name' => 'Test Template',
|
|
'type' => 'admin',
|
|
'status' => ExtensionStatus::Active->value,
|
|
'id' => $template->id,
|
|
];
|
|
|
|
// 훅 호출 추적
|
|
$hookCalls = [];
|
|
|
|
HookManager::addAction('core.templates.before_activate', function ($identifier) use (&$hookCalls) {
|
|
$hookCalls[] = ['hook' => 'before_activate', 'identifier' => $identifier];
|
|
});
|
|
|
|
HookManager::addAction('core.templates.after_activate', function ($templateInfo) use (&$hookCalls) {
|
|
$hookCalls[] = ['hook' => 'after_activate', 'template_id' => $templateInfo['id'] ?? null];
|
|
});
|
|
|
|
// TemplateManager Mock 설정 (두 번째 인수 $copyFiles=false 포함)
|
|
$this->templateManager
|
|
->shouldReceive('activateTemplate')
|
|
->once()
|
|
->with($identifier, false)
|
|
->andReturnUsing(function () use ($template, $templateInfo) {
|
|
// Manager가 DB 업데이트 시뮬레이션
|
|
$template->update(['status' => ExtensionStatus::Active->value]);
|
|
|
|
return $templateInfo; // 배열 반환 (인터페이스 요구사항)
|
|
});
|
|
|
|
// activateTemplate 성공 후 호출되는 메서드 (loadTemplates는 setUp에서 이미 Mock됨)
|
|
$this->templateManager
|
|
->shouldReceive('getTemplateInfo')
|
|
->once()
|
|
->with($identifier)
|
|
->andReturn($templateInfo);
|
|
|
|
// Act
|
|
$result = $this->templateService->activateTemplate($template->id);
|
|
|
|
// Assert
|
|
// 훅이 최소 2개 이상 호출되어야 함 (Laravel 이벤트 시스템 때문에 중복 가능)
|
|
$this->assertGreaterThanOrEqual(2, count($hookCalls));
|
|
|
|
// before_activate가 호출되었는지 확인
|
|
$beforeActivateCalls = array_filter($hookCalls, fn ($call) => $call['hook'] === 'before_activate');
|
|
$this->assertNotEmpty($beforeActivateCalls);
|
|
$firstBeforeActivate = array_values($beforeActivateCalls)[0];
|
|
$this->assertEquals($template->identifier, $firstBeforeActivate['identifier']);
|
|
|
|
// after_activate가 호출되었는지 확인
|
|
$afterActivateCalls = array_filter($hookCalls, fn ($call) => $call['hook'] === 'after_activate');
|
|
$this->assertNotEmpty($afterActivateCalls);
|
|
$firstAfterActivate = array_values($afterActivateCalls)[0];
|
|
$this->assertEquals($template->id, $firstAfterActivate['template_id']);
|
|
|
|
// activateTemplate은 ['success' => true, 'template_info' => $templateInfo] 반환
|
|
$this->assertIsArray($result);
|
|
$this->assertTrue($result['success']);
|
|
$this->assertEquals(ExtensionStatus::Active->value, $result['template_info']['status']);
|
|
}
|
|
|
|
/**
|
|
* Manager의 activateTemplate() 메서드가 정확히 한 번 호출되는지 확인
|
|
*/
|
|
public function test_delegates_file_copy_to_manager(): void
|
|
{
|
|
// Arrange - 고유 identifier 사용하여 트랜잭션 락 충돌 방지
|
|
$identifier = 'test-template-'.uniqid();
|
|
$template = Template::create([
|
|
'identifier' => $identifier,
|
|
'vendor' => 'test-vendor',
|
|
'name' => 'Test Template',
|
|
'type' => 'admin',
|
|
'version' => '1.0.0',
|
|
'status' => ExtensionStatus::Inactive->value,
|
|
]);
|
|
|
|
$templateInfo = [
|
|
'identifier' => $identifier,
|
|
'vendor' => 'test-vendor',
|
|
'name' => 'Test Template',
|
|
'type' => 'admin',
|
|
'status' => ExtensionStatus::Active->value,
|
|
];
|
|
|
|
// TemplateManager Mock 설정 - 정확히 한 번 호출 검증 (두 번째 인수 $copyFiles=false 포함)
|
|
$this->templateManager
|
|
->shouldReceive('activateTemplate')
|
|
->once()
|
|
->with($identifier, false)
|
|
->andReturnUsing(function () use ($template, $templateInfo) {
|
|
$template->update(['status' => ExtensionStatus::Active->value]);
|
|
|
|
return $templateInfo; // 배열 반환 (인터페이스 요구사항)
|
|
});
|
|
|
|
// activateTemplate 성공 후 호출되는 메서드 (loadTemplates는 setUp에서 이미 Mock됨)
|
|
$this->templateManager
|
|
->shouldReceive('getTemplateInfo')
|
|
->once()
|
|
->with($identifier)
|
|
->andReturn($templateInfo);
|
|
|
|
// Act
|
|
$this->templateService->activateTemplate($template->id);
|
|
|
|
// Assert
|
|
// Mockery의 shouldReceive()->once()가 호출 횟수를 검증
|
|
$this->assertTrue(true); // Mockery 검증 통과
|
|
}
|
|
|
|
/**
|
|
* filter_activate_data 훅이 Manager 호출 전에 적용되는지 검증
|
|
*/
|
|
public function test_applies_filter_hook_before_manager_call(): void
|
|
{
|
|
// Arrange - 고유 identifier 사용하여 트랜잭션 락 충돌 방지
|
|
$identifier = 'test-template-'.uniqid();
|
|
$template = Template::create([
|
|
'identifier' => $identifier,
|
|
'vendor' => 'test-vendor',
|
|
'name' => 'Test Template',
|
|
'type' => 'admin',
|
|
'version' => '1.0.0',
|
|
'status' => ExtensionStatus::Inactive->value,
|
|
]);
|
|
|
|
$templateInfo = [
|
|
'identifier' => $identifier,
|
|
'vendor' => 'test-vendor',
|
|
'name' => 'Test Template',
|
|
'type' => 'admin',
|
|
'status' => ExtensionStatus::Active->value,
|
|
];
|
|
|
|
// 필터 훅 등록 - 데이터 변형
|
|
$filterApplied = false;
|
|
HookManager::addFilter('core.templates.filter_activate_data', function ($data, $template) use (&$filterApplied) {
|
|
$filterApplied = true;
|
|
$data['custom_field'] = 'filtered_value';
|
|
|
|
return $data;
|
|
}, 10, 2);
|
|
|
|
// TemplateManager Mock 설정 (두 번째 인수 $copyFiles=false 포함)
|
|
$this->templateManager
|
|
->shouldReceive('activateTemplate')
|
|
->once()
|
|
->with($identifier, false)
|
|
->andReturnUsing(function () use ($template, $templateInfo) {
|
|
$template->update(['status' => ExtensionStatus::Active->value]);
|
|
|
|
return $templateInfo; // 배열 반환 (인터페이스 요구사항)
|
|
});
|
|
|
|
// activateTemplate 성공 후 호출되는 메서드 (loadTemplates는 setUp에서 이미 Mock됨)
|
|
$this->templateManager
|
|
->shouldReceive('getTemplateInfo')
|
|
->once()
|
|
->with($identifier)
|
|
->andReturn($templateInfo);
|
|
|
|
// Act
|
|
$this->templateService->activateTemplate($template->id);
|
|
|
|
// Assert
|
|
$this->assertTrue($filterApplied, 'Filter hook should be applied before manager call');
|
|
}
|
|
|
|
/**
|
|
* Manager에서 예외 발생 시 Service가 적절히 처리하는지 테스트
|
|
*/
|
|
public function test_throws_exception_on_manager_failure(): void
|
|
{
|
|
// Arrange - 고유 identifier 사용하여 트랜잭션 락 충돌 방지
|
|
$identifier = 'test-template-'.uniqid();
|
|
$template = Template::create([
|
|
'identifier' => $identifier,
|
|
'vendor' => 'test-vendor',
|
|
'name' => 'Test Template',
|
|
'type' => 'admin',
|
|
'version' => '1.0.0',
|
|
'status' => ExtensionStatus::Inactive->value,
|
|
]);
|
|
|
|
// TemplateManager Mock 설정 - 예외 발생 (두 번째 인수 $copyFiles=false 포함)
|
|
$this->templateManager
|
|
->shouldReceive('activateTemplate')
|
|
->once()
|
|
->with($identifier, false)
|
|
->andThrow(new TemplateFileCopyException(
|
|
__('exceptions.template_file_copy_failed', ['template' => $identifier]),
|
|
'/source/path',
|
|
'/dest/path'
|
|
));
|
|
|
|
// Act & Assert
|
|
// Service가 예외를 ValidationException으로 래핑함
|
|
$this->expectException(ValidationException::class);
|
|
|
|
$this->templateService->activateTemplate($template->id);
|
|
}
|
|
|
|
/**
|
|
* 존재하지 않는 템플릿 활성화 시 예외 발생 검증
|
|
*/
|
|
public function test_throws_exception_when_template_not_found(): void
|
|
{
|
|
// Arrange
|
|
$nonExistentId = 999;
|
|
|
|
// Act & Assert
|
|
$this->expectException(ValidationException::class);
|
|
|
|
$this->templateService->activateTemplate($nonExistentId);
|
|
}
|
|
}
|