Debian Codegraph @

hugolib/paginator_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 "strings"
19 "testing"
20
21 qt "github.com/frankban/quicktest"
22 )
23
24 func TestPaginator(t *testing.T) {
25 var files strings.Builder
26 files.WriteString(`
27 -- hugo.toml --
28 baseURL = "https://example.com/foo/"
29
30 [pagination]
31 pagerSize = 3
32 path = "thepage"
33
34 [languages.en]
35 weight = 1
36 contentDir = "content/en"
37
38 [languages.nn]
39 weight = 2
40 contentDir = "content/nn"
41 `)
42 // Manually generate content files for the txtar string
43 for i := range 9 {
44 files.WriteString(fmt.Sprintf(`
45 -- content/en/blog/page%d.md --
46 ---
47 title: Page %d
48 ---
49
50 Content.
51 `, i, i))
52 files.WriteString(fmt.Sprintf(`
53 -- content/nn/blog/page%d.md --
54 ---
55 title: Page %d
56 ---
57
58 Content.
59 `, i, i))
60 }
61
62 files.WriteString(`
63 -- layouts/home.html --
64 {{ $pag := $.Paginator }}
65 Total: {{ $pag.TotalPages }}
66 First: {{ $pag.First.URL }}
67 Page Number: {{ $pag.PageNumber }}
68 URL: {{ $pag.URL }}
69 {{ with $pag.Next }}Next: {{ .URL }}{{ end }}
70 {{ with $pag.Prev }}Prev: {{ .URL }}{{ end }}
71 {{ range $i, $e := $pag.Pagers }}
72 {{ printf "%d: %d/%d %t" $i $pag.PageNumber .PageNumber (eq . $pag) -}}
73 {{ end }}
74 -- layouts/index.xml --
75 {{ $pag := $.Paginator }}
76 Total: {{ $pag.TotalPages }}
77 First: {{ $pag.First.URL }}
78 Page Number: {{ $pag.PageNumber }}
79 URL: {{ $pag.URL }}
80 {{ with $pag.Next }}Next: {{ .URL }}{{ end }}
81 {{ with $pag.Prev }}Prev: {{ .URL }}{{ end }}
82 {{ range $i, $e := $pag.Pagers }}
83 {{ printf "%d: %d/%d %t" $i $pag.PageNumber .PageNumber (eq . $pag) -}}
84 {{ end }}
85 `)
86
87 b := Test(t, files.String())
88
89 b.AssertFileContent("public/index.html",
90 "Page Number: 1",
91 "0: 1/1 true")
92
93 b.AssertFileContent("public/thepage/2/index.html",
94 "Total: 3",
95 "Page Number: 2",
96 "URL: /foo/thepage/2/",
97 "Next: /foo/thepage/3/",
98 "Prev: /foo/",
99 "1: 2/2 true",
100 )
101
102 b.AssertFileContent("public/index.xml",
103 "Page Number: 1",
104 "0: 1/1 true")
105 b.AssertFileContent("public/thepage/2/index.xml",
106 "Page Number: 2",
107 "1: 2/2 true")
108
109 b.AssertFileContent("public/nn/index.html",
110 "Page Number: 1",
111 "0: 1/1 true")
112
113 b.AssertFileContent("public/nn/index.xml",
114 "Page Number: 1",
115 "0: 1/1 true")
116 }
117
118 // Issue 6023
119 func TestPaginateWithSort(t *testing.T) {
120 files := `
121 -- hugo.toml --
122 -- content/a/a.md --
123 -- content/z/b.md --
124 -- content/x/b.md --
125 -- content/x/a.md --
126 -- layouts/home.html --
127 Paginate: {{ range (.Paginate (sort .Site.RegularPages ".File.Filename" "desc")).Pages }}|{{ .Path }}{{ end }}
128 `
129 b := Test(t, files)
130
131 b.AssertFileContent("public/index.html", "Paginate: |/z/b|/x/b|/x/a|/a/a")
132 }
133
134 // https://github.com/gohugoio/hugo/issues/6797
135 func TestPaginateOutputFormat(t *testing.T) {
136 var files strings.Builder
137 files.WriteString(`
138 -- hugo.toml --
139 baseURL = "http://example.com/"
140 -- content/_index.md --
141 ---
142 title: "Home"
143 cascade:
144 outputs:
145 - JSON
146 ---
147 `)
148 for i := range 22 {
149 files.WriteString(fmt.Sprintf(`
150 -- content/p%d.md --
151 ---
152 title: "Page"
153 weight: %d
154 ---
155 `, i+1, i+1))
156 }
157
158 files.WriteString(`
159 -- layouts/index.json --
160 JSON: {{ .Paginator.TotalNumberOfElements }}: {{ range .Paginator.Pages }}|{{ .RelPermalink }}{{ end }}:DONE
161 `)
162
163 b := Test(t, files.String())
164
165 b.AssertFileContent("public/index.json",
166 `JSON: 22
167 |/p1/index.json|/p2/index.json|
168 `)
169
170 // This looks odd, so are most bugs.
171 b.AssertFileExists("public/page/1/index.json/index.html", false)
172 b.AssertFileExists("public/page/1/index.json", false)
173 b.AssertFileContent("public/page/2/index.json", `JSON: 22: |/p11/index.json|/p12/index.json`)
174 }
175
176 // Issue 10802
177 func TestPaginatorEmptyPageGroups(t *testing.T) {
178 t.Parallel()
179
180 files := `
181 -- hugo.toml --
182 baseURL = "https://example.com/"
183 -- content/p1.md --
184 -- content/p2.md --
185 -- layouts/home.html --
186 {{ $empty := site.RegularPages | complement site.RegularPages }}
187 Len: {{ len $empty }}: Type: {{ printf "%T" $empty }}
188 {{ $pgs := $empty.GroupByPublishDate "January 2006" }}
189 {{ $pag := .Paginate $pgs }}
190 Len Pag: {{ len $pag.Pages }}
191 `
192 b := Test(t, files)
193
194 b.AssertFileContent("public/index.html", "Len: 0", "Len Pag: 0")
195 }
196
197 func TestPaginatorNodePagesOnly(t *testing.T) {
198 files := `
199 -- hugo.toml --
200 [pagination]
201 pagerSize = 1
202 -- content/p1.md --
203 -- layouts/single.html --
204 Paginator: {{ .Paginator }}
205 `
206 b, err := TestE(t, files)
207 b.Assert(err, qt.IsNotNil)
208 b.Assert(err.Error(), qt.Contains, `error calling Paginator: pagination not supported for this page`)
209 }
210
211 func TestNilPointerErrorMessage(t *testing.T) {
212 files := `
213 -- hugo.toml --
214 -- content/p1.md --
215 -- layouts/single.html --
216 Home Filename: {{ site.Home.File.Filename }}
217 `
218 b, err := TestE(t, files)
219 b.Assert(err, qt.IsNotNil)
220 b.Assert(err.Error(), qt.Contains, `single.html:1:22: executing "single.html" – File is nil; wrap it in if or with: {{ with site.Home.File }}{{ .Filename }}{{ end }}`)
221 }