koel/tests/Feature/PlaylistTest.php

151 lines
4.1 KiB
PHP
Raw Normal View History

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;
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();
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 */
$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,
]);
}
}
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 */
$user = User::factory()->create();
2015-12-13 12:42:28 +08:00
2020-09-06 20:21:39 +02:00
/** @var Playlist $playlist */
$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 */
$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 */
$user = User::factory()->create();
2015-12-13 12:42:28 +08:00
2020-09-06 20:21:39 +02:00
/** @var Playlist $playlist */
$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", [
'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 */
$user = User::factory()->create();
2015-12-13 12:42:28 +08:00
2020-09-06 20:21:39 +02:00
/** @var Playlist $playlist */
$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 */
$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
}
2020-09-06 20:21:39 +02:00
public function testGetPlaylist(): void
{
2020-09-06 20:21:39 +02:00
/** @var User $user */
$user = User::factory()->create();
2020-09-06 20:21:39 +02:00
/** @var Playlist $playlist */
$playlist = Playlist::factory()->create([
'user_id' => $user->id,
]);
$songs = Song::factory(2)->create();
$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());
}
2015-12-13 12:42:28 +08:00
}