v7.0.0-beta.2 release
This commit is contained in:
1 parent
6595fd0eb5
commit
fa8eba7a0d
1165 files changed
+107118
-762127
No files matched your search
@@ -2,14 +2,13 @@
|
||||
|
||||
namespace Tests\Feature\Mail;
|
||||
|
||||
use App\Enums\ExtensionOwnerType;
|
||||
use App\Mail\DbTemplateMail;
|
||||
use App\Models\MailSendLog;
|
||||
use App\Models\MailTemplate;
|
||||
use App\Models\NotificationDefinition;
|
||||
use App\Models\NotificationTemplate;
|
||||
use App\Models\User;
|
||||
use App\Notifications\Auth\PasswordChangedNotification;
|
||||
use App\Notifications\Auth\ResetPasswordNotification;
|
||||
use App\Notifications\Auth\WelcomeNotification;
|
||||
use App\Notifications\GenericNotification;
|
||||
use App\Services\NotificationDefinitionService;
|
||||
use App\Services\NotificationTemplateService;
|
||||
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||||
use Illuminate\Support\Facades\Cache;
|
||||
use Illuminate\Support\Facades\Config;
|
||||
@@ -18,8 +17,8 @@ use Tests\TestCase;
|
||||
/**
|
||||
* 인증 알림 → DbTemplateMail 통합 테스트
|
||||
*
|
||||
* Notification.toMail()이 DB 템플릿을 해석하여 DbTemplateMail을 올바르게 생성하는지,
|
||||
* 비활성 템플릿일 때 logSkipped가 기록되는지 검증합니다.
|
||||
* GenericNotification.toMail()이 notification_templates에서 템플릿을 조회하여
|
||||
* DbTemplateMail을 올바르게 생성하는지, 비활성/미존재 시 스킵되는지 검증합니다.
|
||||
*/
|
||||
class AuthNotificationDbTemplateTest extends TestCase
|
||||
{
|
||||
@@ -35,36 +34,53 @@ class AuthNotificationDbTemplateTest extends TestCase
|
||||
}
|
||||
|
||||
// ========================================================================
|
||||
// WelcomeNotification + DbTemplateMail
|
||||
// Welcome 알림 + DbTemplateMail
|
||||
// ========================================================================
|
||||
|
||||
/**
|
||||
* WelcomeNotification이 활성 템플릿으로 DbTemplateMail 생성
|
||||
* welcome 알림이 활성 템플릿으로 DbTemplateMail 생성
|
||||
*/
|
||||
public function test_welcome_notification_creates_db_template_mail(): void
|
||||
{
|
||||
MailTemplate::factory()->withType('welcome')->withVariables()->create();
|
||||
$this->createDefinitionWithMailChannel('welcome', 'core.auth', 'core', 'core', [
|
||||
'subject' => ['ko' => '[{app_name}] 환영합니다', 'en' => '[{app_name}] Welcome'],
|
||||
'body' => ['ko' => '<p>{name}님 환영합니다</p>', 'en' => '<p>Welcome {name}</p>'],
|
||||
]);
|
||||
|
||||
$user = User::factory()->create(['name' => '홍길동', 'email' => 'hong@example.com']);
|
||||
$notification = new WelcomeNotification();
|
||||
|
||||
$notification = new GenericNotification('welcome', 'core.auth', [
|
||||
'name' => $user->name,
|
||||
'app_name' => config('app.name'),
|
||||
'action_url' => config('app.url') . '/login',
|
||||
'site_url' => config('app.url'),
|
||||
]);
|
||||
|
||||
$mailable = $notification->toMail($user);
|
||||
|
||||
$this->assertInstanceOf(DbTemplateMail::class, $mailable);
|
||||
$this->assertEquals('welcome', $mailable->getTemplateType());
|
||||
$this->assertEquals(ExtensionOwnerType::Core, $mailable->getExtensionType());
|
||||
$this->assertEquals('core', $mailable->getExtensionIdentifier());
|
||||
$this->assertFalse($mailable->isSkipped());
|
||||
}
|
||||
|
||||
/**
|
||||
* WelcomeNotification의 DbTemplateMail에 수신자가 설정됨
|
||||
* welcome 알림의 DbTemplateMail에 수신자가 설정됨
|
||||
*/
|
||||
public function test_welcome_notification_sets_recipient(): void
|
||||
{
|
||||
MailTemplate::factory()->withType('welcome')->withVariables()->create();
|
||||
$this->createDefinitionWithMailChannel('welcome', 'core.auth', 'core', 'core', [
|
||||
'subject' => ['ko' => '환영', 'en' => 'Welcome'],
|
||||
'body' => ['ko' => '<p>환영</p>', 'en' => '<p>Welcome</p>'],
|
||||
]);
|
||||
|
||||
$user = User::factory()->create(['name' => '홍길동', 'email' => 'hong@example.com']);
|
||||
$notification = new WelcomeNotification();
|
||||
|
||||
$notification = new GenericNotification('welcome', 'core.auth', [
|
||||
'name' => '홍길동',
|
||||
'app_name' => 'G7 Test',
|
||||
'action_url' => 'https://g7.test/login',
|
||||
'site_url' => 'https://g7.test',
|
||||
]);
|
||||
|
||||
$mailable = $notification->toMail($user);
|
||||
|
||||
@@ -74,66 +90,48 @@ class AuthNotificationDbTemplateTest extends TestCase
|
||||
}
|
||||
|
||||
/**
|
||||
* WelcomeNotification이 비활성 템플릿에서 스킵 인스턴스 반환 + send() 시 logSkipped 기록
|
||||
*/
|
||||
public function test_welcome_notification_returns_skipped_and_logs_when_inactive(): void
|
||||
{
|
||||
MailTemplate::factory()->withType('welcome')->inactive()->create();
|
||||
|
||||
$user = User::factory()->create(['email' => 'test@example.com']);
|
||||
$notification = new WelcomeNotification();
|
||||
|
||||
$mailable = $notification->toMail($user);
|
||||
|
||||
$this->assertInstanceOf(DbTemplateMail::class, $mailable);
|
||||
$this->assertTrue($mailable->isSkipped());
|
||||
|
||||
$mailable->send(app('mailer'));
|
||||
|
||||
$log = MailSendLog::where('recipient_email', 'test@example.com')
|
||||
->where('template_type', 'welcome')
|
||||
->first();
|
||||
|
||||
$this->assertNotNull($log);
|
||||
$this->assertEquals('skipped', $log->status);
|
||||
$this->assertEquals('notification', $log->source);
|
||||
}
|
||||
|
||||
/**
|
||||
* WelcomeNotification이 템플릿 미존재 시 스킵 인스턴스 반환 + send() 시 logSkipped 기록
|
||||
* welcome 알림이 템플릿 미존재 시 스킵 인스턴스 반환
|
||||
*/
|
||||
public function test_welcome_notification_returns_skipped_when_no_template(): void
|
||||
{
|
||||
$user = User::factory()->create(['email' => 'notemplate@example.com']);
|
||||
$notification = new WelcomeNotification();
|
||||
|
||||
$notification = new GenericNotification('welcome', 'core.auth', [
|
||||
'name' => $user->name ?? '',
|
||||
'app_name' => 'G7 Test',
|
||||
'action_url' => 'https://g7.test/login',
|
||||
'site_url' => 'https://g7.test',
|
||||
]);
|
||||
|
||||
$mailable = $notification->toMail($user);
|
||||
|
||||
$this->assertInstanceOf(DbTemplateMail::class, $mailable);
|
||||
$this->assertTrue($mailable->isSkipped());
|
||||
|
||||
$mailable->send(app('mailer'));
|
||||
|
||||
$this->assertDatabaseHas('mail_send_logs', [
|
||||
'recipient_email' => 'notemplate@example.com',
|
||||
'template_type' => 'welcome',
|
||||
'status' => 'skipped',
|
||||
]);
|
||||
}
|
||||
|
||||
// ========================================================================
|
||||
// ResetPasswordNotification + DbTemplateMail
|
||||
// Reset Password 알림 + DbTemplateMail
|
||||
// ========================================================================
|
||||
|
||||
/**
|
||||
* ResetPasswordNotification이 활성 템플릿으로 DbTemplateMail 생성
|
||||
* reset_password 알림이 활성 템플릿으로 DbTemplateMail 생성
|
||||
*/
|
||||
public function test_reset_password_notification_creates_db_template_mail(): void
|
||||
{
|
||||
MailTemplate::factory()->withType('reset_password')->create();
|
||||
$this->createDefinitionWithMailChannel('reset_password', 'core.auth', 'core', 'core', [
|
||||
'subject' => ['ko' => '비밀번호 재설정', 'en' => 'Password Reset'],
|
||||
'body' => ['ko' => '<p>{name}님, 비밀번호 재설정</p>', 'en' => '<p>Reset password, {name}</p>'],
|
||||
]);
|
||||
|
||||
$user = User::factory()->create();
|
||||
$notification = new ResetPasswordNotification('test-token-123');
|
||||
|
||||
$notification = new GenericNotification('reset_password', 'core.auth', [
|
||||
'name' => $user->name ?? '',
|
||||
'app_name' => 'G7 Test',
|
||||
'action_url' => 'https://g7.test/reset-password?token=abc',
|
||||
'expire_minutes' => '60',
|
||||
'site_url' => 'https://g7.test',
|
||||
]);
|
||||
|
||||
$mailable = $notification->toMail($user);
|
||||
|
||||
@@ -141,59 +139,28 @@ class AuthNotificationDbTemplateTest extends TestCase
|
||||
$this->assertEquals('reset_password', $mailable->getTemplateType());
|
||||
}
|
||||
|
||||
/**
|
||||
* ResetPasswordNotification의 DbTemplateMail에 수신자가 설정됨
|
||||
*/
|
||||
public function test_reset_password_notification_sets_recipient(): void
|
||||
{
|
||||
MailTemplate::factory()->withType('reset_password')->create();
|
||||
|
||||
$user = User::factory()->create(['email' => 'reset@example.com', 'name' => 'Reset User']);
|
||||
$notification = new ResetPasswordNotification('test-token');
|
||||
|
||||
$mailable = $notification->toMail($user);
|
||||
|
||||
$to = collect($mailable->to)->first();
|
||||
$this->assertEquals('reset@example.com', $to['address']);
|
||||
}
|
||||
|
||||
/**
|
||||
* ResetPasswordNotification이 비활성 시 스킵 인스턴스 반환 + send() 시 logSkipped 기록
|
||||
*/
|
||||
public function test_reset_password_notification_logs_skipped_when_inactive(): void
|
||||
{
|
||||
MailTemplate::factory()->withType('reset_password')->inactive()->create();
|
||||
|
||||
$user = User::factory()->create(['email' => 'reset@example.com']);
|
||||
$notification = new ResetPasswordNotification('token');
|
||||
|
||||
$mailable = $notification->toMail($user);
|
||||
|
||||
$this->assertInstanceOf(DbTemplateMail::class, $mailable);
|
||||
$this->assertTrue($mailable->isSkipped());
|
||||
|
||||
$mailable->send(app('mailer'));
|
||||
|
||||
$this->assertDatabaseHas('mail_send_logs', [
|
||||
'recipient_email' => 'reset@example.com',
|
||||
'template_type' => 'reset_password',
|
||||
'status' => 'skipped',
|
||||
]);
|
||||
}
|
||||
|
||||
// ========================================================================
|
||||
// PasswordChangedNotification + DbTemplateMail
|
||||
// Password Changed 알림 + DbTemplateMail
|
||||
// ========================================================================
|
||||
|
||||
/**
|
||||
* PasswordChangedNotification이 활성 템플릿으로 DbTemplateMail 생성
|
||||
* password_changed 알림이 활성 템플릿으로 DbTemplateMail 생성
|
||||
*/
|
||||
public function test_password_changed_notification_creates_db_template_mail(): void
|
||||
{
|
||||
MailTemplate::factory()->withType('password_changed')->create();
|
||||
$this->createDefinitionWithMailChannel('password_changed', 'core.auth', 'core', 'core', [
|
||||
'subject' => ['ko' => '비밀번호 변경', 'en' => 'Password Changed'],
|
||||
'body' => ['ko' => '<p>{name}님, 비밀번호 변경됨</p>', 'en' => '<p>Password changed, {name}</p>'],
|
||||
]);
|
||||
|
||||
$user = User::factory()->create();
|
||||
$notification = new PasswordChangedNotification();
|
||||
|
||||
$notification = new GenericNotification('password_changed', 'core.auth', [
|
||||
'name' => $user->name ?? '',
|
||||
'app_name' => 'G7 Test',
|
||||
'action_url' => 'https://g7.test/login',
|
||||
'site_url' => 'https://g7.test',
|
||||
]);
|
||||
|
||||
$mailable = $notification->toMail($user);
|
||||
|
||||
@@ -202,75 +169,78 @@ class AuthNotificationDbTemplateTest extends TestCase
|
||||
}
|
||||
|
||||
/**
|
||||
* PasswordChangedNotification이 비활성 시 스킵 인스턴스 반환 + send() 시 logSkipped 기록
|
||||
* password_changed 알림이 비활성 템플릿에서 스킵 반환
|
||||
*/
|
||||
public function test_password_changed_notification_logs_skipped_when_inactive(): void
|
||||
public function test_password_changed_notification_returns_skipped_when_inactive(): void
|
||||
{
|
||||
MailTemplate::factory()->withType('password_changed')->inactive()->create();
|
||||
$this->createDefinitionWithMailChannel('password_changed', 'core.auth', 'core', 'core', [
|
||||
'subject' => ['ko' => '비밀번호 변경', 'en' => 'Password Changed'],
|
||||
'body' => ['ko' => '<p>변경됨</p>', 'en' => '<p>Changed</p>'],
|
||||
], templateActive: false);
|
||||
|
||||
$user = User::factory()->create(['email' => 'changed@example.com']);
|
||||
$notification = new PasswordChangedNotification();
|
||||
|
||||
$notification = new GenericNotification('password_changed', 'core.auth', [
|
||||
'name' => $user->name ?? '',
|
||||
'app_name' => 'G7 Test',
|
||||
'action_url' => 'https://g7.test/login',
|
||||
'site_url' => 'https://g7.test',
|
||||
]);
|
||||
|
||||
$mailable = $notification->toMail($user);
|
||||
|
||||
$this->assertInstanceOf(DbTemplateMail::class, $mailable);
|
||||
$this->assertTrue($mailable->isSkipped());
|
||||
}
|
||||
|
||||
$mailable->send(app('mailer'));
|
||||
// ========================================================================
|
||||
// 헬퍼 메서드
|
||||
// ========================================================================
|
||||
|
||||
$this->assertDatabaseHas('mail_send_logs', [
|
||||
'recipient_email' => 'changed@example.com',
|
||||
'template_type' => 'password_changed',
|
||||
'status' => 'skipped',
|
||||
/**
|
||||
* NotificationDefinition + NotificationTemplate(mail 채널) 생성 헬퍼.
|
||||
*
|
||||
* @param string $type
|
||||
* @param string $hookPrefix
|
||||
* @param string $extensionType
|
||||
* @param string $extensionIdentifier
|
||||
* @param array $templateData ['subject' => [...], 'body' => [...]]
|
||||
* @param bool $templateActive
|
||||
* @return NotificationDefinition
|
||||
*/
|
||||
private function createDefinitionWithMailChannel(
|
||||
string $type,
|
||||
string $hookPrefix,
|
||||
string $extensionType,
|
||||
string $extensionIdentifier,
|
||||
array $templateData,
|
||||
bool $templateActive = true,
|
||||
): NotificationDefinition {
|
||||
$definition = NotificationDefinition::create([
|
||||
'type' => $type,
|
||||
'hook_prefix' => $hookPrefix,
|
||||
'extension_type' => $extensionType,
|
||||
'extension_identifier' => $extensionIdentifier,
|
||||
'name' => ['ko' => $type, 'en' => $type],
|
||||
'variables' => [],
|
||||
'channels' => ['mail'],
|
||||
'hooks' => [],
|
||||
'is_active' => true,
|
||||
'is_default' => true,
|
||||
]);
|
||||
}
|
||||
|
||||
// ========================================================================
|
||||
// DbTemplateMail 속성 검증
|
||||
// ========================================================================
|
||||
NotificationTemplate::create([
|
||||
'definition_id' => $definition->id,
|
||||
'channel' => 'mail',
|
||||
'subject' => $templateData['subject'],
|
||||
'body' => $templateData['body'],
|
||||
'is_active' => $templateActive,
|
||||
'is_default' => true,
|
||||
]);
|
||||
|
||||
/**
|
||||
* DbTemplateMail의 커스텀 헤더가 올바르게 설정됨
|
||||
*/
|
||||
public function test_db_template_mail_has_custom_headers(): void
|
||||
{
|
||||
$mail = new DbTemplateMail(
|
||||
renderedSubject: 'Test Subject',
|
||||
renderedBody: '<p>Test</p>',
|
||||
recipientEmail: 'test@example.com',
|
||||
templateType: 'welcome',
|
||||
extensionType: ExtensionOwnerType::Core,
|
||||
extensionIdentifier: 'core',
|
||||
source: 'notification',
|
||||
);
|
||||
app(NotificationDefinitionService::class)->invalidateCache($type);
|
||||
app(NotificationTemplateService::class)->invalidateCache($type, 'mail');
|
||||
|
||||
$headers = $mail->headers();
|
||||
$textHeaders = $headers->text;
|
||||
|
||||
$this->assertEquals('welcome', $textHeaders['X-G7-Template-Type']);
|
||||
$this->assertEquals('core', $textHeaders['X-G7-Extension-Type']);
|
||||
$this->assertEquals('core', $textHeaders['X-G7-Extension-Id']);
|
||||
$this->assertEquals('notification', $textHeaders['X-G7-Source']);
|
||||
}
|
||||
|
||||
/**
|
||||
* DbTemplateMail의 getter 메서드 검증
|
||||
*/
|
||||
public function test_db_template_mail_getters(): void
|
||||
{
|
||||
$mail = new DbTemplateMail(
|
||||
renderedSubject: 'Subject',
|
||||
renderedBody: '<p>Body</p>',
|
||||
recipientEmail: 'test@example.com',
|
||||
templateType: 'reset_password',
|
||||
extensionType: ExtensionOwnerType::Module,
|
||||
extensionIdentifier: 'sirsoft-board',
|
||||
source: 'test_mail',
|
||||
);
|
||||
|
||||
$this->assertEquals('reset_password', $mail->getTemplateType());
|
||||
$this->assertEquals(ExtensionOwnerType::Module, $mail->getExtensionType());
|
||||
$this->assertEquals('sirsoft-board', $mail->getExtensionIdentifier());
|
||||
$this->assertEquals('test_mail', $mail->getSource());
|
||||
return $definition;
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user