Files
HeuJung d17ea28da5 fix(core,board,ecommerce,page,kginicis): 정책값 하드코딩·계약 분리 결함 일괄 수정
관리자 환경설정이 값을 저장은 하지만 실제 동작에는 닿지 않던 결함군을 정리했다.
설정 화면(min/max)·저장 검증(FormRequest)·런타임 적용(Service) 세 계층이 각자
리터럴을 들고 있어 서로 조용히 갈라지던 것이 공통 원인이다.

경계값 SSoT 도입:
- config/core.php `settings_limits` 를 단일 출처로 두고, FormRequest 는 이 값으로
 규칙을 만들고 화면은 설정 응답의 `_meta.limits` 로 같은 값을 받아 바인딩한다.
- 게시판·이커머스도 각 모듈 config 의 limits 를 동일 구조로 노출한다.

적용되지 않던 정책을 실제로 적용:
- 비밀번호 최소 길이·특수문자 필수(신규 지정 7개 경로. 로그인은 제외 — 정책을
 올렸을 때 기존 회원이 자기 계정에 접근조차 못 하는 데드락을 막기 위함)
- 계정 잠금 시간 0 = 무한대(전용 컬럼 분리. timestamp 상한과 NULL 규약 때문에
 기존 컬럼 재사용이 불가능하다) + 관리자 수동 해제 경로
- 업로드 최대 크기·허용 확장자, 게시판 첨부 개수·댓글 깊이, 페이지 첨부 정책,
 장바구니 수량·구매 한도, 마일리지 사용 한도, 주문 부가세

파생 결함:
- 큐 Job(sync 드라이버)이 진행 중인 요청의 Request 바인딩을 덮어써, 뒤이어 실행되는
 훅 리스너가 헤더를 잃고 조용히 건너뛰었다. Job 이 원 바인딩을 복원하도록 고치고,
 전송 헤더가 필요한 리스너는 동기 실행으로 선언한다.
- 확장 업데이트 실패 시 상태만 되돌리고 상태 캐시를 비우지 않아, 그 확장의 관리자
 화면만 캐시 TTL(기본 하루) 동안 404 로 남았다. 실패 경로에도 무효화를 대칭으로
 두고, 빈 목록 자체를 캐시하지 않는다.
- 글 작성 시 첨부 파일이 Service 로 전달되지 않아 검증·권한은 통과하고 첨부만 사라졌다.
- 환경설정 저장 실패 안내가 내부 설정 키를 그대로 노출했다(105개 항목에 라벨 부여).

재발 방지:
- audit 룰 3종(레이아웃 경계 리터럴 / FormRequest 경계 불일치 / Service 재클램프)
- 테스트 실행 범위 강제 훅(check-test-scope)
- 트러블슈팅 사례 2건(큐 Job 요청 오염 / 확장 상태 캐시 고착)

공개 이슈: gnuboard/g7
2026-07-30 07:52:24 +09:00

276 lines
9.4 KiB
PHP

