MySQL: JSON-Schlüsselreihenfolge darf keine Rolle spielen #9

Merged
friloo merged 1 commit from fix/mysql-json into main 2026-09-26 13:00:15 +00:00
9 changed files with 92 additions and 6 deletions

View file

@ -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<mixed>|null $a
* @param array<mixed>|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).
*

View file

@ -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) => '<a href="'.e($file->url()).'" style="color: rgb(var(--primary-600, 37 99 235)); text-decoration: underline">'.e($file->name).'</a>')

View file

@ -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,

View file

@ -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<string, array{label: string, type: string, value: mixed}>
*/
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).
*/

View file

@ -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,

View file

@ -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',

View file

@ -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

View file

@ -101,7 +101,7 @@ class WordpressImportTest extends TestCase
$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'])));
$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');
}

View file

@ -0,0 +1,46 @@
<?php
namespace Tests\Unit;
use App\Pagebuilder\Content;
use Inhaus\Plugins\Forms\Models\Form;
use Inhaus\Plugins\Forms\Models\FormSubmission;
use Tests\TestCase;
/**
* MySQL speichert Schlüssel in JSON-Spalten sortiert (MariaDB nicht). Nichts
* Fachliches darf von dieser Reihenfolge abhängen.
*/
class JsonKeyOrderTest extends TestCase
{
public function test_content_comparison_ignores_key_order(): void
{
$a = ['blocks' => [['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()));
}
}