Skip to content

Commit

Permalink
Correctly filtering conversions which will be applied to media in dif…
Browse files Browse the repository at this point in the history
…ferent collections of the same subject.
  • Loading branch information
StyxUA committed Oct 20, 2024
1 parent 9769f82 commit bb26bf6
Show file tree
Hide file tree
Showing 2 changed files with 110 additions and 6 deletions.
17 changes: 11 additions & 6 deletions src/Conversions/ConversionCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,11 @@ public function setMedia(Media $media): self

public function getByName(string $name): Conversion
{
$conversion = $this->first(fn (Conversion $conversion) => $conversion->getName() === $name);
$conversion = $this
->getConversions($this->media->collection_name)
->first(fn (Conversion $conversion) => $conversion->getName() === $name);

if (! $conversion) {
if (!$conversion) {
throw InvalidConversion::unknownName($name);
}

Expand All @@ -51,7 +53,7 @@ protected function addConversionsFromRelatedModel(Media $media): void
{
$modelName = Arr::get(Relation::morphMap(), $media->model_type, $media->model_type);

if (! class_exists($modelName)) {
if (!class_exists($modelName)) {
return;
}

Expand Down Expand Up @@ -89,14 +91,16 @@ public function getConversions(string $collectionName = ''): self
return $this;
}

return $this->filter(fn (Conversion $conversion) => $conversion->shouldBePerformedOn($collectionName));
return $this
->filter(fn (Conversion $conversion) => $conversion->shouldBePerformedOn($collectionName))
->values();
}

protected function addManipulationToConversion(Manipulations $manipulations, string $conversionName): void
{
/** @var Conversion|null $conversion */
$conversion = $this->first(function (Conversion $conversion) use ($conversionName) {
if (! $conversion->shouldBePerformedOn($this->media->collection_name)) {
if (!$conversion->shouldBePerformedOn($this->media->collection_name)) {
return false;
}

Expand All @@ -113,7 +117,7 @@ protected function addManipulationToConversion(Manipulations $manipulations, str

if ($conversionName === '*') {
$this->each(
fn (Conversion $conversion) => $conversion->addAsFirstManipulations(clone $manipulations)
fn (Conversion $conversion) => $conversion->addAsFirstManipulations(clone $manipulations),
);
}
}
Expand All @@ -124,4 +128,5 @@ public function getConversionsFiles(string $collectionName = ''): self
->getConversions($collectionName)
->map(fn (Conversion $conversion) => $conversion->getConversionFile($this->media));
}

}
99 changes: 99 additions & 0 deletions tests/Conversions/ConversionCollectionConversionsTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
<?php

use Spatie\Image\Enums\Fit;
use Spatie\MediaLibrary\Conversions\Conversion;
use Spatie\MediaLibrary\Conversions\ConversionCollection;
use Spatie\MediaLibrary\MediaCollections\Models\Media;
use Spatie\MediaLibrary\Tests\TestSupport\TestModels\TestModel;

beforeEach(function () {
$this->model = (new class extends TestModel
{
public function registerMediaConversions(?Media $media = null): void
{
$this
->addMediaConversion('preview')
->fit(Fit::Crop, 50, 50)
->performOnCollections('avatar')
->format('png')
->nonQueued();

$this
->addMediaConversion('preview')
->fit(Fit::Crop, 300, 100)
->performOnCollections('signature')
->format('jpeg')
->nonQueued();

$this
->addMediaConversion('web')
->format('webp')
->nonQueued();
}

public function registerMediaCollections(): void
{
$this->addMediaCollection('avatar')->acceptsMimeTypes(['image/png'])->singleFile();
$this->addMediaCollection('signature')->acceptsMimeTypes(['image/jpeg'])->singleFile();
}
})::create(['name' => 'testmodel']);

$avatarMedia = $this->model
->addMedia($this->getTestPng())
->preservingOriginal()
->toMediaCollection('avatar');
$avatarMedia->save();

$signatureMedia = $this->model
->addMedia($this->getTestJpg())
->preservingOriginal()
->toMediaCollection('signature');
$signatureMedia->save();

$this->avatarMedia = $avatarMedia->refresh();
$this->signatureMedia = $signatureMedia->refresh();
});

it('will apply correct conversions for media in different collections', function () {
$conversionCollection = ConversionCollection::createForMedia($this->avatarMedia);
/** @var ConversionCollection<int, Conversion> $conversions */
$conversions = $conversionCollection->getConversions('avatar');

expect($conversions->count())->toBe(2)
->and($conversions->first()->getName())->toBe('preview')
->and($conversions->first()->getResultExtension())->toBe('png')
->and($conversions->first()->getManipulations()->toArray())->toMatchArray([
'fit' => [Fit::Crop, 50, 50],
'format' => ['png'],
])
->and($conversions->last()->getName())->toBe('web')
->and($conversions->last()->getResultExtension())->toBe('webp')
->and($conversions->last()->getManipulations()->toArray())->toMatchArray([
'format' => ['webp'],
]);

$conversionCollection = ConversionCollection::createForMedia($this->signatureMedia);
/** @var ConversionCollection<int, Conversion> $conversions */
$conversions = $conversionCollection->getConversions('signature');

expect($conversions->count())->toBe(2)
->and($conversions->first()->getName())->toBe('preview')
->and($conversions->first()->getResultExtension())->toBe('jpeg')
->and($conversions->first()->getManipulations()->toArray())->toMatchArray([
'fit' => [Fit::Crop, 300, 100],
'format' => ['jpeg'],
])
->and($conversions->last()->getName())->toBe('web')
->and($conversions->last()->getResultExtension())->toBe('webp')
->and($conversions->last()->getManipulations()->toArray())->toMatchArray([
'format' => ['webp'],
]);
});

it('will generate correct filenames for media in different collections but with identically named conversions', function () {
expect($this->model->getFirstMediaUrl('avatar', 'preview'))->toEndWith('.png');
expect($this->model->getFirstMediaUrl('avatar', 'web'))->toEndWith('.webp');

expect($this->model->getFirstMediaUrl('signature', 'preview'))->toEndWith('.jpeg');
expect($this->model->getFirstMediaUrl('signature', 'web'))->toEndWith('.webp');
});

0 comments on commit bb26bf6

Please sign in to comment.