2024-01-04 11:35:36 +00:00
|
|
|
<?php
|
|
|
|
|
2024-02-23 18:36:02 +00:00
|
|
|
namespace Tests\Integration\Services\SongStorages;
|
2024-01-04 11:35:36 +00:00
|
|
|
|
|
|
|
use App\Exceptions\MediaPathNotSetException;
|
|
|
|
use App\Exceptions\SongUploadFailedException;
|
|
|
|
use App\Models\Setting;
|
2024-02-23 18:36:02 +00:00
|
|
|
use App\Services\SongStorages\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
|
|
|
}
|
|
|
|
|
2024-02-23 16:03:54 +00:00
|
|
|
public function testStoreUploadedFileWithMediaPathNotSet(): void
|
2024-01-04 11:35:36 +00:00
|
|
|
{
|
2024-02-23 18:36:02 +00:00
|
|
|
Setting::set('media_path', '');
|
2024-01-04 11:35:36 +00:00
|
|
|
|
2024-03-22 20:34:32 +00:00
|
|
|
$this->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
|
|
|
}
|
|
|
|
|
2024-02-23 16:03:54 +00:00
|
|
|
public function testStoreUploadedFileFails(): void
|
2024-01-04 11:35:36 +00:00
|
|
|
{
|
|
|
|
Setting::set('media_path', public_path('sandbox/media'));
|
|
|
|
|
2024-03-22 20:34:32 +00:00
|
|
|
$this->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
|
|
|
}
|
|
|
|
|
2024-02-23 16:03:54 +00:00
|
|
|
public function testStoreUploadedFile(): void
|
2024-01-04 11:35:36 +00:00
|
|
|
{
|
|
|
|
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
|
|
|
|
2024-02-23 16:03:54 +00:00
|
|
|
public function testStoringWithVisibilityPreference(): void
|
2024-01-23 22:50:50 +00:00
|
|
|
{
|
|
|
|
$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
|
|
|
}
|