content()->sanitize(['blocks' => [ ['id' => 'abc12345', 'type' => 'heading', 'data' => ['text' => ' Willkommen ', 'level' => '9', 'evil' => 'x']], ['type' => 'unbekannt', 'data' => []], 'kein Array', ]]); $this->assertCount(1, $clean['blocks']); $this->assertSame(['id' => 'abc12345', 'type' => 'heading', 'data' => ['text' => 'Willkommen', 'level' => '2', 'align' => 'left']], $clean['blocks'][0]); } public function test_html_and_links_are_made_safe(): void { $clean = $this->content()->sanitize(['blocks' => [ ['type' => 'text', 'data' => ['html' => '

Hallo Link fett

']], ['type' => 'buttons', 'data' => ['buttons' => [ ['label' => 'Böse', 'url' => 'javascript:alert(1)'], ['label' => 'Intern', 'url' => '/seiten/mav'], ['label' => 'Ohne Schema', 'url' => 'www.example.org/x'], ]]], ['type' => 'image', 'data' => ['path' => '../../.env']], ]]); $html = $clean['blocks'][0]['data']['html']; $this->assertStringNotContainsString('script', $html); $this->assertStringNotContainsString('onclick', $html); $this->assertStringNotContainsString('javascript:', $html); $this->assertStringNotContainsString('iframe', $html); $this->assertStringContainsString('fett', $html); $this->assertSame(['', '/seiten/mav', 'https://www.example.org/x'], array_column($clean['blocks'][1]['data']['buttons'], 'url')); $this->assertNull($clean['blocks'][2]['data']['path']); } public function test_columns_follow_the_layout_without_losing_content(): void { $text = fn (string $t) => ['type' => 'heading', 'data' => ['text' => $t]]; $clean = $this->content()->sanitize(['blocks' => [[ 'type' => 'columns', 'data' => ['layout' => '1-1'], 'items' => [['blocks' => [$text('A')]], ['blocks' => [$text('B')]], ['blocks' => [$text('C')]]], ]]]); $columns = $clean['blocks'][0]['items']; $this->assertCount(2, $columns); $this->assertSame(['B', 'C'], array_map(fn ($b) => $b['data']['text'], $columns[1]['blocks'])); $grown = $this->content()->sanitize(['blocks' => [['type' => 'columns', 'data' => ['layout' => '1-1-1-1'], 'items' => []]]]); $this->assertCount(4, $grown['blocks'][0]['items']); } public function test_nesting_depth_is_limited(): void { $block = ['type' => 'heading', 'data' => ['text' => 'tief']]; for ($i = 0; $i < 10; $i++) { $block = ['type' => 'section', 'items' => [['blocks' => [$block]]]]; } $clean = $this->content()->sanitize(['blocks' => [$block]]); $depth = 0; $current = $clean['blocks']; while ($current !== []) { $depth++; $current = $current[0]['items'][0]['blocks'] ?? []; } $this->assertSame(Content::MAX_DEPTH, $depth); } public function test_blocks_can_be_limited_to_audiences(): void { $mav = Role::query()->create(['name' => 'MAV']); $member = User::factory()->withRoles([$mav])->create(); $nurse = User::factory()->create(['department' => 'Pflege']); $other = User::factory()->create(['department' => 'Verwaltung']); $content = $this->content()->sanitize(['blocks' => [ ['type' => 'heading', 'data' => ['text' => 'Für alle']], ['type' => 'heading', 'data' => ['text' => 'Nur MAV oder Pflege'], 'visibility' => ['role' => [(string) $mav->id], 'department' => ['Pflege']]], ]]); $texts = fn (User $user) => array_map(fn ($b) => $b['data']['text'], $this->content()->render($content, $user)['blocks']); $this->assertSame(['Für alle', 'Nur MAV oder Pflege'], $texts($member)); $this->assertSame(['Für alle', 'Nur MAV oder Pflege'], $texts($nurse)); $this->assertSame(['Für alle'], $texts($other)); $this->assertArrayNotHasKey('visibility', $this->content()->render($content, $member)['blocks'][1], 'Zielgruppen gehen nicht an den Browser.'); } public function test_a_failing_plugin_block_does_not_break_the_page(): void { app(Blocks::class)->add(BlockType::make('kaputt', 'Kaputt', 'box', 'dynamic') ->fields([Field::text('title', 'Titel')]) ->resolveUsing(fn () => throw new RuntimeException('Fehler im Plugin'))); $content = $this->content()->sanitize(['blocks' => [ ['type' => 'kaputt', 'data' => ['title' => 'x']], ['type' => 'heading', 'data' => ['text' => 'Bleibt']], ]]); $rendered = $this->content()->render($content, User::factory()->create()); $this->assertSame(['heading'], array_column($rendered['blocks'], 'type')); } public function test_video_urls_become_privacy_friendly_embeds(): void { $content = $this->content()->sanitize(['blocks' => [ ['type' => 'video', 'data' => ['url' => 'https://www.youtube.com/watch?v=dQw4w9WgXcQ']], ['type' => 'video', 'data' => ['url' => 'https://vimeo.com/76979871']], ['type' => 'video', 'data' => ['url' => 'https://example.org/irgendwas']], ]]); $embeds = array_map(fn ($b) => $b['props']['embed'], $this->content()->render($content, null)['blocks']); $this->assertSame('https://www.youtube-nocookie.com/embed/dQw4w9WgXcQ?rel=0', $embeds[0]['src']); $this->assertSame('Vimeo', $embeds[1]['provider']); $this->assertNull($embeds[2]); } public function test_plain_text_is_extracted_for_search(): void { $content = $this->content()->sanitize(['blocks' => [ ['type' => 'tabs', 'items' => [['title' => 'Wahl', 'blocks' => [['type' => 'text', 'data' => ['html' => '

Die MAV-Wahl findet statt.

']]]]]], ]]); $this->assertStringContainsString('Wahl Die MAV-Wahl findet statt.', $this->content()->plainText($content)); } }