koel/tests/Feature/KoelPlus/PlaylistSongTest.php

80 lines
2.8 KiB
PHP
Raw Normal View History

2024-01-18 11:13:05 +00:00
<?php
namespace Tests\Feature\KoelPlus;
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
{
/** @var Playlist $playlist */
2024-01-18 11:13:05 +00:00
$playlist = Playlist::factory()->create();
$playlist->addPlayables(Song::factory()->public()->count(3)->create());
2024-01-18 11:13:05 +00:00
$collaborator = create_user();
$playlist->addCollaborator($collaborator);
$this->getAs("api/playlists/$playlist->id/songs", $collaborator)
->assertSuccessful()
->assertJsonStructure(['*' => CollaborativeSongResource::JSON_STRUCTURE])
2024-01-18 11:13:05 +00:00
->assertJsonCount(3);
}
public function testPrivateSongsDoNotShowUpInCollaborativePlaylist(): void
{
/** @var Playlist $playlist */
2024-01-18 11:13:05 +00:00
$playlist = Playlist::factory()->create();
$playlist->addPlayables(Song::factory()->public()->count(3)->create());
2024-01-18 11:13:05 +00:00
/** @var Song $privateSong */
$privateSong = Song::factory()->private()->create();
$playlist->addPlayables($privateSong);
2024-01-18 11:13:05 +00:00
$collaborator = create_user();
$playlist->addCollaborator($collaborator);
$this->getAs("api/playlists/$playlist->id/songs", $collaborator)
->assertSuccessful()
->assertJsonStructure(['*' => CollaborativeSongResource::JSON_STRUCTURE])
2024-01-18 11:13:05 +00:00
->assertJsonCount(3)
->assertJsonMissing(['id' => $privateSong->id]);
}
public function testCollaboratorCanAddSongs(): void
{
/** @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();
$playlist->refresh();
2024-09-15 13:33:59 +00:00
$songs->each(static fn (Song $song) => self::assertTrue($playlist->playables->contains($song)));
2024-01-18 11:13:05 +00:00
}
public function testCollaboratorCanRemoveSongs(): void
{
/** @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->addPlayables($songs);
2024-01-18 11:13:05 +00:00
$this->deleteAs("api/playlists/$playlist->id/songs", ['songs' => $songs->pluck('id')->all()], $collaborator)
->assertSuccessful();
$playlist->refresh();
2024-09-15 13:33:59 +00:00
$songs->each(static fn (Song $song) => self::assertFalse($playlist->playables->contains($song)));
2024-01-18 11:13:05 +00:00
}
}