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;
|
2021-10-08 16:23:45 +00:00
|
|
|
use App\Values\SmartPlaylistRule;
|
2021-06-05 10:47:56 +00:00
|
|
|
use Illuminate\Support\Collection;
|
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();
|
2020-11-14 16:57:25 +00:00
|
|
|
|
2020-04-27 18:55:12 +00:00
|
|
|
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 */
|
2020-11-14 16:57:25 +00:00
|
|
|
$user = User::factory()->create();
|
2021-06-05 10:47:56 +00:00
|
|
|
|
|
|
|
/** @var array<Song>|Collection $songs */
|
2015-12-13 04:42:28 +00:00
|
|
|
$songs = Song::orderBy('id')->take(3)->get();
|
|
|
|
|
2022-07-27 08:49:33 +00:00
|
|
|
$response = $this->postAs('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
|
|
|
|
2021-10-08 16:23:45 +00:00
|
|
|
$response->assertOk();
|
2015-12-13 04:42:28 +00:00
|
|
|
|
2021-06-05 10:47:56 +00:00
|
|
|
/** @var Playlist $playlist */
|
2015-12-13 04:42:28 +00:00
|
|
|
$playlist = Playlist::orderBy('id', 'desc')->first();
|
|
|
|
|
2021-10-08 16:23:45 +00:00
|
|
|
self::assertSame('Foo Bar', $playlist->name);
|
|
|
|
self::assertTrue($playlist->user->is($user));
|
|
|
|
self::assertEqualsCanonicalizing($songs->pluck('id')->all(), $playlist->songs->pluck('id')->all());
|
2015-12-13 04:42:28 +00:00
|
|
|
}
|
|
|
|
|
2021-10-08 10:19:44 +00:00
|
|
|
public function testCreatingSmartPlaylist(): void
|
|
|
|
{
|
|
|
|
/** @var User $user */
|
|
|
|
$user = User::factory()->create();
|
|
|
|
|
2021-10-08 16:23:45 +00:00
|
|
|
$rule = SmartPlaylistRule::create([
|
2021-10-08 10:19:44 +00:00
|
|
|
'model' => 'artist.name',
|
2021-10-08 16:23:45 +00:00
|
|
|
'operator' => SmartPlaylistRule::OPERATOR_IS,
|
|
|
|
'value' => ['Bob Dylan'],
|
2021-10-08 10:19:44 +00:00
|
|
|
]);
|
|
|
|
|
2022-07-27 08:49:33 +00:00
|
|
|
$this->postAs('api/playlist', [
|
2021-10-08 10:19:44 +00:00
|
|
|
'name' => 'Smart Foo Bar',
|
2021-10-08 16:23:45 +00:00
|
|
|
'rules' => [
|
|
|
|
[
|
|
|
|
'id' => 12345,
|
|
|
|
'rules' => [$rule->toArray()],
|
|
|
|
],
|
|
|
|
],
|
2021-10-08 10:19:44 +00:00
|
|
|
], $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);
|
2021-10-08 16:23:45 +00:00
|
|
|
self::assertCount(1, $playlist->rule_groups);
|
|
|
|
self::assertTrue($rule->equals($playlist->rule_groups[0]->rules[0]));
|
2021-10-08 10:19:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testCreatingSmartPlaylistIgnoresSongs(): void
|
|
|
|
{
|
2022-07-27 08:49:33 +00:00
|
|
|
$this->postAs('api/playlist', [
|
2021-10-08 10:19:44 +00:00
|
|
|
'name' => 'Smart Foo Bar',
|
|
|
|
'rules' => [
|
2021-10-10 18:05:51 +00:00
|
|
|
[
|
|
|
|
'id' => 12345,
|
|
|
|
'rules' => [
|
|
|
|
SmartPlaylistRule::create([
|
|
|
|
'model' => 'artist.name',
|
|
|
|
'operator' => SmartPlaylistRule::OPERATOR_IS,
|
|
|
|
'value' => ['Bob Dylan'],
|
|
|
|
])->toArray(),
|
|
|
|
],
|
|
|
|
],
|
2021-10-08 10:19:44 +00:00
|
|
|
],
|
2021-10-08 16:23:45 +00:00
|
|
|
'songs' => Song::orderBy('id')->take(3)->get()->pluck('id')->all(),
|
2021-10-10 18:05:51 +00:00
|
|
|
]);
|
2021-10-08 10:19:44 +00:00
|
|
|
|
|
|
|
/** @var Playlist $playlist */
|
|
|
|
$playlist = Playlist::orderBy('id', 'desc')->first();
|
|
|
|
|
|
|
|
self::assertSame('Smart Foo Bar', $playlist->name);
|
|
|
|
self::assertEmpty($playlist->songs);
|
|
|
|
}
|
|
|
|
|
2021-10-10 18:05:51 +00:00
|
|
|
public function testCreatingPlaylistWithNonExistentSongsFails(): void
|
|
|
|
{
|
2022-07-27 15:32:36 +00:00
|
|
|
$this->postAs('api/playlist', [
|
2021-10-10 18:05:51 +00:00
|
|
|
'name' => 'Foo Bar',
|
|
|
|
'rules' => [],
|
|
|
|
'songs' => ['foo'],
|
2022-07-27 15:32:36 +00:00
|
|
|
])
|
|
|
|
->assertUnprocessable();
|
2021-10-10 18:05:51 +00:00
|
|
|
}
|
|
|
|
|
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 Playlist $playlist */
|
2022-07-27 15:32:36 +00:00
|
|
|
$playlist = Playlist::factory()->create(['name' => 'Foo']);
|
2015-12-13 04:42:28 +00:00
|
|
|
|
2022-07-27 15:32:36 +00:00
|
|
|
$this->putAs("api/playlist/$playlist->id", ['name' => 'Bar'], $playlist->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 */
|
2022-07-27 15:32:36 +00:00
|
|
|
$playlist = Playlist::factory()->create(['name' => 'Foo']);
|
2015-12-13 04:42:28 +00:00
|
|
|
|
2022-07-27 15:32:36 +00:00
|
|
|
$this->putAs("api/playlist/$playlist->id", ['name' => 'Qux'])->assertForbidden();
|
2015-12-13 04:42:28 +00:00
|
|
|
}
|
|
|
|
|
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 Playlist $playlist */
|
2022-07-27 15:32:36 +00:00
|
|
|
$playlist = Playlist::factory()->create();
|
2015-12-13 04:42:28 +00:00
|
|
|
|
2022-07-27 15:32:36 +00:00
|
|
|
$this->deleteAs("api/playlist/$playlist->id", [], $playlist->user);
|
|
|
|
|
|
|
|
self::assertModelMissing($playlist);
|
2020-09-06 18:21:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testNonOwnerCannotDeletePlaylist(): void
|
|
|
|
{
|
|
|
|
/** @var Playlist $playlist */
|
2020-11-14 16:57:25 +00:00
|
|
|
$playlist = Playlist::factory()->create();
|
2015-12-13 04:42:28 +00:00
|
|
|
|
2022-07-27 15:32:36 +00:00
|
|
|
$this->deleteAs("api/playlist/$playlist->id")->assertForbidden();
|
|
|
|
|
|
|
|
self::assertModelExists($playlist);
|
2015-12-13 04:42:28 +00:00
|
|
|
}
|
|
|
|
}
|