Files
sm-user-plugin/mail_subjects_test.go
Jakub Zych 6bf219f50a fix(quick-261029-d9j): pick the mail language from Accept-Language, add mail.fallback_locale, Polish subjects
- preferred_locale and the request locale are normalised (comma lists,
  q-weights, region subtags, case) to pl or en by NormalizeMailLocale, so a
  raw Accept-Language such as en-GB,en;q=0.9 selects the -en templates
- new golem15.user.mail.fallback_locale (default pl) is used when neither
  names a supported language; ResolveMailLocaleFor applies it, and Boot
  rejects any value other than pl or en
- activate, reactivate and restore Polish templates get Polish subjects
- no template, route or payload changes; MailTemplate and
  ResolveMailLocale keep their signatures
2026-10-29 09:47:42 +01:00

59 lines
1.7 KiB
Go

package user
import (
"context"
"strings"
"testing"
"git.golem15.com/golem15/summercms/modules/postcard"
)
// TestPolishMailSubjects renders every Polish template next to its -en twin
// and requires a subject of its own, so a Polish template with a copied
// English subject does not slip through.
func TestPolishMailSubjects(t *testing.T) {
p := &Plugin{}
cat := postcard.NewCatalog()
if err := cat.Register(p.ID(), p.MailTemplatesFS(), p.MailTemplates(), p.MailLayouts()); err != nil {
t.Fatal(err)
}
names := map[string]bool{}
for _, name := range p.MailTemplates() {
names[name] = true
}
vars := map[string]any{
"name": "Ada", "Name": "Ada",
"link": "https://example.com/link", "Link": "https://example.com/link",
"code": "1!abc", "Code": "1!abc",
"hours": 72, "Hours": 72,
"login": "ada@example.test", "Login": "ada@example.test",
}
subject := func(t *testing.T, template string) string {
t.Helper()
mem := postcard.NewMemoryDriver()
mailer := postcard.NewMailer(cat, mem, postcard.Options{From: "noreply@example.test"})
if err := mailer.Send(context.Background(), postcard.Message{Template: template, To: []string{"ada@example.test"}, Vars: vars}); err != nil {
t.Fatalf("%s: %v", template, err)
}
msgs := mem.Messages()
if len(msgs) != 1 {
t.Fatalf("%s: %d messages", template, len(msgs))
}
return msgs[0].Subject
}
pairs := 0
for _, name := range p.MailTemplates() {
if strings.HasSuffix(name, "-en") || !names[name+"-en"] {
continue
}
pairs++
pl, en := subject(t, name), subject(t, name+"-en")
if pl == "" || pl == en {
t.Errorf("%s: Polish subject %q, English subject %q", name, pl, en)
}
}
if pairs == 0 {
t.Fatal("no Polish/English template pairs")
}
}