From d7e28eb32d29af9b388ecc3290797ad0428b20cd Mon Sep 17 00:00:00 2001 From: Erik Sadewater <72336569+esadewater@users.noreply.github.com> Date: Tue, 10 Sep 2024 14:29:48 +0200 Subject: [PATCH 1/2] Fix renaming files with custom model Fixes renaming of files when using a custom model with a primary key name other than 'id'. --- src/MediaCollections/Filesystem.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/MediaCollections/Filesystem.php b/src/MediaCollections/Filesystem.php index a3aed27ab..e67accfeb 100644 --- a/src/MediaCollections/Filesystem.php +++ b/src/MediaCollections/Filesystem.php @@ -298,7 +298,7 @@ protected function renameMediaFile(Media $media): void protected function renameConversionFiles(Media $media): void { - $mediaWithOldFileName = config('media-library.media_model')::find($media->id); + $mediaWithOldFileName = config('media-library.media_model')::find($media->getKey()); $mediaWithOldFileName->file_name = $mediaWithOldFileName->getOriginal('file_name'); $conversionDirectory = $this->getConversionDirectory($media); From 9f2bac47c1b727aa9c2813ce5ee9ce02e65762de Mon Sep 17 00:00:00 2001 From: Erik Sadewater Date: Thu, 19 Sep 2024 21:14:52 +0200 Subject: [PATCH 2/2] test: Added test for custom key name Added test media model with custom key name, migration to alter media table with custom key name and test to rename files on a model with custom key name --- tests/Feature/Media/RenameTest.php | 23 +++++++++++++++++++ tests/TestCase.php | 17 ++++++++++++++ .../TestCustomMediaWithCustomKeyName.php | 12 ++++++++++ 3 files changed, 52 insertions(+) create mode 100644 tests/TestSupport/TestModels/TestCustomMediaWithCustomKeyName.php diff --git a/tests/Feature/Media/RenameTest.php b/tests/Feature/Media/RenameTest.php index db02ab62e..975a1270c 100644 --- a/tests/Feature/Media/RenameTest.php +++ b/tests/Feature/Media/RenameTest.php @@ -1,5 +1,8 @@ getTestFilesDirectory('test.jpg'); @@ -14,6 +17,26 @@ $this->assertFileExists($this->getMediaDirectory($media->id.'/test-new-name.jpg')); }); +it('will rename the file with a custom model with custom key name', function () { + config()->set('media-library.media_model', TestCustomMediaWithCustomKeyName::class); + + (new MediaLibraryServiceProvider(app()))->register()->boot(); + + $this->setUpDatabaseCustomKeyName(); + + $testFile = $this->getTestFilesDirectory('test.jpg'); + + $media = $this->testModel->addMedia($testFile)->toMediaCollection(); + + $this->assertFileExists($this->getMediaDirectory($media->getKey().'/test.jpg')); + + $media->file_name = 'test-new-name.jpg'; + $media->save(); + + $this->assertFileDoesNotExist($this->getMediaDirectory($media->getKey().'/test.jpg')); + $this->assertFileExists($this->getMediaDirectory($media->getKey().'/test-new-name.jpg')); +}); + it('will rename conversions', function () { $testFile = $this->getTestFilesDirectory('test.jpg'); diff --git a/tests/TestCase.php b/tests/TestCase.php index 902fdcb50..aaac1169a 100644 --- a/tests/TestCase.php +++ b/tests/TestCase.php @@ -5,9 +5,11 @@ use CreateTemporaryUploadsTable; use Dotgetenv\Dotgetenv; use Illuminate\Database\Eloquent\Relations\Relation; +use Illuminate\Database\Migrations\Migration; use Illuminate\Database\Schema\Blueprint; use Illuminate\Support\Facades\File; use Orchestra\Testbench\TestCase as Orchestra; +use Schema; use Spatie\MediaLibrary\MediaLibraryServiceProvider; use Spatie\MediaLibrary\Support\MediaLibraryPro; use Spatie\MediaLibrary\Tests\TestSupport\TestModels\TestModel; @@ -153,6 +155,21 @@ protected function setUpDatabase($app) $mediaTableMigration->up(); } + protected function setUpDatabaseCustomKeyName() + { + $customKeyNameMigration = new class extends Migration + { + public function up() + { + Schema::table('media', function (Blueprint $table) { + $table->renameColumn('id', 'custom_key_id'); + }); + } + }; + + $customKeyNameMigration->up(); + } + protected function setUpTempTestFiles() { $this->initializeDirectory($this->getTestFilesDirectory()); diff --git a/tests/TestSupport/TestModels/TestCustomMediaWithCustomKeyName.php b/tests/TestSupport/TestModels/TestCustomMediaWithCustomKeyName.php new file mode 100644 index 000000000..79b681ce7 --- /dev/null +++ b/tests/TestSupport/TestModels/TestCustomMediaWithCustomKeyName.php @@ -0,0 +1,12 @@ +