feat(admin): post content on the Blog Editor field type
- blogeditor and mlblogeditor field types (classes.BlogEditorTypes) on the plugin; post content is mlblogeditor with htmlColumn content_html - Decode stores SanitizeHTMLReport output per locale with notices naming what was removed; clean HTML is stored byte for byte, nothing is refused because of markup - Load serves HTML as stored and markdown posts as their content_html - the admin hooks copy written content to content_html per locale without goldmark; untouched content leaves both columns as they are - editor lang keys (en, pl plural notices), tracer smoke test, updated Postgres admin tests
This commit is contained in:
@@ -4,6 +4,7 @@ import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
@@ -16,8 +17,8 @@ func TestPostsFormCompiles(t *testing.T) {
|
||||
token := env.admin(t, controllers.PermissionAccessPosts)
|
||||
rec := env.expect(t, http.StatusOK, http.MethodGet, adminAPI(adminPostsPath+"/schema/form"), token, nil)
|
||||
body := rec.Body.String()
|
||||
if !strings.Contains(body, "mlmarkdown") {
|
||||
t.Fatalf("form schema missing mlmarkdown: %s", body)
|
||||
if !strings.Contains(body, `"type":"mlblogeditor"`) {
|
||||
t.Fatalf("form schema missing mlblogeditor: %s", body)
|
||||
}
|
||||
if !strings.Contains(body, "mltext") {
|
||||
t.Fatalf("form schema missing mltext: %s", body)
|
||||
@@ -40,9 +41,12 @@ func TestPostsAdminCreateSmoke(t *testing.T) {
|
||||
rec := env.expect(t, http.StatusCreated, http.MethodPost, adminAPI(adminPostsPath), privileged, map[string]any{
|
||||
"title": map[string]any{"en": "Hello", "pl": "Witaj"},
|
||||
"slug": map[string]any{"en": "hello-admin", "pl": "witaj-admin"},
|
||||
"content": map[string]any{"en": "Hello **world**", "pl": "Witaj **świecie**"},
|
||||
"content": map[string]any{"en": "<p>Hello <strong>world</strong></p>", "pl": "<p>Witaj <strong>świecie</strong></p>"},
|
||||
"excerpt": map[string]any{"en": "Excerpt", "pl": "Wyjątek"},
|
||||
})
|
||||
if notices := adminNotices(t, rec); notices != nil {
|
||||
t.Fatalf("clean create carries meta.notices: %s", rec.Body.String())
|
||||
}
|
||||
var created struct {
|
||||
ID uint `json:"id"`
|
||||
}
|
||||
@@ -60,14 +64,9 @@ func TestPostsAdminCreateSmoke(t *testing.T) {
|
||||
if stored.Slug != "hello-admin" {
|
||||
t.Fatalf("host slug = %q, want hello-admin", stored.Slug)
|
||||
}
|
||||
if stored.ContentHTML == "" {
|
||||
t.Fatal("content_html is empty")
|
||||
}
|
||||
if strings.Contains(strings.ToLower(stored.ContentHTML), "<script") {
|
||||
t.Fatalf("content_html contains script: %s", stored.ContentHTML)
|
||||
}
|
||||
if !strings.Contains(stored.ContentHTML, "<strong>") && !strings.Contains(stored.ContentHTML, "<p>") {
|
||||
t.Fatalf("content_html missing markup: %s", stored.ContentHTML)
|
||||
// D-02: the admin path stores the same HTML in both columns.
|
||||
if stored.Content != "<p>Hello <strong>world</strong></p>" || stored.ContentHTML != stored.Content {
|
||||
t.Fatalf("content = %q, content_html = %q, want both the submitted HTML", stored.Content, stored.ContentHTML)
|
||||
}
|
||||
|
||||
var plCount int64
|
||||
@@ -139,8 +138,8 @@ func TestPostsAdminResaveKeepsHTML(t *testing.T) {
|
||||
if strings.Contains(stored.ContentHTML, "raw HTML omitted") {
|
||||
t.Fatalf("%s: content_html holds the goldmark placeholder: %q", stage, stored.ContentHTML)
|
||||
}
|
||||
if strings.TrimSpace(stored.ContentHTML) != content {
|
||||
t.Fatalf("%s: content_html = %q, want %q", stage, strings.TrimSpace(stored.ContentHTML), content)
|
||||
if stored.ContentHTML != content {
|
||||
t.Fatalf("%s: content_html = %q, want %q", stage, stored.ContentHTML, content)
|
||||
}
|
||||
if stored.Content != content {
|
||||
t.Fatalf("%s: content = %q, want %q", stage, stored.Content, content)
|
||||
@@ -152,10 +151,12 @@ func TestPostsAdminResaveKeepsHTML(t *testing.T) {
|
||||
assertHTML("re-save")
|
||||
}
|
||||
|
||||
// TestPostsAdminSaveSanitizesHTML pins CR-01 and WR-01 on the admin write
|
||||
// path: unsafe markup in content is stripped from content_html, as Winter's
|
||||
// Html::clean strips it, instead of refusing the save, and a "Data:" label is
|
||||
// ordinary text. content itself is stored as submitted.
|
||||
// TestPostsAdminSaveSanitizesHTML pins CR-01, WR-01 and Phase 16.2 D-02 and
|
||||
// D-10 on the admin write path: unsafe markup in content is stripped, as
|
||||
// Winter's Html::clean strips it, instead of refusing the save; content and
|
||||
// content_html both hold the sanitized HTML for the host locale (en) and the
|
||||
// translation (pl); a "Data:" label is ordinary text; and the response's
|
||||
// meta.notices names what was removed for each locale.
|
||||
func TestPostsAdminSaveSanitizesHTML(t *testing.T) {
|
||||
env := newPostsEnv(t)
|
||||
token := env.admin(t,
|
||||
@@ -184,11 +185,68 @@ func TestPostsAdminSaveSanitizesHTML(t *testing.T) {
|
||||
}
|
||||
const want = "<p>Data: 12 maja 2024</p>\n" +
|
||||
"<p><img src=\"https://cdn.example.test/a.jpg\" alt=\"a\"></p>\n" +
|
||||
"<p>x</p>"
|
||||
if got := strings.TrimSpace(stored.ContentHTML); got != want {
|
||||
t.Fatalf("content_html = %q, want %q", got, want)
|
||||
"<p>x</p>\n"
|
||||
if stored.Content != want || stored.ContentHTML != want {
|
||||
t.Fatalf("host content = %q, content_html = %q, want both %q", stored.Content, stored.ContentHTML, want)
|
||||
}
|
||||
if stored.Content != content {
|
||||
t.Fatalf("content = %q, want it stored as submitted", stored.Content)
|
||||
var raw string
|
||||
if err := env.gdb.Table("golem15_translate_attributes").
|
||||
Select("attribute_data").
|
||||
Where("model_id = ? AND model_type = ? AND locale = ?", fmt.Sprint(stored.ID), stored.MorphName(), "pl").
|
||||
Scan(&raw).Error; err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
var pl map[string]any
|
||||
if err := json.Unmarshal([]byte(raw), &pl); err != nil {
|
||||
t.Fatalf("attribute_data %q: %v", raw, err)
|
||||
}
|
||||
if pl["content"] != want || pl["content_html"] != want {
|
||||
t.Fatalf("pl content = %#v, content_html = %#v, want both %q", pl["content"], pl["content_html"], want)
|
||||
}
|
||||
|
||||
notices := adminNotices(t, rec)
|
||||
for _, locale := range []string{"en", "pl"} {
|
||||
var elements, attributes bool
|
||||
for _, n := range notices {
|
||||
if n.Field != "content" || n.Locale != locale {
|
||||
continue
|
||||
}
|
||||
elements = elements || strings.Contains(n.Message, "2 elements were removed: a, base")
|
||||
attributes = attributes || strings.Contains(n.Message, "1 attribute was removed: onerror")
|
||||
}
|
||||
if !elements || !attributes {
|
||||
t.Fatalf("%s notices = %#v, want removed elements a, base and attribute onerror", locale, notices)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// adminNotice is one meta.notices entry of an admin save response.
|
||||
type adminNotice struct {
|
||||
Field string `json:"field"`
|
||||
Locale string `json:"locale"`
|
||||
Message string `json:"message"`
|
||||
}
|
||||
|
||||
// adminNotices returns a save response's meta.notices, nil when the key is
|
||||
// absent.
|
||||
func adminNotices(t *testing.T, rec *httptest.ResponseRecorder) []adminNotice {
|
||||
t.Helper()
|
||||
var body struct {
|
||||
Meta map[string]json.RawMessage `json:"meta"`
|
||||
}
|
||||
if err := json.Unmarshal(rec.Body.Bytes(), &body); err != nil {
|
||||
t.Fatalf("admin body %q: %v", rec.Body.String(), err)
|
||||
}
|
||||
raw, ok := body.Meta["notices"]
|
||||
if !ok {
|
||||
return nil
|
||||
}
|
||||
var notices []adminNotice
|
||||
if err := json.Unmarshal(raw, ¬ices); err != nil {
|
||||
t.Fatalf("meta.notices %s: %v", raw, err)
|
||||
}
|
||||
if notices == nil {
|
||||
notices = []adminNotice{}
|
||||
}
|
||||
return notices
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user