Skip to content

Commit fd5abfd

Browse files
authored
Fix the settings tab highlighting
When visiting a repos `/settings/units` page, highlight the active tab properly: "Add more..." if the tab is displayed, or "Settings" otherwise. Fixes go-gitea#3188. Signed-off-by: Gergely Nagy <[email protected]> (cherry picked from commit 65ed86e)
1 parent 89d751e commit fd5abfd

File tree

2 files changed

+96
-1
lines changed

2 files changed

+96
-1
lines changed

templates/repo/header.tmpl

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -174,12 +174,14 @@
174174
{{end}}
175175

176176
{{if .Permission.IsAdmin}}
177+
{{$highlightSettings := true}}
177178
{{if and .SignedUser.EnableRepoUnitHints (not (.Repository.AllUnitsEnabled ctx))}}
179+
{{$highlightSettings = false}}
178180
<a class="{{if .PageIsRepoSettingsUnits}}active {{end}}item" href="{{.RepoLink}}/settings/units">
179181
{{svg "octicon-diff-added"}} {{ctx.Locale.Tr "repo.settings.units.add_more"}}
180182
</a>
181183
{{end}}
182-
<a class="{{if and .PageIsRepoSettings (not .PageIsRepoSettingsUnits)}}active {{end}} item" href="{{.RepoLink}}/settings">
184+
<a class="{{if and .PageIsRepoSettings (or $highlightSettings (not .PageIsRepoSettingsUnits))}}active {{end}} item" href="{{.RepoLink}}/settings">
183185
{{svg "octicon-tools"}} {{ctx.Locale.Tr "repo.settings"}}
184186
</a>
185187
{{end}}

tests/integration/repo_settings_test.go

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,11 @@ import (
1414
unit_model "code.gitea.io/gitea/models/unit"
1515
"code.gitea.io/gitea/models/unittest"
1616
user_model "code.gitea.io/gitea/models/user"
17+
"code.gitea.io/gitea/modules/optional"
1718
"code.gitea.io/gitea/modules/setting"
1819
gitea_context "code.gitea.io/gitea/services/context"
1920
repo_service "code.gitea.io/gitea/services/repository"
21+
user_service "code.gitea.io/gitea/services/user"
2022
"code.gitea.io/gitea/tests"
2123

2224
"github.com/stretchr/testify/assert"
@@ -32,6 +34,97 @@ func TestRepoSettingsUnits(t *testing.T) {
3234
session.MakeRequest(t, req, http.StatusOK)
3335
}
3436

37+
func TestRepoAddMoreUnitsHighlighting(t *testing.T) {
38+
defer tests.PrepareTestEnv(t)()
39+
user := unittest.AssertExistsAndLoadBean(t, &user_model.User{Name: "user2"})
40+
session := loginUser(t, user.Name)
41+
42+
// Make sure there are no disabled repos in the settings!
43+
setting.Repository.DisabledRepoUnits = []string{}
44+
unit_model.LoadUnitConfig()
45+
46+
// Create a known-good repo, with some units disabled.
47+
repo, _, f := CreateDeclarativeRepo(t, user, "", []unit_model.Type{
48+
unit_model.TypeCode,
49+
unit_model.TypePullRequests,
50+
unit_model.TypeProjects,
51+
unit_model.TypeActions,
52+
unit_model.TypeIssues,
53+
unit_model.TypeWiki,
54+
}, []unit_model.Type{unit_model.TypePackages}, nil)
55+
defer f()
56+
57+
setUserHints := func(t *testing.T, hints bool) func() {
58+
saved := user.EnableRepoUnitHints
59+
60+
assert.NoError(t, user_service.UpdateUser(db.DefaultContext, user, &user_service.UpdateOptions{
61+
EnableRepoUnitHints: optional.Some(hints),
62+
}))
63+
64+
return func() {
65+
assert.NoError(t, user_service.UpdateUser(db.DefaultContext, user, &user_service.UpdateOptions{
66+
EnableRepoUnitHints: optional.Some(saved),
67+
}))
68+
}
69+
}
70+
71+
assertHighlight := func(t *testing.T, page, uri string, highlighted bool) {
72+
t.Helper()
73+
74+
req := NewRequest(t, "GET", fmt.Sprintf("%s/settings%s", repo.Link(), page))
75+
resp := session.MakeRequest(t, req, http.StatusOK)
76+
htmlDoc := NewHTMLParser(t, resp.Body)
77+
78+
htmlDoc.AssertElement(t, fmt.Sprintf(".overflow-menu-items a[href='%s'].active", fmt.Sprintf("%s/settings%s", repo.Link(), uri)), highlighted)
79+
}
80+
81+
t.Run("hints enabled", func(t *testing.T) {
82+
defer tests.PrintCurrentTest(t)()
83+
defer setUserHints(t, true)()
84+
85+
t.Run("settings", func(t *testing.T) {
86+
defer tests.PrintCurrentTest(t)()
87+
88+
// Visiting the /settings page, "Settings" is highlighted
89+
assertHighlight(t, "", "", true)
90+
// ...but "Add more" isn't.
91+
assertHighlight(t, "", "/units", false)
92+
})
93+
94+
t.Run("units", func(t *testing.T) {
95+
defer tests.PrintCurrentTest(t)()
96+
97+
// Visiting the /settings/units page, "Add more" is highlighted
98+
assertHighlight(t, "/units", "/units", true)
99+
// ...but "Settings" isn't.
100+
assertHighlight(t, "/units", "", false)
101+
})
102+
})
103+
104+
t.Run("hints disabled", func(t *testing.T) {
105+
defer tests.PrintCurrentTest(t)()
106+
defer setUserHints(t, false)()
107+
108+
t.Run("settings", func(t *testing.T) {
109+
defer tests.PrintCurrentTest(t)()
110+
111+
// Visiting the /settings page, "Settings" is highlighted
112+
assertHighlight(t, "", "", true)
113+
// ...but "Add more" isn't (it doesn't exist).
114+
assertHighlight(t, "", "/units", false)
115+
})
116+
117+
t.Run("units", func(t *testing.T) {
118+
defer tests.PrintCurrentTest(t)()
119+
120+
// Visiting the /settings/units page, "Settings" is highlighted
121+
assertHighlight(t, "/units", "", true)
122+
// ...but "Add more" isn't (it doesn't exist)
123+
assertHighlight(t, "/units", "/units", false)
124+
})
125+
})
126+
}
127+
35128
func TestRepoAddMoreUnits(t *testing.T) {
36129
defer tests.PrepareTestEnv(t)()
37130
user := unittest.AssertExistsAndLoadBean(t, &user_model.User{Name: "user2"})

0 commit comments

Comments
 (0)