Skip to content

Commit 8739cd6

Browse files
[5.x] Add moveQuietly method to Asset class (#11804)
1 parent a3e543a commit 8739cd6

2 files changed

Lines changed: 55 additions & 0 deletions

File tree

src/Assets/Asset.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -775,6 +775,13 @@ public function move($folder, $filename = null)
775775
return $this;
776776
}
777777

778+
public function moveQuietly($folder, $filename = null)
779+
{
780+
$this->withEvents = false;
781+
782+
return $this->move(...func_get_args());
783+
}
784+
778785
/**
779786
* Replace an asset and/or its references where necessary.
780787
*

tests/Assets/AssetTest.php

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1105,6 +1105,54 @@ public function it_can_be_moved_to_another_folder()
11051105
Event::assertDispatched(AssetSaved::class);
11061106
}
11071107

1108+
#[Test]
1109+
public function it_can_be_moved_to_another_folder_quietly()
1110+
{
1111+
Storage::fake('local');
1112+
$disk = Storage::disk('local');
1113+
$disk->put('old/asset.txt', 'The asset contents');
1114+
$container = Facades\AssetContainer::make('test')->disk('local');
1115+
Facades\AssetContainer::shouldReceive('save')->with($container);
1116+
Facades\AssetContainer::shouldReceive('findByHandle')->with('test')->andReturn($container);
1117+
$asset = $container->makeAsset('old/asset.txt')->data(['foo' => 'bar']);
1118+
$asset->save();
1119+
$oldMeta = $disk->get('old/.meta/asset.txt.yaml');
1120+
$disk->assertExists('old/asset.txt');
1121+
$disk->assertExists('old/.meta/asset.txt.yaml');
1122+
$this->assertEquals([
1123+
'old/asset.txt',
1124+
], $container->files()->all());
1125+
$this->assertEquals([
1126+
'old/asset.txt' => ['foo' => 'bar'],
1127+
], $container->assets('/', true)->keyBy->path()->map(function ($item) {
1128+
return $item->data()->all();
1129+
})->all());
1130+
1131+
Event::fake();
1132+
$return = $asset->moveQuietly('new');
1133+
1134+
$this->assertEquals($asset, $return);
1135+
$disk->assertMissing('old/asset.txt');
1136+
$disk->assertMissing('old/.meta/asset.txt.yaml');
1137+
$disk->assertExists('new/asset.txt');
1138+
$disk->assertExists('new/.meta/asset.txt.yaml');
1139+
$this->assertEquals($oldMeta, $disk->get('new/.meta/asset.txt.yaml'));
1140+
$this->assertEquals([
1141+
'new/asset.txt',
1142+
], $container->files()->all());
1143+
$this->assertEquals([
1144+
'new/asset.txt' => ['foo' => 'bar'],
1145+
], $container->assets('/', true)->keyBy->path()->map(function ($item) {
1146+
return $item->data()->all();
1147+
})->all());
1148+
$this->assertEquals([
1149+
'old', // the empty directory doesnt actually get deleted
1150+
'new',
1151+
'new/asset.txt',
1152+
], $container->contents()->cached()->keys()->all());
1153+
Event::assertNotDispatched(AssetSaved::class);
1154+
}
1155+
11081156
#[Test]
11091157
public function it_can_be_moved_to_another_folder_with_a_new_filename()
11101158
{

0 commit comments

Comments
 (0)