Loheide-eu-mitarbeiterplatt.../tests/Feature/WordpressImport/WordpressImportTest.php
Friederich Loheide 2b5fe55f8e
All checks were successful
CI / PHP 8.3 / mariadb:10.6 (pull_request) Successful in 2m40s
CI / PHP 8.4 / mariadb:10.6 (pull_request) Successful in 2m23s
CI / PHP 8.3 / mariadb:11.4 (pull_request) Successful in 2m53s
CI / PHP 8.4 / mariadb:11.4 (pull_request) Successful in 2m47s
CI / PHP 8.3 / mysql:8.0 (pull_request) Successful in 3m2s
CI / PHP 8.4 / mysql:8.0 (pull_request) Successful in 3m5s
CI / PHP 8.3 / mysql:8.4 (pull_request) Successful in 2m50s
CI / PHP 8.4 / mysql:8.4 (pull_request) Successful in 2m41s
CI / Build-Artefakte aktuell (pull_request) Successful in 1m6s
MySQL: JSON-Schlüsselreihenfolge darf keine Rolle spielen
MySQL speichert Schlüssel in JSON-Spalten sortiert, MariaDB unverändert.
- Seiten: „Stand unverändert → kein Entwurf“ vergleicht jetzt unabhängig von der
  Schlüsselreihenfolge (auf MySQL entstand sonst bei jedem Speichern ein Entwurf)
- Formulare: Antworten in Ergebnisansicht und E-Mail in der Reihenfolge des Formulars
- Tests vergleichen assoziative Arrays ohne Reihenfolge; neue Tests dafür

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
2026-09-26 11:22:46 +00:00

174 lines
8 KiB
PHP
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php
namespace Tests\Feature\WordpressImport;
use App\Models\User;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Http\UploadedFile;
use Illuminate\Support\Facades\Http;
use Illuminate\Support\Facades\Storage;
use Inhaus\Plugins\News\Models\NewsCategory;
use Inhaus\Plugins\News\Models\NewsPost;
use Inhaus\Plugins\Pages\Models\Page;
use Inhaus\Plugins\WordpressImport\Analyzer;
use Inhaus\Plugins\WordpressImport\ContentConverter;
use Inhaus\Plugins\WordpressImport\Filament\Pages\WordpressImport;
use Inhaus\Plugins\WordpressImport\Importer;
use Inhaus\Plugins\WordpressImport\Models\WpImport;
use Livewire\Livewire;
use Tests\TestCase;
class WordpressImportTest extends TestCase
{
use RefreshDatabase;
private User $admin;
protected function setUp(): void
{
parent::setUp();
Storage::fake('uploads');
Storage::fake('local');
$this->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('<p>Das Sommerfest war ein voller Erfolg.<br>', $fest->body);
$this->assertStringContainsString('<em>Das Buffet im Innenhof</em>', $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('<script', $fest->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('<em>Unterschrift</em>', $dv->body);
$this->assertStringContainsString('<img', $dv->body, 'Relative Bildadressen werden über die Website-Adresse geladen.');
$this->assertEquals(['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', '<rss><channel><item><title>x</title></item></channel></rss>');
$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<ul><li>A</li></ul>\n\n[embed]https://youtu.be/abc[/embed]", fn ($src) => $src);
$this->assertSame("<p>Zeile 1<br>\nZeile 2</p>\n<ul><li>A</li></ul>\n<p><a href=\"https://youtu.be/abc\">https://youtu.be/abc</a></p>", $html);
}
}