Skip to content

Commit

Permalink
Make delete orphaned opt-in, add test
Browse files Browse the repository at this point in the history
  • Loading branch information
mbardelmeijer committed Oct 24, 2023
1 parent d550eb6 commit f6a74f2
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 3 deletions.
6 changes: 3 additions & 3 deletions src/MediaCollections/Commands/CleanCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ class CleanCommand extends Command
protected $signature = 'media-library:clean {modelType?} {collectionName?} {disk?}
{--dry-run : List files that will be removed without removing them},
{--force : Force the operation to run when in production},
{--rate-limit= : Limit the number of requests per second },
{--skip-orphaned : Do not remove orphaned media items},
{--rate-limit= : Limit the number of requests per second},
{--delete-orphaned : Delete orphaned media items},
{--skip-conversions : Do not remove deprecated conversions}';

protected $description = 'Clean deprecated conversions and files without related model.';
Expand Down Expand Up @@ -55,7 +55,7 @@ public function handle(
$this->isDryRun = $this->option('dry-run');
$this->rateLimit = (int) $this->option('rate-limit');

if (! $this->option('skip-orphaned')) {
if ($this->option('delete-orphaned')) {
$this->deleteOrphanedMediaItems();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -274,3 +274,50 @@
expect($this->getMediaDirectory("{$media->id}/test-thumb.jpg"))->toBeFile();
expect($this->getMediaDirectory("{$media->id}/test.jpg"))->toBeFile();
});

it('can clean orphaned media items when enabled', function () {
$mediaToDelete = TestModel::create(['name' => 'test.jpg'])
->addMedia($this->getTestJpg())
->preservingOriginal()
->toMediaCollection('collection1');

$mediaToKeep = TestModel::create(['name' => 'test.jpg'])
->addMedia($this->getTestJpg())
->preservingOriginal()
->toMediaCollection('collection1');

// Delete quietly to avoid deleting the related media file.
$mediaToDelete->model->deleteQuietly();

$this->artisan('media-library:clean', [
'--delete-orphaned' => 'true',
]);

// Media should be deleted from the database.
$this->assertDatabaseMissing('media', [
'id' => $mediaToDelete->id,
]);

// This media should still exist.
$this->assertDatabaseHas('media', [
'id' => $mediaToKeep->id,
]);
});

it('can won\'t clean orphaned media items when disabled', function () {
$media = TestModel::create(['name' => 'test.jpg'])
->addMedia($this->getTestJpg())
->preservingOriginal()
->toMediaCollection('collection1');

// Delete quietly to avoid deleting the related media file.
$media->model->deleteQuietly();

// Without the `--delete-orphaned` flag, the orphaned media should remain.
$this->artisan('media-library:clean');

// This media should still exist.
$this->assertDatabaseHas('media', [
'id' => $media->id,
]);
});

0 comments on commit f6a74f2

Please sign in to comment.