feat(postcard): mail partials, built-in layouts and layout and partial overrides

- pact.HasMailPartials: plugins declare <plugin>::mail.partials.<name>,
  loaded from views/mail/partials/<name>.htm; HasMailTemplates is unchanged
- bodies and layouts call {{ partial "name" "key" value ... }}; a call in a
  Markdown body leaves a per-render random token swapped after goldmark, so
  raw HTML stays disabled for bodies
- built-in partials button, panel, table and subcopy, built-in layouts
  system and minimal next to the unchanged default, reachable by alias
  after a plugin's own aliases
- NewDBSource also reads customized and database-only layouts and
  customized partials; a layout's CSS goes before mail.css

Plan 35-01, task 3.
This commit is contained in:
Jakub Zych
2026-11-13 10:38:14 +01:00
parent 7e5d2c74af
commit 5df1ec27cf
17 changed files with 1273 additions and 71 deletions

View File

@@ -165,6 +165,16 @@ func bootMailApp(t *testing.T, gdb *gorm.DB) (*mailer, *MemoryDriver, *bytes.Buf
return m, drv, &logs
}
// appWith returns an app that publishes gdb.
func appWith(t *testing.T, gdb *gorm.DB) *backpack.App {
t.Helper()
app := backpack.New(nil)
if err := app.Publish(gdb); err != nil {
t.Fatal(err)
}
return app
}
// customizeWelcome turns the welcome template's index row into an override.
// layoutCode names the layout row to point at; empty leaves layout_id NULL.
func customizeWelcome(t *testing.T, gdb *gorm.DB, subject, body, layoutCode string) {
@@ -173,7 +183,7 @@ func customizeWelcome(t *testing.T, gdb *gorm.DB, subject, body, layoutCode stri
if layoutCode != "" {
var id int64
if err := gdb.Raw(`INSERT INTO `+LayoutsTable+` (code, source, name) VALUES (?, 'acme.blog', 'Blog')
ON CONFLICT (code) DO UPDATE SET name = EXCLUDED.name RETURNING id`, layoutCode).Scan(&id).Error; err != nil {
ON CONFLICT (code) DO UPDATE SET code = EXCLUDED.code RETURNING id`, layoutCode).Scan(&id).Error; err != nil {
t.Fatalf("layout row: %v", err)
}
layoutID = id
@@ -297,6 +307,62 @@ func TestMailOverrideSmoke(t *testing.T) {
}
})
t.Run("layouts_and_partials", func(t *testing.T) {
gdb := mailPostgres(t)
m, drv, logs := bootMailApp(t, gdb)
stmts := []string{
// A customized file layout, a database-only layout and an index
// row that is not customized.
`INSERT INTO ` + LayoutsTable + ` (code, source, name, content_html, content_text, content_css, is_custom) VALUES
('acme.blog::mail.layouts.blog', 'acme.blog', 'Blog', '<div class="custom-blog">{{ .Content }}</div><style>{{ .css }}</style>', '[custom] {{ .Content }}', '.custom{margin:0}', TRUE),
('promo', NULL, 'Promo', '<div class="promo">{{ .Content }}{{ partial "button" "url" "https://example.com" "body" "Shop" }}</div>', 'PROMO {{ .Content }}', '', TRUE),
('` + defaultLayoutName + `', 'postcard', 'Default', '<div class="mirror">{{ .Content }}</div>', 'MIRROR {{ .Content }}', '', FALSE)`,
// A customized built-in partial.
`INSERT INTO ` + PartialsTable + ` (code, source, name, content_html, content_text, is_custom) VALUES
('postcard::mail.partials.button', 'postcard', 'Button', '<a class="btn" href="{{ .url }}">{{ .body }}</a>', '{{ .body }}: {{ .url }}', TRUE),
('postcard::mail.partials.panel', 'postcard', 'Panel', 'mirror', 'mirror', FALSE)`,
}
for _, stmt := range stmts {
if err := gdb.Exec(stmt).Error; err != nil {
t.Fatal(err)
}
}
// With no template row the file template is sent in its customized layout.
sendWelcome(t, m, "")
got := lastMessage(t, drv)
if got.Subject != file.Subject || !strings.Contains(got.HTML, `<div class="custom-blog"><p>Hi <strong>Ada</strong>`) ||
!strings.Contains(got.HTML, "<style>.custom{margin:0}</style>") || !strings.HasPrefix(got.Text, "[custom] Hi **Ada**") {
t.Errorf("customized file layout: %+v", got)
}
// A template override on the database-only layout, which calls the
// customized partial.
customizeWelcome(t, gdb, "Sale", "Now.", "promo")
sendWelcome(t, m, "")
got = lastMessage(t, drv)
if got.Subject != "Sale" || !strings.Contains(got.HTML, `<div class="promo"><p>Now.</p>`+"\n"+`<a class="btn" href="https://example.com">Shop</a></div>`) {
t.Errorf("database-only layout: %+v", got)
}
if got.Text != "PROMO Now." {
t.Errorf("database-only layout: text = %q", got.Text)
}
// An empty template name asks for everything except a template override.
all, err := NewDBSource(appWith(t, gdb)).MailOverrides(context.Background(), "")
if err != nil {
t.Fatal(err)
}
if all.Template != nil || len(all.Layouts) != 2 || len(all.Partials) != 1 {
t.Errorf("overrides without a template name = %+v", all)
}
if l := all.Layouts["promo"]; l.Name != "Promo" || !strings.HasPrefix(l.Text, "PROMO") {
t.Errorf("promo layout = %+v", l)
}
if logs.Len() != 0 {
t.Errorf("unexpected log output:\n%s", logs)
}
})
t.Run("file", func(t *testing.T) {
gdb := mailPostgres(t)
m, drv, logs := bootMailApp(t, gdb)