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\Events\LibraryChanged;
|
|
|
|
use App\Models\Album;
|
|
|
|
use App\Models\Artist;
|
|
|
|
use App\Models\Song;
|
|
|
|
use App\Models\User;
|
|
|
|
use Illuminate\Foundation\Testing\WithoutMiddleware;
|
|
|
|
|
2017-08-05 16:56:11 +00:00
|
|
|
class SongTest extends TestCase
|
2016-03-05 09:01:12 +00:00
|
|
|
{
|
2017-08-05 16:56:11 +00:00
|
|
|
use WithoutMiddleware;
|
2016-03-05 09:01:12 +00:00
|
|
|
|
2016-05-30 16:19:52 +00:00
|
|
|
public function setUp()
|
|
|
|
{
|
|
|
|
parent::setUp();
|
|
|
|
$this->createSampleMediaSet();
|
|
|
|
}
|
|
|
|
|
2016-04-24 04:37:04 +00:00
|
|
|
public function testSingleUpdateAllInfoNoCompilation()
|
2016-03-05 09:01:12 +00:00
|
|
|
{
|
|
|
|
$this->expectsEvents(LibraryChanged::class);
|
|
|
|
$song = Song::orderBy('id', 'desc')->first();
|
|
|
|
|
2017-02-14 06:53:02 +00:00
|
|
|
$user = factory(User::class, 'admin')->create();
|
|
|
|
$this->putAsUser('/api/songs', [
|
2016-03-05 09:01:12 +00:00
|
|
|
'songs' => [$song->id],
|
|
|
|
'data' => [
|
|
|
|
'title' => 'Foo Bar',
|
|
|
|
'artistName' => 'John Cena',
|
|
|
|
'albumName' => 'One by One',
|
|
|
|
'lyrics' => 'Lorem ipsum dolor sic amet.',
|
2016-03-20 13:18:34 +00:00
|
|
|
'track' => 1,
|
2016-04-24 04:37:04 +00:00
|
|
|
'compilationState' => 0,
|
2016-03-05 09:01:12 +00:00
|
|
|
],
|
2017-02-14 06:53:02 +00:00
|
|
|
], $user)
|
2016-03-05 09:01:12 +00:00
|
|
|
->seeStatusCode(200);
|
|
|
|
|
|
|
|
$artist = Artist::whereName('John Cena')->first();
|
|
|
|
$this->assertNotNull($artist);
|
|
|
|
|
|
|
|
$album = Album::whereName('One by One')->first();
|
|
|
|
$this->assertNotNull($album);
|
|
|
|
|
|
|
|
$this->seeInDatabase('songs', [
|
|
|
|
'id' => $song->id,
|
|
|
|
'album_id' => $album->id,
|
|
|
|
'lyrics' => 'Lorem ipsum dolor sic amet.',
|
2016-03-20 13:18:34 +00:00
|
|
|
'track' => 1,
|
2016-03-05 09:01:12 +00:00
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
2016-04-24 04:37:04 +00:00
|
|
|
public function testSingleUpdateSomeInfoNoCompilation()
|
2016-03-05 09:01:12 +00:00
|
|
|
{
|
|
|
|
$song = Song::orderBy('id', 'desc')->first();
|
|
|
|
$originalArtistId = $song->album->artist->id;
|
|
|
|
|
2017-02-14 06:53:02 +00:00
|
|
|
$user = factory(User::class, 'admin')->create();
|
|
|
|
$this->putAsUser('/api/songs', [
|
2016-03-05 09:01:12 +00:00
|
|
|
'songs' => [$song->id],
|
|
|
|
'data' => [
|
|
|
|
'title' => '',
|
|
|
|
'artistName' => '',
|
|
|
|
'albumName' => 'One by One',
|
|
|
|
'lyrics' => 'Lorem ipsum dolor sic amet.',
|
2016-03-20 13:18:34 +00:00
|
|
|
'track' => 1,
|
2016-04-24 04:37:04 +00:00
|
|
|
'compilationState' => 0,
|
2016-03-05 09:01:12 +00:00
|
|
|
],
|
2017-02-14 06:53:02 +00:00
|
|
|
], $user)
|
2016-03-05 09:01:12 +00:00
|
|
|
->seeStatusCode(200);
|
|
|
|
|
|
|
|
// We don't expect the song's artist to change
|
|
|
|
$this->assertEquals($originalArtistId, Song::find($song->id)->album->artist->id);
|
|
|
|
|
|
|
|
// But we expect a new album to be created for this artist and contain this song
|
|
|
|
$this->assertEquals('One by One', Song::find($song->id)->album->name);
|
|
|
|
}
|
|
|
|
|
2016-04-24 04:37:04 +00:00
|
|
|
public function testMultipleUpdateAllInfoNoCompilation()
|
2016-03-05 09:01:12 +00:00
|
|
|
{
|
|
|
|
$songIds = Song::orderBy('id', 'desc')->take(3)->pluck('id')->toArray();
|
|
|
|
|
2017-02-14 06:53:02 +00:00
|
|
|
$user = factory(User::class, 'admin')->create();
|
|
|
|
$this->putAsUser('/api/songs', [
|
2016-03-05 09:01:12 +00:00
|
|
|
'songs' => $songIds,
|
|
|
|
'data' => [
|
2016-05-27 03:32:52 +00:00
|
|
|
'title' => 'foo',
|
2016-03-05 09:01:12 +00:00
|
|
|
'artistName' => 'John Cena',
|
|
|
|
'albumName' => 'One by One',
|
2016-05-27 03:32:52 +00:00
|
|
|
'lyrics' => 'bar',
|
|
|
|
'track' => 9999,
|
2016-04-24 04:37:04 +00:00
|
|
|
'compilationState' => 0,
|
2016-03-05 09:01:12 +00:00
|
|
|
],
|
2017-02-14 06:53:02 +00:00
|
|
|
], $user)
|
2016-03-05 09:01:12 +00:00
|
|
|
->seeStatusCode(200);
|
|
|
|
|
|
|
|
$songs = Song::orderBy('id', 'desc')->take(3)->get();
|
|
|
|
|
2016-05-27 03:32:52 +00:00
|
|
|
// Even though we post the title, lyrics, and tracks, we don't expect them to take any effect
|
|
|
|
// because we're updating multiple songs here.
|
|
|
|
$this->assertNotEquals('foo', $songs[0]->title);
|
|
|
|
$this->assertNotEquals('bar', $songs[2]->lyrics);
|
|
|
|
$this->assertNotEquals(9999, $songs[2]->track);
|
2016-03-05 09:01:12 +00:00
|
|
|
|
|
|
|
// But all of these songs must now belong to a new album and artist set
|
|
|
|
$this->assertEquals('One by One', $songs[0]->album->name);
|
|
|
|
$this->assertEquals('One by One', $songs[1]->album->name);
|
|
|
|
$this->assertEquals('One by One', $songs[2]->album->name);
|
|
|
|
|
|
|
|
$this->assertEquals('John Cena', $songs[0]->album->artist->name);
|
|
|
|
$this->assertEquals('John Cena', $songs[1]->album->artist->name);
|
|
|
|
$this->assertEquals('John Cena', $songs[2]->album->artist->name);
|
|
|
|
}
|
|
|
|
|
2016-04-24 04:37:04 +00:00
|
|
|
public function testMultipleUpdateSomeInfoNoCompilation()
|
2016-03-05 09:01:12 +00:00
|
|
|
{
|
|
|
|
$originalSongs = Song::orderBy('id', 'desc')->take(3)->get();
|
|
|
|
$songIds = $originalSongs->pluck('id')->toArray();
|
|
|
|
|
2017-02-14 06:53:02 +00:00
|
|
|
$user = factory(User::class, 'admin')->create();
|
|
|
|
$this->putAsUser('/api/songs', [
|
2016-03-05 09:01:12 +00:00
|
|
|
'songs' => $songIds,
|
|
|
|
'data' => [
|
|
|
|
'title' => 'Foo Bar',
|
|
|
|
'artistName' => 'John Cena',
|
|
|
|
'albumName' => '',
|
|
|
|
'lyrics' => 'Lorem ipsum dolor sic amet.',
|
2016-03-20 13:18:34 +00:00
|
|
|
'track' => 1,
|
2016-04-24 04:37:04 +00:00
|
|
|
'compilationState' => 0,
|
2016-03-05 09:01:12 +00:00
|
|
|
],
|
2017-02-14 06:53:02 +00:00
|
|
|
], $user)
|
2016-03-05 09:01:12 +00:00
|
|
|
->seeStatusCode(200);
|
|
|
|
|
|
|
|
$songs = Song::orderBy('id', 'desc')->take(3)->get();
|
|
|
|
|
|
|
|
// 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.
|
|
|
|
$this->assertEquals($songs[0]->album->name, $originalSongs[0]->album->name);
|
|
|
|
$this->assertNotEquals($songs[0]->album->id, $originalSongs[0]->album->id);
|
|
|
|
$this->assertEquals($songs[1]->album->name, $originalSongs[1]->album->name);
|
|
|
|
$this->assertNotEquals($songs[1]->album->id, $originalSongs[1]->album->id);
|
|
|
|
$this->assertEquals($songs[2]->album->name, $originalSongs[2]->album->name);
|
|
|
|
$this->assertNotEquals($songs[2]->album->id, $originalSongs[2]->album->id);
|
|
|
|
|
|
|
|
// And of course, the new artist is...
|
|
|
|
$this->assertEquals('John Cena', $songs[0]->album->artist->name); // JOHN CENA!!!
|
|
|
|
$this->assertEquals('John Cena', $songs[1]->album->artist->name); // JOHN CENA!!!
|
|
|
|
$this->assertEquals('John Cena', $songs[2]->album->artist->name); // And... JOHN CENAAAAAAAAAAA!!!
|
|
|
|
}
|
2016-04-24 04:37:04 +00:00
|
|
|
|
|
|
|
public function testSingleUpdateAllInfoYesCompilation()
|
|
|
|
{
|
|
|
|
$song = Song::orderBy('id', 'desc')->first();
|
|
|
|
|
2017-02-14 06:53:02 +00:00
|
|
|
$user = factory(User::class, 'admin')->create();
|
|
|
|
$this->putAsUser('/api/songs', [
|
2016-04-24 04:37:04 +00:00
|
|
|
'songs' => [$song->id],
|
|
|
|
'data' => [
|
|
|
|
'title' => 'Foo Bar',
|
|
|
|
'artistName' => 'John Cena',
|
|
|
|
'albumName' => 'One by One',
|
|
|
|
'lyrics' => 'Lorem ipsum dolor sic amet.',
|
|
|
|
'track' => 1,
|
|
|
|
'compilationState' => 1,
|
|
|
|
],
|
2017-02-14 06:53:02 +00:00
|
|
|
], $user)
|
2016-04-24 04:37:04 +00:00
|
|
|
->seeStatusCode(200);
|
|
|
|
|
|
|
|
$compilationAlbum = Album::whereArtistIdAndName(Artist::VARIOUS_ID, 'One by One')->first();
|
|
|
|
$this->assertNotNull($compilationAlbum);
|
|
|
|
|
2017-04-23 16:01:02 +00:00
|
|
|
$artist = Artist::whereName('John Cena')->first();
|
|
|
|
$this->assertNotNull($artist);
|
2016-04-24 04:37:04 +00:00
|
|
|
|
|
|
|
$this->seeInDatabase('songs', [
|
|
|
|
'id' => $song->id,
|
2017-04-29 03:49:14 +00:00
|
|
|
'artist_id' => $artist->id,
|
2016-04-24 04:37:04 +00:00
|
|
|
'album_id' => $compilationAlbum->id,
|
|
|
|
'lyrics' => 'Lorem ipsum dolor sic amet.',
|
|
|
|
'track' => 1,
|
|
|
|
]);
|
|
|
|
|
|
|
|
// Now try changing stuff and make sure things work.
|
|
|
|
// Case 1: Keep compilation state and artist the same
|
2017-02-14 06:53:02 +00:00
|
|
|
$this->putAsUser('/api/songs', [
|
2016-04-24 04:37:04 +00:00
|
|
|
'songs' => [$song->id],
|
|
|
|
'data' => [
|
|
|
|
'title' => 'Barz Qux',
|
|
|
|
'artistName' => 'John Cena',
|
|
|
|
'albumName' => 'Two by Two',
|
|
|
|
'lyrics' => 'Lorem ipsum dolor sic amet.',
|
|
|
|
'track' => 1,
|
|
|
|
'compilationState' => 2,
|
|
|
|
],
|
2017-02-14 06:53:02 +00:00
|
|
|
], $user)
|
2017-06-03 23:21:50 +00:00
|
|
|
// ->seeText('jaja')
|
2016-04-24 04:37:04 +00:00
|
|
|
->seeStatusCode(200);
|
|
|
|
|
|
|
|
$compilationAlbum = Album::whereArtistIdAndName(Artist::VARIOUS_ID, 'Two by Two')->first();
|
|
|
|
$this->assertNotNull($compilationAlbum);
|
|
|
|
|
|
|
|
$contributingArtist = Artist::whereName('John Cena')->first();
|
|
|
|
$this->assertNotNull($contributingArtist);
|
|
|
|
|
|
|
|
$this->seeInDatabase('songs', [
|
|
|
|
'id' => $song->id,
|
2017-04-29 03:49:14 +00:00
|
|
|
'artist_id' => $contributingArtist->id,
|
2016-04-24 04:37:04 +00:00
|
|
|
'album_id' => $compilationAlbum->id,
|
|
|
|
]);
|
|
|
|
|
|
|
|
// Case 2: Keep compilation state, but change the artist.
|
2017-02-14 06:53:02 +00:00
|
|
|
$this->putAsUser('/api/songs', [
|
2016-04-24 04:37:04 +00:00
|
|
|
'songs' => [$song->id],
|
|
|
|
'data' => [
|
|
|
|
'title' => 'Barz Qux',
|
|
|
|
'artistName' => 'Foo Fighters',
|
|
|
|
'albumName' => 'One by One',
|
|
|
|
'lyrics' => 'Lorem ipsum dolor sic amet.',
|
|
|
|
'track' => 1,
|
|
|
|
'compilationState' => 2,
|
|
|
|
],
|
2017-02-14 06:53:02 +00:00
|
|
|
], $user)
|
2016-04-24 04:37:04 +00:00
|
|
|
->seeStatusCode(200);
|
|
|
|
|
|
|
|
$compilationAlbum = Album::whereArtistIdAndName(Artist::VARIOUS_ID, 'One by One')->first();
|
|
|
|
$this->assertNotNull($compilationAlbum);
|
|
|
|
|
|
|
|
$contributingArtist = Artist::whereName('Foo Fighters')->first();
|
|
|
|
$this->assertNotNull($contributingArtist);
|
|
|
|
|
|
|
|
$this->seeInDatabase('songs', [
|
|
|
|
'id' => $song->id,
|
2017-04-29 03:49:14 +00:00
|
|
|
'artist_id' => $contributingArtist->id,
|
2016-04-24 04:37:04 +00:00
|
|
|
'album_id' => $compilationAlbum->id,
|
|
|
|
]);
|
|
|
|
|
|
|
|
// Case 3: Change compilation state only
|
2017-02-14 06:53:02 +00:00
|
|
|
$this->putAsUser('/api/songs', [
|
2016-04-24 04:37:04 +00:00
|
|
|
'songs' => [$song->id],
|
|
|
|
'data' => [
|
|
|
|
'title' => 'Barz Qux',
|
|
|
|
'artistName' => 'Foo Fighters',
|
|
|
|
'albumName' => 'One by One',
|
|
|
|
'lyrics' => 'Lorem ipsum dolor sic amet.',
|
|
|
|
'track' => 1,
|
|
|
|
'compilationState' => 0,
|
|
|
|
],
|
2017-02-14 06:53:02 +00:00
|
|
|
], $user)
|
2016-04-24 04:37:04 +00:00
|
|
|
->seeStatusCode(200);
|
|
|
|
|
|
|
|
$artist = Artist::whereName('Foo Fighters')->first();
|
|
|
|
$this->assertNotNull($artist);
|
|
|
|
$album = Album::whereArtistIdAndName($artist->id, 'One by One')->first();
|
2017-04-23 16:01:02 +00:00
|
|
|
$this->assertNotNull($album);
|
2016-04-24 04:37:04 +00:00
|
|
|
|
|
|
|
$this->seeInDatabase('songs', [
|
|
|
|
'id' => $song->id,
|
2017-04-29 03:49:14 +00:00
|
|
|
'artist_id' => $artist->id,
|
2016-04-24 04:37:04 +00:00
|
|
|
'album_id' => $album->id,
|
|
|
|
]);
|
|
|
|
|
|
|
|
// Case 3: Change compilation state and artist
|
|
|
|
// Remember to set the compliation state back to 1
|
2017-02-14 06:53:02 +00:00
|
|
|
$this->putAsUser('/api/songs', [
|
2016-04-24 04:37:04 +00:00
|
|
|
'songs' => [$song->id],
|
|
|
|
'data' => [
|
|
|
|
'title' => 'Barz Qux',
|
|
|
|
'artistName' => 'Foo Fighters',
|
|
|
|
'albumName' => 'One by One',
|
|
|
|
'lyrics' => 'Lorem ipsum dolor sic amet.',
|
|
|
|
'track' => 1,
|
|
|
|
'compilationState' => 1,
|
|
|
|
],
|
2017-02-14 06:53:02 +00:00
|
|
|
], $user)
|
|
|
|
->putAsUser('/api/songs', [
|
2016-04-24 04:37:04 +00:00
|
|
|
'songs' => [$song->id],
|
|
|
|
'data' => [
|
|
|
|
'title' => 'Twilight of the Thunder God',
|
|
|
|
'artistName' => 'Amon Amarth',
|
|
|
|
'albumName' => 'Twilight of the Thunder God',
|
|
|
|
'lyrics' => 'Thor! Nanananananana Batman.',
|
|
|
|
'track' => 1,
|
|
|
|
'compilationState' => 0,
|
|
|
|
],
|
2017-02-14 06:53:02 +00:00
|
|
|
], $user)
|
2016-04-24 04:37:04 +00:00
|
|
|
->seeStatusCode(200);
|
|
|
|
|
|
|
|
$artist = Artist::whereName('Amon Amarth')->first();
|
|
|
|
$this->assertNotNull($artist);
|
|
|
|
$album = Album::whereArtistIdAndName($artist->id, 'Twilight of the Thunder God')->first();
|
|
|
|
$this->assertNotNull($album);
|
|
|
|
$this->seeInDatabase('songs', [
|
|
|
|
'id' => $song->id,
|
2017-04-29 03:49:14 +00:00
|
|
|
'artist_id' => $artist->id,
|
2016-04-24 04:37:04 +00:00
|
|
|
'album_id' => $album->id,
|
|
|
|
'lyrics' => 'Thor! Nanananananana Batman.', // haha
|
|
|
|
]);
|
|
|
|
}
|
2016-05-27 08:34:38 +00:00
|
|
|
|
2016-09-26 07:32:16 +00:00
|
|
|
public function testDeletingByChunk()
|
|
|
|
{
|
|
|
|
$this->assertNotEquals(0, Song::count());
|
|
|
|
$ids = Song::select('id')->get()->pluck('id')->all();
|
|
|
|
Song::deleteByChunk($ids, 'id', 1);
|
|
|
|
$this->assertEquals(0, Song::count());
|
|
|
|
}
|
2016-03-05 09:01:12 +00:00
|
|
|
}
|