2021-10-25 19:17:25 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Tests\Integration\Services;
|
|
|
|
|
2022-11-27 15:29:29 +00:00
|
|
|
use App\Models\Playlist;
|
|
|
|
use App\Models\PlaylistFolder;
|
|
|
|
use App\Models\Song;
|
2021-10-25 19:17:25 +00:00
|
|
|
use App\Services\PlaylistService;
|
2022-11-27 15:29:29 +00:00
|
|
|
use App\Values\SmartPlaylistRuleGroupCollection;
|
|
|
|
use Illuminate\Support\Collection;
|
2024-01-12 14:41:02 +00:00
|
|
|
use InvalidArgumentException as BaseInvalidArgumentException;
|
2024-01-26 11:06:57 +00:00
|
|
|
use Tests\PlusTestCase;
|
2021-10-25 19:17:25 +00:00
|
|
|
use Tests\TestCase;
|
2022-11-27 15:29:29 +00:00
|
|
|
use Webmozart\Assert\InvalidArgumentException;
|
2021-10-25 19:17:25 +00:00
|
|
|
|
2024-01-11 12:41:33 +00:00
|
|
|
use function Tests\create_user;
|
|
|
|
|
2021-10-25 19:17:25 +00:00
|
|
|
class PlaylistServiceTest extends TestCase
|
|
|
|
{
|
|
|
|
private PlaylistService $service;
|
|
|
|
|
|
|
|
public function setUp(): void
|
|
|
|
{
|
|
|
|
parent::setUp();
|
|
|
|
|
|
|
|
$this->service = app(PlaylistService::class);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testCreatePlaylist(): void
|
|
|
|
{
|
2024-01-11 12:41:33 +00:00
|
|
|
$user = create_user();
|
2021-10-25 19:17:25 +00:00
|
|
|
|
2022-11-27 15:29:29 +00:00
|
|
|
$playlist = $this->service->createPlaylist('foo', $user);
|
|
|
|
|
|
|
|
self::assertSame('foo', $playlist->name);
|
|
|
|
self::assertTrue($user->is($playlist->user));
|
2022-07-18 11:00:37 +00:00
|
|
|
self::assertFalse($playlist->is_smart);
|
2021-10-25 19:17:25 +00:00
|
|
|
}
|
|
|
|
|
2022-11-27 15:29:29 +00:00
|
|
|
public function testCreatePlaylistWithSongs(): void
|
|
|
|
{
|
2024-04-18 11:27:07 +00:00
|
|
|
/** @var Collection<array-key, Song> $songs */
|
2022-11-27 15:29:29 +00:00
|
|
|
$songs = Song::factory(3)->create();
|
|
|
|
|
2024-01-11 12:41:33 +00:00
|
|
|
$user = create_user();
|
2022-11-27 15:29:29 +00:00
|
|
|
|
|
|
|
$playlist = $this->service->createPlaylist('foo', $user, null, $songs->pluck('id')->all());
|
|
|
|
|
|
|
|
self::assertSame('foo', $playlist->name);
|
|
|
|
self::assertTrue($user->is($playlist->user));
|
|
|
|
self::assertFalse($playlist->is_smart);
|
|
|
|
self::assertEqualsCanonicalizing($playlist->songs->pluck('id')->all(), $songs->pluck('id')->all());
|
|
|
|
}
|
|
|
|
|
2021-10-25 19:17:25 +00:00
|
|
|
public function testCreateSmartPlaylist(): void
|
|
|
|
{
|
2022-11-27 15:29:29 +00:00
|
|
|
$rules = SmartPlaylistRuleGroupCollection::create([
|
2021-10-25 19:17:25 +00:00
|
|
|
[
|
2023-04-17 19:45:43 +00:00
|
|
|
'id' => '45368b8f-fec8-4b72-b826-6b295af0da65',
|
2022-11-27 15:29:29 +00:00
|
|
|
'rules' => [
|
|
|
|
[
|
2023-04-17 19:45:43 +00:00
|
|
|
'id' => '8cfa8700-fbc0-4078-b175-af31c20a3582',
|
2022-11-27 15:29:29 +00:00
|
|
|
'model' => 'title',
|
|
|
|
'operator' => 'is',
|
|
|
|
'value' => ['foo'],
|
|
|
|
],
|
|
|
|
],
|
2021-10-25 19:17:25 +00:00
|
|
|
],
|
2022-11-27 15:29:29 +00:00
|
|
|
]);
|
2021-10-25 19:17:25 +00:00
|
|
|
|
2024-01-11 12:41:33 +00:00
|
|
|
$user = create_user();
|
2021-10-25 19:17:25 +00:00
|
|
|
|
2022-11-27 15:29:29 +00:00
|
|
|
$playlist = $this->service->createPlaylist('foo', $user, null, [], $rules);
|
|
|
|
|
|
|
|
self::assertSame('foo', $playlist->name);
|
|
|
|
self::assertTrue($user->is($playlist->user));
|
|
|
|
self::assertTrue($playlist->is_smart);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testCreatePlaylistInFolder(): void
|
|
|
|
{
|
|
|
|
/** @var PlaylistFolder $folder */
|
|
|
|
$folder = PlaylistFolder::factory()->create();
|
|
|
|
|
|
|
|
$playlist = $this->service->createPlaylist('foo', $folder->user, $folder);
|
|
|
|
|
|
|
|
self::assertSame('foo', $playlist->name);
|
2024-01-18 11:13:05 +00:00
|
|
|
self::assertTrue($folder->ownedBy($playlist->user));
|
|
|
|
self::assertTrue($playlist->inFolder($folder));
|
2022-11-27 15:29:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testCreatePlaylistInAnotherUsersFolder(): void
|
|
|
|
{
|
|
|
|
/** @var PlaylistFolder $folder */
|
|
|
|
$folder = PlaylistFolder::factory()->create();
|
|
|
|
|
2024-03-22 20:34:32 +00:00
|
|
|
$this->expectException(InvalidArgumentException::class);
|
2022-11-27 15:29:29 +00:00
|
|
|
|
2024-01-11 12:41:33 +00:00
|
|
|
$this->service->createPlaylist('foo', create_user(), $folder);
|
2022-11-27 15:29:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testUpdateSimplePlaylist(): void
|
|
|
|
{
|
|
|
|
/** @var Playlist $playlist */
|
|
|
|
$playlist = Playlist::factory()->create(['name' => 'foo']);
|
|
|
|
|
|
|
|
$this->service->updatePlaylist($playlist, 'bar');
|
|
|
|
|
|
|
|
self::assertSame('bar', $playlist->name);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testUpdateSmartPlaylist(): void
|
|
|
|
{
|
|
|
|
$rules = SmartPlaylistRuleGroupCollection::create([
|
|
|
|
[
|
2023-04-17 19:45:43 +00:00
|
|
|
'id' => '45368b8f-fec8-4b72-b826-6b295af0da65',
|
2022-11-27 15:29:29 +00:00
|
|
|
'rules' => [
|
|
|
|
[
|
2023-04-17 19:45:43 +00:00
|
|
|
'id' => '8cfa8700-fbc0-4078-b175-af31c20a3582',
|
2022-11-27 15:29:29 +00:00
|
|
|
'model' => 'title',
|
|
|
|
'operator' => 'is',
|
|
|
|
'value' => ['foo'],
|
|
|
|
],
|
|
|
|
],
|
|
|
|
],
|
|
|
|
]);
|
|
|
|
|
|
|
|
/** @var Playlist $playlist */
|
|
|
|
$playlist = Playlist::factory()->create(['name' => 'foo', 'rules' => $rules]);
|
|
|
|
|
|
|
|
$this->service->updatePlaylist($playlist, 'bar', null, SmartPlaylistRuleGroupCollection::create([
|
|
|
|
[
|
2023-04-17 19:45:43 +00:00
|
|
|
'id' => '45368b8f-fec8-4b72-b826-6b295af0da65',
|
2022-11-27 15:29:29 +00:00
|
|
|
'rules' => [
|
|
|
|
[
|
2023-04-17 19:45:43 +00:00
|
|
|
'id' => '8cfa8700-fbc0-4078-b175-af31c20a3582',
|
2022-11-27 15:29:29 +00:00
|
|
|
'model' => 'title',
|
|
|
|
'operator' => 'is',
|
|
|
|
'value' => ['bar'],
|
|
|
|
],
|
|
|
|
],
|
|
|
|
],
|
|
|
|
]));
|
|
|
|
|
|
|
|
$playlist->refresh();
|
|
|
|
|
|
|
|
self::assertSame('bar', $playlist->name);
|
2022-07-18 11:00:37 +00:00
|
|
|
self::assertTrue($playlist->is_smart);
|
2022-11-27 15:29:29 +00:00
|
|
|
self::assertSame($playlist->rule_groups->first()->rules->first()->value, ['bar']);
|
2021-10-25 19:17:25 +00:00
|
|
|
}
|
2024-01-12 14:41:02 +00:00
|
|
|
|
|
|
|
public function testSettingOwnsSongOnlyFailsForCommunityLicenseWhenCreate(): void
|
|
|
|
{
|
2024-03-22 20:34:32 +00:00
|
|
|
$this->expectException(BaseInvalidArgumentException::class);
|
|
|
|
$this->expectExceptionMessage('"Own songs only" option only works with smart playlists and Plus license.');
|
2024-01-12 14:41:02 +00:00
|
|
|
|
|
|
|
$this->service->createPlaylist(
|
|
|
|
name: 'foo',
|
|
|
|
user: create_user(),
|
|
|
|
ruleGroups: SmartPlaylistRuleGroupCollection::create([
|
|
|
|
[
|
|
|
|
'id' => '45368b8f-fec8-4b72-b826-6b295af0da65',
|
|
|
|
'rules' => [
|
|
|
|
[
|
|
|
|
'id' => '8cfa8700-fbc0-4078-b175-af31c20a3582',
|
|
|
|
'model' => 'title',
|
|
|
|
'operator' => 'is',
|
|
|
|
'value' => ['foo'],
|
|
|
|
],
|
|
|
|
],
|
|
|
|
],
|
|
|
|
]),
|
|
|
|
ownSongsOnly: true
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testSettingOwnsSongOnlyFailsForCommunityLicenseWhenUpdate(): void
|
|
|
|
{
|
2024-03-22 20:34:32 +00:00
|
|
|
$this->expectException(BaseInvalidArgumentException::class);
|
|
|
|
$this->expectExceptionMessage('"Own songs only" option only works with smart playlists and Plus license.');
|
2024-01-12 14:41:02 +00:00
|
|
|
|
|
|
|
/** @var Playlist $playlist */
|
|
|
|
$playlist = Playlist::factory()->smart()->create();
|
|
|
|
|
|
|
|
$this->service->updatePlaylist(
|
|
|
|
playlist: $playlist,
|
|
|
|
name: 'foo',
|
|
|
|
ownSongsOnly: true
|
|
|
|
);
|
|
|
|
}
|
2024-01-26 11:06:57 +00:00
|
|
|
|
|
|
|
public function testAddSongsToPlaylist(): void
|
|
|
|
{
|
|
|
|
/** @var Playlist $playlist */
|
|
|
|
$playlist = Playlist::factory()->create();
|
|
|
|
$playlist->addSongs(Song::factory(3)->create());
|
|
|
|
$songs = Song::factory(2)->create();
|
|
|
|
|
|
|
|
$addedSongs = $this->service->addSongsToPlaylist($playlist, $songs, $playlist->user);
|
|
|
|
$playlist->refresh();
|
|
|
|
|
|
|
|
self::assertCount(2, $addedSongs);
|
|
|
|
self::assertCount(5, $playlist->songs);
|
|
|
|
self::assertEqualsCanonicalizing($addedSongs->pluck('id')->all(), $songs->pluck('id')->all());
|
|
|
|
$songs->each(static fn (Song $song) => self::assertTrue($playlist->songs->contains($song)));
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testPrivateSongsAreMadePublicWhenAddedToCollaborativePlaylist(): void
|
|
|
|
{
|
|
|
|
PlusTestCase::enablePlusLicense();
|
|
|
|
|
|
|
|
/** @var Playlist $playlist */
|
|
|
|
$playlist = Playlist::factory()->create();
|
|
|
|
$user = create_user();
|
|
|
|
$playlist->collaborators()->attach($user);
|
|
|
|
$playlist->refresh();
|
|
|
|
self::assertTrue($playlist->is_collaborative);
|
|
|
|
|
|
|
|
$songs = Song::factory(2)->create(['is_public' => false]);
|
|
|
|
|
|
|
|
$this->service->addSongsToPlaylist($playlist, $songs, $user);
|
|
|
|
|
|
|
|
$songs->each(static fn (Song $song) => self::assertTrue($song->refresh()->is_public));
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testMakePlaylistSongsPublic(): void
|
|
|
|
{
|
|
|
|
/** @var Playlist $playlist */
|
|
|
|
$playlist = Playlist::factory()->create();
|
|
|
|
$playlist->addSongs(Song::factory(2)->create(['is_public' => false]));
|
|
|
|
|
|
|
|
$this->service->makePlaylistSongsPublic($playlist);
|
|
|
|
|
|
|
|
$playlist->songs->each(static fn (Song $song) => self::assertTrue($song->is_public));
|
|
|
|
}
|
2024-01-29 21:58:50 +00:00
|
|
|
|
|
|
|
public function testMoveSongsInPlaylist(): void
|
|
|
|
{
|
|
|
|
/** @var Playlist $playlist */
|
|
|
|
$playlist = Playlist::factory()->create();
|
|
|
|
|
2024-04-18 11:27:07 +00:00
|
|
|
/** @var Collection<array-key, Song> $songs */
|
2024-01-29 21:58:50 +00:00
|
|
|
$songs = Song::factory(4)->create();
|
|
|
|
$ids = $songs->pluck('id')->all();
|
|
|
|
$playlist->addSongs($songs);
|
|
|
|
|
|
|
|
$this->service->moveSongsInPlaylist($playlist, [$ids[2], $ids[3]], $ids[0], 'after');
|
|
|
|
self::assertSame([$ids[0], $ids[2], $ids[3], $ids[1]], $playlist->refresh()->songs->pluck('id')->all());
|
|
|
|
|
|
|
|
$this->service->moveSongsInPlaylist($playlist, [$ids[0]], $ids[3], 'before');
|
|
|
|
self::assertSame([$ids[2], $ids[0], $ids[3], $ids[1]], $playlist->refresh()->songs->pluck('id')->all());
|
|
|
|
|
|
|
|
// move to the first position
|
|
|
|
$this->service->moveSongsInPlaylist($playlist, [$ids[0], $ids[1]], $ids[2], 'before');
|
|
|
|
self::assertSame([$ids[0], $ids[1], $ids[2], $ids[3]], $playlist->refresh()->songs->pluck('id')->all());
|
|
|
|
|
|
|
|
// move to the last position
|
|
|
|
$this->service->moveSongsInPlaylist($playlist, [$ids[0], $ids[1]], $ids[3], 'after');
|
|
|
|
self::assertSame([$ids[2], $ids[3], $ids[0], $ids[1]], $playlist->refresh()->songs->pluck('id')->all());
|
|
|
|
}
|
2021-10-25 19:17:25 +00:00
|
|
|
}
|