From 4ef7e496b878911fd82f9036bde886ca725c4449 Mon Sep 17 00:00:00 2001 From: wxiaoguang Date: Mon, 4 Mar 2024 13:50:55 +0800 Subject: [PATCH] Add a trailing slash to dashboard links (#29555) (#29573) Backport #29555 --- modules/contexttest/context_tests.go | 4 +++- modules/translation/mock.go | 4 +++- modules/translation/translation.go | 2 +- routers/web/user/home_test.go | 17 +++++++++++++++++ templates/base/paginate.tmpl | 14 ++++++++------ 5 files changed, 32 insertions(+), 9 deletions(-) diff --git a/modules/contexttest/context_tests.go b/modules/contexttest/context_tests.go index 97d912ea57..b9c1ab2964 100644 --- a/modules/contexttest/context_tests.go +++ b/modules/contexttest/context_tests.go @@ -12,6 +12,7 @@ import ( "net/url" "strings" "testing" + "time" access_model "code.gitea.io/gitea/models/perm/access" repo_model "code.gitea.io/gitea/models/repo" @@ -61,7 +62,8 @@ func MockContext(t *testing.T, reqPath string, opts ...MockContextOption) (*cont base.Locale = &translation.MockLocale{} ctx := context.NewWebContext(base, opt.Render, nil) - + ctx.PageData = map[string]any{} + ctx.Data["PageStartTime"] = time.Now() chiCtx := chi.NewRouteContext() ctx.Base.AppendContextValue(chi.RouteCtxKey, chiCtx) return ctx, resp diff --git a/modules/translation/mock.go b/modules/translation/mock.go index 2d0cb17324..d4c60e2517 100644 --- a/modules/translation/mock.go +++ b/modules/translation/mock.go @@ -6,7 +6,9 @@ package translation import "fmt" // MockLocale provides a mocked locale without any translations -type MockLocale struct{} +type MockLocale struct { + Lang, LangName string // these fields are used directly in templates: ctx.Locale.Lang +} var _ Locale = (*MockLocale)(nil) diff --git a/modules/translation/translation.go b/modules/translation/translation.go index dba4de6607..0acfe48c97 100644 --- a/modules/translation/translation.go +++ b/modules/translation/translation.go @@ -140,7 +140,7 @@ func Match(tags ...language.Tag) language.Tag { // locale represents the information of localization. type locale struct { i18n.Locale - Lang, LangName string // these fields are used directly in templates: .i18n.Lang + Lang, LangName string // these fields are used directly in templates: ctx.Locale.Lang msgPrinter *message.Printer } diff --git a/routers/web/user/home_test.go b/routers/web/user/home_test.go index 1eb1fad057..418dac7bb3 100644 --- a/routers/web/user/home_test.go +++ b/routers/web/user/home_test.go @@ -10,8 +10,10 @@ import ( "code.gitea.io/gitea/models/db" repo_model "code.gitea.io/gitea/models/repo" "code.gitea.io/gitea/models/unittest" + "code.gitea.io/gitea/modules/context" "code.gitea.io/gitea/modules/contexttest" "code.gitea.io/gitea/modules/setting" + "code.gitea.io/gitea/modules/templates" "github.com/stretchr/testify/assert" ) @@ -117,3 +119,18 @@ func TestMilestonesForSpecificRepo(t *testing.T) { assert.Len(t, ctx.Data["Milestones"], 1) assert.Len(t, ctx.Data["Repos"], 2) // both repo 42 and 1 have milestones and both are owned by user 2 } + +func TestDashboardPagination(t *testing.T) { + ctx, _ := contexttest.MockContext(t, "/", contexttest.MockContextOption{Render: templates.HTMLRenderer()}) + page := context.NewPagination(10, 3, 1, 3) + + setting.AppSubURL = "/SubPath" + out, err := ctx.RenderToString("base/paginate", map[string]any{"Link": setting.AppSubURL, "Page": page}) + assert.NoError(t, err) + assert.Contains(t, out, ``) + + setting.AppSubURL = "" + out, err = ctx.RenderToString("base/paginate", map[string]any{"Link": setting.AppSubURL, "Page": page}) + assert.NoError(t, err) + assert.Contains(t, out, ``) +} diff --git a/templates/base/paginate.tmpl b/templates/base/paginate.tmpl index 503817c339..ef7d0b341b 100644 --- a/templates/base/paginate.tmpl +++ b/templates/base/paginate.tmpl @@ -1,13 +1,15 @@ -{{$paginationLink := .Page.GetParams}} +{{$paginationParams := .Page.GetParams}} +{{$paginationLink := $.Link}} +{{if eq $paginationLink AppSubUrl}}{{$paginationLink = print $paginationLink "/"}}{{end}} {{with .Page.Paginater}} {{if gt .TotalPages 1}}