feat: SEO change history

This commit is contained in:
2026-10-27 10:45:30 +01:00
parent 9f1082d62d
commit e8a6b9fc77
21 changed files with 1070 additions and 26 deletions

View File

@@ -1,12 +1,16 @@
package seo_test
import (
"encoding/json"
"fmt"
"net/http"
"net/url"
"strings"
"testing"
seo "git.golem15.com/golem15/sm-seo-plugin"
"git.golem15.com/golem15/sm-seo-plugin/classes"
"git.golem15.com/golem15/summercms/modules/bouncer"
)
// seoTabLabel is the English label of seo.TabSEO.
@@ -124,3 +128,96 @@ func TestSEOTabPermission(t *testing.T) {
t.Fatalf("editor show carries SEO values: %s", show.Body.String())
}
}
// revisionRow is a history row as the tests read it.
type revisionRow struct {
Via string
SubjectKind string
SubjectID string
What string
AuthorLabel string
Before string
After string
}
func (e *testEnv) revisions(t *testing.T, kind string) []revisionRow {
t.Helper()
var rows []revisionRow
if err := e.db.Raw(`SELECT via, subject_kind, subject_id, what, author_label, COALESCE(before::text, '') AS before, COALESCE(after::text, '') AS after
FROM golem15_seo_revisions WHERE subject_kind = ? ORDER BY id`, kind).Scan(&rows).Error; err != nil {
t.Fatal(err)
}
return rows
}
// TestHistoryRecordsAdminWrite checks D-13 for admin writes: an SEO tab
// save writes one revision with via admin, the author and the changed
// field before and after; an unchanged save writes none; rule create,
// update and delete and a settings save are recorded too, and the history
// list and preview serve them.
func TestHistoryRecordsAdminWrite(t *testing.T) {
env := bootEnv(t)
token := env.admin(t, true)
id := env.createPage(t, token, map[string]any{"title": "About", "slug": "about", "published": true, "seo_title": map[string]any{"en": "First"}})
env.expect(t, http.StatusOK, http.MethodPut, adminAPI("/acme/pages/pages/"+id), token, map[string]any{
"title": "About", "slug": "about", "published": true, "seo_title": map[string]any{"en": "Second"},
})
env.expect(t, http.StatusOK, http.MethodPut, adminAPI("/acme/pages/pages/"+id), token, map[string]any{
"title": "About", "slug": "about", "published": true, "seo_title": map[string]any{"en": "Second"},
})
rows := env.revisions(t, "entry")
if len(rows) != 2 {
t.Fatalf("entry revisions = %+v, want create and one change", rows)
}
last := rows[1]
if last.Via != "admin" || last.SubjectID != pagesModel+"|"+id+"|en" || !strings.Contains(last.What, "About") || !strings.Contains(last.What, "(en)") || last.AuthorLabel == "" {
t.Fatalf("revision = %+v", last)
}
if !jsonContains(last.Before, `"title": "First"`) || !jsonContains(last.After, `"title": "Second"`) || strings.Contains(last.After, "description") {
t.Fatalf("revision before/after = %s / %s, want the title only", last.Before, last.After)
}
rec := env.expect(t, http.StatusCreated, http.MethodPost, adminAPI("/golem15/seo/rules"), token, map[string]any{"url": "/landing", "match": "exact", "title": "Landing"})
var created struct {
Data struct {
ID json.Number `json:"id"`
} `json:"data"`
}
if err := json.Unmarshal(rec.Body.Bytes(), &created); err != nil {
t.Fatal(err)
}
ruleID := created.Data.ID.String()
env.expect(t, http.StatusOK, http.MethodPut, adminAPI("/golem15/seo/rules/"+ruleID), token, map[string]any{"url": "/landing", "match": "exact", "title": "Landing page"})
env.expect(t, http.StatusOK, http.MethodDelete, adminAPI("/golem15/seo/rules/"+ruleID), token, nil)
rules := env.revisions(t, "rule")
if len(rules) != 3 || rules[0].Before != "{}" || !jsonContains(rules[1].After, `"title": "Landing page"`) || rules[2].After != "{}" || !jsonContains(rules[2].Before, `"url": "/landing"`) {
t.Fatalf("rule revisions = %+v", rules)
}
env.expect(t, http.StatusOK, http.MethodPut, adminAPI("/golem15/seo/settings/1"), token, map[string]any{"twitter_site": "@acme", "type_acme_page_robots": "noindex,follow"})
settings := env.revisions(t, "settings")
if len(settings) != 2 || !jsonContains(settings[0].After, `"twitter_site": "@acme"`) || settings[1].SubjectID != "type:"+pagesTypeKey {
t.Fatalf("settings revisions = %+v", settings)
}
list := env.expect(t, http.StatusOK, http.MethodGet, adminAPI("/golem15/seo/history"), token, nil)
if !strings.Contains(list.Body.String(), "SEO title: First → Second") || !strings.Contains(list.Body.String(), `"via_label":"Admin"`) {
t.Fatalf("history list = %s", list.Body.String())
}
var firstID uint
if err := env.db.Raw(`SELECT id FROM golem15_seo_revisions WHERE subject_kind = 'entry' ORDER BY id DESC LIMIT 1`).Scan(&firstID).Error; err != nil {
t.Fatal(err)
}
partial := env.expect(t, http.StatusOK, http.MethodGet, adminAPI(fmt.Sprintf("/golem15/seo/history/partials/history_diff?id=%d", firstID)), token, nil)
if !strings.Contains(partial.Body.String(), "Second") {
t.Fatalf("history diff partial = %s", partial.Body.String())
}
mcp := bouncer.WithDispatch(t.Context(), &bouncer.Dispatch{Guards: []string{"backend"}, Principal: &bouncer.Principal{ID: 1, Backend: true}})
if got := classes.ViaOf(mcp); got != "mcp" {
t.Fatalf("ViaOf(MCP dispatch) = %q, want mcp", got)
}
if got := classes.ViaOf(classes.WithVia(mcp, "import")); got != "import" {
t.Fatalf("ViaOf(WithVia import) = %q", got)
}
}