fix(16.2): CR-01 serve content_html when a locale's content is blank

A translation that carries only content_html (an API write through
FormatTranslation) opened as an empty editor, and the first edit in
that locale silently replaced the translated body. Load now serves the
htmlColumn attribute whenever the stored content is blank and that
attribute is not empty; an untouched save echoes it and is skipped, an
edit writes the edited HTML to both columns.
This commit is contained in:
2026-10-10 16:33:08 +02:00
parent 85963b19ef
commit 159323197d
3 changed files with 109 additions and 6 deletions

View File

@@ -236,6 +236,45 @@ func TestBlogEditorTracerSmoke(t *testing.T) {
}
})
// CR-01: a translation that carries only content_html opens with that
// body. An untouched save echoes the served value, which cabana skips;
// an edit writes the edited HTML to both content and content_html.
t.Run("blank content opens with content_html and an edit writes both columns", func(t *testing.T) {
const apiBody = "<h2>Translated</h2>\n<p>Written through the API.</p>\n"
served := load(t, "", func(attribute string) (string, bool, error) {
if attribute != "content_html" {
t.Fatalf("Translated(%q), want content_html", attribute)
}
return apiBody, true, nil
})
if served != apiBody {
t.Fatalf("Load(blank content) = %q, want the stored content_html", served)
}
const edited = "<h2>Translated</h2>\n<p>Edited in the admin.</p>\n"
decoded, err := ml.Decode(context.Background(), pact.FormFieldValue{Field: "content", Locale: "en", Value: edited})
if err != nil || decoded.Value != edited {
t.Fatalf("Decode(edited) = %#v, %v", decoded, err)
}
writer := &recordingWriter{}
app := backpack.New(nil)
if err := app.Publish[cabana.TranslationWriter](writer); err != nil {
t.Fatal(err)
}
ctl := postsAdminController{app: func() *backpack.App { return app }}
ctx := bouncer.WithUser(context.Background(), editorPrincipal())
ctx = cabana.WithFieldWrites(ctx, map[string][]cabana.FieldWrite{
"content": {{Locale: "en", Value: decoded.Value}},
})
post := &models.Post{Content: "<p>Host</p>", ContentHTML: "<p>Host</p>"}
if err := ctl.FormAfterUpdate(ctx, post); err != nil {
t.Fatal(err)
}
if len(writer.writes) != 1 || writer.writes[0] != (recordedWrite{field: "content_html", locale: "en", value: edited}) {
t.Fatalf("translation writes = %#v, want en content_html = the edited value", writer.writes)
}
})
t.Run("list value", func(t *testing.T) {
got := ml.ListValue(context.Background(), "<h2>Title</h2><p>Some <strong>bold</strong>\n text</p><style>p{}</style>")
if got != "Title Some bold text" {