Übernimm Übungen aus Shinkan in den Übungsbestand und dokumentiere die Entscheidungen
CI Feature / test (push) Successful in 2m49s
Deploy Test / test (push) Successful in 2m30s
Deploy Test / deploy (push) Successful in 28s

Quellübung mit stabiler Kennung und Inhaltswert, genau ein Stand je Prüfsumme, bewusste Übernahme als neue lokale
Fassung, lokale Ergänzungen bleiben erhalten, Ausblenden an der Quelle setzt nur die Verfügbarkeit. Seite
Übernahme (Shinkan), Feldzuordnung, ADR-49 (inklusive Abweichung bei der Mediensperre), Abnahmestand und
Rückfallverfahren.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
LarsandClaude Sonnet 5.5 committed 2026-11-23 17:10:47 +01:00
1 parent bbee288e69
commit 881703ba0e
23 files changed
+1151 -6

No files matched your search

+296
View File
@@ -0,0 +1,296 @@
<?php
namespace Tests\Feature;
use App\Domain\Catalog\ArticleService;
use App\Domain\Catalog\Composition;
use App\Domain\Catalog\CompositionKind;
use App\Domain\Catalog\ContentService;
use App\Domain\Catalog\ExerciseImportRun;
use App\Domain\Catalog\ExerciseSource;
use App\Domain\Catalog\ExerciseSourceSnapshot;
use App\Domain\Catalog\ShinkanImport;
use App\Filament\Pages\Uebernahme;
use App\Models\User;
use Filament\Facades\Filament;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Http\Client\Factory;
use Illuminate\Support\Facades\Http;
use Livewire\Livewire;
use Tests\Support\BuildsExercises;
use Tests\TestCase;
/**
* Übernahme aus Shinkan in den Übungsbestand, mit Beispieldaten im Format der Katalogausgabe (`catalog_document`).
* Der Nachweis gilt dem heute gelieferten Teilumfang: Texte, Dauer, Ausstattung, Varianten und Medienmetadaten.
*/
class ShinkanUebernahmeTest extends TestCase
{
use BuildsExercises;
use RefreshDatabase;
private const ID = '6f1c1c52-0a53-4f6b-8e0e-3f0c2f6b9a11';
private const VARIANT_A = '11111111-1111-4111-8111-111111111111';
private const VARIANT_B = '22222222-2222-4222-8222-222222222222';
protected function setUp(): void
{
parent::setUp();
config(['services.shinkan.url' => 'https://shinkan.test', 'services.shinkan.key' => 'geheim-fuer-den-test']);
Filament::setCurrentPanel(Filament::getPanel('verwaltung'));
}
public function test_a_first_call_records_each_exercise_once_without_creating_a_local_version(): void
{
$this->fake([$this->document('h1')], [['source_id' => self::ID, 'change' => 'added', 'content_hash' => 'h1']], 'c1');
$run = app(ShinkanImport::class)->fetch();
$this->assertSame('ok', $run->status);
$this->assertSame('c1', $run->cursor);
$this->assertSame(1, $run->added);
$this->assertSame(1, ExerciseSource::query()->count());
$this->assertSame(1, ExerciseSourceSnapshot::query()->count());
$this->assertSame(0, Composition::query()->count(), 'Abrufen legt keine lokale Übung an.');
}
public function test_the_same_content_fetched_again_creates_nothing_new(): void
{
$this->fake([$this->document('h1')], [['source_id' => self::ID, 'change' => 'added', 'content_hash' => 'h1']], 'c1');
app(ShinkanImport::class)->fetch();
$this->fake([$this->document('h1')], [['source_id' => self::ID, 'change' => 'updated', 'content_hash' => 'h1']], 'c2');
app(ShinkanImport::class)->fetch();
app(ShinkanImport::class)->record($this->document('h1'));
$this->assertSame(1, ExerciseSourceSnapshot::query()->count());
$this->assertSame('c2', ExerciseImportRun::query()->orderByDesc('id')->value('cursor'));
}
public function test_adopting_creates_one_immutable_local_version_with_the_source_fields_and_variants(): void
{
$snapshot = app(ShinkanImport::class)->record($this->document('h1'));
$version = app(ShinkanImport::class)->adopt($snapshot);
$again = app(ShinkanImport::class)->adopt($snapshot->fresh());
$this->assertSame($version->id, $again->id);
$this->assertSame(1, $version->composition->versions()->count());
$this->assertSame(CompositionKind::Exercise, $version->composition->kind);
$this->assertSame('uebung', $version->composition->editorialContentKind->slug);
$this->assertSame('Sicher stehen und Stopp sagen', $version->composition->display_title ?? $version->composition->designation);
$this->assertSame('Kurz erklärt.', $version->composition->summary);
$this->assertSame('Nein sagen üben', $version->learning_goal);
$this->assertSame(10, $version->duration_minutes);
$this->assertSame(15, $version->duration_max);
$this->assertSame('Matten, Pratzen', $version->material_note);
$this->assertSame($snapshot->id, $version->source_snapshot_id);
$this->assertSame([self::VARIANT_A, self::VARIANT_B], $version->variants->pluck('key')->all());
$this->assertSame(['quelle', 'quelle'], $version->variants->pluck('origin')->all());
$this->assertSame(['Vorbereitung', 'Durchführung', 'Trainerhinweise', 'Abweichung der Durchführung'], $version->members->pluck('role')->all());
$this->assertSame(['quelle'], $version->members->pluck('origin')->unique()->all());
$this->assertSame(self::VARIANT_A, $version->members->last()->variant->key);
$this->assertSame($version->composition_id, ExerciseSource::query()->firstOrFail()->composition_id);
}
public function test_a_changed_variant_is_recognised_and_a_new_version_keeps_the_local_addition(): void
{
$import = app(ShinkanImport::class);
$first = $import->adopt($import->record($this->document('h1')));
$film = $this->film('Eigenes Lara-und-Felix-Video');
$content = app(ContentService::class);
// Lokale Ergänzung als neue lokale Fassung: eigenes Video in der Variante A.
$members = $first->members->map(fn ($member): array => [
'role' => $member->role, 'target' => $member->contentElementVersion, 'variant' => $member->variant?->key, 'origin' => $member->origin,
])->all();
$members[] = ['role' => 'Eigenes Video', 'target' => $film, 'variant' => self::VARIANT_A, 'requirement' => 'ergaenzend', 'origin' => 'lokal'];
$second = $content->publishComposition($first->composition, $members, null, 'Ergänzt', [], $first->variants->map(fn ($v): array => [
'key' => $v->key, 'name' => $v->name, 'description' => $v->description, 'origin' => $v->origin,
])->all());
$changed = $this->document('h2');
$changed['variants'][0]['execution_changes'] = 'Mit Abstand von zwei Armlängen.';
$source = ExerciseSource::query()->firstOrFail();
$this->assertSame('uebernommen', $import->state($source->fresh()));
$snapshot = $import->record($changed);
$this->assertSame('geaendert', $import->state($source->fresh()));
$third = $import->adopt($snapshot);
$this->assertSame(3, $third->number);
$local = $third->members->where('origin', 'lokal');
$this->assertSame(['Eigenes Video'], $local->pluck('role')->all());
$this->assertSame(self::VARIANT_A, $local->first()->variant->key);
$this->assertSame('ergaenzend', $local->first()->requirement);
$this->assertStringContainsString('zwei Armlängen', $third->members->firstWhere('role', 'Abweichung der Durchführung')->contentElementVersion->body);
$this->assertSame('uebernommen', $import->state($source->fresh()));
// Die älteren Fassungen bleiben unverändert.
$this->assertSame(4, $first->fresh()->members()->count());
$this->assertSame(5, $second->fresh()->members()->count());
}
public function test_the_unchanged_text_of_a_source_section_keeps_the_same_text_version(): void
{
$import = app(ShinkanImport::class);
$first = $import->adopt($import->record($this->document('h1')));
$changed = $this->document('h2');
$changed['variants'][0]['execution_changes'] = 'Anders.';
$second = $import->adopt($import->record($changed));
$this->assertSame(
$first->members->firstWhere('role', 'Durchführung')->content_element_version_id,
$second->members->firstWhere('role', 'Durchführung')->content_element_version_id,
);
$this->assertNotSame(
$first->members->firstWhere('role', 'Abweichung der Durchführung')->content_element_version_id,
$second->members->firstWhere('role', 'Abweichung der Durchführung')->content_element_version_id,
);
}
public function test_a_failed_call_keeps_the_stored_state_and_the_cursor_and_names_no_secret(): void
{
$this->fake([$this->document('h1')], [['source_id' => self::ID, 'change' => 'added', 'content_hash' => 'h1']], 'c1');
app(ShinkanImport::class)->fetch();
$this->freshHttp();
Http::fake([
'shinkan.test/api/distribution/v1/catalog/*' => Http::response(['code' => 'boom'], 500),
'shinkan.test/api/distribution/v1/catalog*' => Http::response([
'exercises' => [['source_id' => self::ID, 'title' => 'x', 'content_hash' => 'h2']],
'changes' => [['source_id' => self::ID, 'change' => 'updated', 'content_hash' => 'h2']],
'next_cursor' => 'c2',
]),
]);
$run = app(ShinkanImport::class)->fetch();
$this->assertSame('fehler', $run->status);
$this->assertStringContainsString('Status 500', $run->error);
$this->assertStringNotContainsString('geheim-fuer-den-test', (string) $run->error);
$this->assertStringNotContainsString('shinkan.test', (string) $run->error);
$this->assertSame(1, ExerciseSourceSnapshot::query()->count());
// Der nächste Abruf beginnt wieder beim letzten erfolgreichen Cursor.
$this->fake([$this->document('h2')], [['source_id' => self::ID, 'change' => 'updated', 'content_hash' => 'h2']], 'c2');
$this->assertSame('ok', app(ShinkanImport::class)->fetch()->status);
$this->assertSame(2, ExerciseSourceSnapshot::query()->count());
Http::assertSent(fn ($request): bool => str_contains($request->url(), 'cursor=c1'));
}
public function test_an_unconfigured_connection_is_reported_without_a_call(): void
{
config(['services.shinkan.url' => null, 'services.shinkan.key' => null]);
Http::fake();
$run = app(ShinkanImport::class)->fetch();
$this->assertSame('fehler', $run->status);
$this->assertStringContainsString('nicht eingerichtet', $run->error);
Http::assertNothingSent();
}
public function test_hiding_at_the_source_marks_only_the_availability_and_leaves_local_use_alone(): void
{
$import = app(ShinkanImport::class);
$version = $import->adopt($import->record($this->document('h1')));
$module = $this->moduleWith('Modul', $version, [self::VARIANT_A]);
$articles = app(ArticleService::class);
$release = $articles->publishRelease($articles->create('modul-shinkan'), $module, 'Mit Shinkan-Übung.');
$this->fake([], [['source_id' => self::ID, 'change' => 'removed']], 'c9', listed: []);
$run = app(ShinkanImport::class)->fetch();
$source = ExerciseSource::query()->firstOrFail();
$this->assertSame(1, $run->removed);
$this->assertSame('nicht_mehr_bereitgestellt', $import->state($source));
$this->assertSame('veroeffentlicht', $release->fresh()->status->value);
$this->assertSame(1, $version->composition->fresh()->versions()->count());
}
public function test_source_media_metadata_never_becomes_deliverable_content(): void
{
$import = app(ShinkanImport::class);
$version = $import->adopt($import->record($this->document('h1')));
$module = $this->moduleWith('Modul', $version);
$articles = app(ArticleService::class);
$release = $articles->publishRelease($articles->create('modul-medien'), $module, 'Nur Texte.');
$this->assertNotEmpty($release->contents);
$this->assertSame(['text'], $release->contents->map(fn ($c): string => $c->elementVersion->element->output_class)->unique()->all());
$this->assertStringNotContainsString('Quellvideo', json_encode($release->contents->map->path->all()));
}
public function test_the_page_lists_states_and_adopts_on_purpose(): void
{
$import = app(ShinkanImport::class);
$import->record($this->document('h1'));
$user = User::factory()->redakteur()->create();
$user->saveAppAuthenticationSecret('JBSWY3DPEHPK3PXP');
$component = Livewire::actingAs($user, 'staff')
->test(Uebernahme::class)
->assertOk()
->assertSee('Sicher stehen und Stopp sagen')
->assertSee('Neu bei der Quelle')
->assertSee('Quellvideo (video)')
->assertSee('2 Varianten');
$snapshot = ExerciseSourceSnapshot::query()->firstOrFail();
$component->call('adopt', $snapshot->id)->assertSee('Fassung 1 ist lokal angelegt')->assertSee('Übernommen')->assertSee('Lokale Übung öffnen');
$this->assertNotNull($snapshot->fresh()->adopted_version_id);
}
private function freshHttp(): void
{
Http::swap(new Factory);
}
/**
* @param list<array<string, mixed>> $details
* @param list<array<string, mixed>> $changes
* @param list<array<string, mixed>>|null $listed
*/
private function fake(array $details, array $changes, string $cursor, ?array $listed = null): void
{
$listing = $listed ?? array_map(fn (array $d): array => ['source_id' => $d['source_id'], 'title' => $d['title'], 'content_hash' => $d['content_hash']], $details);
$fakes = [];
foreach ($details as $detail) {
$fakes['shinkan.test/api/distribution/v1/catalog/'.$detail['source_id']] = Http::response($detail);
}
$fakes['shinkan.test/api/distribution/v1/catalog*'] = Http::response(['exercises' => $listing, 'changes' => $changes, 'next_cursor' => $cursor]);
$this->freshHttp();
Http::fake($fakes);
}
/**
* @return array<string, mixed>
*/
private function document(string $hash): array
{
return [
'source_id' => self::ID,
'title' => 'Sicher stehen und Stopp sagen',
'summary' => 'Kurz erklärt.',
'goal' => 'Nein sagen üben',
'execution' => "Die Kinder stehen sicher.\n\n- Stimme einsetzen\n- Stopp sagen",
'preparation' => 'Matten auslegen.',
'trainer_notes' => 'Auf Abstand achten.',
'equipment' => 'Matten, Pratzen',
'duration_min' => 10,
'duration_max' => 15,
'variants' => [
['id' => self::VARIANT_A, 'name' => 'Mit Partner', 'description' => 'Zu zweit', 'execution_changes' => 'Mit Partner üben.', 'sequence_order' => 1],
['id' => self::VARIANT_B, 'name' => 'Allein', 'description' => 'Einzeln', 'execution_changes' => '', 'sequence_order' => 2],
],
'media' => [['id' => '33333333-3333-4333-8333-333333333333', 'media_type' => 'video', 'title' => 'Quellvideo', 'sort_order' => 1]],
'content_hash' => $hash,
];
}
}