koel/tests/Feature/UploadTest.php

64 lines
1.7 KiB
PHP
Raw Normal View History

2020-06-07 20:43:04 +00:00
<?php
namespace Tests\Feature;
2022-09-02 05:00:49 +00:00
use App\Events\LibraryChanged;
2020-06-07 20:43:04 +00:00
use App\Exceptions\MediaPathNotSetException;
use App\Exceptions\SongUploadFailedException;
use App\Models\Setting;
use App\Models\User;
2022-08-08 16:00:59 +00:00
use Illuminate\Http\Response;
2020-06-07 20:43:04 +00:00
use Illuminate\Http\UploadedFile;
2022-09-02 05:00:49 +00:00
use Illuminate\Support\Facades\Event;
2024-01-09 18:34:40 +00:00
use Tests\TestCase;
2020-06-07 20:43:04 +00:00
class UploadTest extends TestCase
{
2022-08-08 16:00:59 +00:00
private UploadedFile $file;
2020-06-07 20:43:04 +00:00
public function setUp(): void
{
parent::setUp();
2024-01-07 12:43:10 +00:00
$this->file = UploadedFile::fromFile(test_path('songs/full.mp3'), 'song.mp3'); //@phpstan-ignore-line
2020-06-07 20:43:04 +00:00
}
public function testUnauthorizedPost(): void
{
Setting::set('media_path');
2020-06-07 20:43:04 +00:00
2022-08-08 16:00:59 +00:00
$this->postAs('/api/upload', ['file' => $this->file])->assertForbidden();
2020-06-07 20:43:04 +00:00
}
2020-12-22 20:11:22 +00:00
/** @return array<mixed> */
2020-06-07 20:43:04 +00:00
public function provideUploadExceptions(): array
{
return [
2022-08-08 16:00:59 +00:00
[MediaPathNotSetException::class, Response::HTTP_FORBIDDEN],
[SongUploadFailedException::class, Response::HTTP_BAD_REQUEST],
2020-06-07 20:43:04 +00:00
];
}
public function testUploadFailsIfMediaPathIsNotSet(): void
2020-06-07 20:43:04 +00:00
{
Setting::set('media_path');
2022-07-27 15:32:36 +00:00
/** @var User $admin */
$admin = User::factory()->admin()->create();
$this->postAs('/api/upload', ['file' => $this->file], $admin)->assertForbidden();
2020-06-07 20:43:04 +00:00
}
public function testUploadSuccessful(): void
2020-06-07 20:43:04 +00:00
{
2022-09-02 05:00:49 +00:00
Event::fake(LibraryChanged::class);
Setting::set('media_path', public_path('sandbox/media'));
2022-07-27 15:32:36 +00:00
/** @var User $admin */
$admin = User::factory()->admin()->create();
2022-08-08 16:00:59 +00:00
$this->postAs('/api/upload', ['file' => $this->file], $admin)->assertJsonStructure(['song', 'album']);
2022-09-02 05:00:49 +00:00
Event::assertDispatched(LibraryChanged::class);
2020-06-07 20:43:04 +00:00
}
}