koel/tests/Feature/PlaylistTest.php

144 lines
3.9 KiB
PHP
Raw Normal View History

2015-12-13 04:42:28 +00:00
<?php
2017-02-14 06:53:02 +00:00
namespace Tests\Feature;
2015-12-13 04:42:28 +00:00
use App\Models\Playlist;
2015-12-14 13:22:39 +00:00
use App\Models\Song;
use App\Models\User;
2015-12-13 04:42:28 +00:00
2017-08-05 16:56:11 +00:00
class PlaylistTest extends TestCase
2015-12-13 04:42:28 +00:00
{
2019-07-22 07:03:23 +00:00
public function setUp(): void
2015-12-13 04:42:28 +00:00
{
parent::setUp();
static::createSampleMediaSet();
2015-12-13 04:42:28 +00:00
}
2019-07-22 07:03:23 +00:00
public function testCreatingPlaylist(): void
2015-12-13 04:42:28 +00:00
{
2020-09-06 18:21:39 +00:00
/** @var User $user */
2015-12-13 04:42:28 +00:00
$user = factory(User::class)->create();
$songs = Song::orderBy('id')->take(3)->get();
2016-09-26 06:30:00 +00:00
$this->postAsUser('api/playlist', [
2018-11-25 21:21:46 +00:00
'name' => 'Foo Bar',
'songs' => $songs->pluck('id')->toArray(),
'rules' => [],
], $user);
2015-12-13 04:42:28 +00:00
2020-09-06 18:21:39 +00:00
self::assertDatabaseHas('playlists', [
2015-12-13 04:42:28 +00:00
'user_id' => $user->id,
'name' => 'Foo Bar',
]);
$playlist = Playlist::orderBy('id', 'desc')->first();
foreach ($songs as $song) {
2020-09-06 18:21:39 +00:00
self::assertDatabaseHas('playlist_song', [
2015-12-13 04:42:28 +00:00
'playlist_id' => $playlist->id,
'song_id' => $song->id,
]);
}
}
2020-09-06 18:21:39 +00:00
public function testUpdatePlaylistName(): void
2015-12-13 04:42:28 +00:00
{
2020-09-06 18:21:39 +00:00
/** @var User $user */
2015-12-13 04:42:28 +00:00
$user = factory(User::class)->create();
2020-09-06 18:21:39 +00:00
/** @var Playlist $playlist */
2015-12-13 04:42:28 +00:00
$playlist = factory(Playlist::class)->create([
'user_id' => $user->id,
2020-09-06 18:21:39 +00:00
'name' => 'Foo',
2015-12-13 04:42:28 +00:00
]);
2020-09-06 18:21:39 +00:00
$this->putAsUser("api/playlist/{$playlist->id}", ['name' => 'Bar'], $user);
2015-12-13 04:42:28 +00:00
2020-09-06 18:21:39 +00:00
self::assertSame('Bar', $playlist->refresh()->name);
}
public function testNonOwnerCannotUpdatePlaylist(): void
{
/** @var Playlist $playlist */
$playlist = factory(Playlist::class)->create([
'name' => 'Foo',
2015-12-13 04:42:28 +00:00
]);
2020-09-06 18:21:39 +00:00
$response = $this->putAsUser("api/playlist/{$playlist->id}", ['name' => 'Qux']);
$response->assertStatus(403);
2015-12-13 04:42:28 +00:00
}
2020-09-06 18:21:39 +00:00
public function testSyncPlaylist(): void
2015-12-13 04:42:28 +00:00
{
2020-09-06 18:21:39 +00:00
/** @var User $user */
2015-12-13 04:42:28 +00:00
$user = factory(User::class)->create();
2020-09-06 18:21:39 +00:00
/** @var Playlist $playlist */
2015-12-13 04:42:28 +00:00
$playlist = factory(Playlist::class)->create([
'user_id' => $user->id,
]);
$songs = Song::orderBy('id')->take(4)->get();
2016-06-04 15:15:14 +00:00
$playlist->songs()->attach($songs->pluck('id')->toArray());
2015-12-13 04:42:28 +00:00
$removedSong = $songs->pop();
2016-09-26 06:30:00 +00:00
$this->putAsUser("api/playlist/{$playlist->id}/sync", [
'songs' => $songs->pluck('id')->toArray(),
], $user);
2015-12-13 04:42:28 +00:00
// We should still see the first 3 songs, but not the removed one
foreach ($songs as $song) {
2020-09-06 18:21:39 +00:00
self::assertDatabaseHas('playlist_song', [
2015-12-13 04:42:28 +00:00
'playlist_id' => $playlist->id,
'song_id' => $song->id,
]);
}
2020-09-06 18:21:39 +00:00
self::assertDatabaseMissing('playlist_song', [
2015-12-13 04:42:28 +00:00
'playlist_id' => $playlist->id,
'song_id' => $removedSong->id,
]);
}
2020-09-06 18:21:39 +00:00
public function testDeletePlaylist(): void
2015-12-13 04:42:28 +00:00
{
2020-09-06 18:21:39 +00:00
/** @var User $user */
2015-12-13 04:42:28 +00:00
$user = factory(User::class)->create();
2020-09-06 18:21:39 +00:00
/** @var Playlist $playlist */
2015-12-13 04:42:28 +00:00
$playlist = factory(Playlist::class)->create([
'user_id' => $user->id,
]);
2020-09-06 18:21:39 +00:00
$this->deleteAsUser("api/playlist/{$playlist->id}", [], $user);
self::assertDatabaseMissing('playlists', ['id' => $playlist->id]);
}
public function testNonOwnerCannotDeletePlaylist(): void
{
/** @var Playlist $playlist */
$playlist = factory(Playlist::class)->create();
2015-12-13 04:42:28 +00:00
2020-09-06 18:21:39 +00:00
$this->deleteAsUser("api/playlist/{$playlist->id}")
->assertStatus(403);
2015-12-13 04:42:28 +00:00
}
2020-09-06 18:21:39 +00:00
public function testGetPlaylist(): void
{
2020-09-06 18:21:39 +00:00
/** @var User $user */
$user = factory(User::class)->create();
2020-09-06 18:21:39 +00:00
/** @var Playlist $playlist */
$playlist = factory(Playlist::class)->create([
'user_id' => $user->id,
]);
$songs = factory(Song::class, 2)->create();
$playlist->songs()->saveMany($songs);
$this->getAsUser("api/playlist/{$playlist->id}/songs", $user)
2020-09-06 18:21:39 +00:00
->assertJson($songs->pluck('id')->all());
}
2015-12-13 04:42:28 +00:00
}