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,240 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\Unit\Helpers;
|
||||
|
||||
use App\Extension\Helpers\FilePermissionHelper;
|
||||
use Illuminate\Support\Facades\File;
|
||||
use Tests\TestCase;
|
||||
|
||||
/**
|
||||
* FilePermissionHelper 단위 테스트
|
||||
*
|
||||
* copyDirectory의 퍼미션 보존, excludes 처리, removeOrphans 동작을 검증합니다.
|
||||
*/
|
||||
class FilePermissionHelperTest extends TestCase
|
||||
{
|
||||
/**
|
||||
* 테스트에서 사용하는 임시 디렉토리 목록 (tearDown에서 정리)
|
||||
*
|
||||
* @var array<string>
|
||||
*/
|
||||
private array $tempDirs = [];
|
||||
|
||||
protected function tearDown(): void
|
||||
{
|
||||
foreach ($this->tempDirs as $dir) {
|
||||
if (File::isDirectory($dir)) {
|
||||
File::deleteDirectory($dir);
|
||||
}
|
||||
}
|
||||
|
||||
parent::tearDown();
|
||||
}
|
||||
|
||||
/**
|
||||
* 테스트용 임시 디렉토리를 생성합니다.
|
||||
*
|
||||
* @return string 생성된 디렉토리 경로
|
||||
*/
|
||||
private function createTempDir(): string
|
||||
{
|
||||
$dir = storage_path('test_fileperm_'.uniqid());
|
||||
File::ensureDirectoryExists($dir);
|
||||
$this->tempDirs[] = $dir;
|
||||
|
||||
return $dir;
|
||||
}
|
||||
|
||||
// ========================================================================
|
||||
// removeOrphans 기본 동작 (false) — 소스에 없는 파일 유지
|
||||
// ========================================================================
|
||||
|
||||
/**
|
||||
* removeOrphans 기본값(false)일 때 소스에 없는 대상 파일이 유지되는지 검증합니다.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function test_copy_directory_does_not_remove_orphans_by_default(): void
|
||||
{
|
||||
$source = $this->createTempDir();
|
||||
$dest = $this->createTempDir();
|
||||
|
||||
// 소스: file_a.txt만 존재
|
||||
File::put($source.DIRECTORY_SEPARATOR.'file_a.txt', 'source_a');
|
||||
|
||||
// 대상: file_a.txt + orphan.txt (소스에 없는 파일)
|
||||
File::put($dest.DIRECTORY_SEPARATOR.'file_a.txt', 'old_a');
|
||||
File::put($dest.DIRECTORY_SEPARATOR.'orphan.txt', 'orphan_content');
|
||||
|
||||
FilePermissionHelper::copyDirectory($source, $dest);
|
||||
|
||||
// file_a.txt는 소스 내용으로 덮어쓰기
|
||||
$this->assertEquals('source_a', File::get($dest.DIRECTORY_SEPARATOR.'file_a.txt'));
|
||||
|
||||
// orphan.txt는 유지 (기본 동작)
|
||||
$this->assertTrue(File::exists($dest.DIRECTORY_SEPARATOR.'orphan.txt'));
|
||||
$this->assertEquals('orphan_content', File::get($dest.DIRECTORY_SEPARATOR.'orphan.txt'));
|
||||
}
|
||||
|
||||
// ========================================================================
|
||||
// removeOrphans=true — 소스에 없는 파일 삭제
|
||||
// ========================================================================
|
||||
|
||||
/**
|
||||
* removeOrphans=true일 때 소스에 없는 파일이 삭제되는지 검증합니다.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function test_copy_directory_removes_orphans_when_enabled(): void
|
||||
{
|
||||
$source = $this->createTempDir();
|
||||
$dest = $this->createTempDir();
|
||||
|
||||
// 소스: file_a.txt만 존재
|
||||
File::put($source.DIRECTORY_SEPARATOR.'file_a.txt', 'source_a');
|
||||
|
||||
// 대상: file_a.txt + orphan.txt
|
||||
File::put($dest.DIRECTORY_SEPARATOR.'file_a.txt', 'old_a');
|
||||
File::put($dest.DIRECTORY_SEPARATOR.'orphan.txt', 'orphan_content');
|
||||
|
||||
FilePermissionHelper::copyDirectory($source, $dest, removeOrphans: true);
|
||||
|
||||
// file_a.txt는 소스 내용으로 덮어쓰기
|
||||
$this->assertEquals('source_a', File::get($dest.DIRECTORY_SEPARATOR.'file_a.txt'));
|
||||
|
||||
// orphan.txt는 삭제됨
|
||||
$this->assertFalse(File::exists($dest.DIRECTORY_SEPARATOR.'orphan.txt'));
|
||||
}
|
||||
|
||||
// ========================================================================
|
||||
// removeOrphans=true — 소스에 없는 디렉토리도 재귀 삭제
|
||||
// ========================================================================
|
||||
|
||||
/**
|
||||
* removeOrphans=true일 때 소스에 없는 디렉토리가 재귀 삭제되는지 검증합니다.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function test_copy_directory_removes_orphan_directories(): void
|
||||
{
|
||||
$source = $this->createTempDir();
|
||||
$dest = $this->createTempDir();
|
||||
|
||||
// 소스: subdir_a/file.txt만 존재
|
||||
File::ensureDirectoryExists($source.DIRECTORY_SEPARATOR.'subdir_a');
|
||||
File::put($source.DIRECTORY_SEPARATOR.'subdir_a'.DIRECTORY_SEPARATOR.'file.txt', 'content');
|
||||
|
||||
// 대상: subdir_a/ + orphan_dir/ (소스에 없는 디렉토리)
|
||||
File::ensureDirectoryExists($dest.DIRECTORY_SEPARATOR.'subdir_a');
|
||||
File::put($dest.DIRECTORY_SEPARATOR.'subdir_a'.DIRECTORY_SEPARATOR.'file.txt', 'old');
|
||||
File::ensureDirectoryExists($dest.DIRECTORY_SEPARATOR.'orphan_dir');
|
||||
File::put($dest.DIRECTORY_SEPARATOR.'orphan_dir'.DIRECTORY_SEPARATOR.'deep.txt', 'deep_content');
|
||||
|
||||
FilePermissionHelper::copyDirectory($source, $dest, removeOrphans: true);
|
||||
|
||||
// subdir_a는 유지되고 내용 덮어쓰기
|
||||
$this->assertTrue(File::isDirectory($dest.DIRECTORY_SEPARATOR.'subdir_a'));
|
||||
$this->assertEquals('content', File::get($dest.DIRECTORY_SEPARATOR.'subdir_a'.DIRECTORY_SEPARATOR.'file.txt'));
|
||||
|
||||
// orphan_dir는 재귀 삭제
|
||||
$this->assertFalse(File::isDirectory($dest.DIRECTORY_SEPARATOR.'orphan_dir'));
|
||||
}
|
||||
|
||||
// ========================================================================
|
||||
// removeOrphans=true — 하위 디렉토리 내 orphan 파일도 삭제
|
||||
// ========================================================================
|
||||
|
||||
/**
|
||||
* removeOrphans=true일 때 하위 디렉토리 내 소스에 없는 파일도 삭제되는지 검증합니다.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function test_copy_directory_removes_orphans_in_subdirectories(): void
|
||||
{
|
||||
$source = $this->createTempDir();
|
||||
$dest = $this->createTempDir();
|
||||
|
||||
// 소스: sub/keep.txt만 존재
|
||||
File::ensureDirectoryExists($source.DIRECTORY_SEPARATOR.'sub');
|
||||
File::put($source.DIRECTORY_SEPARATOR.'sub'.DIRECTORY_SEPARATOR.'keep.txt', 'keep');
|
||||
|
||||
// 대상: sub/keep.txt + sub/orphan.txt
|
||||
File::ensureDirectoryExists($dest.DIRECTORY_SEPARATOR.'sub');
|
||||
File::put($dest.DIRECTORY_SEPARATOR.'sub'.DIRECTORY_SEPARATOR.'keep.txt', 'old');
|
||||
File::put($dest.DIRECTORY_SEPARATOR.'sub'.DIRECTORY_SEPARATOR.'orphan.txt', 'orphan');
|
||||
|
||||
FilePermissionHelper::copyDirectory($source, $dest, removeOrphans: true);
|
||||
|
||||
// keep.txt는 덮어쓰기
|
||||
$this->assertEquals('keep', File::get($dest.DIRECTORY_SEPARATOR.'sub'.DIRECTORY_SEPARATOR.'keep.txt'));
|
||||
|
||||
// sub/orphan.txt는 삭제
|
||||
$this->assertFalse(File::exists($dest.DIRECTORY_SEPARATOR.'sub'.DIRECTORY_SEPARATOR.'orphan.txt'));
|
||||
}
|
||||
|
||||
// ========================================================================
|
||||
// removeOrphans=true + excludes — 제외 대상은 삭제하지 않음
|
||||
// ========================================================================
|
||||
|
||||
/**
|
||||
* removeOrphans=true이더라도 excludes 대상은 삭제하지 않는지 검증합니다.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function test_copy_directory_preserves_excluded_orphans(): void
|
||||
{
|
||||
$source = $this->createTempDir();
|
||||
$dest = $this->createTempDir();
|
||||
|
||||
// 소스: file_a.txt만 존재
|
||||
File::put($source.DIRECTORY_SEPARATOR.'file_a.txt', 'source_a');
|
||||
|
||||
// 대상: file_a.txt + vendor/ + node_modules/ (excludes 대상)
|
||||
File::put($dest.DIRECTORY_SEPARATOR.'file_a.txt', 'old_a');
|
||||
File::ensureDirectoryExists($dest.DIRECTORY_SEPARATOR.'vendor');
|
||||
File::put($dest.DIRECTORY_SEPARATOR.'vendor'.DIRECTORY_SEPARATOR.'autoload.php', 'vendor_content');
|
||||
File::ensureDirectoryExists($dest.DIRECTORY_SEPARATOR.'node_modules');
|
||||
File::put($dest.DIRECTORY_SEPARATOR.'node_modules'.DIRECTORY_SEPARATOR.'package.json', 'nm_content');
|
||||
|
||||
$excludes = ['vendor', 'node_modules'];
|
||||
|
||||
FilePermissionHelper::copyDirectory($source, $dest, excludes: $excludes, removeOrphans: true);
|
||||
|
||||
// excludes 대상은 삭제되지 않음
|
||||
$this->assertTrue(File::isDirectory($dest.DIRECTORY_SEPARATOR.'vendor'));
|
||||
$this->assertTrue(File::isDirectory($dest.DIRECTORY_SEPARATOR.'node_modules'));
|
||||
$this->assertTrue(File::exists($dest.DIRECTORY_SEPARATOR.'vendor'.DIRECTORY_SEPARATOR.'autoload.php'));
|
||||
}
|
||||
|
||||
// ========================================================================
|
||||
// 기존 파일 퍼미션 보존 확인
|
||||
// ========================================================================
|
||||
|
||||
/**
|
||||
* 기존 파일의 퍼미션이 보존되는지 검증합니다. (Linux/Mac에서만 의미있음)
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function test_copy_file_preserves_permissions_on_existing_files(): void
|
||||
{
|
||||
$source = $this->createTempDir();
|
||||
$dest = $this->createTempDir();
|
||||
|
||||
$srcFile = $source.DIRECTORY_SEPARATOR.'script.sh';
|
||||
$destFile = $dest.DIRECTORY_SEPARATOR.'script.sh';
|
||||
|
||||
File::put($srcFile, '#!/bin/bash\necho new');
|
||||
File::put($destFile, '#!/bin/bash\necho old');
|
||||
|
||||
// Windows에서는 chmod가 제한적이므로 기본 동작만 검증
|
||||
$originalPerms = fileperms($destFile);
|
||||
|
||||
FilePermissionHelper::copyFile($srcFile, $destFile);
|
||||
|
||||
// 내용은 소스로 교체
|
||||
$this->assertStringContainsString('new', File::get($destFile));
|
||||
|
||||
// 퍼미션 복원 시도 확인 (Windows에서는 값이 같을 수 있음)
|
||||
$this->assertEquals($originalPerms, fileperms($destFile));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,352 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\Unit\Helpers;
|
||||
|
||||
use App\Enums\PermissionType;
|
||||
use App\Enums\ScheduleType;
|
||||
use App\Enums\ScopeType;
|
||||
use App\Helpers\PermissionHelper;
|
||||
use App\Models\Menu;
|
||||
use App\Models\Permission;
|
||||
use App\Models\Role;
|
||||
use App\Models\Schedule;
|
||||
use App\Models\User;
|
||||
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||||
use Tests\TestCase;
|
||||
|
||||
class PermissionHelperScopeTest extends TestCase
|
||||
{
|
||||
use RefreshDatabase;
|
||||
|
||||
private Permission $permission;
|
||||
|
||||
private User $scopeUser;
|
||||
|
||||
private User $otherUser;
|
||||
|
||||
private User $sameRoleUser;
|
||||
|
||||
private Role $role;
|
||||
|
||||
protected function setUp(): void
|
||||
{
|
||||
parent::setUp();
|
||||
|
||||
// static 캐시 초기화
|
||||
$this->clearPermissionCache();
|
||||
|
||||
// 기본 권한 생성
|
||||
$this->permission = Permission::create([
|
||||
'identifier' => 'test.scope.read',
|
||||
'name' => ['ko' => '스코프 테스트 조회', 'en' => 'Scope Test Read'],
|
||||
'type' => PermissionType::Admin,
|
||||
'resource_route_key' => 'menu',
|
||||
'owner_key' => 'created_by',
|
||||
]);
|
||||
|
||||
// 사용자 생성
|
||||
$this->scopeUser = User::factory()->create();
|
||||
$this->otherUser = User::factory()->create();
|
||||
$this->sameRoleUser = User::factory()->create();
|
||||
|
||||
// 역할 생성 및 할당
|
||||
$this->role = Role::create([
|
||||
'identifier' => 'test_scope_role',
|
||||
'name' => ['ko' => '스코프 역할', 'en' => 'Scope Role'],
|
||||
'is_active' => true,
|
||||
]);
|
||||
$this->role->permissions()->attach($this->permission->id, ['scope_type' => ScopeType::Self]);
|
||||
$this->scopeUser->roles()->attach($this->role->id);
|
||||
$this->sameRoleUser->roles()->attach($this->role->id);
|
||||
}
|
||||
|
||||
protected function tearDown(): void
|
||||
{
|
||||
$this->clearPermissionCache();
|
||||
parent::tearDown();
|
||||
}
|
||||
|
||||
// ========================================================================
|
||||
// applyPermissionScope — 코어: Menu (owner_key='created_by')
|
||||
// ========================================================================
|
||||
|
||||
/**
|
||||
* Menu — scope=null → 전체 메뉴 조회 (쿼리 변경 없음)
|
||||
*/
|
||||
public function test_apply_scope_null_returns_all_menus(): void
|
||||
{
|
||||
$this->setScopeType(null);
|
||||
|
||||
$this->createMenu(['created_by' => $this->scopeUser->id]);
|
||||
$this->createMenu(['created_by' => $this->otherUser->id]);
|
||||
$this->createMenu(['created_by' => $this->sameRoleUser->id]);
|
||||
|
||||
$query = Menu::query();
|
||||
PermissionHelper::applyPermissionScope($query, 'test.scope.read', $this->scopeUser);
|
||||
|
||||
$this->assertSame(3, $query->count());
|
||||
}
|
||||
|
||||
/**
|
||||
* Menu — scope=self → 자기가 만든 메뉴만 조회
|
||||
*/
|
||||
public function test_apply_scope_self_filters_own_menus(): void
|
||||
{
|
||||
$this->createMenu(['created_by' => $this->scopeUser->id]);
|
||||
$this->createMenu(['created_by' => $this->otherUser->id]);
|
||||
|
||||
$query = Menu::query();
|
||||
PermissionHelper::applyPermissionScope($query, 'test.scope.read', $this->scopeUser);
|
||||
|
||||
$this->assertSame(1, $query->count());
|
||||
}
|
||||
|
||||
/**
|
||||
* Menu — scope=role → 동일 역할 사용자가 만든 메뉴만 조회
|
||||
*/
|
||||
public function test_apply_scope_role_filters_same_role_menus(): void
|
||||
{
|
||||
$this->setScopeType(ScopeType::Role);
|
||||
|
||||
$this->createMenu(['created_by' => $this->scopeUser->id]);
|
||||
$this->createMenu(['created_by' => $this->sameRoleUser->id]);
|
||||
$this->createMenu(['created_by' => $this->otherUser->id]);
|
||||
|
||||
$query = Menu::query();
|
||||
PermissionHelper::applyPermissionScope($query, 'test.scope.read', $this->scopeUser);
|
||||
|
||||
$this->assertSame(2, $query->count());
|
||||
}
|
||||
|
||||
// ========================================================================
|
||||
// applyPermissionScope — 코어: User (owner_key='id')
|
||||
// ========================================================================
|
||||
|
||||
/**
|
||||
* User — scope=null → 전체 사용자 조회
|
||||
*/
|
||||
public function test_apply_scope_null_returns_all_users(): void
|
||||
{
|
||||
$userPerm = $this->createPermissionWithScope('test.users.read', 'user', 'id', null);
|
||||
|
||||
$query = User::query();
|
||||
PermissionHelper::applyPermissionScope($query, 'test.users.read', $this->scopeUser);
|
||||
|
||||
// setUp에서 3명 + 생성 안 해도 3명은 존재
|
||||
$this->assertSame(User::count(), $query->count());
|
||||
}
|
||||
|
||||
/**
|
||||
* User — scope=self → 자기 자신만 조회 (WHERE id = user_id)
|
||||
*/
|
||||
public function test_apply_scope_self_filters_own_user(): void
|
||||
{
|
||||
$this->createPermissionWithScope('test.users.read', 'user', 'id', ScopeType::Self);
|
||||
|
||||
$query = User::query();
|
||||
PermissionHelper::applyPermissionScope($query, 'test.users.read', $this->scopeUser);
|
||||
|
||||
$this->assertSame(1, $query->count());
|
||||
$this->assertEquals($this->scopeUser->id, $query->first()->id);
|
||||
}
|
||||
|
||||
/**
|
||||
* User — scope=role → 동일 역할 사용자만 조회
|
||||
*/
|
||||
public function test_apply_scope_role_filters_same_role_users(): void
|
||||
{
|
||||
$this->createPermissionWithScope('test.users.read', 'user', 'id', ScopeType::Role);
|
||||
|
||||
$query = User::query();
|
||||
PermissionHelper::applyPermissionScope($query, 'test.users.read', $this->scopeUser);
|
||||
|
||||
// scopeUser + sameRoleUser = 2명
|
||||
$this->assertSame(2, $query->count());
|
||||
$userIds = $query->pluck('id')->toArray();
|
||||
$this->assertContains($this->scopeUser->id, $userIds);
|
||||
$this->assertContains($this->sameRoleUser->id, $userIds);
|
||||
}
|
||||
|
||||
// ========================================================================
|
||||
// applyPermissionScope — 코어: Schedule (owner_key='created_by')
|
||||
// ========================================================================
|
||||
|
||||
/**
|
||||
* Schedule — scope=null → 전체 스케줄 조회
|
||||
*/
|
||||
public function test_apply_scope_null_returns_all_schedules(): void
|
||||
{
|
||||
$this->createPermissionWithScope('test.schedules.read', 'schedule', 'created_by', null);
|
||||
|
||||
$this->createSchedule(['created_by' => $this->scopeUser->id]);
|
||||
$this->createSchedule(['created_by' => $this->otherUser->id]);
|
||||
|
||||
$query = Schedule::query();
|
||||
PermissionHelper::applyPermissionScope($query, 'test.schedules.read', $this->scopeUser);
|
||||
|
||||
$this->assertSame(2, $query->count());
|
||||
}
|
||||
|
||||
/**
|
||||
* Schedule — scope=self → 자기가 만든 스케줄만 조회
|
||||
*/
|
||||
public function test_apply_scope_self_filters_own_schedules(): void
|
||||
{
|
||||
$this->createPermissionWithScope('test.schedules.read', 'schedule', 'created_by', ScopeType::Self);
|
||||
|
||||
$this->createSchedule(['created_by' => $this->scopeUser->id]);
|
||||
$this->createSchedule(['created_by' => $this->otherUser->id]);
|
||||
|
||||
$query = Schedule::query();
|
||||
PermissionHelper::applyPermissionScope($query, 'test.schedules.read', $this->scopeUser);
|
||||
|
||||
$this->assertSame(1, $query->count());
|
||||
}
|
||||
|
||||
/**
|
||||
* Schedule — scope=role → 동일 역할 사용자가 만든 스케줄만 조회
|
||||
*/
|
||||
public function test_apply_scope_role_filters_same_role_schedules(): void
|
||||
{
|
||||
$this->createPermissionWithScope('test.schedules.read', 'schedule', 'created_by', ScopeType::Role);
|
||||
|
||||
$this->createSchedule(['created_by' => $this->scopeUser->id]);
|
||||
$this->createSchedule(['created_by' => $this->sameRoleUser->id]);
|
||||
$this->createSchedule(['created_by' => $this->otherUser->id]);
|
||||
|
||||
$query = Schedule::query();
|
||||
PermissionHelper::applyPermissionScope($query, 'test.schedules.read', $this->scopeUser);
|
||||
|
||||
$this->assertSame(2, $query->count());
|
||||
}
|
||||
|
||||
// ========================================================================
|
||||
// applyPermissionScope — owner_key=null 또는 비인증 사용자
|
||||
// ========================================================================
|
||||
|
||||
/**
|
||||
* owner_key=null → 쿼리 변경 없음 (스코프 체크 스킵)
|
||||
*/
|
||||
public function test_apply_scope_no_owner_key_does_not_modify_query(): void
|
||||
{
|
||||
Permission::create([
|
||||
'identifier' => 'test.system.read',
|
||||
'name' => ['ko' => '시스템 조회', 'en' => 'System Read'],
|
||||
'type' => PermissionType::Admin,
|
||||
'resource_route_key' => null,
|
||||
'owner_key' => null,
|
||||
]);
|
||||
|
||||
$this->createMenu(['created_by' => $this->scopeUser->id]);
|
||||
$this->createMenu(['created_by' => $this->otherUser->id]);
|
||||
|
||||
$query = Menu::query();
|
||||
PermissionHelper::applyPermissionScope($query, 'test.system.read', $this->scopeUser);
|
||||
|
||||
$this->assertSame(2, $query->count());
|
||||
}
|
||||
|
||||
/**
|
||||
* 비인증 사용자 → 쿼리 변경 없음
|
||||
*/
|
||||
public function test_apply_scope_unauthenticated_does_not_modify_query(): void
|
||||
{
|
||||
$this->createMenu(['created_by' => $this->scopeUser->id]);
|
||||
|
||||
$query = Menu::query();
|
||||
PermissionHelper::applyPermissionScope($query, 'test.scope.read', null);
|
||||
|
||||
$this->assertSame(1, $query->count());
|
||||
}
|
||||
|
||||
// ========================================================================
|
||||
// 헬퍼 메서드
|
||||
// ========================================================================
|
||||
|
||||
/**
|
||||
* 테스트용 Menu 생성 헬퍼
|
||||
*
|
||||
* @param array $overrides 오버라이드할 필드
|
||||
* @return Menu 생성된 메뉴
|
||||
*/
|
||||
private function createMenu(array $overrides = []): Menu
|
||||
{
|
||||
return Menu::create(array_merge([
|
||||
'name' => ['ko' => '테스트 메뉴', 'en' => 'Test Menu'],
|
||||
'slug' => 'test-menu-'.uniqid(),
|
||||
'url' => '/admin/test',
|
||||
'icon' => 'fas fa-cog',
|
||||
'order' => 1,
|
||||
'is_active' => true,
|
||||
], $overrides));
|
||||
}
|
||||
|
||||
/**
|
||||
* 테스트용 Schedule 생성 헬퍼
|
||||
*
|
||||
* @param array $overrides 오버라이드할 필드
|
||||
* @return Schedule 생성된 스케줄
|
||||
*/
|
||||
private function createSchedule(array $overrides = []): Schedule
|
||||
{
|
||||
return Schedule::create(array_merge([
|
||||
'name' => 'Test Schedule '.uniqid(),
|
||||
'type' => ScheduleType::Artisan,
|
||||
'command' => 'test:command',
|
||||
'expression' => '* * * * *',
|
||||
'frequency' => 'everyMinute',
|
||||
], $overrides));
|
||||
}
|
||||
|
||||
/**
|
||||
* 기본 permission의 scope_type 변경
|
||||
*
|
||||
* @param ScopeType|null $scopeType scope_type 값
|
||||
* @return void
|
||||
*/
|
||||
private function setScopeType(?ScopeType $scopeType): void
|
||||
{
|
||||
$this->role->permissions()->syncWithoutDetaching([
|
||||
$this->permission->id => ['scope_type' => $scopeType],
|
||||
]);
|
||||
$this->clearPermissionCache();
|
||||
}
|
||||
|
||||
/**
|
||||
* 별도 권한 생성 및 역할에 할당하는 헬퍼
|
||||
*
|
||||
* @param string $identifier 권한 식별자
|
||||
* @param string|null $routeKey resource_route_key
|
||||
* @param string|null $ownerKey owner_key
|
||||
* @param ScopeType|null $scopeType scope_type
|
||||
* @return Permission 생성된 권한
|
||||
*/
|
||||
private function createPermissionWithScope(string $identifier, ?string $routeKey, ?string $ownerKey, ?ScopeType $scopeType): Permission
|
||||
{
|
||||
$perm = Permission::create([
|
||||
'identifier' => $identifier,
|
||||
'name' => ['ko' => $identifier, 'en' => $identifier],
|
||||
'type' => PermissionType::Admin,
|
||||
'resource_route_key' => $routeKey,
|
||||
'owner_key' => $ownerKey,
|
||||
]);
|
||||
|
||||
$this->role->permissions()->attach($perm->id, ['scope_type' => $scopeType]);
|
||||
$this->clearPermissionCache();
|
||||
|
||||
return $perm;
|
||||
}
|
||||
|
||||
/**
|
||||
* PermissionHelper static 캐시 초기화
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
private function clearPermissionCache(): void
|
||||
{
|
||||
$reflection = new \ReflectionClass(PermissionHelper::class);
|
||||
$prop = $reflection->getProperty('permissionCache');
|
||||
$prop->setAccessible(true);
|
||||
$prop->setValue(null, []);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,274 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\Unit\Helpers;
|
||||
|
||||
use App\Enums\PermissionType;
|
||||
use App\Helpers\PermissionHelper;
|
||||
use App\Models\Menu;
|
||||
use App\Models\Permission;
|
||||
use App\Models\Role;
|
||||
use App\Models\User;
|
||||
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||||
use Illuminate\Support\Facades\Auth;
|
||||
use Tests\TestCase;
|
||||
|
||||
class PermissionHelperTest extends TestCase
|
||||
{
|
||||
use RefreshDatabase;
|
||||
|
||||
private Permission $permission;
|
||||
private Permission $noScopePermission;
|
||||
private User $ownerUser;
|
||||
private User $otherUser;
|
||||
private User $sharedRoleUser;
|
||||
|
||||
protected function setUp(): void
|
||||
{
|
||||
parent::setUp();
|
||||
|
||||
// static 캐시 초기화
|
||||
$this->clearPermissionCache();
|
||||
|
||||
// 스코프가 있는 권한 (메뉴 관리)
|
||||
$this->permission = Permission::create([
|
||||
'identifier' => 'test.menus.update',
|
||||
'name' => ['ko' => '메뉴 수정', 'en' => 'Update Menu'],
|
||||
'type' => PermissionType::Admin,
|
||||
'resource_route_key' => 'menu',
|
||||
'owner_key' => 'created_by',
|
||||
]);
|
||||
|
||||
// 스코프가 없는 권한 (시스템 리소스)
|
||||
$this->noScopePermission = Permission::create([
|
||||
'identifier' => 'test.settings.read',
|
||||
'name' => ['ko' => '설정 조회', 'en' => 'Read Settings'],
|
||||
'type' => PermissionType::Admin,
|
||||
'resource_route_key' => null,
|
||||
'owner_key' => null,
|
||||
]);
|
||||
|
||||
// 사용자 생성
|
||||
$this->ownerUser = User::factory()->create();
|
||||
$this->otherUser = User::factory()->create();
|
||||
$this->sharedRoleUser = User::factory()->create();
|
||||
|
||||
// 역할 생성 및 할당
|
||||
$role = Role::create([
|
||||
'identifier' => 'test_manager',
|
||||
'name' => ['ko' => '매니저', 'en' => 'Manager'],
|
||||
'is_active' => true,
|
||||
]);
|
||||
$role->permissions()->attach($this->permission->id, ['scope_type' => 'self']);
|
||||
$role->permissions()->attach($this->noScopePermission->id, ['scope_type' => null]);
|
||||
$this->ownerUser->roles()->attach($role->id);
|
||||
$this->sharedRoleUser->roles()->attach($role->id);
|
||||
}
|
||||
|
||||
protected function tearDown(): void
|
||||
{
|
||||
$this->clearPermissionCache();
|
||||
parent::tearDown();
|
||||
}
|
||||
|
||||
// ========================================================================
|
||||
// checkScopeAccess 테스트
|
||||
// ========================================================================
|
||||
|
||||
/**
|
||||
* scope=null, 타인 리소스 → true (전체 접근)
|
||||
*/
|
||||
public function test_check_scope_access_null_scope_allows_all(): void
|
||||
{
|
||||
// null scope 역할 생성
|
||||
$role = Role::create([
|
||||
'identifier' => 'test_admin_full',
|
||||
'name' => ['ko' => '전체 접근', 'en' => 'Full Access'],
|
||||
'is_active' => true,
|
||||
]);
|
||||
$role->permissions()->attach($this->permission->id, ['scope_type' => null]);
|
||||
$user = User::factory()->create();
|
||||
$user->roles()->attach($role->id);
|
||||
|
||||
$menu = $this->createMenu(['created_by' => $this->otherUser->id]);
|
||||
|
||||
$this->assertTrue(PermissionHelper::checkScopeAccess($menu, 'test.menus.update', $user));
|
||||
}
|
||||
|
||||
/**
|
||||
* scope='self', 자기 리소스 → true
|
||||
*/
|
||||
public function test_check_scope_access_self_allows_own_resource(): void
|
||||
{
|
||||
$menu = $this->createMenu(['created_by' => $this->ownerUser->id]);
|
||||
|
||||
$this->assertTrue(PermissionHelper::checkScopeAccess($menu, 'test.menus.update', $this->ownerUser));
|
||||
}
|
||||
|
||||
/**
|
||||
* scope='self', 타인 리소스 → false
|
||||
*/
|
||||
public function test_check_scope_access_self_denies_other_resource(): void
|
||||
{
|
||||
$menu = $this->createMenu(['created_by' => $this->otherUser->id]);
|
||||
|
||||
$this->assertFalse(PermissionHelper::checkScopeAccess($menu, 'test.menus.update', $this->ownerUser));
|
||||
}
|
||||
|
||||
/**
|
||||
* scope='role', 동일 역할 사용자의 리소스 → true
|
||||
*/
|
||||
public function test_check_scope_access_role_allows_shared_role_resource(): void
|
||||
{
|
||||
// role scope 역할
|
||||
$role = Role::create([
|
||||
'identifier' => 'test_role_scope',
|
||||
'name' => ['ko' => '역할 스코프', 'en' => 'Role Scope'],
|
||||
'is_active' => true,
|
||||
]);
|
||||
$role->permissions()->attach($this->permission->id, ['scope_type' => 'role']);
|
||||
|
||||
$userA = User::factory()->create();
|
||||
$userB = User::factory()->create();
|
||||
$userA->roles()->attach($role->id);
|
||||
$userB->roles()->attach($role->id);
|
||||
|
||||
$menu = $this->createMenu(['created_by' => $userB->id]);
|
||||
|
||||
$this->assertTrue(PermissionHelper::checkScopeAccess($menu, 'test.menus.update', $userA));
|
||||
}
|
||||
|
||||
/**
|
||||
* scope='role', 다른 역할 사용자의 리소스 → false
|
||||
*/
|
||||
public function test_check_scope_access_role_denies_different_role_resource(): void
|
||||
{
|
||||
$role = Role::create([
|
||||
'identifier' => 'test_role_scope2',
|
||||
'name' => ['ko' => '역할 스코프2', 'en' => 'Role Scope2'],
|
||||
'is_active' => true,
|
||||
]);
|
||||
$role->permissions()->attach($this->permission->id, ['scope_type' => 'role']);
|
||||
|
||||
$userA = User::factory()->create();
|
||||
$userA->roles()->attach($role->id);
|
||||
|
||||
// otherUser has no role
|
||||
$menu = $this->createMenu(['created_by' => $this->otherUser->id]);
|
||||
|
||||
$this->assertFalse(PermissionHelper::checkScopeAccess($menu, 'test.menus.update', $userA));
|
||||
}
|
||||
|
||||
/**
|
||||
* resource_route_key=null → true (스코프 체크 스킵)
|
||||
*/
|
||||
public function test_check_scope_access_skips_when_no_resource_route_key(): void
|
||||
{
|
||||
$menu = $this->createMenu(['created_by' => $this->otherUser->id]);
|
||||
|
||||
$this->assertTrue(PermissionHelper::checkScopeAccess($menu, 'test.settings.read', $this->ownerUser));
|
||||
}
|
||||
|
||||
/**
|
||||
* 비인증 사용자 → true (스코프 체크 스킵)
|
||||
*/
|
||||
public function test_check_scope_access_skips_for_unauthenticated_user(): void
|
||||
{
|
||||
$menu = $this->createMenu(['created_by' => $this->otherUser->id]);
|
||||
|
||||
$this->assertTrue(PermissionHelper::checkScopeAccess($menu, 'test.menus.update', null));
|
||||
}
|
||||
|
||||
// ========================================================================
|
||||
// applyPermissionScope 테스트
|
||||
// ========================================================================
|
||||
|
||||
/**
|
||||
* scope=null → 쿼리 변경 없음
|
||||
*/
|
||||
public function test_apply_scope_null_does_not_modify_query(): void
|
||||
{
|
||||
$role = Role::create([
|
||||
'identifier' => 'test_full_access',
|
||||
'name' => ['ko' => '전체', 'en' => 'Full'],
|
||||
'is_active' => true,
|
||||
]);
|
||||
$role->permissions()->attach($this->permission->id, ['scope_type' => null]);
|
||||
$user = User::factory()->create();
|
||||
$user->roles()->attach($role->id);
|
||||
|
||||
// 메뉴 3개 생성
|
||||
$this->createMenu(['created_by' => $user->id]);
|
||||
$this->createMenu(['created_by' => $this->otherUser->id]);
|
||||
$this->createMenu(['created_by' => $this->ownerUser->id]);
|
||||
|
||||
$query = Menu::query();
|
||||
PermissionHelper::applyPermissionScope($query, 'test.menus.update', $user);
|
||||
|
||||
$this->assertSame(3, $query->count());
|
||||
}
|
||||
|
||||
/**
|
||||
* scope='self' → WHERE owner_key = user_id 추가
|
||||
*/
|
||||
public function test_apply_scope_self_filters_own_resources(): void
|
||||
{
|
||||
$this->createMenu(['created_by' => $this->ownerUser->id]);
|
||||
$this->createMenu(['created_by' => $this->otherUser->id]);
|
||||
|
||||
$query = Menu::query();
|
||||
PermissionHelper::applyPermissionScope($query, 'test.menus.update', $this->ownerUser);
|
||||
|
||||
$this->assertSame(1, $query->count());
|
||||
}
|
||||
|
||||
/**
|
||||
* owner_key=null → 쿼리 변경 없음
|
||||
*/
|
||||
public function test_apply_scope_no_owner_key_does_not_modify_query(): void
|
||||
{
|
||||
$this->createMenu(['created_by' => $this->ownerUser->id]);
|
||||
$this->createMenu(['created_by' => $this->otherUser->id]);
|
||||
|
||||
$query = Menu::query();
|
||||
PermissionHelper::applyPermissionScope($query, 'test.settings.read', $this->ownerUser);
|
||||
|
||||
$this->assertSame(2, $query->count());
|
||||
}
|
||||
|
||||
/**
|
||||
* 비인증 사용자 → 쿼리 변경 없음
|
||||
*/
|
||||
public function test_apply_scope_unauthenticated_does_not_modify_query(): void
|
||||
{
|
||||
$this->createMenu(['created_by' => $this->ownerUser->id]);
|
||||
|
||||
$query = Menu::query();
|
||||
PermissionHelper::applyPermissionScope($query, 'test.menus.update', null);
|
||||
|
||||
$this->assertSame(1, $query->count());
|
||||
}
|
||||
|
||||
// ========================================================================
|
||||
// 헬퍼 메서드
|
||||
// ========================================================================
|
||||
|
||||
private function createMenu(array $overrides = []): Menu
|
||||
{
|
||||
return Menu::create(array_merge([
|
||||
'name' => ['ko' => '테스트 메뉴', 'en' => 'Test Menu'],
|
||||
'slug' => 'test-menu-' . uniqid(),
|
||||
'url' => '/admin/test',
|
||||
'icon' => 'fas fa-cog',
|
||||
'order' => 1,
|
||||
'is_active' => true,
|
||||
], $overrides));
|
||||
}
|
||||
|
||||
private function clearPermissionCache(): void
|
||||
{
|
||||
$reflection = new \ReflectionClass(PermissionHelper::class);
|
||||
$prop = $reflection->getProperty('permissionCache');
|
||||
$prop->setAccessible(true);
|
||||
$prop->setValue(null, []);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,367 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\Unit\Helpers;
|
||||
|
||||
use App\Helpers\TimezoneHelper;
|
||||
use Carbon\Carbon;
|
||||
use Illuminate\Support\Facades\App;
|
||||
use Tests\TestCase;
|
||||
|
||||
/**
|
||||
* TimezoneHelper 단위 테스트
|
||||
*/
|
||||
class TimezoneHelperTest extends TestCase
|
||||
{
|
||||
protected function setUp(): void
|
||||
{
|
||||
parent::setUp();
|
||||
|
||||
// 테스트 시간 고정 (UTC 기준 2025-01-15 00:00:00)
|
||||
Carbon::setTestNow(Carbon::parse('2025-01-15 00:00:00', 'UTC'));
|
||||
}
|
||||
|
||||
protected function tearDown(): void
|
||||
{
|
||||
Carbon::setTestNow();
|
||||
parent::tearDown();
|
||||
}
|
||||
|
||||
/**
|
||||
* user_timezone 바인딩이 없을 때 config 기본값을 반환하는지 테스트합니다.
|
||||
*/
|
||||
public function test_get_user_timezone_returns_config_default(): void
|
||||
{
|
||||
App::forgetInstance('user_timezone');
|
||||
|
||||
$this->assertEquals(
|
||||
config('app.default_user_timezone', 'Asia/Seoul'),
|
||||
TimezoneHelper::getUserTimezone()
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* user_timezone 바인딩이 있을 때 해당 값을 반환하는지 테스트합니다.
|
||||
*/
|
||||
public function test_get_user_timezone_returns_bound_value(): void
|
||||
{
|
||||
App::instance('user_timezone', 'America/New_York');
|
||||
|
||||
$this->assertEquals('America/New_York', TimezoneHelper::getUserTimezone());
|
||||
}
|
||||
|
||||
/**
|
||||
* null datetime에 대해 null을 반환하는지 테스트합니다.
|
||||
*/
|
||||
public function test_to_user_timezone_returns_null_for_null(): void
|
||||
{
|
||||
$this->assertNull(TimezoneHelper::toUserTimezone(null));
|
||||
}
|
||||
|
||||
/**
|
||||
* ISO8601 형식으로 사용자 타임존 변환이 올바른지 테스트합니다.
|
||||
*/
|
||||
public function test_to_user_timezone_converts_to_iso8601(): void
|
||||
{
|
||||
App::instance('user_timezone', 'Asia/Seoul');
|
||||
|
||||
$utcDatetime = Carbon::parse('2025-01-15 00:00:00', 'UTC');
|
||||
$result = TimezoneHelper::toUserTimezone($utcDatetime);
|
||||
|
||||
$this->assertNotNull($result);
|
||||
|
||||
// ISO8601 파싱 후 +09:00 확인
|
||||
$parsed = Carbon::parse($result);
|
||||
$this->assertEquals('+09:00', $parsed->timezone->getName());
|
||||
$this->assertEquals(9, $parsed->hour);
|
||||
}
|
||||
|
||||
/**
|
||||
* Y-m-d H:i:s 형식 변환이 올바른지 테스트합니다.
|
||||
*/
|
||||
public function test_to_user_date_time_string_converts_correctly(): void
|
||||
{
|
||||
App::instance('user_timezone', 'Asia/Seoul');
|
||||
|
||||
$utcDatetime = Carbon::parse('2025-01-15 00:00:00', 'UTC');
|
||||
$result = TimezoneHelper::toUserDateTimeString($utcDatetime);
|
||||
|
||||
$this->assertEquals('2025-01-15 09:00:00', $result);
|
||||
}
|
||||
|
||||
/**
|
||||
* null datetime에 대해 toUserDateTimeString이 null을 반환하는지 테스트합니다.
|
||||
*/
|
||||
public function test_to_user_date_time_string_returns_null_for_null(): void
|
||||
{
|
||||
$this->assertNull(TimezoneHelper::toUserDateTimeString(null));
|
||||
}
|
||||
|
||||
/**
|
||||
* Y-m-d 형식 변환이 올바른지 테스트합니다.
|
||||
*/
|
||||
public function test_to_user_date_string_converts_correctly(): void
|
||||
{
|
||||
App::instance('user_timezone', 'Asia/Seoul');
|
||||
|
||||
// UTC 23:00 → Asia/Seoul 다음날 08:00
|
||||
$utcDatetime = Carbon::parse('2025-01-15 23:00:00', 'UTC');
|
||||
$result = TimezoneHelper::toUserDateString($utcDatetime);
|
||||
|
||||
// UTC 23시 + 9시간 = 다음날 08시
|
||||
$this->assertEquals('2025-01-16', $result);
|
||||
}
|
||||
|
||||
/**
|
||||
* null datetime에 대해 toUserDateString이 null을 반환하는지 테스트합니다.
|
||||
*/
|
||||
public function test_to_user_date_string_returns_null_for_null(): void
|
||||
{
|
||||
$this->assertNull(TimezoneHelper::toUserDateString(null));
|
||||
}
|
||||
|
||||
/**
|
||||
* toUserCarbon이 사용자 타임존의 Carbon 인스턴스를 반환하는지 테스트합니다.
|
||||
*/
|
||||
public function test_to_user_carbon_returns_carbon_with_user_timezone(): void
|
||||
{
|
||||
App::instance('user_timezone', 'America/New_York');
|
||||
|
||||
$utcDatetime = Carbon::parse('2025-01-15 12:00:00', 'UTC');
|
||||
$result = TimezoneHelper::toUserCarbon($utcDatetime);
|
||||
|
||||
$this->assertInstanceOf(Carbon::class, $result);
|
||||
$this->assertEquals('America/New_York', $result->timezone->getName());
|
||||
$this->assertEquals(7, $result->hour); // UTC 12:00 → EST 07:00
|
||||
}
|
||||
|
||||
/**
|
||||
* null datetime에 대해 toUserCarbon이 null을 반환하는지 테스트합니다.
|
||||
*/
|
||||
public function test_to_user_carbon_returns_null_for_null(): void
|
||||
{
|
||||
$this->assertNull(TimezoneHelper::toUserCarbon(null));
|
||||
}
|
||||
|
||||
/**
|
||||
* toUserCarbon이 원본 datetime을 변경하지 않는지 테스트합니다 (immutability).
|
||||
*/
|
||||
public function test_to_user_carbon_does_not_mutate_original(): void
|
||||
{
|
||||
App::instance('user_timezone', 'Asia/Seoul');
|
||||
|
||||
$original = Carbon::parse('2025-01-15 00:00:00', 'UTC');
|
||||
TimezoneHelper::toUserCarbon($original);
|
||||
|
||||
$this->assertEquals('UTC', $original->timezone->getName());
|
||||
$this->assertEquals(0, $original->hour);
|
||||
}
|
||||
|
||||
/**
|
||||
* toUserTimezone도 원본 datetime을 변경하지 않는지 테스트합니다 (immutability).
|
||||
*/
|
||||
public function test_to_user_timezone_does_not_mutate_original(): void
|
||||
{
|
||||
App::instance('user_timezone', 'Asia/Seoul');
|
||||
|
||||
$original = Carbon::parse('2025-01-15 00:00:00', 'UTC');
|
||||
TimezoneHelper::toUserTimezone($original);
|
||||
|
||||
$this->assertEquals('UTC', $original->timezone->getName());
|
||||
$this->assertEquals(0, $original->hour);
|
||||
}
|
||||
|
||||
/**
|
||||
* toUserCarbon에서 diffForHumans를 사용할 수 있는지 테스트합니다.
|
||||
*/
|
||||
public function test_to_user_carbon_supports_diff_for_humans(): void
|
||||
{
|
||||
App::instance('user_timezone', 'Asia/Seoul');
|
||||
|
||||
$utcDatetime = Carbon::now('UTC')->subHours(2);
|
||||
$result = TimezoneHelper::toUserCarbon($utcDatetime);
|
||||
|
||||
$this->assertNotNull($result);
|
||||
$this->assertIsString($result->diffForHumans());
|
||||
}
|
||||
|
||||
// ==================== getSiteTimezone ====================
|
||||
|
||||
/**
|
||||
* getSiteTimezone이 config 값을 반환하는지 테스트합니다.
|
||||
*/
|
||||
public function test_get_site_timezone_returns_config_value(): void
|
||||
{
|
||||
$expected = config('app.default_user_timezone');
|
||||
|
||||
$this->assertEquals($expected, TimezoneHelper::getSiteTimezone());
|
||||
}
|
||||
|
||||
// ==================== toSiteDateString ====================
|
||||
|
||||
/**
|
||||
* toSiteDateString이 사이트 타임존 기준 Y-m-d를 반환하는지 테스트합니다.
|
||||
*/
|
||||
public function test_to_site_date_string_converts_correctly(): void
|
||||
{
|
||||
config(['app.default_user_timezone' => 'Asia/Seoul']);
|
||||
|
||||
// UTC 15:00 → KST 다음날 00:00
|
||||
$utcDatetime = Carbon::parse('2025-01-15 15:00:00', 'UTC');
|
||||
$result = TimezoneHelper::toSiteDateString($utcDatetime);
|
||||
|
||||
$this->assertEquals('2025-01-16', $result);
|
||||
}
|
||||
|
||||
/**
|
||||
* toSiteDateString이 null에 대해 null을 반환하는지 테스트합니다.
|
||||
*/
|
||||
public function test_to_site_date_string_returns_null_for_null(): void
|
||||
{
|
||||
$this->assertNull(TimezoneHelper::toSiteDateString(null));
|
||||
}
|
||||
|
||||
// ==================== fromSiteDateStartOfDay ====================
|
||||
|
||||
/**
|
||||
* fromSiteDateStartOfDay가 사이트 타임존 00:00:00을 UTC로 변환하는지 테스트합니다.
|
||||
*/
|
||||
public function test_from_site_date_start_of_day_converts_to_utc(): void
|
||||
{
|
||||
config(['app.default_user_timezone' => 'Asia/Seoul']);
|
||||
|
||||
$result = TimezoneHelper::fromSiteDateStartOfDay('2025-03-15');
|
||||
|
||||
// 2025-03-15 00:00:00 KST = 2025-03-14 15:00:00 UTC
|
||||
$this->assertInstanceOf(Carbon::class, $result);
|
||||
$this->assertEquals('2025-03-14 15:00:00', $result->format('Y-m-d H:i:s'));
|
||||
$this->assertEquals('UTC', $result->timezone->getName());
|
||||
}
|
||||
|
||||
/**
|
||||
* fromSiteDateStartOfDay가 null에 대해 null을 반환하는지 테스트합니다.
|
||||
*/
|
||||
public function test_from_site_date_start_of_day_returns_null_for_null(): void
|
||||
{
|
||||
$this->assertNull(TimezoneHelper::fromSiteDateStartOfDay(null));
|
||||
}
|
||||
|
||||
// ==================== fromSiteDateEndOfDay ====================
|
||||
|
||||
/**
|
||||
* fromSiteDateEndOfDay가 사이트 타임존 23:59:59를 UTC로 변환하는지 테스트합니다.
|
||||
*/
|
||||
public function test_from_site_date_end_of_day_converts_to_utc(): void
|
||||
{
|
||||
config(['app.default_user_timezone' => 'Asia/Seoul']);
|
||||
|
||||
$result = TimezoneHelper::fromSiteDateEndOfDay('2025-03-20');
|
||||
|
||||
// 2025-03-20 23:59:59 KST = 2025-03-20 14:59:59 UTC
|
||||
$this->assertInstanceOf(Carbon::class, $result);
|
||||
$this->assertEquals('2025-03-20 14:59:59', $result->format('Y-m-d H:i:s'));
|
||||
$this->assertEquals('UTC', $result->timezone->getName());
|
||||
}
|
||||
|
||||
/**
|
||||
* fromSiteDateEndOfDay가 null에 대해 null을 반환하는지 테스트합니다.
|
||||
*/
|
||||
public function test_from_site_date_end_of_day_returns_null_for_null(): void
|
||||
{
|
||||
$this->assertNull(TimezoneHelper::fromSiteDateEndOfDay(null));
|
||||
}
|
||||
|
||||
// ==================== 왕복 변환 테스트 ====================
|
||||
|
||||
/**
|
||||
* fromSiteDateStartOfDay → toSiteDateString 왕복 변환이 원래 날짜를 유지하는지 테스트합니다.
|
||||
*/
|
||||
public function test_round_trip_start_of_day_preserves_date(): void
|
||||
{
|
||||
config(['app.default_user_timezone' => 'Asia/Seoul']);
|
||||
|
||||
$originalDate = '2025-06-15';
|
||||
$utcCarbon = TimezoneHelper::fromSiteDateStartOfDay($originalDate);
|
||||
$result = TimezoneHelper::toSiteDateString($utcCarbon);
|
||||
|
||||
$this->assertEquals($originalDate, $result);
|
||||
}
|
||||
|
||||
/**
|
||||
* fromSiteDateEndOfDay → toSiteDateString 왕복 변환이 원래 날짜를 유지하는지 테스트합니다.
|
||||
*/
|
||||
public function test_round_trip_end_of_day_preserves_date(): void
|
||||
{
|
||||
config(['app.default_user_timezone' => 'Asia/Seoul']);
|
||||
|
||||
$originalDate = '2025-06-15';
|
||||
$utcCarbon = TimezoneHelper::fromSiteDateEndOfDay($originalDate);
|
||||
$result = TimezoneHelper::toSiteDateString($utcCarbon);
|
||||
|
||||
$this->assertEquals($originalDate, $result);
|
||||
}
|
||||
|
||||
// ==================== toSiteDateTimeLocalString ====================
|
||||
|
||||
/**
|
||||
* toSiteDateTimeLocalString이 datetime-local 호환 형식을 반환하는지 테스트합니다.
|
||||
*/
|
||||
public function test_to_site_date_time_local_string_converts_correctly(): void
|
||||
{
|
||||
config(['app.default_user_timezone' => 'Asia/Seoul']);
|
||||
|
||||
// UTC 15:00 → KST 다음날 00:00
|
||||
$utcDatetime = Carbon::parse('2025-01-15 15:00:00', 'UTC');
|
||||
$result = TimezoneHelper::toSiteDateTimeLocalString($utcDatetime);
|
||||
|
||||
$this->assertEquals('2025-01-16T00:00', $result);
|
||||
}
|
||||
|
||||
/**
|
||||
* toSiteDateTimeLocalString이 null에 대해 null을 반환하는지 테스트합니다.
|
||||
*/
|
||||
public function test_to_site_date_time_local_string_returns_null_for_null(): void
|
||||
{
|
||||
$this->assertNull(TimezoneHelper::toSiteDateTimeLocalString(null));
|
||||
}
|
||||
|
||||
// ==================== fromSiteDateTime ====================
|
||||
|
||||
/**
|
||||
* fromSiteDateTime이 datetime-local 입력을 사이트 타임존 기준 UTC로 변환하는지 테스트합니다.
|
||||
*/
|
||||
public function test_from_site_date_time_converts_to_utc(): void
|
||||
{
|
||||
config(['app.default_user_timezone' => 'Asia/Seoul']);
|
||||
|
||||
// 2025-03-15 09:30 KST = 2025-03-15 00:30:00 UTC
|
||||
$result = TimezoneHelper::fromSiteDateTime('2025-03-15T09:30');
|
||||
|
||||
$this->assertInstanceOf(Carbon::class, $result);
|
||||
$this->assertEquals('2025-03-15 00:30:00', $result->format('Y-m-d H:i:s'));
|
||||
$this->assertEquals('UTC', $result->timezone->getName());
|
||||
}
|
||||
|
||||
/**
|
||||
* fromSiteDateTime이 null에 대해 null을 반환하는지 테스트합니다.
|
||||
*/
|
||||
public function test_from_site_date_time_returns_null_for_null(): void
|
||||
{
|
||||
$this->assertNull(TimezoneHelper::fromSiteDateTime(null));
|
||||
}
|
||||
|
||||
// ==================== datetime-local 왕복 변환 ====================
|
||||
|
||||
/**
|
||||
* fromSiteDateTime → toSiteDateTimeLocalString 왕복 변환이 원래 값을 유지하는지 테스트합니다.
|
||||
*/
|
||||
public function test_round_trip_date_time_local_preserves_value(): void
|
||||
{
|
||||
config(['app.default_user_timezone' => 'Asia/Seoul']);
|
||||
|
||||
$original = '2025-06-15T14:30';
|
||||
$utcCarbon = TimezoneHelper::fromSiteDateTime($original);
|
||||
$result = TimezoneHelper::toSiteDateTimeLocalString($utcCarbon);
|
||||
|
||||
$this->assertEquals($original, $result);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user