2015-12-13 04:42:28 +00:00
|
|
|
<?php
|
|
|
|
|
2021-12-06 16:12:47 +00:00
|
|
|
namespace Tests\Integration\Services;
|
2017-02-14 06:53:02 +00:00
|
|
|
|
2016-02-02 07:47:00 +00:00
|
|
|
use App\Events\LibraryChanged;
|
2021-12-06 16:12:47 +00:00
|
|
|
use App\Events\MediaSyncCompleted;
|
2016-02-04 15:04:53 +00:00
|
|
|
use App\Libraries\WatchRecord\InotifyWatchRecord;
|
2015-12-13 04:42:28 +00:00
|
|
|
use App\Models\Album;
|
2016-04-24 04:37:04 +00:00
|
|
|
use App\Models\Artist;
|
2022-07-07 10:45:57 +00:00
|
|
|
use App\Models\Setting;
|
2015-12-13 04:42:28 +00:00
|
|
|
use App\Models\Song;
|
2018-08-29 09:41:24 +00:00
|
|
|
use App\Services\FileSynchronizer;
|
2018-08-19 15:26:34 +00:00
|
|
|
use App\Services\MediaSyncService;
|
2019-10-09 17:36:22 +00:00
|
|
|
use getID3;
|
2022-07-07 10:45:57 +00:00
|
|
|
use Illuminate\Support\Arr;
|
2020-12-22 23:01:49 +00:00
|
|
|
use Mockery;
|
2021-12-06 16:12:47 +00:00
|
|
|
use Tests\Feature\TestCase;
|
2015-12-13 04:42:28 +00:00
|
|
|
|
2021-12-06 16:12:47 +00:00
|
|
|
class MediaSyncServiceTest extends TestCase
|
2015-12-13 04:42:28 +00:00
|
|
|
{
|
2022-07-07 10:45:57 +00:00
|
|
|
private MediaSyncService $mediaService;
|
2018-08-19 09:05:33 +00:00
|
|
|
|
2019-07-22 07:03:23 +00:00
|
|
|
public function setUp(): void
|
2018-08-19 09:05:33 +00:00
|
|
|
{
|
|
|
|
parent::setUp();
|
2020-11-14 16:57:25 +00:00
|
|
|
|
2022-07-07 10:45:57 +00:00
|
|
|
Setting::set('media_path', realpath($this->mediaPath));
|
2018-08-19 15:26:34 +00:00
|
|
|
$this->mediaService = app(MediaSyncService::class);
|
2018-08-19 09:05:33 +00:00
|
|
|
}
|
|
|
|
|
2022-07-07 10:45:57 +00:00
|
|
|
private function path($subPath): string
|
|
|
|
{
|
|
|
|
return realpath($this->mediaPath . $subPath);
|
|
|
|
}
|
|
|
|
|
2020-11-14 16:57:25 +00:00
|
|
|
public function testSync(): void
|
2015-12-13 04:42:28 +00:00
|
|
|
{
|
2022-07-07 10:45:57 +00:00
|
|
|
$this->expectsEvents(MediaSyncCompleted::class);
|
2016-02-02 07:47:00 +00:00
|
|
|
|
2022-07-07 10:45:57 +00:00
|
|
|
$this->mediaService->sync();
|
2015-12-13 04:42:28 +00:00
|
|
|
|
|
|
|
// Standard mp3 files under root path should be recognized
|
2021-12-06 16:12:47 +00:00
|
|
|
self::assertDatabaseHas(Song::class, [
|
2022-07-07 10:45:57 +00:00
|
|
|
'path' => $this->path('/full.mp3'),
|
2016-03-20 14:14:02 +00:00
|
|
|
'track' => 5,
|
|
|
|
]);
|
2015-12-13 04:42:28 +00:00
|
|
|
|
|
|
|
// Ogg files and audio files in subdirectories should be recognized
|
2022-07-07 10:45:57 +00:00
|
|
|
self::assertDatabaseHas(Song::class, ['path' => $this->path('/subdir/back-in-black.ogg')]);
|
2015-12-13 04:42:28 +00:00
|
|
|
|
2016-08-07 10:30:55 +00:00
|
|
|
// GitHub issue #380. folder.png should be copied and used as the cover for files
|
|
|
|
// under subdir/
|
2022-07-07 10:45:57 +00:00
|
|
|
/** @var Song $song */
|
2022-08-09 18:45:11 +00:00
|
|
|
$song = Song::query()->where('path', $this->path('/subdir/back-in-black.ogg'))->first();
|
2022-07-07 10:45:57 +00:00
|
|
|
self::assertNotEmpty($song->album->cover);
|
2016-08-07 10:30:55 +00:00
|
|
|
|
2016-01-26 06:30:48 +00:00
|
|
|
// File search shouldn't be case-sensitive.
|
2022-07-07 10:45:57 +00:00
|
|
|
self::assertDatabaseHas(Song::class, ['path' => $this->path('/subdir/no-name.mp3')]);
|
2016-01-26 06:30:48 +00:00
|
|
|
|
2015-12-13 04:42:28 +00:00
|
|
|
// Non-audio files shouldn't be recognized
|
2022-07-07 10:45:57 +00:00
|
|
|
self::assertDatabaseMissing(Song::class, ['path' => $this->path('/rubbish.log')]);
|
2015-12-13 04:42:28 +00:00
|
|
|
|
|
|
|
// Broken/corrupted audio files shouldn't be recognized
|
2022-07-07 10:45:57 +00:00
|
|
|
self::assertDatabaseMissing(Song::class, ['path' => $this->path('/fake.mp3')]);
|
2015-12-13 04:42:28 +00:00
|
|
|
|
|
|
|
// Artists should be created
|
2022-07-07 10:45:57 +00:00
|
|
|
self::assertDatabaseHas(Artist::class, ['name' => 'Cuckoo']);
|
|
|
|
self::assertDatabaseHas(Artist::class, ['name' => 'Koel']);
|
2015-12-13 04:42:28 +00:00
|
|
|
|
|
|
|
// Albums should be created
|
2022-07-07 10:45:57 +00:00
|
|
|
self::assertDatabaseHas(Album::class, ['name' => 'Koel Testing Vol. 1']);
|
2015-12-13 04:42:28 +00:00
|
|
|
|
|
|
|
// Albums and artists should be correctly linked
|
2022-07-07 10:45:57 +00:00
|
|
|
/** @var Album $album */
|
2022-08-09 18:45:11 +00:00
|
|
|
$album = Album::query()->where('name', 'Koel Testing Vol. 1')->first();
|
2020-09-06 18:21:39 +00:00
|
|
|
self::assertEquals('Koel', $album->artist->name);
|
2015-12-13 04:42:28 +00:00
|
|
|
|
2016-04-24 04:37:04 +00:00
|
|
|
// Compilation albums, artists and songs must be recognized
|
2022-07-07 10:45:57 +00:00
|
|
|
/** @var Song $song */
|
2022-08-09 18:45:11 +00:00
|
|
|
$song = Song::query()->where('title', 'This song belongs to a compilation')->first();
|
2022-07-07 10:45:57 +00:00
|
|
|
self::assertFalse($song->album->artist->is($song->artist));
|
|
|
|
self::assertSame('Koel', $song->album->artist->name);
|
|
|
|
self::assertSame('Cuckoo', $song->artist->name);
|
|
|
|
}
|
2016-04-24 04:37:04 +00:00
|
|
|
|
2022-07-07 10:45:57 +00:00
|
|
|
public function testModifiedFileIsResynced(): void
|
|
|
|
{
|
|
|
|
$this->mediaService->sync();
|
2015-12-13 04:42:28 +00:00
|
|
|
|
2022-08-09 18:45:11 +00:00
|
|
|
/** @var Song $song */
|
|
|
|
$song = Song::query()->first();
|
2015-12-13 04:42:28 +00:00
|
|
|
|
2022-07-07 10:45:57 +00:00
|
|
|
touch($song->path, $time = time() + 1000);
|
|
|
|
$this->mediaService->sync();
|
2015-12-13 04:42:28 +00:00
|
|
|
|
2022-07-07 10:45:57 +00:00
|
|
|
self::assertSame($time, $song->refresh()->mtime);
|
2015-12-13 04:42:28 +00:00
|
|
|
}
|
2016-02-02 07:47:00 +00:00
|
|
|
|
2022-07-07 10:45:57 +00:00
|
|
|
public function testResyncWithoutForceDoesNotResetData(): void
|
2016-03-22 08:22:39 +00:00
|
|
|
{
|
2022-07-07 10:45:57 +00:00
|
|
|
$this->expectsEvents(MediaSyncCompleted::class);
|
2016-03-22 08:22:39 +00:00
|
|
|
|
2022-07-07 10:45:57 +00:00
|
|
|
$this->mediaService->sync();
|
2016-03-22 08:22:39 +00:00
|
|
|
|
2022-08-09 18:45:11 +00:00
|
|
|
/** @var Song $song */
|
|
|
|
$song = Song::query()->first();
|
2016-03-22 08:22:39 +00:00
|
|
|
|
|
|
|
$song->update([
|
|
|
|
'title' => "It's John Cena!",
|
|
|
|
'lyrics' => 'Booom Wroooom',
|
|
|
|
]);
|
|
|
|
|
2022-07-07 10:45:57 +00:00
|
|
|
$this->mediaService->sync();
|
2016-03-22 08:22:39 +00:00
|
|
|
|
2022-07-07 10:45:57 +00:00
|
|
|
$song->refresh();
|
|
|
|
self::assertSame("It's John Cena!", $song->title);
|
|
|
|
self::assertSame('Booom Wroooom', $song->lyrics);
|
|
|
|
}
|
2016-03-22 08:22:39 +00:00
|
|
|
|
2022-07-07 10:45:57 +00:00
|
|
|
public function testForceSyncResetsData(): void
|
|
|
|
{
|
|
|
|
$this->expectsEvents(MediaSyncCompleted::class);
|
2016-03-22 08:22:39 +00:00
|
|
|
|
2022-07-07 10:45:57 +00:00
|
|
|
$this->mediaService->sync();
|
|
|
|
|
2022-08-09 18:45:11 +00:00
|
|
|
/** @var Song $song */
|
|
|
|
$song = Song::query()->first();
|
2022-07-07 10:45:57 +00:00
|
|
|
|
|
|
|
$song->update([
|
|
|
|
'title' => "It's John Cena!",
|
|
|
|
'lyrics' => 'Booom Wroooom',
|
|
|
|
]);
|
|
|
|
|
|
|
|
$this->mediaService->sync(force: true);
|
|
|
|
|
|
|
|
$song->refresh();
|
|
|
|
|
|
|
|
self::assertNotSame("It's John Cena!", $song->title);
|
|
|
|
self::assertNotSame('Booom Wroooom', $song->lyrics);
|
2016-03-22 08:22:39 +00:00
|
|
|
}
|
|
|
|
|
2022-07-07 10:45:57 +00:00
|
|
|
public function testSyncWithIgnoredTags(): void
|
2016-03-22 08:22:39 +00:00
|
|
|
{
|
2022-07-07 10:45:57 +00:00
|
|
|
$this->expectsEvents(MediaSyncCompleted::class);
|
2016-03-22 08:22:39 +00:00
|
|
|
|
2022-07-07 10:45:57 +00:00
|
|
|
$this->mediaService->sync();
|
2016-03-22 08:22:39 +00:00
|
|
|
|
2022-08-09 18:45:11 +00:00
|
|
|
/** @var Song $song */
|
|
|
|
$song = Song::query()->first();
|
2016-03-22 08:22:39 +00:00
|
|
|
|
|
|
|
$song->update([
|
|
|
|
'title' => "It's John Cena!",
|
|
|
|
'lyrics' => 'Booom Wroooom',
|
|
|
|
]);
|
|
|
|
|
2022-07-07 10:45:57 +00:00
|
|
|
$this->mediaService->sync(ignores: ['title'], force: true);
|
2016-03-22 08:22:39 +00:00
|
|
|
|
2022-07-07 10:45:57 +00:00
|
|
|
$song->refresh();
|
|
|
|
|
|
|
|
self::assertSame("It's John Cena!", $song->title);
|
|
|
|
self::assertNotSame('Booom Wroooom', $song->lyrics);
|
2016-03-22 08:22:39 +00:00
|
|
|
}
|
|
|
|
|
2022-07-07 10:45:57 +00:00
|
|
|
public function testSyncAllTagsForNewFilesRegardlessOfIgnoredOption(): void
|
2016-07-05 10:14:12 +00:00
|
|
|
{
|
2022-07-07 10:45:57 +00:00
|
|
|
$this->mediaService->sync();
|
2017-08-05 21:20:05 +00:00
|
|
|
|
2022-08-09 18:45:11 +00:00
|
|
|
/** @var Song $song */
|
|
|
|
$song = Song::query()->first();
|
|
|
|
|
2016-07-05 10:14:12 +00:00
|
|
|
$song->delete();
|
|
|
|
|
2022-07-07 10:45:57 +00:00
|
|
|
$this->mediaService->sync(ignores: ['title', 'disc', 'track'], force: true);
|
2017-06-03 16:21:53 +00:00
|
|
|
|
2022-07-07 10:45:57 +00:00
|
|
|
// Song should be added back with all info
|
|
|
|
self::assertEquals(
|
2022-08-09 18:45:11 +00:00
|
|
|
Arr::except(Song::query()->where('path', $song->path)->first()->toArray(), ['id', 'created_at']),
|
2022-08-01 10:42:33 +00:00
|
|
|
Arr::except($song->toArray(), ['id', 'created_at'])
|
2022-07-07 10:45:57 +00:00
|
|
|
);
|
2016-07-05 10:14:12 +00:00
|
|
|
}
|
|
|
|
|
2020-11-14 16:57:25 +00:00
|
|
|
public function testSyncAddedSongViaWatch(): void
|
2016-02-02 07:47:00 +00:00
|
|
|
{
|
2017-01-06 03:04:08 +00:00
|
|
|
$this->expectsEvents(LibraryChanged::class);
|
|
|
|
|
2022-07-07 10:45:57 +00:00
|
|
|
$path = $this->path('/blank.mp3');
|
2018-08-19 09:05:33 +00:00
|
|
|
$this->mediaService->syncByWatchRecord(new InotifyWatchRecord("CLOSE_WRITE,CLOSE $path"));
|
2016-02-02 07:47:00 +00:00
|
|
|
|
2022-07-07 10:45:57 +00:00
|
|
|
self::assertDatabaseHas(Song::class, ['path' => $path]);
|
2016-02-02 07:47:00 +00:00
|
|
|
}
|
|
|
|
|
2020-11-14 16:57:25 +00:00
|
|
|
public function testSyncDeletedSongViaWatch(): void
|
2016-02-02 07:47:00 +00:00
|
|
|
{
|
|
|
|
$this->expectsEvents(LibraryChanged::class);
|
|
|
|
|
2020-04-27 18:55:12 +00:00
|
|
|
static::createSampleMediaSet();
|
2022-08-09 18:45:11 +00:00
|
|
|
|
|
|
|
/** @var Song $song */
|
|
|
|
$song = Song::query()->first();
|
2016-02-02 07:47:00 +00:00
|
|
|
|
2022-07-07 10:45:57 +00:00
|
|
|
$this->mediaService->syncByWatchRecord(new InotifyWatchRecord("DELETE $song->path"));
|
2016-02-02 07:47:00 +00:00
|
|
|
|
2022-07-07 10:45:57 +00:00
|
|
|
self::assertModelMissing($song);
|
2016-02-02 07:47:00 +00:00
|
|
|
}
|
|
|
|
|
2020-11-14 16:57:25 +00:00
|
|
|
public function testSyncDeletedDirectoryViaWatch(): void
|
2016-02-02 07:47:00 +00:00
|
|
|
{
|
2021-12-06 16:12:47 +00:00
|
|
|
$this->expectsEvents(LibraryChanged::class, MediaSyncCompleted::class);
|
2016-02-02 07:47:00 +00:00
|
|
|
|
2022-07-07 10:45:57 +00:00
|
|
|
$this->mediaService->sync();
|
2016-02-02 07:47:00 +00:00
|
|
|
|
2021-12-06 16:12:47 +00:00
|
|
|
$this->mediaService->syncByWatchRecord(new InotifyWatchRecord("MOVED_FROM,ISDIR $this->mediaPath/subdir"));
|
2016-02-02 07:47:00 +00:00
|
|
|
|
2022-07-07 10:45:57 +00:00
|
|
|
self::assertDatabaseMissing('songs', ['path' => $this->path('/subdir/sic.mp3')]);
|
|
|
|
self::assertDatabaseMissing('songs', ['path' => $this->path('/subdir/no-name.mp3')]);
|
|
|
|
self::assertDatabaseMissing('songs', ['path' => $this->path('/subdir/back-in-black.mp3')]);
|
2016-02-02 07:47:00 +00:00
|
|
|
}
|
2016-05-05 15:03:30 +00:00
|
|
|
|
2020-11-14 16:57:25 +00:00
|
|
|
public function testHtmlEntities(): void
|
2016-05-05 15:03:30 +00:00
|
|
|
{
|
2022-07-07 10:45:57 +00:00
|
|
|
$path = $this->path('/songs/blank.mp3');
|
2022-08-31 21:27:40 +00:00
|
|
|
$analyzed = [
|
|
|
|
'filenamepath' => $path,
|
|
|
|
'tags' => [
|
|
|
|
'id3v2' => [
|
|
|
|
'title' => ['水谷広実'],
|
|
|
|
'album' => ['小岩井こ Random'],
|
|
|
|
'artist' => ['佐倉綾音 Unknown'],
|
|
|
|
],
|
|
|
|
],
|
|
|
|
'encoding' => 'UTF-8',
|
|
|
|
'playtime_seconds' => 100,
|
|
|
|
];
|
2022-07-07 10:45:57 +00:00
|
|
|
|
2020-12-22 23:01:49 +00:00
|
|
|
$this->swap(
|
|
|
|
getID3::class,
|
|
|
|
Mockery::mock(getID3::class, [
|
2022-08-31 21:27:40 +00:00
|
|
|
'CopyTagsToComments' => $analyzed,
|
|
|
|
'analyze' => $analyzed,
|
2020-12-22 23:01:49 +00:00
|
|
|
])
|
|
|
|
);
|
2016-05-05 15:03:30 +00:00
|
|
|
|
2018-08-29 09:41:24 +00:00
|
|
|
/** @var FileSynchronizer $fileSynchronizer */
|
|
|
|
$fileSynchronizer = app(FileSynchronizer::class);
|
2022-07-07 10:45:57 +00:00
|
|
|
$info = $fileSynchronizer->setFile($path)->getFileScanInformation();
|
2016-05-05 15:03:30 +00:00
|
|
|
|
2022-07-07 10:45:57 +00:00
|
|
|
self::assertSame('佐倉綾音 Unknown', $info->artistName);
|
|
|
|
self::assertSame('小岩井こ Random', $info->albumName);
|
|
|
|
self::assertSame('水谷広実', $info->title);
|
2016-05-05 15:03:30 +00:00
|
|
|
}
|
2016-09-17 15:56:58 +00:00
|
|
|
|
2020-11-14 16:57:25 +00:00
|
|
|
public function testOptionallyIgnoreHiddenFiles(): void
|
2016-09-17 15:56:58 +00:00
|
|
|
{
|
2016-09-19 02:08:50 +00:00
|
|
|
config(['koel.ignore_dot_files' => false]);
|
2022-07-07 10:45:57 +00:00
|
|
|
$this->mediaService->sync();
|
|
|
|
self::assertDatabaseHas(Album::class, ['name' => 'Hidden Album']);
|
2016-09-17 15:56:58 +00:00
|
|
|
|
2016-09-19 02:08:50 +00:00
|
|
|
config(['koel.ignore_dot_files' => true]);
|
2022-07-07 10:45:57 +00:00
|
|
|
$this->mediaService->sync();
|
|
|
|
self::assertDatabaseMissing(Album::class, ['name' => 'Hidden Album']);
|
2016-09-17 15:56:58 +00:00
|
|
|
}
|
2015-12-13 04:42:28 +00:00
|
|
|
}
|