From 3bd4db450de3bf7b0824e541c073d1cfbdb0bfc3 Mon Sep 17 00:00:00 2001 From: Erik Sadewater Date: Thu, 19 Sep 2024 20:47:44 +0200 Subject: [PATCH] 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 @@ +