2
0
Fork 0
mirror of https://github.com/koel/koel synced 2024-12-26 04:23:05 +00:00
koel/tests/Integration/Services/MediaSyncServiceTest.php

254 lines
7.9 KiB
PHP
Raw Normal View History

2015-12-13 04:42:28 +00:00
<?php
namespace Tests\Integration\Services;
2017-02-14 06:53:02 +00:00
2016-02-02 07:47:00 +00:00
use App\Events\LibraryChanged;
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;
use App\Services\FileSynchronizer;
2018-08-19 15:26:34 +00:00
use App\Services\MediaSyncService;
use getID3;
2022-07-07 10:45:57 +00:00
use Illuminate\Support\Arr;
2020-12-22 23:01:49 +00:00
use Mockery;
use Tests\Feature\TestCase;
2015-12-13 04:42:28 +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();
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);
}
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
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
// 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 */
$song = Song::where('path', $this->path('/subdir/back-in-black.ogg'))->first();
self::assertNotEmpty($song->album->cover);
// 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')]);
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 */
$album = Album::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 */
$song = Song::where('title', 'This song belongs to a compilation')->first();
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-07-07 10:45:57 +00:00
$song = Song::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-07-07 10:45:57 +00:00
$song = Song::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();
$song = Song::first();
$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-07-07 10:45:57 +00:00
$song = Song::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-07-07 10:45:57 +00:00
$song = Song::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(
Arr::except(Song::findOrFail($song->id)->toArray(), 'created_at'),
Arr::except($song->toArray(), 'created_at')
);
2016-07-05 10:14:12 +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
}
public function testSyncDeletedSongViaWatch(): void
2016-02-02 07:47:00 +00:00
{
$this->expectsEvents(LibraryChanged::class);
static::createSampleMediaSet();
2022-07-07 10:45:57 +00:00
$song = Song::first();
self::assertModelExists($song);
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
}
public function testSyncDeletedDirectoryViaWatch(): void
2016-02-02 07:47:00 +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
$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
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');
2020-12-22 23:01:49 +00:00
$this->swap(
getID3::class,
Mockery::mock(getID3::class, [
'analyze' => [
2022-07-07 10:45:57 +00:00
'filenamepath' => $path,
2020-12-22 23:01:49 +00:00
'tags' => [
'id3v2' => [
'title' => ['&#27700;&#35895;&#24195;&#23455;'],
'album' => ['&#23567;&#23721;&#20117;&#12371; Random'],
'artist' => ['&#20304;&#20489;&#32190;&#38899; Unknown'],
],
2016-05-05 15:03:30 +00:00
],
2020-12-22 23:01:49 +00:00
'encoding' => 'UTF-8',
'playtime_seconds' => 100,
2016-05-05 15:03:30 +00:00
],
2020-12-22 23:01:49 +00:00
])
);
2016-05-05 15:03:30 +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
}
public function testOptionallyIgnoreHiddenFiles(): void
{
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-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']);
}
2015-12-13 04:42:28 +00:00
}