admin = User::factory()->admin()->create(); } private function snippet(string $kind, string $name, string $heading): int { return $this->actingAs($this->admin)->postJson('/redaktion/bausteine', [ 'kind' => $kind, 'name' => $name, 'content' => ['blocks' => [['id' => 'blk'.$kind, 'type' => 'heading', 'data' => ['text' => $heading]], ['type' => 'gibt-es-nicht']]], ])->assertCreated()->json('snippet.id'); } public function test_snippet_blocks_render_everywhere_and_follow_changes(): void { $id = $this->snippet('block', 'Kontakt Personal', 'Personalabteilung: Durchwahl 200'); $this->assertCount(1, ContentSnippet::query()->find($id)->content['blocks']); // unbekannte Blöcke verworfen $page = Page::query()->create(['title' => 'Urlaub']); $page->publish('Urlaub', app(Content::class)->sanitize(['blocks' => [['type' => 'pages.snippet', 'data' => ['snippet' => (string) $id]]]]), $this->admin); $this->get('/seiten/urlaub')->assertInertia(fn (Assert $p) => $p ->where('content.blocks.0.component', 'pages::Snippet') ->where('content.blocks.0.props.blocks.0.data.text', 'Personalabteilung: Durchwahl 200')); $this->putJson("/redaktion/bausteine/{$id}", ['name' => 'Kontakt Personal', 'content' => ['blocks' => [['type' => 'heading', 'data' => ['text' => 'Neu: Durchwahl 300']]]]])->assertOk(); $this->get('/seiten/urlaub')->assertInertia(fn (Assert $p) => $p->where('content.blocks.0.props.blocks.0.data.text', 'Neu: Durchwahl 300')); // Verwendungsnachweis und Editor $this->get("/redaktion/bausteine/{$id}")->assertOk()->assertInertia(fn (Assert $p) => $p ->component('pages::SnippetEditor')->where('usages.0.title', 'Urlaub') ->where('blockTypes', fn ($types) => ! collect($types)->contains('key', 'pages.snippet'))); // Gelöscht: Block verschwindet von der Seite $this->delete("/redaktion/bausteine/{$id}")->assertRedirect(); $this->get('/seiten/urlaub')->assertInertia(fn (Assert $p) => $p->where('content.blocks', [])); } public function test_snippets_cannot_include_themselves_endlessly(): void { $id = $this->snippet('block', 'Schleife', 'Kopf'); ContentSnippet::query()->whereKey($id)->update(['content' => json_encode(['blocks' => [ ['id' => 'blkloop1', 'type' => 'pages.snippet', 'data' => ['snippet' => (string) $id]], ['id' => 'blkhead1', 'type' => 'heading', 'data' => ['text' => 'Kopf']], ]])]); $page = Page::query()->create(['title' => 'Schleife']); $page->publish('Schleife', app(Content::class)->sanitize(['blocks' => [['type' => 'pages.snippet', 'data' => ['snippet' => (string) $id]]]]), $this->admin); $this->get('/seiten/schleife')->assertOk()->assertInertia(fn (Assert $p) => $p ->where('content.blocks.0.props.blocks', fn ($blocks) => collect($blocks)->pluck('type')->all() === ['heading'])); } public function test_new_pages_start_from_a_template(): void { $template = $this->snippet('template', 'Abteilungsseite', 'Über uns'); $this->getJson('/redaktion/bausteine?kind=template')->assertJsonPath('snippets.0.name', 'Abteilungsseite'); $this->post('/redaktion/seiten', ['title' => 'Einkauf', 'template_id' => $template])->assertRedirect(); $page = Page::query()->where('title', 'Einkauf')->firstOrFail(); $this->assertSame('Über uns', $page->draft['blocks'][0]['data']['text']); $this->assertNull($page->content); // Bausteine sind keine Vorlagen $block = $this->snippet('block', 'Baustein', 'x'); $this->post('/redaktion/seiten', ['title' => 'Falsch', 'template_id' => $block])->assertSessionHasErrors('template_id'); } public function test_only_page_managers_maintain_snippets(): void { $id = $this->snippet('block', 'Kontakt', 'x'); $role = Role::query()->create(['name' => 'Redaktion']); $role->grants()->create(['ability' => 'pages.edit', 'resource_type' => 'page', 'resource_id' => 1, 'include_children' => true]); $editor = User::factory()->withRoles([$role])->create(); $this->actingAs($editor)->postJson('/redaktion/bausteine', ['kind' => 'block', 'name' => 'x', 'content' => ['blocks' => []]])->assertForbidden(); $this->actingAs($editor)->putJson("/redaktion/bausteine/{$id}", ['name' => 'x', 'content' => ['blocks' => []]])->assertForbidden(); $this->actingAs($editor)->delete("/redaktion/bausteine/{$id}")->assertForbidden(); $this->actingAs($editor)->get("/redaktion/bausteine/{$id}")->assertForbidden(); } }