2024-01-27 11:24:34 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Tests\Feature\KoelPlus;
|
|
|
|
|
|
|
|
use App\Models\Artist;
|
|
|
|
use App\Models\Song;
|
|
|
|
use App\Services\MediaMetadataService;
|
|
|
|
use Mockery;
|
2024-03-22 20:34:32 +00:00
|
|
|
use Mockery\MockInterface;
|
2024-01-27 11:24:34 +00:00
|
|
|
use Tests\PlusTestCase;
|
|
|
|
|
|
|
|
use function Tests\create_admin;
|
|
|
|
use function Tests\create_user;
|
|
|
|
|
|
|
|
class ArtistImageTest extends PlusTestCase
|
|
|
|
{
|
2024-03-22 20:34:32 +00:00
|
|
|
private MockInterface|MediaMetadataService $mediaMetadataService;
|
|
|
|
|
2024-01-27 11:24:34 +00:00
|
|
|
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()
|
2024-03-22 20:34:32 +00:00
|
|
|
->with(Mockery::on(static fn (Artist $target) => $target->is($artist)), 'data:image/jpeg;base64,Rm9v');
|
2024-01-27 11:24:34 +00:00
|
|
|
|
2024-02-24 15:37:01 +00:00
|
|
|
$this->putAs("api/artists/$artist->id/image", ['image' => 'data:image/jpeg;base64,Rm9v'], $user)
|
2024-01-27 11:24:34 +00:00
|
|
|
->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();
|
|
|
|
|
2024-02-24 15:37:01 +00:00
|
|
|
$this->putAs("api/artists/$artist->id/image", ['image' => 'data:image/jpeg;base64,Rm9v'], $user)
|
2024-01-27 11:24:34 +00:00
|
|
|
->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()
|
2024-03-22 20:34:32 +00:00
|
|
|
->with(Mockery::on(static fn (Artist $target) => $target->is($artist)), 'data:image/jpeg;base64,Rm9v');
|
2024-01-27 11:24:34 +00:00
|
|
|
|
2024-02-24 15:37:01 +00:00
|
|
|
$this->putAs("api/artists/$artist->id/image", ['image' => 'data:image/jpeg;base64,Rm9v'], create_admin())
|
2024-01-27 11:24:34 +00:00
|
|
|
->assertOk();
|
|
|
|
}
|
|
|
|
}
|