Some checks failed
CI / PHP 8.3 / mariadb:10.6 (pull_request) Successful in 5m14s
CI / PHP 8.4 / mariadb:10.6 (pull_request) Successful in 5m4s
CI / PHP 8.3 / mariadb:11.4 (pull_request) Successful in 4m58s
CI / PHP 8.4 / mariadb:11.4 (pull_request) Successful in 5m0s
CI / PHP 8.3 / mysql:8.0 (pull_request) Failing after 7m38s
CI / PHP 8.4 / mysql:8.0 (pull_request) Failing after 7m28s
CI / PHP 8.3 / mysql:8.4 (pull_request) Failing after 5m54s
CI / PHP 8.4 / mysql:8.4 (pull_request) Failing after 5m49s
CI / Build-Artefakte aktuell (pull_request) Successful in 1m51s
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
348 lines
18 KiB
PHP
348 lines
18 KiB
PHP
<?php
|
|
|
|
namespace Tests\Feature\Pages;
|
|
|
|
use App\Models\Grant;
|
|
use App\Models\Role;
|
|
use App\Models\User;
|
|
use App\Notifications\PortalNotification;
|
|
use App\Pagebuilder\Content;
|
|
use App\Support\Permissions\Audiences;
|
|
use App\Support\Permissions\Permissions;
|
|
use Illuminate\Foundation\Testing\RefreshDatabase;
|
|
use Illuminate\Http\UploadedFile;
|
|
use Illuminate\Support\Facades\Notification;
|
|
use Illuminate\Support\Facades\Storage;
|
|
use Inertia\Testing\AssertableInertia as Assert;
|
|
use Inhaus\Plugins\Pages\Models\Page;
|
|
use Inhaus\Plugins\Pages\Models\PageFile;
|
|
use Inhaus\Plugins\Pages\PageTree;
|
|
use Tests\TestCase;
|
|
|
|
class PagesTest extends TestCase
|
|
{
|
|
use RefreshDatabase;
|
|
|
|
private User $admin;
|
|
|
|
protected function setUp(): void
|
|
{
|
|
parent::setUp();
|
|
|
|
Storage::fake('uploads');
|
|
Storage::fake('local');
|
|
$this->admin = User::factory()->admin()->create();
|
|
}
|
|
|
|
private function content(string $heading = 'Hallo'): array
|
|
{
|
|
return ['blocks' => [['id' => 'blk00001', 'type' => 'heading', 'data' => ['text' => $heading]]]];
|
|
}
|
|
|
|
private function publishedPage(string $title, ?Page $parent = null, string $heading = 'Hallo'): Page
|
|
{
|
|
$page = Page::query()->create(['title' => $title, 'parent_id' => $parent?->id]);
|
|
$page->publish($title, app(Content::class)->sanitize($this->content($heading)), $this->admin);
|
|
|
|
return $page->fresh();
|
|
}
|
|
|
|
private function mavRole(Page $page, bool $publish = true, bool $create = false): Role
|
|
{
|
|
$role = Role::query()->create(['name' => 'MAV']);
|
|
$role->grants()->create(['ability' => 'pages.edit', 'resource_type' => 'page', 'resource_id' => $page->id, 'include_children' => true]);
|
|
|
|
if ($create) {
|
|
$role->grants()->create(['ability' => 'pages.create', 'resource_type' => 'page', 'resource_id' => $page->id, 'include_children' => true]);
|
|
}
|
|
|
|
if ($publish) {
|
|
$role->grants()->create(['ability' => 'pages.publish', 'resource_type' => 'page', 'resource_id' => $page->id, 'include_children' => true]);
|
|
}
|
|
|
|
return $role;
|
|
}
|
|
|
|
public function test_draft_publish_and_versions(): void
|
|
{
|
|
$this->actingAs($this->admin)->post('/redaktion/seiten', ['title' => 'Arbeitsschutz'])->assertRedirect();
|
|
$page = Page::query()->sole();
|
|
$this->assertSame('arbeitsschutz', $page->path);
|
|
|
|
$this->get('/redaktion/seiten/'.$page->id)->assertOk()->assertInertia(fn (Assert $p) => $p
|
|
->component('pages::Editor')
|
|
->where('working.title', 'Arbeitsschutz')
|
|
->has('blockTypes')
|
|
->where('page.can.publish', true));
|
|
|
|
$this->putJson('/redaktion/seiten/'.$page->id.'/entwurf', ['title' => 'Arbeitsschutz', 'content' => $this->content('Entwurf')])
|
|
->assertOk()->assertJsonPath('page.has_draft', true);
|
|
|
|
$employee = User::factory()->create();
|
|
$this->actingAs($employee)->get('/seiten/arbeitsschutz')->assertNotFound();
|
|
|
|
$this->actingAs($this->admin)->postJson('/redaktion/seiten/'.$page->id.'/veroeffentlichen', ['title' => 'Arbeitsschutz', 'content' => $this->content('Version 1')])
|
|
->assertOk()->assertJsonPath('page.is_published', true)->assertJsonPath('page.has_draft', false);
|
|
|
|
$this->actingAs($employee)->get('/seiten/arbeitsschutz')->assertOk()->assertInertia(fn (Assert $p) => $p
|
|
->component('pages::Show')
|
|
->where('content.blocks.0.data.text', 'Version 1')
|
|
->where('content.blocks.0.component', 'Heading')
|
|
->where('editor', null));
|
|
|
|
// Neuer Entwurf ändert die veröffentlichte Seite nicht.
|
|
$this->actingAs($this->admin)->putJson('/redaktion/seiten/'.$page->id.'/entwurf', ['title' => 'Arbeitsschutz', 'content' => $this->content('Version 2')]);
|
|
$this->actingAs($employee)->get('/seiten/arbeitsschutz')->assertInertia(fn (Assert $p) => $p->where('content.blocks.0.data.text', 'Version 1'));
|
|
|
|
// Unveränderter Stand = kein Entwurf.
|
|
$this->actingAs($this->admin)->putJson('/redaktion/seiten/'.$page->id.'/entwurf', ['title' => 'Arbeitsschutz', 'content' => $this->content('Version 1')])
|
|
->assertJsonPath('page.has_draft', false);
|
|
|
|
$this->postJson('/redaktion/seiten/'.$page->id.'/veroeffentlichen', ['title' => 'Arbeitsschutz neu', 'content' => $this->content('Version 2')]);
|
|
$revisions = $this->getJson('/redaktion/seiten/'.$page->id.'/versionen')->assertOk()->json('revisions');
|
|
$this->assertCount(2, $revisions);
|
|
|
|
$first = end($revisions);
|
|
$this->postJson('/redaktion/seiten/'.$page->id.'/versionen/'.$first['id'].'/wiederherstellen')
|
|
->assertOk()->assertJsonPath('working.content.blocks.0.data.text', 'Version 1')->assertJsonPath('working.title', 'Arbeitsschutz');
|
|
$this->assertSame('Arbeitsschutz neu', $page->fresh()->title, 'Wiederherstellen erzeugt nur einen Entwurf.');
|
|
}
|
|
|
|
public function test_mav_edits_its_page_and_all_subpages_but_nothing_else(): void
|
|
{
|
|
$mavPage = $this->publishedPage('MAV');
|
|
$other = $this->publishedPage('Geschäftsführung');
|
|
$mav = User::factory()->withRoles([$this->mavRole($mavPage, create: true)])->create();
|
|
|
|
$this->actingAs($mav)->post('/redaktion/seiten', ['title' => 'Wahlen', 'parent_id' => $mavPage->id])->assertRedirect();
|
|
$child = Page::query()->where('title', 'Wahlen')->sole();
|
|
$this->assertSame('mav/wahlen', $child->path);
|
|
|
|
// Neue Unterseite ist sofort mit abgedeckt (Rechte-Cache hängt am Seitenbaum).
|
|
$this->get('/redaktion/seiten/'.$child->id)->assertOk();
|
|
$this->postJson('/redaktion/seiten/'.$child->id.'/veroeffentlichen', ['title' => 'Wahlen', 'content' => $this->content()])->assertOk();
|
|
|
|
$this->get('/redaktion/seiten/'.$other->id)->assertForbidden();
|
|
$this->putJson('/redaktion/seiten/'.$other->id.'/entwurf', ['title' => 'x', 'content' => $this->content()])->assertForbidden();
|
|
$this->post('/redaktion/seiten', ['title' => 'Oberste Ebene'])->assertForbidden();
|
|
|
|
$this->get('/redaktion')->assertOk()->assertInertia(fn (Assert $p) => $p
|
|
->where('sections.0.key', 'pages')
|
|
->where('sections.0.props.pages', fn ($pages) => collect($pages)->pluck('title')->sort()->values()->all() === ['MAV', 'Wahlen']));
|
|
|
|
$this->get('/seiten/mav')->assertInertia(fn (Assert $p) => $p->where('editor.url', route('pages.editor', $mavPage)));
|
|
$this->get('/seiten/geschaeftsfuehrung')->assertInertia(fn (Assert $p) => $p->where('editor', null));
|
|
}
|
|
|
|
public function test_without_publish_right_a_review_is_requested(): void
|
|
{
|
|
Notification::fake();
|
|
$page = $this->publishedPage('MAV');
|
|
$author = User::factory()->withRoles([$this->mavRole($page, publish: false)])->create();
|
|
|
|
$publisherRole = Role::query()->create(['name' => 'Redaktion']);
|
|
$publisherRole->grants()->create(['ability' => 'pages.publish', 'resource_type' => 'page', 'resource_id' => $page->id]);
|
|
$publisher = User::factory()->withRoles([$publisherRole])->create();
|
|
$unrelated = User::factory()->create();
|
|
|
|
$this->actingAs($author)->get('/redaktion/seiten/'.$page->id)->assertInertia(fn (Assert $p) => $p->where('page.can.publish', false));
|
|
$this->postJson('/redaktion/seiten/'.$page->id.'/veroeffentlichen', ['title' => 'MAV', 'content' => $this->content()])->assertForbidden();
|
|
|
|
$this->postJson('/redaktion/seiten/'.$page->id.'/freigabe', ['message' => 'Bitte bis Freitag'])->assertOk()->assertJsonPath('recipients', 2);
|
|
|
|
Notification::assertSentTo([$publisher, $this->admin], PortalNotification::class, fn ($n) => str_contains($n->body, 'Bitte bis Freitag'));
|
|
Notification::assertNotSentTo([$author, $unrelated], PortalNotification::class);
|
|
|
|
$this->postJson('/redaktion/seiten/'.$page->id.'/freigabe')->assertStatus(429);
|
|
}
|
|
|
|
public function test_visibility_is_inherited_by_subpages(): void
|
|
{
|
|
$care = $this->publishedPage('Pflege');
|
|
$child = $this->publishedPage('Dienstpläne', $care);
|
|
Audiences::sync('page', $care->id, ['department' => ['Pflege']]);
|
|
PageTree::flush();
|
|
|
|
$nurse = User::factory()->create(['department' => 'Pflege']);
|
|
$clerk = User::factory()->create(['department' => 'Verwaltung']);
|
|
|
|
$this->actingAs($nurse)->get('/seiten/pflege/dienstplaene')->assertOk();
|
|
$this->actingAs($clerk)->get('/seiten/pflege/dienstplaene')->assertNotFound();
|
|
$this->actingAs($clerk)->get('/seiten')->assertInertia(fn (Assert $p) => $p->where('tree', []));
|
|
$this->actingAs($nurse)->get('/seiten')->assertInertia(fn (Assert $p) => $p->where('tree.0.children.0.id', $child->id));
|
|
}
|
|
|
|
public function test_edit_lock_prevents_concurrent_changes(): void
|
|
{
|
|
$page = $this->publishedPage('Handbuch');
|
|
$colleague = User::factory()->admin()->create(['first_name' => 'Kim', 'last_name' => 'Kollegin']);
|
|
|
|
$this->actingAs($this->admin)->get('/redaktion/seiten/'.$page->id)->assertInertia(fn (Assert $p) => $p->where('lockedBy', null));
|
|
|
|
$this->actingAs($colleague)->get('/redaktion/seiten/'.$page->id)->assertInertia(fn (Assert $p) => $p->where('lockedBy.id', $this->admin->id));
|
|
$this->putJson('/redaktion/seiten/'.$page->id.'/entwurf', ['title' => 'x', 'content' => $this->content()])->assertStatus(423);
|
|
$this->postJson('/redaktion/seiten/'.$page->id.'/sperre')->assertStatus(423);
|
|
|
|
$this->postJson('/redaktion/seiten/'.$page->id.'/sperre', ['takeover' => true])->assertOk();
|
|
$this->putJson('/redaktion/seiten/'.$page->id.'/entwurf', ['title' => 'Handbuch', 'content' => $this->content('Kim')])->assertOk();
|
|
|
|
$this->actingAs($this->admin)->putJson('/redaktion/seiten/'.$page->id.'/entwurf', ['title' => 'x', 'content' => $this->content()])
|
|
->assertStatus(423)->assertJsonPath('lockedBy.name', 'Kim Kollegin');
|
|
|
|
// Ohne Lebenszeichen läuft die Sperre ab.
|
|
$this->travel(Page::LOCK_SECONDS + 1)->seconds();
|
|
$this->putJson('/redaktion/seiten/'.$page->id.'/entwurf', ['title' => 'Handbuch', 'content' => $this->content()])->assertOk();
|
|
}
|
|
|
|
public function test_scheduled_publishing(): void
|
|
{
|
|
$page = $this->publishedPage('Sommerfest', heading: 'Alt');
|
|
$employee = User::factory()->create();
|
|
|
|
$this->actingAs($this->admin)->postJson('/redaktion/seiten/'.$page->id.'/veroeffentlichen', [
|
|
'title' => 'Sommerfest', 'content' => $this->content('Neu'), 'scheduled_at' => now()->addDay()->toIso8601String(),
|
|
])->assertOk()->assertJsonPath('page.scheduled_at', fn ($v) => $v !== null);
|
|
|
|
$this->actingAs($employee)->get('/seiten/sommerfest')->assertInertia(fn (Assert $p) => $p->where('content.blocks.0.data.text', 'Alt'));
|
|
|
|
$this->travel(25)->hours();
|
|
$this->artisan('schedule:run');
|
|
|
|
$this->actingAs($employee)->get('/seiten/sommerfest')->assertInertia(fn (Assert $p) => $p->where('content.blocks.0.data.text', 'Neu'));
|
|
$this->assertNull($page->fresh()->scheduled_at);
|
|
$this->assertSame('scheduled', $page->revisions()->first()->action);
|
|
|
|
$this->actingAs($this->admin)->postJson('/redaktion/seiten/'.$page->id.'/veroeffentlichen', [
|
|
'title' => 'x', 'content' => $this->content(), 'scheduled_at' => now()->subHour()->toIso8601String(),
|
|
])->assertUnprocessable();
|
|
}
|
|
|
|
public function test_downloads_are_only_served_to_people_who_see_the_page(): void
|
|
{
|
|
$page = $this->publishedPage('Formulare');
|
|
Audiences::sync('page', $page->id, ['department' => ['Pflege']]);
|
|
PageTree::flush();
|
|
|
|
$this->actingAs($this->admin)->postJson('/redaktion/seiten/'.$page->id.'/dateien', ['file' => UploadedFile::fake()->create('shell.php', 1)])->assertUnprocessable();
|
|
$file = $this->postJson('/redaktion/seiten/'.$page->id.'/dateien', ['file' => UploadedFile::fake()->create('Urlaubsantrag 2027.pdf', 120, 'application/pdf')])
|
|
->assertOk()->json('file');
|
|
|
|
$this->assertSame('Urlaubsantrag 2027.pdf', $file['name']);
|
|
$stored = PageFile::query()->sole();
|
|
Storage::disk('local')->assertExists($stored->path);
|
|
$this->assertStringNotContainsString('Urlaubsantrag', $stored->path);
|
|
|
|
$this->postJson('/redaktion/seiten/'.$page->id.'/veroeffentlichen', ['title' => 'Formulare', 'content' => ['blocks' => [['type' => 'downloads', 'data' => ['files' => [$stored->id, 999]]]]]]);
|
|
|
|
$nurse = User::factory()->create(['department' => 'Pflege']);
|
|
$this->actingAs($nurse)->get('/seiten/formulare')->assertInertia(fn (Assert $p) => $p->where('content.blocks.0.props.files.0.name', 'Urlaubsantrag 2027.pdf')->has('content.blocks.0.props.files', 1));
|
|
$this->get($file['url'])->assertOk()->assertDownload('Urlaubsantrag 2027.pdf');
|
|
|
|
$this->actingAs(User::factory()->create(['department' => 'Küche']))->get($file['url'])->assertNotFound();
|
|
}
|
|
|
|
public function test_images_are_stored_as_webp(): void
|
|
{
|
|
$page = $this->publishedPage('Galerie');
|
|
|
|
$result = $this->actingAs($this->admin)->postJson('/redaktion/seiten/'.$page->id.'/bilder', ['image' => UploadedFile::fake()->image('foto.jpg', 3000, 2000)])
|
|
->assertOk()->json();
|
|
|
|
$this->assertStringStartsWith('pagebuilder/pages/'.$page->id.'/', $result['path']);
|
|
$this->assertStringEndsWith('.webp', $result['path']);
|
|
Storage::disk('uploads')->assertExists($result['path']);
|
|
|
|
$props = $this->postJson('/redaktion/seiten/'.$page->id.'/vorschau', ['blocks' => [['id' => 'img00001', 'type' => 'image', 'data' => ['path' => $result['path']]]]])
|
|
->assertOk()->json('props.img00001');
|
|
$this->assertStringContainsString($result['path'].'?v=', $props['url']);
|
|
}
|
|
|
|
public function test_top_level_pages_can_appear_in_the_main_navigation(): void
|
|
{
|
|
$page = $this->publishedPage('MAV');
|
|
$page->update(['in_navigation' => true]);
|
|
$restricted = $this->publishedPage('Leitung');
|
|
$restricted->update(['in_navigation' => true]);
|
|
Audiences::sync('page', $restricted->id, ['department' => ['Leitung']]);
|
|
PageTree::flush();
|
|
|
|
$this->actingAs(User::factory()->create())->get('/seiten/mav')->assertInertia(fn (Assert $p) => $p
|
|
->where('navigation', fn ($items) => collect($items)->contains(fn ($i) => $i['label'] === 'MAV' && $i['active'])
|
|
&& ! collect($items)->contains('label', 'Leitung')
|
|
&& ! collect($items)->contains('key', 'editorial')));
|
|
}
|
|
|
|
public function test_changing_the_address_moves_all_subpages(): void
|
|
{
|
|
$parent = $this->publishedPage('MAV');
|
|
$child = $this->publishedPage('Wahlen', $parent);
|
|
$grandchild = $this->publishedPage('Ergebnisse', $child);
|
|
|
|
$this->actingAs($this->admin)->putJson('/redaktion/seiten/'.$parent->id.'/einstellungen', ['slug' => 'mitarbeitervertretung'])->assertOk();
|
|
|
|
$this->assertSame('mitarbeitervertretung/wahlen', $child->fresh()->path);
|
|
$this->assertSame('mitarbeitervertretung/wahlen/ergebnisse', $grandchild->fresh()->path);
|
|
$this->get('/seiten/mitarbeitervertretung/wahlen/ergebnisse')->assertOk();
|
|
|
|
$this->putJson('/redaktion/seiten/'.$parent->id.'/einstellungen', ['slug' => 'Mit Leerzeichen'])->assertUnprocessable();
|
|
$this->deleteJson('/redaktion/seiten/'.$parent->id)->assertUnprocessable();
|
|
$this->deleteJson('/redaktion/seiten/'.$grandchild->id)->assertOk()->assertJsonPath('redirect', '/seiten/mitarbeitervertretung/wahlen');
|
|
}
|
|
|
|
public function test_notifications_can_be_read_and_opened(): void
|
|
{
|
|
$this->admin->notify(new PortalNotification('Freigabe angefordert', 'Bitte prüfen', '/seiten/mav'));
|
|
$this->admin->notify(new PortalNotification('Fremder Link', '', 'https://evil.example'));
|
|
|
|
$this->actingAs($this->admin)->get('/')->assertInertia(fn (Assert $p) => $p->where('notifications.unread', 2));
|
|
$items = $this->getJson('/benachrichtigungen')->assertOk()->assertJsonPath('unread', 2)->json('notifications');
|
|
|
|
$mav = collect($items)->firstWhere('title', 'Freigabe angefordert');
|
|
$this->get('/benachrichtigungen/'.$mav['id'])->assertRedirect('/seiten/mav');
|
|
$this->get('/benachrichtigungen/'.collect($items)->firstWhere('title', 'Fremder Link')['id'])->assertRedirect('/');
|
|
|
|
$this->postJson('/benachrichtigungen/gelesen')->assertOk();
|
|
$this->assertSame(0, $this->admin->unreadNotifications()->count());
|
|
|
|
$this->actingAs(User::factory()->create())->get('/benachrichtigungen/'.$mav['id'])->assertNotFound();
|
|
}
|
|
|
|
public function test_role_form_offers_including_subpages(): void
|
|
{
|
|
$page = $this->publishedPage('MAV');
|
|
$child = $this->publishedPage('Wahlen', $page);
|
|
$role = Role::query()->create(['name' => 'MAV']);
|
|
$role->grants()->create(['ability' => 'pages.edit', 'resource_type' => 'page', 'resource_id' => $page->id, 'include_children' => false]);
|
|
$user = User::factory()->withRoles([$role])->create();
|
|
|
|
$this->assertTrue($user->can('update', $page));
|
|
$this->assertFalse($user->can('update', $child), 'Ohne „inkl. Unterseiten“ nur die Seite selbst.');
|
|
|
|
Grant::query()->update(['include_children' => true]);
|
|
Permissions::flush();
|
|
|
|
$this->assertTrue($user->fresh()->can('update', $child));
|
|
}
|
|
|
|
public function test_creating_subpages_is_a_separate_optional_right(): void
|
|
{
|
|
$mavPage = $this->publishedPage('MAV');
|
|
$role = $this->mavRole($mavPage);
|
|
$member = User::factory()->withRoles([$role])->create();
|
|
|
|
// Nur bearbeiten/veröffentlichen: keine Unterseiten
|
|
$this->actingAs($member)->post('/redaktion/seiten', ['title' => 'Wahlen', 'parent_id' => $mavPage->id])->assertForbidden();
|
|
$this->get('/redaktion/seiten/'.$mavPage->id)->assertInertia(fn (Assert $p) => $p->where('page.can.create_child', false));
|
|
|
|
$role->grants()->create(['ability' => 'pages.create', 'resource_type' => 'page', 'resource_id' => $mavPage->id, 'include_children' => true]);
|
|
Permissions::flush();
|
|
|
|
$this->post('/redaktion/seiten', ['title' => 'Wahlen', 'parent_id' => $mavPage->id])->assertRedirect();
|
|
$child = Page::query()->where('title', 'Wahlen')->sole();
|
|
$this->assertSame($mavPage->id, $child->parent_id);
|
|
$this->assertTrue($member->fresh()->can('update', $child));
|
|
|
|
// Oberste Ebene bleibt „Seiten verwalten“ vorbehalten
|
|
$this->post('/redaktion/seiten', ['title' => 'Eigenes'])->assertForbidden();
|
|
}
|
|
}
|