2024-04-26 13:35:26 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Tests\Integration\KoelPlus\Services\SongStorages;
|
|
|
|
|
|
|
|
use App\Models\Song;
|
|
|
|
use App\Services\SongStorages\SftpStorage;
|
|
|
|
use Illuminate\Http\UploadedFile;
|
|
|
|
use Illuminate\Support\Facades\Storage;
|
2024-10-24 10:45:45 +00:00
|
|
|
use PHPUnit\Framework\Attributes\Test;
|
2024-04-26 13:35:26 +00:00
|
|
|
use Tests\PlusTestCase;
|
|
|
|
|
|
|
|
use function Tests\create_user;
|
|
|
|
use function Tests\test_path;
|
|
|
|
|
|
|
|
class SftpStorageTest extends PlusTestCase
|
|
|
|
{
|
|
|
|
private SftpStorage $service;
|
|
|
|
private UploadedFile $file;
|
|
|
|
|
|
|
|
public function setUp(): void
|
|
|
|
{
|
|
|
|
parent::setUp();
|
|
|
|
|
|
|
|
$this->service = app(SftpStorage::class);
|
|
|
|
$this->file = UploadedFile::fromFile(test_path('songs/full.mp3'), 'song.mp3'); //@phpstan-ignore-line
|
|
|
|
}
|
|
|
|
|
2024-10-24 10:45:45 +00:00
|
|
|
#[Test]
|
|
|
|
public function storeUploadedFile(): void
|
2024-04-26 13:35:26 +00:00
|
|
|
{
|
|
|
|
self::assertEquals(0, Song::query()->where('storage', 'sftp')->count());
|
|
|
|
|
|
|
|
Storage::fake('sftp');
|
|
|
|
$song = $this->service->storeUploadedFile($this->file, create_user());
|
|
|
|
|
|
|
|
Storage::disk('sftp')->assertExists($song->storage_metadata->getPath());
|
|
|
|
self::assertEquals(1, Song::query()->where('storage', 'sftp')->count());
|
|
|
|
}
|
|
|
|
|
2024-10-24 10:45:45 +00:00
|
|
|
#[Test]
|
|
|
|
public function storingWithVisibilityPreference(): void
|
2024-04-26 13:35:26 +00:00
|
|
|
{
|
2024-05-31 05:40:34 +00:00
|
|
|
Storage::fake('sftp');
|
|
|
|
|
2024-04-26 13:35:26 +00:00
|
|
|
$user = create_user();
|
|
|
|
|
|
|
|
$user->preferences->makeUploadsPublic = true;
|
|
|
|
$user->save();
|
|
|
|
|
|
|
|
self::assertTrue($this->service->storeUploadedFile($this->file, $user)->is_public);
|
|
|
|
|
|
|
|
$user->preferences->makeUploadsPublic = false;
|
|
|
|
$user->save();
|
|
|
|
|
|
|
|
$privateFile = UploadedFile::fromFile(test_path('songs/full.mp3'), 'song.mp3'); //@phpstan-ignore-line
|
|
|
|
self::assertFalse($this->service->storeUploadedFile($privateFile, $user)->is_public);
|
|
|
|
}
|
|
|
|
|
2024-10-24 10:45:45 +00:00
|
|
|
#[Test]
|
|
|
|
public function deleteSong(): void
|
2024-04-26 13:35:26 +00:00
|
|
|
{
|
|
|
|
Storage::fake('sftp');
|
|
|
|
|
|
|
|
$song = $this->service->storeUploadedFile($this->file, create_user());
|
|
|
|
Storage::disk('sftp')->assertExists($song->storage_metadata->getPath());
|
|
|
|
|
|
|
|
$this->service->delete($song);
|
|
|
|
Storage::disk('sftp')->assertMissing($song->storage_metadata->getPath());
|
|
|
|
}
|
|
|
|
|
2024-10-24 10:45:45 +00:00
|
|
|
#[Test]
|
|
|
|
public function getSongContent(): void
|
2024-04-26 13:35:26 +00:00
|
|
|
{
|
|
|
|
/** @var Song $song */
|
|
|
|
$song = Song::factory()->create();
|
|
|
|
|
|
|
|
Storage::fake('sftp');
|
|
|
|
Storage::shouldReceive('disk->get')->with($song->storage_metadata->getPath())->andReturn('binary-content');
|
|
|
|
|
|
|
|
self::assertEquals('binary-content', $this->service->getSongContent($song));
|
|
|
|
}
|
|
|
|
|
2024-10-24 10:45:45 +00:00
|
|
|
#[Test]
|
|
|
|
public function copyToLocal(): void
|
2024-04-26 13:35:26 +00:00
|
|
|
{
|
|
|
|
/** @var Song $song */
|
|
|
|
$song = Song::factory()->create();
|
|
|
|
|
|
|
|
Storage::fake('sftp');
|
|
|
|
Storage::shouldReceive('disk->get')->with($song->storage_metadata->getPath())->andReturn('binary-content');
|
|
|
|
|
|
|
|
$localPath = $this->service->copyToLocal($song);
|
|
|
|
|
|
|
|
self::assertStringEqualsFile($localPath, 'binary-content');
|
|
|
|
}
|
|
|
|
}
|