Skip to content

Commit

Permalink
Add a test
Browse files Browse the repository at this point in the history
  • Loading branch information
duncanmcclean committed Dec 18, 2024
1 parent 389d569 commit 6f35197
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 2 deletions.
2 changes: 1 addition & 1 deletion src/Transformers/AssetsTransformer.php
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ private function assetPath(AssetContainerContract $assetContainer, string $path)

private function processAssetUsingSourcePreset($asset, string $path, string $contents): void
{
Storage::disk('local')->put($tempPath = 'statamic/temp-assets/'.uniqid().'.'.Str::afterLast($path, '.'), $contents);
Storage::disk('local')->put($tempPath = 'statamic/temp-assets/'.Str::random().'.'.Str::afterLast($path, '.'), $contents);

$uploadedFile = new UploadedFile(
path: Storage::disk('local')->path($tempPath),
Expand Down
44 changes: 43 additions & 1 deletion tests/Transformers/AssetsTransformerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,18 @@
namespace Statamic\Importer\Tests\Transformers;

use Illuminate\Http\UploadedFile;
use Illuminate\Support\Facades\File;
use Illuminate\Support\Facades\Http;
use Illuminate\Support\Facades\Storage;
use Mockery;
use PHPUnit\Framework\Attributes\Test;
use Statamic\Facades\AssetContainer;
use Statamic\Facades\Collection;
use Statamic\Facades\Glide;
use Statamic\Importer\Facades\Import;
use Statamic\Importer\Tests\TestCase;
use Statamic\Importer\Transformers\AssetsTransformer;
use Statamic\Support\Str;
use Statamic\Testing\Concerns\PreventsSavingStacheItemsToDisk;

class AssetsTransformerTest extends TestCase
Expand All @@ -32,7 +36,7 @@ protected function setUp(): void
$this->collection = tap(Collection::make('pages'))->save();

$this->blueprint = $this->collection->entryBlueprint();
$this->blueprint->ensureField('featured_image', ['type' => 'assets', 'max_files' => 1])->save();
$this->blueprint->ensureField('featured_image', ['type' => 'assets', 'container' => 'assets', 'max_files' => 1])->save();

$this->field = $this->blueprint->field('featured_image');

Expand Down Expand Up @@ -228,4 +232,42 @@ public function it_sets_alt_text_on_downloaded_asset()
$asset = $this->assetContainer->asset('2024/10/image.png');
$this->assertEquals('A photo taken by someone.', $asset->get('alt'));
}

#[Test]
public function it_processes_asset_using_source_preset()
{
Http::fake([
'https://example.com/wp-content/uploads/2024/10/image.png' => Http::response(UploadedFile::fake()->image('image.png')->size(100)->get()),
]);

Str::createRandomStringsUsing(fn () => 'temp');

File::ensureDirectoryExists(storage_path('statamic/glide/tmp'));
File::put(storage_path('statamic/glide/tmp/temp.png/random/temp.png'), 'Transformed Image');

Glide::shouldReceive('server')->once()->andReturnSelf();
Glide::shouldReceive('makeImage')->once()->with('temp.png', ['p' => 'thumbnail'])->andReturn('temp.png/random/temp.png');

$this->assetContainer->disk('public')->sourcePreset('thumbnail')->save();

$transformer = new AssetsTransformer(
import: $this->import,
blueprint: $this->blueprint,
field: $this->field,
config: [
'related_field' => 'url',
'base_url' => 'https://example.com/wp-content/uploads',
'download_when_missing' => true,
'process_downloaded_images' => true,
],
);

$output = $transformer->transform('https://example.com/wp-content/uploads/2024/10/image.png');

$this->assertEquals('2024/10/image.png', $output);

Storage::disk('public')->assertExists('2024/10/image.png');

$this->assertFileDoesNotExist(storage_path('statamic/glide/tmp/temp.png/random/temp.png'));
}
}

0 comments on commit 6f35197

Please sign in to comment.