paginator_test.go (3725B)
1 // Copyright 2019 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 "path/filepath"
19 "testing"
20
21 qt "github.com/frankban/quicktest"
22 )
23
24 func TestPaginator(t *testing.T) {
25 configFile := `
26 baseURL = "https://example.com/foo/"
27 paginate = 3
28 paginatepath = "thepage"
29
30 [languages.en]
31 weight = 1
32 contentDir = "content/en"
33
34 [languages.nn]
35 weight = 2
36 contentDir = "content/nn"
37
38 `
39 b := newTestSitesBuilder(t).WithConfigFile("toml", configFile)
40 var content []string
41 for i := 0; i < 9; i++ {
42 for _, contentDir := range []string{"content/en", "content/nn"} {
43 content = append(content, fmt.Sprintf(contentDir+"/blog/page%d.md", i), fmt.Sprintf(`---
44 title: Page %d
45 ---
46
47 Content.
48 `, i))
49 }
50 }
51
52 b.WithContent(content...)
53
54 pagTemplate := `
55 {{ $pag := $.Paginator }}
56 Total: {{ $pag.TotalPages }}
57 First: {{ $pag.First.URL }}
58 Page Number: {{ $pag.PageNumber }}
59 URL: {{ $pag.URL }}
60 {{ with $pag.Next }}Next: {{ .URL }}{{ end }}
61 {{ with $pag.Prev }}Prev: {{ .URL }}{{ end }}
62 {{ range $i, $e := $pag.Pagers }}
63 {{ printf "%d: %d/%d %t" $i $pag.PageNumber .PageNumber (eq . $pag) -}}
64 {{ end }}
65 `
66
67 b.WithTemplatesAdded("index.html", pagTemplate)
68 b.WithTemplatesAdded("index.xml", pagTemplate)
69
70 b.Build(BuildCfg{})
71
72 b.AssertFileContent("public/index.html",
73 "Page Number: 1",
74 "0: 1/1 true")
75
76 b.AssertFileContent("public/thepage/2/index.html",
77 "Total: 3",
78 "Page Number: 2",
79 "URL: /foo/thepage/2/",
80 "Next: /foo/thepage/3/",
81 "Prev: /foo/",
82 "1: 2/2 true",
83 )
84
85 b.AssertFileContent("public/index.xml",
86 "Page Number: 1",
87 "0: 1/1 true")
88 b.AssertFileContent("public/thepage/2/index.xml",
89 "Page Number: 2",
90 "1: 2/2 true")
91
92 b.AssertFileContent("public/nn/index.html",
93 "Page Number: 1",
94 "0: 1/1 true")
95
96 b.AssertFileContent("public/nn/index.xml",
97 "Page Number: 1",
98 "0: 1/1 true")
99 }
100
101 // Issue 6023
102 func TestPaginateWithSort(t *testing.T) {
103 b := newTestSitesBuilder(t).WithSimpleConfigFile()
104 b.WithTemplatesAdded("index.html", `{{ range (.Paginate (sort .Site.RegularPages ".File.Filename" "desc")).Pages }}|{{ .File.Filename }}{{ end }}`)
105 b.Build(BuildCfg{}).AssertFileContent("public/index.html",
106 filepath.FromSlash("|content/sect/doc1.nn.md|content/sect/doc1.nb.md|content/sect/doc1.fr.md|content/sect/doc1.en.md"))
107 }
108
109 // https://github.com/gohugoio/hugo/issues/6797
110 func TestPaginateOutputFormat(t *testing.T) {
111 b := newTestSitesBuilder(t).WithSimpleConfigFile()
112 b.WithContent("_index.md", `---
113 title: "Home"
114 cascade:
115 outputs:
116 - JSON
117 ---`)
118
119 for i := 0; i < 22; i++ {
120 b.WithContent(fmt.Sprintf("p%d.md", i+1), fmt.Sprintf(`---
121 title: "Page"
122 weight: %d
123 ---`, i+1))
124 }
125
126 b.WithTemplatesAdded("index.json", `JSON: {{ .Paginator.TotalNumberOfElements }}: {{ range .Paginator.Pages }}|{{ .RelPermalink }}{{ end }}:DONE`)
127 b.Build(BuildCfg{})
128
129 b.AssertFileContent("public/index.json",
130 `JSON: 22
131 |/p1/index.json|/p2/index.json|
132 `)
133
134 // This looks odd, so are most bugs.
135 b.Assert(b.CheckExists("public/page/1/index.json/index.html"), qt.Equals, false)
136 b.Assert(b.CheckExists("public/page/1/index.json"), qt.Equals, false)
137 b.AssertFileContent("public/page/2/index.json", `JSON: 22: |/p11/index.json|/p12/index.json`)
138 }