seed(ShippingTypeSeeder::class); $this->adminUser = $this->createAdminUser([ 'sirsoft-ecommerce.shipping-policies.create', ]); } private function krSetting(array $overrides = []): array { return array_merge([ 'country_code' => 'KR', 'shipping_method' => 'parcel', 'currency_code' => 'KRW', 'charge_policy' => 'fixed', 'base_fee' => 3000, 'free_threshold' => null, 'ranges' => null, 'api_endpoint' => null, 'api_request_fields' => null, 'api_response_fee_field' => null, 'extra_fee_enabled' => false, 'extra_fee_settings' => null, 'extra_fee_multiply' => false, 'is_active' => true, ], $overrides); } private function payload(array $countrySettings): array { return [ 'name' => ['ko' => '테스트정책', 'en' => 'Test Policy'], 'is_active' => true, 'is_default' => false, 'country_settings' => $countrySettings, ]; } private function store(array $payload, string $locale = 'ko') { // SetLocale 미들웨어는 인증 사용자의 language 를 최우선 적용하므로 해당 값을 세팅 $this->adminUser->forceFill(['language' => $locale])->save(); return $this->actingAs($this->adminUser)->postJson($this->apiBase, $payload); } /** * 검증 메시지 값(error key 가 아닌 message 텍스트)에 raw 키가 노출되지 않았는지 확인. * * Laravel 의 errors 객체 key 는 정상적으로 필드 경로(country_settings.0.fee)지만, * 사용자에게 보이는 message 값에는 :attribute placeholder 미치환 키나 lang 네임스페이스가 * 들어가면 안 된다. */ private function assertNoRawKeys($response): void { foreach ($this->allMessages($response) as $message) { $this->assertStringNotContainsString('country_settings.', $message, "raw key in: {$message}"); $this->assertStringNotContainsString('sirsoft-ecommerce::validation', $message, "lang namespace in: {$message}"); // humanize 폴백(예: "country settings.0.ranges.tiers.1.fee") 차단 $this->assertStringNotContainsString('country settings', $message, "humanized raw key in: {$message}"); } } /** * errors 객체의 모든 메시지 값을 평탄화하여 반환합니다. * * (Laravel error key 는 dot 을 포함하므로 json('errors.x.y') 점표기 접근이 부정확 → * 전체 errors 를 받아 메시지 값만 평탄화) * * @return array */ private function allMessages($response): array { $errors = $response->json('errors') ?? []; $flat = []; foreach ($errors as $messages) { foreach ((array) $messages as $message) { $flat[] = $message; } } return $flat; } // ───────── A12: 구간 배송비 음수 ───────── #[Test] public function test_negative_tier_fee_returns_friendly_message(): void { $cs = $this->krSetting([ 'charge_policy' => 'range_amount', 'ranges' => [ 'type' => 'price', 'unit_value' => 1000, 'tiers' => [ ['min' => 0, 'max' => null, 'fee' => -500], ], ], ]); $response = $this->store($this->payload([$cs])); $response->assertStatus(422); $this->assertNoRawKeys($response); $this->assertStringContainsString('0 이상', implode(' ', $this->allMessages($response))); } #[Test] public function test_negative_unit_value_returns_friendly_message(): void { $cs = $this->krSetting([ 'charge_policy' => 'range_amount', 'ranges' => [ 'type' => 'price', 'unit_value' => -10, 'tiers' => [ ['min' => 0, 'max' => null, 'fee' => 1000], ], ], ]); $response = $this->store($this->payload([$cs])); $response->assertStatus(422); $this->assertNoRawKeys($response); } // ───────── A12: en locale ───────── #[Test] public function test_negative_tier_fee_en_locale(): void { $cs = $this->krSetting([ 'charge_policy' => 'range_amount', 'ranges' => [ 'type' => 'price', 'unit_value' => 1000, 'tiers' => [ ['min' => 0, 'max' => null, 'fee' => -500], ], ], ]); $response = $this->store($this->payload([$cs]), 'en'); $response->assertStatus(422); $this->assertNoRawKeys($response); $this->assertStringContainsString('0 or greater', $response->getContent()); } // ───────── A13 W1: api_endpoint 필수 ───────── #[Test] public function test_api_policy_requires_endpoint(): void { $cs = $this->krSetting([ 'charge_policy' => 'api', 'api_endpoint' => '', ]); $response = $this->store($this->payload([$cs])); $response->assertStatus(422); $this->assertNoRawKeys($response); $this->assertStringContainsString('API 주소', implode(' ', $this->allMessages($response))); } #[Test] public function test_non_api_policy_allows_null_endpoint(): void { // 비-API 정책은 endpoint null 허용 (회귀 가드) $cs = $this->krSetting(['charge_policy' => 'fixed', 'api_endpoint' => null]); $response = $this->store($this->payload([$cs])); $response->assertSuccessful(); } #[Test] public function test_api_policy_with_valid_endpoint_passes(): void { $cs = $this->krSetting([ 'charge_policy' => 'api', 'api_endpoint' => 'https://example.com/calc', 'api_request_fields' => ['items', 'group_total'], ]); $response = $this->store($this->payload([$cs])); $response->assertSuccessful(); } // ───────── A13 W2: 잘못된 URL ───────── #[Test] public function test_invalid_url_returns_friendly_message(): void { $cs = $this->krSetting([ 'charge_policy' => 'api', 'api_endpoint' => 'not-a-url', ]); $response = $this->store($this->payload([$cs])); $response->assertStatus(422); $this->assertNoRawKeys($response); $this->assertStringContainsString('URL', $response->getContent()); } // ───────── A13 W3: api_request_fields Rule::in ───────── #[Test] public function test_unsupported_request_field_rejected(): void { $cs = $this->krSetting([ 'charge_policy' => 'api', 'api_endpoint' => 'https://example.com/calc', 'api_request_fields' => ['weight'], // 후보 외 ]); $response = $this->store($this->payload([$cs])); $response->assertStatus(422); $this->assertNoRawKeys($response); } #[Test] public function test_supported_request_fields_accepted(): void { $cs = $this->krSetting([ 'charge_policy' => 'api', 'api_endpoint' => 'https://example.com/calc', 'api_request_fields' => ['policy_id', 'country_code', 'items', 'group_total', 'total_quantity'], ]); $response = $this->store($this->payload([$cs])); $response->assertSuccessful(); } // ───────── MP12: 계산 API 고급 설정(api_config) ───────── #[Test] public function test_api_config_full_settings_accepted(): void { $cs = $this->krSetting([ 'charge_policy' => 'api', 'api_endpoint' => 'https://example.com/calc', 'api_request_fields' => ['policy_id', 'group_total'], 'api_config' => [ 'http_method' => 'GET', 'auth_type' => 'bearer', 'auth_token' => 'tok-123', 'response_type' => 'json', 'response_path' => 'data.fee', 'field_map' => ['policy_id' => 'policyId'], ], ]); $response = $this->store($this->payload([$cs])); $response->assertSuccessful(); } #[Test] public function test_custom_header_auth_requires_header_name(): void { $cs = $this->krSetting([ 'charge_policy' => 'api', 'api_endpoint' => 'https://example.com/calc', 'api_config' => [ 'auth_type' => 'custom_header', 'auth_token' => 'key-abc', // auth_header_name 누락 ], ]); $response = $this->store($this->payload([$cs])); $response->assertStatus(422); $this->assertNoRawKeys($response); } #[Test] public function test_field_map_rejects_injection_characters(): void { $cs = $this->krSetting([ 'charge_policy' => 'api', 'api_endpoint' => 'https://example.com/calc', 'api_config' => [ 'field_map' => ['policy_id' => "bad\r\nX-Inject: 1"], ], ]); $response = $this->store($this->payload([$cs])); $response->assertStatus(422); $this->assertNoRawKeys($response); } #[Test] public function test_invalid_http_method_rejected(): void { $cs = $this->krSetting([ 'charge_policy' => 'api', 'api_endpoint' => 'https://example.com/calc', 'api_config' => ['http_method' => 'DELETE'], ]); $response = $this->store($this->payload([$cs])); $response->assertStatus(422); } // ───────── #94: 구간 필수값·경계 규칙 ───────── #[Test] public function test_range_policy_without_tiers_is_rejected(): void { // 구간이 없으면 모든 주문이 조용히 0원(무료배송)이 된다 $cs = $this->krSetting([ 'charge_policy' => 'range_quantity', 'base_fee' => 0, 'ranges' => ['type' => 'quantity', 'tiers' => []], ]); $response = $this->store($this->payload([$cs])); $response->assertStatus(422); $this->assertNoRawKeys($response); $this->assertStringContainsString('구간을 1개 이상', implode(' ', $this->allMessages($response))); } #[Test] public function test_per_unit_policy_without_unit_value_is_rejected(): void { $cs = $this->krSetting([ 'charge_policy' => 'per_weight', 'base_fee' => 1000, 'ranges' => ['type' => 'per_weight'], ]); $response = $this->store($this->payload([$cs])); $response->assertStatus(422); $this->assertNoRawKeys($response); $this->assertStringContainsString('단위값', implode(' ', $this->allMessages($response))); } #[Test] public function test_conditional_free_without_threshold_is_rejected(): void { $cs = $this->krSetting([ 'charge_policy' => 'conditional_free', 'base_fee' => 3000, 'free_threshold' => null, ]); $response = $this->store($this->payload([$cs])); $response->assertStatus(422); $this->assertNoRawKeys($response); $this->assertStringContainsString('무료배송 기준금액', implode(' ', $this->allMessages($response))); } #[Test] public function test_api_policy_with_zero_base_fee_is_rejected(): void { // API 폴백 배송비가 0 이면 외부 API 장애가 곧 무음 무료배송이 된다 $cs = $this->krSetting([ 'charge_policy' => 'api', 'base_fee' => 0, 'api_endpoint' => 'https://example.com/calc', ]); $response = $this->store($this->payload([$cs])); $response->assertStatus(422); $this->assertNoRawKeys($response); } #[Test] public function test_middle_tier_without_max_is_rejected(): void { // 중간 구간의 종료값이 비면 뒤 구간이 영구히 도달 불가해진다 $cs = $this->krSetting([ 'charge_policy' => 'range_weight', 'base_fee' => 0, 'ranges' => [ 'type' => 'weight', 'tiers' => [ ['min' => 0, 'max' => null, 'fee' => 3000], ['min' => 2, 'max' => null, 'fee' => 5000], ], ], ]); $response = $this->store($this->payload([$cs])); $response->assertStatus(422); $this->assertNoRawKeys($response); $this->assertStringContainsString('종료값을 입력', implode(' ', $this->allMessages($response))); } #[Test] public function test_continuous_range_accepts_next_min_equal_to_previous_max(): void { $cs = $this->krSetting([ 'charge_policy' => 'range_weight', 'base_fee' => 0, 'ranges' => [ 'type' => 'weight', 'tiers' => [ ['min' => 0, 'max' => 2, 'fee' => 3000], ['min' => 2, 'max' => null, 'fee' => 5000], ], ], ]); $this->store($this->payload([$cs]))->assertStatus(201); } #[Test] public function test_continuous_range_rejects_legacy_max_plus_one_gap(): void { // 구 규칙(max + 1)은 2 초과 3 미만 구간에 간극을 만든다 $cs = $this->krSetting([ 'charge_policy' => 'range_weight', 'base_fee' => 0, 'ranges' => [ 'type' => 'weight', 'tiers' => [ ['min' => 0, 'max' => 2, 'fee' => 3000], ['min' => 3, 'max' => null, 'fee' => 5000], ], ], ]); $response = $this->store($this->payload([$cs])); $response->assertStatus(422); $this->assertStringContainsString('연속', implode(' ', $this->allMessages($response))); } #[Test] public function test_continuous_range_accepts_decimal_boundary(): void { // 소수 구간(2.5kg)은 정상 설정이다 — 구 max+1 규칙에서는 저장 자체가 불가했다 $cs = $this->krSetting([ 'charge_policy' => 'range_weight', 'base_fee' => 0, 'ranges' => [ 'type' => 'weight', 'tiers' => [ ['min' => 0, 'max' => 2.5, 'fee' => 3000], ['min' => 2.5, 'max' => null, 'fee' => 5000], ], ], ]); $this->store($this->payload([$cs]))->assertStatus(201); } #[Test] public function test_discrete_quantity_range_accepts_max_plus_one(): void { // 수량은 이산형이라 "5개 다음은 6개"가 맞다 (공개 #94 본문 설정) $cs = $this->krSetting([ 'charge_policy' => 'range_quantity', 'base_fee' => 0, 'ranges' => [ 'type' => 'quantity', 'tiers' => [ ['min' => 0, 'max' => 5, 'fee' => 3000], ['min' => 6, 'max' => null, 'fee' => 5000], ], ], ]); $this->store($this->payload([$cs]))->assertStatus(201); } #[Test] public function test_discrete_quantity_range_rejects_fractional_boundary(): void { $cs = $this->krSetting([ 'charge_policy' => 'range_quantity', 'base_fee' => 0, 'ranges' => [ 'type' => 'quantity', 'tiers' => [ ['min' => 0, 'max' => 5.5, 'fee' => 3000], ['min' => 6.5, 'max' => null, 'fee' => 5000], ], ], ]); $response = $this->store($this->payload([$cs])); $response->assertStatus(422); $this->assertStringContainsString('정수', implode(' ', $this->allMessages($response))); } // ───────── #94: 도서산간 우편번호 형식 ───────── #[Test] public function test_extra_fee_accepts_three_supported_zipcode_formats(): void { $cs = $this->krSetting([ 'extra_fee_enabled' => true, 'extra_fee_settings' => [ ['zipcode' => '63000', 'fee' => 3000, 'region' => '정확일치'], ['zipcode' => '63000-63644', 'fee' => 3000, 'region' => '범위'], ['zipcode' => '63*', 'fee' => 3000, 'region' => '접두 와일드카드'], ], ]); $this->store($this->payload([$cs]))->assertStatus(201); } #[Test] public function test_extra_fee_rejects_unsupported_zipcode_format(): void { // 어떤 우편번호에도 매칭되지 않아 추가배송비가 조용히 누락된다 $cs = $this->krSetting([ 'extra_fee_enabled' => true, 'extra_fee_settings' => [ ['zipcode' => '제주 63*', 'fee' => 3000, 'region' => '오형식'], ], ]); $response = $this->store($this->payload([$cs])); $response->assertStatus(422); $this->assertNoRawKeys($response); $this->assertStringContainsString('우편번호', implode(' ', $this->allMessages($response))); } }