2024-01-10 21:37:24 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Tests\Feature\KoelPlus;
|
|
|
|
|
2024-04-18 11:27:07 +00:00
|
|
|
use App\Events\MultipleSongsLiked;
|
|
|
|
use App\Events\MultipleSongsUnliked;
|
|
|
|
use App\Events\SongLikeToggled;
|
2024-01-10 21:37:24 +00:00
|
|
|
use App\Models\Song;
|
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-18 11:13:05 +00:00
|
|
|
use Tests\PlusTestCase;
|
2024-01-10 21:37:24 +00:00
|
|
|
|
2024-01-11 12:41:33 +00:00
|
|
|
use function Tests\create_user;
|
|
|
|
|
2024-01-18 11:13:05 +00:00
|
|
|
class InteractionTest extends PlusTestCase
|
2024-01-10 21:37:24 +00:00
|
|
|
{
|
2024-10-24 10:45:45 +00:00
|
|
|
#[Test]
|
|
|
|
public function policyForRegisterPlay(): void
|
2024-01-10 21:37:24 +00:00
|
|
|
{
|
2024-04-18 11:27:07 +00:00
|
|
|
Event::fake(SongLikeToggled::class);
|
2024-01-10 21:37:24 +00:00
|
|
|
|
2024-01-11 12:41:33 +00:00
|
|
|
$owner = create_user();
|
2024-01-10 21:37:24 +00:00
|
|
|
|
|
|
|
// Can't increase play count of a private song that doesn't belong to the user
|
|
|
|
$externalPrivateSong = Song::factory()->private()->create();
|
|
|
|
$this->postAs('api/interaction/play', ['song' => $externalPrivateSong->id], $owner)
|
|
|
|
->assertForbidden();
|
|
|
|
|
|
|
|
// Can increase play count of a public song that doesn't belong to the user
|
|
|
|
/** @var Song $externalPublicSong */
|
|
|
|
$externalPublicSong = Song::factory()->public()->create();
|
|
|
|
$this->postAs('api/interaction/play', ['song' => $externalPublicSong->id], $owner)
|
|
|
|
->assertSuccessful();
|
|
|
|
|
|
|
|
// Can increase play count of a private song that belongs to the user
|
|
|
|
/** @var Song $ownPrivateSong */
|
|
|
|
$ownPrivateSong = Song::factory()->private()->for($owner, 'owner')->create();
|
|
|
|
$this->postAs('api/interaction/play', ['song' => $ownPrivateSong->id], $ownPrivateSong->owner)
|
|
|
|
->assertSuccessful();
|
|
|
|
}
|
|
|
|
|
2024-10-24 10:45:45 +00:00
|
|
|
#[Test]
|
|
|
|
public function policyForToggleLike(): void
|
2024-01-10 21:37:24 +00:00
|
|
|
{
|
2024-04-18 11:27:07 +00:00
|
|
|
Event::fake(SongLikeToggled::class);
|
2024-01-10 21:37:24 +00:00
|
|
|
|
2024-01-11 12:41:33 +00:00
|
|
|
$owner = create_user();
|
2024-01-10 21:37:24 +00:00
|
|
|
|
|
|
|
// Can't like a private song that doesn't belong to the user
|
|
|
|
/** @var Song $externalPrivateSong */
|
|
|
|
$externalPrivateSong = Song::factory()->private()->create();
|
|
|
|
$this->postAs('api/interaction/like', ['song' => $externalPrivateSong->id], $owner)
|
|
|
|
->assertForbidden();
|
|
|
|
|
|
|
|
// Can like a public song that doesn't belong to the user
|
|
|
|
/** @var Song $externalPublicSong */
|
|
|
|
$externalPublicSong = Song::factory()->public()->create();
|
|
|
|
$this->postAs('api/interaction/like', ['song' => $externalPublicSong->id], $owner)
|
|
|
|
->assertSuccessful();
|
|
|
|
|
|
|
|
// Can like a private song that belongs to the user
|
|
|
|
/** @var Song $ownPrivateSong */
|
|
|
|
$ownPrivateSong = Song::factory()->private()->for($owner, 'owner')->create();
|
|
|
|
$this->postAs('api/interaction/like', ['song' => $ownPrivateSong->id], $owner)
|
|
|
|
->assertSuccessful();
|
|
|
|
}
|
|
|
|
|
2024-10-24 10:45:45 +00:00
|
|
|
#[Test]
|
|
|
|
public function policyForBatchLike(): void
|
2024-01-10 21:37:24 +00:00
|
|
|
{
|
2024-04-18 11:27:07 +00:00
|
|
|
Event::fake(MultipleSongsLiked::class);
|
2024-01-10 21:37:24 +00:00
|
|
|
|
2024-01-11 12:41:33 +00:00
|
|
|
$owner = create_user();
|
2024-01-10 21:37:24 +00:00
|
|
|
|
|
|
|
// Can't batch like private songs that don't belong to the user
|
|
|
|
$externalPrivateSongs = Song::factory()->count(3)->private()->create();
|
2024-11-09 14:56:48 +00:00
|
|
|
$this->postAs('api/interaction/batch/like', ['songs' => $externalPrivateSongs->modelKeys()], $owner)
|
2024-01-10 21:37:24 +00:00
|
|
|
->assertForbidden();
|
|
|
|
|
|
|
|
// Can batch like public songs that don't belong to the user
|
|
|
|
$externalPublicSongs = Song::factory()->count(3)->public()->create();
|
2024-11-09 14:56:48 +00:00
|
|
|
$this->postAs('api/interaction/batch/like', ['songs' => $externalPublicSongs->modelKeys()], $owner)
|
2024-01-10 21:37:24 +00:00
|
|
|
->assertSuccessful();
|
|
|
|
|
|
|
|
// Can batch like private songs that belong to the user
|
|
|
|
$ownPrivateSongs = Song::factory()->count(3)->private()->for($owner, 'owner')->create();
|
2024-11-09 14:56:48 +00:00
|
|
|
$this->postAs('api/interaction/batch/like', ['songs' => $ownPrivateSongs->modelKeys()], $owner)
|
2024-01-10 21:37:24 +00:00
|
|
|
->assertSuccessful();
|
|
|
|
|
|
|
|
// Can't batch like a mix of inaccessible and accessible songs
|
|
|
|
$mixedSongs = $externalPrivateSongs->merge($externalPublicSongs);
|
2024-11-09 14:56:48 +00:00
|
|
|
$this->postAs('api/interaction/batch/like', ['songs' => $mixedSongs->modelKeys()], $owner)
|
2024-01-10 21:37:24 +00:00
|
|
|
->assertForbidden();
|
|
|
|
}
|
|
|
|
|
2024-10-24 10:45:45 +00:00
|
|
|
#[Test]
|
|
|
|
public function policyForBatchUnlike(): void
|
2024-01-10 21:37:24 +00:00
|
|
|
{
|
2024-04-18 11:27:07 +00:00
|
|
|
Event::fake(MultipleSongsUnliked::class);
|
2024-01-10 21:37:24 +00:00
|
|
|
|
2024-01-11 12:41:33 +00:00
|
|
|
$owner = create_user();
|
2024-01-10 21:37:24 +00:00
|
|
|
|
|
|
|
// Can't batch unlike private songs that don't belong to the user
|
|
|
|
$externalPrivateSongs = Song::factory()->count(3)->private()->create();
|
2024-11-09 14:56:48 +00:00
|
|
|
$this->postAs('api/interaction/batch/unlike', ['songs' => $externalPrivateSongs->modelKeys()], $owner)
|
2024-01-10 21:37:24 +00:00
|
|
|
->assertForbidden();
|
|
|
|
|
|
|
|
// Can batch unlike public songs that don't belong to the user
|
|
|
|
$externalPublicSongs = Song::factory()->count(3)->public()->create();
|
2024-11-09 14:56:48 +00:00
|
|
|
$this->postAs('api/interaction/batch/unlike', ['songs' => $externalPublicSongs->modelKeys()], $owner)
|
2024-01-10 21:37:24 +00:00
|
|
|
->assertSuccessful();
|
|
|
|
|
|
|
|
// Can batch unlike private songs that belong to the user
|
|
|
|
$ownPrivateSongs = Song::factory()->count(3)->private()->for($owner, 'owner')->create();
|
2024-11-09 14:56:48 +00:00
|
|
|
$this->postAs('api/interaction/batch/unlike', ['songs' => $ownPrivateSongs->modelKeys()], $owner)
|
2024-01-10 21:37:24 +00:00
|
|
|
->assertSuccessful();
|
|
|
|
|
|
|
|
// Can't batch unlike a mix of inaccessible and accessible songs
|
|
|
|
$mixedSongs = $externalPrivateSongs->merge($externalPublicSongs);
|
2024-11-09 14:56:48 +00:00
|
|
|
$this->postAs('api/interaction/batch/unlike', ['songs' => $mixedSongs->modelKeys()], $owner)
|
2024-01-10 21:37:24 +00:00
|
|
|
->assertForbidden();
|
|
|
|
}
|
|
|
|
}
|