feat(quick-261115-cha): let a mail template use another plugin's layout by full code
- resolveLayoutLocked: after the plugin's own alias and the built-in aliases, a full layout name already in the catalog resolves to itself; an unregistered full name fails with a require-the-owner hint - another plugin's short alias and the short-alias error are unchanged - TestCrossPluginLayout covers register, send, errors and precedence - postcard README and docs/services/mail.md describe the rule
This commit is contained in:
170
modules/postcard/layout_ref_test.go
Normal file
170
modules/postcard/layout_ref_test.go
Normal file
@@ -0,0 +1,170 @@
|
||||
package postcard
|
||||
|
||||
import (
|
||||
"strings"
|
||||
"testing"
|
||||
"testing/fstest"
|
||||
)
|
||||
|
||||
// Cross-plugin layout fixtures: acme.lender owns the card layout, and
|
||||
// acme.borrower (which ships no layouts) points its templates at it by full
|
||||
// code.
|
||||
const (
|
||||
lenderID = "acme.lender"
|
||||
borrowerID = "acme.borrower"
|
||||
lenderCardLayout = "acme.lender::mail.layouts.card"
|
||||
lenderNotice = "acme.lender::mail.notice"
|
||||
borrowerCode = "acme.borrower::mail.code"
|
||||
borrowerCodeEn = "acme.borrower::mail.code-en"
|
||||
borrowerPlain = "acme.borrower::mail.plain"
|
||||
lenderCardHTML = `<div class="lender-card">`
|
||||
lenderCardFooter = "-- Lender card"
|
||||
)
|
||||
|
||||
func lenderFS() fstest.MapFS {
|
||||
return fstest.MapFS{
|
||||
"views/mail/layouts/card.htm": {Data: []byte("name = \"Card\"\n==\n{{ .Content }}\n\n" + lenderCardFooter + "\n==\n" + lenderCardHTML + "{{ .Content }}</div>\n")},
|
||||
"views/mail/notice.htm": {Data: []byte("subject = \"Notice\"\nlayout = \"card\"\n==\nA notice.\n")},
|
||||
}
|
||||
}
|
||||
|
||||
// borrowerFS returns the borrower's code templates with the given header
|
||||
// lines (for example a layout key) and a plain template with no layout key.
|
||||
func borrowerFS(header string) fstest.MapFS {
|
||||
code := []byte("subject = \"Code {{ .code }}\"\n" + header + "==\nYour code is **{{ .code }}**.\n")
|
||||
return fstest.MapFS{
|
||||
"views/mail/code.htm": {Data: code},
|
||||
"views/mail/code-en.htm": {Data: code},
|
||||
"views/mail/plain.htm": {Data: []byte("subject = \"Plain\"\n==\nPlain body.\n")},
|
||||
}
|
||||
}
|
||||
|
||||
func registerLender(t *testing.T, cat *Catalog) {
|
||||
t.Helper()
|
||||
if err := cat.Register(lenderID, lenderFS(), []string{lenderNotice}, map[string]string{"card": lenderCardLayout}); err != nil {
|
||||
t.Fatalf("Register lender: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func registerBorrower(cat *Catalog, header string) error {
|
||||
return cat.Register(borrowerID, borrowerFS(header), []string{borrowerCode, borrowerCodeEn, borrowerPlain}, nil)
|
||||
}
|
||||
|
||||
// borrowerCatalog is a catalog with the lender registered, then the borrower
|
||||
// whose code templates use the lender's card layout by full code.
|
||||
func borrowerCatalog(t *testing.T) *Catalog {
|
||||
t.Helper()
|
||||
cat := NewCatalog()
|
||||
registerLender(t, cat)
|
||||
if err := registerBorrower(cat, "layout = \""+lenderCardLayout+"\"\n"); err != nil {
|
||||
t.Fatalf("Register borrower: %v", err)
|
||||
}
|
||||
return cat
|
||||
}
|
||||
|
||||
func templateLayout(t *testing.T, cat *Catalog, name string) string {
|
||||
t.Helper()
|
||||
tf, ok := cat.Template(name)
|
||||
if !ok {
|
||||
t.Fatalf("Template(%s) not found", name)
|
||||
}
|
||||
return tf.Layout
|
||||
}
|
||||
|
||||
// TestCrossPluginLayout covers a template naming another plugin's layout by
|
||||
// its full code: registration, the send, the boot errors and the unchanged
|
||||
// alias precedence.
|
||||
func TestCrossPluginLayout(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
t.Run("file_layout_by_full_code", func(t *testing.T) {
|
||||
t.Parallel()
|
||||
cat := borrowerCatalog(t)
|
||||
for _, name := range []string{borrowerCode, borrowerCodeEn} {
|
||||
if got := templateLayout(t, cat, name); got != lenderCardLayout {
|
||||
t.Errorf("%s Layout = %q, want %q", name, got, lenderCardLayout)
|
||||
}
|
||||
drv := NewMemoryDriver()
|
||||
m := NewMailer(cat, drv, Options{})
|
||||
msg := sendTemplate(t, m, drv, name, map[string]any{"code": "123456"})
|
||||
i := strings.Index(msg.HTML, lenderCardHTML)
|
||||
if i < 0 {
|
||||
t.Fatalf("%s HTML has no lender card:\n%s", name, msg.HTML)
|
||||
}
|
||||
if !strings.Contains(msg.HTML[i:], "<strong>123456</strong>") {
|
||||
t.Errorf("%s HTML has no strong code inside the card:\n%s", name, msg.HTML)
|
||||
}
|
||||
if !strings.Contains(msg.Text, lenderCardFooter) {
|
||||
t.Errorf("%s Text has no lender footer:\n%s", name, msg.Text)
|
||||
}
|
||||
if msg.Subject != "Code 123456" {
|
||||
t.Errorf("%s Subject = %q", name, msg.Subject)
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("owner_registers_later", func(t *testing.T) {
|
||||
t.Parallel()
|
||||
err := registerBorrower(NewCatalog(), "layout = \""+lenderCardLayout+"\"\n")
|
||||
if err == nil {
|
||||
t.Fatal("Register borrower without the lender succeeded")
|
||||
}
|
||||
for _, want := range []string{"unknown layout " + lenderCardLayout, borrowerCode, "required"} {
|
||||
if !strings.Contains(err.Error(), want) {
|
||||
t.Errorf("error %q does not contain %q", err, want)
|
||||
}
|
||||
}
|
||||
if !strings.HasPrefix(err.Error(), "postcard: unknown layout "+lenderCardLayout) {
|
||||
t.Errorf("error %q does not start with the unknown layout prefix", err)
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("unknown_full_code", func(t *testing.T) {
|
||||
t.Parallel()
|
||||
cat := NewCatalog()
|
||||
registerLender(t, cat)
|
||||
err := registerBorrower(cat, "layout = \"acme.lender::mail.layouts.nope\"\n")
|
||||
if err == nil || !strings.Contains(err.Error(), "unknown layout acme.lender::mail.layouts.nope") {
|
||||
t.Fatalf("err = %v, want unknown layout acme.lender::mail.layouts.nope", err)
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("other_plugin_alias_refused", func(t *testing.T) {
|
||||
t.Parallel()
|
||||
cat := NewCatalog()
|
||||
registerLender(t, cat)
|
||||
err := registerBorrower(cat, "layout = \"card\"\n")
|
||||
if err == nil || err.Error() != "postcard: unknown layout card" {
|
||||
t.Fatalf("err = %v, want exactly postcard: unknown layout card", err)
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("precedence_unchanged", func(t *testing.T) {
|
||||
t.Parallel()
|
||||
cat := NewCatalog()
|
||||
registerLender(t, cat)
|
||||
if got := templateLayout(t, cat, lenderNotice); got != lenderCardLayout {
|
||||
t.Errorf("lender notice Layout = %q, want %q", got, lenderCardLayout)
|
||||
}
|
||||
if err := registerBorrower(cat, "layout = \"system\"\n"); err != nil {
|
||||
t.Fatalf("Register borrower: %v", err)
|
||||
}
|
||||
if got := templateLayout(t, cat, borrowerCode); got != systemLayoutName {
|
||||
t.Errorf("system Layout = %q, want %q", got, systemLayoutName)
|
||||
}
|
||||
if got := templateLayout(t, cat, borrowerPlain); got != defaultLayoutName {
|
||||
t.Errorf("no-layout Layout = %q, want %q", got, defaultLayoutName)
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("builtin_full_code", func(t *testing.T) {
|
||||
t.Parallel()
|
||||
cat := NewCatalog()
|
||||
if err := registerBorrower(cat, "layout = \""+minimalLayoutName+"\"\n"); err != nil {
|
||||
t.Fatalf("Register borrower: %v", err)
|
||||
}
|
||||
if got := templateLayout(t, cat, borrowerCode); got != minimalLayoutName {
|
||||
t.Errorf("Layout = %q, want %q", got, minimalLayoutName)
|
||||
}
|
||||
})
|
||||
}
|
||||
Reference in New Issue
Block a user