- 공개 : MATCH 는 커버 인덱스가 있을 때만 조립 — 부재 시 LIKE 폴백 + 1회 경고, 카테고리 검색 예외를 categories_failed/search_failed 로 표면화하고 basic 템플릿이 오류 안내 렌더 - 공개 동근원: paginate page 명시 전달 (언어팩 check-updates, 상품 문의 목록) - 공개 동근원: raw SQL 접두사/별칭 하드코딩 정리 (board 시더, 7.0.6 업그레이드 스텝, 결제 3플러그인 컨트롤러 51지점 모델 파생 전환) - audit 룰 3종 신설 + repository-raw-hardcoded-table 컨트롤러 확대, 확장 TestCase 오토로더 중복 선언 가드 16지점, ja 언어팩 동기
354 lines
12 KiB
PHP
354 lines
12 KiB
PHP
<?php
|
|
|
|
namespace Modules\Sirsoft\Board\Tests;
|
|
|
|
use App\Contracts\Extension\HookListenerInterface;
|
|
use App\Enums\ExtensionStatus;
|
|
use App\Extension\HookListenerRegistrar;
|
|
use App\Extension\HookManager;
|
|
use App\Extension\ModuleManager;
|
|
use App\Http\Middleware\PermissionMiddleware;
|
|
use App\Models\Module;
|
|
use App\Models\Permission;
|
|
use App\Models\Role;
|
|
use App\Models\User;
|
|
use Illuminate\Foundation\Testing\DatabaseTransactions;
|
|
use Illuminate\Support\Facades\Route;
|
|
use Illuminate\Support\Facades\Schema;
|
|
use Modules\Sirsoft\Board\Models\Board;
|
|
use Modules\Sirsoft\Board\Providers\BoardServiceProvider;
|
|
use Tests\TestCase;
|
|
|
|
/**
|
|
* Board 모듈 테스트 베이스 클래스
|
|
*
|
|
* 모든 Board 모듈 테스트는 이 클래스를 상속받아야 합니다.
|
|
* 모듈 오토로드, ServiceProvider 등록, 마이그레이션, 라우트 등록을 자동으로 처리합니다.
|
|
*
|
|
* 성능 최적화:
|
|
* - 각 테스트 메서드는 DatabaseTransactions로 트랜잭션 롤백만 수행 (빠름)
|
|
* - board_posts/board_comments/board_attachments 단일 테이블 사용 (DDL 불필요)
|
|
*/
|
|
abstract class ModuleTestCase extends TestCase
|
|
{
|
|
use DatabaseTransactions;
|
|
|
|
/**
|
|
* 모듈 루트 경로를 반환합니다.
|
|
*
|
|
* __DIR__을 기반으로 동적 해석하여 _bundled/활성 디렉토리 모두에서 동작합니다.
|
|
*
|
|
* @return string 모듈 루트 절대 경로
|
|
*/
|
|
protected function getModuleBasePath(): string
|
|
{
|
|
// __DIR__ = {module_root}/tests/ → dirname = {module_root}
|
|
return dirname(__DIR__);
|
|
}
|
|
|
|
/**
|
|
* 마이그레이션 완료 플래그 (클래스당 한 번만 실행)
|
|
*/
|
|
protected static bool $migrated = false;
|
|
|
|
/**
|
|
* 테스트용 공유 게시판 slug (클래스 내 모든 테스트가 공유)
|
|
*/
|
|
protected static string $sharedBoardSlug = '';
|
|
|
|
/**
|
|
* HookManager static state 스냅샷 — tearDown 에서 복원하여 테스트 간 훅 격리 보장.
|
|
*
|
|
* @var array{hooks: array, filters: array, dispatching: array}|null
|
|
*/
|
|
private ?array $hookSnapshot = null;
|
|
|
|
/**
|
|
* 테스트 환경 설정
|
|
*/
|
|
protected function setUp(): void
|
|
{
|
|
parent::setUp();
|
|
|
|
// 모듈 오토로드 등록 (테스트 환경)
|
|
$this->registerModuleAutoload();
|
|
|
|
// 모듈 ServiceProvider 등록 (Repository 바인딩)
|
|
$this->app->register(BoardServiceProvider::class);
|
|
|
|
// 모듈 마이그레이션 실행 (boards 테이블 등)
|
|
$this->runModuleMigrationIfNeeded();
|
|
|
|
// 모듈을 활성화 상태로 등록 (테스트 환경)
|
|
$this->registerModuleAsActive();
|
|
|
|
// ModuleManager 메모리 맵에 sirsoft-board 로드 (테스트 환경)
|
|
// CoreServiceProvider::boot()에서 loadModules()가 호출되지만,
|
|
// 테스트 컨테이너에서 ModuleManager 싱글톤이 비어있는 경우를 대비해 명시적으로 재로드
|
|
$this->app->make(ModuleManager::class)->loadModules();
|
|
|
|
// _bundled 디렉토리 모듈은 loadModules() 가 스캔하지 않아
|
|
// 모듈 인스턴스 등록 + 훅 리스너 자동 등록이 누락된다.
|
|
// 테스트 환경에서 module.php 가 선언한 getHookListeners() 의 리스너들을 수동 등록.
|
|
$this->registerBundledModuleInstance();
|
|
|
|
// 모듈 라우트를 수동으로 등록
|
|
$this->registerModuleRoutes();
|
|
|
|
// 기본 역할 생성
|
|
$this->createDefaultRoles();
|
|
|
|
// HookManager 상태 스냅샷 (tearDown 에서 복원하여 테스트 내 추가 훅만 제거)
|
|
$this->snapshotHookManager();
|
|
|
|
// PermissionMiddleware::$guestRoleCache 초기화 — 이전 테스트에서 로드된 guest role/permissions 캐시가
|
|
// DatabaseTransactions 롤백 후에도 남아있어 다음 테스트의 새 permission 설정이 반영되지 않는 문제 회피.
|
|
PermissionMiddleware::clearGuestRoleCache();
|
|
}
|
|
|
|
/**
|
|
* tearDown 에 HookManager 상태 복원.
|
|
*/
|
|
protected function tearDown(): void
|
|
{
|
|
$this->restoreHookManager();
|
|
|
|
parent::tearDown();
|
|
}
|
|
|
|
/**
|
|
* HookManager static $hooks / $filters / $dispatching 를 스냅샷.
|
|
*/
|
|
private function snapshotHookManager(): void
|
|
{
|
|
$ref = new \ReflectionClass(HookManager::class);
|
|
$this->hookSnapshot = [
|
|
'hooks' => $ref->getProperty('hooks')->getValue(),
|
|
'filters' => $ref->getProperty('filters')->getValue(),
|
|
'dispatching' => $ref->getProperty('dispatching')->getValue(),
|
|
];
|
|
}
|
|
|
|
/**
|
|
* 스냅샷 시점으로 HookManager 복원.
|
|
*/
|
|
private function restoreHookManager(): void
|
|
{
|
|
if ($this->hookSnapshot === null) {
|
|
return;
|
|
}
|
|
|
|
$ref = new \ReflectionClass(HookManager::class);
|
|
$ref->getProperty('hooks')->setValue(null, $this->hookSnapshot['hooks']);
|
|
$ref->getProperty('filters')->setValue(null, $this->hookSnapshot['filters']);
|
|
$ref->getProperty('dispatching')->setValue(null, $this->hookSnapshot['dispatching']);
|
|
|
|
$this->hookSnapshot = null;
|
|
}
|
|
|
|
/**
|
|
* 모듈 마이그레이션 실행 (필요한 경우에만)
|
|
*
|
|
* 코어 테이블이 없으면 먼저 코어 마이그레이션을 실행하고,
|
|
* 그 후 모듈 마이그레이션을 실행합니다.
|
|
* static $migrated 플래그로 프로세스당 한 번만 실행합니다.
|
|
*/
|
|
protected function runModuleMigrationIfNeeded(): void
|
|
{
|
|
if (static::$migrated) {
|
|
return;
|
|
}
|
|
|
|
// 매 PHP process 첫 setUp 시 DB 를 완전 초기화 후 코어+모듈 마이그레이션을 처음부터
|
|
// 실행한다 (board 는 DatabaseTransactions 사용 — RefreshDatabase 가 아니므로 schema
|
|
// 자동 재구축 없음). 이전 process 의 부분 schema 잔재(예: 컬럼은 추가됐지만 migration
|
|
// record 누락) 로 인한 "Duplicate column" 등 충돌을 차단한다.
|
|
$this->artisan('migrate:fresh');
|
|
|
|
// 모듈 마이그레이션 실행 (코어 테이블 생성 후)
|
|
$this->artisan('migrate', [
|
|
'--path' => $this->getModuleBasePath().'/database/migrations',
|
|
'--realpath' => true,
|
|
]);
|
|
|
|
static::$migrated = true;
|
|
}
|
|
|
|
/**
|
|
* _bundled 디렉토리 모듈 인스턴스 + 훅 리스너 수동 등록.
|
|
*
|
|
* ModuleManager::loadModules() 는 modules/ (활성) 디렉토리만 스캔하고
|
|
* _bundled 는 메타데이터만 로드 (loadBundledModules) — 인스턴스/훅 미등록.
|
|
* 테스트 환경에서는 _bundled 에서 직접 실행하므로 module.php 의
|
|
* getHookListeners() 가 선언한 리스너들을 수동으로 등록해야 실제 부트 시점과 동일한 훅 흐름이 복원된다.
|
|
*/
|
|
protected function registerBundledModuleInstance(): void
|
|
{
|
|
$moduleClass = \Modules\Sirsoft\Board\Module::class;
|
|
|
|
if (! class_exists($moduleClass)) {
|
|
require_once $this->getModuleBasePath().'/module.php';
|
|
}
|
|
|
|
$module = new $moduleClass;
|
|
|
|
/** @var ModuleManager $manager */
|
|
$manager = $this->app->make(ModuleManager::class);
|
|
|
|
// ModuleManager.modules 에 인스턴스 주입
|
|
$reflection = new \ReflectionClass($manager);
|
|
$modulesProp = $reflection->getProperty('modules');
|
|
$modulesProp->setAccessible(true);
|
|
$current = $modulesProp->getValue($manager);
|
|
if (! isset($current['sirsoft-board'])) {
|
|
$current['sirsoft-board'] = $module;
|
|
$modulesProp->setValue($manager, $current);
|
|
}
|
|
|
|
// 훅 리스너 등록 — module.php 의 getHookListeners() 반환 클래스들을 HookListenerRegistrar 로 등록
|
|
if (method_exists($module, 'getHookListeners')) {
|
|
foreach ($module->getHookListeners() as $listenerClass) {
|
|
if (! class_exists($listenerClass)) {
|
|
continue;
|
|
}
|
|
if (! in_array(HookListenerInterface::class, class_implements($listenerClass), true)) {
|
|
continue;
|
|
}
|
|
try {
|
|
HookListenerRegistrar::register($listenerClass, 'sirsoft-board');
|
|
} catch (\Throwable $e) {
|
|
// 중복 등록 등 무해한 예외는 무시 (snapshot/restore 패턴이 정리)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 모듈을 활성화 상태로 등록합니다.
|
|
*/
|
|
protected function registerModuleAsActive(): void
|
|
{
|
|
// 이미 등록되어 있으면 스킵
|
|
if (Module::where('identifier', 'sirsoft-board')->exists()) {
|
|
return;
|
|
}
|
|
|
|
Module::create([
|
|
'identifier' => 'sirsoft-board',
|
|
'vendor' => 'sirsoft',
|
|
'name' => ['ko' => '게시판', 'en' => 'Board'],
|
|
'status' => ExtensionStatus::Active->value,
|
|
'version' => '1.0.0',
|
|
'config' => [],
|
|
]);
|
|
}
|
|
|
|
/**
|
|
* 모듈 오토로드를 등록합니다.
|
|
*/
|
|
protected function registerModuleAutoload(): void
|
|
{
|
|
$moduleBasePath = $this->getModuleBasePath().'/src/';
|
|
|
|
spl_autoload_register(function ($class) use ($moduleBasePath) {
|
|
$prefix = 'Modules\\Sirsoft\\Board\\';
|
|
$len = strlen($prefix);
|
|
|
|
if (strncmp($prefix, $class, $len) !== 0) {
|
|
return;
|
|
}
|
|
|
|
$relativeClass = substr($class, $len);
|
|
$file = $moduleBasePath.str_replace('\\', '/', $relativeClass).'.php';
|
|
|
|
if (file_exists($file)
|
|
&& ! class_exists($class, false) && ! interface_exists($class, false)
|
|
&& ! trait_exists($class, false) && ! enum_exists($class, false)) {
|
|
// 활성 디렉토리 사본이 이미 로드된 심볼을 다시 선언하면 fatal 이 된다 —
|
|
// 선언 여부를 자체 확인하고 require_once 로 이중 방어한다
|
|
require_once $file;
|
|
}
|
|
});
|
|
}
|
|
|
|
/**
|
|
* 모듈 라우트를 등록합니다.
|
|
*/
|
|
protected function registerModuleRoutes(): void
|
|
{
|
|
$apiRoutesFile = $this->getModuleBasePath().'/src/routes/api.php';
|
|
|
|
if (file_exists($apiRoutesFile)) {
|
|
Route::prefix('api/modules/sirsoft-board')
|
|
->name('api.modules.sirsoft-board.')
|
|
->middleware('api')
|
|
->group($apiRoutesFile);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 기본 역할들을 생성합니다.
|
|
*/
|
|
protected function createDefaultRoles(): void
|
|
{
|
|
Role::firstOrCreate(
|
|
['identifier' => 'admin'],
|
|
['name' => ['ko' => '관리자', 'en' => 'Administrator']]
|
|
);
|
|
|
|
Role::firstOrCreate(
|
|
['identifier' => 'user'],
|
|
['name' => ['ko' => '일반 사용자', 'en' => 'User']]
|
|
);
|
|
|
|
Role::firstOrCreate(
|
|
['identifier' => 'guest'],
|
|
['name' => ['ko' => '비회원', 'en' => 'Guest']]
|
|
);
|
|
|
|
}
|
|
|
|
/**
|
|
* 관리자 역할을 가진 사용자를 생성합니다.
|
|
*
|
|
* @param array $permissions 추가 권한 목록
|
|
* @return User
|
|
*/
|
|
protected function createAdminUser(array $permissions = []): User
|
|
{
|
|
$adminRole = Role::where('identifier', 'admin')->first();
|
|
$user = User::factory()->create();
|
|
$user->roles()->attach($adminRole->id);
|
|
|
|
// 추가 권한이 있으면 생성 및 할당
|
|
if (! empty($permissions)) {
|
|
foreach ($permissions as $permissionIdentifier) {
|
|
$permission = Permission::firstOrCreate(
|
|
['identifier' => $permissionIdentifier],
|
|
[
|
|
'name' => ['ko' => $permissionIdentifier, 'en' => $permissionIdentifier],
|
|
'type' => 'admin',
|
|
]
|
|
);
|
|
$adminRole->permissions()->syncWithoutDetaching([$permission->id]);
|
|
}
|
|
}
|
|
|
|
return $user;
|
|
}
|
|
|
|
/**
|
|
* 일반 사용자를 생성합니다.
|
|
*
|
|
* @return User
|
|
*/
|
|
protected function createUser(): User
|
|
{
|
|
$userRole = Role::where('identifier', 'user')->first();
|
|
$user = User::factory()->create();
|
|
$user->roles()->attach($userRole->id);
|
|
|
|
return $user;
|
|
}
|
|
}
|