- Added `ticket_generation_policy` column to `catalog_items` table to manage ticket generation strategies. - Created `ticket_validity_times` table to allow multiple validity times per ticket. - Introduced `validity_time_id` column in `event_dates` table to associate event dates with validity times. - Established `ticket_validity_groups` and `ticket_validity_group_times` tables to group validity times for tickets. - Updated seeder to set default ticket generation policy for specific tenant. - Enhanced tests to cover new functionality, including ticket generation based on event dates and validity times. - Refactored ticket validity checks to accommodate multiple validity times in a group.
164 lines
6.2 KiB
PHP
164 lines
6.2 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\Tenant\Models\Tenant;
|
|
use App\Domains\Ticket\Enums\ValidityTimeType;
|
|
use App\Domains\Ticket\Models\Ticket;
|
|
use App\Domains\Ticket\Models\ValidityTime;
|
|
use Illuminate\Foundation\Testing\RefreshDatabase;
|
|
use Illuminate\Support\Str;
|
|
use Tests\TestCase;
|
|
|
|
class TicketControllerTest extends TestCase
|
|
{
|
|
use RefreshDatabase;
|
|
|
|
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');
|
|
$validityTimes = collect([
|
|
ValidityTime::query()->create([
|
|
'type' => ValidityTimeType::FixedWindow,
|
|
'fixed_starts_at' => now()->subHour(),
|
|
'fixed_expires_at' => now()->addHour(),
|
|
]),
|
|
ValidityTime::query()->create([
|
|
'type' => ValidityTimeType::FixedWindow,
|
|
'fixed_starts_at' => now()->addDay(),
|
|
'fixed_expires_at' => now()->addDays(2),
|
|
]),
|
|
]);
|
|
$validityTimes->each(function (ValidityTime $validityTime) use ($newerTicket): void {
|
|
$group = $newerTicket->validityGroups()->create();
|
|
$group->validityTimes()->attach($validityTime);
|
|
});
|
|
|
|
$this->actingAs($user, 'sanctum')
|
|
->getJson("/api/tenants/{$tenant->codigo}/tickets")
|
|
->assertOk()
|
|
->assertJsonCount(2, 'data')
|
|
->assertJsonPath('data.0.id', $newerTicket->id)
|
|
->assertJsonPath('data.0.name', 'Newer ticket')
|
|
->assertJsonPath('data.0.is_valid', true)
|
|
->assertJsonPath('data.0.is_expired', false)
|
|
->assertJsonPath('data.0.is_used', false)
|
|
->assertJsonCount(2, 'data.0.validity_times')
|
|
->assertJsonCount(2, 'data.0.validity_groups')
|
|
->assertJsonCount(1, 'data.0.validity_groups.0.validity_times')
|
|
->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
|
|
{
|
|
return Ticket::query()->create([
|
|
'tenant_code' => $tenant->codigo,
|
|
'ticket' => (string) Str::uuid(),
|
|
'name' => $name,
|
|
'description' => "Description for {$name}",
|
|
'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',
|
|
]);
|
|
}
|
|
}
|