Files
Gnuboard7/plugins/_bundled/sirsoft-message_bizppurio/tests/Unit/Services/AlimtalkPayloadMapperTest.php
T
HeuJung e17731d8cc feat(message_bizppurio): 알림톡 템플릿 라이프사이클 개편 — DB 기반 작성·검수·승인
비즈뿌리오 알림톡 템플릿을 콘솔 위임(발송 전 실시간 조회) 방식에서 시스템 내
작성(draft) → 검수 신청(requested) → 승인(approved) 라이프사이클로 개편한다.
발송 판정의 SSoT 를 DB(bizppurio_templates)로 옮기고 발송 전 실시간 조회를 폐지.

- 알림↔템플릿 바인딩 모델(notification_bindings) 제거, 알림 1건당 템플릿 1행으로 대체
- 검수 신청: template_code 자체 채번(codeCheck 재시도) → kapi add/update → request
- 상태 동기화: 30분 스케줄러 + 수동 새로고침, 승인 전이 시 content 를 approved_content 로 동결
- SMS 본문 언어별 입력 복원(수신자 로케일 발송), SMS 단독 선택 시 알림톡 미발송
- 3면(코어/게시판/이커머스) '비즈뿌리오' 통합 탭 + 플러그인 '알림 템플릿 관리' 화면
- 중복 검수 신청은 서버 원자 선점(claimForInspection 조건부 UPDATE)으로 차단
- admin_basic 1.0.6 / board 1.0.5 / ecommerce 1.1.2: 채널 서브탭 통합 탭 선언(tab_channels) 지원
- 검증: 계획서 대비 5라운드 전수검증, §6.3 실측 매트릭스 Playwright 19 고정,
 PHPUnit 421 / Vitest 228 green, audit 0 error
2026-08-22 22:44:57 +09:00

348 lines
15 KiB
PHP

