2024-01-04 11:35:36 +00:00
|
|
|
<?php
|
|
|
|
|
2024-02-04 20:31:01 +00:00
|
|
|
namespace Tests\Integration\Services\FileStorage;
|
2024-01-04 11:35:36 +00:00
|
|
|
|
|
|
|
use App\Exceptions\MediaPathNotSetException;
|
|
|
|
use App\Exceptions\SongUploadFailedException;
|
|
|
|
use App\Models\Setting;
|
2024-02-04 20:31:01 +00:00
|
|
|
use App\Services\SongStorage\LocalStorage;
|
2024-01-04 11:35:36 +00:00
|
|
|
use Illuminate\Http\UploadedFile;
|
|
|
|
use Mockery;
|
|
|
|
use Tests\TestCase;
|
|
|
|
|
2024-01-11 12:41:33 +00:00
|
|
|
use function Tests\create_user;
|
|
|
|
use function Tests\test_path;
|
|
|
|
|
2024-02-04 20:31:01 +00:00
|
|
|
class LocalStorageTest extends TestCase
|
2024-01-04 11:35:36 +00:00
|
|
|
{
|
2024-02-04 20:31:01 +00:00
|
|
|
private LocalStorage $service;
|
2024-01-04 11:35:36 +00:00
|
|
|
|
|
|
|
public function setUp(): void
|
|
|
|
{
|
|
|
|
parent::setUp();
|
|
|
|
|
2024-02-04 20:31:01 +00:00
|
|
|
$this->service = app(LocalStorage::class);
|
2024-01-04 11:35:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testHandleUploadedFileWithMediaPathNotSet(): void
|
|
|
|
{
|
|
|
|
Setting::set('media_path');
|
|
|
|
|
|
|
|
self::expectException(MediaPathNotSetException::class);
|
2024-02-04 20:31:01 +00:00
|
|
|
$this->service->storeUploadedFile(Mockery::mock(UploadedFile::class), create_user());
|
2024-01-04 11:35:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testHandleUploadedFileFails(): void
|
|
|
|
{
|
|
|
|
Setting::set('media_path', public_path('sandbox/media'));
|
|
|
|
|
|
|
|
self::expectException(SongUploadFailedException::class);
|
2024-02-04 20:31:01 +00:00
|
|
|
$this->service->storeUploadedFile(UploadedFile::fake()->create('fake.mp3'), create_user());
|
2024-01-04 11:35:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testHandleUploadedFile(): void
|
|
|
|
{
|
|
|
|
Setting::set('media_path', public_path('sandbox/media'));
|
2024-01-11 12:41:33 +00:00
|
|
|
$user = create_user();
|
2024-01-04 11:35:36 +00:00
|
|
|
|
2024-02-04 20:31:01 +00:00
|
|
|
$song = $this->service->storeUploadedFile(UploadedFile::fromFile(test_path('songs/full.mp3')), $user); //@phpstan-ignore-line
|
2024-01-04 11:35:36 +00:00
|
|
|
|
|
|
|
self::assertSame($song->owner_id, $user->id);
|
|
|
|
self::assertSame(public_path("sandbox/media/__KOEL_UPLOADS_\${$user->id}__/full.mp3"), $song->path);
|
|
|
|
}
|
2024-01-23 22:50:50 +00:00
|
|
|
|
|
|
|
public function testUploadingTakesIntoAccountUploadVisibilityPreference(): void
|
|
|
|
{
|
|
|
|
$user = create_user();
|
|
|
|
$user->preferences->makeUploadsPublic = true;
|
|
|
|
$user->save();
|
|
|
|
|
|
|
|
Setting::set('media_path', public_path('sandbox/media'));
|
2024-02-04 20:31:01 +00:00
|
|
|
$song = $this->service->storeUploadedFile(UploadedFile::fromFile(test_path('songs/full.mp3')), $user); //@phpstan-ignore-line
|
2024-01-23 22:50:50 +00:00
|
|
|
self::assertTrue($song->is_public);
|
|
|
|
|
|
|
|
$user->preferences->makeUploadsPublic = false;
|
|
|
|
$user->save();
|
2024-02-04 20:31:01 +00:00
|
|
|
$privateSongs = $this->service->storeUploadedFile(UploadedFile::fromFile(test_path('songs/full.mp3')), $user); //@phpstan-ignore-line
|
2024-01-23 22:50:50 +00:00
|
|
|
self::assertFalse($privateSongs->is_public);
|
|
|
|
}
|
2024-01-04 11:35:36 +00:00
|
|
|
}
|