From 2c50eee8310a90b2210ff5e4907908333f38915c Mon Sep 17 00:00:00 2001 From: MrMeshok <72924086+MrMeshok@users.noreply.github.com> Date: Mon, 6 May 2024 14:16:55 +0500 Subject: [PATCH] Fix manipulations not applying to non default collections (#3589) * Fix manipulations not applying to non default collections * Fix tests with incorrect manipulations --- src/Conversions/ConversionCollection.php | 2 +- .../Conversions/ConversionCollectionTest.php | 32 +++++++++++++++++++ .../Feature/Media/UpdateManipulationsTest.php | 18 ++++++----- 3 files changed, 43 insertions(+), 9 deletions(-) diff --git a/src/Conversions/ConversionCollection.php b/src/Conversions/ConversionCollection.php index a2a89d12b..bd8de7f87 100644 --- a/src/Conversions/ConversionCollection.php +++ b/src/Conversions/ConversionCollection.php @@ -96,7 +96,7 @@ protected function addManipulationToConversion(Manipulations $manipulations, str { /** @var Conversion|null $conversion */ $conversion = $this->first(function (Conversion $conversion) use ($conversionName) { - if (! in_array($this->media->collection_name, $conversion->getPerformOnCollections())) { + if (! $conversion->shouldBePerformedOn($this->media->collection_name)) { return false; } diff --git a/tests/Conversions/ConversionCollectionTest.php b/tests/Conversions/ConversionCollectionTest.php index 80e6450d6..5430b8b44 100644 --- a/tests/Conversions/ConversionCollectionTest.php +++ b/tests/Conversions/ConversionCollectionTest.php @@ -19,8 +19,17 @@ $secondMedia->manipulations = ['thumb' => ['greyscale' => [], 'height' => [20]]]; $secondMedia->save(); + $avatarMedia = $this->testModelWithConversion + ->addMedia($this->getTestJpg()) + ->preservingOriginal() + ->toMediaCollection('avatar'); + + $avatarMedia->manipulations = ['thumb' => ['greyscale' => [], 'height' => [10]]]; + $avatarMedia->save(); + $this->media = $media->fresh(); $this->secondMedia = $media->fresh(); + $this->avatarMedia = $avatarMedia->fresh(); }); it('will prepend the manipulation saved on the model and the wildmark manipulations', function () { @@ -75,6 +84,29 @@ ], $manipulations); }); +it('will prepend the manipulation saved on the model with non default collection', function () { + $conversionCollection = ConversionCollection::createForMedia($this->avatarMedia); + + $conversion = $conversionCollection->getConversions()[0]; + + expect($conversion->getName())->toEqual('thumb'); + + $manipulations = $conversion + ->getManipulations() + ->toArray(); + + $this->assertArrayHasKey('optimize', $manipulations); + + unset($manipulations['optimize']); + + $this->assertEquals([ + 'greyscale' => [], + 'height' => [10], + 'format' => ['jpg'], + 'width' => [50], + ], $manipulations); +}); + it('will apply the manipulation on the equally named conversion of every model', function () { $mediaItems = [$this->media, $this->secondMedia]; $manipulations = []; diff --git a/tests/Feature/Media/UpdateManipulationsTest.php b/tests/Feature/Media/UpdateManipulationsTest.php index 4d6175de5..2c61e18ea 100644 --- a/tests/Feature/Media/UpdateManipulationsTest.php +++ b/tests/Feature/Media/UpdateManipulationsTest.php @@ -23,8 +23,8 @@ public function registerMediaConversions(?Media $media = null): void $media->manipulations = [ 'update_test' => [ - 'width' => 1, - 'height' => 1, + 'width' => [1], + 'height' => [1], ], ]; @@ -51,9 +51,10 @@ public function registerMediaConversions(?Media $media = null): void $media->manipulations = [ 'update_test' => [ - 'width' => 1, - 'height' => 1, - ], ]; + 'width' => [1], + 'height' => [1], + ], + ]; $media->save(); @@ -63,9 +64,10 @@ public function registerMediaConversions(?Media $media = null): void $media->manipulations = [ 'update_test' => [ - 'width' => 1, - 'height' => 1, - ], ]; + 'width' => [1], + 'height' => [1], + ], + ]; $media->updated_at = now()->addSecond();