Loheide-eu-mitarbeiterplatt.../tests/Feature/DashboardCustomizationTest.php
Friederich Loheide c495dfa1cb
Some checks failed
CI / PHP 8.4 / mariadb:10.6 (pull_request) Successful in 3m29s
CI / PHP 8.3 / mariadb:10.6 (pull_request) Successful in 3m31s
CI / PHP 8.4 / mariadb:11.4 (pull_request) Successful in 3m56s
CI / PHP 8.3 / mariadb:11.4 (pull_request) Successful in 4m2s
CI / PHP 8.3 / mysql:8.0 (pull_request) Failing after 5m20s
CI / PHP 8.4 / mysql:8.0 (pull_request) Failing after 5m22s
CI / PHP 8.3 / mysql:8.4 (pull_request) Failing after 4m42s
CI / PHP 8.4 / mysql:8.4 (pull_request) Failing after 4m41s
CI / Build-Artefakte aktuell (pull_request) Successful in 1m18s
Backend: eigenes kompaktes Theme, Übersicht, Cluster mit Unterseiten, Plugin-Liste
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
2026-09-26 20:50:17 +00:00

118 lines
5.7 KiB
PHP

<?php
namespace Tests\Feature;
use App\Dashboard\Widgets;
use App\Filament\Pages\DashboardSettings;
use App\Filament\Resources\DashboardTiles\Pages\ManageDashboardTiles;
use App\Models\DashboardTile;
use App\Models\User;
use App\Support\Permissions\Audiences;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Http\UploadedFile;
use Illuminate\Support\Facades\Storage;
use Livewire\Livewire;
use Tests\TestCase;
class DashboardCustomizationTest extends TestCase
{
use RefreshDatabase;
private function keys(User $user, string $area): array
{
$widgets = $this->actingAs($user)->get('/')->viewData('page')['props']['widgets'];
return array_values(array_column(array_filter($widgets, fn ($w) => $w['area'] === $area), 'key'));
}
public function test_admin_adds_an_image_tile_with_link(): void
{
Storage::fake('uploads');
$admin = User::factory()->admin()->create();
$this->actingAs($admin);
Livewire::test(ManageDashboardTiles::class)
->callAction('create', [
'name' => 'Betriebsrat-Banner',
'type' => 'image',
'area' => 'aside',
'image_path' => UploadedFile::fake()->image('banner.jpg', 800, 600),
'url' => 'intranet.example.org/aktion',
'alt' => 'Aktion Gesundheit',
'is_active' => true,
])
->assertHasNoActionErrors();
$tile = DashboardTile::query()->sole();
$this->assertSame('https://intranet.example.org/aktion', $tile->url);
$props = $this->dashboardWidget($this->get('/'), $tile->widgetKey());
$this->assertSame('image', $props['tile']['type']);
$this->assertSame('Aktion Gesundheit', $props['tile']['alt']);
$this->assertNotNull($props['tile']['image_url']);
}
public function test_tiles_respect_period_and_audience(): void
{
$nurse = User::factory()->create(['department' => 'Pflege']);
$clerk = User::factory()->create(['department' => 'Verwaltung']);
$notice = DashboardTile::query()->create(['name' => 'Hinweis', 'type' => 'notice', 'area' => 'top', 'title' => 'Heute Betriebsversammlung', 'body' => '<p>14 Uhr <script>x</script></p>', 'variant' => 'info']);
$expired = DashboardTile::query()->create(['name' => 'Alt', 'type' => 'text', 'area' => 'aside', 'body' => 'x', 'ends_at' => now()->subDay()]);
$inactive = DashboardTile::query()->create(['name' => 'Aus', 'type' => 'text', 'area' => 'aside', 'body' => 'x', 'is_active' => false]);
Audiences::sync('dashboard_tile', $notice->id, ['department' => ['Pflege']]);
$this->assertSame(['tile.'.$notice->id], $this->keys($nurse, 'top'));
$this->assertSame([], $this->keys($clerk, 'top'));
$this->assertNotContains($expired->widgetKey(), $this->keys($nurse, 'aside'));
$this->assertNotContains($inactive->widgetKey(), $this->keys($nurse, 'aside'));
$this->assertStringNotContainsString('script', $notice->fresh()->body_html);
}
public function test_admin_reorders_and_hides_tiles(): void
{
$admin = User::factory()->admin()->create();
$tile = DashboardTile::query()->create(['name' => 'Banner', 'type' => 'text', 'area' => 'aside', 'body' => 'Hallo']);
$default = $this->keys($admin, 'aside');
$this->assertSame('core.greeting', $default[0]);
$this->assertSame($tile->widgetKey(), end($default), 'Neue eigene Kacheln stehen zunächst am Ende.');
$page = Livewire::actingAs($admin)->test(DashboardSettings::class);
$aside = collect($page->get('data.aside'))->values();
$this->assertContains('Eigene Kachel: Banner', $aside->pluck('label'));
// Eigene Kachel nach oben, „Personen finden“ ausblenden
$reordered = $aside->sortBy(fn ($item) => $item['key'] === $tile->widgetKey() ? 0 : 1)->values()
->map(fn ($item) => $item['key'] === 'core.people-search' ? [...$item, 'visible' => false] : $item)->all();
$page->set('data.aside', $reordered)->call('save')->assertHasNoErrors();
$keys = $this->keys($admin, 'aside');
$this->assertSame($tile->widgetKey(), $keys[0]);
$this->assertNotContains('core.people-search', $keys);
$this->actingAs(User::factory()->create())->get('/admin/startseite-menue/startseite')->assertForbidden();
}
public function test_admin_moves_quick_links_to_sidebar(): void
{
$admin = User::factory()->admin()->create();
$this->assertContains('core.quick-links', $this->keys($admin, 'main'));
$page = Livewire::actingAs($admin)->test(DashboardSettings::class);
$main = collect($page->get('data.main'))->values()
->map(fn ($item) => $item['key'] === 'core.quick-links' ? [...$item, 'area' => 'aside'] : $item)->all();
$page->set('data.main', $main)->call('save')->assertHasNoErrors();
$this->assertNotContains('core.quick-links', $this->keys($admin, 'main'));
$this->assertContains('core.quick-links', $this->keys($admin, 'aside'));
$this->assertContains('core.quick-links', collect($page->get('data.aside'))->pluck('key'), 'Formular zeigt die Kachel im neuen Bereich');
// Nicht verschiebbare Kacheln bleiben, wo sie hingehören
$this->assertSame(['main', 'aside'], collect(app(Widgets::class)->forAdmin('aside'))->firstWhere('key', 'core.quick-links')['areas']);
$aside = collect($page->get('data.aside'))->values()
->map(fn ($item) => $item['key'] === 'core.greeting' ? [...$item, 'area' => 'main'] : $item)->all();
$page->set('data.aside', $aside)->call('save');
$this->assertContains('core.greeting', $this->keys($admin, 'aside'));
}
}