koel/tests/Feature/SongTest.php

331 lines
10 KiB
PHP
Raw Normal View History

2016-03-05 09:01:12 +00:00
<?php
2017-02-14 06:53:02 +00:00
namespace Tests\Feature;
2016-03-05 09:01:12 +00:00
use App\Models\Album;
use App\Models\Artist;
use App\Models\Song;
use App\Models\User;
2021-06-05 10:47:56 +00:00
use Illuminate\Support\Collection;
2016-03-05 09:01:12 +00:00
2017-08-05 16:56:11 +00:00
class SongTest extends TestCase
2016-03-05 09:01:12 +00:00
{
2023-06-04 21:51:53 +00:00
public const JSON_STRUCTURE = [
'type',
'id',
'title',
'lyrics',
'album_id',
'album_name',
'artist_id',
'artist_name',
'album_artist_id',
'album_artist_name',
'album_cover',
'length',
'liked',
'play_count',
'track',
'genre',
'year',
'disc',
'created_at',
];
public const JSON_COLLECTION_STRUCTURE = [
'data' => [
'*' => self::JSON_STRUCTURE,
],
'links' => [
'first',
'last',
'prev',
'next',
],
'meta' => [
'current_page',
'from',
'path',
'per_page',
'to',
],
];
public function testIndex(): void
2016-05-30 16:19:52 +00:00
{
2023-06-04 21:51:53 +00:00
Song::factory(10)->create();
2018-08-19 21:17:05 +00:00
2023-06-04 21:51:53 +00:00
$this->getAs('api/songs')->assertJsonStructure(self::JSON_COLLECTION_STRUCTURE);
$this->getAs('api/songs?sort=title&order=desc')->assertJsonStructure(self::JSON_COLLECTION_STRUCTURE);
}
public function testShow(): void
{
/** @var Song $song */
$song = Song::factory()->create();
$this->getAs('api/songs/' . $song->id)->assertJsonStructure(self::JSON_STRUCTURE);
}
public function testDelete(): void
{
/** @var Collection|array<array-key, Song> $songs */
$songs = Song::factory(3)->create();
/** @var User $admin */
$admin = User::factory()->admin()->create();
$this->deleteAs('api/songs', ['songs' => $songs->pluck('id')->all()], $admin)
->assertNoContent();
$songs->each(fn (Song $song) => $this->assertModelMissing($song));
}
public function testUnauthorizedDelete(): void
{
/** @var Collection|array<array-key, Song> $songs */
$songs = Song::factory(3)->create();
$this->deleteAs('api/songs', ['songs' => $songs->pluck('id')->all()])
->assertForbidden();
$songs->each(fn (Song $song) => $this->assertModelExists($song));
2016-05-30 16:19:52 +00:00
}
2019-07-22 07:03:23 +00:00
public function testSingleUpdateAllInfoNoCompilation(): void
2016-03-05 09:01:12 +00:00
{
2023-06-04 21:51:53 +00:00
static::createSampleMediaSet();
2022-07-27 15:32:36 +00:00
/** @var User $user */
$user = User::factory()->admin()->create();
/** @var Song $song */
$song = Song::query()->first();
2021-06-05 10:47:56 +00:00
2022-07-27 08:49:33 +00:00
$this->putAs('/api/songs', [
'songs' => [$song->id],
'data' => [
'title' => 'Foo Bar',
2022-07-06 16:08:55 +00:00
'artist_name' => 'John Cena',
'album_name' => 'One by One',
'lyrics' => 'Lorem ipsum dolor sic amet.',
'track' => 1,
2022-07-06 16:08:55 +00:00
'disc' => 2,
],
2022-07-27 15:32:36 +00:00
], $user)
->assertOk();
2016-03-05 09:01:12 +00:00
2021-06-05 10:47:56 +00:00
/** @var Artist $artist */
$artist = Artist::query()->where('name', 'John Cena')->first();
2020-09-06 18:21:39 +00:00
self::assertNotNull($artist);
2016-03-05 09:01:12 +00:00
2021-06-05 10:47:56 +00:00
/** @var Album $album */
$album = Album::query()->where('name', 'One by One')->first();
2020-09-06 18:21:39 +00:00
self::assertNotNull($album);
2016-03-05 09:01:12 +00:00
2022-07-06 16:08:55 +00:00
self::assertDatabaseHas(Song::class, [
2016-03-05 09:01:12 +00:00
'id' => $song->id,
'album_id' => $album->id,
'lyrics' => 'Lorem ipsum dolor sic amet.',
'track' => 1,
2022-07-06 16:08:55 +00:00
'disc' => 2,
2016-03-05 09:01:12 +00:00
]);
}
2019-07-22 07:03:23 +00:00
public function testSingleUpdateSomeInfoNoCompilation(): void
2016-03-05 09:01:12 +00:00
{
2023-06-04 21:51:53 +00:00
static::createSampleMediaSet();
2022-07-27 15:32:36 +00:00
/** @var User $user */
$user = User::factory()->admin()->create();
/** @var Song $song */
$song = Song::query()->first();
2022-07-06 16:08:55 +00:00
$originalArtistId = $song->artist->id;
2021-06-05 10:47:56 +00:00
2022-07-27 08:49:33 +00:00
$this->putAs('/api/songs', [
'songs' => [$song->id],
'data' => [
'title' => '',
2022-07-06 16:08:55 +00:00
'artist_name' => '',
'album_name' => 'One by One',
'lyrics' => 'Lorem ipsum dolor sic amet.',
'track' => 1,
],
2022-07-27 15:32:36 +00:00
], $user)
->assertOk();
2016-03-05 09:01:12 +00:00
// We don't expect the song's artist to change
2022-10-07 14:25:44 +00:00
self::assertSame($originalArtistId, $song->refresh()->artist->id);
2016-03-05 09:01:12 +00:00
// But we expect a new album to be created for this artist and contain this song
2022-10-07 14:25:44 +00:00
self::assertSame('One by One', $song->album->name);
2016-03-05 09:01:12 +00:00
}
2022-07-06 16:08:55 +00:00
public function testMultipleUpdateNoCompilation(): void
2016-03-05 09:01:12 +00:00
{
2023-06-04 21:51:53 +00:00
static::createSampleMediaSet();
2022-07-27 15:32:36 +00:00
/** @var User $user */
$user = User::factory()->admin()->create();
2023-06-04 21:51:53 +00:00
$songIds = Song::query()->latest()->take(3)->pluck('id')->all();
2016-03-05 09:01:12 +00:00
2022-07-27 08:49:33 +00:00
$this->putAs('/api/songs', [
'songs' => $songIds,
'data' => [
'title' => null,
2022-07-06 16:08:55 +00:00
'artist_name' => 'John Cena',
'album_name' => 'One by One',
'lyrics' => null,
'track' => 9999,
],
2022-07-27 15:32:36 +00:00
], $user)
->assertOk();
2016-03-05 09:01:12 +00:00
/** @var Collection|array<array-key, Song> $songs */
$songs = Song::query()->whereIn('id', $songIds)->get();
2016-03-05 09:01:12 +00:00
2022-07-06 16:08:55 +00:00
// All of these songs must now belong to a new album and artist set
2022-10-07 14:25:44 +00:00
self::assertSame('One by One', $songs[0]->album->name);
2022-07-06 16:08:55 +00:00
self::assertSame($songs[0]->album_id, $songs[1]->album_id);
self::assertSame($songs[0]->album_id, $songs[2]->album_id);
2016-03-05 09:01:12 +00:00
2022-10-07 14:25:44 +00:00
self::assertSame('John Cena', $songs[0]->artist->name);
2022-07-06 16:08:55 +00:00
self::assertSame($songs[0]->artist_id, $songs[1]->artist_id);
self::assertSame($songs[0]->artist_id, $songs[2]->artist_id);
self::assertNotSame($songs[0]->title, $songs[1]->title);
self::assertNotSame($songs[0]->lyrics, $songs[1]->lyrics);
self::assertSame(9999, $songs[0]->track);
self::assertSame(9999, $songs[1]->track);
self::assertSame(9999, $songs[2]->track);
2016-03-05 09:01:12 +00:00
}
2022-07-06 16:08:55 +00:00
public function testMultipleUpdateCreatingNewAlbumsAndArtists(): void
2016-03-05 09:01:12 +00:00
{
2023-06-04 21:51:53 +00:00
static::createSampleMediaSet();
2022-07-27 15:32:36 +00:00
/** @var User $user */
$user = User::factory()->admin()->create();
2022-07-06 16:08:55 +00:00
/** @var array<array-key, Song>|Collection $originalSongs */
$originalSongs = Song::query()->latest()->take(3)->get();
2016-03-05 09:01:12 +00:00
2022-07-27 08:49:33 +00:00
$this->putAs('/api/songs', [
2023-06-04 21:51:53 +00:00
'songs' => $originalSongs->pluck('id')->all(),
'data' => [
'title' => 'Foo Bar',
2022-07-06 16:08:55 +00:00
'artist_name' => 'John Cena',
'album_name' => '',
'lyrics' => 'Lorem ipsum dolor sic amet.',
'track' => 1,
],
2022-07-27 15:32:36 +00:00
], $user)
->assertOk();
2016-03-05 09:01:12 +00:00
2022-10-12 09:27:35 +00:00
/** @var array<array-key, Song>|Collection $songs */
$songs = Song::query()->whereIn('id', $originalSongs->pluck('id'))->get();
2016-03-05 09:01:12 +00:00
// Even though the album name doesn't change, a new artist should have been created
// and thus, a new album with the same name was created as well.
2022-10-07 14:25:44 +00:00
self::assertSame($songs[0]->album->name, $originalSongs[0]->album->name);
self::assertNotSame($songs[0]->album->id, $originalSongs[0]->album->id);
self::assertSame($songs[1]->album->name, $originalSongs[1]->album->name);
self::assertNotSame($songs[1]->album->id, $originalSongs[1]->album->id);
self::assertSame($songs[2]->album->name, $originalSongs[2]->album->name);
self::assertNotSame($songs[2]->album->id, $originalSongs[2]->album->id);
2016-03-05 09:01:12 +00:00
// And of course, the new artist is...
2022-10-07 14:25:44 +00:00
self::assertSame('John Cena', $songs[0]->artist->name); // JOHN CENA!!!
self::assertSame('John Cena', $songs[1]->artist->name); // JOHN CENA!!!
self::assertSame('John Cena', $songs[2]->artist->name); // And... JOHN CENAAAAAAAAAAA!!!
2016-03-05 09:01:12 +00:00
}
2016-04-24 04:37:04 +00:00
2022-07-06 16:08:55 +00:00
public function testSingleUpdateAllInfoWithCompilation(): void
2016-04-24 04:37:04 +00:00
{
2023-06-04 21:51:53 +00:00
static::createSampleMediaSet();
2022-07-27 15:32:36 +00:00
/** @var User $user */
$user = User::factory()->admin()->create();
/** @var Song $song */
$song = Song::query()->first();
2022-07-27 08:49:33 +00:00
$this->putAs('/api/songs', [
'songs' => [$song->id],
'data' => [
'title' => 'Foo Bar',
2022-07-06 16:08:55 +00:00
'artist_name' => 'John Cena',
'album_name' => 'One by One',
'album_artist_name' => 'John Lennon',
'lyrics' => 'Lorem ipsum dolor sic amet.',
'track' => 1,
2022-07-06 16:08:55 +00:00
'disc' => 2,
],
2022-07-27 15:32:36 +00:00
], $user)
->assertOk();
2016-04-24 04:37:04 +00:00
2022-07-06 16:08:55 +00:00
/** @var Album $album */
$album = Album::query()->where('name', 'One by One')->first();
2022-07-06 16:08:55 +00:00
/** @var Artist $albumArtist */
$albumArtist = Artist::query()->where('name', 'John Lennon')->first();
2016-04-24 04:37:04 +00:00
2021-06-05 10:47:56 +00:00
/** @var Artist $artist */
$artist = Artist::query()->where('name', 'John Cena')->first();
2016-04-24 04:37:04 +00:00
2022-07-06 16:08:55 +00:00
self::assertDatabaseHas(Song::class, [
2016-04-24 04:37:04 +00:00
'id' => $song->id,
'artist_id' => $artist->id,
2022-07-06 16:08:55 +00:00
'album_id' => $album->id,
2016-04-24 04:37:04 +00:00
'lyrics' => 'Lorem ipsum dolor sic amet.',
'track' => 1,
2022-07-06 16:08:55 +00:00
'disc' => 2,
2016-04-24 04:37:04 +00:00
]);
2022-07-06 16:08:55 +00:00
self::assertTrue($album->artist->is($albumArtist));
2016-04-24 04:37:04 +00:00
}
2016-05-27 08:34:38 +00:00
public function testUpdateSingleSongWithEmptyTrackAndDisc(): void
{
2023-06-04 21:51:53 +00:00
static::createSampleMediaSet();
/** @var User $user */
$user = User::factory()->admin()->create();
/** @var Song $song */
$song = Song::factory()->create([
'track' => 12,
'disc' => 2,
]);
$this->putAs('/api/songs', [
'songs' => [$song->id],
'data' => [
'track' => null,
'disc' => null,
],
], $user)
->assertOk();
$song->refresh();
self::assertSame(0, $song->track);
self::assertSame(1, $song->disc);
}
2019-07-22 07:03:23 +00:00
public function testDeletingByChunk(): void
2016-09-26 07:32:16 +00:00
{
2023-06-04 21:51:53 +00:00
Song::factory(5)->create();
2022-10-07 14:25:44 +00:00
self::assertNotSame(0, Song::query()->count());
$ids = Song::query()->select('id')->get()->pluck('id')->all();
2022-07-27 15:32:36 +00:00
Song::deleteByChunk($ids, 1);
2022-07-27 15:32:36 +00:00
2022-10-07 14:25:44 +00:00
self::assertSame(0, Song::query()->count());
2016-09-26 07:32:16 +00:00
}
2016-03-05 09:01:12 +00:00
}