2015-12-13 04:42:28 +00:00
|
|
|
<?php
|
|
|
|
|
2017-02-14 06:53:02 +00:00
|
|
|
namespace Tests\Feature;
|
|
|
|
|
2024-01-25 16:21:26 +00:00
|
|
|
use App\Http\Resources\PlaylistResource;
|
2015-12-13 04:42:28 +00:00
|
|
|
use App\Models\Playlist;
|
2015-12-14 13:22:39 +00:00
|
|
|
use App\Models\Song;
|
2021-10-08 16:23:45 +00:00
|
|
|
use App\Values\SmartPlaylistRule;
|
2021-06-05 10:47:56 +00:00
|
|
|
use Illuminate\Support\Collection;
|
2024-01-09 18:34:40 +00:00
|
|
|
use Tests\TestCase;
|
2015-12-13 04:42:28 +00:00
|
|
|
|
2024-01-11 12:41:33 +00:00
|
|
|
use function Tests\create_user;
|
|
|
|
|
2017-08-05 16:56:11 +00:00
|
|
|
class PlaylistTest extends TestCase
|
2015-12-13 04:42:28 +00:00
|
|
|
{
|
2023-08-16 12:56:31 +00:00
|
|
|
public function testListing(): void
|
|
|
|
{
|
2024-01-11 12:41:33 +00:00
|
|
|
$user = create_user();
|
2023-08-16 12:56:31 +00:00
|
|
|
Playlist::factory()->for($user)->count(3)->create();
|
|
|
|
|
|
|
|
$this->getAs('api/playlists', $user)
|
2024-01-25 16:21:26 +00:00
|
|
|
->assertJsonStructure(['*' => PlaylistResource::JSON_STRUCTURE])
|
2023-08-16 12:56:31 +00:00
|
|
|
->assertJsonCount(3, '*');
|
|
|
|
}
|
|
|
|
|
2019-07-22 07:03:23 +00:00
|
|
|
public function testCreatingPlaylist(): void
|
2015-12-13 04:42:28 +00:00
|
|
|
{
|
2024-01-11 12:41:33 +00:00
|
|
|
$user = create_user();
|
2021-06-05 10:47:56 +00:00
|
|
|
|
|
|
|
/** @var array<Song>|Collection $songs */
|
2023-06-05 21:46:41 +00:00
|
|
|
$songs = Song::factory(4)->create();
|
2015-12-13 04:42:28 +00:00
|
|
|
|
2023-06-05 21:46:41 +00:00
|
|
|
$this->postAs('api/playlists', [
|
2018-11-25 21:21:46 +00:00
|
|
|
'name' => 'Foo Bar',
|
2023-06-05 21:46:41 +00:00
|
|
|
'songs' => $songs->pluck('id')->all(),
|
2018-11-25 21:21:46 +00:00
|
|
|
'rules' => [],
|
2023-06-05 21:46:41 +00:00
|
|
|
], $user)
|
2024-01-25 16:21:26 +00:00
|
|
|
->assertJsonStructure(PlaylistResource::JSON_STRUCTURE);
|
2015-12-13 04:42:28 +00:00
|
|
|
|
2021-06-05 10:47:56 +00:00
|
|
|
/** @var Playlist $playlist */
|
2024-01-18 11:13:05 +00:00
|
|
|
$playlist = Playlist::query()->latest()->first();
|
2015-12-13 04:42:28 +00:00
|
|
|
|
2021-10-08 16:23:45 +00:00
|
|
|
self::assertSame('Foo Bar', $playlist->name);
|
2024-01-18 11:13:05 +00:00
|
|
|
self::assertTrue($playlist->ownedBy($user));
|
2024-03-27 09:53:05 +00:00
|
|
|
self::assertNull($playlist->getFolder());
|
2021-10-08 16:23:45 +00:00
|
|
|
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
|
|
|
|
{
|
2024-01-11 12:41:33 +00:00
|
|
|
$user = create_user();
|
2021-10-08 10:19:44 +00:00
|
|
|
|
2024-01-04 21:51:32 +00:00
|
|
|
$rule = SmartPlaylistRule::make([
|
2021-10-08 10:19:44 +00:00
|
|
|
'model' => 'artist.name',
|
2024-04-18 14:11:47 +00:00
|
|
|
'operator' => 'is',
|
2021-10-08 16:23:45 +00:00
|
|
|
'value' => ['Bob Dylan'],
|
2021-10-08 10:19:44 +00:00
|
|
|
]);
|
|
|
|
|
2023-06-05 21:46:41 +00:00
|
|
|
$this->postAs('api/playlists', [
|
2021-10-08 10:19:44 +00:00
|
|
|
'name' => 'Smart Foo Bar',
|
2021-10-08 16:23:45 +00:00
|
|
|
'rules' => [
|
|
|
|
[
|
2023-06-05 21:46:41 +00:00
|
|
|
'id' => '2a4548cd-c67f-44d4-8fec-34ff75c8a026',
|
2021-10-08 16:23:45 +00:00
|
|
|
'rules' => [$rule->toArray()],
|
|
|
|
],
|
|
|
|
],
|
2024-01-25 16:21:26 +00:00
|
|
|
], $user)->assertJsonStructure(PlaylistResource::JSON_STRUCTURE);
|
2021-10-08 10:19:44 +00:00
|
|
|
|
|
|
|
/** @var Playlist $playlist */
|
2024-01-18 11:13:05 +00:00
|
|
|
$playlist = Playlist::query()->latest()->first();
|
2021-10-08 10:19:44 +00:00
|
|
|
|
|
|
|
self::assertSame('Smart Foo Bar', $playlist->name);
|
2024-01-18 11:13:05 +00:00
|
|
|
self::assertTrue($playlist->ownedBy($user));
|
2021-10-08 10:19:44 +00:00
|
|
|
self::assertTrue($playlist->is_smart);
|
2021-10-08 16:23:45 +00:00
|
|
|
self::assertCount(1, $playlist->rule_groups);
|
2024-03-27 09:53:05 +00:00
|
|
|
self::assertNull($playlist->getFolder());
|
2021-10-08 16:23:45 +00:00
|
|
|
self::assertTrue($rule->equals($playlist->rule_groups[0]->rules[0]));
|
2021-10-08 10:19:44 +00:00
|
|
|
}
|
|
|
|
|
2023-06-05 21:46:41 +00:00
|
|
|
public function testCreatingSmartPlaylistFailsIfSongsProvided(): void
|
2021-10-08 10:19:44 +00:00
|
|
|
{
|
2023-06-05 21:46:41 +00:00
|
|
|
$this->postAs('api/playlists', [
|
2021-10-08 10:19:44 +00:00
|
|
|
'name' => 'Smart Foo Bar',
|
|
|
|
'rules' => [
|
2021-10-10 18:05:51 +00:00
|
|
|
[
|
2023-06-05 21:46:41 +00:00
|
|
|
'id' => '2a4548cd-c67f-44d4-8fec-34ff75c8a026',
|
2021-10-10 18:05:51 +00:00
|
|
|
'rules' => [
|
2024-01-04 21:51:32 +00:00
|
|
|
SmartPlaylistRule::make([
|
2021-10-10 18:05:51 +00:00
|
|
|
'model' => 'artist.name',
|
2024-04-18 14:11:47 +00:00
|
|
|
'operator' => 'is',
|
2021-10-10 18:05:51 +00:00
|
|
|
'value' => ['Bob Dylan'],
|
|
|
|
])->toArray(),
|
|
|
|
],
|
|
|
|
],
|
2021-10-08 10:19:44 +00:00
|
|
|
],
|
2023-06-05 21:46:41 +00:00
|
|
|
'songs' => Song::factory(3)->create()->pluck('id')->all(),
|
2022-11-27 15:29:29 +00:00
|
|
|
])->assertUnprocessable();
|
2021-10-08 10:19:44 +00:00
|
|
|
}
|
|
|
|
|
2021-10-10 18:05:51 +00:00
|
|
|
public function testCreatingPlaylistWithNonExistentSongsFails(): void
|
|
|
|
{
|
2023-06-05 21:46:41 +00:00
|
|
|
$this->postAs('api/playlists', [
|
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
|
|
|
|
2023-06-05 21:46:41 +00:00
|
|
|
$this->putAs("api/playlists/$playlist->id", ['name' => 'Bar'], $playlist->user)
|
2024-01-25 16:21:26 +00:00
|
|
|
->assertJsonStructure(PlaylistResource::JSON_STRUCTURE);
|
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
|
|
|
|
2023-06-05 21:46:41 +00:00
|
|
|
$this->putAs("api/playlists/$playlist->id", ['name' => 'Qux'])->assertForbidden();
|
|
|
|
self::assertSame('Foo', $playlist->refresh()->name);
|
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
|
|
|
|
2023-06-05 21:46:41 +00:00
|
|
|
$this->deleteAs("api/playlists/$playlist->id", [], $playlist->user);
|
2022-07-27 15:32:36 +00:00
|
|
|
|
|
|
|
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
|
|
|
|
2023-06-05 21:46:41 +00:00
|
|
|
$this->deleteAs("api/playlists/$playlist->id")->assertForbidden();
|
2022-07-27 15:32:36 +00:00
|
|
|
|
|
|
|
self::assertModelExists($playlist);
|
2015-12-13 04:42:28 +00:00
|
|
|
}
|
|
|
|
}
|