2024-01-25 16:21:26 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Tests\Integration\KoelPlus\Services;
|
|
|
|
|
|
|
|
use App\Events\NewPlaylistCollaboratorJoined;
|
|
|
|
use App\Exceptions\CannotRemoveOwnerFromPlaylistException;
|
|
|
|
use App\Exceptions\NotAPlaylistCollaboratorException;
|
2024-01-29 21:58:50 +00:00
|
|
|
use App\Exceptions\OperationNotApplicableForSmartPlaylistException;
|
2024-01-25 16:21:26 +00:00
|
|
|
use App\Exceptions\PlaylistCollaborationTokenExpiredException;
|
|
|
|
use App\Models\Playlist;
|
|
|
|
use App\Models\PlaylistCollaborationToken;
|
|
|
|
use App\Services\PlaylistCollaborationService;
|
2024-04-18 11:27:07 +00:00
|
|
|
use Illuminate\Support\Facades\Event;
|
2024-10-24 10:45:45 +00:00
|
|
|
use PHPUnit\Framework\Attributes\Test;
|
2024-01-25 16:21:26 +00:00
|
|
|
use Tests\PlusTestCase;
|
|
|
|
|
|
|
|
use function Tests\create_user;
|
|
|
|
|
|
|
|
class PlaylistCollaborationServiceTest extends PlusTestCase
|
|
|
|
{
|
|
|
|
private PlaylistCollaborationService $service;
|
|
|
|
|
|
|
|
public function setUp(): void
|
|
|
|
{
|
|
|
|
parent::setUp();
|
|
|
|
|
|
|
|
$this->service = app(PlaylistCollaborationService::class);
|
|
|
|
}
|
|
|
|
|
2024-10-24 10:45:45 +00:00
|
|
|
#[Test]
|
|
|
|
public function createToken(): void
|
2024-01-25 16:21:26 +00:00
|
|
|
{
|
|
|
|
/** @var Playlist $playlist */
|
|
|
|
$playlist = Playlist::factory()->create();
|
|
|
|
|
|
|
|
$token = $this->service->createToken($playlist);
|
|
|
|
|
|
|
|
self::assertNotNull($token->token);
|
|
|
|
self::assertFalse($token->expired);
|
|
|
|
self::assertSame($playlist->id, $token->playlist_id);
|
|
|
|
}
|
|
|
|
|
2024-10-24 10:45:45 +00:00
|
|
|
#[Test]
|
|
|
|
public function createTokenFailsIfPlaylistIsSmart(): void
|
2024-01-25 16:21:26 +00:00
|
|
|
{
|
2024-01-29 21:58:50 +00:00
|
|
|
$this->expectException(OperationNotApplicableForSmartPlaylistException::class);
|
2024-01-25 16:21:26 +00:00
|
|
|
|
|
|
|
/** @var Playlist $playlist */
|
|
|
|
$playlist = Playlist::factory()->smart()->create();
|
|
|
|
|
|
|
|
$this->service->createToken($playlist);
|
|
|
|
}
|
|
|
|
|
2024-10-24 10:45:45 +00:00
|
|
|
#[Test]
|
|
|
|
public function acceptUsingToken(): void
|
2024-01-25 16:21:26 +00:00
|
|
|
{
|
2024-04-18 11:27:07 +00:00
|
|
|
Event::fake(NewPlaylistCollaboratorJoined::class);
|
2024-01-25 16:21:26 +00:00
|
|
|
|
|
|
|
/** @var PlaylistCollaborationToken $token */
|
|
|
|
$token = PlaylistCollaborationToken::factory()->create();
|
|
|
|
$user = create_user();
|
|
|
|
self::assertFalse($token->playlist->collaborators->contains($user));
|
|
|
|
|
|
|
|
$this->service->acceptUsingToken($token->token, $user);
|
|
|
|
|
|
|
|
self::assertTrue($token->refresh()->playlist->collaborators->contains($user));
|
2024-04-18 11:27:07 +00:00
|
|
|
Event::assertDispatched(NewPlaylistCollaboratorJoined::class);
|
2024-01-25 16:21:26 +00:00
|
|
|
}
|
|
|
|
|
2024-10-24 10:45:45 +00:00
|
|
|
#[Test]
|
|
|
|
public function failsToAcceptExpiredToken(): void
|
2024-01-25 16:21:26 +00:00
|
|
|
{
|
|
|
|
$this->expectException(PlaylistCollaborationTokenExpiredException::class);
|
2024-04-18 11:27:07 +00:00
|
|
|
Event::fake(NewPlaylistCollaboratorJoined::class);
|
2024-01-25 16:21:26 +00:00
|
|
|
|
|
|
|
/** @var PlaylistCollaborationToken $token */
|
|
|
|
$token = PlaylistCollaborationToken::factory()->create();
|
|
|
|
$user = create_user();
|
|
|
|
|
|
|
|
$this->travel(8)->days();
|
|
|
|
|
|
|
|
$this->service->acceptUsingToken($token->token, $user);
|
|
|
|
|
|
|
|
self::assertFalse($token->refresh()->playlist->collaborators->contains($user));
|
2024-04-18 11:27:07 +00:00
|
|
|
Event::assertNotDispatched(NewPlaylistCollaboratorJoined::class);
|
2024-01-25 16:21:26 +00:00
|
|
|
}
|
|
|
|
|
2024-10-24 10:45:45 +00:00
|
|
|
#[Test]
|
|
|
|
public function getCollaborators(): void
|
2024-01-25 16:21:26 +00:00
|
|
|
{
|
|
|
|
/** @var Playlist $playlist */
|
|
|
|
$playlist = Playlist::factory()->create();
|
|
|
|
$user = create_user();
|
|
|
|
$playlist->addCollaborator($user);
|
|
|
|
|
|
|
|
$collaborators = $this->service->getCollaborators($playlist->refresh());
|
|
|
|
|
|
|
|
self::assertEqualsCanonicalizing([$playlist->user_id, $user->id], $collaborators->pluck('id')->toArray());
|
|
|
|
}
|
|
|
|
|
2024-10-24 10:45:45 +00:00
|
|
|
#[Test]
|
|
|
|
public function removeCollaborator(): void
|
2024-01-25 16:21:26 +00:00
|
|
|
{
|
|
|
|
/** @var Playlist $playlist */
|
|
|
|
$playlist = Playlist::factory()->create();
|
|
|
|
$user = create_user();
|
|
|
|
$playlist->addCollaborator($user);
|
|
|
|
|
|
|
|
self::assertTrue($playlist->refresh()->hasCollaborator($user));
|
|
|
|
|
|
|
|
$this->service->removeCollaborator($playlist, $user);
|
|
|
|
|
|
|
|
self::assertFalse($playlist->refresh()->hasCollaborator($user));
|
|
|
|
}
|
|
|
|
|
2024-10-24 10:45:45 +00:00
|
|
|
#[Test]
|
|
|
|
public function cannotRemoveNonExistingCollaborator(): void
|
2024-01-25 16:21:26 +00:00
|
|
|
{
|
|
|
|
$this->expectException(NotAPlaylistCollaboratorException::class);
|
|
|
|
|
|
|
|
/** @var Playlist $playlist */
|
|
|
|
$playlist = Playlist::factory()->create();
|
|
|
|
$user = create_user();
|
|
|
|
|
|
|
|
$this->service->removeCollaborator($playlist, $user);
|
|
|
|
}
|
|
|
|
|
2024-10-24 10:45:45 +00:00
|
|
|
#[Test]
|
|
|
|
public function cannotRemoveOwner(): void
|
2024-01-25 16:21:26 +00:00
|
|
|
{
|
|
|
|
$this->expectException(CannotRemoveOwnerFromPlaylistException::class);
|
|
|
|
|
|
|
|
/** @var Playlist $playlist */
|
|
|
|
$playlist = Playlist::factory()->create();
|
|
|
|
|
|
|
|
$this->service->removeCollaborator($playlist, $playlist->user);
|
|
|
|
}
|
|
|
|
}
|