admin = User::factory()->admin()->create(); $png = UploadedFile::fake()->image('x.png', 40, 30)->getContent(); Http::fake([ 'intranet.example.org/wp-content/uploads/*' => Http::response($png, 200, ['Content-Type' => 'image/png']), '*' => Http::response('nicht da', 404), ]); } private function analyze(): WpImport { Storage::disk('local')->put('wp-import/export.xml', file_get_contents(base_path('tests/Fixtures/wordpress-export.xml'))); return tap(app(Analyzer::class)->analyze(Storage::disk('local')->path('wp-import/export.xml'), $this->admin), fn ($i) => $i->update(['file' => 'wp-import/export.xml'])); } private function runImport(WpImport $import, array $options = []): WpImport { $importer = app(Importer::class); $importer->start($import, array_merge(['download_images' => true, 'fallback_author' => $this->admin->id], $options)); for ($i = 0; $i < 20 && ! $importer->step($import->fresh()); $i++); return $import->fresh(); } public function test_export_is_analyzed(): void { $import = $this->analyze(); $this->assertSame('Altes Intranet', $import->site_title); $this->assertSame('intranet.example.org', $import->source()); $this->assertSame(['post' => 4, 'page' => 2, 'attachment' => 1, 'category' => 2, 'author' => 2, 'other' => 1, 'drafts' => 1], $import->stats['found']); } public function test_posts_categories_authors_and_images_are_imported(): void { $redaktion = User::factory()->create(['email' => 'redaktion@firma.de']); $existing = NewsCategory::factory()->create(['name' => 'MAV-Info', 'slug' => 'mav-info']); $import = $this->runImport($this->analyze()); $this->assertSame('done', $import->status); $this->assertSame(2, NewsPost::query()->count(), 'Entwurf und privater Beitrag bleiben draußen.'); $this->assertSame(2, NewsCategory::query()->count(), 'Vorhandene Kategorie wird genutzt, fehlende angelegt.'); $fest = NewsPost::query()->where('slug', 'sommerfest')->sole(); $this->assertSame('Sommerfest 2024 – Rückblick', $fest->title); $this->assertSame('Aus den Bereichen & Häusern', $fest->category->name); $this->assertSame($redaktion->id, $fest->author_id); $this->assertTrue($fest->isPublished()); $this->assertSame('2024-06-21 16:30:00', $fest->published_at->utc()->format('Y-m-d H:i:s')); $this->assertNotNull($fest->cover_path); Storage::disk('uploads')->assertExists($fest->cover_path); $this->assertStringContainsString('

Das Sommerfest war ein voller Erfolg.
', $fest->body); $this->assertStringContainsString('Das Buffet im Innenhof', $fest->body); $this->assertStringNotContainsString('intranet.example.org', $fest->body, 'Bilder liegen jetzt im Portal.'); $this->assertStringNotContainsString('srcset', $fest->body); $this->assertStringNotContainsString('contact-form-7', $fest->body); $this->assertStringNotContainsString('body_html); $this->assertSame('Das Sommerfest war ein voller Erfolg. Vielen Dank an alle Helfer! Das Buffet im Innenhof Nächstes Jahr', $fest->teaser); $dv = NewsPost::query()->where('slug', 'neue-dienstvereinbarung')->sole(); $this->assertSame($existing->id, $dv->category_id); $this->assertSame($this->admin->id, $dv->author_id, 'Unbekannte Person → Ersatz.'); $this->assertSame('Kurz zusammengefasst.', $dv->teaser); $this->assertStringNotContainsString('wp:paragraph', $dv->body); $this->assertStringContainsString('Unterschrift', $dv->body); $this->assertStringContainsString('body, 'Relative Bildadressen werden über die Website-Adresse geladen.'); $this->assertSame(['created' => 3, 'updated' => 1, 'skipped' => 2, 'failed' => 0], array_intersect_key($import->stats, array_flip(['created', 'updated', 'skipped', 'failed']))); Storage::disk('local')->assertMissing('wp-import/export.xml'); } public function test_reimport_does_not_duplicate_and_can_update(): void { $this->runImport($this->analyze()); NewsPost::query()->where('slug', 'sommerfest')->update(['title' => 'Geändert']); $second = $this->runImport($this->analyze()); $this->assertSame(2, NewsPost::query()->count()); $this->assertSame('Geändert', NewsPost::query()->where('slug', 'sommerfest')->value('title')); $this->assertSame(0, $second->stats['created']); Http::assertSentCount(4); // Titelbild, zwei Bilder im Text und die Galerie – beim zweiten Mal nichts erneut geladen $this->runImport($this->analyze(), ['update_existing' => true, 'include_drafts' => true]); $this->assertSame('Sommerfest 2024 – Rückblick', NewsPost::query()->where('slug', 'sommerfest')->value('title')); $this->assertSame(NewsPost::DRAFT, NewsPost::query()->where('title', 'Entwurf: Weihnachtsfeier')->value('status')); } public function test_pages_keep_their_hierarchy(): void { $import = $this->runImport($this->analyze(), ['import_pages' => true, 'publish_pages' => true]); $mav = Page::query()->where('path', 'mav')->sole(); $child = Page::query()->where('path', 'mav/wahlergebnis')->sole(); $this->assertSame($mav->id, $child->parent_id); $this->assertTrue($child->isPublished()); $this->assertSame('text', $child->content['blocks'][0]['type']); $this->assertSame('imported', $mav->revisions()->first()->action); $this->assertSame(0, $import->stats['failed']); } public function test_non_wordpress_files_are_rejected(): void { Storage::disk('local')->put('wp-import/kaputt.xml', 'x'); $this->expectExceptionMessage('keine WordPress-Exportdatei'); app(Analyzer::class)->analyze(Storage::disk('local')->path('wp-import/kaputt.xml'), $this->admin); } public function test_admin_page_runs_the_import(): void { $this->actingAs($this->admin); Livewire::test(WordpressImport::class) ->set('upload.file', UploadedFile::fake()->createWithContent('export.xml', file_get_contents(base_path('tests/Fixtures/wordpress-export.xml')))) ->call('analyze') ->assertRedirect(); Livewire::test(WordpressImport::class) ->assertSee('Altes Intranet') ->assertSee('Rita Redaktion') ->call('start') ->assertHasNoErrors() ->call('step') ->assertSee('Import abgeschlossen'); $this->assertSame(2, NewsPost::query()->count()); $this->actingAs(User::factory()->create())->get('/admin/wordpress-import')->assertForbidden(); } public function test_classic_content_conversion(): void { $html = app(ContentConverter::class)->convert("Zeile 1\nZeile 2\n\n

\n\n[embed]https://youtu.be/abc[/embed]", fn ($src) => $src); $this->assertSame("

Zeile 1
\nZeile 2

\n\n

https://youtu.be/abc

", $html); } }