feat: localize guest endpoints and caching

This commit is contained in:
Codex Agent
2025-11-12 15:48:06 +01:00
parent d91108c883
commit 062932ce38
19 changed files with 1538 additions and 595 deletions

View File

@@ -0,0 +1,98 @@
<?php
namespace Tests\Feature\Api\Event;
use App\Models\Emotion;
use App\Models\Event;
use App\Models\Task;
use App\Models\TaskCollection;
use App\Services\EventJoinTokenService;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Tests\TestCase;
class EventTasksLocaleTest extends TestCase
{
use RefreshDatabase;
public function test_it_returns_requested_locale_payload(): void
{
config(['app.supported_locales' => ['de', 'en']]);
$event = Event::factory()->create([
'default_locale' => 'de',
'status' => 'published',
]);
$token = app(EventJoinTokenService::class)->createToken($event, ['label' => 'test']);
$plainToken = $token->plain_token;
$emotion = Emotion::factory()->create([
'name' => ['de' => 'Freude', 'en' => 'Joy'],
]);
$task = Task::factory()->create([
'tenant_id' => $event->tenant_id,
'title' => ['de' => 'Kussmoment', 'en' => 'Kiss Moment'],
'description' => ['de' => 'DE Beschreibung', 'en' => 'EN Description'],
'example_text' => ['de' => 'DE Anleitung', 'en' => 'EN Instructions'],
'emotion_id' => $emotion->id,
]);
$collection = TaskCollection::factory()->create([
'tenant_id' => $event->tenant_id,
]);
$collection->tasks()->attach($task->id, ['sort_order' => 1]);
$collection->events()->attach($event->id, ['sort_order' => 1]);
$response = $this->getJson("/api/v1/events/{$plainToken}/tasks?locale=en");
$response->assertOk();
$response->assertHeader('X-Content-Locale', 'en');
$response->assertJsonCount(1);
$response->assertJson([[
'id' => $task->id,
'title' => 'Kiss Moment',
'description' => 'EN Description',
'instructions' => 'EN Instructions',
'duration' => 3,
'is_completed' => false,
]]);
$this->assertSame('emotion-'.$emotion->id, $response->json('0.emotion.slug'));
}
public function test_it_falls_back_to_event_locale_when_locale_invalid(): void
{
config(['app.supported_locales' => ['de', 'en']]);
$event = Event::factory()->create([
'default_locale' => 'de',
'status' => 'published',
]);
$token = app(EventJoinTokenService::class)->createToken($event, ['label' => 'fallback']);
$plainToken = $token->plain_token;
$task = Task::factory()->create([
'tenant_id' => $event->tenant_id,
'title' => ['de' => 'Aufgabe', 'en' => 'Task'],
'description' => ['de' => 'Beschreibung', 'en' => 'Description'],
'example_text' => ['de' => 'Beispiel', 'en' => 'Example'],
]);
$collection = TaskCollection::factory()->create([
'tenant_id' => $event->tenant_id,
]);
$collection->tasks()->attach($task->id, ['sort_order' => 1]);
$collection->events()->attach($event->id, ['sort_order' => 1]);
$response = $this->withHeaders(['Accept-Language' => 'zz-ZZ'])
->getJson("/api/v1/events/{$plainToken}/tasks?locale=it");
$response->assertOk();
$response->assertHeader('X-Content-Locale', 'de');
$response->assertJson([[
'title' => 'Aufgabe',
]]);
}
}