<?php
namespace Modules\Sirsoft\Board\Tests\Feature;
require_once __DIR__.'/../ModuleTestCase.php';
use App\Models\Role;
use App\Models\User;
use Modules\Sirsoft\Board\Exceptions\CommentDepthExceededException;
use Modules\Sirsoft\Board\Services\CommentService;
use Modules\Sirsoft\Board\Tests\BoardTestCase;
/**
* 대댓글 깊이 제한 테스트
*
* 검증 목적:
* - 저장되는 depth 가 항상 부모 depth + 1 (하드코딩 상한 없음)
* - 게시판 설정 max_comment_depth 를 넘는 답글은 422
* - max 를 낮게(3) / 높게(10) 설정한 두 경우 모두 설정값을 따른다
*
* @group board
* @group comment
* @group depth
*/
class CommentDepthLimitTest extends BoardTestCase
{
private User $memberUser;
protected function getTestBoardSlug(): string
{
return 'comment-depth-limit';
}
protected function getDefaultBoardAttributes(string $slug): array
{
return [
'slug' => $slug,
'name' => ['ko' => '댓글 깊이 제한 게시판', 'en' => 'Comment Depth Limit Board'],
'is_active' => true,
'use_comment' => true,
'max_comment_depth' => 10,
];
}
protected function setUp(): void
{
parent::setUp();
$this->setGuestPermissions(['posts.read', 'posts.write', 'comments.read', 'comments.write']);
$this->grantUserRolePermissions(['posts.read', 'posts.write', 'comments.read', 'comments.write']);
$this->memberUser = User::factory()->create(['email' => 'comment-depth@test.com']);
$userRole = Role::where('identifier', 'user')->first();
if ($userRole) {
$this->memberUser->roles()->attach($userRole->id);
}
}
/**
* max_comment_depth=10 게시판에서 depth 는 1..10 까지 정확히 증가한다
*
* @scenario entity=board_comment, parent_target=unrelated
*
* @effects depth_equals_parent_depth_plus_one, depth_follows_board_setting_not_literal
*/
public function test_depth_increments_up_to_configured_max(): void
{
$postId = $this->createTestPost();
$parentId = $this->createTestComment($postId, ['depth' => 0]);
for ($expectedDepth = 1; $expectedDepth <= 10; $expectedDepth++) {
$response = $this->actingAs($this->memberUser)
->postJson($this->storeUrl($postId), [
'content' => "{$expectedDepth}단계 답글",
'parent_id' => $parentId,
]);
$response->assertStatus(201);
$parentId = (int) $response->json('data.id');
$this->assertDatabaseHas('board_comments', [
'id' => $parentId,
'depth' => $expectedDepth,
]);
}
}
/**
* max_comment_depth=10 게시판에서 11단계 답글은 422
*
* @scenario entity=board_comment, parent_target=unrelated
*
* @effects depth_beyond_board_max_rejected_422
*/
public function test_reply_beyond_configured_max_is_rejected(): void
{
$postId = $this->createTestPost();
// depth 10 인 부모 댓글을 직접 생성 → 11단계 시도
$parentId = $this->createTestComment($postId, ['depth' => 10]);
$this->actingAs($this->memberUser)
->postJson($this->storeUrl($postId), [
'content' => '11단계 답글 시도',
'parent_id' => $parentId,
])
->assertStatus(422)
->assertJsonValidationErrors(['parent_id']);
}
/**
* max_comment_depth=3 게시판에서 4단계 답글은 422
*
* @scenario entity=board_comment, parent_target=unrelated
*
* @effects depth_beyond_board_max_rejected_422, depth_follows_board_setting_not_literal
*/
public function test_reply_beyond_lowered_max_is_rejected(): void
{
$this->updateBoardSettings(['max_comment_depth' => 3]);
$postId = $this->createTestPost();
$parentId = $this->createTestComment($postId, ['depth' => 3]);
$this->actingAs($this->memberUser)
->postJson($this->storeUrl($postId), [
'content' => '4단계 답글 시도',
'parent_id' => $parentId,
])
->assertStatus(422)
->assertJsonValidationErrors(['parent_id']);
}
/**
* max_comment_depth=3 게시판에서 3단계까지는 정상 생성된다
*
* @scenario entity=board_comment, parent_target=unrelated
*
* @effects depth_equals_parent_depth_plus_one, depth_follows_board_setting_not_literal
*/
public function test_reply_within_lowered_max_is_allowed(): void
{
$this->updateBoardSettings(['max_comment_depth' => 3]);
$postId = $this->createTestPost();
$parentId = $this->createTestComment($postId, ['depth' => 2]);
$response = $this->actingAs($this->memberUser)
->postJson($this->storeUrl($postId), [
'content' => '3단계 답글',
'parent_id' => $parentId,
]);
$response->assertStatus(201);
$this->assertDatabaseHas('board_comments', [
'id' => (int) $response->json('data.id'),
'depth' => 3,
]);
}
/**
* max_comment_depth=0 게시판에서는 대댓글 자체가 불가능하다
*
* 0 은 "답글 없음" 설정이다. 상한을 리터럴로 클램프하거나 0 을 무제한으로 오해하면
* 이 게시판에서 대댓글이 그대로 달린다.
*
* @scenario entity=board_comment, parent_target=unrelated
*
* @effects depth_beyond_board_max_rejected_422, depth_follows_board_setting_not_literal
*/
public function test_reply_is_rejected_when_max_depth_is_zero(): void
{
$this->updateBoardSettings(['max_comment_depth' => 0]);
$postId = $this->createTestPost();
$parentId = $this->createTestComment($postId, ['depth' => 0]);
$this->actingAs($this->memberUser)
->postJson($this->storeUrl($postId), [
'content' => '대댓글 불가 게시판에서의 답글 시도',
'parent_id' => $parentId,
])
->assertStatus(422)
->assertJsonValidationErrors(['parent_id']);
}
/**
* max_comment_depth=0 이어도 최상위 댓글은 정상 작성된다 (과잉 차단 방지)
*
* @scenario entity=board_comment, parent_target=unrelated
*
* @effects depth_follows_board_setting_not_literal
*/
public function test_top_level_comment_is_allowed_when_max_depth_is_zero(): void
{
$this->updateBoardSettings(['max_comment_depth' => 0]);
$postId = $this->createTestPost();
$this->actingAs($this->memberUser)
->postJson($this->storeUrl($postId), ['content' => '최상위 댓글'])
->assertStatus(201);
}
/**
* 댓글 생성 엔드포인트 URL 을 생성합니다.
*
* @param int $postId 게시글 ID
* @return string 댓글 생성 URL
*/
private function storeUrl(int $postId): string
{
return "/api/modules/sirsoft-board/boards/{$this->board->slug}/posts/{$postId}/comments";
}
/**
* FormRequest 를 거치지 않는 경로(훅 · Service 직접 호출)에서도 깊이 상한이 지켜진다
*
* `CommentValidationRule` 은 요청 단계 선차단이라 Service 를 직접 부르는 확장 코드에는
* 걸리지 않는다. 첨부 개수(B3)가 Service 를 최종 불변조건으로 삼은 것과 같은 이유로,
* 깊이 상한도 Service 가 마지막 관문이어야 한다.
*
* @scenario entity=board_comment, parent_target=unrelated
*
* @effects depth_beyond_board_max_rejected_422
*/
public function test_service_direct_call_rejects_reply_beyond_max_depth(): void
{
$this->updateBoardSettings(['max_comment_depth' => 2]);
$postId = $this->createTestPost();
$depth0 = $this->createTestComment($postId, ['depth' => 0]);
$depth1 = $this->createTestComment($postId, ['parent_id' => $depth0, 'depth' => 1]);
$depth2 = $this->createTestComment($postId, ['parent_id' => $depth1, 'depth' => 2]);
$service = app(CommentService::class);
$this->expectException(CommentDepthExceededException::class);
// depth 3 = 상한 2 초과 → Service 가 막아야 한다
$service->createComment($this->board->slug, [
'post_id' => $postId,
'parent_id' => $depth2,
'content' => 'FormRequest 를 우회한 깊이 초과 답글',
'user_id' => $this->memberUser->id,
]);
}
/**
* 상한 이내의 Service 직접 호출은 그대로 통과한다 (과잉 차단 방지)
*
* @scenario entity=board_comment, parent_target=unrelated
*
* @effects depth_follows_board_setting_not_literal
*/
public function test_service_direct_call_allows_reply_within_max_depth(): void
{
$this->updateBoardSettings(['max_comment_depth' => 2]);
$postId = $this->createTestPost();
$depth0 = $this->createTestComment($postId, ['depth' => 0]);
$comment = app(CommentService::class)->createComment($this->board->slug, [
'post_id' => $postId,
'parent_id' => $depth0,
'content' => '상한 이내 답글',
'user_id' => $this->memberUser->id,
]);
$this->assertSame(1, (int) $comment->depth);
}
}