mirror of
https://github.com/koel/koel
synced 2024-11-24 21:23:06 +00:00
144 lines
3.9 KiB
PHP
144 lines
3.9 KiB
PHP
<?php
|
|
|
|
namespace Tests\Feature;
|
|
|
|
use App\Models\Playlist;
|
|
use App\Models\Song;
|
|
use App\Models\User;
|
|
|
|
class PlaylistTest extends TestCase
|
|
{
|
|
public function setUp(): void
|
|
{
|
|
parent::setUp();
|
|
|
|
static::createSampleMediaSet();
|
|
}
|
|
|
|
public function testCreatingPlaylist(): void
|
|
{
|
|
/** @var User $user */
|
|
$user = User::factory()->create();
|
|
$songs = Song::orderBy('id')->take(3)->get();
|
|
|
|
$this->postAsUser('api/playlist', [
|
|
'name' => 'Foo Bar',
|
|
'songs' => $songs->pluck('id')->toArray(),
|
|
'rules' => [],
|
|
], $user);
|
|
|
|
self::assertDatabaseHas('playlists', [
|
|
'user_id' => $user->id,
|
|
'name' => 'Foo Bar',
|
|
]);
|
|
|
|
$playlist = Playlist::orderBy('id', 'desc')->first();
|
|
|
|
foreach ($songs as $song) {
|
|
self::assertDatabaseHas('playlist_song', [
|
|
'playlist_id' => $playlist->id,
|
|
'song_id' => $song->id,
|
|
]);
|
|
}
|
|
}
|
|
|
|
public function testUpdatePlaylistName(): void
|
|
{
|
|
/** @var User $user */
|
|
$user = User::factory()->create();
|
|
|
|
/** @var Playlist $playlist */
|
|
$playlist = Playlist::factory()->create([
|
|
'user_id' => $user->id,
|
|
'name' => 'Foo',
|
|
]);
|
|
|
|
$this->putAsUser("api/playlist/{$playlist->id}", ['name' => 'Bar'], $user);
|
|
|
|
self::assertSame('Bar', $playlist->refresh()->name);
|
|
}
|
|
|
|
public function testNonOwnerCannotUpdatePlaylist(): void
|
|
{
|
|
/** @var Playlist $playlist */
|
|
$playlist = Playlist::factory()->create([
|
|
'name' => 'Foo',
|
|
]);
|
|
|
|
$response = $this->putAsUser("api/playlist/{$playlist->id}", ['name' => 'Qux']);
|
|
$response->assertStatus(403);
|
|
}
|
|
|
|
public function testSyncPlaylist(): void
|
|
{
|
|
/** @var User $user */
|
|
$user = User::factory()->create();
|
|
|
|
/** @var Playlist $playlist */
|
|
$playlist = Playlist::factory()->create([
|
|
'user_id' => $user->id,
|
|
]);
|
|
|
|
$songs = Song::orderBy('id')->take(4)->get();
|
|
$playlist->songs()->attach($songs->pluck('id')->toArray());
|
|
|
|
$removedSong = $songs->pop();
|
|
|
|
$this->putAsUser("api/playlist/{$playlist->id}/sync", [
|
|
'songs' => $songs->pluck('id')->toArray(),
|
|
], $user);
|
|
|
|
// We should still see the first 3 songs, but not the removed one
|
|
foreach ($songs as $song) {
|
|
self::assertDatabaseHas('playlist_song', [
|
|
'playlist_id' => $playlist->id,
|
|
'song_id' => $song->id,
|
|
]);
|
|
}
|
|
|
|
self::assertDatabaseMissing('playlist_song', [
|
|
'playlist_id' => $playlist->id,
|
|
'song_id' => $removedSong->id,
|
|
]);
|
|
}
|
|
|
|
public function testDeletePlaylist(): void
|
|
{
|
|
/** @var User $user */
|
|
$user = User::factory()->create();
|
|
|
|
/** @var Playlist $playlist */
|
|
$playlist = Playlist::factory()->create([
|
|
'user_id' => $user->id,
|
|
]);
|
|
|
|
$this->deleteAsUser("api/playlist/{$playlist->id}", [], $user);
|
|
self::assertDatabaseMissing('playlists', ['id' => $playlist->id]);
|
|
}
|
|
|
|
public function testNonOwnerCannotDeletePlaylist(): void
|
|
{
|
|
/** @var Playlist $playlist */
|
|
$playlist = Playlist::factory()->create();
|
|
|
|
$this->deleteAsUser("api/playlist/{$playlist->id}")
|
|
->assertStatus(403);
|
|
}
|
|
|
|
public function testGetPlaylist(): void
|
|
{
|
|
/** @var User $user */
|
|
$user = User::factory()->create();
|
|
|
|
/** @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)
|
|
->assertJson($songs->pluck('id')->all());
|
|
}
|
|
}
|