koel/tests/Integration/Services/FileStorage/LocalStorageTest.php

70 lines
2.2 KiB
PHP
Raw Normal View History

2024-01-04 11:35:36 +00:00
<?php
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;
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;
class LocalStorageTest extends TestCase
2024-01-04 11:35:36 +00:00
{
private LocalStorage $service;
2024-01-04 11:35:36 +00:00
public function setUp(): void
{
parent::setUp();
$this->service = app(LocalStorage::class);
2024-01-04 11:35:36 +00:00
}
public function testStoreUploadedFileWithMediaPathNotSet(): void
2024-01-04 11:35:36 +00:00
{
Setting::unset('media_path');
2024-01-04 11:35:36 +00:00
self::expectException(MediaPathNotSetException::class);
$this->service->storeUploadedFile(Mockery::mock(UploadedFile::class), create_user());
2024-01-04 11:35:36 +00:00
}
public function testStoreUploadedFileFails(): void
2024-01-04 11:35:36 +00:00
{
Setting::set('media_path', public_path('sandbox/media'));
self::expectException(SongUploadFailedException::class);
$this->service->storeUploadedFile(UploadedFile::fake()->create('fake.mp3'), create_user());
2024-01-04 11:35:36 +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
$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);
}
public function testStoringWithVisibilityPreference(): void
{
$user = create_user();
$user->preferences->makeUploadsPublic = true;
$user->save();
Setting::set('media_path', public_path('sandbox/media'));
$song = $this->service->storeUploadedFile(UploadedFile::fromFile(test_path('songs/full.mp3')), $user); //@phpstan-ignore-line
self::assertTrue($song->is_public);
$user->preferences->makeUploadsPublic = false;
$user->save();
$privateSongs = $this->service->storeUploadedFile(UploadedFile::fromFile(test_path('songs/full.mp3')), $user); //@phpstan-ignore-line
self::assertFalse($privateSongs->is_public);
}
2024-01-04 11:35:36 +00:00
}