2015-12-13 12:42:28 +08:00
|
|
|
<?php
|
|
|
|
|
2017-02-14 14:53:02 +08:00
|
|
|
namespace Tests\Feature;
|
|
|
|
|
2015-12-13 12:42:28 +08:00
|
|
|
use App\Models\Playlist;
|
2021-10-08 12:19:44 +02:00
|
|
|
use App\Models\Rule;
|
2015-12-14 08:22:39 -05:00
|
|
|
use App\Models\Song;
|
|
|
|
use App\Models\User;
|
2021-06-05 12:47:56 +02:00
|
|
|
use Illuminate\Support\Collection;
|
2015-12-13 12:42:28 +08:00
|
|
|
|
2017-08-05 17:56:11 +01:00
|
|
|
class PlaylistTest extends TestCase
|
2015-12-13 12:42:28 +08:00
|
|
|
{
|
2019-07-22 09:03:23 +02:00
|
|
|
public function setUp(): void
|
2015-12-13 12:42:28 +08:00
|
|
|
{
|
|
|
|
parent::setUp();
|
2020-11-14 17:57:25 +01:00
|
|
|
|
2020-04-27 20:55:12 +02:00
|
|
|
static::createSampleMediaSet();
|
2015-12-13 12:42:28 +08:00
|
|
|
}
|
|
|
|
|
2019-07-22 09:03:23 +02:00
|
|
|
public function testCreatingPlaylist(): void
|
2015-12-13 12:42:28 +08:00
|
|
|
{
|
2020-09-06 20:21:39 +02:00
|
|
|
/** @var User $user */
|
2020-11-14 17:57:25 +01:00
|
|
|
$user = User::factory()->create();
|
2021-06-05 12:47:56 +02:00
|
|
|
|
|
|
|
/** @var array<Song>|Collection $songs */
|
2015-12-13 12:42:28 +08:00
|
|
|
$songs = Song::orderBy('id')->take(3)->get();
|
|
|
|
|
2016-09-26 14:30:00 +08:00
|
|
|
$this->postAsUser('api/playlist', [
|
2018-11-25 22:21:46 +01:00
|
|
|
'name' => 'Foo Bar',
|
|
|
|
'songs' => $songs->pluck('id')->toArray(),
|
|
|
|
'rules' => [],
|
|
|
|
], $user);
|
2015-12-13 12:42:28 +08:00
|
|
|
|
2020-09-06 20:21:39 +02:00
|
|
|
self::assertDatabaseHas('playlists', [
|
2015-12-13 12:42:28 +08:00
|
|
|
'user_id' => $user->id,
|
|
|
|
'name' => 'Foo Bar',
|
|
|
|
]);
|
|
|
|
|
2021-06-05 12:47:56 +02:00
|
|
|
/** @var Playlist $playlist */
|
2015-12-13 12:42:28 +08:00
|
|
|
$playlist = Playlist::orderBy('id', 'desc')->first();
|
|
|
|
|
|
|
|
foreach ($songs as $song) {
|
2020-09-06 20:21:39 +02:00
|
|
|
self::assertDatabaseHas('playlist_song', [
|
2015-12-13 12:42:28 +08:00
|
|
|
'playlist_id' => $playlist->id,
|
|
|
|
'song_id' => $song->id,
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-08 12:19:44 +02:00
|
|
|
public function testCreatingSmartPlaylist(): void
|
|
|
|
{
|
|
|
|
/** @var User $user */
|
|
|
|
$user = User::factory()->create();
|
|
|
|
|
|
|
|
$rule = Rule::create([
|
|
|
|
'model' => 'artist.name',
|
|
|
|
'operator' => Rule::OPERATOR_IS,
|
|
|
|
'value' => 'Bob Dylan',
|
|
|
|
]);
|
|
|
|
|
|
|
|
$this->postAsUser('api/playlist', [
|
|
|
|
'name' => 'Smart Foo Bar',
|
|
|
|
'rules' => [$rule->toArray()],
|
|
|
|
], $user);
|
|
|
|
|
|
|
|
/** @var Playlist $playlist */
|
|
|
|
$playlist = Playlist::orderBy('id', 'desc')->first();
|
|
|
|
|
|
|
|
self::assertSame('Smart Foo Bar', $playlist->name);
|
|
|
|
self::assertTrue($playlist->user->is($user));
|
|
|
|
self::assertTrue($playlist->is_smart);
|
|
|
|
self::assertCount(1, $playlist->rules);
|
|
|
|
self::assertTrue(Rule::create($playlist->rules[0])->equals($rule));
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testCreatingSmartPlaylistIgnoresSongs(): void
|
|
|
|
{
|
|
|
|
/** @var User $user */
|
|
|
|
$user = User::factory()->create();
|
|
|
|
|
|
|
|
$this->postAsUser('api/playlist', [
|
|
|
|
'name' => 'Smart Foo Bar',
|
|
|
|
'rules' => [
|
|
|
|
Rule::create([
|
|
|
|
'model' => 'artist.name',
|
|
|
|
'operator' => Rule::OPERATOR_IS,
|
|
|
|
'value' => 'Bob Dylan',
|
|
|
|
])->toArray(),
|
|
|
|
],
|
|
|
|
'songs' => Song::orderBy('id')->take(3)->get()->pluck('id')->toArray(),
|
|
|
|
], $user);
|
|
|
|
|
|
|
|
/** @var Playlist $playlist */
|
|
|
|
$playlist = Playlist::orderBy('id', 'desc')->first();
|
|
|
|
|
|
|
|
self::assertSame('Smart Foo Bar', $playlist->name);
|
|
|
|
self::assertEmpty($playlist->songs);
|
|
|
|
}
|
|
|
|
|
2020-09-06 20:21:39 +02:00
|
|
|
public function testUpdatePlaylistName(): void
|
2015-12-13 12:42:28 +08:00
|
|
|
{
|
2020-09-06 20:21:39 +02:00
|
|
|
/** @var User $user */
|
2020-11-14 17:57:25 +01:00
|
|
|
$user = User::factory()->create();
|
2015-12-13 12:42:28 +08:00
|
|
|
|
2020-09-06 20:21:39 +02:00
|
|
|
/** @var Playlist $playlist */
|
2020-11-14 17:57:25 +01:00
|
|
|
$playlist = Playlist::factory()->create([
|
2015-12-13 12:42:28 +08:00
|
|
|
'user_id' => $user->id,
|
2020-09-06 20:21:39 +02:00
|
|
|
'name' => 'Foo',
|
2015-12-13 12:42:28 +08:00
|
|
|
]);
|
|
|
|
|
2020-09-06 20:21:39 +02:00
|
|
|
$this->putAsUser("api/playlist/{$playlist->id}", ['name' => 'Bar'], $user);
|
2015-12-13 12:42:28 +08:00
|
|
|
|
2020-09-06 20:21:39 +02:00
|
|
|
self::assertSame('Bar', $playlist->refresh()->name);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testNonOwnerCannotUpdatePlaylist(): void
|
|
|
|
{
|
|
|
|
/** @var Playlist $playlist */
|
2020-11-14 17:57:25 +01:00
|
|
|
$playlist = Playlist::factory()->create([
|
2020-09-06 20:21:39 +02:00
|
|
|
'name' => 'Foo',
|
2015-12-13 12:42:28 +08:00
|
|
|
]);
|
|
|
|
|
2020-09-06 20:21:39 +02:00
|
|
|
$response = $this->putAsUser("api/playlist/{$playlist->id}", ['name' => 'Qux']);
|
|
|
|
$response->assertStatus(403);
|
2015-12-13 12:42:28 +08:00
|
|
|
}
|
|
|
|
|
2020-09-06 20:21:39 +02:00
|
|
|
public function testSyncPlaylist(): void
|
2015-12-13 12:42:28 +08:00
|
|
|
{
|
2020-09-06 20:21:39 +02:00
|
|
|
/** @var User $user */
|
2020-11-14 17:57:25 +01:00
|
|
|
$user = User::factory()->create();
|
2015-12-13 12:42:28 +08:00
|
|
|
|
2020-09-06 20:21:39 +02:00
|
|
|
/** @var Playlist $playlist */
|
2020-11-14 17:57:25 +01:00
|
|
|
$playlist = Playlist::factory()->create([
|
2015-12-13 12:42:28 +08:00
|
|
|
'user_id' => $user->id,
|
|
|
|
]);
|
|
|
|
|
2021-06-05 12:47:56 +02:00
|
|
|
/** @var array<Song>|Collection $songs */
|
2015-12-13 12:42:28 +08:00
|
|
|
$songs = Song::orderBy('id')->take(4)->get();
|
2016-06-04 23:15:14 +08:00
|
|
|
$playlist->songs()->attach($songs->pluck('id')->toArray());
|
2015-12-13 12:42:28 +08:00
|
|
|
|
2021-06-05 12:47:56 +02:00
|
|
|
/** @var Song $removedSong */
|
2015-12-13 12:42:28 +08:00
|
|
|
$removedSong = $songs->pop();
|
|
|
|
|
2016-09-26 14:30:00 +08:00
|
|
|
$this->putAsUser("api/playlist/{$playlist->id}/sync", [
|
2020-01-08 15:21:29 +01:00
|
|
|
'songs' => $songs->pluck('id')->toArray(),
|
|
|
|
], $user);
|
2015-12-13 12:42:28 +08:00
|
|
|
|
|
|
|
// We should still see the first 3 songs, but not the removed one
|
|
|
|
foreach ($songs as $song) {
|
2020-09-06 20:21:39 +02:00
|
|
|
self::assertDatabaseHas('playlist_song', [
|
2015-12-13 12:42:28 +08:00
|
|
|
'playlist_id' => $playlist->id,
|
|
|
|
'song_id' => $song->id,
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
2020-09-06 20:21:39 +02:00
|
|
|
self::assertDatabaseMissing('playlist_song', [
|
2015-12-13 12:42:28 +08:00
|
|
|
'playlist_id' => $playlist->id,
|
|
|
|
'song_id' => $removedSong->id,
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
2020-09-06 20:21:39 +02:00
|
|
|
public function testDeletePlaylist(): void
|
2015-12-13 12:42:28 +08:00
|
|
|
{
|
2020-09-06 20:21:39 +02:00
|
|
|
/** @var User $user */
|
2020-11-14 17:57:25 +01:00
|
|
|
$user = User::factory()->create();
|
2015-12-13 12:42:28 +08:00
|
|
|
|
2020-09-06 20:21:39 +02:00
|
|
|
/** @var Playlist $playlist */
|
2020-11-14 17:57:25 +01:00
|
|
|
$playlist = Playlist::factory()->create([
|
2015-12-13 12:42:28 +08:00
|
|
|
'user_id' => $user->id,
|
|
|
|
]);
|
|
|
|
|
2020-09-06 20:21:39 +02:00
|
|
|
$this->deleteAsUser("api/playlist/{$playlist->id}", [], $user);
|
|
|
|
self::assertDatabaseMissing('playlists', ['id' => $playlist->id]);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testNonOwnerCannotDeletePlaylist(): void
|
|
|
|
{
|
|
|
|
/** @var Playlist $playlist */
|
2020-11-14 17:57:25 +01:00
|
|
|
$playlist = Playlist::factory()->create();
|
2015-12-13 12:42:28 +08:00
|
|
|
|
2020-09-06 20:21:39 +02:00
|
|
|
$this->deleteAsUser("api/playlist/{$playlist->id}")
|
|
|
|
->assertStatus(403);
|
2015-12-13 12:42:28 +08:00
|
|
|
}
|
2017-12-02 17:05:40 +01:00
|
|
|
|
2020-09-06 20:21:39 +02:00
|
|
|
public function testGetPlaylist(): void
|
2017-12-02 17:05:40 +01:00
|
|
|
{
|
2020-09-06 20:21:39 +02:00
|
|
|
/** @var User $user */
|
2020-11-14 17:57:25 +01:00
|
|
|
$user = User::factory()->create();
|
2017-12-02 17:05:40 +01:00
|
|
|
|
2020-09-06 20:21:39 +02:00
|
|
|
/** @var Playlist $playlist */
|
2020-11-14 17:57:25 +01:00
|
|
|
$playlist = Playlist::factory()->create([
|
2017-12-02 17:05:40 +01:00
|
|
|
'user_id' => $user->id,
|
|
|
|
]);
|
|
|
|
|
2020-11-14 17:57:25 +01:00
|
|
|
$songs = Song::factory(2)->create();
|
2017-12-02 17:05:40 +01:00
|
|
|
$playlist->songs()->saveMany($songs);
|
|
|
|
|
|
|
|
$this->getAsUser("api/playlist/{$playlist->id}/songs", $user)
|
2020-09-06 20:21:39 +02:00
|
|
|
->assertJson($songs->pluck('id')->all());
|
2017-12-02 17:05:40 +01:00
|
|
|
}
|
2015-12-13 12:42:28 +08:00
|
|
|
}
|