feat: journal-author content type

- journal-author (/blog/author/:author, :author the author slug else the
  login): references, Resolve, Records of the authors with a published
  post, Describe as a profile per locale, exact Match on the pattern
- golem15.journal.author_pages false resolves nothing, describes empty
  URLs and never matches
This commit is contained in:
Jakub Zych
2026-11-04 10:19:10 +01:00
parent e8e77a154c
commit 805bfdb837
7 changed files with 370 additions and 6 deletions

View File

@@ -185,3 +185,81 @@ func TestJournalDescriptorFields(t *testing.T) {
})
}
}
// TestJournalAuthorType (D-43, D-34, P33-07): journal-author lists the
// authors with a published post, describes them as profiles at
// /blog/author/:author (the author slug, else the login) per locale, matches
// exactly on the pattern, and with golem15.journal.author_pages false
// describes empty URLs and never matches.
func TestJournalAuthorType(t *testing.T) {
requirePostgres(t)
for _, tc := range []struct {
name string
files map[string]string
on bool
}{
{"default", map[string]string{}, true},
{"off", map[string]string{"golem15.yaml": "journal:\n author_pages: false\n"}, false},
} {
t.Run(tc.name, func(t *testing.T) {
_, gdb, reg := bootMenuItemTypes(t, tc.files)
seedLocales(t, gdb)
ctx := context.Background()
past := time.Date(2026, 3, 4, 10, 0, 0, 0, time.UTC)
ada := seedAuthor(t, gdb, "ada", "Ada", "Lovelace", "ada-l")
bob := seedAuthor(t, gdb, "bob", "Bob", "", "")
seedAuthor(t, gdb, "carl", "Carl", "Idle", "carl")
for _, p := range []models.Post{
{Title: "By Ada", Slug: "by-ada", Content: "x", Published: true, PublishedAt: &past, UserID: &ada},
{Title: "By Bob", Slug: "by-bob", Content: "x", Published: true, PublishedAt: &past, UserID: &bob},
{Title: "Carl draft", Slug: "carl-draft", Content: "x", Published: false, PublishedAt: &past},
} {
p := p
if err := gdb.Create(&p).Error; err != nil {
t.Fatal(err)
}
}
typ, ok := reg.ContentType(classes.MenuTypeAuthor)
if !ok || typ.Content.Kind != pact.ContentKindProfile || typ.Content.Controller != "" || typ.Content.Model != "Backend\\Models\\User" {
t.Fatalf("journal-author descriptor = %+v", typ.Content)
}
page, err := reg.Records(ctx, classes.MenuTypeAuthor, "", 0, "")
if err != nil || len(page.Records) != 2 || page.Records[0].ID != id(ada) || page.Records[1].ID != id(bob) {
t.Fatalf("Records = %+v, %v; want Ada and Bob only", page, err)
}
rec := describe(t, reg, classes.MenuTypeAuthor, id(ada))
want := "en= pl="
if tc.on {
want = "en=/blog/author/ada-l pl=/pl/blog/author/ada-l"
}
if localeURLs(rec) != want || rec.Locales[0].Title != "Ada Lovelace" || !rec.Published {
t.Fatalf("Describe Ada = %+v; want URLs %s", rec, want)
}
if tc.on {
if got := localeURLs(describe(t, reg, classes.MenuTypeAuthor, id(bob))); got != "en=/blog/author/bob pl=/pl/blog/author/bob" {
t.Fatalf("login fallback URLs = %s", got)
}
}
for _, path := range []string{"/blog/author/ada-l", "/pl/blog/author/ada-l"} {
key, res, err := reg.MatchPath(ctx, path, "")
if err != nil {
t.Fatal(err)
}
if tc.on && (key != classes.MenuTypeAuthor || res == nil || !res.Exact || res.ID != id(ada)) {
t.Fatalf("MatchPath(%s) = %q %+v", path, key, res)
}
if !tc.on && res != nil && key == classes.MenuTypeAuthor {
t.Fatalf("MatchPath(%s) matched with author pages off: %+v", path, res)
}
}
if key, res, err := reg.MatchPath(ctx, "/blog/author/carl", ""); err != nil || (res != nil && key == classes.MenuTypeAuthor) {
t.Fatalf("an author without a published post matched: %q %+v %v", key, res, err)
}
item, err := reg.Resolve(ctx, classes.MenuTypeAuthor, id(ada), "", false, "")
if err != nil || (item != nil) != tc.on || (tc.on && item.URL != "/blog/author/ada-l") {
t.Fatalf("Resolve = %+v, %v", item, err)
}
})
}
}