- golem15.sitemap plugin: golem15_sitemap_definitions migration, Definition model (Fillable theme and data) with tolerant PHP-shaped item decoding and PHP toArray item encoding - Url::to and buildUrl port, libxml-format serializer, generator for url and registry items - GET /sitemap.xml serves the definition with theme default (404 without one, empty body without items) - tests/php/gen-goldens.php and the tracer golden; TestSitemapGolden and the Postgres TestSitemapTracer
190 lines
6.0 KiB
Go
190 lines
6.0 KiB
Go
package sitemap_test
|
|
|
|
import (
|
|
"context"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"os"
|
|
"path/filepath"
|
|
"testing"
|
|
"time"
|
|
|
|
_ "git.golem15.com/golem15/sm-sitemap-plugin"
|
|
"git.golem15.com/golem15/sm-sitemap-plugin/models"
|
|
"git.golem15.com/golem15/summercms/modules/backpack"
|
|
"git.golem15.com/golem15/summercms/modules/compass"
|
|
"git.golem15.com/golem15/summercms/modules/lagoon"
|
|
"git.golem15.com/golem15/summercms/modules/pact"
|
|
"git.golem15.com/golem15/summercms/modules/party"
|
|
"git.golem15.com/golem15/summercms/modules/surf"
|
|
"gorm.io/gorm"
|
|
)
|
|
|
|
// testTypesPluginID is the fake content plugin that registers menu item
|
|
// types for the sitemap smoke tests. The real Journal and sitemap
|
|
// combination is proven by the host application's tests.
|
|
const testTypesPluginID = "acme.sitemaptypes"
|
|
|
|
// acmeTypes is a content plugin with one menu item type, acme-post, whose
|
|
// reference "7" resolves to a fixed post with two alternates.
|
|
type acmeTypes struct{}
|
|
|
|
func (acmeTypes) ID() string { return testTypesPluginID }
|
|
func (acmeTypes) Requires() []string { return nil }
|
|
func (acmeTypes) Register(*backpack.App) error { return nil }
|
|
func (acmeTypes) Boot(*backpack.App) error { return nil }
|
|
func (acmeTypes) MenuItemTypes() []pact.MenuItemType {
|
|
mtime := time.Date(2026, 3, 4, 10, 0, 0, 0, time.UTC)
|
|
return []pact.MenuItemType{{
|
|
Key: "acme-post",
|
|
Label: "Acme post",
|
|
DefaultPattern: "/news/:slug",
|
|
References: func(context.Context) ([]pact.MenuItemReference, error) {
|
|
return []pact.MenuItemReference{{Value: "7", Label: "Hello"}}, nil
|
|
},
|
|
Resolve: func(_ context.Context, in pact.MenuItemResolve) (*pact.ResolvedMenuItem, error) {
|
|
if in.Reference != "7" {
|
|
return nil, nil
|
|
}
|
|
return &pact.ResolvedMenuItem{
|
|
Title: "Hello",
|
|
URL: "/news/hello",
|
|
MTime: &mtime,
|
|
Alternates: []pact.MenuItemAlternate{
|
|
{Locale: "pl", URL: "/news/hello"},
|
|
{Locale: "en", URL: "/en/news/hello"},
|
|
},
|
|
}, nil
|
|
},
|
|
}}
|
|
}
|
|
|
|
var _ pact.HasMenuItemTypes = acmeTypes{}
|
|
|
|
func init() {
|
|
party.Register(acmeTypes{})
|
|
}
|
|
|
|
// sitemapEnv is a booted application with the sitemap and the fake types.
|
|
type sitemapEnv struct {
|
|
h http.Handler
|
|
app *backpack.App
|
|
gdb *gorm.DB
|
|
}
|
|
|
|
func bootSitemap(t *testing.T) *sitemapEnv {
|
|
t.Helper()
|
|
sqlDB := freshDB(t)
|
|
gdb, err := lagoon.Use(t.Context(), sqlDB)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
dir := t.TempDir()
|
|
if err := os.WriteFile(filepath.Join(dir, "app.yaml"), []byte("name: sitemap-smoke\nlocale: en\nfallback_locale: en\nurl: https://api.example.test\n"), 0o644); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
cfg, err := compass.Open(compass.Options{
|
|
Dir: dir,
|
|
Environ: []string{"SUMMER_ENV=development", "SUMMER_ADMIN__JWT__SECRET=sitemap-smoke-test-only-hs256-secret-key"},
|
|
})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
for key, value := range map[string]any{
|
|
"http.body_limits.default_bytes": 1048576,
|
|
"http.body_limits.upload_bytes": 8388608,
|
|
"mail.driver": "log",
|
|
"mail.from": "noreply@example.test",
|
|
} {
|
|
if err := cfg.Set(key, value); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
app := backpack.New(cfg)
|
|
if err := lagoon.Publish(app, sqlDB, gdb); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
plugins, err := party.Activate(app, []string{testTypesPluginID, "golem15.sitemap"})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := lagoon.Migrate(gdb, plugins); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
h, err := surf.Assemble(app, plugins)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
return &sitemapEnv{h: h, app: app, gdb: gdb}
|
|
}
|
|
|
|
func (e *sitemapEnv) get(t *testing.T, path string) *httptest.ResponseRecorder {
|
|
t.Helper()
|
|
req := httptest.NewRequest(http.MethodGet, path, nil)
|
|
req.Host = "api.example.test"
|
|
rec := httptest.NewRecorder()
|
|
e.h.ServeHTTP(rec, req)
|
|
return rec
|
|
}
|
|
|
|
func (e *sitemapEnv) storeDefinition(t *testing.T, items []models.DefinitionItem) models.Definition {
|
|
t.Helper()
|
|
theme := models.DefinitionTheme
|
|
def := models.Definition{Theme: &theme}
|
|
def.SetItems(items)
|
|
if err := e.gdb.Create(&def).Error; err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
return def
|
|
}
|
|
|
|
func text(s string) *string { return &s }
|
|
|
|
// TestSitemapTracer stores a definition with a url item and a registry
|
|
// item, GETs /sitemap.xml and compares the body with the PHP golden.
|
|
func TestSitemapTracer(t *testing.T) {
|
|
e := bootSitemap(t)
|
|
|
|
if rec := e.get(t, "/sitemap.xml"); rec.Code != http.StatusNotFound {
|
|
t.Fatalf("no definition: %d, want 404", rec.Code)
|
|
}
|
|
|
|
def := e.storeDefinition(t, nil)
|
|
rec := e.get(t, "/sitemap.xml")
|
|
if rec.Code != http.StatusOK || rec.Body.Len() != 0 || rec.Header().Get("Content-Type") != "application/xml" {
|
|
t.Fatalf("empty definition: %d %q %q", rec.Code, rec.Header().Get("Content-Type"), rec.Body.String())
|
|
}
|
|
|
|
def.SetItems([]models.DefinitionItem{
|
|
{Type: models.ItemTypeURL, URL: text("/"), Changefreq: text("always"), Priority: text("1.0")},
|
|
{Type: "acme-post", Reference: text("7"), URLPattern: text(""), Nesting: true, Changefreq: text("weekly"), Priority: text("0.5")},
|
|
})
|
|
if err := e.gdb.Save(&def).Error; err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
rec = e.get(t, "/sitemap.xml")
|
|
if rec.Code != http.StatusOK {
|
|
t.Fatalf("GET /sitemap.xml: %d %s", rec.Code, rec.Body.String())
|
|
}
|
|
if ct := rec.Header().Get("Content-Type"); ct != "application/xml" {
|
|
t.Fatalf("Content-Type = %q", ct)
|
|
}
|
|
want, err := os.ReadFile(filepath.Join("classes", "testdata", "tracer.golden"))
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if got := rec.Body.String(); got != string(want) {
|
|
t.Fatalf("/sitemap.xml differs from the PHP golden\n got: %q\nwant: %q", got, want)
|
|
}
|
|
|
|
// The stored data keeps the PHP item keys.
|
|
var raw string
|
|
if err := e.gdb.Raw(`SELECT data FROM golem15_sitemap_definitions WHERE id = ?`, def.ID).Scan(&raw).Error; err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
const wantData = `[{"changefreq":"always","priority":"1.0","nesting":false,"type":"url","url":"/","reference":null,"cmsPage":null,"urlPattern":null},{"changefreq":"weekly","priority":"0.5","nesting":true,"type":"acme-post","url":null,"reference":"7","cmsPage":null,"urlPattern":""}]`
|
|
if raw != wantData {
|
|
t.Fatalf("stored data = %s", raw)
|
|
}
|
|
}
|