diff --git a/app/Domain/Catalog/ShinkanImport.php b/app/Domain/Catalog/ShinkanImport.php index ac59edf..73769a5 100644 --- a/app/Domain/Catalog/ShinkanImport.php +++ b/app/Domain/Catalog/ShinkanImport.php @@ -37,6 +37,45 @@ public function configured(): bool return filled(config('services.shinkan.url')) && filled(config('services.shinkan.key')); } + /** + * Zustand der Anbindung ohne Werte: Adresse und Schlüssel stehen nur in der Umgebungsdatei des Servers. + * + * @return array{url: bool, key: bool, host: string|null} + */ + public function status(): array + { + $url = (string) config('services.shinkan.url'); + + return [ + 'url' => filled($url), + 'key' => filled(config('services.shinkan.key')), + 'host' => filled($url) ? (parse_url($url, PHP_URL_HOST) ?: null) : null, + ]; + } + + /** + * Verbindung prüfen, ohne etwas zu speichern. Der Katalogabruf legt auf der Seite von Shinkan einen Stand an; hier + * wird nur gezählt, was die Quelle für Lara & Felix bereitstellt. + * + * @return array{ok: bool, message: string} + */ + public function check(): array + { + if (! $this->configured()) { + return ['ok' => false, 'message' => 'Die Anbindung ist nicht eingerichtet. Adresse und Schlüssel fehlen in der Umgebung des Servers.']; + } + + try { + $catalog = $this->get('/catalog'); + } catch (Throwable $exception) { + return ['ok' => false, 'message' => 'Die Verbindung ist fehlgeschlagen: '.$this->plain($exception)]; + } + + $count = count((array) ($catalog['exercises'] ?? [])); + + return ['ok' => true, 'message' => 'Die Verbindung funktioniert. Die Quelle stellt '.$count.($count === 1 ? ' Übung' : ' Übungen').' bereit. Es wurde nichts übernommen.']; + } + public function fetch(): ExerciseImportRun { $run = ExerciseImportRun::query()->create(['status' => 'laeuft', 'started_at' => now()]); diff --git a/app/Filament/Pages/Uebernahme.php b/app/Filament/Pages/Uebernahme.php index f865de2..86130e9 100644 --- a/app/Filament/Pages/Uebernahme.php +++ b/app/Filament/Pages/Uebernahme.php @@ -24,13 +24,13 @@ class Uebernahme extends Page { protected static string|\UnitEnum|null $navigationGroup = 'Inhalte'; - protected static ?string $navigationLabel = 'Übernahme (Shinkan)'; + protected static ?string $navigationLabel = 'Übungen aus Shinkan'; protected static ?int $navigationSort = 6; protected static ?string $slug = 'uebernahme'; - protected static ?string $title = 'Übernahme aus Shinkan'; + protected static ?string $title = 'Übungen aus Shinkan übernehmen'; protected string $view = 'filament.pages.uebernahme'; @@ -64,6 +64,15 @@ public function fetch(): void : (string) $run->error; } + public function check(): void + { + abort_unless(static::canAccess(), 403); + + $result = app(ShinkanImport::class)->check(); + $this->feedbackError = ! $result['ok']; + $this->feedback = $result['message']; + } + public function adopt(int $snapshotId): void { abort_unless(static::canAccess(), 403); @@ -114,6 +123,7 @@ public function daten(): array return [ 'zeilen' => $rows, 'eingerichtet' => app(ShinkanImport::class)->configured(), + 'anbindung' => app(ShinkanImport::class)->status(), 'letzterLauf' => ExerciseImportRun::query()->orderByDesc('id')->first(), ]; } diff --git a/app/Filament/Resources/Compositions/Pages/ListCompositionKind.php b/app/Filament/Resources/Compositions/Pages/ListCompositionKind.php index 8017dbf..bb89d40 100644 --- a/app/Filament/Resources/Compositions/Pages/ListCompositionKind.php +++ b/app/Filament/Resources/Compositions/Pages/ListCompositionKind.php @@ -4,6 +4,7 @@ use App\Domain\Catalog\CompositionKind; use App\Domain\Catalog\ContentService; +use App\Filament\Pages\Uebernahme; use App\Filament\Resources\ContentElements\ContentElementResource; use App\Filament\Support\ListsCompositions; use App\Filament\Support\ReportsDomainRules; @@ -55,6 +56,18 @@ public function content(Schema $schema): Schema protected function getHeaderActions(): array { + if ($this->kind() === CompositionKind::Exercise) { + return [ + $this->createCompositionAction($this->kind()), + Action::make('shinkan') + ->label('Aus Shinkan übernehmen') + ->icon('heroicon-o-arrow-down-tray') + ->color('gray') + ->visible(fn (): bool => ContentElementResource::editor()) + ->url(fn (): string => Uebernahme::getUrl()), + ]; + } + if ($this->onDesk()) { return [$this->createCompositionAction($this->kind())]; } diff --git a/resources/views/filament/pages/uebernahme.blade.php b/resources/views/filament/pages/uebernahme.blade.php index a22d291..b80247a 100644 --- a/resources/views/filament/pages/uebernahme.blade.php +++ b/resources/views/filament/pages/uebernahme.blade.php @@ -1,14 +1,22 @@ @php($daten = $this->daten())
-

Übernahme aus Shinkan

+

Übungen aus Shinkan übernehmen

Shinkan stellt für Lara & Felix ausgewählte Übungen bereit. „Abrufen“ aktualisiert nur den Quellenstand. Erst „Übernehmen“ legt eine unveränderliche lokale Fassung an. Produkte verwenden sie erst, wenn du sie dort einsetzt und das Produkt veröffentlichst.

-

- +

+

Anbindung

+
+
Adresse der Quelle
{{ $daten['anbindung']['url'] ? ($daten['anbindung']['host'] ?? 'eingetragen') : 'nicht eingetragen' }}
+
Zugangsschlüssel
{{ $daten['anbindung']['key'] ? 'hinterlegt (wird nie angezeigt)' : 'nicht hinterlegt' }}
+
@unless ($daten['eingerichtet']) - Die Anbindung ist in dieser Umgebung nicht eingerichtet. +

Die Anbindung ist in dieser Umgebung nicht eingerichtet. Adresse und Schlüssel liegen aus Sicherheitsgründen nur in der Umgebungsdatei des Servers (SHINKAN_DISTRIBUTION_URL und SHINKAN_DISTRIBUTION_KEY). Sie werden nicht in dieser Oberfläche gespeichert und stehen nie im Repository.

@endunless -

+

+ + +

+
@if ($this->feedback !== '')

{{ $this->feedback }}

@endif diff --git a/tests/Feature/ShinkanUebernahmeTest.php b/tests/Feature/ShinkanUebernahmeTest.php index 043d30d..12b4306 100644 --- a/tests/Feature/ShinkanUebernahmeTest.php +++ b/tests/Feature/ShinkanUebernahmeTest.php @@ -11,6 +11,7 @@ use App\Domain\Catalog\ExerciseSourceSnapshot; use App\Domain\Catalog\ShinkanImport; use App\Filament\Pages\Uebernahme; +use App\Filament\Resources\Practices\Pages\ListPractices; use App\Models\User; use Filament\Facades\Filament; use Illuminate\Foundation\Testing\RefreshDatabase; @@ -245,6 +246,45 @@ public function test_the_page_lists_states_and_adopts_on_purpose(): void $this->assertNotNull($snapshot->fresh()->adopted_version_id); } + public function test_the_exercise_list_leads_to_the_takeover_page(): void + { + $user = User::factory()->redakteur()->create(); + $user->saveAppAuthenticationSecret('JBSWY3DPEHPK3PXP'); + + Livewire::actingAs($user, 'staff') + ->test(ListPractices::class) + ->assertSee('Aus Shinkan übernehmen') + ->assertSee(Uebernahme::getUrl(), false); + } + + public function test_the_page_shows_the_connection_state_without_values_and_checks_the_connection(): void + { + $user = User::factory()->redakteur()->create(); + $user->saveAppAuthenticationSecret('JBSWY3DPEHPK3PXP'); + $this->fake([$this->document('h1')], [], 'c1'); + + Livewire::actingAs($user, 'staff') + ->test(Uebernahme::class) + ->assertSee('Adresse der Quelle') + ->assertSee('shinkan.test') + ->assertSee('hinterlegt (wird nie angezeigt)') + ->assertDontSee('geheim-fuer-den-test') + ->assertDontSee('https://shinkan.test') + ->call('check') + ->assertSee('Die Verbindung funktioniert. Die Quelle stellt 1 Übung bereit. Es wurde nichts übernommen.'); + + $this->assertSame(0, ExerciseSourceSnapshot::query()->count()); + + config(['services.shinkan.key' => null]); + + Livewire::actingAs($user, 'staff') + ->test(Uebernahme::class) + ->assertSee('nicht hinterlegt') + ->assertSee('SHINKAN_DISTRIBUTION_KEY') + ->call('check') + ->assertSee('nicht eingerichtet'); + } + private function freshHttp(): void { Http::swap(new Factory);