Files
sm-sitemap-plugin/sitemap_extras_test.go
Jakub Zych 5bf56fde57 feat: image sitemap files (tracer)
- migration 202610280004: image_file_no, kind, published_at and parent_refs
  on the slot table; the image, News, IndexNow and HTML sitemap settings
- the rebuild stores each page's featured and body images (absolute on the
  site, deduplicated, at most 1000) and numbers the rows with images into
  sitemap-images-<n>.xml with the per-type files' stable rules
- the index lists the image files while images_enabled is on; the files
  route serves them with image:image and image:loc only
2026-11-04 12:19:05 +01:00

184 lines
7.1 KiB
Go

package sitemap_test
import (
"context"
"fmt"
"net/http"
"regexp"
"strings"
"testing"
"time"
"git.golem15.com/golem15/summercms/modules/backpack"
"git.golem15.com/golem15/summercms/modules/pact"
"git.golem15.com/golem15/summercms/modules/party"
)
// journalPluginID is a fake blog plugin with an article type (posts at
// /blog/<slug>) and a collection type (categories at /kategoria/<slug>)
// whose records live in contentStore. It knows nothing about the sitemap.
const journalPluginID = "acme.journal"
// The fake blog content types.
const (
postType = "acme-post"
categoryType = "acme-category"
)
// journalPlugin is acme.journal.
type journalPlugin struct{}
func (journalPlugin) ID() string { return journalPluginID }
func (journalPlugin) Requires() []string { return nil }
func (journalPlugin) Register(*backpack.App) error { return nil }
func (journalPlugin) Boot(*backpack.App) error { return nil }
func (journalPlugin) MenuItemTypes() []pact.MenuItemType {
typ := func(key, label, kind, pattern string) pact.MenuItemType {
return pact.MenuItemType{
Key: key,
Label: label,
DefaultPattern: pattern,
Placeholders: []string{"slug"},
Resolve: func(context.Context, pact.MenuItemResolve) (*pact.ResolvedMenuItem, error) {
return nil, nil
},
Content: &pact.ContentDescriptor{
Model: key,
Kind: kind,
Records: func(_ context.Context, in pact.ContentList) (pact.ContentPage, error) {
return contentStore.page(key, in), nil
},
Describe: func(context.Context, pact.ContentDescribe) (*pact.ContentRecord, error) {
return nil, nil
},
},
}
}
return []pact.MenuItemType{
typ(postType, "Posts", pact.ContentKindArticle, "/blog/:slug"),
typ(categoryType, "Categories", pact.ContentKindCollection, "/kategoria/:slug"),
}
}
var _ pact.HasMenuItemTypes = journalPlugin{}
func init() {
party.Register(journalPlugin{})
}
// bootJournal boots the fake blog plugin and the sitemap on a fresh
// database with an empty content store; overrides set config keys.
func bootJournal(t *testing.T, overrides ...map[string]any) *sitemapEnv {
t.Helper()
contentStore.reset(t)
return bootPlugins(t, []string{journalPluginID, "golem15.sitemap"}, overrides...)
}
// post is a published post in one locale at /blog/<slug>, first published
// at published, with images.
func post(id int, slug, locale string, published time.Time, images ...string) fakeRecord {
l := pact.ContentLocale{Locale: locale, URL: "/blog/" + slug, Title: "Post " + slug}
for i, img := range images {
if i == 0 {
l.Image = img
}
l.Images = append(l.Images, pact.ContentImage{URL: img})
}
return fakeRecord{ID: id, Published: true, LastMod: published, PublishedAt: &published, Locales: []pact.ContentLocale{l}}
}
// exec runs one SQL statement on the test database.
func (e *sitemapEnv) exec(t *testing.T, sql string, args ...any) {
t.Helper()
if err := e.gdb.Exec(sql, args...).Error; err != nil {
t.Fatal(err)
}
}
// imageLocs lists the image:loc values of an image sitemap body.
func imageLocs(body string) []string {
var out []string
for _, m := range regexp.MustCompile(`<image:loc>([^<]*)</image:loc>`).FindAllStringSubmatch(body, -1) {
out = append(out, m[1])
}
return out
}
// TestImageSitemap is the tracer of D-31's image sitemap: a post's
// featured and body images appear, after a rebuild, in sitemap-images-1.xml
// linked from the index: the page's <loc> and one image:image/image:loc per
// image (featured first, repeated and unsafe addresses dropped, paths made
// absolute), only those two tags, at most 1000 per page; a page without
// images is not listed; images_enabled off takes the files out of the index
// and answers 404.
func TestImageSitemap(t *testing.T) {
e := bootJournal(t)
at := time.Date(2026, 10, 1, 8, 0, 0, 0, time.UTC)
contentStore.put(postType, post(1, "a", "en", at, "/media/a.jpg", "/media/a.jpg", "https://cdn.example.test/b.png", "javascript:alert(1)", "/media/c&d.webp"))
contentStore.put(postType, post(2, "b", "en", at.Add(time.Hour)))
many := make([]string, 1200)
for i := range many {
many[i] = fmt.Sprintf("/media/many-%d.jpg", i)
}
contentStore.put(postType, post(3, "c", "en", at.Add(2*time.Hour), many...))
e.rebuild(t)
index := e.get(t, "/sitemap.xml").Body.String()
if got := locs(index); strings.Join(got, " ") != "https://api.example.test/sitemap-posts-1.xml https://api.example.test/sitemap-images-1.xml" {
t.Fatalf("index locs = %v\n%s", got, index)
}
rec := e.get(t, "/_sitemap/files/sitemap-images-1.xml")
body := rec.Body.String()
if rec.Code != http.StatusOK || rec.Header().Get("Content-Type") != "application/xml" || rec.Header().Get("X-Robots-Tag") != "noindex" {
t.Fatalf("image file: %d %v %s", rec.Code, rec.Header(), body)
}
if !strings.Contains(body, `<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" xmlns:image="http://www.google.com/schemas/sitemap-image/1.1">`) {
t.Fatalf("image file head:\n%s", body[:min(len(body), 300)])
}
if got := locs(body); strings.Join(got, " ") != "https://api.example.test/blog/a https://api.example.test/blog/c" {
t.Fatalf("image file pages = %v", got)
}
first := body[strings.Index(body, "<url><loc>https://api.example.test/blog/a</loc>"):]
first = first[:strings.Index(first, "</url>")]
if got := imageLocs(first); strings.Join(got, " ") != "https://api.example.test/media/a.jpg https://cdn.example.test/b.png https://api.example.test/media/c&amp;d.webp" {
t.Fatalf("post a images = %v\n%s", got, first)
}
if n := len(imageLocs(body)); n != 3+1000 {
t.Fatalf("image file lists %d images, want 1003", n)
}
for _, banned := range []string{"image:caption", "image:title", "javascript:", "<lastmod>", "xhtml"} {
if strings.Contains(body, banned) {
t.Errorf("image file contains %q", banned)
}
}
if rec := e.get(t, "/_sitemap/files/sitemap-images-2.xml"); rec.Code != http.StatusNotFound {
t.Errorf("image file 2: %d", rec.Code)
}
// Stable numbering: with a file size of 1 the second page with images
// is renumbered into its own file once, and a page that loses its images
// leaves a hole instead of moving the later file.
e.setFileSize(t, 1)
e.rebuild(t)
if got := locs(e.get(t, "/_sitemap/files/sitemap-images-2.xml").Body.String()); strings.Join(got, " ") != "https://api.example.test/blog/c" {
t.Fatalf("image file 2 after the size change = %v", got)
}
contentStore.put(postType, post(1, "a", "en", at))
e.rebuild(t)
if rec := e.get(t, "/_sitemap/files/sitemap-images-1.xml"); rec.Code != http.StatusNotFound {
t.Errorf("image file 1 without images: %d %s", rec.Code, rec.Body.String())
}
if got := locs(e.get(t, "/_sitemap/files/sitemap-images-2.xml").Body.String()); strings.Join(got, " ") != "https://api.example.test/blog/c" {
t.Fatalf("image file 2 moved = %v", got)
}
e.exec(t, `UPDATE golem15_sitemap_settings SET images_enabled = FALSE WHERE id = 1`)
if index := e.get(t, "/sitemap.xml").Body.String(); strings.Contains(index, "sitemap-images") {
t.Fatalf("index with the image sitemap off:\n%s", index)
}
if rec := e.get(t, "/_sitemap/files/sitemap-images-2.xml"); rec.Code != http.StatusNotFound {
t.Errorf("image file with the image sitemap off: %d", rec.Code)
}
}