v7.0.0-beta.1 release
This commit is contained in:
commit
6595fd0eb5
3967 files changed
+1875383
No files matched your search
@@ -0,0 +1,486 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\Unit\Models;
|
||||
|
||||
use App\Enums\ActivityLogType;
|
||||
use App\Models\ActivityLog;
|
||||
use App\Models\User;
|
||||
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||||
use Tests\TestCase;
|
||||
|
||||
/**
|
||||
* ActivityLog 모델 테스트
|
||||
*
|
||||
* 모델의 fillable, 캐스팅, 접근자(accessor), 스코프를 검증합니다.
|
||||
*/
|
||||
class ActivityLogTest extends TestCase
|
||||
{
|
||||
use RefreshDatabase;
|
||||
|
||||
// ========================================================================
|
||||
// fillable
|
||||
// ========================================================================
|
||||
|
||||
/**
|
||||
* description_key, description_params, changes가 fillable에 포함
|
||||
*/
|
||||
public function test_i18n_fields_are_fillable(): void
|
||||
{
|
||||
$log = ActivityLog::create([
|
||||
'log_type' => ActivityLogType::Admin,
|
||||
'action' => 'user.create',
|
||||
'description_key' => 'activity_log.description.user_create',
|
||||
'description_params' => ['user_id' => 'abc-123'],
|
||||
'changes' => [
|
||||
['field' => 'name', 'old' => null, 'new' => 'John'],
|
||||
],
|
||||
]);
|
||||
|
||||
$this->assertNotNull($log->id);
|
||||
$this->assertEquals('activity_log.description.user_create', $log->description_key);
|
||||
$this->assertEquals(['user_id' => 'abc-123'], $log->description_params);
|
||||
$this->assertEquals([['field' => 'name', 'old' => null, 'new' => 'John']], $log->changes);
|
||||
}
|
||||
|
||||
/**
|
||||
* 모든 fillable 필드가 정의되어 있는지 확인
|
||||
*/
|
||||
public function test_fillable_includes_all_expected_fields(): void
|
||||
{
|
||||
$log = new ActivityLog;
|
||||
$expected = [
|
||||
'log_type',
|
||||
'loggable_type',
|
||||
'loggable_id',
|
||||
'user_id',
|
||||
'action',
|
||||
'description_key',
|
||||
'description_params',
|
||||
'properties',
|
||||
'changes',
|
||||
'ip_address',
|
||||
'user_agent',
|
||||
'created_at',
|
||||
];
|
||||
|
||||
$this->assertEquals($expected, $log->getFillable());
|
||||
}
|
||||
|
||||
// ========================================================================
|
||||
// casts
|
||||
// ========================================================================
|
||||
|
||||
/**
|
||||
* description_params가 배열로 캐스팅됨
|
||||
*/
|
||||
public function test_description_params_is_cast_to_array(): void
|
||||
{
|
||||
$log = ActivityLog::create([
|
||||
'log_type' => ActivityLogType::Admin,
|
||||
'action' => 'user.create',
|
||||
'description_params' => ['user_id' => 'abc-123', 'name' => 'John'],
|
||||
]);
|
||||
|
||||
$log->refresh();
|
||||
|
||||
$this->assertIsArray($log->description_params);
|
||||
$this->assertEquals('abc-123', $log->description_params['user_id']);
|
||||
$this->assertEquals('John', $log->description_params['name']);
|
||||
}
|
||||
|
||||
/**
|
||||
* changes가 배열로 캐스팅됨
|
||||
*/
|
||||
public function test_changes_is_cast_to_array(): void
|
||||
{
|
||||
$changes = [
|
||||
['field' => 'email', 'old' => 'old@example.com', 'new' => 'new@example.com'],
|
||||
];
|
||||
|
||||
$log = ActivityLog::create([
|
||||
'log_type' => ActivityLogType::Admin,
|
||||
'action' => 'user.update',
|
||||
'changes' => $changes,
|
||||
]);
|
||||
|
||||
$log->refresh();
|
||||
|
||||
$this->assertIsArray($log->changes);
|
||||
$this->assertEquals($changes, $log->changes);
|
||||
}
|
||||
|
||||
/**
|
||||
* properties가 배열로 캐스팅됨
|
||||
*/
|
||||
public function test_properties_is_cast_to_array(): void
|
||||
{
|
||||
$props = ['key' => 'value', 'nested' => ['a' => 1]];
|
||||
|
||||
$log = ActivityLog::create([
|
||||
'log_type' => ActivityLogType::System,
|
||||
'action' => 'config.update',
|
||||
'properties' => $props,
|
||||
]);
|
||||
|
||||
$log->refresh();
|
||||
|
||||
$this->assertIsArray($log->properties);
|
||||
$this->assertEquals($props, $log->properties);
|
||||
}
|
||||
|
||||
/**
|
||||
* log_type이 ActivityLogType Enum으로 캐스팅됨
|
||||
*/
|
||||
public function test_log_type_is_cast_to_enum(): void
|
||||
{
|
||||
$log = ActivityLog::create([
|
||||
'log_type' => ActivityLogType::User,
|
||||
'action' => 'login',
|
||||
]);
|
||||
|
||||
$log->refresh();
|
||||
|
||||
$this->assertInstanceOf(ActivityLogType::class, $log->log_type);
|
||||
$this->assertEquals(ActivityLogType::User, $log->log_type);
|
||||
}
|
||||
|
||||
/**
|
||||
* created_at이 datetime으로 캐스팅됨
|
||||
*/
|
||||
public function test_created_at_is_cast_to_datetime(): void
|
||||
{
|
||||
$log = ActivityLog::create([
|
||||
'log_type' => ActivityLogType::Admin,
|
||||
'action' => 'user.create',
|
||||
]);
|
||||
|
||||
$log->refresh();
|
||||
|
||||
$this->assertInstanceOf(\Illuminate\Support\Carbon::class, $log->created_at);
|
||||
}
|
||||
|
||||
// ========================================================================
|
||||
// accessors
|
||||
// ========================================================================
|
||||
|
||||
/**
|
||||
* localized_description: description_key가 있으면 번역된 문자열 반환
|
||||
*/
|
||||
public function test_localized_description_returns_translated_string(): void
|
||||
{
|
||||
$locale = app()->getLocale();
|
||||
|
||||
// 번역 키를 임시 등록
|
||||
app('translator')->addLines([
|
||||
'activity_log.description.user_create' => ':user_id 사용자가 생성되었습니다',
|
||||
], $locale);
|
||||
|
||||
$log = ActivityLog::create([
|
||||
'log_type' => ActivityLogType::Admin,
|
||||
'action' => 'user.create',
|
||||
'description_key' => 'activity_log.description.user_create',
|
||||
'description_params' => ['user_id' => 'abc-123'],
|
||||
]);
|
||||
|
||||
$this->assertEquals('abc-123 사용자가 생성되었습니다', $log->localized_description);
|
||||
}
|
||||
|
||||
/**
|
||||
* localized_description: description_key가 null이면 빈 문자열 반환
|
||||
*/
|
||||
public function test_localized_description_returns_empty_string_when_key_is_null(): void
|
||||
{
|
||||
$log = ActivityLog::create([
|
||||
'log_type' => ActivityLogType::Admin,
|
||||
'action' => 'user.create',
|
||||
'description_key' => null,
|
||||
]);
|
||||
|
||||
$this->assertEquals('', $log->localized_description);
|
||||
}
|
||||
|
||||
/**
|
||||
* localized_description: description_params가 null이어도 동작
|
||||
*/
|
||||
public function test_localized_description_works_with_null_params(): void
|
||||
{
|
||||
$locale = app()->getLocale();
|
||||
|
||||
app('translator')->addLines([
|
||||
'activity_log.description.simple' => '단순 작업',
|
||||
], $locale);
|
||||
|
||||
$log = ActivityLog::create([
|
||||
'log_type' => ActivityLogType::Admin,
|
||||
'action' => 'simple',
|
||||
'description_key' => 'activity_log.description.simple',
|
||||
'description_params' => null,
|
||||
]);
|
||||
|
||||
$this->assertEquals('단순 작업', $log->localized_description);
|
||||
}
|
||||
|
||||
/**
|
||||
* action_label: 1단계 - 전체 키로 번역 성공 시 번역문 반환
|
||||
*/
|
||||
public function test_action_label_returns_full_key_translation(): void
|
||||
{
|
||||
$locale = app()->getLocale();
|
||||
|
||||
app('translator')->addLines([
|
||||
'activity_log.action.user.create' => '사용자 생성',
|
||||
], $locale);
|
||||
|
||||
$log = ActivityLog::create([
|
||||
'log_type' => ActivityLogType::Admin,
|
||||
'action' => 'user.create',
|
||||
]);
|
||||
|
||||
$this->assertEquals('사용자 생성', $log->action_label);
|
||||
}
|
||||
|
||||
/**
|
||||
* action_label: 2단계 - 전체 키 실패 시 마지막 세그먼트로 조회
|
||||
*/
|
||||
public function test_action_label_falls_back_to_last_segment(): void
|
||||
{
|
||||
$locale = app()->getLocale();
|
||||
|
||||
// 전체 키 'activity_log.action.user.create'는 등록하지 않음
|
||||
app('translator')->addLines([
|
||||
'activity_log.action.create' => '생성',
|
||||
], $locale);
|
||||
|
||||
$log = ActivityLog::create([
|
||||
'log_type' => ActivityLogType::Admin,
|
||||
'action' => 'user.create',
|
||||
]);
|
||||
|
||||
$this->assertEquals('생성', $log->action_label);
|
||||
}
|
||||
|
||||
/**
|
||||
* action_label: 3단계 - 모든 번역 실패 시 raw action 문자열 반환
|
||||
*/
|
||||
public function test_action_label_falls_back_to_raw_action(): void
|
||||
{
|
||||
// 번역 키 미등록 → fallback to raw
|
||||
$log = ActivityLog::create([
|
||||
'log_type' => ActivityLogType::Admin,
|
||||
'action' => 'some.unknown.action',
|
||||
]);
|
||||
|
||||
$this->assertEquals('some.unknown.action', $log->action_label);
|
||||
}
|
||||
|
||||
/**
|
||||
* actor_name: user 관계가 있으면 사용자 이름 반환
|
||||
*/
|
||||
public function test_actor_name_returns_user_name(): void
|
||||
{
|
||||
$user = User::factory()->create(['name' => 'Alice']);
|
||||
|
||||
$log = ActivityLog::create([
|
||||
'log_type' => ActivityLogType::Admin,
|
||||
'action' => 'user.update',
|
||||
'user_id' => $user->id,
|
||||
]);
|
||||
|
||||
$log->load('user');
|
||||
|
||||
$this->assertEquals('Alice', $log->actor_name);
|
||||
}
|
||||
|
||||
/**
|
||||
* actor_name: user가 없으면 시스템 라벨 반환
|
||||
*/
|
||||
public function test_actor_name_returns_system_label_when_no_user(): void
|
||||
{
|
||||
$log = ActivityLog::create([
|
||||
'log_type' => ActivityLogType::System,
|
||||
'action' => 'maintenance',
|
||||
'user_id' => null,
|
||||
]);
|
||||
|
||||
// __('common.system')의 번역 결과를 그대로 비교
|
||||
$this->assertEquals(__('common.system'), $log->actor_name);
|
||||
}
|
||||
|
||||
/**
|
||||
* log_type_label: Enum의 label() 메서드를 통해 번역된 라벨 반환
|
||||
*/
|
||||
public function test_log_type_label_returns_translated_label(): void
|
||||
{
|
||||
$log = ActivityLog::create([
|
||||
'log_type' => ActivityLogType::Admin,
|
||||
'action' => 'test',
|
||||
]);
|
||||
|
||||
// Enum의 label() 결과와 일치하는지 확인
|
||||
$this->assertEquals(ActivityLogType::Admin->label(), $log->log_type_label);
|
||||
}
|
||||
|
||||
// ========================================================================
|
||||
// relationships
|
||||
// ========================================================================
|
||||
|
||||
/**
|
||||
* user 관계: BelongsTo User
|
||||
*/
|
||||
public function test_user_relationship(): void
|
||||
{
|
||||
$user = User::factory()->create();
|
||||
|
||||
$log = ActivityLog::create([
|
||||
'log_type' => ActivityLogType::Admin,
|
||||
'action' => 'login',
|
||||
'user_id' => $user->id,
|
||||
]);
|
||||
|
||||
$this->assertInstanceOf(User::class, $log->user);
|
||||
$this->assertEquals($user->id, $log->user->id);
|
||||
}
|
||||
|
||||
// ========================================================================
|
||||
// scopes
|
||||
// ========================================================================
|
||||
|
||||
/**
|
||||
* admin 스코프: 관리자 로그만 반환
|
||||
*/
|
||||
public function test_admin_scope(): void
|
||||
{
|
||||
ActivityLog::create(['log_type' => ActivityLogType::Admin, 'action' => 'a']);
|
||||
ActivityLog::create(['log_type' => ActivityLogType::User, 'action' => 'b']);
|
||||
ActivityLog::create(['log_type' => ActivityLogType::System, 'action' => 'c']);
|
||||
|
||||
$adminLogs = ActivityLog::admin()->get();
|
||||
|
||||
$this->assertCount(1, $adminLogs);
|
||||
$this->assertEquals(ActivityLogType::Admin, $adminLogs->first()->log_type);
|
||||
}
|
||||
|
||||
/**
|
||||
* userLog 스코프: 사용자 로그만 반환
|
||||
*/
|
||||
public function test_user_log_scope(): void
|
||||
{
|
||||
ActivityLog::create(['log_type' => ActivityLogType::Admin, 'action' => 'a']);
|
||||
ActivityLog::create(['log_type' => ActivityLogType::User, 'action' => 'b']);
|
||||
|
||||
$userLogs = ActivityLog::userLog()->get();
|
||||
|
||||
$this->assertCount(1, $userLogs);
|
||||
$this->assertEquals(ActivityLogType::User, $userLogs->first()->log_type);
|
||||
}
|
||||
|
||||
/**
|
||||
* system 스코프: 시스템 로그만 반환
|
||||
*/
|
||||
public function test_system_scope(): void
|
||||
{
|
||||
ActivityLog::create(['log_type' => ActivityLogType::Admin, 'action' => 'a']);
|
||||
ActivityLog::create(['log_type' => ActivityLogType::System, 'action' => 'c']);
|
||||
|
||||
$systemLogs = ActivityLog::system()->get();
|
||||
|
||||
$this->assertCount(1, $systemLogs);
|
||||
$this->assertEquals(ActivityLogType::System, $systemLogs->first()->log_type);
|
||||
}
|
||||
|
||||
/**
|
||||
* action 스코프: 특정 액션의 로그만 반환
|
||||
*/
|
||||
public function test_action_scope(): void
|
||||
{
|
||||
ActivityLog::create(['log_type' => ActivityLogType::Admin, 'action' => 'login']);
|
||||
ActivityLog::create(['log_type' => ActivityLogType::Admin, 'action' => 'logout']);
|
||||
|
||||
$loginLogs = ActivityLog::action('login')->get();
|
||||
|
||||
$this->assertCount(1, $loginLogs);
|
||||
$this->assertEquals('login', $loginLogs->first()->action);
|
||||
}
|
||||
|
||||
/**
|
||||
* byUser 스코프: 특정 사용자의 로그만 반환
|
||||
*/
|
||||
public function test_by_user_scope(): void
|
||||
{
|
||||
$user1 = User::factory()->create();
|
||||
$user2 = User::factory()->create();
|
||||
|
||||
ActivityLog::create(['log_type' => ActivityLogType::Admin, 'action' => 'a', 'user_id' => $user1->id]);
|
||||
ActivityLog::create(['log_type' => ActivityLogType::Admin, 'action' => 'b', 'user_id' => $user2->id]);
|
||||
|
||||
$logs = ActivityLog::byUser($user1->id)->get();
|
||||
|
||||
$this->assertCount(1, $logs);
|
||||
$this->assertEquals($user1->id, $logs->first()->user_id);
|
||||
}
|
||||
|
||||
// ========================================================================
|
||||
// boot (created_at 자동 설정)
|
||||
// ========================================================================
|
||||
|
||||
/**
|
||||
* created_at이 미지정이면 자동 설정됨
|
||||
*/
|
||||
public function test_created_at_is_auto_set_on_create(): void
|
||||
{
|
||||
$log = ActivityLog::create([
|
||||
'log_type' => ActivityLogType::Admin,
|
||||
'action' => 'test',
|
||||
]);
|
||||
|
||||
$this->assertNotNull($log->created_at);
|
||||
}
|
||||
|
||||
// ========================================================================
|
||||
// loggable_type_display 접근자
|
||||
// ========================================================================
|
||||
|
||||
/**
|
||||
* loggable_type_display: FQCN에서 짧은 클래스명 추출
|
||||
*/
|
||||
public function test_loggable_type_display_extracts_short_class_name(): void
|
||||
{
|
||||
$log = ActivityLog::create([
|
||||
'log_type' => ActivityLogType::Admin,
|
||||
'action' => 'product.update',
|
||||
'loggable_type' => 'Modules\\Sirsoft\\Ecommerce\\Models\\Product',
|
||||
'loggable_id' => 1,
|
||||
]);
|
||||
|
||||
$this->assertEquals('Product', $log->loggable_type_display);
|
||||
}
|
||||
|
||||
/**
|
||||
* loggable_type_display: 코어 모델 FQCN도 정상 추출
|
||||
*/
|
||||
public function test_loggable_type_display_handles_core_model(): void
|
||||
{
|
||||
$log = ActivityLog::create([
|
||||
'log_type' => ActivityLogType::Admin,
|
||||
'action' => 'user.update',
|
||||
'loggable_type' => 'App\\Models\\User',
|
||||
'loggable_id' => 1,
|
||||
]);
|
||||
|
||||
$this->assertEquals('User', $log->loggable_type_display);
|
||||
}
|
||||
|
||||
/**
|
||||
* loggable_type_display: null이면 null 반환
|
||||
*/
|
||||
public function test_loggable_type_display_returns_null_when_loggable_type_is_null(): void
|
||||
{
|
||||
$log = ActivityLog::create([
|
||||
'log_type' => ActivityLogType::System,
|
||||
'action' => 'maintenance',
|
||||
'loggable_type' => null,
|
||||
]);
|
||||
|
||||
$this->assertNull($log->loggable_type_display);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,379 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\Unit\Models;
|
||||
|
||||
use App\Models\User;
|
||||
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||||
use Tests\TestCase;
|
||||
use PHPUnit\Framework\Attributes\Group;
|
||||
|
||||
/**
|
||||
* sirsoft-board 모듈의 Board 모델 테스트
|
||||
*/
|
||||
#[Group('module-dependent')]
|
||||
class BoardModelTest extends TestCase
|
||||
{
|
||||
use RefreshDatabase;
|
||||
|
||||
/**
|
||||
* Board 모델 클래스명
|
||||
*/
|
||||
private string $boardClass = 'Modules\\Sirsoft\\Board\\Models\\Board';
|
||||
|
||||
/**
|
||||
* 테스트 환경 설정
|
||||
*/
|
||||
#[Group('module')]
|
||||
protected function setUp(): void
|
||||
{
|
||||
parent::setUp();
|
||||
|
||||
// Board 클래스가 로드되어 있지 않으면 테스트 스킵
|
||||
if (! class_exists($this->boardClass)) {
|
||||
$this->markTestSkipped('sirsoft-board 모듈이 설치되어 있지 않습니다.');
|
||||
}
|
||||
|
||||
// 모듈 마이그레이션 실행
|
||||
$this->artisan('migrate', [
|
||||
'--path' => 'modules/sirsoft-board/database/migrations',
|
||||
'--realpath' => true,
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Board 모델 인스턴스 생성
|
||||
*
|
||||
* @param array $data 생성 데이터
|
||||
* @return mixed Board 모델 인스턴스
|
||||
*/
|
||||
private function createBoard(array $data): mixed
|
||||
{
|
||||
return $this->boardClass::create($data);
|
||||
}
|
||||
|
||||
/**
|
||||
* fillable 속성이 정상적으로 할당되는지 테스트
|
||||
*/
|
||||
public function test_fillable_attributes_are_assignable(): void
|
||||
{
|
||||
// Arrange
|
||||
$data = [
|
||||
'name' => ['ko' => '공지사항', 'en' => 'Notice Board'],
|
||||
'slug' => 'notice',
|
||||
'per_page' => 20,
|
||||
'per_page_mobile' => 15,
|
||||
'order_by' => 'created_at',
|
||||
'order_direction' => 'DESC',
|
||||
'type' => 'basic',
|
||||
'categories' => ['공지사항', '자유게시판'],
|
||||
'show_view_count' => true,
|
||||
'secret_mode' => 'disabled',
|
||||
'use_comment' => true,
|
||||
'use_reply' => true,
|
||||
'use_report' => false,
|
||||
'min_title_length' => 2,
|
||||
'max_title_length' => 200,
|
||||
'min_content_length' => 10,
|
||||
'max_content_length' => 10000,
|
||||
'min_comment_length' => 2,
|
||||
'max_comment_length' => 1000,
|
||||
'use_file_upload' => false,
|
||||
'max_file_size' => 10485760,
|
||||
'max_file_count' => 5,
|
||||
'allowed_extensions' => ['jpg', 'png', 'pdf'],
|
||||
'comment_order' => 'ASC',
|
||||
'board_manager_id' => null,
|
||||
'notify_author_on_comment' => false,
|
||||
'notify_admin_on_post' => false,
|
||||
'blocked_keywords' => ['금지어1', '금지어2'],
|
||||
];
|
||||
|
||||
// Act
|
||||
$board = $this->createBoard($data);
|
||||
|
||||
// Assert
|
||||
$this->assertInstanceOf($this->boardClass, $board);
|
||||
$this->assertEquals('notice', $board->slug);
|
||||
$this->assertEquals(20, $board->per_page);
|
||||
$this->assertEquals('basic', $board->type);
|
||||
}
|
||||
|
||||
/**
|
||||
* name 필드가 배열로 캐스팅되는지 테스트
|
||||
*/
|
||||
public function test_name_is_cast_to_array(): void
|
||||
{
|
||||
// Arrange
|
||||
$nameData = [
|
||||
'ko' => '공지사항',
|
||||
'en' => 'Notice Board',
|
||||
];
|
||||
|
||||
// Act
|
||||
$board = $this->createBoard([
|
||||
'name' => $nameData,
|
||||
'slug' => 'notice',
|
||||
]);
|
||||
|
||||
// Assert
|
||||
$this->assertIsArray($board->name);
|
||||
$this->assertEquals($nameData, $board->name);
|
||||
}
|
||||
|
||||
/**
|
||||
* categories 필드가 배열로 캐스팅되는지 테스트
|
||||
*/
|
||||
public function test_categories_is_cast_to_array(): void
|
||||
{
|
||||
// Arrange
|
||||
$categories = ['공지사항', '자유게시판', '질문답변'];
|
||||
|
||||
// Act
|
||||
$board = $this->createBoard([
|
||||
'name' => ['ko' => '게시판'],
|
||||
'slug' => 'test-board',
|
||||
'categories' => $categories,
|
||||
]);
|
||||
|
||||
// Assert
|
||||
$this->assertIsArray($board->categories);
|
||||
$this->assertEquals($categories, $board->categories);
|
||||
}
|
||||
|
||||
/**
|
||||
* allowed_extensions 필드가 배열로 캐스팅되는지 테스트
|
||||
*/
|
||||
public function test_allowed_extensions_is_cast_to_array(): void
|
||||
{
|
||||
// Arrange
|
||||
$extensions = ['jpg', 'jpeg', 'png', 'gif', 'pdf', 'zip'];
|
||||
|
||||
// Act
|
||||
$board = $this->createBoard([
|
||||
'name' => ['ko' => '게시판'],
|
||||
'slug' => 'test-board',
|
||||
'allowed_extensions' => $extensions,
|
||||
]);
|
||||
|
||||
// Assert
|
||||
$this->assertIsArray($board->allowed_extensions);
|
||||
$this->assertEquals($extensions, $board->allowed_extensions);
|
||||
}
|
||||
|
||||
/**
|
||||
* blocked_keywords 필드가 배열로 캐스팅되는지 테스트
|
||||
*/
|
||||
public function test_blocked_keywords_is_cast_to_array(): void
|
||||
{
|
||||
// Arrange
|
||||
$keywords = ['금지어1', '금지어2', '금지어3'];
|
||||
|
||||
// Act
|
||||
$board = $this->createBoard([
|
||||
'name' => ['ko' => '게시판'],
|
||||
'slug' => 'test-board',
|
||||
'blocked_keywords' => $keywords,
|
||||
]);
|
||||
|
||||
// Assert
|
||||
$this->assertIsArray($board->blocked_keywords);
|
||||
$this->assertEquals($keywords, $board->blocked_keywords);
|
||||
}
|
||||
|
||||
/**
|
||||
* boolean 필드가 정상적으로 캐스팅되는지 테스트
|
||||
*/
|
||||
public function test_boolean_fields_are_cast_correctly(): void
|
||||
{
|
||||
// Act
|
||||
$board = $this->createBoard([
|
||||
'name' => ['ko' => '게시판'],
|
||||
'slug' => 'test-board',
|
||||
'show_view_count' => true,
|
||||
'use_comment' => false,
|
||||
'use_reply' => true,
|
||||
'use_report' => false,
|
||||
'use_file_upload' => true,
|
||||
'notify_author_on_comment' => false,
|
||||
'notify_admin_on_post' => true,
|
||||
]);
|
||||
|
||||
// Assert
|
||||
$this->assertIsBool($board->show_view_count);
|
||||
$this->assertTrue($board->show_view_count);
|
||||
$this->assertFalse($board->use_comment);
|
||||
$this->assertTrue($board->use_reply);
|
||||
$this->assertFalse($board->use_report);
|
||||
$this->assertTrue($board->use_file_upload);
|
||||
$this->assertFalse($board->notify_author_on_comment);
|
||||
$this->assertTrue($board->notify_admin_on_post);
|
||||
}
|
||||
|
||||
/**
|
||||
* boardManager 관계 테스트
|
||||
*/
|
||||
public function test_belongs_to_board_manager(): void
|
||||
{
|
||||
// Arrange
|
||||
$user = User::factory()->create();
|
||||
|
||||
// Act
|
||||
$board = $this->createBoard([
|
||||
'name' => ['ko' => '게시판'],
|
||||
'slug' => 'test-board',
|
||||
'board_manager_id' => $user->id,
|
||||
]);
|
||||
|
||||
// Assert
|
||||
$this->assertInstanceOf(User::class, $board->boardManager);
|
||||
$this->assertEquals($user->id, $board->boardManager->id);
|
||||
}
|
||||
|
||||
/**
|
||||
* creator 관계 테스트
|
||||
*/
|
||||
public function test_belongs_to_creator(): void
|
||||
{
|
||||
// Arrange
|
||||
$user = User::factory()->create();
|
||||
|
||||
// Act
|
||||
$board = $this->createBoard([
|
||||
'name' => ['ko' => '게시판'],
|
||||
'slug' => 'test-board',
|
||||
]);
|
||||
$board->created_by = $user->id;
|
||||
$board->save();
|
||||
|
||||
// Assert
|
||||
$board->refresh();
|
||||
$this->assertInstanceOf(User::class, $board->creator);
|
||||
$this->assertEquals($user->id, $board->creator->id);
|
||||
}
|
||||
|
||||
/**
|
||||
* updater 관계 테스트
|
||||
*/
|
||||
public function test_belongs_to_updater(): void
|
||||
{
|
||||
// Arrange
|
||||
$user = User::factory()->create();
|
||||
|
||||
// Act
|
||||
$board = $this->createBoard([
|
||||
'name' => ['ko' => '게시판'],
|
||||
'slug' => 'test-board',
|
||||
]);
|
||||
$board->updated_by = $user->id;
|
||||
$board->save();
|
||||
|
||||
// Assert
|
||||
$board->refresh();
|
||||
$this->assertInstanceOf(User::class, $board->updater);
|
||||
$this->assertEquals($user->id, $board->updater->id);
|
||||
}
|
||||
|
||||
/**
|
||||
* getLocalizedName 메서드 테스트 (한국어)
|
||||
*/
|
||||
public function test_get_localized_name_returns_korean_name(): void
|
||||
{
|
||||
// Arrange
|
||||
$board = $this->createBoard([
|
||||
'name' => [
|
||||
'ko' => '공지사항',
|
||||
'en' => 'Notice Board',
|
||||
],
|
||||
'slug' => 'notice',
|
||||
]);
|
||||
|
||||
// Act
|
||||
$localizedName = $board->getLocalizedName('ko');
|
||||
|
||||
// Assert
|
||||
$this->assertEquals('공지사항', $localizedName);
|
||||
}
|
||||
|
||||
/**
|
||||
* getLocalizedName 메서드 테스트 (영어)
|
||||
*/
|
||||
public function test_get_localized_name_returns_english_name(): void
|
||||
{
|
||||
// Arrange
|
||||
$board = $this->createBoard([
|
||||
'name' => [
|
||||
'ko' => '공지사항',
|
||||
'en' => 'Notice Board',
|
||||
],
|
||||
'slug' => 'notice',
|
||||
]);
|
||||
|
||||
// Act
|
||||
$localizedName = $board->getLocalizedName('en');
|
||||
|
||||
// Assert
|
||||
$this->assertEquals('Notice Board', $localizedName);
|
||||
}
|
||||
|
||||
/**
|
||||
* getLocalizedName 메서드 테스트 (fallback)
|
||||
*/
|
||||
public function test_get_localized_name_returns_fallback_when_locale_not_found(): void
|
||||
{
|
||||
// Arrange
|
||||
$board = $this->createBoard([
|
||||
'name' => [
|
||||
'ko' => '공지사항',
|
||||
'en' => 'Notice Board',
|
||||
],
|
||||
'slug' => 'notice',
|
||||
]);
|
||||
|
||||
// Act
|
||||
$localizedName = $board->getLocalizedName('ja'); // 존재하지 않는 로케일
|
||||
|
||||
// Assert
|
||||
// fallback_locale이 'ko'이므로 한국어 이름이 반환되어야 함
|
||||
$this->assertEquals('공지사항', $localizedName);
|
||||
}
|
||||
|
||||
/**
|
||||
* localizedName Attribute 테스트
|
||||
*/
|
||||
public function test_localized_name_attribute_returns_current_locale_name(): void
|
||||
{
|
||||
// Arrange
|
||||
app()->setLocale('ko');
|
||||
$board = $this->createBoard([
|
||||
'name' => [
|
||||
'ko' => '공지사항',
|
||||
'en' => 'Notice Board',
|
||||
],
|
||||
'slug' => 'notice',
|
||||
]);
|
||||
|
||||
// Act
|
||||
$localizedName = $board->localized_name;
|
||||
|
||||
// Assert
|
||||
$this->assertEquals('공지사항', $localizedName);
|
||||
}
|
||||
|
||||
/**
|
||||
* guarded 속성 테스트 (created_by, updated_by)
|
||||
*/
|
||||
public function test_created_by_and_updated_by_are_guarded(): void
|
||||
{
|
||||
// Act
|
||||
$board = $this->createBoard([
|
||||
'name' => ['ko' => '게시판'],
|
||||
'slug' => 'test-board',
|
||||
'created_by' => 999, // guarded이므로 무시되어야 함
|
||||
'updated_by' => 999, // guarded이므로 무시되어야 함
|
||||
]);
|
||||
|
||||
// Assert
|
||||
$this->assertNull($board->created_by);
|
||||
$this->assertNull($board->updated_by);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,179 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\Unit\Models;
|
||||
|
||||
use App\Enums\ExtensionOwnerType;
|
||||
use App\Enums\MailSendStatus;
|
||||
use App\Models\MailSendLog;
|
||||
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||||
use Tests\TestCase;
|
||||
|
||||
/**
|
||||
* MailSendLog 모델 테스트
|
||||
*
|
||||
* 모델의 fillable, 캐스팅, 스코프 메서드를 검증합니다.
|
||||
*/
|
||||
class MailSendLogTest extends TestCase
|
||||
{
|
||||
use RefreshDatabase;
|
||||
|
||||
// ========================================================================
|
||||
// fillable / casts
|
||||
// ========================================================================
|
||||
|
||||
/**
|
||||
* source 컬럼이 fillable에 포함
|
||||
*/
|
||||
public function test_source_is_fillable(): void
|
||||
{
|
||||
$log = MailSendLog::create([
|
||||
'recipient_email' => 'test@example.com',
|
||||
'subject' => 'Test',
|
||||
'template_type' => 'welcome',
|
||||
'extension_type' => ExtensionOwnerType::Core,
|
||||
'extension_identifier' => 'core',
|
||||
'source' => 'notification',
|
||||
'status' => MailSendStatus::Sent->value,
|
||||
'sent_at' => now(),
|
||||
]);
|
||||
|
||||
$this->assertEquals('notification', $log->source);
|
||||
}
|
||||
|
||||
/**
|
||||
* subject가 nullable
|
||||
*/
|
||||
public function test_subject_is_nullable(): void
|
||||
{
|
||||
$log = MailSendLog::create([
|
||||
'recipient_email' => 'test@example.com',
|
||||
'subject' => null,
|
||||
'extension_type' => ExtensionOwnerType::Core,
|
||||
'extension_identifier' => 'core',
|
||||
'status' => MailSendStatus::Sent->value,
|
||||
'sent_at' => now(),
|
||||
]);
|
||||
|
||||
$this->assertNull($log->subject);
|
||||
}
|
||||
|
||||
/**
|
||||
* sent_at이 datetime으로 캐스팅됨
|
||||
*/
|
||||
public function test_sent_at_is_cast_to_datetime(): void
|
||||
{
|
||||
$log = MailSendLog::factory()->create();
|
||||
|
||||
$this->assertInstanceOf(\Carbon\Carbon::class, $log->sent_at);
|
||||
}
|
||||
|
||||
// ========================================================================
|
||||
// scopeByTemplateType
|
||||
// ========================================================================
|
||||
|
||||
/**
|
||||
* 특정 템플릿 유형으로 필터
|
||||
*/
|
||||
public function test_scope_by_template_type_filters_correctly(): void
|
||||
{
|
||||
MailSendLog::factory()->withTemplateType('welcome')->count(2)->create();
|
||||
MailSendLog::factory()->withTemplateType('password_reset')->count(3)->create();
|
||||
|
||||
$result = MailSendLog::byTemplateType('welcome')->get();
|
||||
|
||||
$this->assertCount(2, $result);
|
||||
}
|
||||
|
||||
/**
|
||||
* null 템플릿 유형으로 필터 (whereNull)
|
||||
*/
|
||||
public function test_scope_by_template_type_handles_null(): void
|
||||
{
|
||||
MailSendLog::factory()->create(['template_type' => null]);
|
||||
MailSendLog::factory()->withTemplateType('welcome')->create();
|
||||
|
||||
$result = MailSendLog::byTemplateType(null)->get();
|
||||
|
||||
$this->assertCount(1, $result);
|
||||
$this->assertNull($result->first()->template_type);
|
||||
}
|
||||
|
||||
// ========================================================================
|
||||
// scopeByExtension
|
||||
// ========================================================================
|
||||
|
||||
/**
|
||||
* 확장 타입별 필터
|
||||
*/
|
||||
public function test_scope_by_extension_filters_by_type(): void
|
||||
{
|
||||
MailSendLog::factory()->forExtension(ExtensionOwnerType::Core, 'core')->count(3)->create();
|
||||
MailSendLog::factory()->forExtension(ExtensionOwnerType::Module, 'sirsoft-board')->count(2)->create();
|
||||
|
||||
$result = MailSendLog::byExtension(ExtensionOwnerType::Core)->get();
|
||||
|
||||
$this->assertCount(3, $result);
|
||||
}
|
||||
|
||||
/**
|
||||
* 확장 타입 + 식별자 필터
|
||||
*/
|
||||
public function test_scope_by_extension_filters_by_type_and_identifier(): void
|
||||
{
|
||||
MailSendLog::factory()->forExtension(ExtensionOwnerType::Module, 'sirsoft-board')->count(2)->create();
|
||||
MailSendLog::factory()->forExtension(ExtensionOwnerType::Module, 'sirsoft-ecommerce')->count(3)->create();
|
||||
|
||||
$result = MailSendLog::byExtension(ExtensionOwnerType::Module, 'sirsoft-board')->get();
|
||||
|
||||
$this->assertCount(2, $result);
|
||||
}
|
||||
|
||||
// ========================================================================
|
||||
// scopeBySource
|
||||
// ========================================================================
|
||||
|
||||
/**
|
||||
* 발송 출처별 필터
|
||||
*/
|
||||
public function test_scope_by_source_filters_correctly(): void
|
||||
{
|
||||
MailSendLog::factory()->create(['source' => 'notification']);
|
||||
MailSendLog::factory()->create(['source' => 'test_mail']);
|
||||
MailSendLog::factory()->create(['source' => 'notification']);
|
||||
|
||||
$result = MailSendLog::bySource('notification')->get();
|
||||
|
||||
$this->assertCount(2, $result);
|
||||
}
|
||||
|
||||
/**
|
||||
* null 발송 출처로 필터 (whereNull)
|
||||
*/
|
||||
public function test_scope_by_source_handles_null(): void
|
||||
{
|
||||
MailSendLog::factory()->create(['source' => null]);
|
||||
MailSendLog::factory()->create(['source' => 'notification']);
|
||||
|
||||
$result = MailSendLog::bySource(null)->get();
|
||||
|
||||
$this->assertCount(1, $result);
|
||||
$this->assertNull($result->first()->source);
|
||||
}
|
||||
|
||||
// ========================================================================
|
||||
// scopeByStatus
|
||||
// ========================================================================
|
||||
|
||||
/**
|
||||
* 상태별 필터
|
||||
*/
|
||||
public function test_scope_by_status_filters_correctly(): void
|
||||
{
|
||||
MailSendLog::factory()->count(3)->create(['status' => MailSendStatus::Sent->value]);
|
||||
MailSendLog::factory()->failed()->count(2)->create();
|
||||
|
||||
$result = MailSendLog::byStatus(MailSendStatus::Failed->value)->get();
|
||||
|
||||
$this->assertCount(2, $result);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,242 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\Unit\Models;
|
||||
|
||||
use App\Models\MailTemplate;
|
||||
use App\Models\User;
|
||||
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||||
use Tests\TestCase;
|
||||
|
||||
/**
|
||||
* MailTemplate 모델 테스트
|
||||
*
|
||||
* 모델의 fillable, casts, 스코프, replaceVariables, 관계를 검증합니다.
|
||||
*/
|
||||
class MailTemplateTest extends TestCase
|
||||
{
|
||||
use RefreshDatabase;
|
||||
|
||||
// ========================================================================
|
||||
// fillable / casts
|
||||
// ========================================================================
|
||||
|
||||
/**
|
||||
* type이 fillable에 포함
|
||||
*/
|
||||
public function test_type_is_fillable(): void
|
||||
{
|
||||
$template = MailTemplate::factory()->withType('welcome')->create();
|
||||
|
||||
$this->assertEquals('welcome', $template->type);
|
||||
}
|
||||
|
||||
/**
|
||||
* subject가 array로 캐스팅됨
|
||||
*/
|
||||
public function test_subject_is_cast_to_array(): void
|
||||
{
|
||||
$template = MailTemplate::factory()->create([
|
||||
'subject' => ['ko' => '한국어 제목', 'en' => 'English Subject'],
|
||||
]);
|
||||
|
||||
$this->assertIsArray($template->subject);
|
||||
$this->assertEquals('한국어 제목', $template->subject['ko']);
|
||||
}
|
||||
|
||||
/**
|
||||
* body가 array로 캐스팅됨
|
||||
*/
|
||||
public function test_body_is_cast_to_array(): void
|
||||
{
|
||||
$template = MailTemplate::factory()->create([
|
||||
'body' => ['ko' => '<p>본문</p>', 'en' => '<p>Body</p>'],
|
||||
]);
|
||||
|
||||
$this->assertIsArray($template->body);
|
||||
$this->assertEquals('<p>본문</p>', $template->body['ko']);
|
||||
}
|
||||
|
||||
/**
|
||||
* variables가 array로 캐스팅됨
|
||||
*/
|
||||
public function test_variables_is_cast_to_array(): void
|
||||
{
|
||||
$template = MailTemplate::factory()->create([
|
||||
'variables' => [['key' => 'name', 'description' => '이름']],
|
||||
]);
|
||||
|
||||
$this->assertIsArray($template->variables);
|
||||
$this->assertEquals('name', $template->variables[0]['key']);
|
||||
}
|
||||
|
||||
/**
|
||||
* is_active가 boolean으로 캐스팅됨
|
||||
*/
|
||||
public function test_is_active_is_cast_to_boolean(): void
|
||||
{
|
||||
$template = MailTemplate::factory()->create(['is_active' => 1]);
|
||||
|
||||
$this->assertIsBool($template->is_active);
|
||||
$this->assertTrue($template->is_active);
|
||||
}
|
||||
|
||||
/**
|
||||
* is_default가 boolean으로 캐스팅됨
|
||||
*/
|
||||
public function test_is_default_is_cast_to_boolean(): void
|
||||
{
|
||||
$template = MailTemplate::factory()->create(['is_default' => 0]);
|
||||
|
||||
$this->assertIsBool($template->is_default);
|
||||
$this->assertFalse($template->is_default);
|
||||
}
|
||||
|
||||
// ========================================================================
|
||||
// 관계
|
||||
// ========================================================================
|
||||
|
||||
/**
|
||||
* updater 관계가 User를 반환
|
||||
*/
|
||||
public function test_updater_returns_user(): void
|
||||
{
|
||||
$user = User::factory()->create();
|
||||
$template = MailTemplate::factory()->create(['updated_by' => $user->id]);
|
||||
|
||||
$this->assertInstanceOf(User::class, $template->updater);
|
||||
$this->assertEquals($user->id, $template->updater->id);
|
||||
}
|
||||
|
||||
/**
|
||||
* updated_by가 null이면 updater도 null
|
||||
*/
|
||||
public function test_updater_returns_null_when_no_updater(): void
|
||||
{
|
||||
$template = MailTemplate::factory()->create(['updated_by' => null]);
|
||||
|
||||
$this->assertNull($template->updater);
|
||||
}
|
||||
|
||||
// ========================================================================
|
||||
// scopeActive
|
||||
// ========================================================================
|
||||
|
||||
/**
|
||||
* active 스코프가 활성 템플릿만 반환
|
||||
*/
|
||||
public function test_scope_active_filters_correctly(): void
|
||||
{
|
||||
MailTemplate::factory()->count(2)->create(['is_active' => true]);
|
||||
MailTemplate::factory()->inactive()->create();
|
||||
|
||||
$result = MailTemplate::active()->get();
|
||||
|
||||
$this->assertCount(2, $result);
|
||||
}
|
||||
|
||||
// ========================================================================
|
||||
// scopeByType
|
||||
// ========================================================================
|
||||
|
||||
/**
|
||||
* byType 스코프가 특정 유형만 반환
|
||||
*/
|
||||
public function test_scope_by_type_filters_correctly(): void
|
||||
{
|
||||
MailTemplate::factory()->withType('welcome')->create();
|
||||
MailTemplate::factory()->withType('reset_password')->create();
|
||||
|
||||
$result = MailTemplate::byType('welcome')->get();
|
||||
|
||||
$this->assertCount(1, $result);
|
||||
$this->assertEquals('welcome', $result->first()->type);
|
||||
}
|
||||
|
||||
// ========================================================================
|
||||
// getLocalizedSubject / getLocalizedBody
|
||||
// ========================================================================
|
||||
|
||||
/**
|
||||
* getLocalizedSubject가 현재 로케일 제목을 반환
|
||||
*/
|
||||
public function test_get_localized_subject_returns_current_locale(): void
|
||||
{
|
||||
app()->setLocale('ko');
|
||||
|
||||
$template = MailTemplate::factory()->create([
|
||||
'subject' => ['ko' => '한국어 제목', 'en' => 'English Subject'],
|
||||
]);
|
||||
|
||||
$this->assertEquals('한국어 제목', $template->getLocalizedSubject());
|
||||
}
|
||||
|
||||
/**
|
||||
* getLocalizedSubject가 지정된 로케일 제목을 반환
|
||||
*/
|
||||
public function test_get_localized_subject_with_specific_locale(): void
|
||||
{
|
||||
$template = MailTemplate::factory()->create([
|
||||
'subject' => ['ko' => '한국어 제목', 'en' => 'English Subject'],
|
||||
]);
|
||||
|
||||
$this->assertEquals('English Subject', $template->getLocalizedSubject('en'));
|
||||
}
|
||||
|
||||
/**
|
||||
* getLocalizedSubject가 fallback으로 ko를 반환
|
||||
*/
|
||||
public function test_get_localized_subject_falls_back_to_ko(): void
|
||||
{
|
||||
$template = MailTemplate::factory()->create([
|
||||
'subject' => ['ko' => '한국어 제목'],
|
||||
]);
|
||||
|
||||
$this->assertEquals('한국어 제목', $template->getLocalizedSubject('ja'));
|
||||
}
|
||||
|
||||
/**
|
||||
* getLocalizedBody가 현재 로케일 본문을 반환
|
||||
*/
|
||||
public function test_get_localized_body_returns_current_locale(): void
|
||||
{
|
||||
app()->setLocale('en');
|
||||
|
||||
$template = MailTemplate::factory()->create([
|
||||
'body' => ['ko' => '<p>본문</p>', 'en' => '<p>Body</p>'],
|
||||
]);
|
||||
|
||||
$this->assertEquals('<p>Body</p>', $template->getLocalizedBody());
|
||||
}
|
||||
|
||||
// ========================================================================
|
||||
// replaceVariables
|
||||
// ========================================================================
|
||||
|
||||
/**
|
||||
* replaceVariables가 변수를 올바르게 치환
|
||||
*/
|
||||
public function test_replace_variables_substitutes_correctly(): void
|
||||
{
|
||||
$template = MailTemplate::factory()->withVariables()->create();
|
||||
|
||||
$result = $template->replaceVariables([
|
||||
'name' => '홍길동',
|
||||
'app_name' => 'G7',
|
||||
], 'ko');
|
||||
|
||||
$this->assertEquals('[G7] 홍길동님 환영합니다', $result['subject']);
|
||||
$this->assertStringContainsString('홍길동님, G7에 가입해 주셔서', $result['body']);
|
||||
}
|
||||
|
||||
/**
|
||||
* replaceVariables가 누락된 변수는 그대로 유지
|
||||
*/
|
||||
public function test_replace_variables_keeps_unreplaced_variables(): void
|
||||
{
|
||||
$template = MailTemplate::factory()->withVariables()->create();
|
||||
|
||||
$result = $template->replaceVariables(['name' => '홍길동'], 'ko');
|
||||
|
||||
$this->assertStringContainsString('{app_name}', $result['subject']);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,236 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\Unit\Models;
|
||||
|
||||
use App\Enums\PermissionType;
|
||||
use App\Models\Permission;
|
||||
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||||
use Tests\TestCase;
|
||||
|
||||
/**
|
||||
* Permission 모델 테스트
|
||||
*
|
||||
* Permission 모델의 type 필드 및 관련 메서드를 검증합니다.
|
||||
*/
|
||||
class PermissionModelTest extends TestCase
|
||||
{
|
||||
use RefreshDatabase;
|
||||
|
||||
// ========================================================================
|
||||
// type 필드 테스트
|
||||
// ========================================================================
|
||||
|
||||
/**
|
||||
* Permission 생성 시 type 필드가 PermissionType Enum으로 캐스팅되는지 확인
|
||||
*/
|
||||
public function test_type_field_is_cast_to_permission_type_enum(): void
|
||||
{
|
||||
$permission = Permission::create([
|
||||
'identifier' => 'test.permission',
|
||||
'name' => ['ko' => '테스트 권한', 'en' => 'Test Permission'],
|
||||
'description' => ['ko' => '테스트용', 'en' => 'For testing'],
|
||||
'type' => 'admin',
|
||||
]);
|
||||
|
||||
$this->assertInstanceOf(PermissionType::class, $permission->type);
|
||||
$this->assertEquals(PermissionType::Admin, $permission->type);
|
||||
}
|
||||
|
||||
/**
|
||||
* type 필드에 user 값 저장 및 조회 테스트
|
||||
*/
|
||||
public function test_can_create_permission_with_user_type(): void
|
||||
{
|
||||
$permission = Permission::create([
|
||||
'identifier' => 'test.user.permission',
|
||||
'name' => ['ko' => '사용자 권한', 'en' => 'User Permission'],
|
||||
'description' => ['ko' => '사용자용', 'en' => 'For users'],
|
||||
'type' => 'user',
|
||||
]);
|
||||
|
||||
$this->assertEquals(PermissionType::User, $permission->type);
|
||||
$this->assertEquals('user', $permission->type->value);
|
||||
}
|
||||
|
||||
/**
|
||||
* PermissionType Enum 인스턴스로 type 필드 설정 테스트
|
||||
*/
|
||||
public function test_can_set_type_with_enum_instance(): void
|
||||
{
|
||||
$permission = Permission::create([
|
||||
'identifier' => 'test.enum.permission',
|
||||
'name' => ['ko' => '테스트', 'en' => 'Test'],
|
||||
'type' => PermissionType::Admin,
|
||||
]);
|
||||
|
||||
$this->assertEquals(PermissionType::Admin, $permission->type);
|
||||
}
|
||||
|
||||
// ========================================================================
|
||||
// isAdminPermission() 메서드 테스트
|
||||
// ========================================================================
|
||||
|
||||
/**
|
||||
* admin 타입 권한에서 isAdminPermission()이 true 반환
|
||||
*/
|
||||
public function test_is_admin_permission_returns_true_for_admin_type(): void
|
||||
{
|
||||
$permission = Permission::create([
|
||||
'identifier' => 'test.admin',
|
||||
'name' => ['ko' => '관리자 권한', 'en' => 'Admin Permission'],
|
||||
'type' => PermissionType::Admin,
|
||||
]);
|
||||
|
||||
$this->assertTrue($permission->isAdminPermission());
|
||||
}
|
||||
|
||||
/**
|
||||
* user 타입 권한에서 isAdminPermission()이 false 반환
|
||||
*/
|
||||
public function test_is_admin_permission_returns_false_for_user_type(): void
|
||||
{
|
||||
$permission = Permission::create([
|
||||
'identifier' => 'test.user',
|
||||
'name' => ['ko' => '사용자 권한', 'en' => 'User Permission'],
|
||||
'type' => PermissionType::User,
|
||||
]);
|
||||
|
||||
$this->assertFalse($permission->isAdminPermission());
|
||||
}
|
||||
|
||||
// ========================================================================
|
||||
// isUserPermission() 메서드 테스트
|
||||
// ========================================================================
|
||||
|
||||
/**
|
||||
* user 타입 권한에서 isUserPermission()이 true 반환
|
||||
*/
|
||||
public function test_is_user_permission_returns_true_for_user_type(): void
|
||||
{
|
||||
$permission = Permission::create([
|
||||
'identifier' => 'test.user.perm',
|
||||
'name' => ['ko' => '사용자 권한', 'en' => 'User Permission'],
|
||||
'type' => PermissionType::User,
|
||||
]);
|
||||
|
||||
$this->assertTrue($permission->isUserPermission());
|
||||
}
|
||||
|
||||
/**
|
||||
* admin 타입 권한에서 isUserPermission()이 false 반환
|
||||
*/
|
||||
public function test_is_user_permission_returns_false_for_admin_type(): void
|
||||
{
|
||||
$permission = Permission::create([
|
||||
'identifier' => 'test.admin.perm',
|
||||
'name' => ['ko' => '관리자 권한', 'en' => 'Admin Permission'],
|
||||
'type' => PermissionType::Admin,
|
||||
]);
|
||||
|
||||
$this->assertFalse($permission->isUserPermission());
|
||||
}
|
||||
|
||||
// ========================================================================
|
||||
// adminPermissions() 스코프 테스트
|
||||
// ========================================================================
|
||||
|
||||
/**
|
||||
* adminPermissions() 스코프가 admin 타입만 조회하는지 확인
|
||||
*/
|
||||
public function test_admin_permissions_scope_returns_only_admin_type(): void
|
||||
{
|
||||
// admin 타입 권한 2개 생성
|
||||
Permission::create([
|
||||
'identifier' => 'test.admin.1',
|
||||
'name' => ['ko' => '관리자1', 'en' => 'Admin1'],
|
||||
'type' => PermissionType::Admin,
|
||||
]);
|
||||
Permission::create([
|
||||
'identifier' => 'test.admin.2',
|
||||
'name' => ['ko' => '관리자2', 'en' => 'Admin2'],
|
||||
'type' => PermissionType::Admin,
|
||||
]);
|
||||
|
||||
// user 타입 권한 1개 생성
|
||||
Permission::create([
|
||||
'identifier' => 'test.user.1',
|
||||
'name' => ['ko' => '사용자1', 'en' => 'User1'],
|
||||
'type' => PermissionType::User,
|
||||
]);
|
||||
|
||||
$adminPermissions = Permission::adminPermissions()->get();
|
||||
|
||||
$this->assertCount(2, $adminPermissions);
|
||||
$adminPermissions->each(function ($permission) {
|
||||
$this->assertEquals(PermissionType::Admin, $permission->type);
|
||||
});
|
||||
}
|
||||
|
||||
// ========================================================================
|
||||
// userPermissions() 스코프 테스트
|
||||
// ========================================================================
|
||||
|
||||
/**
|
||||
* userPermissions() 스코프가 user 타입만 조회하는지 확인
|
||||
*/
|
||||
public function test_user_permissions_scope_returns_only_user_type(): void
|
||||
{
|
||||
// admin 타입 권한 1개 생성
|
||||
Permission::create([
|
||||
'identifier' => 'test.admin.scope',
|
||||
'name' => ['ko' => '관리자', 'en' => 'Admin'],
|
||||
'type' => PermissionType::Admin,
|
||||
]);
|
||||
|
||||
// user 타입 권한 2개 생성
|
||||
Permission::create([
|
||||
'identifier' => 'test.user.scope.1',
|
||||
'name' => ['ko' => '사용자1', 'en' => 'User1'],
|
||||
'type' => PermissionType::User,
|
||||
]);
|
||||
Permission::create([
|
||||
'identifier' => 'test.user.scope.2',
|
||||
'name' => ['ko' => '사용자2', 'en' => 'User2'],
|
||||
'type' => PermissionType::User,
|
||||
]);
|
||||
|
||||
$userPermissions = Permission::userPermissions()->get();
|
||||
|
||||
$this->assertCount(2, $userPermissions);
|
||||
$userPermissions->each(function ($permission) {
|
||||
$this->assertEquals(PermissionType::User, $permission->type);
|
||||
});
|
||||
}
|
||||
|
||||
// ========================================================================
|
||||
// fillable 및 casts 테스트
|
||||
// ========================================================================
|
||||
|
||||
/**
|
||||
* type 필드가 fillable에 포함되어 있는지 확인
|
||||
*/
|
||||
public function test_type_is_in_fillable(): void
|
||||
{
|
||||
$permission = new Permission();
|
||||
|
||||
$this->assertContains('type', $permission->getFillable());
|
||||
}
|
||||
|
||||
/**
|
||||
* type 필드가 올바르게 캐스팅되는지 확인 (casts 배열)
|
||||
*/
|
||||
public function test_type_is_properly_cast(): void
|
||||
{
|
||||
$permission = Permission::create([
|
||||
'identifier' => 'test.cast',
|
||||
'name' => ['ko' => '캐스트 테스트', 'en' => 'Cast Test'],
|
||||
'type' => 'admin',
|
||||
]);
|
||||
|
||||
// DB에서 다시 조회
|
||||
$freshPermission = Permission::find($permission->id);
|
||||
|
||||
// Enum으로 캐스팅되는지 확인
|
||||
$this->assertInstanceOf(PermissionType::class, $freshPermission->type);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,269 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\Unit\Models;
|
||||
|
||||
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||||
use PHPUnit\Framework\Attributes\Group;
|
||||
use Tests\TestCase;
|
||||
|
||||
/**
|
||||
* sirsoft-ecommerce 모듈의 ProductLabelAssignment 모델 테스트
|
||||
*/
|
||||
#[Group('module-dependent')]
|
||||
class ProductLabelAssignmentTest extends TestCase
|
||||
{
|
||||
use RefreshDatabase;
|
||||
|
||||
/**
|
||||
* ProductLabelAssignment 모델 클래스명
|
||||
*/
|
||||
private string $assignmentClass = 'Modules\\Sirsoft\\Ecommerce\\Models\\ProductLabelAssignment';
|
||||
|
||||
/**
|
||||
* ProductLabel 모델 클래스명
|
||||
*/
|
||||
private string $labelClass = 'Modules\\Sirsoft\\Ecommerce\\Models\\ProductLabel';
|
||||
|
||||
/**
|
||||
* Product 모델 클래스명
|
||||
*/
|
||||
private string $productClass = 'Modules\\Sirsoft\\Ecommerce\\Models\\Product';
|
||||
|
||||
/**
|
||||
* 테스트 환경 설정
|
||||
*/
|
||||
protected function setUp(): void
|
||||
{
|
||||
parent::setUp();
|
||||
|
||||
// ProductLabelAssignment 클래스가 로드되어 있지 않으면 테스트 스킵
|
||||
if (! class_exists($this->assignmentClass)) {
|
||||
$this->markTestSkipped('sirsoft-ecommerce 모듈이 설치되어 있지 않습니다.');
|
||||
}
|
||||
|
||||
// 모듈 마이그레이션 실행
|
||||
$this->artisan('migrate', [
|
||||
'--path' => 'modules/sirsoft-ecommerce/database/migrations',
|
||||
'--realpath' => true,
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* 테스트용 ProductLabel 생성
|
||||
*
|
||||
* @param array $overrides 오버라이드할 속성
|
||||
* @return mixed ProductLabel 인스턴스
|
||||
*/
|
||||
private function createLabel(array $overrides = []): mixed
|
||||
{
|
||||
$data = array_merge([
|
||||
'name' => ['ko' => '테스트 라벨', 'en' => 'Test Label'],
|
||||
'color' => '#FF0000',
|
||||
'is_active' => true,
|
||||
], $overrides);
|
||||
|
||||
return $this->labelClass::create($data);
|
||||
}
|
||||
|
||||
/**
|
||||
* 테스트용 Product 생성
|
||||
*
|
||||
* @param array $overrides 오버라이드할 속성
|
||||
* @return mixed Product 인스턴스
|
||||
*/
|
||||
private function createProduct(array $overrides = []): mixed
|
||||
{
|
||||
$data = array_merge([
|
||||
'name' => ['ko' => '테스트 상품', 'en' => 'Test Product'],
|
||||
'product_code' => 'TEST-'.uniqid(),
|
||||
'selling_price' => 10000,
|
||||
'sales_status' => 'on_sale',
|
||||
'display_status' => 'visible',
|
||||
'tax_status' => 'taxable',
|
||||
], $overrides);
|
||||
|
||||
return $this->productClass::create($data);
|
||||
}
|
||||
|
||||
/**
|
||||
* 테스트용 ProductLabelAssignment 생성
|
||||
*
|
||||
* @param array $overrides 오버라이드할 속성
|
||||
* @return mixed ProductLabelAssignment 인스턴스
|
||||
*/
|
||||
private function createAssignment(array $overrides = []): mixed
|
||||
{
|
||||
// 기본 label과 product 생성
|
||||
$label = $this->createLabel();
|
||||
$product = $this->createProduct();
|
||||
|
||||
$data = array_merge([
|
||||
'product_id' => $product->id,
|
||||
'label_id' => $label->id,
|
||||
], $overrides);
|
||||
|
||||
return $this->assignmentClass::create($data);
|
||||
}
|
||||
|
||||
/**
|
||||
* start_date와 end_date가 date로 캐스팅되는지 테스트
|
||||
*/
|
||||
public function test_dates_are_cast_to_date(): void
|
||||
{
|
||||
// Arrange
|
||||
$label = $this->createLabel();
|
||||
$product = $this->createProduct();
|
||||
|
||||
// Act
|
||||
$assignment = $this->assignmentClass::create([
|
||||
'product_id' => $product->id,
|
||||
'label_id' => $label->id,
|
||||
'start_date' => '2025-01-15',
|
||||
'end_date' => '2025-06-30',
|
||||
]);
|
||||
|
||||
// Assert
|
||||
$this->assertInstanceOf(\Illuminate\Support\Carbon::class, $assignment->start_date);
|
||||
$this->assertInstanceOf(\Illuminate\Support\Carbon::class, $assignment->end_date);
|
||||
$this->assertEquals('2025-01-15', $assignment->start_date->toDateString());
|
||||
$this->assertEquals('2025-06-30', $assignment->end_date->toDateString());
|
||||
}
|
||||
|
||||
/**
|
||||
* isActive()가 날짜 범위 내일 때 true를 반환하는지 테스트
|
||||
*/
|
||||
public function test_is_active_returns_true_when_within_date_range(): void
|
||||
{
|
||||
// Arrange
|
||||
$assignment = $this->createAssignment([
|
||||
'start_date' => now()->subDay()->toDateString(),
|
||||
'end_date' => now()->addDay()->toDateString(),
|
||||
]);
|
||||
|
||||
// Act & Assert
|
||||
$this->assertTrue($assignment->isActive());
|
||||
}
|
||||
|
||||
/**
|
||||
* isActive()가 시작일 이전일 때 false를 반환하는지 테스트
|
||||
*/
|
||||
public function test_is_active_returns_false_when_before_start_date(): void
|
||||
{
|
||||
// Arrange
|
||||
$assignment = $this->createAssignment([
|
||||
'start_date' => now()->addDay()->toDateString(),
|
||||
'end_date' => now()->addDays(10)->toDateString(),
|
||||
]);
|
||||
|
||||
// Act & Assert
|
||||
$this->assertFalse($assignment->isActive());
|
||||
}
|
||||
|
||||
/**
|
||||
* isActive()가 종료일 이후일 때 false를 반환하는지 테스트
|
||||
*/
|
||||
public function test_is_active_returns_false_when_after_end_date(): void
|
||||
{
|
||||
// Arrange
|
||||
$assignment = $this->createAssignment([
|
||||
'start_date' => now()->subDays(10)->toDateString(),
|
||||
'end_date' => now()->subDay()->toDateString(),
|
||||
]);
|
||||
|
||||
// Act & Assert
|
||||
$this->assertFalse($assignment->isActive());
|
||||
}
|
||||
|
||||
/**
|
||||
* isActive()가 날짜 제한 없을 때 true를 반환하는지 테스트
|
||||
*/
|
||||
public function test_is_active_returns_true_when_no_date_limits(): void
|
||||
{
|
||||
// Arrange
|
||||
$assignment = $this->createAssignment([
|
||||
'start_date' => null,
|
||||
'end_date' => null,
|
||||
]);
|
||||
|
||||
// Act & Assert
|
||||
$this->assertTrue($assignment->isActive());
|
||||
}
|
||||
|
||||
/**
|
||||
* scopeCurrentlyActive가 활성 라벨만 필터링하는지 테스트
|
||||
*/
|
||||
public function test_scope_currently_active_filters_correctly(): void
|
||||
{
|
||||
// Arrange
|
||||
$label = $this->createLabel();
|
||||
$product = $this->createProduct();
|
||||
|
||||
// 활성 라벨 (날짜 범위 내)
|
||||
$activeAssignment = $this->assignmentClass::create([
|
||||
'product_id' => $product->id,
|
||||
'label_id' => $label->id,
|
||||
'start_date' => now()->subDay()->toDateString(),
|
||||
'end_date' => now()->addDay()->toDateString(),
|
||||
]);
|
||||
|
||||
// 비활성 라벨 (기간 지남)
|
||||
$label2 = $this->createLabel();
|
||||
$inactiveAssignment = $this->assignmentClass::create([
|
||||
'product_id' => $product->id,
|
||||
'label_id' => $label2->id,
|
||||
'start_date' => now()->subDays(10)->toDateString(),
|
||||
'end_date' => now()->subDays(5)->toDateString(),
|
||||
]);
|
||||
|
||||
// Act
|
||||
$activeAssignments = $this->assignmentClass::currentlyActive()->get();
|
||||
|
||||
// Assert
|
||||
$this->assertTrue($activeAssignments->contains('id', $activeAssignment->id));
|
||||
$this->assertFalse($activeAssignments->contains('id', $inactiveAssignment->id));
|
||||
}
|
||||
|
||||
/**
|
||||
* product 관계가 정상 동작하는지 테스트
|
||||
*/
|
||||
public function test_product_relationship_works(): void
|
||||
{
|
||||
// Arrange
|
||||
$label = $this->createLabel();
|
||||
$product = $this->createProduct(['name' => ['ko' => '관계 테스트 상품', 'en' => 'Relation Test Product']]);
|
||||
|
||||
$assignment = $this->assignmentClass::create([
|
||||
'product_id' => $product->id,
|
||||
'label_id' => $label->id,
|
||||
]);
|
||||
|
||||
// Act
|
||||
$relatedProduct = $assignment->product;
|
||||
|
||||
// Assert
|
||||
$this->assertInstanceOf($this->productClass, $relatedProduct);
|
||||
$this->assertEquals($product->id, $relatedProduct->id);
|
||||
}
|
||||
|
||||
/**
|
||||
* label 관계가 정상 동작하는지 테스트
|
||||
*/
|
||||
public function test_label_relationship_works(): void
|
||||
{
|
||||
// Arrange
|
||||
$label = $this->createLabel(['name' => ['ko' => '관계 테스트 라벨', 'en' => 'Relation Test Label']]);
|
||||
$product = $this->createProduct();
|
||||
|
||||
$assignment = $this->assignmentClass::create([
|
||||
'product_id' => $product->id,
|
||||
'label_id' => $label->id,
|
||||
]);
|
||||
|
||||
// Act
|
||||
$relatedLabel = $assignment->label;
|
||||
|
||||
// Assert
|
||||
$this->assertInstanceOf($this->labelClass, $relatedLabel);
|
||||
$this->assertEquals($label->id, $relatedLabel->id);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,255 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\Unit\Models;
|
||||
|
||||
use App\Enums\ExtensionOwnerType;
|
||||
use App\Models\Role;
|
||||
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||||
use Tests\TestCase;
|
||||
|
||||
/**
|
||||
* Role 모델 테스트
|
||||
*
|
||||
* Role 모델의 확장 소유권 관련 메서드를 검증합니다.
|
||||
*/
|
||||
class RoleModelTest extends TestCase
|
||||
{
|
||||
use RefreshDatabase;
|
||||
|
||||
// ========================================================================
|
||||
// isCore() 메서드 테스트
|
||||
// ========================================================================
|
||||
|
||||
/**
|
||||
* extension_type이 Core인 역할에서 isCore()가 true 반환
|
||||
*/
|
||||
public function test_is_core_returns_true_for_core_role(): void
|
||||
{
|
||||
$role = Role::create([
|
||||
'identifier' => 'admin',
|
||||
'name' => ['ko' => '관리자', 'en' => 'Administrator'],
|
||||
'extension_type' => ExtensionOwnerType::Core,
|
||||
'extension_identifier' => 'core',
|
||||
'is_active' => true,
|
||||
]);
|
||||
|
||||
$this->assertTrue($role->isCore());
|
||||
}
|
||||
|
||||
/**
|
||||
* extension_type이 null인 역할에서 isCore()가 false 반환
|
||||
*/
|
||||
public function test_is_core_returns_false_for_user_created_role(): void
|
||||
{
|
||||
$role = Role::create([
|
||||
'identifier' => 'custom_role',
|
||||
'name' => ['ko' => '사용자 역할', 'en' => 'Custom Role'],
|
||||
'is_active' => true,
|
||||
]);
|
||||
|
||||
$this->assertFalse($role->isCore());
|
||||
}
|
||||
|
||||
/**
|
||||
* extension_type이 Module인 역할에서 isCore()가 false 반환
|
||||
*/
|
||||
public function test_is_core_returns_false_for_module_role(): void
|
||||
{
|
||||
$role = Role::create([
|
||||
'identifier' => 'sirsoft-board.manager',
|
||||
'name' => ['ko' => '게시판 관리자', 'en' => 'Board Manager'],
|
||||
'extension_type' => ExtensionOwnerType::Module,
|
||||
'extension_identifier' => 'sirsoft-board',
|
||||
'is_active' => true,
|
||||
]);
|
||||
|
||||
$this->assertFalse($role->isCore());
|
||||
}
|
||||
|
||||
// ========================================================================
|
||||
// isDeletable() 메서드 테스트
|
||||
// ========================================================================
|
||||
|
||||
/**
|
||||
* extension_type이 null인 역할은 삭제 가능
|
||||
*/
|
||||
public function test_is_deletable_returns_true_for_user_created_role(): void
|
||||
{
|
||||
$role = Role::create([
|
||||
'identifier' => 'custom_role',
|
||||
'name' => ['ko' => '사용자 역할', 'en' => 'Custom Role'],
|
||||
'is_active' => true,
|
||||
]);
|
||||
|
||||
$this->assertTrue($role->isDeletable());
|
||||
}
|
||||
|
||||
/**
|
||||
* extension_type이 Core인 역할은 삭제 불가
|
||||
*/
|
||||
public function test_is_deletable_returns_false_for_core_role(): void
|
||||
{
|
||||
$role = Role::create([
|
||||
'identifier' => 'admin',
|
||||
'name' => ['ko' => '관리자', 'en' => 'Administrator'],
|
||||
'extension_type' => ExtensionOwnerType::Core,
|
||||
'extension_identifier' => 'core',
|
||||
'is_active' => true,
|
||||
]);
|
||||
|
||||
$this->assertFalse($role->isDeletable());
|
||||
}
|
||||
|
||||
/**
|
||||
* extension_type이 Module인 역할은 삭제 불가
|
||||
*/
|
||||
public function test_is_deletable_returns_false_for_module_role(): void
|
||||
{
|
||||
$role = Role::create([
|
||||
'identifier' => 'sirsoft-board.manager',
|
||||
'name' => ['ko' => '게시판 관리자', 'en' => 'Board Manager'],
|
||||
'extension_type' => ExtensionOwnerType::Module,
|
||||
'extension_identifier' => 'sirsoft-board',
|
||||
'is_active' => true,
|
||||
]);
|
||||
|
||||
$this->assertFalse($role->isDeletable());
|
||||
}
|
||||
|
||||
/**
|
||||
* extension_type이 Plugin인 역할은 삭제 불가
|
||||
*/
|
||||
public function test_is_deletable_returns_false_for_plugin_role(): void
|
||||
{
|
||||
$role = Role::create([
|
||||
'identifier' => 'sirsoft-payment.manager',
|
||||
'name' => ['ko' => '결제 관리자', 'en' => 'Payment Manager'],
|
||||
'extension_type' => ExtensionOwnerType::Plugin,
|
||||
'extension_identifier' => 'sirsoft-payment',
|
||||
'is_active' => true,
|
||||
]);
|
||||
|
||||
$this->assertFalse($role->isDeletable());
|
||||
}
|
||||
|
||||
// ========================================================================
|
||||
// isExtensionOwned() 메서드 테스트
|
||||
// ========================================================================
|
||||
|
||||
/**
|
||||
* extension_type이 Module인 역할에서 isExtensionOwned()가 true 반환
|
||||
*/
|
||||
public function test_is_extension_owned_returns_true_for_module_role(): void
|
||||
{
|
||||
$role = Role::create([
|
||||
'identifier' => 'sirsoft-board.manager',
|
||||
'name' => ['ko' => '게시판 관리자', 'en' => 'Board Manager'],
|
||||
'extension_type' => ExtensionOwnerType::Module,
|
||||
'extension_identifier' => 'sirsoft-board',
|
||||
'is_active' => true,
|
||||
]);
|
||||
|
||||
$this->assertTrue($role->isExtensionOwned());
|
||||
}
|
||||
|
||||
/**
|
||||
* extension_type이 Plugin인 역할에서 isExtensionOwned()가 true 반환
|
||||
*/
|
||||
public function test_is_extension_owned_returns_true_for_plugin_role(): void
|
||||
{
|
||||
$role = Role::create([
|
||||
'identifier' => 'sirsoft-payment.manager',
|
||||
'name' => ['ko' => '결제 관리자', 'en' => 'Payment Manager'],
|
||||
'extension_type' => ExtensionOwnerType::Plugin,
|
||||
'extension_identifier' => 'sirsoft-payment',
|
||||
'is_active' => true,
|
||||
]);
|
||||
|
||||
$this->assertTrue($role->isExtensionOwned());
|
||||
}
|
||||
|
||||
/**
|
||||
* extension_type이 Core인 역할에서 isExtensionOwned()가 false 반환
|
||||
*/
|
||||
public function test_is_extension_owned_returns_false_for_core_role(): void
|
||||
{
|
||||
$role = Role::create([
|
||||
'identifier' => 'admin',
|
||||
'name' => ['ko' => '관리자', 'en' => 'Administrator'],
|
||||
'extension_type' => ExtensionOwnerType::Core,
|
||||
'extension_identifier' => 'core',
|
||||
'is_active' => true,
|
||||
]);
|
||||
|
||||
$this->assertFalse($role->isExtensionOwned());
|
||||
}
|
||||
|
||||
/**
|
||||
* extension_type이 null인 역할에서 isExtensionOwned()가 false 반환
|
||||
*/
|
||||
public function test_is_extension_owned_returns_false_for_user_created_role(): void
|
||||
{
|
||||
$role = Role::create([
|
||||
'identifier' => 'custom_role',
|
||||
'name' => ['ko' => '사용자 역할', 'en' => 'Custom Role'],
|
||||
'is_active' => true,
|
||||
]);
|
||||
|
||||
$this->assertFalse($role->isExtensionOwned());
|
||||
}
|
||||
|
||||
// ========================================================================
|
||||
// extension_type 캐스팅 테스트
|
||||
// ========================================================================
|
||||
|
||||
/**
|
||||
* extension_type 필드가 ExtensionOwnerType Enum으로 캐스팅되는지 확인
|
||||
*/
|
||||
public function test_extension_type_is_cast_to_enum(): void
|
||||
{
|
||||
$role = Role::create([
|
||||
'identifier' => 'test_cast',
|
||||
'name' => ['ko' => '캐스트 테스트', 'en' => 'Cast Test'],
|
||||
'extension_type' => ExtensionOwnerType::Module,
|
||||
'extension_identifier' => 'test-module',
|
||||
'is_active' => true,
|
||||
]);
|
||||
|
||||
$freshRole = Role::find($role->id);
|
||||
|
||||
$this->assertInstanceOf(ExtensionOwnerType::class, $freshRole->extension_type);
|
||||
$this->assertEquals(ExtensionOwnerType::Module, $freshRole->extension_type);
|
||||
}
|
||||
|
||||
/**
|
||||
* extension_type이 null일 때 올바르게 처리되는지 확인
|
||||
*/
|
||||
public function test_extension_type_null_is_handled_correctly(): void
|
||||
{
|
||||
$role = Role::create([
|
||||
'identifier' => 'test_null',
|
||||
'name' => ['ko' => 'Null 테스트', 'en' => 'Null Test'],
|
||||
'is_active' => true,
|
||||
]);
|
||||
|
||||
$freshRole = Role::find($role->id);
|
||||
|
||||
$this->assertNull($freshRole->extension_type);
|
||||
$this->assertNull($freshRole->extension_identifier);
|
||||
}
|
||||
|
||||
// ========================================================================
|
||||
// fillable 테스트
|
||||
// ========================================================================
|
||||
|
||||
/**
|
||||
* extension_type과 extension_identifier가 fillable에 포함되어 있는지 확인
|
||||
*/
|
||||
public function test_extension_fields_are_in_fillable(): void
|
||||
{
|
||||
$role = new Role();
|
||||
|
||||
$this->assertContains('extension_type', $role->getFillable());
|
||||
$this->assertContains('extension_identifier', $role->getFillable());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,254 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\Unit\Models;
|
||||
|
||||
use App\Models\Template;
|
||||
use App\Models\TemplateLayout;
|
||||
use App\Models\TemplateLayoutVersion;
|
||||
use App\Models\User;
|
||||
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||||
use Tests\TestCase;
|
||||
|
||||
class TemplateLayoutVersionTest extends TestCase
|
||||
{
|
||||
use RefreshDatabase;
|
||||
|
||||
/**
|
||||
* 레이아웃 버전 관계 테스트
|
||||
*/
|
||||
public function test_template_layout_has_versions_relationship(): void
|
||||
{
|
||||
// Arrange
|
||||
$template = Template::factory()->create();
|
||||
$layout = TemplateLayout::factory()->create(['template_id' => $template->id]);
|
||||
|
||||
$version1 = TemplateLayoutVersion::factory()->create([
|
||||
'layout_id' => $layout->id,
|
||||
'version' => 1,
|
||||
]);
|
||||
|
||||
$version2 = TemplateLayoutVersion::factory()->create([
|
||||
'layout_id' => $layout->id,
|
||||
'version' => 2,
|
||||
]);
|
||||
|
||||
// Act
|
||||
$versions = $layout->versions;
|
||||
|
||||
// Assert
|
||||
$this->assertCount(2, $versions);
|
||||
$this->assertEquals(2, $versions->first()->version); // 최신순 정렬
|
||||
$this->assertEquals(1, $versions->last()->version);
|
||||
}
|
||||
|
||||
/**
|
||||
* 최신 버전 조회 테스트
|
||||
*/
|
||||
public function test_can_get_latest_version(): void
|
||||
{
|
||||
// Arrange
|
||||
$template = Template::factory()->create();
|
||||
$layout = TemplateLayout::factory()->create(['template_id' => $template->id]);
|
||||
|
||||
TemplateLayoutVersion::factory()->create([
|
||||
'layout_id' => $layout->id,
|
||||
'version' => 1,
|
||||
]);
|
||||
|
||||
$latestVersion = TemplateLayoutVersion::factory()->create([
|
||||
'layout_id' => $layout->id,
|
||||
'version' => 2,
|
||||
]);
|
||||
|
||||
// Act
|
||||
$result = $layout->latestVersion();
|
||||
|
||||
// Assert
|
||||
$this->assertNotNull($result);
|
||||
$this->assertEquals($latestVersion->id, $result->id);
|
||||
$this->assertEquals(2, $result->version);
|
||||
}
|
||||
|
||||
/**
|
||||
* 버전이 없을 때 null 반환 테스트
|
||||
*/
|
||||
public function test_latest_version_returns_null_when_no_versions(): void
|
||||
{
|
||||
// Arrange
|
||||
$template = Template::factory()->create();
|
||||
$layout = TemplateLayout::factory()->create(['template_id' => $template->id]);
|
||||
|
||||
// Act
|
||||
$result = $layout->latestVersion();
|
||||
|
||||
// Assert
|
||||
$this->assertNull($result);
|
||||
}
|
||||
|
||||
/**
|
||||
* 특정 버전 조회 테스트
|
||||
*/
|
||||
public function test_can_get_specific_version(): void
|
||||
{
|
||||
// Arrange
|
||||
$template = Template::factory()->create();
|
||||
$layout = TemplateLayout::factory()->create(['template_id' => $template->id]);
|
||||
|
||||
TemplateLayoutVersion::factory()->create([
|
||||
'layout_id' => $layout->id,
|
||||
'version' => 1,
|
||||
]);
|
||||
|
||||
$version2 = TemplateLayoutVersion::factory()->create([
|
||||
'layout_id' => $layout->id,
|
||||
'version' => 2,
|
||||
]);
|
||||
|
||||
// Act
|
||||
$result = $layout->getVersion(2);
|
||||
|
||||
// Assert
|
||||
$this->assertNotNull($result);
|
||||
$this->assertEquals($version2->id, $result->id);
|
||||
$this->assertEquals(2, $result->version);
|
||||
}
|
||||
|
||||
/**
|
||||
* 존재하지 않는 버전 조회 시 null 반환 테스트
|
||||
*/
|
||||
public function test_get_version_returns_null_when_version_not_found(): void
|
||||
{
|
||||
// Arrange
|
||||
$template = Template::factory()->create();
|
||||
$layout = TemplateLayout::factory()->create(['template_id' => $template->id]);
|
||||
|
||||
TemplateLayoutVersion::factory()->create([
|
||||
'layout_id' => $layout->id,
|
||||
'version' => 1,
|
||||
]);
|
||||
|
||||
// Act
|
||||
$result = $layout->getVersion(99);
|
||||
|
||||
// Assert
|
||||
$this->assertNull($result);
|
||||
}
|
||||
|
||||
/**
|
||||
* Eager loading 테스트
|
||||
*/
|
||||
public function test_can_eager_load_versions(): void
|
||||
{
|
||||
// Arrange
|
||||
$template = Template::factory()->create();
|
||||
$layout = TemplateLayout::factory()->create(['template_id' => $template->id]);
|
||||
|
||||
TemplateLayoutVersion::factory()->count(3)->create([
|
||||
'layout_id' => $layout->id,
|
||||
]);
|
||||
|
||||
// Act
|
||||
$layoutWithVersions = TemplateLayout::with('versions')->find($layout->id);
|
||||
|
||||
// Assert
|
||||
$this->assertTrue($layoutWithVersions->relationLoaded('versions'));
|
||||
$this->assertCount(3, $layoutWithVersions->versions);
|
||||
}
|
||||
|
||||
/**
|
||||
* content 필드 배열 캐스팅 테스트
|
||||
*/
|
||||
public function test_content_is_cast_to_array(): void
|
||||
{
|
||||
// Arrange
|
||||
$template = Template::factory()->create();
|
||||
$layout = TemplateLayout::factory()->create(['template_id' => $template->id]);
|
||||
|
||||
$contentData = [
|
||||
'version' => '1.0.0',
|
||||
'components' => [
|
||||
['type' => 'header', 'props' => ['title' => 'Test']],
|
||||
],
|
||||
];
|
||||
|
||||
// Act
|
||||
$version = TemplateLayoutVersion::factory()->create([
|
||||
'layout_id' => $layout->id,
|
||||
'version' => 1,
|
||||
'content' => $contentData,
|
||||
]);
|
||||
|
||||
// Assert
|
||||
$this->assertIsArray($version->content);
|
||||
$this->assertEquals($contentData, $version->content);
|
||||
}
|
||||
|
||||
/**
|
||||
* changes_summary 필드 배열 캐스팅 테스트
|
||||
*/
|
||||
public function test_changes_summary_is_cast_to_array(): void
|
||||
{
|
||||
// Arrange
|
||||
$template = Template::factory()->create();
|
||||
$layout = TemplateLayout::factory()->create(['template_id' => $template->id]);
|
||||
|
||||
$changesSummary = [
|
||||
'added' => 3,
|
||||
'removed' => 2,
|
||||
'is_restored' => false,
|
||||
];
|
||||
|
||||
// Act
|
||||
$version = TemplateLayoutVersion::factory()->create([
|
||||
'layout_id' => $layout->id,
|
||||
'version' => 1,
|
||||
'changes_summary' => $changesSummary,
|
||||
]);
|
||||
|
||||
// Assert
|
||||
$this->assertIsArray($version->changes_summary);
|
||||
$this->assertEquals($changesSummary, $version->changes_summary);
|
||||
}
|
||||
|
||||
/**
|
||||
* belongsTo 관계 테스트
|
||||
*/
|
||||
public function test_belongs_to_layout(): void
|
||||
{
|
||||
// Arrange
|
||||
$template = Template::factory()->create();
|
||||
$layout = TemplateLayout::factory()->create(['template_id' => $template->id]);
|
||||
|
||||
// Act
|
||||
$version = TemplateLayoutVersion::factory()->create([
|
||||
'layout_id' => $layout->id,
|
||||
'version' => 1,
|
||||
]);
|
||||
|
||||
// Assert
|
||||
$this->assertInstanceOf(TemplateLayout::class, $version->layout);
|
||||
$this->assertEquals($layout->id, $version->layout->id);
|
||||
}
|
||||
|
||||
/**
|
||||
* creator 관계 테스트
|
||||
*/
|
||||
public function test_belongs_to_creator(): void
|
||||
{
|
||||
// Arrange
|
||||
$template = Template::factory()->create();
|
||||
$layout = TemplateLayout::factory()->create(['template_id' => $template->id]);
|
||||
$user = User::factory()->create();
|
||||
|
||||
// Act
|
||||
$version = TemplateLayoutVersion::factory()->create([
|
||||
'layout_id' => $layout->id,
|
||||
'version' => 1,
|
||||
'created_by' => $user->id,
|
||||
]);
|
||||
|
||||
// Assert
|
||||
$this->assertInstanceOf(User::class, $version->creator);
|
||||
$this->assertEquals($user->id, $version->creator->id);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,451 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\Unit\Models;
|
||||
|
||||
use App\Enums\ExtensionOwnerType;
|
||||
use App\Enums\PermissionType;
|
||||
use App\Models\Permission;
|
||||
use App\Models\Role;
|
||||
use App\Models\User;
|
||||
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||||
use Tests\TestCase;
|
||||
use PHPUnit\Framework\Attributes\Test;
|
||||
|
||||
/**
|
||||
* User 모델 테스트
|
||||
*
|
||||
* User 모델의 is_super 필드 및 관련 메서드를 검증합니다.
|
||||
*/
|
||||
class UserModelTest extends TestCase
|
||||
{
|
||||
use RefreshDatabase;
|
||||
|
||||
// ========================================================================
|
||||
// is_super 필드 테스트
|
||||
// ========================================================================
|
||||
|
||||
/**
|
||||
* is_super 필드가 fillable에 포함되어 있는지 확인
|
||||
*/
|
||||
public function test_is_super_is_in_fillable(): void
|
||||
{
|
||||
$user = new User();
|
||||
|
||||
$this->assertContains('is_super', $user->getFillable());
|
||||
}
|
||||
|
||||
/**
|
||||
* is_super 필드가 boolean으로 캐스팅되는지 확인
|
||||
*/
|
||||
public function test_is_super_is_cast_to_boolean(): void
|
||||
{
|
||||
$user = User::factory()->create(['is_super' => 1]);
|
||||
|
||||
$this->assertIsBool($user->is_super);
|
||||
$this->assertTrue($user->is_super);
|
||||
}
|
||||
|
||||
/**
|
||||
* is_super 기본값이 false인지 확인
|
||||
*/
|
||||
public function test_is_super_defaults_to_false(): void
|
||||
{
|
||||
$user = User::factory()->create();
|
||||
|
||||
$this->assertFalse($user->is_super);
|
||||
}
|
||||
|
||||
// ========================================================================
|
||||
// isSuperAdmin() 메서드 테스트
|
||||
// ========================================================================
|
||||
|
||||
/**
|
||||
* is_super가 true인 사용자에서 isSuperAdmin()이 true 반환
|
||||
*/
|
||||
public function test_is_super_admin_returns_true_when_is_super_is_true(): void
|
||||
{
|
||||
$user = User::factory()->create(['is_super' => true]);
|
||||
|
||||
$this->assertTrue($user->isSuperAdmin());
|
||||
}
|
||||
|
||||
/**
|
||||
* is_super가 false인 사용자에서 isSuperAdmin()이 false 반환
|
||||
*/
|
||||
public function test_is_super_admin_returns_false_when_is_super_is_false(): void
|
||||
{
|
||||
$user = User::factory()->create(['is_super' => false]);
|
||||
|
||||
$this->assertFalse($user->isSuperAdmin());
|
||||
}
|
||||
|
||||
/**
|
||||
* is_super가 null인 사용자에서 isSuperAdmin()이 false 반환
|
||||
*/
|
||||
public function test_is_super_admin_returns_false_when_is_super_is_null(): void
|
||||
{
|
||||
$user = User::factory()->create();
|
||||
// 직접 null로 설정 (DB에서 기본값이 false이지만 null 상황 테스트)
|
||||
$user->is_super = null;
|
||||
|
||||
$this->assertFalse($user->isSuperAdmin());
|
||||
}
|
||||
|
||||
// ========================================================================
|
||||
// isAdmin() 메서드 테스트
|
||||
// ========================================================================
|
||||
|
||||
/**
|
||||
* admin 타입 권한을 가진 역할이 있으면 isAdmin()이 true 반환
|
||||
*/
|
||||
public function test_is_admin_returns_true_when_user_has_admin_type_permission(): void
|
||||
{
|
||||
// admin 타입 권한 생성
|
||||
$adminPermission = Permission::create([
|
||||
'identifier' => 'test.admin.permission',
|
||||
'name' => ['ko' => '관리자 권한', 'en' => 'Admin Permission'],
|
||||
'type' => PermissionType::Admin,
|
||||
]);
|
||||
|
||||
// 역할 생성 및 권한 연결
|
||||
$role = Role::create([
|
||||
'identifier' => 'test-admin-role',
|
||||
'name' => ['ko' => '테스트 관리자', 'en' => 'Test Admin'],
|
||||
]);
|
||||
$role->permissions()->attach($adminPermission->id);
|
||||
|
||||
// 사용자 생성 및 역할 연결
|
||||
$user = User::factory()->create();
|
||||
$user->roles()->attach($role->id);
|
||||
|
||||
$this->assertTrue($user->isAdmin());
|
||||
}
|
||||
|
||||
/**
|
||||
* user 타입 권한만 가진 경우 isAdmin()이 false 반환
|
||||
*/
|
||||
public function test_is_admin_returns_false_when_user_has_only_user_type_permission(): void
|
||||
{
|
||||
// user 타입 권한 생성
|
||||
$userPermission = Permission::create([
|
||||
'identifier' => 'test.user.permission',
|
||||
'name' => ['ko' => '사용자 권한', 'en' => 'User Permission'],
|
||||
'type' => PermissionType::User,
|
||||
]);
|
||||
|
||||
// 역할 생성 및 권한 연결
|
||||
$role = Role::create([
|
||||
'identifier' => 'test-user-role',
|
||||
'name' => ['ko' => '테스트 사용자', 'en' => 'Test User'],
|
||||
]);
|
||||
$role->permissions()->attach($userPermission->id);
|
||||
|
||||
// 사용자 생성 및 역할 연결
|
||||
$user = User::factory()->create();
|
||||
$user->roles()->attach($role->id);
|
||||
|
||||
$this->assertFalse($user->isAdmin());
|
||||
}
|
||||
|
||||
/**
|
||||
* 권한이 없는 사용자에서 isAdmin()이 false 반환
|
||||
*/
|
||||
public function test_is_admin_returns_false_when_user_has_no_permissions(): void
|
||||
{
|
||||
$user = User::factory()->create();
|
||||
|
||||
$this->assertFalse($user->isAdmin());
|
||||
}
|
||||
|
||||
/**
|
||||
* 역할이 없는 사용자에서 isAdmin()이 false 반환
|
||||
*/
|
||||
public function test_is_admin_returns_false_when_user_has_no_roles(): void
|
||||
{
|
||||
$user = User::factory()->create();
|
||||
|
||||
// 역할 없음 확인
|
||||
$this->assertEquals(0, $user->roles()->count());
|
||||
$this->assertFalse($user->isAdmin());
|
||||
}
|
||||
|
||||
/**
|
||||
* admin과 user 타입 권한을 모두 가진 경우 isAdmin()이 true 반환
|
||||
*/
|
||||
public function test_is_admin_returns_true_when_user_has_both_admin_and_user_permissions(): void
|
||||
{
|
||||
// admin 타입 권한
|
||||
$adminPermission = Permission::create([
|
||||
'identifier' => 'test.mixed.admin',
|
||||
'name' => ['ko' => '관리자 권한', 'en' => 'Admin Permission'],
|
||||
'type' => PermissionType::Admin,
|
||||
]);
|
||||
|
||||
// user 타입 권한
|
||||
$userPermission = Permission::create([
|
||||
'identifier' => 'test.mixed.user',
|
||||
'name' => ['ko' => '사용자 권한', 'en' => 'User Permission'],
|
||||
'type' => PermissionType::User,
|
||||
]);
|
||||
|
||||
// 역할 생성 및 두 권한 모두 연결
|
||||
$role = Role::create([
|
||||
'identifier' => 'test-mixed-role',
|
||||
'name' => ['ko' => '혼합 역할', 'en' => 'Mixed Role'],
|
||||
]);
|
||||
$role->permissions()->attach([$adminPermission->id, $userPermission->id]);
|
||||
|
||||
// 사용자 생성 및 역할 연결
|
||||
$user = User::factory()->create();
|
||||
$user->roles()->attach($role->id);
|
||||
|
||||
$this->assertTrue($user->isAdmin());
|
||||
}
|
||||
|
||||
// ========================================================================
|
||||
// scopeSuperAdmins() 스코프 테스트
|
||||
// ========================================================================
|
||||
|
||||
/**
|
||||
* superAdmins() 스코프가 is_super=true인 사용자만 조회하는지 확인
|
||||
*/
|
||||
public function test_super_admins_scope_returns_only_super_admins(): void
|
||||
{
|
||||
// 슈퍼 관리자 2명 생성
|
||||
User::factory()->create(['is_super' => true, 'email' => 'super1@test.com']);
|
||||
User::factory()->create(['is_super' => true, 'email' => 'super2@test.com']);
|
||||
|
||||
// 일반 사용자 3명 생성
|
||||
User::factory()->create(['is_super' => false, 'email' => 'user1@test.com']);
|
||||
User::factory()->create(['is_super' => false, 'email' => 'user2@test.com']);
|
||||
User::factory()->create(['is_super' => false, 'email' => 'user3@test.com']);
|
||||
|
||||
$superAdmins = User::superAdmins()->get();
|
||||
|
||||
$this->assertCount(2, $superAdmins);
|
||||
$superAdmins->each(function ($user) {
|
||||
$this->assertTrue($user->is_super);
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* 슈퍼 관리자가 없으면 빈 컬렉션 반환
|
||||
*/
|
||||
#[Test]
|
||||
public function test_super_admins_scope_returns_empty_when_no_super_admins(): void
|
||||
{
|
||||
// 일반 사용자만 생성
|
||||
User::factory()->count(3)->create(['is_super' => false]);
|
||||
|
||||
$superAdmins = User::superAdmins()->get();
|
||||
|
||||
$this->assertCount(0, $superAdmins);
|
||||
}
|
||||
|
||||
// ========================================================================
|
||||
// 복합 시나리오 테스트
|
||||
// ========================================================================
|
||||
|
||||
/**
|
||||
* 슈퍼 관리자이면서 관리자 권한도 가진 사용자 테스트
|
||||
*/
|
||||
public function test_user_can_be_both_super_admin_and_have_admin_permissions(): void
|
||||
{
|
||||
// admin 타입 권한 생성
|
||||
$adminPermission = Permission::create([
|
||||
'identifier' => 'core.users.read',
|
||||
'name' => ['ko' => '사용자 조회', 'en' => 'Read Users'],
|
||||
'extension_type' => ExtensionOwnerType::Core,
|
||||
'extension_identifier' => 'core',
|
||||
'type' => PermissionType::Admin,
|
||||
]);
|
||||
|
||||
// 역할 생성 및 권한 연결
|
||||
$role = Role::create([
|
||||
'identifier' => 'admin',
|
||||
'name' => ['ko' => '관리자', 'en' => 'Administrator'],
|
||||
'extension_type' => ExtensionOwnerType::Core,
|
||||
'extension_identifier' => 'core',
|
||||
]);
|
||||
$role->permissions()->attach($adminPermission->id);
|
||||
|
||||
// 슈퍼 관리자 생성 및 역할 연결
|
||||
$user = User::factory()->create(['is_super' => true]);
|
||||
$user->roles()->attach($role->id);
|
||||
|
||||
$this->assertTrue($user->isSuperAdmin());
|
||||
$this->assertTrue($user->isAdmin());
|
||||
}
|
||||
|
||||
/**
|
||||
* 일반 관리자(is_super=false)이지만 관리자 권한은 가진 사용자 테스트
|
||||
*/
|
||||
public function test_regular_admin_has_admin_permissions_but_not_super(): void
|
||||
{
|
||||
// admin 타입 권한 생성
|
||||
$adminPermission = Permission::create([
|
||||
'identifier' => 'core.settings.read',
|
||||
'name' => ['ko' => '설정 조회', 'en' => 'Read Settings'],
|
||||
'extension_type' => ExtensionOwnerType::Core,
|
||||
'extension_identifier' => 'core',
|
||||
'type' => PermissionType::Admin,
|
||||
]);
|
||||
|
||||
// 역할 생성 및 권한 연결
|
||||
$role = Role::create([
|
||||
'identifier' => 'manager',
|
||||
'name' => ['ko' => '매니저', 'en' => 'Manager'],
|
||||
]);
|
||||
$role->permissions()->attach($adminPermission->id);
|
||||
|
||||
// 일반 관리자 생성 (is_super=false)
|
||||
$user = User::factory()->create(['is_super' => false]);
|
||||
$user->roles()->attach($role->id);
|
||||
|
||||
$this->assertFalse($user->isSuperAdmin());
|
||||
$this->assertTrue($user->isAdmin());
|
||||
}
|
||||
|
||||
// ========================================================================
|
||||
// hasPermission() 메서드 type 파라미터 테스트
|
||||
// ========================================================================
|
||||
|
||||
/**
|
||||
* hasPermission()에서 type을 지정하면 해당 타입의 권한만 체크합니다.
|
||||
*/
|
||||
public function test_has_permission_with_type_checks_specific_type(): void
|
||||
{
|
||||
// admin 타입 권한 생성
|
||||
$adminPermission = Permission::create([
|
||||
'identifier' => 'test.permission',
|
||||
'name' => ['ko' => '테스트 권한', 'en' => 'Test Permission'],
|
||||
'type' => PermissionType::Admin,
|
||||
]);
|
||||
|
||||
// 역할 생성 및 권한 연결
|
||||
$role = Role::create([
|
||||
'identifier' => 'test-role',
|
||||
'name' => ['ko' => '테스트 역할', 'en' => 'Test Role'],
|
||||
]);
|
||||
$role->permissions()->attach($adminPermission->id);
|
||||
|
||||
// 사용자 생성 및 역할 연결
|
||||
$user = User::factory()->create();
|
||||
$user->roles()->attach($role->id);
|
||||
|
||||
// admin 타입으로 체크하면 true
|
||||
$this->assertTrue($user->hasPermission('test.permission', PermissionType::Admin));
|
||||
|
||||
// user 타입으로 체크하면 false
|
||||
$this->assertFalse($user->hasPermission('test.permission', PermissionType::User));
|
||||
|
||||
// type 없이 체크하면 true (기존 동작 유지)
|
||||
$this->assertTrue($user->hasPermission('test.permission'));
|
||||
}
|
||||
|
||||
/**
|
||||
* hasPermission()에서 type을 null로 지정하면 모든 타입을 체크합니다.
|
||||
*/
|
||||
public function test_has_permission_without_type_checks_all_types(): void
|
||||
{
|
||||
// user 타입 권한 생성
|
||||
$userPermission = Permission::create([
|
||||
'identifier' => 'test.user.only',
|
||||
'name' => ['ko' => '사용자 전용 권한', 'en' => 'User Only Permission'],
|
||||
'type' => PermissionType::User,
|
||||
]);
|
||||
|
||||
// 역할 생성 및 권한 연결
|
||||
$role = Role::create([
|
||||
'identifier' => 'user-role',
|
||||
'name' => ['ko' => '사용자 역할', 'en' => 'User Role'],
|
||||
]);
|
||||
$role->permissions()->attach($userPermission->id);
|
||||
|
||||
// 사용자 생성 및 역할 연결
|
||||
$user = User::factory()->create();
|
||||
$user->roles()->attach($role->id);
|
||||
|
||||
// type 없이 체크하면 true
|
||||
$this->assertTrue($user->hasPermission('test.user.only'));
|
||||
$this->assertTrue($user->hasPermission('test.user.only', null));
|
||||
}
|
||||
|
||||
// ========================================================================
|
||||
// hasPermissions() 메서드 type 파라미터 테스트
|
||||
// ========================================================================
|
||||
|
||||
/**
|
||||
* hasPermissions()에서 type을 지정하면 해당 타입의 권한만 체크합니다.
|
||||
*/
|
||||
public function test_has_permissions_with_type_checks_specific_type(): void
|
||||
{
|
||||
// admin 타입 권한 2개 생성
|
||||
$adminPerm1 = Permission::create([
|
||||
'identifier' => 'admin.perm1',
|
||||
'name' => ['ko' => '관리자 권한1', 'en' => 'Admin Permission 1'],
|
||||
'type' => PermissionType::Admin,
|
||||
]);
|
||||
|
||||
$adminPerm2 = Permission::create([
|
||||
'identifier' => 'admin.perm2',
|
||||
'name' => ['ko' => '관리자 권한2', 'en' => 'Admin Permission 2'],
|
||||
'type' => PermissionType::Admin,
|
||||
]);
|
||||
|
||||
// 역할 생성 및 권한 연결
|
||||
$role = Role::create([
|
||||
'identifier' => 'multi-admin-role',
|
||||
'name' => ['ko' => '다중 권한 역할', 'en' => 'Multi Permission Role'],
|
||||
]);
|
||||
$role->permissions()->attach([$adminPerm1->id, $adminPerm2->id]);
|
||||
|
||||
// 사용자 생성 및 역할 연결
|
||||
$user = User::factory()->create();
|
||||
$user->roles()->attach($role->id);
|
||||
|
||||
// admin 타입으로 AND 체크
|
||||
$this->assertTrue(
|
||||
$user->hasPermissions(['admin.perm1', 'admin.perm2'], true, PermissionType::Admin)
|
||||
);
|
||||
|
||||
// user 타입으로 체크하면 실패
|
||||
$this->assertFalse(
|
||||
$user->hasPermissions(['admin.perm1', 'admin.perm2'], true, PermissionType::User)
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* hasPermissions()에서 OR 로직과 type을 함께 사용합니다.
|
||||
*/
|
||||
public function test_has_permissions_with_or_logic_and_type(): void
|
||||
{
|
||||
// admin 타입 권한 1개만 생성
|
||||
$adminPerm = Permission::create([
|
||||
'identifier' => 'admin.single',
|
||||
'name' => ['ko' => '단일 관리자 권한', 'en' => 'Single Admin Permission'],
|
||||
'type' => PermissionType::Admin,
|
||||
]);
|
||||
|
||||
// 역할 생성 및 권한 연결
|
||||
$role = Role::create([
|
||||
'identifier' => 'single-perm-role',
|
||||
'name' => ['ko' => '단일 권한 역할', 'en' => 'Single Permission Role'],
|
||||
]);
|
||||
$role->permissions()->attach($adminPerm->id);
|
||||
|
||||
// 사용자 생성 및 역할 연결
|
||||
$user = User::factory()->create();
|
||||
$user->roles()->attach($role->id);
|
||||
|
||||
// OR 로직으로 admin 타입 체크 - 하나만 있어도 true
|
||||
$this->assertTrue(
|
||||
$user->hasPermissions(['admin.single', 'admin.nonexistent'], false, PermissionType::Admin)
|
||||
);
|
||||
|
||||
// OR 로직으로 user 타입 체크 - 해당 권한 없으므로 false
|
||||
$this->assertFalse(
|
||||
$user->hasPermissions(['admin.single', 'admin.nonexistent'], false, PermissionType::User)
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,149 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\Unit\Models;
|
||||
|
||||
use App\Enums\PermissionType;
|
||||
use App\Models\Permission;
|
||||
use App\Models\Role;
|
||||
use App\Models\User;
|
||||
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||||
use Tests\TestCase;
|
||||
|
||||
class UserScopeTest extends TestCase
|
||||
{
|
||||
use RefreshDatabase;
|
||||
|
||||
private Permission $permission;
|
||||
|
||||
protected function setUp(): void
|
||||
{
|
||||
parent::setUp();
|
||||
|
||||
$this->permission = Permission::create([
|
||||
'identifier' => 'test.scope.read',
|
||||
'name' => ['ko' => '테스트 스코프 권한', 'en' => 'Test Scope Permission'],
|
||||
'description' => ['ko' => '스코프 테스트용', 'en' => 'For scope testing'],
|
||||
'type' => PermissionType::Admin,
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* 역할 1개, scope_type=null → null 반환
|
||||
*/
|
||||
public function test_single_role_with_null_scope_returns_null(): void
|
||||
{
|
||||
$user = $this->createUserWithScope(null);
|
||||
$this->assertNull($user->getEffectiveScopeForPermission('test.scope.read'));
|
||||
}
|
||||
|
||||
/**
|
||||
* 역할 1개, scope_type='self' → 'self' 반환
|
||||
*/
|
||||
public function test_single_role_with_self_scope_returns_self(): void
|
||||
{
|
||||
$user = $this->createUserWithScope('self');
|
||||
$this->assertSame('self', $user->getEffectiveScopeForPermission('test.scope.read'));
|
||||
}
|
||||
|
||||
/**
|
||||
* 역할 1개, scope_type='role' → 'role' 반환
|
||||
*/
|
||||
public function test_single_role_with_role_scope_returns_role(): void
|
||||
{
|
||||
$user = $this->createUserWithScope('role');
|
||||
$this->assertSame('role', $user->getEffectiveScopeForPermission('test.scope.read'));
|
||||
}
|
||||
|
||||
/**
|
||||
* 역할 2개: null + self → null (union: 가장 넓은 범위)
|
||||
*/
|
||||
public function test_two_roles_null_and_self_returns_null(): void
|
||||
{
|
||||
$user = User::factory()->create();
|
||||
|
||||
$role1 = $this->createRoleWithScope('role_null', null);
|
||||
$role2 = $this->createRoleWithScope('role_self', 'self');
|
||||
|
||||
$user->roles()->attach([$role1->id, $role2->id]);
|
||||
|
||||
$this->assertNull($user->getEffectiveScopeForPermission('test.scope.read'));
|
||||
}
|
||||
|
||||
/**
|
||||
* 역할 2개: role + self → 'role' (union)
|
||||
*/
|
||||
public function test_two_roles_role_and_self_returns_role(): void
|
||||
{
|
||||
$user = User::factory()->create();
|
||||
|
||||
$role1 = $this->createRoleWithScope('role_r', 'role');
|
||||
$role2 = $this->createRoleWithScope('role_s', 'self');
|
||||
|
||||
$user->roles()->attach([$role1->id, $role2->id]);
|
||||
|
||||
$this->assertSame('role', $user->getEffectiveScopeForPermission('test.scope.read'));
|
||||
}
|
||||
|
||||
/**
|
||||
* 역할 2개: null + role → null (union)
|
||||
*/
|
||||
public function test_two_roles_null_and_role_returns_null(): void
|
||||
{
|
||||
$user = User::factory()->create();
|
||||
|
||||
$role1 = $this->createRoleWithScope('role_null2', null);
|
||||
$role2 = $this->createRoleWithScope('role_r2', 'role');
|
||||
|
||||
$user->roles()->attach([$role1->id, $role2->id]);
|
||||
|
||||
$this->assertNull($user->getEffectiveScopeForPermission('test.scope.read'));
|
||||
}
|
||||
|
||||
/**
|
||||
* 역할 3개: self + self + self → 'self'
|
||||
*/
|
||||
public function test_three_roles_all_self_returns_self(): void
|
||||
{
|
||||
$user = User::factory()->create();
|
||||
|
||||
$role1 = $this->createRoleWithScope('role_s1', 'self');
|
||||
$role2 = $this->createRoleWithScope('role_s2', 'self');
|
||||
$role3 = $this->createRoleWithScope('role_s3', 'self');
|
||||
|
||||
$user->roles()->attach([$role1->id, $role2->id, $role3->id]);
|
||||
|
||||
$this->assertSame('self', $user->getEffectiveScopeForPermission('test.scope.read'));
|
||||
}
|
||||
|
||||
/**
|
||||
* 권한 미보유 → null 반환 (기본값)
|
||||
*/
|
||||
public function test_no_permission_returns_null(): void
|
||||
{
|
||||
$user = User::factory()->create();
|
||||
$this->assertNull($user->getEffectiveScopeForPermission('test.scope.read'));
|
||||
}
|
||||
|
||||
private function createUserWithScope(?string $scopeType): User
|
||||
{
|
||||
$user = User::factory()->create();
|
||||
$role = $this->createRoleWithScope('role_' . ($scopeType ?? 'null'), $scopeType);
|
||||
$user->roles()->attach($role->id);
|
||||
return $user;
|
||||
}
|
||||
|
||||
private function createRoleWithScope(string $identifier, ?string $scopeType): Role
|
||||
{
|
||||
$role = Role::create([
|
||||
'identifier' => $identifier . '_' . uniqid(),
|
||||
'name' => ['ko' => $identifier, 'en' => $identifier],
|
||||
'is_active' => true,
|
||||
]);
|
||||
|
||||
$role->permissions()->attach($this->permission->id, [
|
||||
'scope_type' => $scopeType,
|
||||
]);
|
||||
|
||||
return $role;
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user