redakteur()->create(); $editor->saveAppAuthenticationSecret('JBSWY3DPEHPK3PXP'); return $editor; } /** * @param array $extra * @return array */ private function section(string $heading, array $extra = []): array { return $extra + ['layout' => 'u4', 'style' => [], 'slots' => [['type' => 'heading', 'v' => 1, 'data' => ['heading' => $heading]]]]; } /** * @param list> $sections */ private function page(string $path, array $sections, ?string $seoTitle = null): Page { $page = Page::query()->create(['path' => $path]); app(PageService::class)->saveRevision($page, $sections, $seoTitle, null, null); return $page->fresh(); } private function editorFor(Page $page): Testable { return Livewire::actingAs($this->editor(), 'staff')->test(EditPage::class, ['record' => $page->id]); } private function action(string $name, string $key): TestAction { return TestAction::make($name)->schemaComponent('sections')->arguments(['item' => $key]); } /** * @return list> */ private function stored(Page $page): array { return $page->fresh()->latestRevision->sections(); } public function test_copy_leaves_the_source_alone_and_adds_an_identical_new_section_at_the_end_of_the_target(): void { $source = $this->page('/quelle', [$this->section('Eins', ['name' => 'Kurse', 'anchor' => 'kurse', 'hidden' => true]), $this->section('Zwei')]); $target = $this->page('/ziel', [$this->section('Ziel A')], 'Zieltitel'); $sourceRevisions = $source->revisions()->count(); $before = $this->stored($source); $component = $this->editorFor($source); $key = array_keys($component->get('data.sections'))[0]; $component->callAction($this->action('auf_seite_kopieren', $key), ['page' => $target->id, 'position' => 'end'])->assertHasNoActionErrors(); $this->assertSame($sourceRevisions, $source->revisions()->count(), 'Die Quellseite wird nicht angefasst.'); $this->assertSame($before, $this->stored($source)); $this->assertFalse($component->instance()->hasUnsavedEditorChanges()); $stored = $this->stored($target); $this->assertCount(2, $stored); $this->assertSame('Ziel A', $stored[0]['slots'][0]['data']['heading']); $copy = $stored[1]; $this->assertSame('Eins', $copy['slots'][0]['data']['heading']); $this->assertSame(['Kurse', 'kurse', true, 'u4'], [$copy['name'], $copy['anchor'], $copy['hidden'], $copy['layout']]); $this->assertNotSame($before[0]['id'], $copy['id'], 'Die Kopie ist ein eigener Abschnitt.'); $this->assertNotSame($before[0]['slots'][0]['id'], $copy['slots'][0]['id']); $this->assertSame('Zieltitel', $target->fresh()->latestRevision->seo_title, 'Die übrigen Angaben der Zielseite bleiben.'); $this->assertNull($target->fresh()->published_revision_id, 'Es wird nichts veröffentlicht.'); } public function test_the_position_in_the_target_can_be_start_end_or_after_a_section(): void { $source = $this->page('/quelle', [$this->section('Neu')]); foreach (['start' => 0, 'after:1' => 1, 'after:2' => 2, 'end' => 3] as $position => $expected) { $target = $this->page('/ziel-'.str_replace(':', '-', $position), [$this->section('A'), $this->section('B'), $this->section('C')]); $component = $this->editorFor($source); $key = array_key_first($component->get('data.sections')); $component->callAction($this->action('auf_seite_kopieren', $key), ['page' => $target->id, 'position' => $position]); $headings = array_map(fn (array $section): string => $section['slots'][0]['data']['heading'], $this->stored($target)); $this->assertCount(4, $headings, $position); $this->assertSame('Neu', $headings[$expected], 'Position '.$position); } } public function test_move_puts_the_section_on_the_target_and_removes_it_from_the_saved_source(): void { $source = $this->page('/quelle', [$this->section('Bleibt'), $this->section('Wandert', ['name' => 'Wanderer'])]); $target = $this->page('/ziel', [$this->section('Ziel A')]); $original = $this->stored($source)[1]; $component = $this->editorFor($source); $key = array_keys($component->get('data.sections'))[1]; $component->callAction($this->action('auf_seite_verschieben', $key), ['page' => $target->id, 'position' => 'start'])->assertHasNoActionErrors(); $moved = $this->stored($target)[0]; $this->assertSame('Wandert', $moved['slots'][0]['data']['heading']); $this->assertSame($original['id'], $moved['id'], 'Beim Verschieben bleibt die Kennung.'); $this->assertSame('Wanderer', $moved['name']); $left = $this->stored($source); $this->assertCount(1, $left); $this->assertSame('Bleibt', $left[0]['slots'][0]['data']['heading']); $this->assertCount(1, $component->get('data.sections'), 'Der Arbeitsstand zeigt den Abschnitt nicht mehr.'); $this->assertFalse($component->instance()->hasUnsavedEditorChanges(), 'Die Quellseite ist gespeichert.'); } public function test_the_unsaved_working_state_of_the_section_is_what_gets_copied(): void { $source = $this->page('/quelle', [$this->section('Alt')]); $target = $this->page('/ziel', []); $component = $this->editorFor($source); $key = array_key_first($component->get('data.sections')); $component->set("data.sections.{$key}.name", 'Nur im Entwurf'); $component->callAction($this->action('auf_seite_kopieren', $key), ['page' => $target->id, 'position' => 'end']); $this->assertSame('Nur im Entwurf', $this->stored($target)[0]['name']); $this->assertTrue($component->instance()->hasUnsavedEditorChanges(), 'Kopieren speichert die Quellseite nicht mit.'); } public function test_a_taken_anchor_on_the_target_gets_a_number_and_the_user_is_told(): void { $source = $this->page('/quelle', [$this->section('Eins', ['anchor' => 'kurse'])]); $target = $this->page('/ziel', [$this->section('A', ['anchor' => 'kurse'])]); $component = $this->editorFor($source); $key = array_key_first($component->get('data.sections')); $component->callAction($this->action('auf_seite_kopieren', $key), ['page' => $target->id, 'position' => 'end']) ->assertNotified('Abschnitt kopiert'); $stored = $this->stored($target); $this->assertSame('kurse', $stored[0]['anchor']); $this->assertSame('kurse-2', $stored[1]['anchor']); } public function test_a_full_target_page_refuses_and_nothing_is_written_not_even_when_moving(): void { $source = $this->page('/quelle', [$this->section('Eins'), $this->section('Zwei')]); $target = $this->page('/voll', array_map(fn (int $number): array => $this->section('S'.$number), range(1, PageSections::MAX_SECTIONS))); $component = $this->editorFor($source); $key = array_keys($component->get('data.sections'))[0]; $sourceRevisions = $source->revisions()->count(); $targetRevisions = $target->revisions()->count(); $component->callAction($this->action('auf_seite_verschieben', $key), ['page' => $target->id, 'position' => 'end']); $this->assertSame($targetRevisions, $target->revisions()->count()); $this->assertSame($sourceRevisions, $source->revisions()->count()); $this->assertCount(2, $component->get('data.sections'), 'Der Abschnitt bleibt im Arbeitsstand.'); $this->assertCount(2, $this->stored($source)); } public function test_the_dialog_offers_other_pages_and_positions_by_section_names_and_headings(): void { $source = $this->page('/quelle', [$this->section('Eins')]); $this->page('/ziel', [$this->section('Willkommen'), $this->section('Zwei', ['name' => 'Kursliste'])]); $empty = $this->page('/leer', []); $component = $this->editorFor($source); $key = array_key_first($component->get('data.sections')); // Die geöffnete Seite ist kein Ziel: Der Dialog lehnt sie ab. $component->callAction($this->action('auf_seite_kopieren', $key), ['page' => $source->id, 'position' => 'end'])->assertHasActionErrors(['page']); $positions = app(SectionTransfer::class)->positions(Page::query()->where('path', '/ziel')->firstOrFail()); $this->assertSame(['start', 'end', 'after:1', 'after:2'], array_keys($positions)); $this->assertSame('Am Anfang der Seite', $positions['start']); $this->assertSame('Hinter Abschnitt 1: Willkommen', $positions['after:1']); $this->assertSame('Hinter Abschnitt 2: Kursliste', $positions['after:2']); $this->assertSame(['end'], array_keys(app(SectionTransfer::class)->positions($empty))); } public function test_every_section_header_has_both_switches(): void { $page = $this->page('/schalter', [$this->section('A'), $this->section('B'), $this->section('C')]); $html = $this->editorFor($page)->html(); $this->assertSame(3, substr_count($html, 'data-luf-uebertragen="kopieren"')); $this->assertSame(3, substr_count($html, 'data-luf-uebertragen="verschieben"')); } public function test_the_target_must_be_another_existing_page(): void { $source = $this->page('/quelle', [$this->section('Eins')]); $component = $this->editorFor($source); $key = array_key_first($component->get('data.sections')); $revisions = $source->revisions()->count(); $component->call('transferSection', $key, $source->id, 'end', false)->call('transferSection', $key, 999999, 'end', false); $this->assertSame($revisions, $source->revisions()->count()); $this->assertCount(1, $component->get('data.sections')); } }