|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Tests\Actions; |
| 4 | + |
| 5 | +use Illuminate\Http\UploadedFile; |
| 6 | +use Illuminate\Support\Facades\Storage; |
| 7 | +use PHPUnit\Framework\Attributes\Test; |
| 8 | +use Statamic\Actions\ReplaceAsset; |
| 9 | +use Statamic\Assets\AssetContainer; |
| 10 | +use Statamic\Facades\Asset; |
| 11 | +use Tests\PreventSavingStacheItemsToDisk; |
| 12 | +use Tests\TestCase; |
| 13 | + |
| 14 | +class ReplaceAssetTest extends TestCase |
| 15 | +{ |
| 16 | + use PreventSavingStacheItemsToDisk; |
| 17 | + |
| 18 | + private $container; |
| 19 | + |
| 20 | + public function setUp(): void |
| 21 | + { |
| 22 | + parent::setUp(); |
| 23 | + |
| 24 | + Storage::fake('test'); |
| 25 | + |
| 26 | + $this->container = tap( |
| 27 | + (new AssetContainer)->handle('test_container')->disk('test') |
| 28 | + )->save(); |
| 29 | + } |
| 30 | + |
| 31 | + private function createAsset($filename, $yaml = null) |
| 32 | + { |
| 33 | + $file = UploadedFile::fake()->image($filename, 30, 60); |
| 34 | + Storage::disk('test')->putFileAs('path/to', $file, $filename); |
| 35 | + |
| 36 | + if ($yaml) { |
| 37 | + Storage::disk('test')->put('path/to/.meta/'.$filename.'.yaml', $yaml); |
| 38 | + } |
| 39 | + } |
| 40 | + |
| 41 | + private function runAction(array $values) |
| 42 | + { |
| 43 | + return (new ReplaceAsset) |
| 44 | + ->context(['container' => 'test_container']) |
| 45 | + ->run(collect([Asset::find('test_container::path/to/original.jpg')]), $values); |
| 46 | + } |
| 47 | + |
| 48 | + #[Test] |
| 49 | + public function it_copies_data_from_the_original_asset() |
| 50 | + { |
| 51 | + $this->createAsset('original.jpg', "data:\n alt: 'Original alt'\n copyright: 'Someone'\n focus: 25-75-1"); |
| 52 | + $this->createAsset('new.jpg', "data:\n alt: 'New alt'\n caption: 'New caption'"); |
| 53 | + |
| 54 | + $this->runAction([ |
| 55 | + 'new_asset' => 'path/to/new.jpg', |
| 56 | + 'copy_data' => true, |
| 57 | + 'delete_original' => true, |
| 58 | + ]); |
| 59 | + |
| 60 | + $this->assertNull($this->container->asset('path/to/original.jpg')); |
| 61 | + $this->assertEquals([ |
| 62 | + 'alt' => 'Original alt', |
| 63 | + 'caption' => 'New caption', |
| 64 | + 'copyright' => 'Someone', |
| 65 | + 'focus' => '25-75-1', |
| 66 | + ], $this->container->asset('path/to/new.jpg')->data()->sortKeys()->all()); |
| 67 | + } |
| 68 | + |
| 69 | + #[Test] |
| 70 | + public function it_does_not_copy_data_when_disabled() |
| 71 | + { |
| 72 | + $this->createAsset('original.jpg', "data:\n alt: 'Original alt'"); |
| 73 | + $this->createAsset('new.jpg'); |
| 74 | + |
| 75 | + $this->runAction([ |
| 76 | + 'new_asset' => 'path/to/new.jpg', |
| 77 | + 'copy_data' => false, |
| 78 | + 'delete_original' => false, |
| 79 | + ]); |
| 80 | + |
| 81 | + $this->assertEquals(['alt' => 'Original alt'], $this->container->asset('path/to/original.jpg')->data()->all()); |
| 82 | + $this->assertEquals([], $this->container->asset('path/to/new.jpg')->data()->all()); |
| 83 | + } |
| 84 | +} |
0 commit comments