Skip to content

Commit

Permalink
Correctly remove media folders after removing all media files (#3688)
Browse files Browse the repository at this point in the history
* Typo fix

* Moved removing media files after removing conversions and responsive files.

* Removed unused import from test.
  • Loading branch information
StyxUA authored Aug 9, 2024
1 parent 65e10ad commit 5ccca6d
Show file tree
Hide file tree
Showing 3 changed files with 110 additions and 3 deletions.
4 changes: 2 additions & 2 deletions src/Support/FileRemover/DefaultFileRemover.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,14 @@ public function removeAllFiles(Media $media): void
{

if ($media->conversions_disk && $media->disk !== $media->conversions_disk) {
$this->removeFromMediaDirectory($media, $media->conversions_disk);
$this->removeFromConversionsDirectory($media, $media->conversions_disk);
$this->removeFromResponsiveImagesDirectory($media, $media->conversions_disk);
$this->removeFromMediaDirectory($media, $media->conversions_disk);
}

$this->removeFromMediaDirectory($media, $media->disk);
$this->removeFromConversionsDirectory($media, $media->disk);
$this->removeFromResponsiveImagesDirectory($media, $media->disk);
$this->removeFromMediaDirectory($media, $media->disk);
}

public function removeFromMediaDirectory(Media $media, string $disk): void
Expand Down
107 changes: 107 additions & 0 deletions tests/Feature/FileAdder/MediaConversions/DeleteMediaFolderTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
<?php

use Illuminate\Support\Facades\File;

beforeEach(function () {
foreach (range(1, 3) as $index) {
$this->testModelWithoutMediaConversions
->addMedia($this->getTestJpg())
->preservingOriginal()
->toMediaCollection();

$this->testModelWithMultipleConversions
->addMedia($this->getTestJpg())
->preservingOriginal()
->toMediaCollection();

$this->testModelWithResponsiveImages
->addMedia($this->getTestJpg())
->preservingOriginal()
->toMediaCollection();
}
});

it('will remove the media folder when deleting a media model without conversions', function () {
$ids = $this->testModelWithoutMediaConversions->getMedia()->pluck('id');

$ids->map(function ($id) {
expect(File::isDirectory($this->getMediaDirectory($id)))->toBeTrue();
});

$this->testModelWithoutMediaConversions->clearMediaCollection();

$ids->map(function ($id) {
expect(File::isDirectory($this->getMediaDirectory($id)))->toBeFalse();
});
});

it('will remove the media folder when deleting a subject without media conversions', function () {
$ids = $this->testModelWithoutMediaConversions->getMedia()->pluck('id');

$ids->map(function ($id) {
expect(File::isDirectory($this->getMediaDirectory($id)))->toBeTrue();
});

$this->testModelWithoutMediaConversions->delete();

$ids->map(function ($id) {
expect(File::isDirectory($this->getMediaDirectory($id)))->toBeFalse();
});
});

it('will remove the media folder when deleting a media model with conversions', function () {
$ids = $this->testModelWithMultipleConversions->getMedia()->pluck('id');

$ids->map(function ($id) {
expect(File::isDirectory($this->getMediaDirectory($id)))->toBeTrue();
});

$this->testModelWithMultipleConversions->clearMediaCollection();

$ids->map(function ($id) {
expect(File::isDirectory($this->getMediaDirectory($id)))->toBeFalse();
});
});

it('will remove the media folder when deleting a subject with media conversions', function () {
$ids = $this->testModelWithMultipleConversions->getMedia()->pluck('id');

$ids->map(function ($id) {
expect(File::isDirectory($this->getMediaDirectory($id)))->toBeTrue();
});

$this->testModelWithMultipleConversions->delete();

$ids->map(function ($id) {
expect(File::isDirectory($this->getMediaDirectory($id)))->toBeFalse();
});
});

it('will remove the media folder when deleting a media model with conversions and responsive images', function () {
$ids = $this->testModelWithResponsiveImages->getMedia()->pluck('id');

$ids->map(function ($id) {
expect(File::isDirectory($this->getMediaDirectory($id)))->toBeTrue();
});

$this->testModelWithResponsiveImages->clearMediaCollection();

$ids->map(function ($id) {
expect(File::isDirectory($this->getMediaDirectory($id)))->toBeFalse();
});
});

it('will remove the media folder when deleting a subject with media conversions and responsive images', function () {
$ids = $this->testModelWithResponsiveImages->getMedia()->pluck('id');

$ids->map(function ($id) {
expect(File::isDirectory($this->getMediaDirectory($id)))->toBeTrue();
});

$this->testModelWithResponsiveImages->delete();

$ids->map(function ($id) {
expect(File::isDirectory($this->getMediaDirectory($id)))->toBeFalse();
});
});

2 changes: 1 addition & 1 deletion tests/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ abstract class TestCase extends Orchestra

protected TestModelWithConversion $testModelWithConversion;

protected TestModelWithMultipleConversion $testModelWithMultipleConversion;
protected TestModelWithMultipleConversions $testModelWithMultipleConversions;

protected TestModelWithPreviewConversion $testModelWithPreviewConversion;

Expand Down

0 comments on commit 5ccca6d

Please sign in to comment.