StorageInterface::url 의 public 하드코딩을 완화하고 core.storage.filter_url 훅을 신설해, 완전 공개 자산(상품·카테고리·리뷰·에디터 이미지)의 직접 URL 서빙을 옵트인으로 열었다. 미설정 시 기존 스트리밍 100% 보존. 디스크 결정은 확장 개별 설정 > 코어 전역 > 미설정 순이며, 서빙·삭제·이동은 행에 기록된 disk 를 따라 전환 전후 자산이 혼재해도 각자 실제 저장 위치를 향한다. 고아 disk(공급 플러그인 비활성화)는 스트리밍으로 폴백해 500 이 되지 않는다. 공개 이슈 (@lyg-kaban 건의) 대응.
343 lines
11 KiB
PHP
343 lines
11 KiB
PHP
<?php
|
|
|
|
namespace Tests\Unit\Extension\Storage;
|
|
|
|
use App\Extension\HookManager;
|
|
use App\Extension\Storage\ModuleStorageDriver;
|
|
use Illuminate\Foundation\Testing\RefreshDatabase;
|
|
use Illuminate\Support\Facades\Config;
|
|
use Illuminate\Support\Facades\Storage;
|
|
use PHPUnit\Framework\Attributes\Test;
|
|
use Symfony\Component\HttpFoundation\StreamedResponse;
|
|
use Tests\TestCase;
|
|
|
|
/**
|
|
* ModuleStorageDriver 단위 테스트
|
|
*/
|
|
class ModuleStorageDriverTest extends TestCase
|
|
{
|
|
use RefreshDatabase;
|
|
|
|
private ModuleStorageDriver $driver;
|
|
|
|
protected function setUp(): void
|
|
{
|
|
parent::setUp();
|
|
|
|
// 테스트용 스토리지 드라이버 생성
|
|
$this->driver = new ModuleStorageDriver('test-module', 'modules');
|
|
|
|
// 테스트 디렉토리 초기화
|
|
Storage::disk('modules')->deleteDirectory('test-module');
|
|
}
|
|
|
|
protected function tearDown(): void
|
|
{
|
|
// 테스트 데이터 정리
|
|
Storage::disk('modules')->deleteDirectory('test-module');
|
|
HookManager::resetAll();
|
|
|
|
parent::tearDown();
|
|
}
|
|
|
|
#[Test]
|
|
public function it_can_put_and_get_file(): void
|
|
{
|
|
// Given: 파일 내용
|
|
$content = 'test content';
|
|
|
|
// When: 파일 저장
|
|
$result = $this->driver->put('settings', 'config.json', $content);
|
|
|
|
// Then: 저장 성공
|
|
$this->assertTrue($result);
|
|
|
|
// And: 파일 읽기 성공
|
|
$retrieved = $this->driver->get('settings', 'config.json');
|
|
$this->assertEquals($content, $retrieved);
|
|
}
|
|
|
|
#[Test]
|
|
public function it_returns_null_for_non_existent_file(): void
|
|
{
|
|
// When: 존재하지 않는 파일 읽기
|
|
$result = $this->driver->get('settings', 'non-existent.json');
|
|
|
|
// Then: null 반환
|
|
$this->assertNull($result);
|
|
}
|
|
|
|
#[Test]
|
|
public function it_can_check_file_existence(): void
|
|
{
|
|
// Given: 파일 저장
|
|
$this->driver->put('settings', 'config.json', 'test');
|
|
|
|
// When & Then: 파일 존재 확인
|
|
$this->assertTrue($this->driver->exists('settings', 'config.json'));
|
|
$this->assertFalse($this->driver->exists('settings', 'non-existent.json'));
|
|
}
|
|
|
|
#[Test]
|
|
public function it_can_delete_file(): void
|
|
{
|
|
// Given: 파일 저장
|
|
$this->driver->put('settings', 'config.json', 'test');
|
|
|
|
// When: 파일 삭제
|
|
$result = $this->driver->delete('settings', 'config.json');
|
|
|
|
// Then: 삭제 성공
|
|
$this->assertTrue($result);
|
|
$this->assertFalse($this->driver->exists('settings', 'config.json'));
|
|
}
|
|
|
|
#[Test]
|
|
public function it_can_list_files_in_category(): void
|
|
{
|
|
// Given: 여러 파일 저장
|
|
$this->driver->put('attachments', 'file1.txt', 'content1');
|
|
$this->driver->put('attachments', 'file2.txt', 'content2');
|
|
$this->driver->put('attachments', 'subdir/file3.txt', 'content3');
|
|
|
|
// When: 파일 목록 조회
|
|
$files = $this->driver->files('attachments');
|
|
|
|
// Then: 파일 목록 확인 (재귀적으로 모든 파일 포함)
|
|
$this->assertIsArray($files);
|
|
$this->assertNotEmpty($files);
|
|
}
|
|
|
|
#[Test]
|
|
public function it_can_delete_directory(): void
|
|
{
|
|
// Given: 디렉토리와 파일 생성
|
|
$this->driver->put('cache', 'dir1/file1.txt', 'content1');
|
|
$this->driver->put('cache', 'dir1/file2.txt', 'content2');
|
|
|
|
// When: 디렉토리 삭제
|
|
$result = $this->driver->deleteDirectory('cache', 'dir1');
|
|
|
|
// Then: 삭제 성공
|
|
$this->assertTrue($result);
|
|
$this->assertFalse($this->driver->exists('cache', 'dir1/file1.txt'));
|
|
}
|
|
|
|
#[Test]
|
|
public function it_can_delete_all_files_in_category(): void
|
|
{
|
|
// Given: 여러 파일 저장
|
|
$this->driver->put('temp', 'file1.txt', 'content1');
|
|
$this->driver->put('temp', 'file2.txt', 'content2');
|
|
$this->driver->put('temp', 'subdir/file3.txt', 'content3');
|
|
|
|
// When: 카테고리 전체 삭제
|
|
$result = $this->driver->deleteAll('temp');
|
|
|
|
// Then: 삭제 성공
|
|
$this->assertTrue($result);
|
|
$this->assertFalse($this->driver->exists('temp', 'file1.txt'));
|
|
$this->assertFalse($this->driver->exists('temp', 'file2.txt'));
|
|
$this->assertFalse($this->driver->exists('temp', 'subdir/file3.txt'));
|
|
}
|
|
|
|
#[Test]
|
|
public function it_returns_correct_base_path(): void
|
|
{
|
|
// When: 기본 경로 조회
|
|
$basePath = $this->driver->getBasePath('settings');
|
|
|
|
// Then: 올바른 경로 반환
|
|
$expectedPath = Storage::disk('modules')->path('test-module/settings');
|
|
$this->assertEquals($expectedPath, $basePath);
|
|
}
|
|
|
|
#[Test]
|
|
public function it_returns_correct_disk_name(): void
|
|
{
|
|
// When: 디스크 이름 조회
|
|
$disk = $this->driver->getDisk();
|
|
|
|
// Then: 'modules' 반환
|
|
$this->assertEquals('modules', $disk);
|
|
}
|
|
|
|
#[Test]
|
|
public function it_returns_null_for_url_on_private_disk(): void
|
|
{
|
|
// Given: local disk (private)
|
|
$this->driver->put('images', 'test.jpg', 'image content');
|
|
|
|
// When: URL 조회
|
|
$url = $this->driver->url('images', 'test.jpg');
|
|
|
|
// Then: null 반환 (private disk)
|
|
$this->assertNull($url);
|
|
}
|
|
|
|
#[Test]
|
|
public function it_returns_url_for_public_disk(): void
|
|
{
|
|
// Given: public disk 사용
|
|
$publicDriver = new ModuleStorageDriver('test-module', 'public');
|
|
$publicDriver->put('images', 'test.jpg', 'image content');
|
|
|
|
// When: URL 조회
|
|
$url = $publicDriver->url('images', 'test.jpg');
|
|
|
|
// Then: URL 반환
|
|
$this->assertNotNull($url);
|
|
$this->assertStringContainsString('test-module/images/test.jpg', $url);
|
|
|
|
// Cleanup
|
|
Storage::disk('public')->deleteDirectory('test-module');
|
|
}
|
|
|
|
/**
|
|
* url 이 설정된 디스크(S3+CDN 등)면 모듈 드라이버도 직접 URL 을 돌려준다.
|
|
*
|
|
* @effects url_returned_for_public_and_url_configured_disk
|
|
*/
|
|
#[Test]
|
|
public function it_returns_direct_url_for_url_configured_disk(): void
|
|
{
|
|
Config::set('filesystems.disks.fake_cdn', [
|
|
'driver' => 'local',
|
|
'root' => storage_path('framework/testing/disks/fake_cdn'),
|
|
'url' => 'https://cdn.test/assets',
|
|
]);
|
|
|
|
$cdnDriver = new ModuleStorageDriver('test-module', 'fake_cdn');
|
|
|
|
$this->assertSame(
|
|
'https://cdn.test/assets/test-module/images/test.jpg',
|
|
$cdnDriver->url('images', 'test.jpg')
|
|
);
|
|
}
|
|
|
|
/**
|
|
* url 값이 빈 문자열/공백이면 직접 URL 로 보지 않는다 (AWS_URL 미설정 방어).
|
|
*
|
|
* @effects blank_url_config_defended
|
|
*/
|
|
#[Test]
|
|
public function it_returns_null_when_disk_url_is_blank(): void
|
|
{
|
|
Config::set('filesystems.disks.fake_cdn', [
|
|
'driver' => 'local',
|
|
'root' => storage_path('framework/testing/disks/fake_cdn'),
|
|
'url' => ' ',
|
|
]);
|
|
|
|
$this->assertNull((new ModuleStorageDriver('test-module', 'fake_cdn'))->url('images', 'test.jpg'));
|
|
}
|
|
|
|
/**
|
|
* 모듈 드라이버의 `core.storage.filter_url` 컨텍스트는 scope=module +
|
|
* 확장 식별자를 싣는다 (코어/플러그인과 구분되는 축).
|
|
*
|
|
* @effects filter_url_hook_always_fires_with_six_context_keys
|
|
*/
|
|
#[Test]
|
|
public function it_passes_module_scope_and_identifier_to_url_filter_hook(): void
|
|
{
|
|
$captured = null;
|
|
|
|
HookManager::addFilter('core.storage.filter_url', function ($url, $context) use (&$captured) {
|
|
$captured = $context;
|
|
|
|
return $url;
|
|
});
|
|
|
|
(new ModuleStorageDriver('test-module', 'modules'))->url('images', 'sub/a.jpg');
|
|
|
|
$this->assertSame([
|
|
'scope' => 'module',
|
|
'identifier' => 'test-module',
|
|
'disk' => 'modules',
|
|
'category' => 'images',
|
|
'path' => 'sub/a.jpg',
|
|
'full_path' => 'test-module/images/sub/a.jpg',
|
|
], $captured);
|
|
}
|
|
|
|
#[Test]
|
|
public function it_uses_correct_path_pattern(): void
|
|
{
|
|
// Given: 파일 저장
|
|
$this->driver->put('attachments', 'board/2024/01/file.txt', 'content');
|
|
|
|
// When: 파일 존재 확인
|
|
$exists = $this->driver->exists('attachments', 'board/2024/01/file.txt');
|
|
|
|
// Then: 올바른 경로로 저장됨
|
|
$this->assertTrue($exists);
|
|
|
|
// And: Storage Facade로도 확인 가능
|
|
$storagePath = 'test-module/attachments/board/2024/01/file.txt';
|
|
$this->assertTrue(Storage::disk('modules')->exists($storagePath));
|
|
}
|
|
|
|
#[Test]
|
|
public function it_returns_streamed_response_for_existing_file(): void
|
|
{
|
|
// Given: 파일 저장
|
|
$this->driver->put('images', 'test.jpg', 'image content');
|
|
|
|
// When: StreamedResponse 생성
|
|
$response = $this->driver->response('images', 'test.jpg', 'download.jpg', [
|
|
'Content-Type' => 'image/jpeg',
|
|
]);
|
|
|
|
// Then: StreamedResponse 반환
|
|
$this->assertInstanceOf(StreamedResponse::class, $response);
|
|
}
|
|
|
|
#[Test]
|
|
public function it_returns_null_for_response_on_non_existent_file(): void
|
|
{
|
|
// When: 존재하지 않는 파일로 StreamedResponse 생성 시도
|
|
$response = $this->driver->response('images', 'non-existent.jpg', 'download.jpg');
|
|
|
|
// Then: null 반환
|
|
$this->assertNull($response);
|
|
}
|
|
|
|
// ========================================================================
|
|
// 트랙 2-A — `.preserve-ownership` 마커 자동 작성 (코어 7.0.0-beta.4+)
|
|
//
|
|
// ModuleStorageDriver 가 첫 디렉토리 생성 시 마커 파일을 자동으로 작성하여
|
|
// 미래 코어 update 의 chownRecursive 가 storage/app/modules/{id}/ 트리를
|
|
// 자동 skip 하도록 함. 시드 시점 owner/perms 영구 보존.
|
|
// ========================================================================
|
|
|
|
#[Test]
|
|
public function it_writes_preservation_marker_on_first_put(): void
|
|
{
|
|
// When: 첫 파일 저장 (디렉토리 자동 생성)
|
|
$this->driver->put('images', 'product/img.jpg', 'image-data');
|
|
|
|
// Then: 모듈 루트에 .preserve-ownership 마커 자동 생성
|
|
$markerPath = 'test-module/.preserve-ownership';
|
|
$this->assertTrue(
|
|
Storage::disk('modules')->exists($markerPath),
|
|
'ModuleStorageDriver 가 첫 사용 시 .preserve-ownership 마커를 자동 작성해야 함 '
|
|
.'(미래 chownRecursive 가드 트리거)',
|
|
);
|
|
}
|
|
|
|
#[Test]
|
|
public function it_does_not_overwrite_existing_marker(): void
|
|
{
|
|
// Given: 마커 사전 작성 (운영자 커스텀 내용)
|
|
Storage::disk('modules')->put('test-module/.preserve-ownership', 'custom marker');
|
|
|
|
// When: 추가 파일 저장
|
|
$this->driver->put('images', 'test.jpg', 'data');
|
|
|
|
// Then: 기존 마커 내용 보존 (멱등)
|
|
$content = Storage::disk('modules')->get('test-module/.preserve-ownership');
|
|
$this->assertSame('custom marker', $content, '기존 마커 내용 무변경 (멱등)');
|
|
}
|
|
}
|