Werte aus der Umgebung des Servers haben Vorrang, sonst gilt der in der Verwaltung gepflegte. Der Schlüssel liegt verschlüsselt in site_settings und wird nie angezeigt. Änderung nur für Katalog- und Systemverwalter. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
431 lines
20 KiB
PHP
431 lines
20 KiB
PHP
<?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\ShinkanConnection;
|
|
use App\Domain\Catalog\ShinkanImport;
|
|
use App\Domain\Design\SiteSetting;
|
|
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;
|
|
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);
|
|
}
|
|
|
|
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')
|
|
->call('check')
|
|
->assertSee('nicht eingerichtet');
|
|
}
|
|
|
|
public function test_address_and_key_can_be_kept_in_the_administration_and_the_key_is_stored_encrypted(): void
|
|
{
|
|
config(['services.shinkan.url' => null, 'services.shinkan.key' => null]);
|
|
$keeper = $this->staff('katalogverwalter');
|
|
|
|
Livewire::actingAs($keeper, 'staff')
|
|
->test(Uebernahme::class)
|
|
->assertSee('Anbindung speichern')
|
|
->set('url', 'https://shinkan.example/')
|
|
->set('apiKey', 'schluessel-aus-shinkan')
|
|
->call('saveConnection')
|
|
->assertSee('Die Anbindung ist gespeichert')
|
|
->assertSet('apiKey', '')
|
|
->assertSee('shinkan.example')
|
|
->assertSee('in der Verwaltung gepflegt')
|
|
->assertDontSee('schluessel-aus-shinkan');
|
|
|
|
$stored = SiteSetting::query()->findOrFail('shinkan.anbindung')->value;
|
|
$this->assertSame('https://shinkan.example', $stored['url']);
|
|
$this->assertStringNotContainsString('schluessel-aus-shinkan', json_encode($stored));
|
|
$this->assertSame('schluessel-aus-shinkan', app(ShinkanConnection::class)->apiKey());
|
|
|
|
$this->freshHttp();
|
|
Http::fake(['shinkan.example/*' => Http::response(['exercises' => [], 'changes' => [], 'next_cursor' => 'c1'])]);
|
|
|
|
$this->assertSame('ok', app(ShinkanImport::class)->fetch()->status);
|
|
Http::assertSent(fn ($request): bool => $request->hasHeader('Authorization', 'Bearer schluessel-aus-shinkan') && str_starts_with($request->url(), 'https://shinkan.example/api/distribution/v1/catalog'));
|
|
}
|
|
|
|
public function test_a_value_from_the_server_environment_wins_over_the_one_kept_in_the_administration(): void
|
|
{
|
|
$connection = app(ShinkanConnection::class);
|
|
config(['services.shinkan.url' => null, 'services.shinkan.key' => null]);
|
|
$connection->save('https://verwaltung.example', 'schluessel-verwaltung');
|
|
|
|
$this->assertSame('verwaltung', $connection->source('key'));
|
|
|
|
config(['services.shinkan.url' => 'https://server.example', 'services.shinkan.key' => 'schluessel-server']);
|
|
|
|
$this->assertSame('https://server.example', $connection->url());
|
|
$this->assertSame('schluessel-server', $connection->apiKey());
|
|
$this->assertSame('server', $connection->source('key'));
|
|
|
|
config(['services.shinkan.key' => null]);
|
|
|
|
$this->assertSame('schluessel-verwaltung', $connection->apiKey(), 'Ohne Wert in der Umgebung gilt der gepflegte.');
|
|
}
|
|
|
|
public function test_only_catalog_and_system_administrators_change_the_connection_and_the_address_is_checked(): void
|
|
{
|
|
config(['services.shinkan.url' => null, 'services.shinkan.key' => null]);
|
|
|
|
Livewire::actingAs($this->staff('redakteur'), 'staff')
|
|
->test(Uebernahme::class)
|
|
->assertDontSee('Anbindung speichern')
|
|
->set('url', 'https://x.example')
|
|
->call('saveConnection')
|
|
->assertForbidden();
|
|
|
|
$this->assertSame(0, SiteSetting::query()->where('key', 'shinkan.anbindung')->count());
|
|
|
|
Livewire::actingAs($this->staff('systemverwalter'), 'staff')
|
|
->test(Uebernahme::class)
|
|
->set('url', 'ftp://kein-web')
|
|
->set('apiKey', 'x')
|
|
->call('saveConnection')
|
|
->assertSee('keine gültige Internetadresse');
|
|
}
|
|
|
|
public function test_the_kept_key_can_be_removed_and_a_new_one_replaces_it(): void
|
|
{
|
|
config(['services.shinkan.url' => null, 'services.shinkan.key' => null]);
|
|
$connection = app(ShinkanConnection::class);
|
|
$connection->save('https://shinkan.example', 'alt');
|
|
$connection->save('https://shinkan.example', '');
|
|
$this->assertSame('alt', $connection->apiKey(), 'Ein leeres Feld lässt den Schlüssel unverändert.');
|
|
$connection->save('https://shinkan.example', 'neu');
|
|
$this->assertSame('neu', $connection->apiKey());
|
|
|
|
Livewire::actingAs($this->staff('katalogverwalter'), 'staff')->test(Uebernahme::class)->call('removeKey');
|
|
|
|
$this->assertNull($connection->apiKey());
|
|
$this->assertFalse(app(ShinkanImport::class)->configured());
|
|
}
|
|
|
|
private function staff(string $role): User
|
|
{
|
|
$user = User::factory()->{$role}()->create();
|
|
$user->saveAppAuthenticationSecret('JBSWY3DPEHPK3PXP');
|
|
|
|
return $user;
|
|
}
|
|
|
|
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,
|
|
];
|
|
}
|
|
}
|