Skip to content

Commit

Permalink
don’t delete if same path
Browse files Browse the repository at this point in the history
  • Loading branch information
edalzell committed Dec 7, 2024
1 parent ed9161d commit fd38bc8
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/Assets/Asset.php
Original file line number Diff line number Diff line change
Expand Up @@ -777,6 +777,10 @@ public function move($folder, $filename = null)
*/
public function replace(Asset $originalAsset, $deleteOriginal = false)
{
if ($this->path() == $originalAsset->path()) {
return $this;
}

// Temporarily disable the reference updater to avoid triggering reference updates
// until after the `AssetReplaced` event is fired. We still want to fire events
// like `AssetDeleted` and `AssetSaved` though, so that other listeners will
Expand Down
24 changes: 24 additions & 0 deletions tests/Assets/AssetTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1487,6 +1487,30 @@ public function it_can_delete_original_asset_when_replacing()
})->all());
}

#[Test]
public function it_doesnt_delete_original_asset_when_replacing_with_same_asset()
{
$this->fakeEventWithMacros();
$disk = Storage::fake('local');
$disk->put('some/asset.txt', 'asset contents');
$container = Facades\AssetContainer::make('test')->disk('local');
Facades\AssetContainer::shouldReceive('save')->with($container);
Facades\AssetContainer::shouldReceive('findByHandle')->with('test')->andReturn($container);
$oldAsset = tap($container->makeAsset('some/asset.txt')->data(['foo' => 'bar']))->saveQuietly();
$sameAsset = $container->makeAsset('some/asset.txt')->data(['foo' => 'bar']);

// Replace with `$deleteOriginal = true`
$return = $sameAsset->replace($oldAsset, true);

Event::assertDispatched(AssetDeleted::class, 0); // because we passed the flag, the original asset should be deleted
Event::assertDispatched(AssetSaved::class, 0); // by default, the new asset is not renamed
Event::assertDispatched(AssetReplaced::class, 0); // our `UpdateAssetReferencesTest` covers what happens _after_ an asset is replaced

$this->assertEquals($sameAsset, $return);
$disk->assertExists('some/asset.txt');
$disk->assertExists('some/.meta/asset.txt.yaml');
}

#[Test]
public function it_gets_dimensions()
{
Expand Down

0 comments on commit fd38bc8

Please sign in to comment.