Files
shopit-back/tests/Feature/Tenant/TenantTimezoneTest.php

77 lines
2.6 KiB
PHP

<?php
namespace Tests\Feature\Tenant;
use App\Domains\Core\Tenant\Models\Tenant;
use App\Shared\Attachable\Enums\AttachmentType;
use App\Shared\Attachable\Models\Attachment;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Str;
use Tests\TestCase;
class TenantTimezoneTest extends TestCase
{
use RefreshDatabase;
public function test_timezone_is_exposed_by_the_api(): void
{
$tenant = $this->createTenant('timezone');
$this->getJson("/api/tenants/{$tenant->codigo}")->assertOk()
->assertJsonPath('data.timezone', 'America/Argentina/Buenos_Aires');
}
public function test_event_stores_utc_and_timezone_changes_preserve_stored_instants(): void
{
$tenant = $this->createTenant('event-zone');
$date = $tenant->eventDates()->create([
'date' => '2026-09-21', 'time_start' => '22:00', 'time_end' => '02:00',
]);
$this->assertDatabaseHas('validity_times', [
'id' => $date->validity_time_id,
'fixed_starts_at' => '2026-09-22 01:00:00',
'fixed_expires_at' => '2026-09-22 05:00:00',
]);
$tenant->update(['timezone' => 'Asia/Tokyo']);
$this->assertSame('2026-09-22 01:00:00', $date->validityTime->fresh()->fixed_starts_at->format('Y-m-d H:i:s'));
}
private function createTenant(string $code): Tenant
{
$attachmentIds = collect(['header', 'footer'])->map(function (string $name): int {
$key = (string) Str::uuid();
return Attachment::query()->create([
'key' => $key,
'path' => "tenants/{$key}.png",
'filename' => "{$name}.png",
'type' => AttachmentType::Image,
'mime_type' => 'image/png',
])->id;
});
$clientId = DB::table('clients')->insertGetId([
'code' => $code,
'name' => Str::headline($code),
]);
$tenantId = DB::table('tenants')->insertGetId([
'client_id' => $clientId,
'codigo' => $code,
'nombre' => Str::headline($code),
'dominio' => "{$code}.test",
'primary_color' => '#000000',
'secondary_color' => '#000000',
'danger_color' => '#000000',
'success_color' => '#000000',
'header_bg_color' => '#ffffff',
'footer_bg_color' => '#ffffff',
'header_logo_id' => $attachmentIds[0],
'footer_logo_id' => $attachmentIds[1],
]);
return Tenant::query()->findOrFail($tenantId);
}
}