2024-01-18 11:13:05 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Tests\Feature\KoelPlus;
|
|
|
|
|
2024-01-25 16:21:26 +00:00
|
|
|
use App\Http\Resources\CollaborativeSongResource;
|
2024-01-18 11:13:05 +00:00
|
|
|
use App\Models\Playlist;
|
|
|
|
use App\Models\Song;
|
|
|
|
use Tests\PlusTestCase;
|
|
|
|
|
|
|
|
use function Tests\create_user;
|
|
|
|
|
|
|
|
class PlaylistSongTest extends PlusTestCase
|
|
|
|
{
|
|
|
|
public function testGetSongsInCollaborativePlaylist(): void
|
|
|
|
{
|
2024-01-22 22:33:16 +00:00
|
|
|
/** @var Playlist $playlist */
|
2024-01-18 11:13:05 +00:00
|
|
|
$playlist = Playlist::factory()->create();
|
|
|
|
$playlist->addSongs(Song::factory()->public()->count(3)->create());
|
|
|
|
|
|
|
|
$collaborator = create_user();
|
|
|
|
$playlist->addCollaborator($collaborator);
|
|
|
|
|
|
|
|
$this->getAs("api/playlists/$playlist->id/songs", $collaborator)
|
|
|
|
->assertSuccessful()
|
2024-01-25 16:21:26 +00:00
|
|
|
->assertJsonStructure(['*' => CollaborativeSongResource::JSON_STRUCTURE])
|
2024-01-18 11:13:05 +00:00
|
|
|
->assertJsonCount(3);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testPrivateSongsDoNotShowUpInCollaborativePlaylist(): void
|
|
|
|
{
|
2024-01-22 22:33:16 +00:00
|
|
|
/** @var Playlist $playlist */
|
2024-01-18 11:13:05 +00:00
|
|
|
$playlist = Playlist::factory()->create();
|
|
|
|
$playlist->addSongs(Song::factory()->public()->count(3)->create());
|
|
|
|
|
|
|
|
/** @var Song $privateSong */
|
|
|
|
$privateSong = Song::factory()->private()->create();
|
|
|
|
$playlist->addSongs($privateSong);
|
|
|
|
|
|
|
|
$collaborator = create_user();
|
|
|
|
$playlist->addCollaborator($collaborator);
|
|
|
|
|
|
|
|
$this->getAs("api/playlists/$playlist->id/songs", $collaborator)
|
|
|
|
->assertSuccessful()
|
2024-01-25 16:21:26 +00:00
|
|
|
->assertJsonStructure(['*' => CollaborativeSongResource::JSON_STRUCTURE])
|
2024-01-18 11:13:05 +00:00
|
|
|
->assertJsonCount(3)
|
|
|
|
->assertJsonMissing(['id' => $privateSong->id]);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testCollaboratorCanAddSongs(): void
|
|
|
|
{
|
2024-01-22 22:33:16 +00:00
|
|
|
/** @var Playlist $playlist */
|
2024-01-18 11:13:05 +00:00
|
|
|
$playlist = Playlist::factory()->create();
|
|
|
|
$collaborator = create_user();
|
|
|
|
$playlist->addCollaborator($collaborator);
|
|
|
|
$songs = Song::factory()->for($collaborator, 'owner')->count(3)->create();
|
|
|
|
|
|
|
|
$this->postAs("api/playlists/$playlist->id/songs", ['songs' => $songs->pluck('id')->all()], $collaborator)
|
|
|
|
->assertSuccessful();
|
|
|
|
|
2024-01-25 16:21:26 +00:00
|
|
|
$playlist->refresh();
|
|
|
|
$songs->each(static fn (Song $song) => self::assertTrue($playlist->songs->contains($song)));
|
2024-01-18 11:13:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testCollaboratorCanRemoveSongs(): void
|
|
|
|
{
|
2024-01-22 22:33:16 +00:00
|
|
|
/** @var Playlist $playlist */
|
2024-01-18 11:13:05 +00:00
|
|
|
$playlist = Playlist::factory()->create();
|
|
|
|
$collaborator = create_user();
|
|
|
|
$playlist->addCollaborator($collaborator);
|
|
|
|
$songs = Song::factory()->for($collaborator, 'owner')->count(3)->create();
|
|
|
|
$playlist->addSongs($songs);
|
|
|
|
|
|
|
|
$this->deleteAs("api/playlists/$playlist->id/songs", ['songs' => $songs->pluck('id')->all()], $collaborator)
|
|
|
|
->assertSuccessful();
|
|
|
|
|
2024-01-25 16:21:26 +00:00
|
|
|
$playlist->refresh();
|
|
|
|
$songs->each(static fn (Song $song) => self::assertFalse($playlist->songs->contains($song)));
|
2024-01-18 11:13:05 +00:00
|
|
|
}
|
|
|
|
}
|