Loheide-eu-mitarbeiterplatt.../tests/Feature/EventsEditorTest.php
Friederich Loheide d7d09d6b9a
Some checks failed
CI / PHP 8.3 / mariadb:10.6 (pull_request) Successful in 3m53s
CI / PHP 8.4 / mariadb:10.6 (pull_request) Successful in 3m42s
CI / PHP 8.3 / mariadb:11.4 (pull_request) Successful in 4m13s
CI / PHP 8.4 / mariadb:11.4 (pull_request) Successful in 4m10s
CI / PHP 8.3 / mysql:8.0 (pull_request) Failing after 5m46s
CI / PHP 8.4 / mysql:8.0 (pull_request) Failing after 5m35s
CI / PHP 8.3 / mysql:8.4 (pull_request) Failing after 4m54s
CI / PHP 8.4 / mysql:8.4 (pull_request) Failing after 4m53s
CI / Build-Artefakte aktuell (pull_request) Successful in 1m10s
Termine im Portal bearbeiten (0.23.0)
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
2026-09-27 07:56:48 +00:00

82 lines
4.1 KiB
PHP

<?php
namespace Tests\Feature;
use App\Models\Role;
use App\Models\User;
use App\Support\Permissions\Audiences;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\Storage;
use Inertia\Testing\AssertableInertia as Assert;
use Inhaus\Plugins\Events\Models\Event;
use Tests\TestCase;
class EventsEditorTest extends TestCase
{
use RefreshDatabase;
private User $editor;
protected function setUp(): void
{
parent::setUp();
Storage::fake('uploads');
$role = Role::query()->create(['name' => 'Terminredaktion']);
$role->grants()->create(['ability' => 'events.manage']);
$this->editor = User::factory()->withRoles([$role])->create();
}
public function test_events_are_created_edited_and_deleted_in_the_portal(): void
{
$pflege = Role::query()->create(['name' => 'Pflege']);
$this->actingAs($this->editor)->get('/redaktion/termine/neu')->assertOk()->assertInertia(fn (Assert $p) => $p->component('events::Editor')->where('event', null));
$this->post('/redaktion/termine', [
'title' => 'Sommerfest', 'all_day' => false,
'starts_at' => now()->addWeek()->format('Y-m-d').'T15:00', 'ends_at' => now()->addWeek()->format('Y-m-d').'T20:00',
'location' => 'Innenhof', 'description' => '<p>Mit <strong>Grill</strong><script>alert(1)</script></p>',
'is_published' => true, 'audience' => ['role' => [(string) $pflege->id]],
])->assertRedirect()->assertSessionHasNoErrors();
$event = Event::query()->where('title', 'Sommerfest')->firstOrFail();
$this->assertSame($this->editor->id, $event->author_id);
$this->assertStringContainsString('<strong>Grill</strong>', $event->description_html);
$this->assertStringNotContainsString('script', $event->description_html);
$this->assertSame([(string) $pflege->id], Audiences::get('event', $event->id)['role']);
// Zielgruppe greift
$this->actingAs(User::factory()->create())->get("/termine/{$event->id}")->assertNotFound();
$this->actingAs(User::factory()->withRoles([$pflege])->create())->get("/termine/{$event->id}")->assertOk()->assertInertia(fn (Assert $p) => $p->where('editUrl', null));
// Bearbeiten: ganztägig, Zielgruppe entfernen
$this->actingAs($this->editor)->get("/termine/{$event->id}")->assertInertia(fn (Assert $p) => $p->where('editUrl', route('events.edit', $event)));
$this->put("/redaktion/termine/{$event->id}", [
'title' => 'Sommerfest 2026', 'all_day' => true, 'starts_at' => '2026-07-10', 'ends_at' => '2026-07-11', 'is_published' => true, 'audience' => [],
])->assertRedirect(route('events.show', $event));
$event->refresh();
$this->assertTrue($event->all_day);
$this->assertSame('2026-07-10 00:00', $event->starts_at->format('Y-m-d H:i'));
$this->assertSame('2026-07-11 23:59', $event->ends_at->format('Y-m-d H:i'));
$this->assertSame([], Audiences::get('event', $event->id)['role']);
$this->put("/redaktion/termine/{$event->id}", ['title' => 'x', 'starts_at' => '2026-07-10T10:00', 'ends_at' => '2026-07-09T10:00'])
->assertSessionHasErrors(['ends_at' => 'Das Ende darf nicht vor dem Beginn liegen.']);
$this->delete("/redaktion/termine/{$event->id}")->assertRedirect('/termine');
$this->assertNull(Event::query()->find($event->id));
}
public function test_only_event_managers_may_edit(): void
{
$event = Event::factory()->create();
$user = User::factory()->create();
$this->actingAs($user)->get('/redaktion/termine/neu')->assertForbidden();
$this->actingAs($user)->post('/redaktion/termine', ['title' => 'x', 'starts_at' => '2026-07-10T10:00'])->assertForbidden();
$this->actingAs($user)->put("/redaktion/termine/{$event->id}", ['title' => 'x', 'starts_at' => '2026-07-10T10:00'])->assertForbidden();
$this->actingAs($user)->delete("/redaktion/termine/{$event->id}")->assertForbidden();
$this->actingAs($user)->get('/termine')->assertInertia(fn (Assert $p) => $p->where('canManage', false));
}
}