mirror of
https://github.com/koel/koel
synced 2024-11-24 05:03:05 +00:00
73 lines
2.3 KiB
PHP
73 lines
2.3 KiB
PHP
<?php
|
|
|
|
namespace Tests\Feature\KoelPlus;
|
|
|
|
use App\Models\Artist;
|
|
use App\Models\Song;
|
|
use App\Services\MediaMetadataService;
|
|
use Mockery;
|
|
use Tests\PlusTestCase;
|
|
|
|
use function Tests\create_admin;
|
|
use function Tests\create_user;
|
|
|
|
class ArtistImageTest extends PlusTestCase
|
|
{
|
|
public function setUp(): void
|
|
{
|
|
parent::setUp();
|
|
|
|
$this->mediaMetadataService = self::mock(MediaMetadataService::class);
|
|
}
|
|
|
|
public function testNormalUserCanUploadImageIfOwningAllSongsInArtist(): void
|
|
{
|
|
$user = create_user();
|
|
|
|
/** @var Artist $artist */
|
|
$artist = Artist::factory()->create();
|
|
$artist->songs()->saveMany(Song::factory()->for($user, 'owner')->count(3)->create());
|
|
|
|
$this->mediaMetadataService
|
|
->shouldReceive('writeArtistImage')
|
|
->once()
|
|
->with(Mockery::on(static fn (Artist $target) => $target->is($artist)), 'Foo', 'jpeg');
|
|
|
|
$this->putAs("api/artists/$artist->id/image", ['image' => 'data:image/jpeg;base64,Rm9v'], $user)
|
|
->assertOk();
|
|
}
|
|
|
|
public function testNormalUserCannotUploadImageIfNotOwningAllSongsInArtist(): void
|
|
{
|
|
$user = create_user();
|
|
|
|
/** @var Artist $artist */
|
|
$artist = Artist::factory()->create();
|
|
$artist->songs()->saveMany(Song::factory()->for($user, 'owner')->count(3)->create());
|
|
$artist->songs()->save(Song::factory()->create());
|
|
|
|
$this->mediaMetadataService
|
|
->shouldReceive('writeArtistImage')
|
|
->never();
|
|
|
|
$this->putAs("api/artists/$artist->id/image", ['image' => 'data:image/jpeg;base64,Rm9v'], $user)
|
|
->assertForbidden();
|
|
}
|
|
|
|
public function testAdminCanUploadImageEvenIfNotOwningAllSongsInArtist(): void
|
|
{
|
|
$user = create_user();
|
|
|
|
/** @var Artist $artist */
|
|
$artist = Artist::factory()->create();
|
|
$artist->songs()->saveMany(Song::factory()->for($user, 'owner')->count(3)->create());
|
|
|
|
$this->mediaMetadataService
|
|
->shouldReceive('writeArtistImage')
|
|
->once()
|
|
->with(Mockery::on(static fn (Artist $target) => $target->is($artist)), 'Foo', 'jpeg');
|
|
|
|
$this->putAs("api/artists/$artist->id/image", ['image' => 'data:image/jpeg;base64,Rm9v'], create_admin())
|
|
->assertOk();
|
|
}
|
|
}
|