feat: add slug field to events and implement tests for slug uniqueness per tenant
This commit is contained in:
@@ -2,9 +2,9 @@
|
||||
|
||||
namespace App\Domains\Ticketing\Event\Models;
|
||||
|
||||
use App\Domains\Commerce\Catalog\Models\CatalogItem;
|
||||
use App\Domains\Core\Tenant\Models\SocialMedia;
|
||||
use App\Domains\Core\Tenant\Models\Tenant;
|
||||
use App\Domains\Commerce\Catalog\Models\CatalogItem;
|
||||
use App\Shared\Attachable\Models\Attachment;
|
||||
use Illuminate\Database\Eloquent\Attributes\Fillable;
|
||||
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||
|
||||
@@ -2,8 +2,8 @@
|
||||
|
||||
namespace App\Domains\Ticketing\Event\Resources;
|
||||
|
||||
use App\Domains\Ticketing\Event\Services\EventDateGroupingService;
|
||||
use App\Domains\Ticketing\Event\Models\Event;
|
||||
use App\Domains\Ticketing\Event\Services\EventDateGroupingService;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Http\Resources\Json\JsonResource;
|
||||
|
||||
|
||||
@@ -11,14 +11,14 @@ return new class extends Migration
|
||||
public function up(): void
|
||||
{
|
||||
Schema::table('events', function (Blueprint $table): void {
|
||||
$table->string('slug')->nullable()->after('tenant_code');
|
||||
$table->string('slug')->nullable();
|
||||
});
|
||||
|
||||
$usedSlugsByTenant = [];
|
||||
|
||||
foreach (DB::table('events')->orderBy('tenant_code')->orderBy('id')->get() as $event) {
|
||||
$baseSlug = Str::slug((string) $event->title);
|
||||
$baseSlug = $baseSlug !== '' ? mb_substr($baseSlug, 0, 240) : 'evento-'.$event->id;
|
||||
$baseSlug = $baseSlug !== '' ? substr($baseSlug, 0, 240) : 'evento-'.$event->id;
|
||||
$slug = $baseSlug;
|
||||
$suffix = 2;
|
||||
|
||||
|
||||
88
tests/Feature/Event/PublicEventControllerTest.php
Normal file
88
tests/Feature/Event/PublicEventControllerTest.php
Normal file
@@ -0,0 +1,88 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\Feature\Event;
|
||||
|
||||
use App\Domains\Core\Tenant\Models\Tenant;
|
||||
use App\Domains\Ticketing\Event\Models\Event;
|
||||
use App\Shared\Attachable\Enums\AttachmentType;
|
||||
use App\Shared\Attachable\Models\Attachment;
|
||||
use Database\Seeders\WebsiteTypeSeeder;
|
||||
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||||
use Illuminate\Support\Str;
|
||||
use Tests\TestCase;
|
||||
|
||||
class PublicEventControllerTest extends TestCase
|
||||
{
|
||||
use RefreshDatabase;
|
||||
|
||||
public function test_it_resolves_an_event_by_slug_within_its_tenant(): void
|
||||
{
|
||||
$this->seed(WebsiteTypeSeeder::class);
|
||||
$firstTenant = $this->createTenant('first');
|
||||
$secondTenant = $this->createTenant('second');
|
||||
|
||||
Event::query()->create([
|
||||
'tenant_code' => $firstTenant->codigo,
|
||||
'slug' => 'gran-fiesta',
|
||||
'title' => 'Fiesta del primer tenant',
|
||||
'published_at' => now()->subMinute(),
|
||||
]);
|
||||
Event::query()->create([
|
||||
'tenant_code' => $secondTenant->codigo,
|
||||
'slug' => 'gran-fiesta',
|
||||
'title' => 'Fiesta del segundo tenant',
|
||||
'published_at' => now()->subMinute(),
|
||||
]);
|
||||
|
||||
$this->getJson('/api/tenants/second/events/gran-fiesta')
|
||||
->assertOk()
|
||||
->assertJsonPath('data.slug', 'gran-fiesta')
|
||||
->assertJsonPath('data.title', 'Fiesta del segundo tenant');
|
||||
}
|
||||
|
||||
public function test_it_does_not_resolve_the_old_numeric_event_url(): void
|
||||
{
|
||||
$this->seed(WebsiteTypeSeeder::class);
|
||||
$tenant = $this->createTenant('acme');
|
||||
$event = Event::query()->create([
|
||||
'tenant_code' => $tenant->codigo,
|
||||
'slug' => 'gran-fiesta',
|
||||
'title' => 'Gran Fiesta',
|
||||
'published_at' => now()->subMinute(),
|
||||
]);
|
||||
|
||||
$this->getJson("/api/tenants/acme/events/{$event->id}")->assertNotFound();
|
||||
}
|
||||
|
||||
private function createTenant(string $code): Tenant
|
||||
{
|
||||
$header = $this->createAttachment($code.'-header');
|
||||
$footer = $this->createAttachment($code.'-footer');
|
||||
|
||||
return Tenant::query()->create([
|
||||
'codigo' => $code,
|
||||
'nombre' => ucfirst($code),
|
||||
'dominio' => $code.'.test',
|
||||
'storefront_website_type_code' => 'onticket_multi_event',
|
||||
'primary_color' => '#000000',
|
||||
'secondary_color' => '#000000',
|
||||
'danger_color' => '#000000',
|
||||
'success_color' => '#000000',
|
||||
'header_bg_color' => '#000000',
|
||||
'footer_bg_color' => '#000000',
|
||||
'header_logo_id' => $header->id,
|
||||
'footer_logo_id' => $footer->id,
|
||||
]);
|
||||
}
|
||||
|
||||
private function createAttachment(string $name): Attachment
|
||||
{
|
||||
return Attachment::query()->create([
|
||||
'key' => (string) Str::uuid(),
|
||||
'path' => 'test/'.$name.'.png',
|
||||
'filename' => $name.'.png',
|
||||
'type' => AttachmentType::Image,
|
||||
'mime_type' => 'image/png',
|
||||
]);
|
||||
}
|
||||
}
|
||||
37
tests/Feature/Migrations/AddSlugsToEventsTest.php
Normal file
37
tests/Feature/Migrations/AddSlugsToEventsTest.php
Normal file
@@ -0,0 +1,37 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\Feature\Migrations;
|
||||
|
||||
use Illuminate\Database\Schema\Blueprint;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use Illuminate\Support\Facades\Schema;
|
||||
use Tests\TestCase;
|
||||
|
||||
class AddSlugsToEventsTest extends TestCase
|
||||
{
|
||||
public function test_it_backfills_unique_slugs_per_tenant(): void
|
||||
{
|
||||
Schema::create('events', function (Blueprint $table): void {
|
||||
$table->id();
|
||||
$table->string('tenant_code');
|
||||
$table->string('title');
|
||||
});
|
||||
|
||||
DB::table('events')->insert([
|
||||
['id' => 1, 'tenant_code' => 'acme', 'title' => 'Gran Fiesta'],
|
||||
['id' => 2, 'tenant_code' => 'acme', 'title' => 'Gran Fiesta'],
|
||||
['id' => 3, 'tenant_code' => 'other', 'title' => 'Gran Fiesta'],
|
||||
['id' => 4, 'tenant_code' => 'acme', 'title' => '---'],
|
||||
]);
|
||||
|
||||
$migration = require database_path('migrations/2026_09_22_010000_add_slugs_to_events.php');
|
||||
$migration->up();
|
||||
|
||||
$this->assertSame([
|
||||
1 => 'gran-fiesta',
|
||||
2 => 'gran-fiesta-2',
|
||||
3 => 'gran-fiesta',
|
||||
4 => 'evento-4',
|
||||
], DB::table('events')->orderBy('id')->pluck('slug', 'id')->all());
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user