Loheide-eu-mitarbeiterplatt.../tests/Feature/Speiseplan/SpeiseplanTest.php
Friederich Loheide b50d089946
Some checks failed
CI / PHP 8.3 / mariadb:10.6 (pull_request) Failing after 3m18s
CI / PHP 8.4 / mariadb:10.6 (pull_request) Failing after 3m27s
CI / PHP 8.3 / mariadb:11.4 (pull_request) Failing after 3m0s
CI / PHP 8.4 / mariadb:11.4 (pull_request) Failing after 2m56s
CI / PHP 8.3 / mysql:8.0 (pull_request) Failing after 4m4s
CI / PHP 8.4 / mysql:8.0 (pull_request) Failing after 4m1s
CI / PHP 8.3 / mysql:8.4 (pull_request) Failing after 3m41s
CI / PHP 8.4 / mysql:8.4 (pull_request) Failing after 3m39s
CI / Build-Artefakte aktuell (pull_request) Failing after 1m10s
QM-Texterkennung: Imagick nur mit Ghostscript als verfügbar melden
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
2026-09-26 16:38:17 +00:00

100 lines
4 KiB
PHP

<?php
namespace Tests\Feature\Speiseplan;
use App\Models\Role;
use App\Models\User;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Http\UploadedFile;
use Illuminate\Support\Carbon;
use Illuminate\Support\Facades\Storage;
use Inertia\Testing\AssertableInertia as Assert;
use Inhaus\Plugins\Speiseplan\Models\Week;
use Inhaus\Plugins\Speiseplan\SpeiseplanPlugin;
use Tests\TestCase;
class SpeiseplanTest extends TestCase
{
use RefreshDatabase;
private User $kitchen;
protected function setUp(): void
{
parent::setUp();
Storage::fake('local');
Carbon::setTestNow('2026-09-30 09:00'); // Mittwoch, KW 40
$role = Role::query()->create(['name' => 'Küche']);
$role->grants()->create(['ability' => 'speiseplan.manage']);
$this->kitchen = User::factory()->withRoles([$role])->create();
}
protected function tearDown(): void
{
Carbon::setTestNow();
parent::tearDown();
}
private function upload(string $week): void
{
$this->actingAs($this->kitchen)->post('/speiseplan/wochen', ['week' => $week, 'image' => UploadedFile::fake()->image('plan.jpg', 1240, 1754)])->assertSessionHasNoErrors();
}
public function test_weeks_ahead_current_and_next_week_are_shown(): void
{
$this->upload('2026-10-01'); // irgendein Tag → Montag 28.09.
$this->upload('2026-10-05');
$this->upload('2026-10-19'); // drei Wochen im Voraus
$this->assertSame(['2026-09-28', '2026-10-05', '2026-10-19'], Week::query()->orderBy('week_start')->pluck('week_start')->map->toDateString()->all());
$this->actingAs(User::factory()->create())->get('/speiseplan')->assertOk()->assertInertia(fn (Assert $p) => $p
->component('speiseplan::Index')
->where('weeks.0.title', 'Diese Woche')
->where('weeks.0.plan.week', 40)
->where('weeks.0.plan.width', 1240)
->where('weeks.1.plan.week', 41)
->where('canManage', false)
->where('manage', null));
$this->get(Week::query()->first()->present()['image_url'])->assertOk();
}
public function test_uploading_again_replaces_the_week(): void
{
$this->upload('2026-09-28');
$old = Week::query()->sole()->path;
$this->upload('2026-09-29');
$this->assertSame(1, Week::query()->count());
Storage::disk('local')->assertMissing($old);
}
public function test_only_images_are_stored_and_only_the_kitchen_uploads(): void
{
$this->actingAs($this->kitchen)->post('/speiseplan/wochen', ['week' => '2026-09-28', 'image' => UploadedFile::fake()->create('plan.pdf', 10, 'application/pdf')])->assertSessionHasErrors('image');
$this->actingAs(User::factory()->create())->post('/speiseplan/wochen', ['week' => '2026-09-28', 'image' => UploadedFile::fake()->image('p.jpg')])->assertForbidden();
}
public function test_allergen_pdf_and_print_view(): void
{
$this->upload('2026-09-28');
$this->actingAs($this->kitchen)->post('/speiseplan/allergene', ['file' => UploadedFile::fake()->create('Allergene.pdf', 20, 'application/pdf')])->assertSessionHasNoErrors();
$user = User::factory()->create();
$this->actingAs($user)->get('/speiseplan')->assertInertia(fn (Assert $p) => $p->where('allergens.name', 'Allergene.pdf'));
$this->get('/speiseplan/allergene')->assertOk()->assertHeader('Content-Type', 'application/pdf');
$this->get('/speiseplan/druck')->assertOk()->assertInertia(fn (Assert $p) => $p->component('speiseplan::Print')->has('plans', 1));
}
public function test_old_weeks_are_deleted_after_two_weeks(): void
{
foreach (['2026-09-07', '2026-09-14', '2026-09-21', '2026-09-28'] as $week) {
$this->upload($week);
}
$this->assertSame(1, SpeiseplanPlugin::purge());
$this->assertSame(['2026-09-14', '2026-09-21', '2026-09-28'], Week::query()->orderBy('week_start')->pluck('week_start')->map->toDateString()->all());
}
}