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

@@ -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');