<?php
namespace Plugins\Sirsoft\MessageBizppurio\Tests\Unit\Services;
use Plugins\Sirsoft\MessageBizppurio\Services\AlimtalkPayloadMapper;
use Plugins\Sirsoft\MessageBizppurio\Tests\PluginTestCase;
/**
* AlimtalkPayloadMapper — 카카오 상세조회 응답 → 발송 API content.at 변환 + 변수 치환 검증 (B안 5-2).
*
* 카카오 관리 API 필드(templateContent/buttons.linkMo/…)를 발송 API 필드
* (message/button.url_mobile/…)로 변환하고, 각 필드의 #{var} 를 알림 data 로 치환한다.
* 빈/부재 필드는 payload 에 넣지 않는다(방어적).
*/
class AlimtalkPayloadMapperTest extends PluginTestCase
{
private function mapper(): AlimtalkPayloadMapper
{
return new AlimtalkPayloadMapper;
}
/**
* @effects snapshot_content_substituted_with_notification_data
*/
public function test_본문의_변수를_치환해_message로_반환한다(): void
{
$result = $this->mapper()->map(
['templateContent' => '#{name}님 주문 #{order_no} 완료'],
['name' => '홍길동', 'order_no' => 'A123'],
);
$this->assertSame('홍길동님 주문 A123 완료', $result['message']);
}
/**
* @effects snapshot_buttons_mapped_to_dispatch_button_fields
*/
public function test_웹링크_버튼을_발송형식으로_변환하고_url변수를_치환한다(): void
{
$result = $this->mapper()->map(
[
'templateContent' => '본문',
'buttons' => [
[
'name' => '주문조회',
'linkType' => 'WL',
'linkMo' => 'https://m.shop/orders/#{order_no}',
'linkPc' => 'https://shop/orders/#{order_no}',
],
],
],
['order_no' => 'A123'],
);
$button = $result['extra']['button'][0];
$this->assertSame('주문조회', $button['name']);
$this->assertSame('WL', $button['type']);
$this->assertSame('https://m.shop/orders/A123', $button['url_mobile']);
$this->assertSame('https://shop/orders/A123', $button['url_pc']);
// 카카오 필드명(linkType/linkMo)은 발송 payload 에 남지 않아야 한다.
$this->assertArrayNotHasKey('linkType', $button);
$this->assertArrayNotHasKey('linkMo', $button);
}
/**
* @effects snapshot_buttons_mapped_to_dispatch_button_fields
*/
public function test_앱링크_전화_플러그인_버튼_필드를_매핑한다(): void
{
$result = $this->mapper()->map(
[
'templateContent' => '본문',
'buttons' => [
['name' => '앱', 'linkType' => 'AL', 'linkAnd' => 'myapp://a', 'linkIos' => 'myapp://i'],
['name' => '전화', 'linkType' => 'TN', 'telNumber' => '1588-0000'],
['name' => '플러그인', 'linkType' => 'P1', 'pluginId' => 'PLUG_1'],
],
],
[],
);
$buttons = $result['extra']['button'];
$this->assertSame('myapp://a', $buttons[0]['scheme_android']);
$this->assertSame('myapp://i', $buttons[0]['scheme_ios']);
$this->assertSame('1588-0000', $buttons[1]['tel_number']);
$this->assertSame('PLUG_1', $buttons[2]['plugin_id']);
}
public function test_quick_replies를_quickreply로_변환한다(): void
{
$result = $this->mapper()->map(
[
'templateContent' => '본문',
'quickReplies' => [
['name' => '바로가기', 'linkType' => 'WL', 'linkMo' => 'https://m.shop'],
],
],
[],
);
$qr = $result['extra']['quickreply'][0];
$this->assertSame('바로가기', $qr['name']);
$this->assertSame('WL', $qr['type']);
$this->assertSame('https://m.shop', $qr['url_mobile']);
}
public function test_title_header를_치환해_매핑한다(): void
{
$result = $this->mapper()->map(
[
'templateContent' => '본문',
'templateTitle' => '#{name}님',
'templateHeader' => '주문 안내',
],
['name' => '홍길동'],
);
$this->assertSame('홍길동님', $result['extra']['title']);
$this->assertSame('주문 안내', $result['extra']['header']);
}
public function test_item과_itemhighlight를_매핑하고_치환한다(): void
{
$result = $this->mapper()->map(
[
'templateContent' => '본문',
'templateItem' => [
'list' => [
['title' => '상품', 'description' => '#{product}'],
],
'summary' => ['title' => '합계', 'description' => '#{total}원'],
],
'templateItemHighlight' => ['title' => '#{name}님', 'description' => '주문완료'],
],
['product' => '티셔츠', 'total' => '20,000', 'name' => '홍길동'],
);
$this->assertSame('티셔츠', $result['extra']['item']['list'][0]['description']);
$this->assertSame('20,000원', $result['extra']['item']['summary']['description']);
$this->assertSame('홍길동님', $result['extra']['itemhighlight']['title']);
}
public function test_대표링크를_link로_변환하고_치환한다(): void
{
$result = $this->mapper()->map(
[
'templateContent' => '본문',
'templateRepresentLink' => [
'linkMo' => 'https://m.shop/#{id}',
'linkPc' => 'https://shop/#{id}',
],
],
['id' => 'X1'],
);
$this->assertSame('https://m.shop/X1', $result['extra']['link']['url_mobile']);
$this->assertSame('https://shop/X1', $result['extra']['link']['url_pc']);
}
public function test_원문에_프로토콜_접두어가_있고_변수값도_완전url이면_중복을_제거한다(): void
{
// 결함② — 카카오 콘솔에 `http://#{action_url}` 로 등록된 버튼. action_url 자체가
// config('app.url') 기반 완전 URL(https://...)이라 단순 치환 시 프로토콜이 중복된다.
$result = $this->mapper()->map(
[
'templateContent' => '본문',
'buttons' => [
['name' => '로그인하기', 'linkType' => 'WL', 'linkMo' => 'http://#{action_url}'],
],
],
['action_url' => 'https://ehkim.gnuboard.net/login'],
);
$this->assertSame(
'https://ehkim.gnuboard.net/login',
$result['extra']['button'][0]['url_mobile'],
'원문 접두어(http://)를 제거해 프로토콜 중복(http://https://...)을 방지해야 한다.',
);
}
public function test_원문_접두어가_https이고_변수값도_https이면_중복을_제거한다(): void
{
// 실제 카카오 콘솔 등록값(관리자 화면 상세조회 스크린샷 확인, 2026-07-24) —
// "회원가입 환영" 템플릿 버튼이 http:// 가 아니라 https://#{action_url} 로 등록돼 있다.
$result = $this->mapper()->map(
[
'templateContent' => '본문',
'buttons' => [
['name' => '로그인하기', 'linkType' => 'WL', 'linkMo' => 'https://#{action_url}'],
],
],
['action_url' => 'https://ehkim.gnuboard.net/login'],
);
$this->assertSame(
'https://ehkim.gnuboard.net/login',
$result['extra']['button'][0]['url_mobile'],
'원문 접두어가 https:// 인 경우도 동일하게 중복(https://https://...)을 방지해야 한다.',
);
}
public function test_원문에_프로토콜_접두어가_없으면_변수값을_그대로_둔다(): void
{
// 가이드 문서 원안(#{action_url} 만 등록)대로면 애초에 중복이 없으므로 손대지 않는다.
$result = $this->mapper()->map(
[
'templateContent' => '본문',
'buttons' => [
['name' => '로그인하기', 'linkType' => 'WL', 'linkMo' => '#{action_url}'],
],
],
['action_url' => 'https://ehkim.gnuboard.net/login'],
);
$this->assertSame('https://ehkim.gnuboard.net/login', $result['extra']['button'][0]['url_mobile']);
}
public function test_원문_접두어가_있어도_변수값이_상대경로면_접두어를_유지한다(): void
{
// 변수값 자체가 프로토콜을 포함하지 않는 경우(상대경로 등) 접두어는 진짜로 필요한
// 부분이므로 제거하면 안 된다.
$result = $this->mapper()->map(
[
'templateContent' => '본문',
'buttons' => [
['name' => '이동', 'linkType' => 'WL', 'linkMo' => 'http://#{path}'],
],
],
['path' => '/foo'],
);
$this->assertSame('http:///foo', $result['extra']['button'][0]['url_mobile']);
}
public function test_대표링크도_프로토콜_중복을_제거한다(): void
{
// mapLinkFields(대표링크)도 mapButtons 와 동일 규칙을 공유해야 한다.
$result = $this->mapper()->map(
[
'templateContent' => '본문',
'templateRepresentLink' => ['linkMo' => 'http://#{action_url}'],
],
['action_url' => 'https://ehkim.gnuboard.net/login'],
);
$this->assertSame('https://ehkim.gnuboard.net/login', $result['extra']['link']['url_mobile']);
}
public function test_변수가_data에_없으면_원문을_유지한다(): void
{
// data 에 없는 변수는 원문(#{key}) 유지 정책 — 프로토콜 중복 방어 로직이 이 정책을
// 깨서는 안 된다(원문이 http:// 로 시작하지만 치환 자체가 안 일어나므로 그대로 둔다).
$result = $this->mapper()->map(
[
'templateContent' => '본문',
'buttons' => [
['name' => '로그인하기', 'linkType' => 'WL', 'linkMo' => 'http://#{unknown_var}'],
],
],
[],
);
$this->assertSame('http://#{unknown_var}', $result['extra']['button'][0]['url_mobile']);
}
public function test_부재_필드는_extra에_넣지_않는다(): void
{
// 본문만 있는 단순 템플릿 → extra 는 비어야 한다.
$result = $this->mapper()->map(['templateContent' => '본문'], []);
$this->assertSame('본문', $result['message']);
$this->assertSame([], $result['extra']);
}
public function test_빈_버튼배열은_extra에_button을_만들지_않는다(): void
{
$result = $this->mapper()->map(
['templateContent' => '본문', 'buttons' => []],
[],
);
$this->assertArrayNotHasKey('button', $result['extra']);
}
public function test_substitute_text는_임의_텍스트의_변수를_치환한다(): void
{
// 대체 SMS·SMS 단독 본문(sms_body)이 이 공개 진입점으로 치환된다 (#597 §3.5).
$result = (new AlimtalkPayloadMapper)->substituteText(
'[샵] #{name}님 #{order_number} 주문',
['name' => '김철수', 'order_number' => 'A1'],
);
$this->assertSame('[샵] 김철수님 A1 주문', $result);
}
/**
* @effects snapshot_content_substituted_with_notification_data, snapshot_buttons_mapped_to_dispatch_button_fields
*/
public function test_등록_페이로드_형태의_승인_스냅샷을_그대로_소비한다(): void
{
// #597: 발송 소스가 kapi 상세 응답에서 등록 페이로드 스냅샷(approved_content)으로
// 전환됐다. 두 형태는 필드명이 동일하다는 대응표를 여기서 고정한다:
// templateContent→message / buttons[].linkMo→button[].url_mobile /
// quickReplies→quickreply / templateTitle→title / templateHeader→header /
// templateItem→item / templateItemHighlight→itemhighlight / templateRepresentLink→link
$snapshot = [
'templateName' => '주문 완료',
'templateMessageType' => 'MI',
'templateEmphasizeType' => 'ITEM_LIST',
'templateContent' => '#{name}님 주문 완료',
'templateTitle' => '주문 #{order_number}',
'templateHeader' => '헤더',
'categoryCode' => '001001',
'buttons' => [
['name' => '주문조회', 'linkType' => 'WL', 'linkMo' => 'https://m.shop/#{order_number}'],
],
'quickReplies' => [
['name' => '문의', 'linkType' => 'BK'],
],
'templateItem' => [
'list' => [
['title' => '품목', 'description' => '#{item_name}'],
['title' => '수량', 'description' => '#{qty}'],
],
'summary' => ['title' => '합계', 'description' => '#{total}원'],
],
'templateItemHighlight' => ['title' => '#{name}님 주문', 'description' => '결제 완료'],
'templateRepresentLink' => ['linkMo' => 'https://m.shop/orders'],
];
$result = (new AlimtalkPayloadMapper)->map($snapshot, [
'name' => '김철수', 'order_number' => 'A1', 'item_name' => '연필', 'qty' => '2', 'total' => '1000',
]);
$this->assertSame('김철수님 주문 완료', $result['message']);
$this->assertSame('https://m.shop/A1', $result['extra']['button'][0]['url_mobile']);
$this->assertSame('BK', $result['extra']['quickreply'][0]['type']);
$this->assertSame('주문 A1', $result['extra']['title']);
$this->assertSame('헤더', $result['extra']['header']);
$this->assertSame('연필', $result['extra']['item']['list'][0]['description']);
$this->assertSame('1000원', $result['extra']['item']['summary']['description']);
$this->assertSame('김철수님 주문', $result['extra']['itemhighlight']['title']);
$this->assertSame('https://m.shop/orders', $result['extra']['link']['url_mobile']);
// 등록 전용 메타(templateName 등)는 발송 extra 에 실리지 않는다.
$this->assertArrayNotHasKey('templateName', $result['extra']);
}
}