Loheide-eu-mitarbeiterplatt.../tests/Feature/Pages/SnippetsTest.php
Friederich Loheide 2166a7e11a
Some checks failed
CI / PHP 8.3 / mariadb:10.6 (pull_request) Successful in 5m0s
CI / PHP 8.4 / mariadb:10.6 (pull_request) Successful in 5m0s
CI / PHP 8.3 / mariadb:11.4 (pull_request) Successful in 4m50s
CI / PHP 8.4 / mariadb:11.4 (pull_request) Successful in 4m33s
CI / PHP 8.3 / mysql:8.0 (pull_request) Failing after 6m11s
CI / PHP 8.4 / mysql:8.0 (pull_request) Failing after 6m46s
CI / PHP 8.3 / mysql:8.4 (pull_request) Failing after 6m24s
CI / PHP 8.4 / mysql:8.4 (pull_request) Failing after 5m33s
CI / Build-Artefakte aktuell (pull_request) Successful in 1m17s
Seiten: Bausteine und Vorlagen (0.22.0)
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
2026-09-27 07:48:02 +00:00

105 lines
5.2 KiB
PHP

<?php
namespace Tests\Feature\Pages;
use App\Models\Role;
use App\Models\User;
use App\Pagebuilder\Content;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\Storage;
use Inertia\Testing\AssertableInertia as Assert;
use Inhaus\Plugins\Pages\Models\ContentSnippet;
use Inhaus\Plugins\Pages\Models\Page;
use Tests\TestCase;
class SnippetsTest extends TestCase
{
use RefreshDatabase;
private User $admin;
protected function setUp(): void
{
parent::setUp();
Storage::fake('uploads');
$this->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();
}
}