Loheide-eu-mitarbeiterplatt.../tests/Feature/Accessibility/EasyLanguageTest.php
Friederich Loheide 856a5c30af
Some checks failed
CI / PHP 8.3 / mariadb:10.6 (pull_request) Successful in 9m24s
CI / PHP 8.4 / mariadb:10.6 (pull_request) Successful in 9m15s
CI / PHP 8.3 / mariadb:11.4 (pull_request) Successful in 9m12s
CI / PHP 8.4 / mariadb:11.4 (pull_request) Successful in 9m21s
CI / PHP 8.3 / mysql:8.0 (pull_request) Failing after 10m33s
CI / PHP 8.4 / mysql:8.0 (pull_request) Failing after 10m5s
CI / PHP 8.3 / mysql:8.4 (pull_request) Failing after 8m10s
CI / PHP 8.4 / mysql:8.4 (pull_request) Failing after 8m8s
CI / Bekannte Sicherheitslücken in Paketen (pull_request) Successful in 48s
CI / Build-Artefakte aktuell (pull_request) Successful in 1m23s
Vorlesen und Leichte Sprache mit KI-Vorschlag (0.34.0)
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
2026-09-27 13:24:56 +00:00

94 lines
4.9 KiB
PHP

<?php
namespace Tests\Feature\Accessibility;
use App\Ai\AiAssistant;
use App\Models\User;
use App\Support\Settings;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\Crypt;
use Illuminate\Support\Facades\Http;
use Inertia\Testing\AssertableInertia as Assert;
use Inhaus\Plugins\News\Models\NewsPost;
use Inhaus\Plugins\Pages\Models\Page;
use Tests\TestCase;
class EasyLanguageTest extends TestCase
{
use RefreshDatabase;
private function enableAi(): void
{
app(Settings::class)->setMany(['ai.enabled' => true, 'ai.api_key' => Crypt::encryptString('sk-test'), 'ai.model' => 'claude-sonnet-5']);
}
public function test_editors_add_easy_language_and_readers_see_it(): void
{
$admin = User::factory()->admin()->create();
$reader = User::factory()->create();
$post = NewsPost::factory()->create(['published_at' => now()->subHour()]);
$this->actingAs($reader)->get("/neuigkeiten/{$post->slug}")->assertInertia(fn (Assert $page) => $page->where('easy', null));
$this->actingAs($reader)->put("/leichte-sprache/news/{$post->id}", ['html' => '<p>Hallo</p>'])->assertForbidden();
$this->actingAs($admin)->put("/leichte-sprache/news/{$post->id}", ['html' => '<h2>Das Fest</h2><p>Wir feiern.</p><script>alert(1)</script>'])
->assertRedirect()->assertSessionHas('status', 'Leichte Sprache gespeichert.');
$this->actingAs($reader)->get("/neuigkeiten/{$post->slug}")->assertInertia(fn (Assert $page) => $page
->where('easy.html', fn ($html) => str_contains($html, 'Wir feiern.') && ! str_contains($html, 'script'))
->where('easy.can_edit', false));
$this->actingAs($admin)->put("/leichte-sprache/news/{$post->id}", ['html' => '<p> </p>'])->assertSessionHas('status', 'Leichte Sprache entfernt.');
$this->actingAs($reader)->get("/neuigkeiten/{$post->slug}")->assertInertia(fn (Assert $page) => $page->where('easy', null));
$this->actingAs($admin)->put('/leichte-sprache/unbekannt/1', ['html' => '<p>x</p>'])->assertForbidden();
}
public function test_ai_suggestion_only_sends_public_text(): void
{
$this->enableAi();
$admin = User::factory()->admin()->create();
$page = Page::query()->create(['title' => 'Urlaub']);
$page->forceFill(['published_at' => now(), 'content' => ['blocks' => [
['id' => 'a1b2', 'type' => 'text', 'data' => ['html' => '<p>Urlaub beantragen Sie im Portal.</p>']],
['id' => 'c3d4', 'type' => 'text', 'data' => ['html' => '<p>Geheime Regel für Führungskräfte.</p>'], 'visibility' => ['role' => ['1']]],
]]])->save();
Http::fake([AiAssistant::ENDPOINT => Http::response(['content' => [['type' => 'text', 'text' => "```html\n<h2>Urlaub</h2><p>Sie wollen Urlaub?</p>\n```"]]])]);
$this->actingAs($admin)->get('/seiten/urlaub')->assertInertia(fn (Assert $p) => $p->where('easy.ai', true)->where('easy.can_edit', true));
$this->actingAs($admin)->postJson("/leichte-sprache/pages/{$page->id}/vorschlag")
->assertOk()->assertJsonPath('html', '<h2>Urlaub</h2><p>Sie wollen Urlaub?</p>');
Http::assertSent(fn ($request) => $request->hasHeader('x-api-key', 'sk-test')
&& $request['model'] === 'claude-sonnet-5'
&& str_contains($request['messages'][0]['content'], 'Urlaub beantragen')
&& ! str_contains($request['messages'][0]['content'], 'Geheime Regel'));
}
public function test_suggestions_need_a_configured_assistant_and_report_errors(): void
{
$admin = User::factory()->admin()->create();
$post = NewsPost::factory()->create(['published_at' => now()->subHour()]);
$this->actingAs($admin)->postJson("/leichte-sprache/news/{$post->id}/vorschlag")->assertUnprocessable()->assertJsonPath('message', 'Der KI-Assistent ist nicht eingerichtet.');
$this->enableAi();
Http::fake([AiAssistant::ENDPOINT => Http::response(['error' => ['message' => 'invalid x-api-key']], 401)]);
$this->actingAs($admin)->postJson("/leichte-sprache/news/{$post->id}/vorschlag")->assertUnprocessable()->assertJsonPath('message', 'Der API-Schlüssel wurde abgelehnt.');
}
public function test_ai_settings_page_keeps_the_key_secret(): void
{
$admin = User::factory()->admin()->create();
$this->actingAs($admin)->get('/admin/ki-assistent')->assertOk()->assertSee('Datenschutz');
$this->actingAs(User::factory()->create())->get('/admin/ki-assistent')->assertForbidden();
app(AiAssistant::class)->save(['enabled' => true, 'model' => null, 'api_key' => 'sk-geheim']);
$this->assertNotSame('sk-geheim', app(Settings::class)->get('ai.api_key'));
$this->assertTrue(app(AiAssistant::class)->available());
$this->assertSame(AiAssistant::DEFAULT_MODEL, app(AiAssistant::class)->model());
$this->actingAs($admin)->get('/admin/ki-assistent')->assertDontSee('sk-geheim');
}
}