Debian Codegraph @

hugolib/page_permalink_test.go

raw
1 // Copyright 2025 The Hugo Authors. All rights reserved.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 // http://www.apache.org/licenses/LICENSE-2.0
7 //
8 // Unless required by applicable law or agreed to in writing, software
9 // distributed under the License is distributed on an "AS IS" BASIS,
10 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11 // See the License for the specific language governing permissions and
12 // limitations under the License.
13
14 package hugolib
15
16 import (
17 "fmt"
18 "html/template"
19 "testing"
20
21 qt "github.com/frankban/quicktest"
22 )
23
24 func TestPermalink(t *testing.T) {
25 t.Parallel()
26
27 tests := []struct {
28 file string
29 base template.URL
30 slug string
31 url string
32 uglyURLs bool
33 canonifyURLs bool
34 expectedAbs string
35 expectedRel string
36 }{
37 {"x/y/z/boofar.md", "", "", "", false, false, "/x/y/z/boofar/", "/x/y/z/boofar/"},
38 {"x/y/z/boofar.md", "", "", "", false, false, "/x/y/z/boofar/", "/x/y/z/boofar/"},
39 // Issue #1174
40 {"x/y/z/boofar.md", "http://gopher.com/", "", "", false, true, "http://gopher.com/x/y/z/boofar/", "/x/y/z/boofar/"},
41 {"x/y/z/boofar.md", "http://gopher.com/", "", "", true, true, "http://gopher.com/x/y/z/boofar.html", "/x/y/z/boofar.html"},
42 {"x/y/z/boofar.md", "", "boofar", "", false, false, "/x/y/z/boofar/", "/x/y/z/boofar/"},
43 {"x/y/z/boofar.md", "http://barnew/", "", "", false, false, "http://barnew/x/y/z/boofar/", "/x/y/z/boofar/"},
44 {"x/y/z/boofar.md", "http://barnew/", "boofar", "", false, false, "http://barnew/x/y/z/boofar/", "/x/y/z/boofar/"},
45 {"x/y/z/boofar.md", "", "", "", true, false, "/x/y/z/boofar.html", "/x/y/z/boofar.html"},
46 {"x/y/z/boofar.md", "", "", "", true, false, "/x/y/z/boofar.html", "/x/y/z/boofar.html"},
47 {"x/y/z/boofar.md", "", "boofar", "", true, false, "/x/y/z/boofar.html", "/x/y/z/boofar.html"},
48 {"x/y/z/boofar.md", "http://barnew/", "", "", true, false, "http://barnew/x/y/z/boofar.html", "/x/y/z/boofar.html"},
49 {"x/y/z/boofar.md", "http://barnew/", "boofar", "", true, false, "http://barnew/x/y/z/boofar.html", "/x/y/z/boofar.html"},
50 {"x/y/z/boofar.md", "http://barnew/boo/", "booslug", "", true, false, "http://barnew/boo/x/y/z/booslug.html", "/boo/x/y/z/booslug.html"},
51 {"x/y/z/boofar.md", "http://barnew/boo/", "booslug", "", false, true, "http://barnew/boo/x/y/z/booslug/", "/x/y/z/booslug/"},
52 {"x/y/z/boofar.md", "http://barnew/boo/", "booslug", "", false, false, "http://barnew/boo/x/y/z/booslug/", "/boo/x/y/z/booslug/"},
53 {"x/y/z/boofar.md", "http://barnew/boo/", "booslug", "", true, true, "http://barnew/boo/x/y/z/booslug.html", "/x/y/z/booslug.html"},
54 {"x/y/z/boofar.md", "http://barnew/boo", "booslug", "", true, true, "http://barnew/boo/x/y/z/booslug.html", "/x/y/z/booslug.html"},
55 // Issue #4666
56 {"x/y/z/boo-makeindex.md", "http://barnew/boo", "", "", true, true, "http://barnew/boo/x/y/z/boo-makeindex.html", "/x/y/z/boo-makeindex.html"},
57
58 // test URL overrides
59 {"x/y/z/boofar.md", "", "", "/z/y/q/", false, false, "/z/y/q/", "/z/y/q/"},
60 // test URL override with expands
61 {"x/y/z/boofar.md", "", "test", "/z/:slug/", false, false, "/z/test/", "/z/test/"},
62 }
63
64 for i, test := range tests {
65 t.Run(fmt.Sprintf("%s-%d", test.file, i), func(t *testing.T) {
66 t.Parallel()
67 c := qt.New(t)
68
69 files := fmt.Sprintf(`
70 -- hugo.toml --
71 baseURL = %q
72 uglyURLs = %t
73 canonifyURLs = %t
74 -- content/%s --
75 ---
76 title: Page
77 slug: %q
78 url: %q
79 output: ["HTML"]
80 ---
81 `, test.base, test.uglyURLs, test.canonifyURLs, test.file, test.slug, test.url)
82
83 b := Test(t, files)
84 s := b.H.Sites[0]
85 c.Assert(len(s.RegularPages()), qt.Equals, 1)
86 p := s.RegularPages()[0]
87 u := p.Permalink()
88
89 expected := test.expectedAbs
90 if u != expected {
91 t.Fatalf("[%d] Expected abs url: %s, got: %s", i, expected, u)
92 }
93
94 u = p.RelPermalink()
95
96 expected = test.expectedRel
97 if u != expected {
98 t.Errorf("[%d] Expected rel url: %s, got: %s", i, expected, u)
99 }
100 })
101 }
102 }
103
104 func TestRelativeURLInFrontMatter(t *testing.T) {
105 t.Parallel()
106
107 files := `
108 -- hugo.toml --
109 baseURL = "https://example.com"
110 defaultContentLanguage = "en"
111 defaultContentLanguageInSubdir = false
112
113 [Languages]
114 [Languages.en]
115 weight = 10
116 contentDir = "content/en"
117 [Languages.nn]
118 weight = 20
119 contentDir = "content/nn"
120 -- layouts/single.html --
121 Single: {{ .Title }}|Hello|{{ .Lang }}|RelPermalink: {{ .RelPermalink }}|Permalink: {{ .Permalink }}|
122 -- layouts/list.html --
123 List Page 1|{{ .Title }}|Hello|{{ .Permalink }}|
124 -- content/en/blog/page1.md --
125 ---
126 title: "A page"
127 url: "myblog/p1/"
128 ---
129
130 Some content.
131 -- content/en/blog/page2.md --
132 ---
133 title: "A page"
134 url: "../../../../../myblog/p2/"
135 ---
136
137 Some content.
138 -- content/en/blog/page3.md --
139 ---
140 title: "A page"
141 url: "../myblog/../myblog/p3/"
142 ---
143
144 Some content.
145 -- content/en/blog/_index.md --
146 ---
147 title: "A page"
148 url: "this-is-my-english-blog"
149 ---
150
151 Some content.
152 -- content/nn/blog/page1.md --
153 ---
154 title: "A page"
155 url: "myblog/p1/"
156 ---
157
158 Some content.
159 -- content/nn/blog/_index.md --
160 ---
161 title: "A page"
162 url: "this-is-my-blog"
163 ---
164
165 Some content.
166 `
167 b := Test(t, files)
168
169 b.AssertFileContent("public/nn/myblog/p1/index.html", "Single: A page|Hello|nn|RelPermalink: /nn/myblog/p1/|")
170 b.AssertFileContent("public/nn/this-is-my-blog/index.html", "List Page 1|A page|Hello|https://example.com/nn/this-is-my-blog/|")
171 b.AssertFileContent("public/this-is-my-english-blog/index.html", "List Page 1|A page|Hello|https://example.com/this-is-my-english-blog/|")
172 b.AssertFileContent("public/myblog/p1/index.html", "Single: A page|Hello|en|RelPermalink: /myblog/p1/|Permalink: https://example.com/myblog/p1/|")
173 b.AssertFileContent("public/myblog/p2/index.html", "Single: A page|Hello|en|RelPermalink: /myblog/p2/|Permalink: https://example.com/myblog/p2/|")
174 b.AssertFileContent("public/myblog/p3/index.html", "Single: A page|Hello|en|RelPermalink: /myblog/p3/|Permalink: https://example.com/myblog/p3/|")
175 }
176
177 // Issue 13869
178 func TestPermalinksSliceFromModule13869(t *testing.T) {
179 t.Parallel()
180
181 files := `
182 -- hugo.toml --
183 disableKinds = ['home','rss','section','sitemap','taxonomy','term']
184 theme = 'foo'
185 [permalinks]
186 _merge = 'deep'
187 -- content/books/my-book.md --
188 ---
189 title: My Book
190 ---
191 -- layouts/page.html --
192 RelPermalink: {{ .RelPermalink }}
193 -- themes/foo/hugo.toml --
194 [[permalinks]]
195 pattern = '/shelf/:slug/'
196 [permalinks.target]
197 path = '/books/**'
198 `
199
200 b := Test(t, files)
201
202 b.AssertFileContent("public/shelf/my-book/index.html", "RelPermalink: /shelf/my-book/")
203 }