diff --git a/Tenant_CRUD_Postman_Collection.json b/Tenant_CRUD_Postman_Collection.json new file mode 100644 index 0000000..41abfd5 --- /dev/null +++ b/Tenant_CRUD_Postman_Collection.json @@ -0,0 +1,357 @@ +{ + "info": { + "_postman_id": "76df1733-a3d8-4f2b-9ffb-7c70e3032549", + "name": "ShopIt - Tenant CRUD", + "schema": "https://schema.getpostman.com/json/collection/v2.1.0/collection.json" + }, + "item": [ + { + "name": "List Tenants", + "request": { + "method": "GET", + "header": [ + { + "key": "Accept", + "value": "application/json", + "type": "text" + } + ], + "url": { + "raw": "{{base_url}}/api/tenants", + "host": [ + "{{base_url}}" + ], + "path": [ + "api", + "tenants" + ] + } + }, + "response": [] + }, + { + "name": "Bootstrap Tenant", + "request": { + "method": "GET", + "header": [ + { + "key": "Accept", + "value": "application/json", + "type": "text" + } + ], + "url": { + "raw": "{{base_url}}/api/tenants/bootstrap/acme.com", + "host": [ + "{{base_url}}" + ], + "path": [ + "api", + "tenants", + "bootstrap", + "acme.com" + ] + } + }, + "response": [] + }, + { + "name": "Create Tenant (JSON)", + "request": { + "method": "POST", + "header": [ + { + "key": "Accept", + "value": "application/json", + "type": "text" + }, + { + "key": "Content-Type", + "value": "application/json", + "type": "text" + } + ], + "body": { + "mode": "raw", + "raw": "{\n \"codigo\": \"acme\",\n \"nombre\": \"Acme Corporation\",\n \"dominio\": \"acme.com\",\n \"primary_color\": \"#111111\",\n \"secondary_color\": \"#222222\",\n \"danger_color\": \"#333333\",\n \"header_footer_bg_color\": \"#444444\",\n \"header_logo\": \"data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mNk+M9QDwADhgGAWjR9awAAAABJRU5ErkJggg==\",\n \"footer_logo\": \"data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mNk+M9QDwADhgGAWjR9awAAAABJRU5ErkJggg==\"\n}" + }, + "url": { + "raw": "{{base_url}}/api/tenants", + "host": [ + "{{base_url}}" + ], + "path": [ + "api", + "tenants" + ] + } + }, + "response": [] + }, + { + "name": "Create Tenant (Form Data)", + "request": { + "method": "POST", + "header": [ + { + "key": "Accept", + "value": "application/json", + "type": "text" + } + ], + "body": { + "mode": "formdata", + "formdata": [ + { + "key": "codigo", + "value": "acme", + "type": "text" + }, + { + "key": "nombre", + "value": "Acme Corporation", + "type": "text" + }, + { + "key": "dominio", + "value": "acme.com", + "type": "text" + }, + { + "key": "primary_color", + "value": "#111111", + "type": "text" + }, + { + "key": "secondary_color", + "value": "#222222", + "type": "text" + }, + { + "key": "danger_color", + "value": "#333333", + "type": "text" + }, + { + "key": "header_footer_bg_color", + "value": "#444444", + "type": "text" + }, + { + "key": "header_logo", + "type": "file", + "src": [] + }, + { + "key": "footer_logo", + "type": "file", + "src": [] + } + ] + }, + "url": { + "raw": "{{base_url}}/api/tenants", + "host": [ + "{{base_url}}" + ], + "path": [ + "api", + "tenants" + ] + } + }, + "response": [] + }, + { + "name": "Show Tenant", + "request": { + "method": "GET", + "header": [ + { + "key": "Accept", + "value": "application/json", + "type": "text" + } + ], + "url": { + "raw": "{{base_url}}/api/tenants/acme", + "host": [ + "{{base_url}}" + ], + "path": [ + "api", + "tenants", + "acme" + ] + } + }, + "response": [] + }, + { + "name": "Update Tenant (JSON)", + "request": { + "method": "PUT", + "header": [ + { + "key": "Accept", + "value": "application/json", + "type": "text" + }, + { + "key": "Content-Type", + "value": "application/json", + "type": "text" + } + ], + "body": { + "mode": "raw", + "raw": "{\n \"codigo\": \"acme\",\n \"nombre\": \"Acme Corporation Updated\",\n \"dominio\": \"acme.com\",\n \"primary_color\": \"#555555\",\n \"secondary_color\": \"#666666\",\n \"danger_color\": \"#777777\",\n \"header_footer_bg_color\": \"#888888\"\n}" + }, + "url": { + "raw": "{{base_url}}/api/tenants/acme", + "host": [ + "{{base_url}}" + ], + "path": [ + "api", + "tenants", + "acme" + ] + } + }, + "response": [] + }, + { + "name": "Update Tenant (Form Data)", + "request": { + "method": "POST", + "header": [ + { + "key": "Accept", + "value": "application/json", + "type": "text" + } + ], + "body": { + "mode": "formdata", + "formdata": [ + { + "key": "_method", + "value": "PUT", + "type": "text" + }, + { + "key": "codigo", + "value": "acme", + "type": "text" + }, + { + "key": "nombre", + "value": "Acme Corporation Updated", + "type": "text" + }, + { + "key": "dominio", + "value": "acme.com", + "type": "text" + }, + { + "key": "primary_color", + "value": "#555555", + "type": "text" + }, + { + "key": "secondary_color", + "value": "#666666", + "type": "text" + }, + { + "key": "danger_color", + "value": "#777777", + "type": "text" + }, + { + "key": "header_footer_bg_color", + "value": "#888888", + "type": "text" + }, + { + "key": "header_logo", + "type": "file", + "src": [] + }, + { + "key": "footer_logo", + "type": "file", + "src": [] + } + ] + }, + "url": { + "raw": "{{base_url}}/api/tenants/acme", + "host": [ + "{{base_url}}" + ], + "path": [ + "api", + "tenants", + "acme" + ] + } + }, + "response": [] + }, + { + "name": "Delete Tenant", + "request": { + "method": "DELETE", + "header": [ + { + "key": "Accept", + "value": "application/json", + "type": "text" + } + ], + "url": { + "raw": "{{base_url}}/api/tenants/acme", + "host": [ + "{{base_url}}" + ], + "path": [ + "api", + "tenants", + "acme" + ] + } + }, + "response": [] + } + ], + "event": [ + { + "listen": "prerequest", + "script": { + "type": "text/javascript", + "exec": [ + "" + ] + } + }, + { + "listen": "test", + "script": { + "type": "text/javascript", + "exec": [ + "" + ] + } + } + ], + "variable": [ + { + "key": "base_url", + "value": "http://localhost:8000", + "type": "string" + } + ] +} diff --git a/app/Domains/Tenant/Requests/StoreTenantRequest.php b/app/Domains/Tenant/Requests/StoreTenantRequest.php index 734a822..6ba632a 100644 --- a/app/Domains/Tenant/Requests/StoreTenantRequest.php +++ b/app/Domains/Tenant/Requests/StoreTenantRequest.php @@ -37,7 +37,7 @@ class StoreTenantRequest extends FormRequest public function rules(): array { $logoRule = [ - 'nullable', + 'required', static function (string $attribute, mixed $value, Closure $fail): void { if ($value instanceof UploadedFile) { $mime = $value->getMimeType(); @@ -92,15 +92,15 @@ class StoreTenantRequest extends FormRequest $fail("The {$attribute} field must contain a valid domain or URL."); } }, - 'nullable', + 'required', 'string', 'max:255', Rule::unique('tenants', 'dominio'), ], - 'primary_color' => ['nullable', 'string', 'regex:/^#([a-fA-F0-9]{3,4}|[a-fA-F0-9]{6}|[a-fA-F0-9]{8})$/'], - 'secondary_color' => ['nullable', 'string', 'regex:/^#([a-fA-F0-9]{3,4}|[a-fA-F0-9]{6}|[a-fA-F0-9]{8})$/'], - 'danger_color' => ['nullable', 'string', 'regex:/^#([a-fA-F0-9]{3,4}|[a-fA-F0-9]{6}|[a-fA-F0-9]{8})$/'], - 'header_footer_bg_color' => ['nullable', 'string', 'regex:/^#([a-fA-F0-9]{3,4}|[a-fA-F0-9]{6}|[a-fA-F0-9]{8})$/'], + 'primary_color' => ['required', 'string', 'regex:/^#([a-fA-F0-9]{3,4}|[a-fA-F0-9]{6}|[a-fA-F0-9]{8})$/'], + 'secondary_color' => ['required', 'string', 'regex:/^#([a-fA-F0-9]{3,4}|[a-fA-F0-9]{6}|[a-fA-F0-9]{8})$/'], + 'danger_color' => ['required', 'string', 'regex:/^#([a-fA-F0-9]{3,4}|[a-fA-F0-9]{6}|[a-fA-F0-9]{8})$/'], + 'header_footer_bg_color' => ['required', 'string', 'regex:/^#([a-fA-F0-9]{3,4}|[a-fA-F0-9]{6}|[a-fA-F0-9]{8})$/'], 'header_logo' => $logoRule, 'footer_logo' => $logoRule, ]; diff --git a/app/Domains/Tenant/Requests/UpdateTenantRequest.php b/app/Domains/Tenant/Requests/UpdateTenantRequest.php index b2f024a..7c9e926 100644 --- a/app/Domains/Tenant/Requests/UpdateTenantRequest.php +++ b/app/Domains/Tenant/Requests/UpdateTenantRequest.php @@ -21,15 +21,17 @@ class UpdateTenantRequest extends FormRequest protected function prepareForValidation(): void { - $rawDomain = $this->input('dominio'); - $normalizedDomain = TenantDomainNormalizer::normalize($rawDomain); + if ($this->has('dominio')) { + $rawDomain = $this->input('dominio'); + $normalizedDomain = TenantDomainNormalizer::normalize($rawDomain); - $this->hasInvalidDomain = TenantDomainNormalizer::hasValue($rawDomain) - && $normalizedDomain === null; + $this->hasInvalidDomain = TenantDomainNormalizer::hasValue($rawDomain) + && $normalizedDomain === null; - $this->merge([ - 'dominio' => $normalizedDomain, - ]); + $this->merge([ + 'dominio' => $normalizedDomain, + ]); + } } /** @@ -88,12 +90,12 @@ class UpdateTenantRequest extends FormRequest return [ 'codigo' => [ - 'sometimes', + 'nullable', 'string', 'max:255', Rule::unique('tenants', 'codigo')->ignore($tenant?->id), ], - 'nombre' => ['sometimes', 'string', 'max:255'], + 'nombre' => ['nullable', 'string', 'max:255'], 'dominio' => [ 'bail', function (string $attribute, mixed $value, Closure $fail): void { diff --git a/app/Domains/Tenant/Resources/TenantResource.php b/app/Domains/Tenant/Resources/TenantResource.php index acfb500..3b82fdc 100644 --- a/app/Domains/Tenant/Resources/TenantResource.php +++ b/app/Domains/Tenant/Resources/TenantResource.php @@ -24,8 +24,9 @@ class TenantResource extends JsonResource 'secondary_color' => $this->secondary_color, 'danger_color' => $this->danger_color, 'header_footer_bg_color' => $this->header_footer_bg_color, - 'header_logo' => $this->headerLogo?->getTemporaryUrl(), - 'footer_logo' => $this->footerLogo?->getTemporaryUrl(), + // 1 day + 'header_logo' => $this->headerLogo?->getTemporaryUrl(1440), + 'footer_logo' => $this->footerLogo?->getTemporaryUrl(1440 ), ]; } } diff --git a/app/Domains/Tenant/Services/TenantService.php b/app/Domains/Tenant/Services/TenantService.php index 0e9e77e..3964498 100644 --- a/app/Domains/Tenant/Services/TenantService.php +++ b/app/Domains/Tenant/Services/TenantService.php @@ -27,33 +27,40 @@ class TenantService unset($data['header_logo'], $data['footer_logo']); - /** @var Tenant $tenant */ - $tenant = Tenant::query()->create($data); - + $headerAttachmentId = null; if ($headerLogo) { $attachment = Str::isUuid($headerLogo) ? \App\Domains\Attachable\Models\Attachment::query()->where('key', $headerLogo)->first() : $this->attachmentService->store($headerLogo, 'tenants'); if ($attachment) { - $tenant->header_logo_id = $attachment->id; - $tenant->attachments()->attach($attachment->id); + $headerAttachmentId = $attachment->id; } } + $footerAttachmentId = null; if ($footerLogo) { $attachment = Str::isUuid($footerLogo) ? \App\Domains\Attachable\Models\Attachment::query()->where('key', $footerLogo)->first() : $this->attachmentService->store($footerLogo, 'tenants'); if ($attachment) { - $tenant->footer_logo_id = $attachment->id; - $tenant->attachments()->attach($attachment->id); + $footerAttachmentId = $attachment->id; } } - if ($headerLogo || $footerLogo) { - $tenant->save(); + $data['header_logo_id'] = $headerAttachmentId; + $data['footer_logo_id'] = $footerAttachmentId; + + /** @var Tenant $tenant */ + $tenant = Tenant::query()->create($data); + + if ($headerAttachmentId) { + $tenant->attachments()->attach($headerAttachmentId); + } + + if ($footerAttachmentId) { + $tenant->attachments()->attach($footerAttachmentId); } return $tenant; diff --git a/database/migrations/2026_06_18_135000_create_tenants_table.php b/database/migrations/2026_06_18_135000_create_tenants_table.php index 05948f5..4d2cc5d 100644 --- a/database/migrations/2026_06_18_135000_create_tenants_table.php +++ b/database/migrations/2026_06_18_135000_create_tenants_table.php @@ -15,13 +15,13 @@ return new class extends Migration $table->id(); $table->string('codigo')->unique(); $table->string('nombre'); - $table->string('dominio')->nullable(); - $table->string('primary_color')->nullable(); - $table->string('secondary_color')->nullable(); - $table->string('danger_color')->nullable(); - $table->string('header_footer_bg_color')->nullable(); - $table->unsignedBigInteger('header_logo_id')->nullable(); - $table->unsignedBigInteger('footer_logo_id')->nullable(); + $table->string('dominio'); + $table->string('primary_color'); + $table->string('secondary_color'); + $table->string('danger_color'); + $table->string('header_footer_bg_color'); + $table->unsignedBigInteger('header_logo_id'); + $table->unsignedBigInteger('footer_logo_id'); $table->timestamps(); }); } diff --git a/database/migrations/2026_06_25_000500_create_attachments_table.php b/database/migrations/2026_06_25_000500_create_attachments_table.php index ad43dae..61f127d 100644 --- a/database/migrations/2026_06_25_000500_create_attachments_table.php +++ b/database/migrations/2026_06_25_000500_create_attachments_table.php @@ -33,12 +33,12 @@ return new class extends Migration $table->foreign('header_logo_id') ->references('id') ->on('attachments') - ->nullOnDelete(); + ->cascadeOnDelete(); $table->foreign('footer_logo_id') ->references('id') ->on('attachments') - ->nullOnDelete(); + ->cascadeOnDelete(); }); } diff --git a/tests/Feature/Cart/CartControllerTest.php b/tests/Feature/Cart/CartControllerTest.php index d4f4a7c..445e6fd 100644 --- a/tests/Feature/Cart/CartControllerTest.php +++ b/tests/Feature/Cart/CartControllerTest.php @@ -17,11 +17,7 @@ class CartControllerTest extends TestCase public function test_it_returns_an_empty_guest_cart_when_cart_does_not_exist(): void { - Tenant::create([ - 'codigo' => 'acme', - 'nombre' => 'Acme', - 'dominio' => 'acme.com', - ]); + $this->createTenant('acme', 'Acme', 'acme.com'); $this->getJson('/api/tenants/acme/cart') ->assertOk() @@ -272,10 +268,10 @@ class CartControllerTest extends TestCase string $price, string $slugPrefix = 'shirt', ): ProductVariant { - Tenant::query()->firstOrCreate( - ['codigo' => $tenantCode], - ['nombre' => ucfirst($tenantCode), 'dominio' => "{$tenantCode}.com"], - ); + $tenant = Tenant::query()->where('codigo', $tenantCode)->first(); + if (! $tenant) { + $this->createTenant($tenantCode, ucfirst($tenantCode), "{$tenantCode}.com"); + } $category = \App\Domains\Catalog\Models\Category::query()->create([ 'tenant_code' => $tenantCode, @@ -300,4 +296,37 @@ class CartControllerTest extends TestCase 'precio' => $price, ])->load('product'); } + + protected function createTenant(string $codigo, string $nombre, string $dominio): Tenant + { + $hdrKey = (string) \Illuminate\Support\Str::uuid(); + $ftrKey = (string) \Illuminate\Support\Str::uuid(); + + $headerAttachment = \App\Domains\Attachable\Models\Attachment::create([ + 'key' => $hdrKey, + 'path' => 'tenants/' . $hdrKey . '.png', + 'filename' => 'logo_header.png', + 'type' => \App\Domains\Attachable\Enums\AttachmentType::Image, + 'mime_type' => 'image/png', + ]); + $footerAttachment = \App\Domains\Attachable\Models\Attachment::create([ + 'key' => $ftrKey, + 'path' => 'tenants/' . $ftrKey . '.png', + 'filename' => 'logo_footer.png', + 'type' => \App\Domains\Attachable\Enums\AttachmentType::Image, + 'mime_type' => 'image/png', + ]); + + return Tenant::create([ + 'codigo' => $codigo, + 'nombre' => $nombre, + 'dominio' => $dominio, + 'primary_color' => '#111111', + 'secondary_color' => '#222222', + 'danger_color' => '#333333', + 'header_footer_bg_color' => '#444444', + 'header_logo_id' => $headerAttachment->id, + 'footer_logo_id' => $footerAttachment->id, + ]); + } } diff --git a/tests/Feature/Catalog/ProductAttributeControllerTest.php b/tests/Feature/Catalog/ProductAttributeControllerTest.php index 4959fd9..cd1668d 100644 --- a/tests/Feature/Catalog/ProductAttributeControllerTest.php +++ b/tests/Feature/Catalog/ProductAttributeControllerTest.php @@ -12,11 +12,7 @@ class ProductAttributeControllerTest extends TestCase public function test_it_creates_a_select_attribute_with_options(): void { - Tenant::create([ - 'codigo' => 'acme', - 'nombre' => 'Acme', - 'dominio' => 'acme.com', - ]); + $this->createTenant('acme', 'Acme', 'acme.com'); $response = $this->postJson('/api/tenants/acme/product-attributes', [ 'codigo' => 'color', @@ -62,11 +58,7 @@ class ProductAttributeControllerTest extends TestCase public function test_it_rejects_options_for_string_attributes(): void { - Tenant::create([ - 'codigo' => 'acme', - 'nombre' => 'Acme', - 'dominio' => 'acme.com', - ]); + $this->createTenant('acme', 'Acme', 'acme.com'); $response = $this->postJson('/api/tenants/acme/product-attributes', [ 'codigo' => 'material', @@ -81,4 +73,37 @@ class ProductAttributeControllerTest extends TestCase ->assertUnprocessable() ->assertJsonValidationErrors(['options']); } + + protected function createTenant(string $codigo, string $nombre, string $dominio): Tenant + { + $hdrKey = (string) \Illuminate\Support\Str::uuid(); + $ftrKey = (string) \Illuminate\Support\Str::uuid(); + + $headerAttachment = \App\Domains\Attachable\Models\Attachment::create([ + 'key' => $hdrKey, + 'path' => 'tenants/' . $hdrKey . '.png', + 'filename' => 'logo_header.png', + 'type' => \App\Domains\Attachable\Enums\AttachmentType::Image, + 'mime_type' => 'image/png', + ]); + $footerAttachment = \App\Domains\Attachable\Models\Attachment::create([ + 'key' => $ftrKey, + 'path' => 'tenants/' . $ftrKey . '.png', + 'filename' => 'logo_footer.png', + 'type' => \App\Domains\Attachable\Enums\AttachmentType::Image, + 'mime_type' => 'image/png', + ]); + + return Tenant::create([ + 'codigo' => $codigo, + 'nombre' => $nombre, + 'dominio' => $dominio, + 'primary_color' => '#111111', + 'secondary_color' => '#222222', + 'danger_color' => '#333333', + 'header_footer_bg_color' => '#444444', + 'header_logo_id' => $headerAttachment->id, + 'footer_logo_id' => $footerAttachment->id, + ]); + } } diff --git a/tests/Feature/Tenant/BootstrapTenantControllerTest.php b/tests/Feature/Tenant/BootstrapTenantControllerTest.php index 487b3f1..7ba822f 100644 --- a/tests/Feature/Tenant/BootstrapTenantControllerTest.php +++ b/tests/Feature/Tenant/BootstrapTenantControllerTest.php @@ -73,7 +73,7 @@ class BootstrapTenantControllerTest extends TestCase public function test_it_bootstraps_a_tenant_from_a_full_url(): void { - Tenant::create([ + $this->createTenant([ 'codigo' => 'acme', 'nombre' => 'Acme', 'dominio' => 'acme.com', @@ -151,6 +151,12 @@ class BootstrapTenantControllerTest extends TestCase 'codigo' => 'globex', 'nombre' => 'Globex', 'dominio' => 'acme.com', + 'primary_color' => '#111111', + 'secondary_color' => '#222222', + 'danger_color' => '#333333', + 'header_footer_bg_color' => '#444444', + 'header_logo' => (string) Str::uuid(), + 'footer_logo' => (string) Str::uuid(), ]); $secondResponse @@ -160,13 +166,13 @@ class BootstrapTenantControllerTest extends TestCase public function test_it_allows_keeping_the_same_domain_on_update_but_rejects_collisions(): void { - $tenant = Tenant::create([ + $tenant = $this->createTenant([ 'codigo' => 'acme', 'nombre' => 'Acme', 'dominio' => 'acme.com', ]); - $otherTenant = Tenant::create([ + $otherTenant = $this->createTenant([ 'codigo' => 'globex', 'nombre' => 'Globex', 'dominio' => 'globex.com', @@ -246,7 +252,7 @@ class BootstrapTenantControllerTest extends TestCase public function test_it_allows_partial_update_without_required_fields(): void { - $tenant = Tenant::create([ + $tenant = $this->createTenant([ 'codigo' => 'acme', 'nombre' => 'Acme', 'dominio' => 'acme.com', @@ -276,7 +282,13 @@ class BootstrapTenantControllerTest extends TestCase $response = $this->postJson('/api/tenants', [ 'codigo' => 'acme', 'nombre' => 'Acme', + 'dominio' => 'acme.com', 'primary_color' => 'invalid-color', + 'secondary_color' => '#222222', + 'danger_color' => '#333333', + 'header_footer_bg_color' => '#444444', + 'header_logo' => (string) Str::uuid(), + 'footer_logo' => (string) Str::uuid(), ]); $response->assertJsonValidationErrors(['primary_color']); @@ -284,7 +296,13 @@ class BootstrapTenantControllerTest extends TestCase $response2 = $this->postJson('/api/tenants', [ 'codigo' => 'acme', 'nombre' => 'Acme', + 'dominio' => 'acme.com', 'primary_color' => '#12345', + 'secondary_color' => '#222222', + 'danger_color' => '#333333', + 'header_footer_bg_color' => '#444444', + 'header_logo' => (string) Str::uuid(), + 'footer_logo' => (string) Str::uuid(), ]); $response2->assertJsonValidationErrors(['primary_color']); @@ -297,7 +315,13 @@ class BootstrapTenantControllerTest extends TestCase $response = $this->postJson('/api/tenants', [ 'codigo' => 'acme', 'nombre' => 'Acme', + 'dominio' => 'acme.com', + 'primary_color' => '#111111', + 'secondary_color' => '#222222', + 'danger_color' => '#333333', + 'header_footer_bg_color' => '#444444', 'header_logo' => UploadedFile::fake()->create('document.pdf', 10, 'application/pdf'), + 'footer_logo' => (string) Str::uuid(), ]); $response->assertJsonValidationErrors(['header_logo']); @@ -305,7 +329,13 @@ class BootstrapTenantControllerTest extends TestCase $response2 = $this->postJson('/api/tenants', [ 'codigo' => 'acme', 'nombre' => 'Acme', + 'dominio' => 'acme.com', + 'primary_color' => '#111111', + 'secondary_color' => '#222222', + 'danger_color' => '#333333', + 'header_footer_bg_color' => '#444444', 'header_logo' => 'data:application/pdf;base64,JVBERi0xLjQKJdcfqksKMSAwIG9iagogIDw8IC9UeXBlIC9DYXRhbG9nCiAgICAvUGFnZXMgMiAwIFI...', + 'footer_logo' => (string) Str::uuid(), ]); $response2->assertJsonValidationErrors(['header_logo']); @@ -321,6 +351,11 @@ class BootstrapTenantControllerTest extends TestCase $response = $this->postJson('/api/tenants', [ 'codigo' => 'acme', 'nombre' => 'Acme', + 'dominio' => 'acme.com', + 'primary_color' => '#111111', + 'secondary_color' => '#222222', + 'danger_color' => '#333333', + 'header_footer_bg_color' => '#444444', 'header_logo' => $header, 'footer_logo' => $footer, ]); @@ -348,16 +383,64 @@ class BootstrapTenantControllerTest extends TestCase $this->assertDatabaseHas('attachments', ['key' => $tenant->footerLogo->key]); } + private function createTenant(array $attributes = []): Tenant + { + $hdrKey = (string) Str::uuid(); + $ftrKey = (string) Str::uuid(); + + $headerAttachment = \App\Domains\Attachable\Models\Attachment::create([ + 'key' => $hdrKey, + 'path' => 'tenants/' . $hdrKey . '.png', + 'filename' => 'logo_header.png', + 'type' => \App\Domains\Attachable\Enums\AttachmentType::Image, + 'mime_type' => 'image/png', + ]); + $footerAttachment = \App\Domains\Attachable\Models\Attachment::create([ + 'key' => $ftrKey, + 'path' => 'tenants/' . $ftrKey . '.png', + 'filename' => 'logo_footer.png', + 'type' => \App\Domains\Attachable\Enums\AttachmentType::Image, + 'mime_type' => 'image/png', + ]); + + return Tenant::create(array_merge([ + 'codigo' => 'acme', + 'nombre' => 'Acme', + 'dominio' => 'acme.com', + 'primary_color' => '#ff0000', + 'secondary_color' => '#00ff00', + 'danger_color' => '#0000ff', + 'header_footer_bg_color' => '#ffffff', + 'header_logo_id' => $headerAttachment->id, + 'footer_logo_id' => $footerAttachment->id, + ], $attributes)); + } + public function test_it_stores_base64_logos_in_tenants_directory(): void { Storage::fake('s3'); $base64Image = 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mNk+M9QDwADhgGAWjR9awAAAABJRU5ErkJggg=='; + $ftrUuid = (string) Str::uuid(); + $footerAttachment = \App\Domains\Attachable\Models\Attachment::create([ + 'key' => $ftrUuid, + 'path' => 'tenants/' . $ftrUuid . '.png', + 'filename' => 'logo_footer.png', + 'type' => \App\Domains\Attachable\Enums\AttachmentType::Image, + 'mime_type' => 'image/png', + ]); + $response = $this->postJson('/api/tenants', [ 'codigo' => 'acme', 'nombre' => 'Acme', + 'dominio' => 'acme.com', + 'primary_color' => '#111111', + 'secondary_color' => '#222222', + 'danger_color' => '#333333', + 'header_footer_bg_color' => '#444444', 'header_logo' => $base64Image, + 'footer_logo' => $ftrUuid, ]); $response->assertCreated();