From 2b5fe55f8ea6cd9f045d0cd058fe9fbd25487824 Mon Sep 17 00:00:00 2001 From: Friederich Loheide Date: Sat, 26 Sep 2026 11:22:46 +0000 Subject: [PATCH] =?UTF-8?q?MySQL:=20JSON-Schl=C3=BCsselreihenfolge=20darf?= =?UTF-8?q?=20keine=20Rolle=20spielen?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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) --- app/Pagebuilder/Content.php | 25 ++++++++++ .../Resources/Forms/Pages/FormResults.php | 2 +- plugins-core/forms/src/Mail/FormSubmitted.php | 2 +- .../forms/src/Models/FormSubmission.php | 15 ++++++ .../Http/Controllers/PageEditorController.php | 2 +- .../Identity/EntraAttributeMappingTest.php | 2 +- tests/Feature/Update/UpdaterTest.php | 2 +- .../WordpressImport/WordpressImportTest.php | 2 +- tests/Unit/JsonKeyOrderTest.php | 46 +++++++++++++++++++ 9 files changed, 92 insertions(+), 6 deletions(-) create mode 100644 tests/Unit/JsonKeyOrderTest.php diff --git a/app/Pagebuilder/Content.php b/app/Pagebuilder/Content.php index 4bbc50f1..85b882b7 100644 --- a/app/Pagebuilder/Content.php +++ b/app/Pagebuilder/Content.php @@ -78,6 +78,31 @@ class Content return $type?->resolve ? $this->resolve($type, $clean, $user, $context) : []; } + /** + * Gleicher Inhalt? Unabhängig von der Reihenfolge der Schlüssel – MySQL + * sortiert Schlüssel in JSON-Spalten um, MariaDB nicht. + * + * @param array|null $a + * @param array|null $b + */ + public static function same(?array $a, ?array $b): bool + { + return self::canonical($a) === self::canonical($b); + } + + private static function canonical(mixed $value): mixed + { + if (! is_array($value)) { + return $value; + } + + if (! array_is_list($value)) { + ksort($value); + } + + return array_map(self::canonical(...), $value); + } + /** * Alle Werte eines Feldtyps im Baum, z. B. alle Bildpfade (für Aufräumarbeiten). * diff --git a/plugins-core/forms/src/Filament/Resources/Forms/Pages/FormResults.php b/plugins-core/forms/src/Filament/Resources/Forms/Pages/FormResults.php index 871e0456..5d70289c 100644 --- a/plugins-core/forms/src/Filament/Resources/Forms/Pages/FormResults.php +++ b/plugins-core/forms/src/Filament/Resources/Forms/Pages/FormResults.php @@ -120,7 +120,7 @@ class FormResults extends Page implements HasTable $rows .= self::row('Person', e($submission->user ? $submission->user->name.' ('.$submission->user->email.')' : 'gelöschtes Konto')); } - foreach ($submission->data as $key => $answer) { + foreach ($submission->orderedAnswers() as $key => $answer) { if (($answer['type'] ?? null) === 'file') { $value = collect($submission->files->where('field', $key)) ->map(fn ($file) => ''.e($file->name).'') diff --git a/plugins-core/forms/src/Mail/FormSubmitted.php b/plugins-core/forms/src/Mail/FormSubmitted.php index 0276bbf2..2bfc6baa 100644 --- a/plugins-core/forms/src/Mail/FormSubmitted.php +++ b/plugins-core/forms/src/Mail/FormSubmitted.php @@ -44,7 +44,7 @@ class FormSubmitted extends Mailable implements ShouldQueue return new Content(view: 'forms::mail.submitted', with: [ 'portalName' => app(Branding::class)->portalName(), - 'answers' => $includeAnswers ? collect($this->submission->data)->map(fn ($answer, $key) => [ + 'answers' => $includeAnswers ? collect($this->submission->orderedAnswers())->map(fn ($answer, $key) => [ 'label' => $answer['label'], 'value' => $this->submission->display($key), ])->values()->all() : null, diff --git a/plugins-core/forms/src/Models/FormSubmission.php b/plugins-core/forms/src/Models/FormSubmission.php index bb92c103..cdd43b3c 100644 --- a/plugins-core/forms/src/Models/FormSubmission.php +++ b/plugins-core/forms/src/Models/FormSubmission.php @@ -8,6 +8,7 @@ use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\Relations\BelongsTo; use Illuminate\Database\Eloquent\Relations\HasMany; use Illuminate\Support\Carbon; +use Inhaus\Plugins\Forms\FormFields; /** * Ein ausgefülltes Formular. „data“ enthält je Feld Bezeichnung, Typ und Wert – @@ -50,6 +51,20 @@ class FormSubmission extends Model return $this->hasMany(FormFile::class, 'submission_id'); } + /** + * Antworten in der Reihenfolge des Formulars; Felder, die es nur noch in + * dieser Einsendung gibt, am Ende. (MySQL speichert JSON-Schlüssel sortiert.) + * + * @return array + */ + public function orderedAnswers(): array + { + $data = (array) $this->data; + $order = array_keys(FormFields::inputs($this->form)); + + return array_replace(array_intersect_key(array_flip($order), $data), $data); + } + /** * Wert als Text (Listen mit Komma, Ja/Nein, Dateinamen). */ diff --git a/plugins-core/pages/src/Http/Controllers/PageEditorController.php b/plugins-core/pages/src/Http/Controllers/PageEditorController.php index 91b80bfc..bfdfe1f4 100644 --- a/plugins-core/pages/src/Http/Controllers/PageEditorController.php +++ b/plugins-core/pages/src/Http/Controllers/PageEditorController.php @@ -97,7 +97,7 @@ class PageEditorController extends Controller [$title, $content] = $this->validatedWorkingCopy($request); // Stimmt der Stand mit dem veröffentlichten überein, gibt es keinen Entwurf. - $unchanged = $page->isPublished() && $title === $page->title && $content === $page->content; + $unchanged = $page->isPublished() && $title === $page->title && Content::same($content, $page->content); $page->forceFill([ 'draft_title' => $unchanged ? null : $title, diff --git a/tests/Feature/Identity/EntraAttributeMappingTest.php b/tests/Feature/Identity/EntraAttributeMappingTest.php index 410f9d12..0846e7fa 100644 --- a/tests/Feature/Identity/EntraAttributeMappingTest.php +++ b/tests/Feature/Identity/EntraAttributeMappingTest.php @@ -77,7 +77,7 @@ class EntraAttributeMappingTest extends TestCase $user = User::query()->sole(); $this->assertSame('Pflege', $user->job_title, 'Kernfelder lassen sich auf andere Attribute umlegen.'); $this->assertNull($user->department, 'Nicht zugeordnete Kernfelder bleiben unberührt.'); - $this->assertSame([ + $this->assertEquals([ 'personalnummer' => 'P-4711', 'kostenstelle' => 'KST 300', 'eintritt' => '2019-04-01', diff --git a/tests/Feature/Update/UpdaterTest.php b/tests/Feature/Update/UpdaterTest.php index 1af249e4..be74c421 100644 --- a/tests/Feature/Update/UpdaterTest.php +++ b/tests/Feature/Update/UpdaterTest.php @@ -107,7 +107,7 @@ class UpdaterTest extends TestCase $audit = AuditLog::query()->where('action', 'update.installed')->firstOrFail(); $this->assertSame($admin->id, $audit->user_id); - $this->assertSame(['channel' => 'stable', 'sha' => self::SHA, 'migrations' => 0, 'files' => 1], $audit->data); + $this->assertEquals(['channel' => 'stable', 'sha' => self::SHA, 'migrations' => 0, 'files' => 1], $audit->data); } public function test_error_page_instead_of_zip_is_rejected(): void diff --git a/tests/Feature/WordpressImport/WordpressImportTest.php b/tests/Feature/WordpressImport/WordpressImportTest.php index 3cde7010..d76053d4 100644 --- a/tests/Feature/WordpressImport/WordpressImportTest.php +++ b/tests/Feature/WordpressImport/WordpressImportTest.php @@ -101,7 +101,7 @@ class WordpressImportTest extends TestCase $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']))); + $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'); } diff --git a/tests/Unit/JsonKeyOrderTest.php b/tests/Unit/JsonKeyOrderTest.php new file mode 100644 index 00000000..14befae0 --- /dev/null +++ b/tests/Unit/JsonKeyOrderTest.php @@ -0,0 +1,46 @@ + [['id' => 'abcd1234', 'type' => 'heading', 'data' => ['text' => 'Hallo', 'level' => '2', 'align' => 'left']]]]; + $b = ['blocks' => [['data' => ['align' => 'left', 'level' => '2', 'text' => 'Hallo'], 'type' => 'heading', 'id' => 'abcd1234']]]; + $c = ['blocks' => [['id' => 'abcd1234', 'type' => 'heading', 'data' => ['text' => 'Anders', 'level' => '2', 'align' => 'left']]]]; + $swapped = ['blocks' => array_reverse([...$a['blocks'], ...$c['blocks']])]; + + $this->assertTrue(Content::same($a, $b)); + $this->assertFalse(Content::same($a, $c)); + $this->assertFalse(Content::same(['blocks' => [...$a['blocks'], ...$c['blocks']]], $swapped), 'Die Reihenfolge von Blöcken zählt.'); + } + + public function test_form_answers_follow_the_form_order(): void + { + $form = new Form(['fields' => [ + ['type' => 'text', 'data' => ['label' => 'Name', 'key' => 'name']], + ['type' => 'text', 'data' => ['label' => 'Abteilung', 'key' => 'abteilung']], + ['type' => 'textarea', 'data' => ['label' => 'Begründung', 'key' => 'begruendung']], + ]]); + + $submission = new FormSubmission(['data' => [ + 'altfeld' => ['label' => 'Altes Feld', 'type' => 'text', 'value' => 'x'], + 'begruendung' => ['label' => 'Begründung', 'type' => 'textarea', 'value' => 'weil'], + 'name' => ['label' => 'Name', 'type' => 'text', 'value' => 'Anna'], + 'abteilung' => ['label' => 'Abteilung', 'type' => 'text', 'value' => 'Pflege'], + ]]); + $submission->setRelation('form', $form); + + $this->assertSame(['name', 'abteilung', 'begruendung', 'altfeld'], array_keys($submission->orderedAnswers())); + } +}