Files
Gnuboard7/tests/Feature/Api/Admin/ModuleUpdateTest.php
T
HeuJung 9025f6d1aa fix(core,ecommerce,gdpr,message_bizppurio,hello_module,basic,admin_basic): 리베이스 후속 정합 정리
develop 리베이스 3커밋이 각자 정상인데 합쳐지면서 어긋난 지점들을 정리한다.

- 발송 이력 목록이 뒤 페이지로 갈수록 느려지고 같은 시각 발송분이 중복
 노출되던 두 결함을 코어 지연 조인으로 함께 닫았다. 이 파일은 develop 쪽에서
 들어와 492 브랜치에서는 검출될 수 없었다.
- 정렬 허용 컬럼·지연 조인 호출 계약 테스트가 순수 소스 스캔인데도 앱을 부팅해
 케이스마다 DB 연결을 잡았다. 스캔 대상이 늘자 연결 상한(151)을 넘겨 실패했고,
 앱 의존이 없으므로 부팅을 뺐다.
- 확장 업데이트 모달 테스트는 stub 이 모든 체크박스에 같은 식별자를 붙여,
 검색 인덱스 재생성 체크박스가 추가되자 대상을 특정하지 못했다.
- 동의 이력 필터 어휘가 enum 전 케이스로 넓어진 것을 테스트 기대값에 반영하고,
 이력 페이지네이션 버튼의 액션 구조를 나머지와 같은 형태로 통일했다.
- 관리자 확장 화면의 존재 확인 가드가 추가된 뒤 갱신되지 않은 mock 을 맞췄다.

다크 모드 대비가 빠진 버튼 둘을 저장소 관례에 맞추고, 사용자 템플릿에서
그 클래스가 실제 CSS 로 생성되도록 safelist 를 함께 채웠다.
2026-08-02 16:42:52 +09:00

507 lines
18 KiB
PHP

