2022-08-10 14:56:01 +00:00
|
|
|
<?php
|
|
|
|
|
2023-06-05 21:46:41 +00:00
|
|
|
namespace Tests\Feature;
|
2022-08-10 14:56:01 +00:00
|
|
|
|
2024-01-25 16:21:26 +00:00
|
|
|
use App\Http\Resources\PlaylistFolderResource;
|
2024-03-27 09:53:05 +00:00
|
|
|
use App\Models\Playlist;
|
2022-08-10 14:56:01 +00:00
|
|
|
use App\Models\PlaylistFolder;
|
2024-10-24 10:45:45 +00:00
|
|
|
use PHPUnit\Framework\Attributes\Test;
|
2024-01-09 18:34:40 +00:00
|
|
|
use Tests\TestCase;
|
2022-08-10 14:56:01 +00:00
|
|
|
|
2024-01-11 12:41:33 +00:00
|
|
|
use function Tests\create_user;
|
|
|
|
|
2022-08-10 14:56:01 +00:00
|
|
|
class PlaylistFolderTest extends TestCase
|
|
|
|
{
|
2024-10-24 10:45:45 +00:00
|
|
|
#[Test]
|
|
|
|
public function listing(): void
|
2023-08-16 12:56:31 +00:00
|
|
|
{
|
2024-01-11 12:41:33 +00:00
|
|
|
$user = create_user();
|
2023-08-16 12:56:31 +00:00
|
|
|
PlaylistFolder::factory()->for($user)->count(3)->create();
|
|
|
|
|
|
|
|
$this->getAs('api/playlist-folders', $user)
|
2024-01-25 16:21:26 +00:00
|
|
|
->assertJsonStructure(['*' => PlaylistFolderResource::JSON_STRUCTURE])
|
2023-08-16 12:56:31 +00:00
|
|
|
->assertJsonCount(3, '*');
|
|
|
|
}
|
|
|
|
|
2024-10-24 10:45:45 +00:00
|
|
|
#[Test]
|
|
|
|
public function create(): void
|
2022-08-10 14:56:01 +00:00
|
|
|
{
|
2024-01-11 12:41:33 +00:00
|
|
|
$user = create_user();
|
2022-08-10 14:56:01 +00:00
|
|
|
|
|
|
|
$this->postAs('api/playlist-folders', ['name' => 'Classical'], $user)
|
2024-01-25 16:21:26 +00:00
|
|
|
->assertJsonStructure(PlaylistFolderResource::JSON_STRUCTURE);
|
2022-08-10 14:56:01 +00:00
|
|
|
|
|
|
|
$this->assertDatabaseHas(PlaylistFolder::class, ['name' => 'Classical', 'user_id' => $user->id]);
|
|
|
|
}
|
|
|
|
|
2024-10-24 10:45:45 +00:00
|
|
|
#[Test]
|
|
|
|
public function update(): void
|
2022-09-08 05:06:49 +00:00
|
|
|
{
|
|
|
|
$folder = PlaylistFolder::factory()->create(['name' => 'Metal']);
|
|
|
|
|
2024-11-09 14:56:48 +00:00
|
|
|
$this->patchAs("api/playlist-folders/{$folder->id}", ['name' => 'Classical'], $folder->user)
|
2024-01-25 16:21:26 +00:00
|
|
|
->assertJsonStructure(PlaylistFolderResource::JSON_STRUCTURE);
|
2022-09-08 05:06:49 +00:00
|
|
|
|
|
|
|
self::assertSame('Classical', $folder->fresh()->name);
|
|
|
|
}
|
|
|
|
|
2024-10-24 10:45:45 +00:00
|
|
|
#[Test]
|
|
|
|
public function unauthorizedUpdate(): void
|
2022-09-08 05:06:49 +00:00
|
|
|
{
|
|
|
|
$folder = PlaylistFolder::factory()->create(['name' => 'Metal']);
|
|
|
|
|
2024-11-09 14:56:48 +00:00
|
|
|
$this->patchAs("api/playlist-folders/{$folder->id}", ['name' => 'Classical'])
|
2022-09-08 05:06:49 +00:00
|
|
|
->assertForbidden();
|
|
|
|
|
|
|
|
self::assertSame('Metal', $folder->fresh()->name);
|
|
|
|
}
|
|
|
|
|
2024-10-24 10:45:45 +00:00
|
|
|
#[Test]
|
|
|
|
public function destroy(): void
|
2022-08-10 14:56:01 +00:00
|
|
|
{
|
|
|
|
$folder = PlaylistFolder::factory()->create();
|
|
|
|
|
2024-11-09 14:56:48 +00:00
|
|
|
$this->deleteAs("api/playlist-folders/{$folder->id}", ['name' => 'Classical'], $folder->user)
|
2022-08-10 14:56:01 +00:00
|
|
|
->assertNoContent();
|
|
|
|
|
|
|
|
self::assertModelMissing($folder);
|
|
|
|
}
|
|
|
|
|
2024-10-24 10:45:45 +00:00
|
|
|
#[Test]
|
|
|
|
public function nonAuthorizedDelete(): void
|
2022-08-10 14:56:01 +00:00
|
|
|
{
|
|
|
|
/** @var PlaylistFolder $folder */
|
|
|
|
$folder = PlaylistFolder::factory()->create();
|
|
|
|
|
2024-11-09 14:56:48 +00:00
|
|
|
$this->deleteAs("api/playlist-folders/{$folder->id}", ['name' => 'Classical'])
|
2022-08-10 14:56:01 +00:00
|
|
|
->assertForbidden();
|
|
|
|
|
|
|
|
self::assertModelExists($folder);
|
|
|
|
}
|
2024-03-27 09:53:05 +00:00
|
|
|
|
2024-10-24 10:45:45 +00:00
|
|
|
#[Test]
|
|
|
|
public function movingPlaylistToFolder(): void
|
2024-03-27 09:53:05 +00:00
|
|
|
{
|
|
|
|
/** @var PlaylistFolder $folder */
|
|
|
|
$folder = PlaylistFolder::factory()->create();
|
|
|
|
|
|
|
|
/** @var Playlist $playlist */
|
|
|
|
$playlist = Playlist::factory()->for($folder->user)->create();
|
|
|
|
self::assertNull($playlist->getFolderId($folder->user));
|
|
|
|
|
2024-11-09 14:56:48 +00:00
|
|
|
$this->postAs(
|
|
|
|
"api/playlist-folders/{$folder->id}/playlists",
|
|
|
|
['playlists' => [$playlist->id]],
|
|
|
|
$folder->user
|
|
|
|
)
|
2024-03-27 09:53:05 +00:00
|
|
|
->assertSuccessful();
|
|
|
|
|
|
|
|
self::assertTrue($playlist->fresh()->getFolder($folder->user)->is($folder));
|
|
|
|
}
|
|
|
|
|
2024-10-24 10:45:45 +00:00
|
|
|
#[Test]
|
|
|
|
public function unauthorizedMovingPlaylistToFolderIsNotAllowed(): void
|
2024-03-27 09:53:05 +00:00
|
|
|
{
|
|
|
|
/** @var PlaylistFolder $folder */
|
|
|
|
$folder = PlaylistFolder::factory()->create();
|
|
|
|
|
|
|
|
/** @var Playlist $playlist */
|
|
|
|
$playlist = Playlist::factory()->for($folder->user)->create();
|
|
|
|
self::assertNull($playlist->getFolderId($folder->user));
|
|
|
|
|
2024-11-09 14:56:48 +00:00
|
|
|
$this->postAs("api/playlist-folders/{$folder->id}/playlists", ['playlists' => [$playlist->id]])
|
2024-03-27 09:53:05 +00:00
|
|
|
->assertUnprocessable();
|
|
|
|
|
|
|
|
self::assertNull($playlist->fresh()->getFolder($folder->user));
|
|
|
|
}
|
|
|
|
|
2024-10-24 10:45:45 +00:00
|
|
|
#[Test]
|
|
|
|
public function movingPlaylistToRootLevel(): void
|
2024-03-27 09:53:05 +00:00
|
|
|
{
|
|
|
|
/** @var PlaylistFolder $folder */
|
|
|
|
$folder = PlaylistFolder::factory()->create();
|
|
|
|
|
|
|
|
/** @var Playlist $playlist */
|
|
|
|
$playlist = Playlist::factory()->for($folder->user)->create();
|
|
|
|
|
2024-11-09 14:56:48 +00:00
|
|
|
$folder->playlists()->attach($playlist);
|
2024-03-27 09:53:05 +00:00
|
|
|
self::assertTrue($playlist->refresh()->getFolder($folder->user)->is($folder));
|
|
|
|
|
2024-11-09 14:56:48 +00:00
|
|
|
$this->deleteAs(
|
|
|
|
"api/playlist-folders/{$folder->id}/playlists",
|
|
|
|
['playlists' => [$playlist->id]],
|
|
|
|
$folder->user
|
|
|
|
)
|
2024-03-27 09:53:05 +00:00
|
|
|
->assertSuccessful();
|
|
|
|
|
|
|
|
self::assertNull($playlist->fresh()->getFolder($folder->user));
|
|
|
|
}
|
|
|
|
|
2024-10-24 10:45:45 +00:00
|
|
|
#[Test]
|
|
|
|
public function unauthorizedMovingPlaylistToRootLevelIsNotAllowed(): void
|
2024-03-27 09:53:05 +00:00
|
|
|
{
|
|
|
|
/** @var PlaylistFolder $folder */
|
|
|
|
$folder = PlaylistFolder::factory()->create();
|
|
|
|
|
|
|
|
/** @var Playlist $playlist */
|
|
|
|
$playlist = Playlist::factory()->for($folder->user)->create();
|
|
|
|
|
2024-11-09 14:56:48 +00:00
|
|
|
$folder->playlists()->attach($playlist);
|
2024-03-27 09:53:05 +00:00
|
|
|
self::assertTrue($playlist->refresh()->getFolder($folder->user)->is($folder));
|
|
|
|
|
2024-11-09 14:56:48 +00:00
|
|
|
$this->deleteAs("api/playlist-folders/{$folder->id}/playlists", ['playlists' => [$playlist->id]])
|
2024-03-27 09:53:05 +00:00
|
|
|
->assertUnprocessable();
|
|
|
|
|
|
|
|
self::assertTrue($playlist->refresh()->getFolder($folder->user)->is($folder));
|
|
|
|
}
|
2022-08-10 14:56:01 +00:00
|
|
|
}
|