feat(attachment): refactor image cropping functionality to use range objects and update related tests

This commit is contained in:
2026-08-18 12:54:47 -03:00
parent 58cc35fd61
commit 9cfd49f233
16 changed files with 496 additions and 54 deletions

View File

@@ -77,15 +77,21 @@ class AttachmentTest extends TestCase
$original = app(AttachmentService::class)->storeCroppedImage(
UploadedFile::fake()->image('product.jpg', 200, 100),
'attachments/acme',
50,
25,
['start_percentage' => 25, 'end_percentage' => 75],
['start_percentage' => 10, 'end_percentage' => 85],
);
$cropped = $original->croppedAttachment;
$this->assertNotNull($cropped);
$this->assertSame(50.0, $original->crop_horizontal_start_percent);
$this->assertSame(25.0, $original->crop_vertical_start_percent);
$this->assertEquals(
['start_percentage' => 25.0, 'end_percentage' => 75.0],
$original->crop_horizontal,
);
$this->assertEquals(
['start_percentage' => 10.0, 'end_percentage' => 85.0],
$original->crop_vertical,
);
$this->assertTrue($cropped->originalAttachment->is($original));
$this->assertDatabaseCount('attachments', 2);
$this->assertDatabaseHas('attachments', [
@@ -110,8 +116,8 @@ class AttachmentTest extends TestCase
app(AttachmentService::class)->storeCroppedImage(
UploadedFile::fake()->image('product.png'),
'attachments/acme',
100,
0,
['start_percentage' => 75, 'end_percentage' => 25],
['start_percentage' => 0, 'end_percentage' => 100],
);
$this->fail('Expected an AttachmentStorageException to be thrown.');
@@ -129,8 +135,8 @@ class AttachmentTest extends TestCase
app(AttachmentService::class)->storeCroppedImage(
UploadedFile::fake()->createWithContent('invalid.png', 'not-an-image'),
'attachments/acme',
10,
10,
['start_percentage' => 10, 'end_percentage' => 90],
['start_percentage' => 10, 'end_percentage' => 90],
);
$this->fail('Expected an AttachmentStorageException to be thrown.');
@@ -147,8 +153,8 @@ class AttachmentTest extends TestCase
$original = app(AttachmentService::class)->storeCroppedImage(
UploadedFile::fake()->image('product.png'),
'attachments/acme',
10,
10,
['start_percentage' => 10, 'end_percentage' => 90],
['start_percentage' => 10, 'end_percentage' => 90],
);
$cropped = $original->croppedAttachment;
@@ -159,6 +165,37 @@ class AttachmentTest extends TestCase
Storage::disk('s3')->assertMissing($cropped->path);
}
public function test_it_replaces_the_crop_of_an_existing_image(): void
{
Storage::fake('s3');
$original = app(AttachmentService::class)->storeCroppedImage(
UploadedFile::fake()->image('product.jpg', 200, 100),
'attachments/acme',
['start_percentage' => 0, 'end_percentage' => 100],
['start_percentage' => 0, 'end_percentage' => 100],
);
$previousCrop = $original->croppedAttachment;
$updated = app(AttachmentService::class)->updateImageCrop(
$original,
['start_percentage' => 25, 'end_percentage' => 75],
['start_percentage' => 10, 'end_percentage' => 85],
);
$this->assertFalse($updated->croppedAttachment->is($previousCrop));
$this->assertDatabaseMissing('attachments', ['id' => $previousCrop->id]);
Storage::disk('s3')->assertMissing($previousCrop->path);
$croppedSize = getimagesizefromstring(
Storage::disk('s3')->get($updated->croppedAttachment->path)
);
$this->assertIsArray($croppedSize);
$this->assertSame(100, $croppedSize[0]);
$this->assertSame(75, $croppedSize[1]);
}
public function test_it_deletes_from_s3_before_removing_the_database_record(): void
{
Storage::fake('s3');

View File

@@ -92,7 +92,7 @@ class WebsiteTypeSeederTest extends TestCase
'description_html' => 'nullable|string',
'button_text' => 'nullable|string',
'button_href' => 'nullable|string',
'background_image_id' => 'nullable|image_or_base64',
'background_image_id' => 'nullable|cropped_image_or_base64',
],
'transforms' => [
'background_image_id' => [

View File

@@ -2,12 +2,15 @@
namespace Tests\Feature\Tenant;
use App\Domains\Attachable\Models\Attachment;
use App\Domains\Auth\Models\User;
use App\Domains\Authorization\Enums\RoleCode;
use App\Domains\Tenant\Models\Tenant;
use App\Domains\Tenant\Models\WebsiteType;
use Database\Seeders\AuthorizationSeeder;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Http\UploadedFile;
use Illuminate\Support\Facades\Storage;
use Laravel\Sanctum\Sanctum;
use Tests\TestCase;
@@ -217,6 +220,95 @@ class AdminAppWebsiteExtraControllerTest extends TestCase
);
}
public function test_adminapp_user_stores_a_cropped_banner_and_receives_original_edit_data(): void
{
Storage::fake('s3');
$this->websiteType->extras()->create([
'codigo' => 'heroConfig',
'nombre' => 'Banner principal',
'descripcion' => 'Configuración del banner principal.',
'is_required' => false,
'config_schema' => [
'request_rules' => [
'$' => 'required|array',
'background_image_id' => 'nullable|cropped_image_or_base64',
],
'transforms' => [
'background_image_id' => [
'handler' => 'attachment',
'attachment_type' => 'image',
],
],
],
]);
$tenant = $this->createTenant('acme');
$file = UploadedFile::fake()->image('banner.jpg', 200, 100);
$image = 'data:image/jpeg;base64,'.base64_encode((string) file_get_contents($file->getRealPath()));
Sanctum::actingAs($this->createAdminAppUser($tenant));
$response = $this->putJson('/api/v1/adminapp/tenant/website-extras/heroConfig', [
'config' => [
'background_image_id' => [
'image' => $image,
'crop_horizontal' => [
'start_percentage' => 25,
'end_percentage' => 75,
],
'crop_vertical' => [
'start_percentage' => 10,
'end_percentage' => 85,
],
],
],
])->assertOk();
$original = Attachment::query()->whereNotNull('cropped_attachment_id')->sole();
$response
->assertJsonPath('data.extras.heroConfig.background_image_id', $original->key)
->assertJsonPath(
'data.resolved_extras.heroConfig.background_image_id.crop_horizontal.start_percentage',
25
)
->assertJsonPath(
'data.resolved_extras.heroConfig.background_image_id.crop_vertical.end_percentage',
85
);
$this->assertStringContainsString(
$original->key,
$response->json('data.resolved_extras.heroConfig.background_image_id.url')
);
$this->assertDatabaseCount('attachments', 2);
$previousCropId = $original->cropped_attachment_id;
$this->putJson('/api/v1/adminapp/tenant/website-extras/heroConfig', [
'config' => [
'background_image_id' => [
'image' => $original->key,
'crop_horizontal' => [
'start_percentage' => 10,
'end_percentage' => 90,
],
'crop_vertical' => [
'start_percentage' => 20,
'end_percentage' => 80,
],
],
],
])
->assertOk()
->assertJsonPath(
'data.resolved_extras.heroConfig.background_image_id.crop_horizontal.start_percentage',
10
);
$this->assertNotSame($previousCropId, $original->refresh()->cropped_attachment_id);
$this->assertDatabaseMissing('attachments', ['id' => $previousCropId]);
$this->assertDatabaseCount('attachments', 2);
}
public function test_update_returns_not_found_for_an_unsupported_extra_code(): void
{
$tenant = $this->createTenant('acme');

View File

@@ -317,6 +317,64 @@ class BootstrapTenantControllerTest extends TestCase
->assertJsonMissingPath('data.extras.banner');
}
public function test_the_bootstrap_returns_only_the_cropped_banner_url(): void
{
Storage::fake('s3');
$tenant = $this->createTenant();
$websiteType = WebsiteType::query()->create([
'codigo' => 'event-store',
'nombre' => 'Eventos',
]);
$tenant->update(['website_type_code' => $websiteType->codigo]);
$heroDefinition = $websiteType->extras()->create([
'codigo' => 'heroConfig',
'nombre' => 'Banner',
'config_schema' => [
'transforms' => [
'background_image_id' => [
'handler' => 'attachment',
'attachment_type' => 'image',
],
],
],
]);
$cropped = Attachment::query()->create([
'key' => (string) Str::uuid(),
'path' => 'tenants/acme/cropped.jpg',
'filename' => 'cropped.jpg',
'type' => AttachmentType::Image,
'mime_type' => 'image/jpeg',
'extension' => 'jpg',
]);
$original = Attachment::query()->create([
'key' => (string) Str::uuid(),
'path' => 'tenants/acme/original.jpg',
'filename' => 'original.jpg',
'type' => AttachmentType::Image,
'mime_type' => 'image/jpeg',
'extension' => 'jpg',
'crop_horizontal' => ['start_percentage' => 10, 'end_percentage' => 90],
'crop_vertical' => ['start_percentage' => 20, 'end_percentage' => 80],
'cropped_attachment_id' => $cropped->id,
]);
$tenant->websiteExtras()->create([
'website_type_extra_id' => $heroDefinition->id,
'config' => ['background_image_id' => $original->id],
'is_enabled' => true,
]);
$response = $this->getJson('/api/tenants/bootstrap?dominio=acme.com&path=%2F')
->assertOk()
->assertJsonMissingPath('data.extras.heroConfig.crop_horizontal')
->assertJsonMissingPath('data.extras.heroConfig.crop_vertical');
$backgroundImage = $response->json('data.extras.heroConfig.background_image_id');
$this->assertIsString($backgroundImage);
$this->assertStringContainsString('cropped.jpg', $backgroundImage);
$this->assertStringNotContainsString('original.jpg', $backgroundImage);
}
public function test_it_returns_not_found_when_the_domain_does_not_exist(): void
{
$response = $this->getJson('/api/tenants/bootstrap?dominio=missing.example&path=%2F');