2021-10-11 11:30:27 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Tests\Feature;
|
|
|
|
|
|
|
|
use App\Models\Playlist;
|
|
|
|
use App\Models\Song;
|
|
|
|
use App\Models\User;
|
2023-06-05 21:46:41 +00:00
|
|
|
use Illuminate\Support\Collection;
|
2021-10-11 11:30:27 +00:00
|
|
|
|
|
|
|
class PlaylistSongTest extends TestCase
|
|
|
|
{
|
2023-06-05 21:46:41 +00:00
|
|
|
public function testGetNormalPlaylist(): void
|
2021-10-11 11:30:27 +00:00
|
|
|
{
|
2023-06-05 21:46:41 +00:00
|
|
|
/** @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]);
|
2021-10-11 11:30:27 +00:00
|
|
|
}
|
|
|
|
|
2023-06-05 21:46:41 +00:00
|
|
|
public function testGetSmartPlaylist(): void
|
2021-10-11 11:30:27 +00:00
|
|
|
{
|
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(['*' => SongTest::JSON_STRUCTURE]);
|
2021-10-11 11:30:27 +00:00
|
|
|
}
|
|
|
|
|
2023-06-05 21:46:41 +00:00
|
|
|
public function testNonOwnerCannotAccessPlaylist(): void
|
2021-10-11 11:30:27 +00:00
|
|
|
{
|
|
|
|
$user = User::factory()->create();
|
|
|
|
|
|
|
|
/** @var Playlist $playlist */
|
2022-07-27 10:44:25 +00:00
|
|
|
$playlist = Playlist::factory()->for($user)->create();
|
2023-06-05 21:46:41 +00:00
|
|
|
$playlist->songs()->attach(Song::factory(5)->create());
|
|
|
|
|
|
|
|
$this->getAs('api/playlists/' . $playlist->id . '/songs')
|
|
|
|
->assertForbidden();
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testAddSongsToPlaylist(): void
|
|
|
|
{
|
|
|
|
/** @var Playlist $playlist */
|
|
|
|
$playlist = Playlist::factory()->create();
|
|
|
|
|
|
|
|
/** @var Collection|array<array-key, Song> $songs */
|
|
|
|
$songs = Song::factory(2)->create();
|
2021-10-11 11:30:27 +00:00
|
|
|
|
2023-06-05 21:46:41 +00:00
|
|
|
$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();
|
|
|
|
|
|
|
|
/** @var Collection|array<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
|
|
|
|
2022-07-27 10:44:25 +00:00
|
|
|
$playlist->songs()->attach($toRemainSongs->merge($toBeRemovedSongs));
|
2021-10-11 11:30:27 +00:00
|
|
|
|
2023-06-05 21:46:41 +00:00
|
|
|
self::assertCount(7, $playlist->songs);
|
2021-10-11 11:30:27 +00:00
|
|
|
|
2023-06-05 21:46:41 +00:00
|
|
|
$this->deleteAs('api/playlists/' . $playlist->id . '/songs', [
|
|
|
|
'songs' => $toBeRemovedSongs->map(static fn (Song $song) => $song->id)->all(),
|
|
|
|
], $playlist->user)
|
|
|
|
->assertNoContent();
|
2021-10-11 11:30:27 +00:00
|
|
|
|
2023-06-05 21:46:41 +00:00
|
|
|
$playlist->refresh();
|
|
|
|
|
|
|
|
self::assertEqualsCanonicalizing($toRemainSongs->pluck('id')->all(), $playlist->songs->pluck('id')->all());
|
2021-10-11 11:30:27 +00:00
|
|
|
}
|
|
|
|
|
2023-06-05 21:46:41 +00:00
|
|
|
public function testNonOwnerCannotModifyPlaylist(): void
|
2021-10-11 11:30:27 +00:00
|
|
|
{
|
2023-06-05 21:46:41 +00:00
|
|
|
$user = User::factory()->create();
|
|
|
|
|
2021-10-11 11:30:27 +00:00
|
|
|
/** @var Playlist $playlist */
|
2023-06-05 21:46:41 +00:00
|
|
|
$playlist = Playlist::factory()->for($user)->create();
|
|
|
|
|
|
|
|
/** @var Song $song */
|
|
|
|
$song = Song::factory()->create();
|
|
|
|
|
|
|
|
$this->postAs('api/playlists/' . $playlist->id . '/songs', ['songs' => [$song->id]])
|
|
|
|
->assertForbidden();
|
|
|
|
|
|
|
|
$this->deleteAs('api/playlists/' . $playlist->id . '/songs', ['songs' => [$song->id]])
|
|
|
|
->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'],
|
|
|
|
],
|
|
|
|
],
|
|
|
|
],
|
|
|
|
],
|
|
|
|
]);
|
2021-10-11 11:30:27 +00:00
|
|
|
|
2023-06-05 21:46:41 +00:00
|
|
|
/** @var Collection|array<array-key, Song> $songs */
|
2021-10-11 11:30:27 +00:00
|
|
|
$songs = Song::factory(2)->create();
|
2023-06-05 21:46:41 +00:00
|
|
|
$songIds = $songs->map(static fn (Song $song) => $song->id)->all();
|
2021-10-11 11:30:27 +00:00
|
|
|
|
2023-06-05 21:46:41 +00:00
|
|
|
$this->postAs('api/playlists/' . $playlist->id . '/songs', ['songs' => $songIds], $playlist->user)
|
|
|
|
->assertForbidden();
|
2022-12-04 13:56:25 +00:00
|
|
|
|
2023-06-05 21:46:41 +00:00
|
|
|
$this->deleteAs('api/playlists/' . $playlist->id . '/songs', ['songs' => $songIds], $playlist->user)
|
|
|
|
->assertForbidden();
|
2021-10-11 11:30:27 +00:00
|
|
|
}
|
|
|
|
}
|