<?php
namespace Tests\Feature\Api\Admin;
use App\Enums\ExtensionOwnerType;
use App\Enums\PermissionType;
use App\Models\Permission;
use App\Models\Role;
use App\Models\User;
use App\Services\ModuleService;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\Route;
use Illuminate\Validation\ValidationException;
use Mockery;
use Tests\TestCase;
/**
* 모듈 업데이트 API 엔드포인트 테스트
*
* checkUpdates, performUpdate 엔드포인트를 검증합니다.
*/
class ModuleUpdateTest extends TestCase
{
use RefreshDatabase;
private User $admin;
private string $token;
protected function setUp(): void
{
parent::setUp();
$this->admin = $this->createAdminUser();
$this->token = $this->admin->createToken('test-token')->plainTextToken;
}
/**
* 관리자 역할 생성 및 할당
*
* @param array $permissions 사용자에게 부여할 권한 식별자 목록
* @return User 생성된 관리자 사용자
*/
private function createAdminUser(array $permissions = ['core.modules.read', 'core.modules.install', 'core.modules.activate', 'core.modules.uninstall']): User
{
$user = User::factory()->create();
$permissionIds = [];
foreach ($permissions as $permIdentifier) {
$permission = Permission::firstOrCreate(
['identifier' => $permIdentifier],
[
'name' => json_encode(['ko' => $permIdentifier, 'en' => $permIdentifier]),
'description' => json_encode(['ko' => $permIdentifier.' 권한', 'en' => $permIdentifier.' Permission']),
'extension_type' => ExtensionOwnerType::Core,
'extension_identifier' => 'core',
'type' => PermissionType::Admin,
]
);
$permissionIds[] = $permission->id;
}
$roleIdentifier = 'admin_test_'.uniqid();
$testRole = Role::create([
'identifier' => $roleIdentifier,
'name' => json_encode(['ko' => '테스트 관리자', 'en' => 'Test Administrator']),
'description' => json_encode(['ko' => '테스트 관리자', 'en' => 'Test Administrator']),
'is_active' => true,
]);
$adminRole = Role::firstOrCreate(
['identifier' => 'admin'],
[
'name' => json_encode(['ko' => '관리자', 'en' => 'Administrator']),
'description' => json_encode(['ko' => '시스템 관리자', 'en' => 'System Administrator']),
'extension_type' => ExtensionOwnerType::Core,
'extension_identifier' => 'core',
'is_active' => true,
]
);
$testRole->permissions()->sync($permissionIds);
$user->roles()->attach($adminRole->id, [
'assigned_at' => now(),
'assigned_by' => null,
]);
$user->roles()->attach($testRole->id, [
'assigned_at' => now(),
'assigned_by' => null,
]);
return $user->fresh();
}
/**
* 인증된 요청 헬퍼 메서드
*/
private function authRequest(): static
{
return $this->withHeaders([
'Authorization' => 'Bearer '.$this->token,
'Accept' => 'application/json',
]);
}
// ========================================================================
// checkUpdates 엔드포인트 테스트
// ========================================================================
/**
* 인증 없이 업데이트 확인 시 401 반환
*/
public function test_check_updates_returns_401_without_authentication(): void
{
$response = $this->postJson('/api/admin/modules/check-updates');
$response->assertStatus(401);
}
/**
* 업데이트 확인 성공 시 결과 반환
*/
public function test_check_updates_returns_update_results(): void
{
$mockService = Mockery::mock(ModuleService::class);
$mockService->shouldReceive('checkForUpdates')
->once()
->andReturn([
'updated_count' => 1,
'details' => [
'test-module' => [
'update_available' => true,
'update_source' => 'bundled',
'latest_version' => '2.0.0',
'current_version' => '1.0.0',
],
],
]);
$this->app->instance(ModuleService::class, $mockService);
$response = $this->authRequest()->postJson('/api/admin/modules/check-updates');
$response->assertStatus(200)
->assertJsonPath('success', true)
->assertJsonPath('data.updated_count', 1)
->assertJsonStructure([
'success',
'message',
'data' => [
'updated_count',
'details',
],
]);
}
/**
* 업데이트 없을 때 updated_count = 0 반환
*/
public function test_check_updates_returns_zero_when_no_updates(): void
{
$mockService = Mockery::mock(ModuleService::class);
$mockService->shouldReceive('checkForUpdates')
->once()
->andReturn([
'updated_count' => 0,
'details' => [],
]);
$this->app->instance(ModuleService::class, $mockService);
$response = $this->authRequest()->postJson('/api/admin/modules/check-updates');
$response->assertStatus(200)
->assertJsonPath('data.updated_count', 0);
}
/**
* 업데이트 확인 실패 시 422 반환
*/
public function test_check_updates_returns_422_on_validation_error(): void
{
$mockService = Mockery::mock(ModuleService::class);
$mockService->shouldReceive('checkForUpdates')
->once()
->andThrow(
ValidationException::withMessages([
'modules' => [__('modules.check_updates_failed', ['error' => 'Test error'])],
])
);
$this->app->instance(ModuleService::class, $mockService);
$response = $this->authRequest()->postJson('/api/admin/modules/check-updates');
$response->assertStatus(422);
}
/**
* 권한 없이 업데이트 확인 시 403 반환
*/
public function test_check_updates_returns_403_without_permission(): void
{
$userWithoutPermission = $this->createAdminUser(['core.modules.read']);
$token = $userWithoutPermission->createToken('test-token')->plainTextToken;
$response = $this->withHeaders([
'Authorization' => 'Bearer '.$token,
'Accept' => 'application/json',
])->postJson('/api/admin/modules/check-updates');
$response->assertStatus(403);
}
// ========================================================================
// performUpdate 엔드포인트 테스트
// ========================================================================
/**
* 인증 없이 모듈 업데이트 시 401 반환
*/
public function test_perform_update_returns_401_without_authentication(): void
{
$response = $this->postJson('/api/admin/modules/test-module/update');
$response->assertStatus(401);
}
/**
* 모듈 업데이트 성공 시 200과 모듈 정보 반환
*/
public function test_perform_update_returns_module_info_on_success(): void
{
$mockService = Mockery::mock(ModuleService::class);
$mockService->shouldReceive('updateModule')
->with('test-module', Mockery::any(), 'overwrite', false)
->once()
->andReturn([
'success' => true,
'from_version' => '1.0.0',
'to_version' => '2.0.0',
'module_info' => [
'identifier' => 'test-module',
'vendor' => 'test',
'name' => ['ko' => '테스트 모듈', 'en' => 'Test Module'],
'version' => '2.0.0',
'description' => ['ko' => '테스트용', 'en' => 'For testing'],
'dependencies' => [],
'status' => 'active',
'assets' => null,
'update_available' => false,
'update_source' => null,
'latest_version' => null,
'file_version' => null,
'github_url' => null,
'github_changelog_url' => null,
'is_pending' => false,
'is_bundled' => false,
],
]);
$this->app->instance(ModuleService::class, $mockService);
$response = $this->authRequest()->postJson('/api/admin/modules/test-module/update');
$response->assertStatus(200)
->assertJsonPath('success', true)
->assertJsonPath('data.identifier', 'test-module')
->assertJsonPath('data.version', '2.0.0')
->assertJsonPath('data.update_available', false)
->assertJsonStructure([
'success',
'message',
'data' => [
'identifier',
'vendor',
'name',
'version',
'status',
'update_available',
'update_source',
'latest_version',
'is_pending',
'is_bundled',
],
]);
}
/**
* 모듈 업데이트 시 ModuleResource의 업데이트 관련 필드가 포함되는지 확인
*/
public function test_perform_update_response_includes_update_fields(): void
{
$mockService = Mockery::mock(ModuleService::class);
$mockService->shouldReceive('updateModule')
->with('test-module', Mockery::any(), 'overwrite', false)
->once()
->andReturn([
'success' => true,
'from_version' => '1.0.0',
'to_version' => '2.0.0',
'module_info' => [
'identifier' => 'test-module',
'vendor' => 'test',
'name' => ['ko' => '테스트', 'en' => 'Test'],
'version' => '2.0.0',
'description' => ['ko' => '테스트', 'en' => 'Test'],
'dependencies' => [],
'status' => 'active',
'assets' => null,
'update_available' => false,
'update_source' => null,
'latest_version' => '2.0.0',
'file_version' => '2.0.0',
'github_url' => 'https://github.com/test/test-module',
'github_changelog_url' => 'https://github.com/test/test-module/releases',
'is_pending' => false,
'is_bundled' => true,
],
]);
$this->app->instance(ModuleService::class, $mockService);
$response = $this->authRequest()->postJson('/api/admin/modules/test-module/update');
$response->assertStatus(200)
->assertJsonPath('data.github_url', 'https://github.com/test/test-module')
->assertJsonPath('data.github_changelog_url', 'https://github.com/test/test-module/releases')
->assertJsonPath('data.is_bundled', true)
->assertJsonPath('data.is_pending', false)
->assertJsonPath('data.file_version', '2.0.0');
}
/**
* 존재하지 않는 모듈 업데이트 시 422 반환
*/
public function test_perform_update_returns_422_for_nonexistent_module(): void
{
$mockService = Mockery::mock(ModuleService::class);
$mockService->shouldReceive('updateModule')
->with('nonexistent-module', Mockery::any(), 'overwrite', false)
->once()
->andThrow(
ValidationException::withMessages([
'module_name' => [__('modules.errors.update_failed', ['module' => 'nonexistent-module', 'error' => 'Module not found'])],
])
);
$this->app->instance(ModuleService::class, $mockService);
$response = $this->authRequest()->postJson('/api/admin/modules/nonexistent-module/update');
$response->assertStatus(422);
}
/**
* 모듈 업데이트 중 서버 에러 시 500 반환
*/
public function test_perform_update_returns_500_on_server_error(): void
{
$mockService = Mockery::mock(ModuleService::class);
$mockService->shouldReceive('updateModule')
->with('test-module', Mockery::any(), 'overwrite', false)
->once()
->andThrow(new \RuntimeException('Unexpected error'));
$this->app->instance(ModuleService::class, $mockService);
$response = $this->authRequest()->postJson('/api/admin/modules/test-module/update');
$response->assertStatus(500);
}
/**
* 권한 없이 모듈 업데이트 시 403 반환
*/
public function test_perform_update_returns_403_without_permission(): void
{
$userWithoutPermission = $this->createAdminUser(['core.modules.read']);
$token = $userWithoutPermission->createToken('test-token')->plainTextToken;
$response = $this->withHeaders([
'Authorization' => 'Bearer '.$token,
'Accept' => 'application/json',
])->postJson('/api/admin/modules/test-module/update');
$response->assertStatus(403);
}
// ========================================================================
// 라우트 확인 테스트
// ========================================================================
/**
* check-updates 라우트가 올바른 이름을 가지는지 확인
*/
public function test_check_updates_route_name_exists(): void
{
$this->assertTrue(
Route::has('api.admin.modules.check-updates'),
'Route api.admin.modules.check-updates should exist'
);
}
/**
* update 라우트가 올바른 이름을 가지는지 확인
*/
public function test_update_route_name_exists(): void
{
$this->assertTrue(
Route::has('api.admin.modules.update'),
'Route api.admin.modules.update should exist'
);
}
// ========================================================================
// layout_strategy 파라미터 전달 검증
// ========================================================================
/**
* performUpdate 가 layout_strategy='keep' 을 Service 에 전달하는지 확인
*/
public function test_perform_update_sends_layout_strategy_keep_to_service(): void
{
$mockService = Mockery::mock(ModuleService::class);
$mockService->shouldReceive('updateModule')
->with('test-module', Mockery::any(), 'keep', false)
->once()
->andReturn([
'success' => true,
'from_version' => '1.0.0',
'to_version' => '2.0.0',
'module_info' => null,
]);
$this->app->instance(ModuleService::class, $mockService);
$response = $this->authRequest()->postJson('/api/admin/modules/test-module/update', [
'layout_strategy' => 'keep',
]);
$response->assertStatus(200);
}
/**
* performUpdate 는 유효하지 않은 layout_strategy 를 거부 (422)
*/
public function test_perform_update_rejects_invalid_layout_strategy(): void
{
$response = $this->authRequest()->postJson('/api/admin/modules/test-module/update', [
'layout_strategy' => 'garbage',
]);
$response->assertStatus(422);
}
// ========================================================================
// checkModifiedLayouts 엔드포인트 테스트
// ========================================================================
/**
* checkModifiedLayouts 는 Service 결과를 그대로 반환한다
*/
public function test_check_modified_layouts_returns_result_from_service(): void
{
$mockService = Mockery::mock(ModuleService::class);
// 컨트롤러가 미존재 식별자를 404 로 구분하려 존재 확인을 먼저 한다 (#495).
// 이 스텁이 없으면 mock 이 BadMethodCallException 을 던져 500 이 된다.
$mockService->shouldReceive('getModuleInfo')
->with('test-module')
->andReturn(['identifier' => 'test-module', 'version' => '1.0.0']);
$mockService->shouldReceive('checkModifiedLayouts')
->with('test-module')
->once()
->andReturn([
'has_modified_layouts' => true,
'modified_count' => 2,
'modified_layouts' => [
['id' => 1, 'name' => 'test-module.home', 'updated_at' => '2026-04-19 10:00:00', 'size_diff' => 128],
['id' => 2, 'name' => 'test-module.list', 'updated_at' => '2026-04-19 10:05:00', 'size_diff' => -64],
],
]);
$this->app->instance(ModuleService::class, $mockService);
$response = $this->authRequest()->getJson('/api/admin/modules/test-module/check-modified-layouts');
$response->assertStatus(200)
->assertJsonPath('success', true)
->assertJsonPath('data.has_modified_layouts', true)
->assertJsonPath('data.modified_count', 2);
}
/**
* checkModifiedLayouts 는 인증이 없으면 401 반환
*/
public function test_check_modified_layouts_returns_401_without_authentication(): void
{
$response = $this->getJson('/api/admin/modules/test-module/check-modified-layouts');
$response->assertStatus(401);
}
/**
* check-modified-layouts 라우트 네임 확인
*/
public function test_check_modified_layouts_route_name_exists(): void
{
$this->assertTrue(
Route::has('api.admin.modules.check-modified-layouts'),
'Route api.admin.modules.check-modified-layouts should exist'
);
}
}