feat: 可配置站点 URL 风格
- 新增 urlStyle 工具与测试:postPath/userPath/sitePagePath 统一生成链接 - 管理端新增 UrlStylePanel 设置面板,settings 服务与 handler 适配 - 全站路由链接与 JSON-LD/sitemap 切换为统一 URL 生成 - middleware 与相关组件适配
This commit is contained in:
112
backend/service/url_style_test.go
Normal file
112
backend/service/url_style_test.go
Normal file
@@ -0,0 +1,112 @@
|
||||
package service
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/freefire/jiang13-bbs/model"
|
||||
"github.com/glebarez/sqlite"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
func newUrlStyleTestService(t *testing.T) *SettingService {
|
||||
t.Helper()
|
||||
db, err := gorm.Open(sqlite.Open(":memory:"), &gorm.Config{})
|
||||
if err != nil {
|
||||
t.Fatalf("open sqlite: %v", err)
|
||||
}
|
||||
if err := db.AutoMigrate(&model.SiteSetting{}); err != nil {
|
||||
t.Fatalf("migrate: %v", err)
|
||||
}
|
||||
return NewSettingService(db)
|
||||
}
|
||||
|
||||
func TestUrlStyleDefaultWhenMissing(t *testing.T) {
|
||||
s := newUrlStyleTestService(t)
|
||||
got, err := s.UrlStyle()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if got != DefaultUrlStyle {
|
||||
t.Fatalf("missing row should fall back to %q, got %q", DefaultUrlStyle, got)
|
||||
}
|
||||
pub, err := s.Public()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if pub.UrlStyle != DefaultUrlStyle {
|
||||
t.Fatalf("Public().UrlStyle = %q, want %q", pub.UrlStyle, DefaultUrlStyle)
|
||||
}
|
||||
}
|
||||
|
||||
func TestSetUrlStyleRoundTrip(t *testing.T) {
|
||||
s := newUrlStyleTestService(t)
|
||||
for _, style := range []string{"html", "discuz"} {
|
||||
if err := s.SetUrlStyle(style); err != nil {
|
||||
t.Fatalf("SetUrlStyle(%q): %v", style, err)
|
||||
}
|
||||
got, err := s.UrlStyle()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if got != style {
|
||||
t.Fatalf("UrlStyle() = %q, want %q", got, style)
|
||||
}
|
||||
pub, err := s.Public()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if pub.UrlStyle != style {
|
||||
t.Fatalf("Public().UrlStyle = %q, want %q", pub.UrlStyle, style)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestSetUrlStyleDefaultDeletesKey(t *testing.T) {
|
||||
s := newUrlStyleTestService(t)
|
||||
if err := s.SetUrlStyle("html"); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
// 传 default 应删键,恢复「缺行=默认」语义(大小写与空白容错)
|
||||
if err := s.SetUrlStyle(" DEFAULT "); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
got, err := s.UrlStyle()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if got != DefaultUrlStyle {
|
||||
t.Fatalf("after reset got %q, want %q", got, DefaultUrlStyle)
|
||||
}
|
||||
var rows int64
|
||||
s.db.Where("key = ?", SettingKeyUrlStyle).Model(&model.SiteSetting{}).Count(&rows)
|
||||
if rows != 0 {
|
||||
t.Fatalf("default should delete the row, rows=%d", rows)
|
||||
}
|
||||
}
|
||||
|
||||
func TestSetUrlStyleRejectsInvalid(t *testing.T) {
|
||||
s := newUrlStyleTestService(t)
|
||||
for _, bad := range []string{"", "pretty", "HTML5", "thread", "../etc"} {
|
||||
if err := s.SetUrlStyle(bad); err == nil {
|
||||
t.Fatalf("SetUrlStyle(%q) should fail", bad)
|
||||
}
|
||||
}
|
||||
// 非法值不应落库
|
||||
if _, err := s.UrlStyle(); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestUrlStyleInvalidStoredValueFallsBack(t *testing.T) {
|
||||
s := newUrlStyleTestService(t)
|
||||
if err := s.putValue(SettingKeyUrlStyle, "nonsense"); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
got, err := s.UrlStyle()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if got != DefaultUrlStyle {
|
||||
t.Fatalf("invalid stored value should fall back to %q, got %q", DefaultUrlStyle, got)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user