261 lines
10 KiB
PHP
261 lines
10 KiB
PHP
<?php
|
|
|
|
namespace Tests\Feature\Ticket;
|
|
|
|
use App\Domains\Attachable\Enums\AttachmentType;
|
|
use App\Domains\Attachable\Models\Attachment;
|
|
use App\Domains\Auth\Models\User;
|
|
use App\Domains\Catalog\Models\Attribute;
|
|
use App\Domains\Catalog\Models\CatalogItem;
|
|
use App\Domains\Catalog\Models\Inventory;
|
|
use App\Domains\Event\Events\EventDateRescheduled;
|
|
use App\Domains\Event\Events\EventDateSuspended;
|
|
use App\Domains\Event\Models\EventDate;
|
|
use App\Domains\Event\Services\EventService;
|
|
use App\Domains\Tenant\Models\Tenant;
|
|
use App\Domains\Ticket\Models\Ticket;
|
|
use Illuminate\Foundation\Testing\RefreshDatabase;
|
|
use Illuminate\Support\Facades\Event;
|
|
use Illuminate\Support\Str;
|
|
use Tests\TestCase;
|
|
|
|
class TicketControllerTest extends TestCase
|
|
{
|
|
use RefreshDatabase;
|
|
|
|
public function test_ticket_names_follow_reschedules_without_changing_variant_selections(): void
|
|
{
|
|
Event::fake([EventDateRescheduled::class, EventDateSuspended::class]);
|
|
$tenant = $this->createTenant('rescheduled');
|
|
$user = User::factory()->create();
|
|
$ticket = $this->createTicket($tenant, $user, 'COMIDA');
|
|
$item = $ticket->sourceCatalogItem;
|
|
$attribute = Attribute::query()->create([
|
|
'tenant_codigo' => $tenant->codigo,
|
|
'codigo' => 'service',
|
|
'nombre' => 'Servicio',
|
|
'type' => 'string',
|
|
]);
|
|
$itemAttribute = $item->itemAttributes()->create([
|
|
'attribute_id' => $attribute->id,
|
|
'ticket_label' => 'Servicio',
|
|
]);
|
|
$source = $tenant->eventDates()->create([
|
|
'date' => '2026-10-29',
|
|
'time_start' => '08:00:00',
|
|
'time_end' => '10:00:00',
|
|
]);
|
|
$variant = $item->variants()->create([
|
|
'event_date_id' => $source->id,
|
|
'inventory_id' => Inventory::query()->create()->id,
|
|
]);
|
|
$variant->definitions()->create([
|
|
'item_attribute_id' => $itemAttribute->id,
|
|
'value' => 'COMEDOR',
|
|
]);
|
|
$ticket->update(['source_variant_id' => $variant->id]);
|
|
|
|
foreach (['29/10/2026', '02/11/2026', '05/11/2026'] as $step => $expectedDate) {
|
|
if ($step > 0) {
|
|
$previous = $step === 1 ? $source : $tenant->eventDates()->whereDate('date', '2026-11-02')->firstOrFail();
|
|
app(EventService::class)->rescheduleDateForTenant($tenant, $previous, [
|
|
'date' => $step === 1 ? '2026-11-02' : '2026-11-05',
|
|
]);
|
|
}
|
|
|
|
$this->actingAs($user, 'sanctum')
|
|
->getJson("/api/tenants/{$tenant->codigo}/tickets")
|
|
->assertOk()
|
|
->assertJsonPath('data.0.name', "COMIDA ({$expectedDate}, Servicio COMEDOR)")
|
|
->assertJsonPath('data.0.ticket', $ticket->ticket);
|
|
}
|
|
|
|
$destination = $tenant->eventDates()->whereDate('date', '2026-11-05')->firstOrFail();
|
|
$variant->eventDates()->sync([$source->id, $destination->id]);
|
|
$this->assertSame('COMIDA (05/11/2026, Servicio COMEDOR)', $ticket->fresh()->name);
|
|
$this->assertSame('29/10/2026', $variant->fresh()->selectionOptions()->get('event_date')[0]['label']);
|
|
$this->assertSame($source->id, $variant->fresh()->event_date_id);
|
|
|
|
app(EventService::class)->suspendDateForTenant($tenant, $destination);
|
|
|
|
$this->actingAs($user, 'sanctum')
|
|
->getJson("/api/tenants/{$tenant->codigo}/tickets")
|
|
->assertOk()
|
|
->assertJsonPath('data.0.name', 'COMIDA (05/11/2026, Servicio COMEDOR)')
|
|
->assertJsonPath('data.0.status', 'disabled')
|
|
->assertJsonPath('data.0.is_valid', false)
|
|
->assertJsonPath('data.0.starts_at', null)
|
|
->assertJsonPath('data.0.expires_at', null)
|
|
->assertJsonPath('data.0.ticket', $ticket->ticket);
|
|
}
|
|
|
|
public function test_an_authenticated_user_can_list_their_tickets_for_the_tenant(): void
|
|
{
|
|
$tenant = $this->createTenant('current');
|
|
$user = User::factory()->create();
|
|
$olderTicket = $this->createTicket($tenant, $user, 'Older ticket');
|
|
$newerTicket = $this->createTicket($tenant, $user, 'Newer ticket');
|
|
$catalogItem = CatalogItem::query()->create([
|
|
'tenant_code' => $tenant->codigo,
|
|
'slug' => 'dated-ticket',
|
|
'nombre' => 'Dated ticket',
|
|
'descripcion' => 'Dated ticket',
|
|
'precio' => 10,
|
|
'has_tickets' => true,
|
|
]);
|
|
$eventDates = collect([
|
|
EventDate::query()->create([
|
|
'tenant_code' => $tenant->codigo,
|
|
'date' => now()->format('Y-m-d'),
|
|
'time_start' => now()->subHour()->format('H:i:s'),
|
|
'time_end' => now()->addHour()->format('H:i:s'),
|
|
]),
|
|
EventDate::query()->create([
|
|
'tenant_code' => $tenant->codigo,
|
|
'date' => now()->addDay()->format('Y-m-d'),
|
|
'time_start' => '00:00:00',
|
|
'time_end' => '23:59:59',
|
|
]),
|
|
]);
|
|
$variant = $catalogItem->variants()->create([
|
|
'inventory_id' => Inventory::query()->create()->id,
|
|
]);
|
|
$variant->eventDates()->sync($eventDates->pluck('id'));
|
|
$newerTicket->update([
|
|
'source_catalog_item_id' => $catalogItem->id,
|
|
'source_variant_id' => $variant->id,
|
|
]);
|
|
$expectedName = 'Dated ticket ('.$eventDates
|
|
->pluck('date')
|
|
->map(fn ($date): string => $date->format('d/m/Y'))
|
|
->implode(', ').')';
|
|
|
|
$this->actingAs($user, 'sanctum')
|
|
->getJson("/api/tenants/{$tenant->codigo}/tickets")
|
|
->assertOk()
|
|
->assertJsonCount(2, 'data')
|
|
->assertJsonPath('data.0.id', $newerTicket->id)
|
|
->assertJsonPath('data.0.name', $expectedName)
|
|
->assertJsonPath('data.0.is_valid', true)
|
|
->assertJsonPath('data.0.is_expired', false)
|
|
->assertJsonPath('data.0.is_used', false)
|
|
->assertJsonMissingPath('data.0.validity_times')
|
|
->assertJsonMissingPath('data.0.validity_groups')
|
|
->assertJsonMissingPath('data.0.validity_time')
|
|
->assertJsonPath('data.1.id', $olderTicket->id)
|
|
->assertJsonMissingPath('meta')
|
|
->assertJsonMissingPath('links');
|
|
}
|
|
|
|
public function test_it_does_not_include_tickets_from_other_users_or_tenants(): void
|
|
{
|
|
$tenant = $this->createTenant('current');
|
|
$otherTenant = $this->createTenant('other');
|
|
$user = User::factory()->create();
|
|
$otherUser = User::factory()->create();
|
|
$visibleTicket = $this->createTicket($tenant, $user, 'Visible');
|
|
$this->createTicket($tenant, $otherUser, 'Other user');
|
|
$this->createTicket($otherTenant, $user, 'Other tenant');
|
|
|
|
$this->actingAs($user, 'sanctum')
|
|
->getJson("/api/tenants/{$tenant->codigo}/tickets")
|
|
->assertOk()
|
|
->assertJsonCount(1, 'data')
|
|
->assertJsonPath('data.0.id', $visibleTicket->id);
|
|
}
|
|
|
|
public function test_authentication_is_required_to_list_tickets(): void
|
|
{
|
|
$tenant = $this->createTenant('current');
|
|
|
|
$this->getJson("/api/tenants/{$tenant->codigo}/tickets")
|
|
->assertUnauthorized();
|
|
}
|
|
|
|
public function test_an_authenticated_user_can_download_a_pdf_for_their_tickets(): void
|
|
{
|
|
$tenant = $this->createTenant('current');
|
|
$user = User::factory()->create();
|
|
$firstTicket = $this->createTicket($tenant, $user, 'First ticket');
|
|
$secondTicket = $this->createTicket($tenant, $user, 'Second ticket');
|
|
|
|
$response = $this->actingAs($user, 'sanctum')
|
|
->post("/api/tenants/{$tenant->codigo}/tickets/pdf", [
|
|
'ticket_ids' => [$firstTicket->id, $secondTicket->id],
|
|
]);
|
|
|
|
$response
|
|
->assertOk()
|
|
->assertHeader('content-type', 'application/pdf')
|
|
->assertHeader(
|
|
'content-disposition',
|
|
"attachment; filename=tickets_{$secondTicket->id}_{$firstTicket->id}.pdf"
|
|
);
|
|
$this->assertStringStartsWith('%PDF', $response->getContent());
|
|
}
|
|
|
|
public function test_a_user_cannot_download_someone_elses_ticket(): void
|
|
{
|
|
$tenant = $this->createTenant('current');
|
|
$user = User::factory()->create();
|
|
$otherUser = User::factory()->create();
|
|
$ownTicket = $this->createTicket($tenant, $user, 'Own ticket');
|
|
$otherTicket = $this->createTicket($tenant, $otherUser, 'Other ticket');
|
|
|
|
$this->actingAs($user, 'sanctum')
|
|
->postJson("/api/tenants/{$tenant->codigo}/tickets/pdf", [
|
|
'ticket_ids' => [$ownTicket->id, $otherTicket->id],
|
|
])
|
|
->assertNotFound();
|
|
}
|
|
|
|
private function createTicket(Tenant $tenant, User $user, string $name): Ticket
|
|
{
|
|
$catalogItem = CatalogItem::query()->create([
|
|
'tenant_code' => $tenant->codigo,
|
|
'slug' => Str::slug($name).'-'.Str::lower(Str::random(8)),
|
|
'nombre' => $name,
|
|
'descripcion' => "Description for {$name}",
|
|
'precio' => 10,
|
|
'has_tickets' => true,
|
|
]);
|
|
|
|
return Ticket::query()->create([
|
|
'tenant_code' => $tenant->codigo,
|
|
'ticket' => (string) Str::uuid(),
|
|
'source_catalog_item_id' => $catalogItem->id,
|
|
'user_id' => $user->id,
|
|
]);
|
|
}
|
|
|
|
private function createTenant(string $code): Tenant
|
|
{
|
|
$headerLogo = $this->createAttachment("{$code}-header");
|
|
$footerLogo = $this->createAttachment("{$code}-footer");
|
|
|
|
return Tenant::query()->create([
|
|
'codigo' => $code,
|
|
'nombre' => ucfirst($code),
|
|
'dominio' => "{$code}.local",
|
|
'primary_color' => '#000000',
|
|
'secondary_color' => '#000000',
|
|
'danger_color' => '#000000',
|
|
'success_color' => '#000000',
|
|
'header_bg_color' => '#000000',
|
|
'footer_bg_color' => '#000000',
|
|
'header_logo_id' => $headerLogo->id,
|
|
'footer_logo_id' => $footerLogo->id,
|
|
]);
|
|
}
|
|
|
|
private function createAttachment(string $name): Attachment
|
|
{
|
|
return Attachment::query()->create([
|
|
'path' => "test/{$name}.png",
|
|
'filename' => "{$name}.png",
|
|
'type' => AttachmentType::Image,
|
|
'mime_type' => 'image/png',
|
|
]);
|
|
}
|
|
}
|