create(['title' => $title, 'parent_id' => $parent?->id]); $page->publish($title, app(Content::class)->sanitize(['blocks' => []]), User::factory()->admin()->create()); return $page->fresh(); } private function menuFor(User $user, string $url = '/'): array { $menu = null; $this->actingAs($user)->get($url)->assertInertia(function (Assert $page) use (&$menu) { $menu = $page->toArray()['props']['navigation']; }); return $menu; } public function test_without_a_saved_menu_the_automatic_menu_applies(): void { $keys = array_column($this->menuFor(User::factory()->create()), 'key'); $this->assertSame('home', $keys[0]); $this->assertContains('news', $keys); } public function test_custom_menu_with_pages_subpages_groups_links_and_roles(): void { $mav = $this->page('Mitarbeitervertretung'); $this->page('Sprechstunden', $mav); $this->page('Wahlen', $mav); $hr = Role::query()->create(['name' => 'Personal']); app(Settings::class)->set(Navigation::MENU_SETTING, [ ['id' => 'a', 'type' => 'module', 'module' => 'home'], ['id' => 'b', 'type' => 'page', 'target' => (string) $mav->id, 'subpages' => true, 'icon' => 'heart-handshake'], ['id' => 'c', 'type' => 'group', 'label' => 'Service', 'children' => [ ['id' => 'c1', 'type' => 'module', 'module' => 'news', 'label' => 'Aktuelles', 'icon' => 'gibt-es-nicht'], ['id' => 'c2', 'type' => 'link', 'url' => 'https://kantine.example.org', 'label' => 'Speiseplan', 'new_tab' => true, 'icon' => 'soup'], ['id' => 'c3', 'type' => 'link', 'url' => 'javascript:alert(1)', 'label' => 'Böse'], ]], ['id' => 'd', 'type' => 'group', 'label' => 'Nur Personal', 'roles' => [$hr->id], 'children' => [ ['id' => 'd1', 'type' => 'link', 'url' => '/personen', 'label' => 'Verzeichnis'], ]], ['id' => 'e', 'type' => 'group', 'label' => 'Leer', 'children' => []], ['id' => 'f', 'type' => 'module', 'module' => 'gibt-es-nicht'], ]); $menu = $this->menuFor(User::factory()->create(), '/seiten/mitarbeitervertretung/wahlen'); $this->assertSame(['Startseite', 'Mitarbeitervertretung', 'Service'], array_column($menu, 'label')); $this->assertSame(['Sprechstunden', 'Wahlen'], array_column($menu[1]['children'], 'label')); $this->assertTrue($menu[1]['active'], 'Unterseite aktiv → Menüpunkt aktiv'); $this->assertTrue($menu[1]['children'][1]['active']); $this->assertSame(['Aktuelles', 'Speiseplan'], array_column($menu[2]['children'], 'label'), 'Unsichere Links fallen weg'); $this->assertTrue($menu[2]['children'][1]['new_tab']); $this->assertSame('heart-handshake', $menu[1]['icon']); $this->assertNull($menu[2]['children'][0]['icon'], 'Unbekannte Symbole fallen weg'); $this->assertSame('soup', $menu[2]['children'][1]['icon'], 'Auch Symbole außerhalb der kuratierten Auswahl'); $this->assertNull($menu[2]['href']); $withRole = User::factory()->withRoles([$hr])->create(); $this->assertContains('Nur Personal', array_column($this->menuFor($withRole), 'label')); } public function test_deleted_pages_disappear_from_the_menu(): void { $page = $this->page('Alt'); app(Settings::class)->set(Navigation::MENU_SETTING, [['id' => 'a', 'type' => 'page', 'target' => (string) $page->id]]); $page->delete(); app(PageTree::class)->flush(); $this->assertSame([], $this->menuFor(User::factory()->create())); } public function test_admin_builds_the_menu_starting_from_the_automatic_one(): void { $this->actingAs(User::factory()->admin()->create()); $component = Livewire::test(MenuSettings::class); $items = $component->get('data.items'); $this->assertSame('home', collect($items)->first()['module']); $component->set('data.items', [ 'x' => ['id' => 'x', 'type' => 'module', 'module' => 'news', 'label' => 'News', 'icon' => 'newspaper', 'roles' => [], 'children' => [ 'y' => ['id' => 'y', 'type' => 'link', 'url' => '/termine', 'label' => 'Termine', 'roles' => []], ]], ])->call('save')->assertHasNoErrors(); $saved = app(Settings::class)->get(Navigation::MENU_SETTING); $this->assertSame('News', $saved[0]['label']); $this->assertSame('newspaper', $saved[0]['icon']); $this->assertSame('/termine', $saved[0]['children'][0]['url']); $component->set('data.items.x.children.y.url', 'ftp://nope')->call('save')->assertHasErrors(['data.items.x.children.y.url']); $this->assertSame('/termine', app(Settings::class)->get(Navigation::MENU_SETTING)[0]['children'][0]['url'], 'Ungültiges wird nicht gespeichert'); } public function test_menu_page_requires_the_right(): void { $this->actingAs(User::factory()->create())->get('/admin/menue')->assertForbidden(); } }