feat: HTML sitemap groups API
- GET /_sitemap/api/v1/html?locale= answers the overview: the listed
addresses of the XML sitemaps by group, in the order pages, offers,
services, categories (as a tree), posts (newest first), tags, authors,
other content types, each with its title in the locale, total, the
first html_page_size links and more
- GET /_sitemap/api/v1/html/{group}?locale=&page= answers one page of a
group; past the last page, an unknown group or the HTML sitemap off
answer 404; every answer is noindex; per-IP bucket sitemap-html
- the HTML sitemap tab: html_enabled, the address per locale (html_paths)
and html_page_size 50 to 500, with errors.html_path and errors.page_size
- contract.Listing.Settings answers HTMLEnabled, HTMLPaths, HTMLPageSize
and KeyFile
This commit is contained in:
@@ -13,6 +13,7 @@ import (
|
||||
"time"
|
||||
|
||||
"git.golem15.com/golem15/sm-sitemap-plugin/classes"
|
||||
"git.golem15.com/golem15/sm-sitemap-plugin/contract"
|
||||
"git.golem15.com/golem15/summercms/modules/backpack"
|
||||
"git.golem15.com/golem15/summercms/modules/conga"
|
||||
"git.golem15.com/golem15/summercms/modules/fetchguard"
|
||||
@@ -536,3 +537,192 @@ func TestIndexNowStaging(t *testing.T) {
|
||||
t.Fatalf("key file outside production: %d", rec.Code)
|
||||
}
|
||||
}
|
||||
|
||||
// htmlJSON fetches an HTML sitemap API path and decodes its answer.
|
||||
func (e *sitemapEnv) htmlJSON(t *testing.T, path string, status int) map[string]any {
|
||||
t.Helper()
|
||||
rec := e.get(t, path)
|
||||
if rec.Code != status {
|
||||
t.Fatalf("GET %s: %d, want %d: %s", path, rec.Code, status, rec.Body.String())
|
||||
}
|
||||
if rec.Header().Get("X-Robots-Tag") != "noindex" {
|
||||
t.Errorf("GET %s lacks X-Robots-Tag noindex: %v", path, rec.Header())
|
||||
}
|
||||
out := map[string]any{}
|
||||
if status == http.StatusOK {
|
||||
if err := json.Unmarshal(rec.Body.Bytes(), &out); err != nil {
|
||||
t.Fatalf("decode %s: %v", rec.Body.String(), err)
|
||||
}
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
// titlesOfEntries lists the titles of decoded entries, children in
|
||||
// brackets.
|
||||
func titlesOfEntries(v any) string {
|
||||
list, _ := v.([]any)
|
||||
var parts []string
|
||||
for _, item := range list {
|
||||
e, _ := item.(map[string]any)
|
||||
s, _ := e["title"].(string)
|
||||
if kids, ok := e["children"]; ok {
|
||||
s += "[" + titlesOfEntries(kids) + "]"
|
||||
}
|
||||
parts = append(parts, s)
|
||||
}
|
||||
return strings.Join(parts, ", ")
|
||||
}
|
||||
|
||||
// TestHTMLGroups pins D-34's HTML sitemap API: the overview of a locale
|
||||
// lists the groups in the L3 order (pages, offers, categories, posts) with
|
||||
// titles in the locale, categories nested by parent, posts newest first,
|
||||
// others alphabetical, the first html_page_size links and more when there
|
||||
// are more; a group page pages the rest; past the last page, an unknown
|
||||
// group and the HTML sitemap off are 404; excluded addresses never appear;
|
||||
// every answer is noindex. The settings tab validates its values and
|
||||
// contract.Listing answers the HTML sitemap and key file settings.
|
||||
func TestHTMLGroups(t *testing.T) {
|
||||
contentStore.reset(t)
|
||||
e := bootPlugins(t, []string{contentPluginID, journalPluginID, filterPluginID, "golem15.sitemap"})
|
||||
at := time.Date(2026, 10, 1, 8, 0, 0, 0, time.UTC)
|
||||
pl := func(id int, url, title string) fakeRecord {
|
||||
return fakeRecord{ID: id, Published: true, LastMod: at, Locales: []pact.ContentLocale{{Locale: "pl", URL: url, Title: title}}}
|
||||
}
|
||||
contentStore.put(pageType, pl(1, "/pages/o-nas", "O nas"))
|
||||
contentStore.put(pageType, pl(2, "/pages/kontakt", "Kontakt"))
|
||||
contentStore.put(pageType, pl(3, "/pages/noindex-me", "Ukryta"))
|
||||
contentStore.put(pageType, fakeRecord{ID: 4, Published: true, LastMod: at, Locales: []pact.ContentLocale{{Locale: "en", URL: "/pages/about", Title: "About"}}})
|
||||
contentStore.put(offerType, pl(1, "/offers/zestaw", "Zestaw"))
|
||||
cat := func(id int, slug, title string, parent int) fakeRecord {
|
||||
r := pl(id, "/kategoria/"+slug, title)
|
||||
if parent > 0 {
|
||||
r.Parents = []pact.ContentRef{{Type: categoryType, ID: fmt.Sprint(parent)}}
|
||||
}
|
||||
return r
|
||||
}
|
||||
contentStore.put(categoryType, cat(1, "grzyby", "Grzyby", 0))
|
||||
contentStore.put(categoryType, cat(2, "lecznicze", "Lecznicze", 1))
|
||||
contentStore.put(categoryType, cat(3, "jadalne", "Jadalne", 1))
|
||||
contentStore.put(categoryType, cat(4, "inne", "Inne", 0))
|
||||
for i := 1; i <= 160; i++ {
|
||||
contentStore.put(postType, post(i, fmt.Sprintf("p%03d", i), "pl", at.Add(time.Duration(i)*time.Hour)))
|
||||
}
|
||||
e.exec(t, `INSERT INTO golem15_sitemap_types (type_key, include, slug, titles, updated_at) VALUES (?, TRUE, '', '{"pl":"Wpisy","en":"Posts"}', NOW())`, postType)
|
||||
e.rebuild(t)
|
||||
|
||||
ov := e.htmlJSON(t, "/_sitemap/api/v1/html?locale=pl", http.StatusOK)
|
||||
groups, _ := ov["groups"].([]any)
|
||||
var order []string
|
||||
byKey := map[string]map[string]any{}
|
||||
for _, g := range groups {
|
||||
m := g.(map[string]any)
|
||||
order = append(order, m["slug"].(string))
|
||||
byKey[m["slug"].(string)] = m
|
||||
}
|
||||
if strings.Join(order, " ") != "pages offers categories posts" {
|
||||
t.Fatalf("group order = %v\n%v", order, ov)
|
||||
}
|
||||
if got := titlesOfEntries(byKey["pages"]["entries"]); got != "Kontakt, O nas" {
|
||||
t.Errorf("pages = %s", got)
|
||||
}
|
||||
if got := titlesOfEntries(byKey["categories"]["entries"]); got != "Grzyby[Jadalne, Lecznicze], Inne" {
|
||||
t.Errorf("categories tree = %s", got)
|
||||
}
|
||||
posts := byKey["posts"]
|
||||
entries, _ := posts["entries"].([]any)
|
||||
if posts["title"] != "Wpisy" || posts["total"] != float64(160) || posts["more"] != true || len(entries) != 150 {
|
||||
t.Fatalf("posts group = title %v total %v more %v entries %d", posts["title"], posts["total"], posts["more"], len(entries))
|
||||
}
|
||||
if first := entries[0].(map[string]any); first["title"] != "Post p160" || first["url"] != "https://api.example.test/blog/p160" {
|
||||
t.Errorf("newest post first: %v", first)
|
||||
}
|
||||
if byKey["categories"]["title"] != "Categories" || byKey["pages"]["more"] != false {
|
||||
t.Errorf("categories title %v, pages more %v", byKey["categories"]["title"], byKey["pages"]["more"])
|
||||
}
|
||||
all := fmt.Sprint(ov)
|
||||
for _, banned := range []string{"Ukryta", "noindex-me", "About"} {
|
||||
if strings.Contains(all, banned) {
|
||||
t.Errorf("the pl overview lists %q", banned)
|
||||
}
|
||||
}
|
||||
if en := fmt.Sprint(e.htmlJSON(t, "/_sitemap/api/v1/html?locale=en", http.StatusOK)); !strings.Contains(en, "About") || strings.Contains(en, "Kontakt") {
|
||||
t.Errorf("the en overview: %s", en)
|
||||
}
|
||||
|
||||
page2 := e.htmlJSON(t, "/_sitemap/api/v1/html/posts?locale=pl&page=2", http.StatusOK)
|
||||
if p2, _ := page2["entries"].([]any); page2["page"] != float64(2) || page2["pages"] != float64(2) || page2["group"] != "posts" || page2["title"] != "Wpisy" || len(p2) != 10 {
|
||||
t.Fatalf("posts page 2 = %v", page2)
|
||||
}
|
||||
if got := titlesOfEntries(page2["entries"]); !strings.HasPrefix(got, "Post p010, Post p009") || !strings.HasSuffix(got, "Post p001") {
|
||||
t.Errorf("posts page 2 = %s", got)
|
||||
}
|
||||
if p1 := e.htmlJSON(t, "/_sitemap/api/v1/html/posts?locale=pl", http.StatusOK); p1["page"] != float64(1) {
|
||||
t.Errorf("posts without page = %v", p1["page"])
|
||||
}
|
||||
if got := titlesOfEntries(e.htmlJSON(t, "/_sitemap/api/v1/html/categories?locale=pl", http.StatusOK)["entries"]); got != "Grzyby[Jadalne, Lecznicze], Inne" {
|
||||
t.Errorf("categories page = %s", got)
|
||||
}
|
||||
for _, path := range []string{
|
||||
"/_sitemap/api/v1/html/posts?locale=pl&page=3",
|
||||
"/_sitemap/api/v1/html/posts?locale=pl&page=0",
|
||||
"/_sitemap/api/v1/html/posts?locale=pl&page=x",
|
||||
"/_sitemap/api/v1/html/unknown?locale=pl",
|
||||
"/_sitemap/api/v1/html/offers?locale=en",
|
||||
"/_sitemap/api/v1/html?locale=%3Cx%3E",
|
||||
} {
|
||||
e.htmlJSON(t, path, http.StatusNotFound)
|
||||
}
|
||||
|
||||
// The settings tab.
|
||||
token := e.admin(t, permSitemap)
|
||||
const base = "/golem15/sitemap/settings"
|
||||
schema := e.call(t, http.MethodGet, base+"/schema/form", token, nil).Body.String()
|
||||
for _, want := range []string{`"html_enabled"`, `"html_path"`, `"html_page_size"`, "HTML sitemap"} {
|
||||
if !strings.Contains(schema, want) {
|
||||
t.Errorf("form schema lacks %s", want)
|
||||
}
|
||||
}
|
||||
for _, c := range []struct {
|
||||
body map[string]any
|
||||
field string
|
||||
}{
|
||||
{map[string]any{"html_page_size": 49}, "html_page_size"},
|
||||
{map[string]any{"html_page_size": 501}, "html_page_size"},
|
||||
{map[string]any{"html_path": "mapa"}, "html_path"},
|
||||
{map[string]any{"html_path": "/mapa?x=1"}, "html_path"},
|
||||
{map[string]any{"html_path": "//evil.test"}, "html_path"},
|
||||
} {
|
||||
if rec := e.call(t, http.MethodPut, base+"/1", token, c.body); rec.Code != http.StatusUnprocessableEntity || !strings.Contains(rec.Body.String(), `"`+c.field+`"`) {
|
||||
t.Errorf("%v: %d %s", c.body, rec.Code, rec.Body.String())
|
||||
}
|
||||
}
|
||||
listing, ok := e.app.Lookup[contract.Listing]()
|
||||
if !ok {
|
||||
t.Fatal("no contract.Listing")
|
||||
}
|
||||
ls, err := listing.Settings(t.Context())
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if !ls.HTMLEnabled || ls.HTMLPageSize != 150 || ls.HTMLPaths["pl"] != "/mapa-strony" || ls.HTMLPaths["en"] != "/sitemap" || ls.KeyFile != "" {
|
||||
t.Fatalf("listing settings = %+v", ls)
|
||||
}
|
||||
if rec := e.call(t, http.MethodPut, base+"/1", token, map[string]any{"html_path": "/site-map", "html_page_size": 100}); rec.Code != http.StatusOK {
|
||||
t.Fatalf("save the HTML tab: %d %s", rec.Code, rec.Body.String())
|
||||
}
|
||||
e.exec(t, `UPDATE golem15_sitemap_settings SET indexnow_enabled = TRUE, indexnow_key = '0123456789abcdef0123456789abcdef' WHERE id = 1`)
|
||||
ls, _ = listing.Settings(t.Context())
|
||||
if ls.HTMLPageSize != 100 || ls.HTMLPaths["en"] != "/site-map" || ls.HTMLPaths["pl"] != "/mapa-strony" || ls.KeyFile != "0123456789abcdef0123456789abcdef.txt" {
|
||||
t.Fatalf("listing settings after the save = %+v", ls)
|
||||
}
|
||||
if p := e.htmlJSON(t, "/_sitemap/api/v1/html/posts?locale=pl&page=2", http.StatusOK); p["pages"] != float64(2) || len(p["entries"].([]any)) != 60 {
|
||||
t.Errorf("posts page 2 at size 100 = pages %v", p["pages"])
|
||||
}
|
||||
|
||||
e.call(t, http.MethodPut, base+"/1", token, map[string]any{"html_enabled": false})
|
||||
e.htmlJSON(t, "/_sitemap/api/v1/html?locale=pl", http.StatusNotFound)
|
||||
e.htmlJSON(t, "/_sitemap/api/v1/html/posts?locale=pl", http.StatusNotFound)
|
||||
if ls, _ := listing.Settings(t.Context()); ls.HTMLEnabled {
|
||||
t.Error("listing says the HTML sitemap is on")
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user