koel/tests/Feature/V6/PlaylistSongTest.php

146 lines
4.7 KiB
PHP
Raw Normal View History

2022-07-27 08:49:33 +00:00
<?php
namespace Tests\Feature\V6;
use App\Models\Playlist;
use App\Models\Song;
use App\Models\User;
2022-07-27 15:32:36 +00:00
use Illuminate\Support\Collection;
2022-07-27 08:49:33 +00:00
class PlaylistSongTest extends TestCase
{
public function testGetNormalPlaylist(): void
{
/** @var Playlist $playlist */
$playlist = Playlist::factory()->create();
$playlist->songs()->attach(Song::factory(5)->create());
$this->getAs('api/playlists/' . $playlist->id . '/songs', $playlist->user)
->assertJsonStructure(['*' => SongTest::JSON_STRUCTURE]);
}
public function testGetSmartPlaylist(): void
{
Song::factory()->create(['title' => 'A foo song']);
/** @var Playlist $playlist */
$playlist = Playlist::factory()->create([
'rules' => [
[
'id' => 1658843809274,
'rules' => [
[
'id' => 1658843809274,
'model' => 'title',
'operator' => 'contains',
'value' => ['foo'],
],
],
],
],
]);
$this->getAs('api/playlists/' . $playlist->id . '/songs', $playlist->user)
->assertJsonStructure(['*' => SongTest::JSON_STRUCTURE]);
}
public function testNonOwnerCannotAccessPlaylist(): void
{
$user = User::factory()->create();
/** @var Playlist $playlist */
$playlist = Playlist::factory()->for($user)->create();
$playlist->songs()->attach(Song::factory(5)->create());
$this->getAs('api/playlists/' . $playlist->id . '/songs')
2022-07-27 15:32:36 +00:00
->assertForbidden();
2022-07-27 08:49:33 +00:00
}
public function testAddSongsToPlaylist(): void
{
/** @var Playlist $playlist */
$playlist = Playlist::factory()->create();
2022-07-27 15:32:36 +00:00
/** @var Collection|array<array-key, Song> $songs */
2022-07-27 08:49:33 +00:00
$songs = Song::factory(2)->create();
$this->postAs('api/playlists/' . $playlist->id . '/songs', [
'songs' => $songs->map(static fn (Song $song) => $song->id)->all(),
], $playlist->user)
->assertNoContent();
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();
2022-07-27 15:32:36 +00:00
/** @var Collection|array<array-key, Song> $toBeRemovedSongs */
2022-07-27 08:49:33 +00:00
$toBeRemovedSongs = Song::factory(2)->create();
2022-07-27 15:32:36 +00:00
2022-07-27 08:49:33 +00:00
$playlist->songs()->attach($toRemainSongs->merge($toBeRemovedSongs));
self::assertCount(7, $playlist->songs);
$this->deleteAs('api/playlists/' . $playlist->id . '/songs', [
'songs' => $toBeRemovedSongs->map(static fn (Song $song) => $song->id)->all(),
], $playlist->user)
->assertNoContent();
$playlist->refresh();
self::assertEqualsCanonicalizing($toRemainSongs->pluck('id')->all(), $playlist->songs->pluck('id')->all());
}
public function testNonOwnerCannotModifyPlaylist(): void
{
$user = User::factory()->create();
/** @var Playlist $playlist */
$playlist = Playlist::factory()->for($user)->create();
/** @var Song $song */
$song = Song::factory()->create();
$this->postAs('api/playlists/' . $playlist->id . '/songs', ['songs' => [$song->id]])
2022-07-27 15:32:36 +00:00
->assertForbidden();
2022-07-27 08:49:33 +00:00
$this->deleteAs('api/playlists/' . $playlist->id . '/songs', ['songs' => [$song->id]])
2022-07-27 15:32:36 +00:00
->assertForbidden();
2022-07-27 08:49:33 +00:00
}
public function testSmartPlaylistContentCannotBeModified(): void
{
/** @var Playlist $playlist */
$playlist = Playlist::factory()->create([
'rules' => [
[
'id' => 1658843809274,
'rules' => [
[
'id' => 1658843809274,
'model' => 'title',
'operator' => 'contains',
'value' => ['foo'],
],
],
],
],
]);
2022-07-27 15:32:36 +00:00
/** @var Collection|array<array-key, Song> $songs */
$songs = Song::factory(2)->create();
$songIds = $songs->map(static fn (Song $song) => $song->id)->all();
2022-07-27 08:49:33 +00:00
2022-07-27 15:32:36 +00:00
$this->postAs('api/playlists/' . $playlist->id . '/songs', ['songs' => $songIds], $playlist->user)
->assertForbidden();
2022-07-27 08:49:33 +00:00
2022-07-27 15:32:36 +00:00
$this->deleteAs('api/playlists/' . $playlist->id . '/songs', ['songs' => $songIds], $playlist->user)
->assertForbidden();
2022-07-27 08:49:33 +00:00
}
}