Formulare, WordPress-Import und E-Mail-Versand
Some checks failed
CI / PHP 8.3 / mariadb:10.6 (pull_request) Successful in 2m0s
CI / PHP 8.4 / mariadb:10.6 (pull_request) Successful in 2m7s
CI / PHP 8.3 / mariadb:11.4 (pull_request) Successful in 2m11s
CI / PHP 8.4 / mariadb:11.4 (pull_request) Successful in 2m7s
CI / PHP 8.3 / mysql:8.0 (pull_request) Failing after 2m52s
CI / PHP 8.4 / mysql:8.0 (pull_request) Failing after 3m2s
CI / PHP 8.3 / mysql:8.4 (pull_request) Failing after 2m31s
CI / PHP 8.4 / mysql:8.4 (pull_request) Failing after 2m27s
CI / Build-Artefakte aktuell (pull_request) Successful in 53s
Some checks failed
CI / PHP 8.3 / mariadb:10.6 (pull_request) Successful in 2m0s
CI / PHP 8.4 / mariadb:10.6 (pull_request) Successful in 2m7s
CI / PHP 8.3 / mariadb:11.4 (pull_request) Successful in 2m11s
CI / PHP 8.4 / mariadb:11.4 (pull_request) Successful in 2m7s
CI / PHP 8.3 / mysql:8.0 (pull_request) Failing after 2m52s
CI / PHP 8.4 / mysql:8.0 (pull_request) Failing after 3m2s
CI / PHP 8.3 / mysql:8.4 (pull_request) Failing after 2m31s
CI / PHP 8.4 / mysql:8.4 (pull_request) Failing after 2m27s
CI / Build-Artefakte aktuell (pull_request) Successful in 53s
- Formular-Plugin: Baukasten mit 13 Feldtypen, Bedingungen, Vorbelegung aus dem Profil, Datei-Uploads (geschützt), anonyme Formulare, „nur einmal“, Einsendeschluss, Sichtbarkeit, Löschfrist; Einsendungen im Backend und per Mail (optional ohne Angaben, Kopie an die Person); Rechte je Formular und Rolle für Ansehen/Export und Löschen; CSV für Excel; Pagebuilder-Block - WordPress-Import (Plugin): WXR-Datei einlesen, Kategorien/Personen zuordnen, Beiträge mit Status, Datum, Beitragsbild und Bildern im Text übernehmen, optional Seiten mit Hierarchie; in Portionen, wiederholbar ohne Doppelungen - E-Mail-Versand im Admin (SMTP, verschlüsseltes Passwort, Test-Mail) - Speichertest unabhängig vom Verbrauch der Test-Suite Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
This commit is contained in:
parent
f3987e8386
commit
11123fecc0
118 changed files with 4446 additions and 169 deletions
47
tests/Feature/Admin/MailSettingsTest.php
Normal file
47
tests/Feature/Admin/MailSettingsTest.php
Normal file
|
|
@ -0,0 +1,47 @@
|
|||
<?php
|
||||
|
||||
namespace Tests\Feature\Admin;
|
||||
|
||||
use App\Filament\Pages\MailSettingsPage;
|
||||
use App\Models\User;
|
||||
use App\Support\MailSettings;
|
||||
use App\Support\Settings;
|
||||
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||||
use Livewire\Livewire;
|
||||
use Tests\TestCase;
|
||||
|
||||
class MailSettingsTest extends TestCase
|
||||
{
|
||||
use RefreshDatabase;
|
||||
|
||||
public function test_smtp_settings_are_stored_encrypted_and_applied(): void
|
||||
{
|
||||
$this->actingAs(User::factory()->admin()->create());
|
||||
|
||||
Livewire::test(MailSettingsPage::class)
|
||||
->fillForm([
|
||||
'mailer' => 'smtp', 'host' => 'smtp.office365.com', 'port' => 587, 'encryption' => 'tls',
|
||||
'username' => 'intranet@firma.de', 'password' => 'geheim123', 'from_address' => 'intranet@firma.de', 'from_name' => 'Intranet',
|
||||
])
|
||||
->call('save')
|
||||
->assertHasNoFormErrors();
|
||||
|
||||
$this->assertNotSame('geheim123', app(Settings::class)->get('mail.password'));
|
||||
$this->assertSame('smtp', config('mail.default'));
|
||||
$this->assertSame('smtp.office365.com', config('mail.mailers.smtp.host'));
|
||||
$this->assertSame('geheim123', config('mail.mailers.smtp.password'));
|
||||
$this->assertSame('intranet@firma.de', config('mail.from.address'));
|
||||
|
||||
// Leeres Passwort behält das gespeicherte.
|
||||
Livewire::test(MailSettingsPage::class)->assertFormSet(['password' => null])->fillForm(['host' => 'mail.firma.de'])->call('save');
|
||||
config(['mail.mailers.smtp.password' => null]);
|
||||
app(MailSettings::class)->apply();
|
||||
$this->assertSame('geheim123', config('mail.mailers.smtp.password'));
|
||||
$this->assertSame('mail.firma.de', config('mail.mailers.smtp.host'));
|
||||
}
|
||||
|
||||
public function test_only_permitted_people_open_the_page(): void
|
||||
{
|
||||
$this->actingAs(User::factory()->create())->get('/admin/e-mail')->assertForbidden();
|
||||
}
|
||||
}
|
||||
274
tests/Feature/Forms/FormsTest.php
Normal file
274
tests/Feature/Forms/FormsTest.php
Normal file
|
|
@ -0,0 +1,274 @@
|
|||
<?php
|
||||
|
||||
namespace Tests\Feature\Forms;
|
||||
|
||||
use App\Models\Role;
|
||||
use App\Models\User;
|
||||
use App\Notifications\PortalNotification;
|
||||
use App\Pagebuilder\Content;
|
||||
use App\Support\Permissions\Audiences;
|
||||
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||||
use Illuminate\Http\UploadedFile;
|
||||
use Illuminate\Support\Facades\Mail;
|
||||
use Illuminate\Support\Facades\Notification;
|
||||
use Illuminate\Support\Facades\Storage;
|
||||
use Inertia\Testing\AssertableInertia as Assert;
|
||||
use Inhaus\Plugins\Forms\Filament\Resources\Forms\Pages\CreateForm;
|
||||
use Inhaus\Plugins\Forms\Filament\Resources\Forms\Pages\FormResults;
|
||||
use Inhaus\Plugins\Forms\Filament\Resources\Forms\Pages\ListForms;
|
||||
use Inhaus\Plugins\Forms\Mail\FormSubmitted;
|
||||
use Inhaus\Plugins\Forms\Models\Form;
|
||||
use Inhaus\Plugins\Forms\Models\FormSubmission;
|
||||
use Inhaus\Plugins\Forms\Retention;
|
||||
use Livewire\Livewire;
|
||||
use Tests\TestCase;
|
||||
|
||||
class FormsTest extends TestCase
|
||||
{
|
||||
use RefreshDatabase;
|
||||
|
||||
private User $employee;
|
||||
|
||||
protected function setUp(): void
|
||||
{
|
||||
parent::setUp();
|
||||
|
||||
Storage::fake('local');
|
||||
Mail::fake();
|
||||
Notification::fake();
|
||||
$this->employee = User::factory()->create(['first_name' => 'Anna', 'last_name' => 'Schmidt', 'email' => 'anna@firma.de', 'department' => 'Pflege']);
|
||||
}
|
||||
|
||||
private function trainingForm(array $settings = [], array $attributes = []): Form
|
||||
{
|
||||
return Form::query()->create([
|
||||
'title' => 'Antrag auf Fortbildung',
|
||||
'fields' => [
|
||||
['type' => 'heading', 'data' => ['text' => 'Angaben zur Person']],
|
||||
['type' => 'text', 'data' => ['label' => 'Name', 'required' => true, 'prefill' => 'name']],
|
||||
['type' => 'select', 'data' => ['label' => 'Art', 'required' => true, 'options' => ['Seminar', 'Studium', 'Sonstiges']]],
|
||||
['type' => 'textarea', 'data' => ['label' => 'Welche?', 'required' => true, 'condition_field' => 'art', 'condition_operator' => 'equals', 'condition_value' => 'Sonstiges']],
|
||||
['type' => 'number', 'data' => ['label' => 'Kosten', 'min' => 0, 'max' => 5000]],
|
||||
['type' => 'checkboxes', 'data' => ['label' => 'Tage', 'options' => ['Mo', 'Di', 'Mi']]],
|
||||
['type' => 'file', 'data' => ['label' => 'Nachweis', 'accept' => 'documents', 'max_size' => 2]],
|
||||
['type' => 'checkbox', 'data' => ['label' => 'Ich stimme der Verarbeitung zu', 'key' => 'zustimmung', 'required' => true]],
|
||||
],
|
||||
'settings' => array_merge(['notify_emails' => ['personal@firma.de']], $settings),
|
||||
...$attributes,
|
||||
]);
|
||||
}
|
||||
|
||||
private function resultsRole(Form $form, bool $delete = false): Role
|
||||
{
|
||||
$role = Role::query()->create(['name' => 'Personalabteilung']);
|
||||
$role->grants()->create(['ability' => 'forms.results', 'resource_type' => 'form', 'resource_id' => $form->id]);
|
||||
|
||||
if ($delete) {
|
||||
$role->grants()->create(['ability' => 'forms.results.delete', 'resource_type' => 'form', 'resource_id' => $form->id]);
|
||||
}
|
||||
|
||||
return $role;
|
||||
}
|
||||
|
||||
public function test_keys_are_generated_and_unique(): void
|
||||
{
|
||||
$form = Form::query()->create(['title' => 'Test', 'fields' => [
|
||||
['type' => 'text', 'data' => ['label' => 'Straße & Nr.']],
|
||||
['type' => 'text', 'data' => ['label' => 'Straße & Nr.']],
|
||||
['type' => 'unbekannt', 'data' => ['label' => 'x']],
|
||||
]]);
|
||||
|
||||
$this->assertSame(['strasse_nr', 'strasse_nr_2'], array_column(array_column($form->fields, 'data'), 'key'));
|
||||
$this->assertSame('test', $form->slug);
|
||||
}
|
||||
|
||||
public function test_employee_fills_in_the_form_and_hr_is_notified(): void
|
||||
{
|
||||
$form = $this->trainingForm(['notify_viewers' => true, 'send_copy' => true]);
|
||||
$hr = User::factory()->withRoles([$this->resultsRole($form)])->create();
|
||||
|
||||
$this->actingAs($this->employee)->get('/formulare/antrag-auf-fortbildung')->assertOk()->assertInertia(fn (Assert $p) => $p
|
||||
->component('forms::Show')
|
||||
->where('form.prefill.name', 'Anna Schmidt')
|
||||
->where('form.fields.3.condition', ['field' => 'art', 'operator' => 'equals', 'value' => 'Sonstiges']));
|
||||
|
||||
// Pflichtfelder, Auswahl nur aus den Optionen, Bedingung greift.
|
||||
$this->post('/formulare/antrag-auf-fortbildung', ['answers' => ['name' => 'Anna Schmidt', 'art' => 'Sonstiges', 'kosten' => 9999]])
|
||||
->assertSessionHasErrors(['answers.welche', 'answers.kosten', 'answers.zustimmung']);
|
||||
$this->post('/formulare/antrag-auf-fortbildung', ['answers' => ['name' => 'Anna', 'art' => 'Erfunden', 'zustimmung' => '1']])
|
||||
->assertSessionHasErrors(['answers.art']);
|
||||
|
||||
$this->from('/formulare/antrag-auf-fortbildung')->post('/formulare/antrag-auf-fortbildung', ['answers' => [
|
||||
'name' => 'Anna Schmidt',
|
||||
'art' => 'Seminar',
|
||||
'welche' => 'wird ignoriert, weil ausgeblendet',
|
||||
'kosten' => '450',
|
||||
'tage' => ['Mo', 'Mi'],
|
||||
'nachweis' => [UploadedFile::fake()->create('Angebot.pdf', 100, 'application/pdf')],
|
||||
'zustimmung' => '1',
|
||||
]])->assertRedirect('/formulare/antrag-auf-fortbildung')->assertSessionHas('form_submitted', $form->id);
|
||||
|
||||
$submission = FormSubmission::query()->sole();
|
||||
$this->assertSame($this->employee->id, $submission->user_id);
|
||||
$this->assertArrayNotHasKey('welche', $submission->data);
|
||||
$this->assertSame('Mo, Mi', $submission->display('tage'));
|
||||
$this->assertSame('Ja', $submission->display('zustimmung'));
|
||||
$this->assertSame('Angebot.pdf', $submission->display('nachweis'));
|
||||
Storage::disk('local')->assertExists($submission->files->sole()->path);
|
||||
|
||||
Mail::assertQueued(FormSubmitted::class, fn (FormSubmitted $m) => $m->hasTo('personal@firma.de') && ! $m->copy && $m->hasReplyTo('anna@firma.de'));
|
||||
Mail::assertQueued(FormSubmitted::class, fn (FormSubmitted $m) => $m->hasTo($hr->email) && ! $m->copy);
|
||||
Mail::assertQueued(FormSubmitted::class, fn (FormSubmitted $m) => $m->hasTo('anna@firma.de') && $m->copy);
|
||||
Notification::assertSentTo($hr, PortalNotification::class);
|
||||
|
||||
$html = (new FormSubmitted($form, $submission, $this->employee, false))->render();
|
||||
$this->assertStringContainsString('Anna Schmidt (anna@firma.de)', $html);
|
||||
$this->assertStringContainsString('Mo, Mi', $html);
|
||||
}
|
||||
|
||||
public function test_results_are_only_visible_to_roles_granted_for_this_form(): void
|
||||
{
|
||||
$training = $this->trainingForm();
|
||||
$other = Form::query()->create(['title' => 'Meldung an die MAV', 'fields' => [['type' => 'text', 'data' => ['label' => 'Anliegen']]]]);
|
||||
$training->submissions()->create(['user_id' => $this->employee->id, 'data' => ['name' => ['label' => 'Name', 'type' => 'text', 'value' => 'Anna Schmidt']]]);
|
||||
$other->submissions()->create(['data' => ['anliegen' => ['label' => 'Anliegen', 'type' => 'text', 'value' => 'Vertraulich']]]);
|
||||
|
||||
$hr = User::factory()->withRoles([$this->resultsRole($training)])->create();
|
||||
|
||||
$this->actingAs($hr)->get('/admin/formulare')->assertOk();
|
||||
Livewire::test(ListForms::class)->assertCanSeeTableRecords([$training])->assertCanNotSeeTableRecords([$other]);
|
||||
|
||||
$this->get('/admin/formulare/'.$training->id.'/ergebnisse')->assertOk()->assertSee('Anna Schmidt');
|
||||
$this->get('/admin/formulare/'.$other->id.'/ergebnisse')->assertNotFound();
|
||||
$this->get('/admin/formulare/'.$training->id.'/bearbeiten')->assertForbidden();
|
||||
$this->get('/admin/formulare/'.$other->id.'/export')->assertForbidden();
|
||||
|
||||
$csv = $this->get('/admin/formulare/'.$training->id.'/export')->assertOk()->streamedContent();
|
||||
$this->assertStringStartsWith("\xEF\xBB\xBF", $csv);
|
||||
$this->assertStringContainsString('Anna Schmidt', $csv);
|
||||
|
||||
// Ohne Löschrecht kein Löschen; ansehen markiert als gelesen.
|
||||
$submission = $training->submissions()->sole();
|
||||
Livewire::test(FormResults::class, ['record' => $training->id])
|
||||
->assertTableActionHidden('delete', $submission)
|
||||
->callTableAction('view', $submission);
|
||||
$this->assertNotNull($submission->fresh()->read_at);
|
||||
|
||||
$this->actingAs($this->employee)->get('/admin/formulare/'.$training->id.'/ergebnisse')->assertForbidden();
|
||||
}
|
||||
|
||||
public function test_uploaded_files_are_only_served_to_result_viewers(): void
|
||||
{
|
||||
$form = $this->trainingForm();
|
||||
$this->actingAs($this->employee)->post('/formulare/antrag-auf-fortbildung', ['answers' => [
|
||||
'name' => 'Anna', 'art' => 'Seminar', 'zustimmung' => '1',
|
||||
'nachweis' => [UploadedFile::fake()->create('Zeugnis.pdf', 50, 'application/pdf')],
|
||||
]])->assertSessionHasNoErrors();
|
||||
|
||||
$file = FormSubmission::query()->sole()->files->sole();
|
||||
|
||||
$this->get($file->url())->assertForbidden();
|
||||
$this->actingAs(User::factory()->withRoles([$this->resultsRole($form)])->create())->get($file->url())->assertOk()->assertDownload('Zeugnis.pdf');
|
||||
|
||||
$this->actingAs($this->employee)->post('/formulare/antrag-auf-fortbildung', ['answers' => [
|
||||
'name' => 'Anna', 'art' => 'Seminar', 'zustimmung' => '1',
|
||||
'nachweis' => [UploadedFile::fake()->create('virus.php', 1)],
|
||||
]])->assertSessionHasErrors('answers.nachweis.0');
|
||||
}
|
||||
|
||||
public function test_anonymous_forms_store_no_identity_but_allow_only_one_submission(): void
|
||||
{
|
||||
$form = Form::query()->create([
|
||||
'title' => 'Mitarbeiterbefragung',
|
||||
'fields' => [['type' => 'radio', 'data' => ['label' => 'Zufriedenheit', 'required' => true, 'options' => ['gut', 'mittel', 'schlecht']]]],
|
||||
'settings' => ['anonymous' => true, 'once_per_user' => true, 'notify_emails' => ['mav@firma.de']],
|
||||
]);
|
||||
|
||||
$this->actingAs($this->employee)->post($form->url(), ['answers' => ['zufriedenheit' => 'gut']])->assertSessionHasNoErrors();
|
||||
$this->post($form->url(), ['answers' => ['zufriedenheit' => 'schlecht']])->assertSessionHasErrors('form');
|
||||
|
||||
$submission = FormSubmission::query()->sole();
|
||||
$this->assertNull($submission->user_id);
|
||||
$this->assertNotNull($submission->user_hash);
|
||||
Mail::assertQueued(FormSubmitted::class, fn (FormSubmitted $m) => $m->person === null);
|
||||
|
||||
$this->get($form->url())->assertInertia(fn (Assert $p) => $p->where('form.already_submitted', true)->where('form.anonymous', true));
|
||||
}
|
||||
|
||||
public function test_audience_and_deadline_limit_who_can_submit(): void
|
||||
{
|
||||
$form = $this->trainingForm(attributes: ['closes_at' => now()->addDay()]);
|
||||
Audiences::sync('form', $form->id, ['department' => ['Verwaltung']]);
|
||||
|
||||
$this->actingAs($this->employee)->get($form->url())->assertNotFound();
|
||||
$this->post($form->url(), ['answers' => []])->assertNotFound();
|
||||
$this->get('/formulare')->assertInertia(fn (Assert $p) => $p->where('forms', []));
|
||||
|
||||
Audiences::sync('form', $form->id, []);
|
||||
$this->travel(2)->days();
|
||||
$this->post($form->url(), ['answers' => ['name' => 'x', 'art' => 'Seminar', 'zustimmung' => '1']])->assertSessionHasErrors('form');
|
||||
$this->get('/formulare')->assertInertia(fn (Assert $p) => $p->where('forms', []));
|
||||
}
|
||||
|
||||
public function test_confidential_forms_leave_answers_out_of_the_mail(): void
|
||||
{
|
||||
$form = $this->trainingForm(['mail_answers' => false]);
|
||||
$submission = $form->submissions()->create(['user_id' => $this->employee->id, 'data' => ['name' => ['label' => 'Name', 'type' => 'text', 'value' => 'Geheim']]]);
|
||||
|
||||
$html = (new FormSubmitted($form, $submission, $this->employee, false))->render();
|
||||
|
||||
$this->assertStringNotContainsString('Geheim', $html);
|
||||
$this->assertStringContainsString('/admin/formulare/'.$form->id.'/ergebnisse', $html);
|
||||
}
|
||||
|
||||
public function test_retention_deletes_old_submissions_with_files(): void
|
||||
{
|
||||
$form = $this->trainingForm(['retention_days' => 30]);
|
||||
$old = $form->submissions()->create(['data' => []]);
|
||||
$old->forceFill(['created_at' => now()->subDays(40)])->save();
|
||||
Storage::disk('local')->put('forms/'.$form->id.'/alt.pdf', 'x');
|
||||
$old->files()->create(['field' => 'nachweis', 'name' => 'alt.pdf', 'path' => 'forms/'.$form->id.'/alt.pdf']);
|
||||
$form->submissions()->create(['data' => []]);
|
||||
|
||||
$this->assertSame(1, app(Retention::class)->purge());
|
||||
$this->assertSame(1, $form->submissions()->count());
|
||||
Storage::disk('local')->assertMissing('forms/'.$form->id.'/alt.pdf');
|
||||
}
|
||||
|
||||
public function test_admin_builds_a_form_in_the_backend(): void
|
||||
{
|
||||
$this->actingAs(User::factory()->admin()->create());
|
||||
$hr = Role::query()->create(['name' => 'Personal']);
|
||||
|
||||
Livewire::test(CreateForm::class)
|
||||
->fillForm([
|
||||
'title' => 'Urlaubsantrag',
|
||||
'fields' => [
|
||||
['type' => 'date', 'data' => ['label' => 'Von', 'required' => true, 'width' => 'half']],
|
||||
['type' => 'date', 'data' => ['label' => 'Bis', 'required' => true, 'width' => 'half']],
|
||||
],
|
||||
'settings' => ['notify_emails' => ['personal@firma.de'], 'mail_answers' => true],
|
||||
'result_viewers' => [$hr->id],
|
||||
])
|
||||
->call('create')
|
||||
->assertHasNoFormErrors();
|
||||
|
||||
$form = Form::query()->sole();
|
||||
$this->assertSame(['von', 'bis'], array_column(array_column($form->fields, 'data'), 'key'));
|
||||
$this->assertSame(['personal@firma.de'], $form->setting('notify_emails'));
|
||||
$this->assertTrue(User::factory()->withRoles([$hr])->create()->can('viewResults', $form));
|
||||
}
|
||||
|
||||
public function test_form_block_in_pages(): void
|
||||
{
|
||||
$form = $this->trainingForm();
|
||||
$content = app(Content::class);
|
||||
$rendered = $content->render($content->sanitize(['blocks' => [['type' => 'forms.form', 'data' => ['form' => (string) $form->id]]]]), $this->employee);
|
||||
|
||||
$this->assertSame('Antrag auf Fortbildung', $rendered['blocks'][0]['props']['title']);
|
||||
$this->assertSame('forms::FormBlock', $rendered['blocks'][0]['component']);
|
||||
|
||||
Audiences::sync('form', $form->id, ['department' => ['Verwaltung']]);
|
||||
$this->assertSame([], $content->render($content->sanitize(['blocks' => [['type' => 'forms.form', 'data' => ['form' => (string) $form->id]]]]), $this->employee)['blocks']);
|
||||
}
|
||||
}
|
||||
|
|
@ -77,7 +77,7 @@ class PeopleDirectoryTest extends TestCase
|
|||
->get('/personen')
|
||||
->assertInertia(fn (Assert $page) => $page
|
||||
->where('navigation.0.active', false)
|
||||
->where('navigation.3.label', 'Personen')
|
||||
->where('navigation.3.active', true));
|
||||
->where('navigation', fn ($items) => collect($items)->firstWhere('label', 'Personen')['active'] === true
|
||||
&& collect($items)->where('active', true)->count() === 1));
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -22,7 +22,7 @@ class CorePluginToggleTest extends TestCase
|
|||
|
||||
$file = storage_path('app/plugins.json');
|
||||
$this->previousState = is_file($file) ? file_get_contents($file) : null;
|
||||
file_put_contents($file, json_encode(['news' => ['enabled' => false], 'events' => ['enabled' => false], 'pages' => ['enabled' => false]]));
|
||||
file_put_contents($file, json_encode(['news' => ['enabled' => false], 'events' => ['enabled' => false], 'pages' => ['enabled' => false], 'forms' => ['enabled' => false], 'wordpress-import' => ['enabled' => false]]));
|
||||
|
||||
// Plugins werden beim Start der Anwendung geladen – also neu starten.
|
||||
$this->refreshApplication();
|
||||
|
|
|
|||
|
|
@ -122,15 +122,17 @@ class NewsTest extends TestCase
|
|||
Storage::disk('uploads')->put('news/handyfoto.jpg', UploadedFile::fake()->image('handyfoto.jpg', 4000, 3000)->getContent());
|
||||
gc_collect_cycles();
|
||||
|
||||
// … dann wie auf typischem Shared Hosting mit 128 MB verarbeiten.
|
||||
$this->assertNotFalse(ini_set('memory_limit', '128M'));
|
||||
// … dann wie auf typischem Shared Hosting mit 128 MB verarbeiten. Der Test-Prozess
|
||||
// selbst belegt schon Speicher (viele Tests in einem Lauf), daher 128 MB zusätzlich dazu.
|
||||
$limit = memory_get_usage(true) + 128 * 1024 * 1024;
|
||||
$this->assertNotFalse(ini_set('memory_limit', (string) $limit));
|
||||
|
||||
NewsPost::factory()->create(['cover_path' => 'news/handyfoto.jpg']);
|
||||
|
||||
Storage::disk('uploads')->assertExists('news/handyfoto-large.webp');
|
||||
Storage::disk('uploads')->assertExists('news/handyfoto-thumb.webp');
|
||||
|
||||
ini_set('memory_limit', '128M');
|
||||
ini_set('memory_limit', '512M');
|
||||
}
|
||||
|
||||
public function test_dashboard_shows_featured_and_latest_news(): void
|
||||
|
|
|
|||
174
tests/Feature/WordpressImport/WordpressImportTest.php
Normal file
174
tests/Feature/WordpressImport/WordpressImportTest.php
Normal file
|
|
@ -0,0 +1,174 @@
|
|||
<?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->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', '<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);
|
||||
}
|
||||
}
|
||||
111
tests/Fixtures/wordpress-export.xml
Normal file
111
tests/Fixtures/wordpress-export.xml
Normal file
|
|
@ -0,0 +1,111 @@
|
|||
<?xml version="1.0" encoding="UTF-8" ?>
|
||||
<rss version="2.0"
|
||||
xmlns:excerpt="http://wordpress.org/export/1.2/excerpt/"
|
||||
xmlns:content="http://purl.org/rss/1.0/modules/content/"
|
||||
xmlns:wfw="http://wellformedweb.org/CommentAPI/"
|
||||
xmlns:dc="http://purl.org/dc/elements/1.1/"
|
||||
xmlns:wp="http://wordpress.org/export/1.2/"
|
||||
>
|
||||
<channel>
|
||||
<title>Altes Intranet</title>
|
||||
<link>https://intranet.example.org</link>
|
||||
<wp:wxr_version>1.2</wp:wxr_version>
|
||||
<wp:base_site_url>https://intranet.example.org</wp:base_site_url>
|
||||
<wp:base_blog_url>https://intranet.example.org</wp:base_blog_url>
|
||||
<wp:author><wp:author_id>1</wp:author_id><wp:author_login><![CDATA[redaktion]]></wp:author_login><wp:author_email><![CDATA[Redaktion@Firma.de]]></wp:author_email><wp:author_display_name><![CDATA[Rita Redaktion]]></wp:author_display_name></wp:author>
|
||||
<wp:author><wp:author_id>2</wp:author_id><wp:author_login><![CDATA[ehemalig]]></wp:author_login><wp:author_email><![CDATA[weg@firma.de]]></wp:author_email><wp:author_display_name><![CDATA[Ehemalige Person]]></wp:author_display_name></wp:author>
|
||||
<wp:category><wp:term_id>3</wp:term_id><wp:category_nicename><![CDATA[mav-info]]></wp:category_nicename><wp:category_parent><![CDATA[]]></wp:category_parent><wp:cat_name><![CDATA[MAV-Info]]></wp:cat_name></wp:category>
|
||||
<wp:category><wp:term_id>4</wp:term_id><wp:category_nicename><![CDATA[aus-den-bereichen]]></wp:category_nicename><wp:category_parent><![CDATA[]]></wp:category_parent><wp:cat_name><![CDATA[Aus den Bereichen & Häusern]]></wp:cat_name></wp:category>
|
||||
<item>
|
||||
<title><![CDATA[sommerfest.jpg]]></title>
|
||||
<wp:post_id>10</wp:post_id>
|
||||
<wp:post_type><![CDATA[attachment]]></wp:post_type>
|
||||
<wp:status><![CDATA[inherit]]></wp:status>
|
||||
<wp:attachment_url><![CDATA[https://intranet.example.org/wp-content/uploads/2024/06/sommerfest.jpg]]></wp:attachment_url>
|
||||
</item>
|
||||
<item>
|
||||
<title><![CDATA[Sommerfest 2024 – Rückblick]]></title>
|
||||
<link>https://intranet.example.org/2024/06/sommerfest/</link>
|
||||
<dc:creator><![CDATA[redaktion]]></dc:creator>
|
||||
<wfw:commentRss>https://intranet.example.org/2024/06/sommerfest/feed/</wfw:commentRss>
|
||||
<content:encoded><![CDATA[Das Sommerfest war ein voller Erfolg.
|
||||
Vielen Dank an alle Helfer!
|
||||
|
||||
[caption id="attachment_11" align="aligncenter" width="800"]<a href="https://intranet.example.org/wp-content/uploads/2024/06/buffet.jpg"><img class="wp-image-11" src="https://intranet.example.org/wp-content/uploads/2024/06/buffet-800x600.jpg" alt="Buffet" width="800" height="600" srcset="x 1x" /></a> Das Buffet im Innenhof[/caption]
|
||||
|
||||
<h2>Nächstes Jahr</h2>
|
||||
[gallery ids="10"]
|
||||
[contact-form-7 id="5"]
|
||||
<script>alert(1)</script>]]></content:encoded>
|
||||
<excerpt:encoded><![CDATA[]]></excerpt:encoded>
|
||||
<wp:post_id>20</wp:post_id>
|
||||
<wp:post_date_gmt><![CDATA[2024-06-21 16:30:00]]></wp:post_date_gmt>
|
||||
<wp:post_name><![CDATA[sommerfest]]></wp:post_name>
|
||||
<wp:status><![CDATA[publish]]></wp:status>
|
||||
<wp:post_parent>0</wp:post_parent>
|
||||
<wp:post_type><![CDATA[post]]></wp:post_type>
|
||||
<category domain="category" nicename="aus-den-bereichen"><![CDATA[Aus den Bereichen & Häusern]]></category>
|
||||
<category domain="post_tag" nicename="feier"><![CDATA[Feier]]></category>
|
||||
<wp:postmeta><wp:meta_key><![CDATA[_thumbnail_id]]></wp:meta_key><wp:meta_value><![CDATA[10]]></wp:meta_value></wp:postmeta>
|
||||
</item>
|
||||
<item>
|
||||
<title><![CDATA[Neue Dienstvereinbarung]]></title>
|
||||
<dc:creator><![CDATA[ehemalig]]></dc:creator>
|
||||
<content:encoded><![CDATA[<!-- wp:paragraph -->
|
||||
<p>Die neue <strong>Dienstvereinbarung</strong> gilt ab sofort.</p>
|
||||
<!-- /wp:paragraph -->
|
||||
|
||||
<!-- wp:image {"id":12} -->
|
||||
<figure class="wp-block-image"><img src="/wp-content/uploads/2024/07/dv.png" alt="DV"/><figcaption>Unterschrift</figcaption></figure>
|
||||
<!-- /wp:image -->]]></content:encoded>
|
||||
<excerpt:encoded><![CDATA[Kurz zusammengefasst.]]></excerpt:encoded>
|
||||
<wp:post_id>21</wp:post_id>
|
||||
<wp:post_date_gmt><![CDATA[2024-07-01 08:00:00]]></wp:post_date_gmt>
|
||||
<wp:post_name><![CDATA[neue-dienstvereinbarung]]></wp:post_name>
|
||||
<wp:status><![CDATA[publish]]></wp:status>
|
||||
<wp:post_type><![CDATA[post]]></wp:post_type>
|
||||
<category domain="category" nicename="mav-info"><![CDATA[MAV-Info]]></category>
|
||||
</item>
|
||||
<item>
|
||||
<title><![CDATA[Entwurf: Weihnachtsfeier]]></title>
|
||||
<dc:creator><![CDATA[redaktion]]></dc:creator>
|
||||
<content:encoded><![CDATA[Noch in Arbeit]]></content:encoded>
|
||||
<wp:post_id>22</wp:post_id>
|
||||
<wp:post_date_gmt><![CDATA[0000-00-00 00:00:00]]></wp:post_date_gmt>
|
||||
<wp:post_name><![CDATA[]]></wp:post_name>
|
||||
<wp:status><![CDATA[draft]]></wp:status>
|
||||
<wp:post_type><![CDATA[post]]></wp:post_type>
|
||||
</item>
|
||||
<item>
|
||||
<title><![CDATA[Privat]]></title>
|
||||
<content:encoded><![CDATA[geheim]]></content:encoded>
|
||||
<wp:post_id>23</wp:post_id>
|
||||
<wp:status><![CDATA[private]]></wp:status>
|
||||
<wp:post_type><![CDATA[post]]></wp:post_type>
|
||||
</item>
|
||||
<item>
|
||||
<title><![CDATA[Wahlergebnis]]></title>
|
||||
<content:encoded><![CDATA[<table><tr><td>A</td><td>12</td></tr></table>]]></content:encoded>
|
||||
<wp:post_id>31</wp:post_id>
|
||||
<wp:post_name><![CDATA[wahlergebnis]]></wp:post_name>
|
||||
<wp:status><![CDATA[publish]]></wp:status>
|
||||
<wp:post_parent>30</wp:post_parent>
|
||||
<wp:post_type><![CDATA[page]]></wp:post_type>
|
||||
</item>
|
||||
<item>
|
||||
<title><![CDATA[Mitarbeitervertretung]]></title>
|
||||
<content:encoded><![CDATA[Wir sind für Sie da.]]></content:encoded>
|
||||
<wp:post_id>30</wp:post_id>
|
||||
<wp:post_name><![CDATA[mav]]></wp:post_name>
|
||||
<wp:status><![CDATA[publish]]></wp:status>
|
||||
<wp:post_parent>0</wp:post_parent>
|
||||
<wp:post_type><![CDATA[page]]></wp:post_type>
|
||||
</item>
|
||||
<item>
|
||||
<title><![CDATA[Menüeintrag]]></title>
|
||||
<wp:post_id>40</wp:post_id>
|
||||
<wp:status><![CDATA[publish]]></wp:status>
|
||||
<wp:post_type><![CDATA[nav_menu_item]]></wp:post_type>
|
||||
</item>
|
||||
</channel>
|
||||
</rss>
|
||||
Loading…
Add table
Add a link
Reference in a new issue