2
0
Fork 0
mirror of https://github.com/koel/koel synced 2024-12-30 06:23:05 +00:00
koel/tests/Feature/PlaylistSongTest.php

143 lines
4.5 KiB
PHP
Raw Normal View History

<?php
namespace Tests\Feature;
use App\Http\Resources\SongResource;
use App\Models\Playlist;
use App\Models\Song;
2023-06-05 21:46:41 +00:00
use Illuminate\Support\Collection;
2024-01-09 18:34:40 +00:00
use Tests\TestCase;
2024-01-11 12:41:33 +00:00
use function Tests\create_user;
class PlaylistSongTest extends TestCase
{
2023-06-05 21:46:41 +00:00
public function testGetNormalPlaylist(): void
{
2023-06-05 21:46:41 +00:00
/** @var Playlist $playlist */
$playlist = Playlist::factory()->create();
2024-01-18 11:13:05 +00:00
$playlist->addSongs(Song::factory(5)->create());
2023-06-05 21:46:41 +00:00
2024-01-18 11:13:05 +00:00
$this->getAs("api/playlists/$playlist->id/songs", $playlist->user)
->assertJsonStructure(['*' => SongResource::JSON_STRUCTURE]);
}
2023-06-05 21:46:41 +00:00
public function testGetSmartPlaylist(): void
{
2023-06-05 21:46:41 +00:00
Song::factory()->create(['title' => 'A foo song']);
/** @var Playlist $playlist */
$playlist = Playlist::factory()->create([
'rules' => [
[
'id' => '45368b8f-fec8-4b72-b826-6b295af0da65',
'rules' => [
[
'id' => '2a4548cd-c67f-44d4-8fec-34ff75c8a026',
'model' => 'title',
'operator' => 'contains',
'value' => ['foo'],
],
],
],
],
]);
$this->getAs("api/playlists/$playlist->id/songs", $playlist->user)
->assertJsonStructure(['*' => SongResource::JSON_STRUCTURE]);
}
2023-06-05 21:46:41 +00:00
public function testNonOwnerCannotAccessPlaylist(): void
{
/** @var Playlist $playlist */
2024-01-11 12:41:33 +00:00
$playlist = Playlist::factory()->for(create_user())->create();
2024-01-18 11:13:05 +00:00
$playlist->addSongs(Song::factory(5)->create());
2023-06-05 21:46:41 +00:00
2024-01-18 11:13:05 +00:00
$this->getAs("api/playlists/$playlist->id/songs")
2023-06-05 21:46:41 +00:00
->assertForbidden();
}
public function testAddSongsToPlaylist(): void
{
/** @var Playlist $playlist */
$playlist = Playlist::factory()->create();
/** @var Collection<array-key, Song> $songs */
2023-06-05 21:46:41 +00:00
$songs = Song::factory(2)->create();
2024-01-18 11:13:05 +00:00
$this->postAs("api/playlists/$playlist->id/songs", ['songs' => $songs->pluck('id')->all()], $playlist->user)
->assertSuccessful();
2023-06-05 21:46:41 +00:00
self::assertEqualsCanonicalizing($songs->pluck('id')->all(), $playlist->songs->pluck('id')->all());
}
public function testRemoveSongsFromPlaylist(): void
{
/** @var Playlist $playlist */
$playlist = Playlist::factory()->create();
$toRemainSongs = Song::factory(5)->create();
/** @var Collection<array-key, Song> $toBeRemovedSongs */
2022-07-27 10:44:25 +00:00
$toBeRemovedSongs = Song::factory(2)->create();
2023-06-05 21:46:41 +00:00
2024-01-18 11:13:05 +00:00
$playlist->addSongs($toRemainSongs->merge($toBeRemovedSongs));
2023-06-05 21:46:41 +00:00
self::assertCount(7, $playlist->songs);
2024-01-18 11:13:05 +00:00
$this->deleteAs(
"api/playlists/$playlist->id/songs",
['songs' => $toBeRemovedSongs->pluck('id')->all()],
$playlist->user
)
2023-06-05 21:46:41 +00:00
->assertNoContent();
2023-06-05 21:46:41 +00:00
$playlist->refresh();
self::assertEqualsCanonicalizing($toRemainSongs->pluck('id')->all(), $playlist->songs->pluck('id')->all());
}
2023-06-05 21:46:41 +00:00
public function testNonOwnerCannotModifyPlaylist(): void
{
/** @var Playlist $playlist */
2024-01-11 12:41:33 +00:00
$playlist = Playlist::factory()->for(create_user())->create();
2023-06-05 21:46:41 +00:00
/** @var Song $song */
$song = Song::factory()->create();
2024-01-18 11:13:05 +00:00
$this->postAs("api/playlists/$playlist->id/songs", ['songs' => [$song->id]])
2023-06-05 21:46:41 +00:00
->assertForbidden();
2024-01-18 11:13:05 +00:00
$this->deleteAs("api/playlists/$playlist->id/songs", ['songs' => [$song->id]])
2023-06-05 21:46:41 +00:00
->assertForbidden();
}
public function testSmartPlaylistContentCannotBeModified(): void
{
/** @var Playlist $playlist */
$playlist = Playlist::factory()->create([
'rules' => [
[
'id' => '45368b8f-fec8-4b72-b826-6b295af0da65',
'rules' => [
[
'id' => '2a4548cd-c67f-44d4-8fec-34ff75c8a026',
'model' => 'title',
'operator' => 'contains',
'value' => ['foo'],
],
],
],
],
]);
2024-01-18 11:13:05 +00:00
$songs = Song::factory(2)->create()->pluck('id')->all();
2024-01-18 11:13:05 +00:00
$this->postAs("api/playlists/$playlist->id/songs", ['songs' => $songs], $playlist->user)
2023-06-05 21:46:41 +00:00
->assertForbidden();
2022-12-04 13:56:25 +00:00
2024-01-18 11:13:05 +00:00
$this->deleteAs("api/playlists/$playlist->id/songs", ['songs' => $songs], $playlist->user)
2023-06-05 21:46:41 +00:00
->assertForbidden();
}
}