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;
|
2018-08-23 06:51:16 +00:00
|
|
|
use Exception;
|
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
|
|
|
{
|
2018-08-23 06:51:16 +00:00
|
|
|
/**
|
|
|
|
* @throws Exception
|
|
|
|
*/
|
2019-07-22 07:03:23 +00:00
|
|
|
public function setUp(): void
|
2015-12-13 04:42:28 +00:00
|
|
|
{
|
|
|
|
parent::setUp();
|
|
|
|
$this->createSampleMediaSet();
|
|
|
|
}
|
|
|
|
|
2019-07-22 07:03:23 +00:00
|
|
|
public function testCreatingPlaylist(): void
|
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
|
|
|
|
|
|
|
$this->seeInDatabase('playlists', [
|
|
|
|
'user_id' => $user->id,
|
|
|
|
'name' => 'Foo Bar',
|
|
|
|
]);
|
|
|
|
|
|
|
|
$playlist = Playlist::orderBy('id', 'desc')->first();
|
|
|
|
|
|
|
|
foreach ($songs as $song) {
|
|
|
|
$this->seeInDatabase('playlist_song', [
|
|
|
|
'playlist_id' => $playlist->id,
|
|
|
|
'song_id' => $song->id,
|
|
|
|
]);
|
|
|
|
}
|
2016-01-26 06:32:29 +00:00
|
|
|
|
2016-09-26 06:30:00 +00:00
|
|
|
$this->getAsUser('api/playlist', $user)
|
2016-01-26 06:32:29 +00:00
|
|
|
->seeJson([
|
|
|
|
'id' => $playlist->id,
|
|
|
|
'name' => 'Foo Bar',
|
|
|
|
]);
|
2015-12-13 04:42:28 +00:00
|
|
|
}
|
|
|
|
|
2017-08-05 18:55:02 +00:00
|
|
|
/** @test */
|
2019-07-22 07:03:23 +00:00
|
|
|
public function user_can_update_a_playlists_name(): void
|
2015-12-13 04:42:28 +00:00
|
|
|
{
|
|
|
|
$user = factory(User::class)->create();
|
|
|
|
|
|
|
|
$playlist = factory(Playlist::class)->create([
|
|
|
|
'user_id' => $user->id,
|
|
|
|
]);
|
|
|
|
|
2016-09-26 06:30:00 +00:00
|
|
|
$this->putAsUser("api/playlist/{$playlist->id}", ['name' => 'Foo Bar'], $user);
|
2015-12-13 04:42:28 +00:00
|
|
|
|
|
|
|
$this->seeInDatabase('playlists', [
|
|
|
|
'user_id' => $user->id,
|
|
|
|
'name' => 'Foo Bar',
|
|
|
|
]);
|
|
|
|
|
|
|
|
// Other users can't modify it
|
2016-09-26 06:30:00 +00:00
|
|
|
$this->putAsUser("api/playlist/{$playlist->id}", ['name' => 'Foo Bar'])
|
2015-12-14 16:27:26 +00:00
|
|
|
->seeStatusCode(403);
|
2015-12-13 04:42:28 +00:00
|
|
|
}
|
|
|
|
|
2017-08-05 18:55:02 +00:00
|
|
|
/** @test */
|
2019-07-22 07:03:23 +00:00
|
|
|
public function playlists_can_be_synced(): void
|
2015-12-13 04:42:28 +00:00
|
|
|
{
|
|
|
|
$user = factory(User::class)->create();
|
|
|
|
|
|
|
|
$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", [
|
2016-06-04 15:15:14 +00:00
|
|
|
'songs' => $songs->pluck('id')->toArray(),
|
2015-12-14 16:27:26 +00:00
|
|
|
])
|
|
|
|
->seeStatusCode(403);
|
|
|
|
|
2016-09-26 06:30:00 +00:00
|
|
|
$this->putAsUser("api/playlist/{$playlist->id}/sync", [
|
2016-06-04 15:15:14 +00:00
|
|
|
'songs' => $songs->pluck('id')->toArray(),
|
2016-09-26 06:30:00 +00:00
|
|
|
], $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) {
|
|
|
|
$this->seeInDatabase('playlist_song', [
|
|
|
|
'playlist_id' => $playlist->id,
|
|
|
|
'song_id' => $song->id,
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->notSeeInDatabase('playlist_song', [
|
|
|
|
'playlist_id' => $playlist->id,
|
|
|
|
'song_id' => $removedSong->id,
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
2017-08-05 18:55:02 +00:00
|
|
|
/** @test */
|
2019-07-22 07:03:23 +00:00
|
|
|
public function user_can_delete_a_playlist(): void
|
2015-12-13 04:42:28 +00:00
|
|
|
{
|
|
|
|
$user = factory(User::class)->create();
|
|
|
|
|
|
|
|
$playlist = factory(Playlist::class)->create([
|
|
|
|
'user_id' => $user->id,
|
|
|
|
]);
|
|
|
|
|
2016-09-26 06:30:00 +00:00
|
|
|
$this->deleteAsUser("api/playlist/{$playlist->id}")
|
2015-12-14 16:27:26 +00:00
|
|
|
->seeStatusCode(403);
|
2015-12-13 04:42:28 +00:00
|
|
|
|
2016-09-26 06:30:00 +00:00
|
|
|
$this->deleteAsUser("api/playlist/{$playlist->id}", [], $user)
|
2015-12-14 16:27:26 +00:00
|
|
|
->notSeeInDatabase('playlists', ['id' => $playlist->id]);
|
2015-12-13 04:42:28 +00:00
|
|
|
}
|
2017-12-02 16:05:40 +00:00
|
|
|
|
|
|
|
/** @test */
|
2019-07-22 07:03:23 +00:00
|
|
|
public function playlist_content_can_be_retrieved(): void
|
2017-12-02 16:05:40 +00:00
|
|
|
{
|
|
|
|
$user = factory(User::class)->create();
|
|
|
|
|
|
|
|
$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)
|
|
|
|
->seeJson($songs->pluck('id')->all());
|
|
|
|
}
|
2015-12-13 04:42:28 +00:00
|
|
|
}
|