Refactor attachment cropping functionality to support multiple variants
- Introduced AttachmentCrop model to manage crop variants for attachments. - Updated Attachment model to remove direct crop fields and establish relationships with AttachmentCrop. - Modified AttachmentService to handle storing and updating multiple crop variants (desktop and mobile). - Adjusted validation rules to accommodate new crop structure. - Updated database migration to create attachment_crops table and migrate existing crop data. - Refactored tests to ensure compatibility with the new cropping structure and validate multiple crop variants. - Enhanced documentation to reflect changes in attachment handling and cropping capabilities.
This commit is contained in:
@@ -3,6 +3,7 @@
|
||||
namespace Tests\Feature\Tenant;
|
||||
|
||||
use App\Domains\Attachable\Models\Attachment;
|
||||
use App\Domains\Attachable\Models\AttachmentCrop;
|
||||
use App\Domains\Auth\Models\User;
|
||||
use App\Domains\Authorization\Enums\RoleCode;
|
||||
use App\Domains\Tenant\Models\Tenant;
|
||||
@@ -251,62 +252,98 @@ class AdminAppWebsiteExtraControllerTest extends TestCase
|
||||
'config' => [
|
||||
'background_image_id' => [
|
||||
'image' => $image,
|
||||
'crop_horizontal' => [
|
||||
'start_percentage' => 25,
|
||||
'end_percentage' => 75,
|
||||
],
|
||||
'crop_vertical' => [
|
||||
'start_percentage' => 10,
|
||||
'end_percentage' => 85,
|
||||
'crops' => [
|
||||
'desktop' => [
|
||||
'crop_horizontal' => [
|
||||
'start_percentage' => 25,
|
||||
'end_percentage' => 75,
|
||||
],
|
||||
'crop_vertical' => [
|
||||
'start_percentage' => 10,
|
||||
'end_percentage' => 85,
|
||||
],
|
||||
],
|
||||
'mobile' => [
|
||||
'crop_horizontal' => [
|
||||
'start_percentage' => 30,
|
||||
'end_percentage' => 70,
|
||||
],
|
||||
'crop_vertical' => [
|
||||
'start_percentage' => 0,
|
||||
'end_percentage' => 100,
|
||||
],
|
||||
],
|
||||
],
|
||||
],
|
||||
],
|
||||
])->assertOk();
|
||||
|
||||
$original = Attachment::query()->whereNotNull('cropped_attachment_id')->sole();
|
||||
$original = Attachment::query()->whereHas('cropVariants')->sole();
|
||||
|
||||
$response
|
||||
->assertJsonPath('data.extras.heroConfig.background_image_id', $original->key)
|
||||
->assertJsonPath(
|
||||
'data.resolved_extras.heroConfig.background_image_id.crop_horizontal.start_percentage',
|
||||
'data.resolved_extras.heroConfig.background_image_id.crops.desktop.crop_horizontal.start_percentage',
|
||||
25
|
||||
)
|
||||
->assertJsonPath(
|
||||
'data.resolved_extras.heroConfig.background_image_id.crop_vertical.end_percentage',
|
||||
85
|
||||
'data.resolved_extras.heroConfig.background_image_id.crops.mobile.crop_vertical.end_percentage',
|
||||
100
|
||||
);
|
||||
|
||||
$this->assertStringContainsString(
|
||||
$original->key,
|
||||
$response->json('data.resolved_extras.heroConfig.background_image_id.url')
|
||||
);
|
||||
$this->assertDatabaseCount('attachments', 2);
|
||||
$this->assertDatabaseCount('attachments', 3);
|
||||
$this->assertDatabaseCount('attachment_crops', 2);
|
||||
|
||||
$previousCropId = $original->cropped_attachment_id;
|
||||
$previousCropIds = $original->cropVariants()->pluck('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,
|
||||
'crops' => [
|
||||
'desktop' => [
|
||||
'crop_horizontal' => [
|
||||
'start_percentage' => 10,
|
||||
'end_percentage' => 90,
|
||||
],
|
||||
'crop_vertical' => [
|
||||
'start_percentage' => 20,
|
||||
'end_percentage' => 80,
|
||||
],
|
||||
],
|
||||
'mobile' => [
|
||||
'crop_horizontal' => [
|
||||
'start_percentage' => 35,
|
||||
'end_percentage' => 65,
|
||||
],
|
||||
'crop_vertical' => [
|
||||
'start_percentage' => 0,
|
||||
'end_percentage' => 100,
|
||||
],
|
||||
],
|
||||
],
|
||||
],
|
||||
],
|
||||
])
|
||||
->assertOk()
|
||||
->assertJsonPath(
|
||||
'data.resolved_extras.heroConfig.background_image_id.crop_horizontal.start_percentage',
|
||||
'data.resolved_extras.heroConfig.background_image_id.crops.desktop.crop_horizontal.start_percentage',
|
||||
10
|
||||
);
|
||||
|
||||
$this->assertNotSame($previousCropId, $original->refresh()->cropped_attachment_id);
|
||||
$this->assertDatabaseMissing('attachments', ['id' => $previousCropId]);
|
||||
$this->assertDatabaseCount('attachments', 2);
|
||||
$newCropIds = $original->cropVariants()->pluck('cropped_attachment_id');
|
||||
$this->assertEmpty($previousCropIds->intersect($newCropIds));
|
||||
foreach ($previousCropIds as $previousCropId) {
|
||||
$this->assertDatabaseMissing('attachments', ['id' => $previousCropId]);
|
||||
}
|
||||
$this->assertDatabaseCount('attachments', 3);
|
||||
$this->assertDatabaseHas('attachment_crops', [
|
||||
'attachment_id' => $original->id,
|
||||
'variant' => AttachmentCrop::MOBILE,
|
||||
]);
|
||||
}
|
||||
|
||||
public function test_update_returns_not_found_for_an_unsupported_extra_code(): void
|
||||
|
||||
@@ -4,6 +4,7 @@ namespace Tests\Feature\Tenant;
|
||||
|
||||
use App\Domains\Attachable\Enums\AttachmentType;
|
||||
use App\Domains\Attachable\Models\Attachment;
|
||||
use App\Domains\Attachable\Models\AttachmentCrop;
|
||||
use App\Domains\Authorization\Enums\RoleCode;
|
||||
use App\Domains\Authorization\Models\Role;
|
||||
use App\Domains\Catalog\Models\Category;
|
||||
@@ -338,10 +339,18 @@ class BootstrapTenantControllerTest extends TestCase
|
||||
],
|
||||
],
|
||||
]);
|
||||
$cropped = Attachment::query()->create([
|
||||
$desktop = Attachment::query()->create([
|
||||
'key' => (string) Str::uuid(),
|
||||
'path' => 'tenants/acme/cropped.jpg',
|
||||
'filename' => 'cropped.jpg',
|
||||
'path' => 'tenants/acme/desktop.jpg',
|
||||
'filename' => 'desktop.jpg',
|
||||
'type' => AttachmentType::Image,
|
||||
'mime_type' => 'image/jpeg',
|
||||
'extension' => 'jpg',
|
||||
]);
|
||||
$mobile = Attachment::query()->create([
|
||||
'key' => (string) Str::uuid(),
|
||||
'path' => 'tenants/acme/mobile.jpg',
|
||||
'filename' => 'mobile.jpg',
|
||||
'type' => AttachmentType::Image,
|
||||
'mime_type' => 'image/jpeg',
|
||||
'extension' => 'jpg',
|
||||
@@ -353,10 +362,15 @@ class BootstrapTenantControllerTest extends TestCase
|
||||
'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,
|
||||
]);
|
||||
foreach ([AttachmentCrop::DESKTOP => $desktop, AttachmentCrop::MOBILE => $mobile] as $variant => $crop) {
|
||||
$original->cropVariants()->create([
|
||||
'variant' => $variant,
|
||||
'crop_horizontal' => ['start_percentage' => 10, 'end_percentage' => 90],
|
||||
'crop_vertical' => ['start_percentage' => 20, 'end_percentage' => 80],
|
||||
'cropped_attachment_id' => $crop->id,
|
||||
]);
|
||||
}
|
||||
$tenant->websiteExtras()->create([
|
||||
'website_type_extra_id' => $heroDefinition->id,
|
||||
'config' => ['background_image_id' => $original->id],
|
||||
@@ -368,11 +382,9 @@ class BootstrapTenantControllerTest extends TestCase
|
||||
->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);
|
||||
$response
|
||||
->assertJsonPath('data.extras.heroConfig.background_image_id.desktop', fn (string $url): bool => str_contains($url, 'desktop.jpg'))
|
||||
->assertJsonPath('data.extras.heroConfig.background_image_id.mobile', fn (string $url): bool => str_contains($url, 'mobile.jpg'));
|
||||
}
|
||||
|
||||
public function test_it_returns_not_found_when_the_domain_does_not_exist(): void
|
||||
|
||||
Reference in New Issue
Block a user