Debian Codegraph @

tpl/page/page_integration_test.go

raw
1 // Copyright 2024 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 page_test
15
16 import (
17 "fmt"
18 "strings"
19 "testing"
20
21 "github.com/gohugoio/hugo/hugolib"
22 )
23
24 func TestThatPageIsAvailableEverywhere(t *testing.T) {
25 t.Parallel()
26
27 filesTemplate := `
28 -- hugo.toml --
29 baseURL = 'http://example.com/'
30 disableKinds = ["taxonomy", "term"]
31 enableInlineShortcodes = true
32 enableRobotsTXT = true
33 [pagination]
34 pagerSize = 1
35 LANG_CONFIG
36 -- content/_index.md --
37 ---
38 title: "Home"
39 aliases: ["/homealias/"]
40 ---
41 {{< shortcode "Angled Brackets" >}}
42 {{% shortcode "Percentage" %}}
43
44 {{< outer >}}
45 {{< inner >}}
46 {{< /outer >}}
47
48 {{< foo.inline >}}{{ if page.IsHome }}Shortcode Inline OK.{{ end }}{{< /foo.inline >}}
49
50 ## Heading
51
52 [I'm an inline-style link](https://www.google.com)
53
54 ![alt text](https://github.com/adam-p/markdown-here/raw/master/src/common/images/icon48.png "Logo Title Text 1")
55
56 $$$bash
57 echo "hello";
58 $$$
59
60 -- content/p1.md --
61 -- content/p2/index.md --
62 -- content/p2/p2_1.md --
63 ---
64 title: "P2_1"
65 ---
66 {{< foo.inline >}}{{ if page.IsHome }}Shortcode in bundled page OK.{{ else}}Failed.{{ end }}{{< /foo.inline >}}
67 -- content/p3.md --
68 -- layouts/_markup/render-heading.html --
69 {{ if page.IsHome }}
70 Heading OK.
71 {{ end }}
72 -- layouts/_markup/render-image.html --
73 {{ if page.IsHome }}
74 Image OK.
75 {{ end }}
76 -- layouts/_markup/render-link.html --
77 {{ if page.IsHome }}
78 Link OK.
79 {{ end }}
80 -- layouts/myview.html --
81 {{ if page.IsHome }}
82 Render OK.
83 {{ end }}
84 -- layouts/_markup/render-codeblock.html --
85 {{ if page.IsHome }}
86 Codeblock OK.
87 {{ end }}
88 -- layouts/single.html --
89 Single.
90 -- layouts/home.html --
91 {{ if eq page . }}Page OK.{{ end }}
92 {{ $r := "{{ if page.IsHome }}ExecuteAsTemplate OK.{{ end }}" | resources.FromString "foo.html" | resources.ExecuteAsTemplate "foo.html" . }}
93 {{ $r.Content }}
94 {{ .RenderString "{{< renderstring.inline >}}{{ if page.IsHome }}RenderString OK.{{ end }}{{< /renderstring.inline >}}}}"}}
95 {{ .Render "myview" }}
96 {{ .Content }}
97 partial: {{ partials.Include "foo.html" . }}
98 {{ $pag := (.Paginate site.RegularPages) }}
99 PageNumber: {{ $pag.PageNumber }}/{{ $pag.TotalPages }}|
100 {{ $p2 := site.GetPage "p2" }}
101 {{ $p2_1 := index $p2.Resources 0 }}
102 Bundled page: {{ $p2_1.Content }}
103 -- layouts/alias.html --
104 {{ if eq page .Page }}Alias OK.{{ else }}Failed.{{ end }}
105 -- layouts/404.html --
106 {{ if eq page . }}404 Page OK.{{ else }}Failed.{{ end }}
107 -- layouts/_partials/foo.html --
108 {{ if page.IsHome }}Partial OK.{{ else }}Failed.{{ end }}
109 -- layouts/_shortcodes/outer.html --
110 {{ .Inner }}
111 -- layouts/_shortcodes/inner.html --
112 {{ if page.IsHome }}Shortcode Inner OK.{{ else }}Failed.{{ end }}
113 -- layouts/_shortcodes/shortcode.html --
114 {{ if page.IsHome }}Shortcode {{ .Get 0 }} OK.{{ else }}Failed.{{ end }}
115 -- layouts/sitemap.xml --
116 {{ if eq page . }}Sitemap OK.{{ else }}Failed.{{ end }}
117 -- layouts/robots.txt --
118 {{ if eq page . }}Robots OK.{{ else }}Failed.{{ end }}
119 -- layouts/sitemapindex.xml --
120 {{ with page }}SitemapIndex OK: {{ .Kind }}{{ else }}Failed.{{ end }}
121
122 `
123
124 for _, multilingual := range []bool{true, false} {
125 t.Run(fmt.Sprintf("multilingual-%t", multilingual), func(t *testing.T) {
126 // Fenced code blocks.
127 files := strings.ReplaceAll(filesTemplate, "$$$", "```")
128
129 if multilingual {
130 files = strings.ReplaceAll(files, "LANG_CONFIG", `
131 [languages]
132 [languages.en]
133 weight = 1
134 [languages.no]
135 weight = 2
136 `)
137 } else {
138 files = strings.ReplaceAll(files, "LANG_CONFIG", "")
139 }
140
141 for range 1 {
142
143 b := hugolib.Test(t, files)
144
145 b.AssertFileContent("public/index.html", `
146 Heading OK.
147 Image OK.
148 Link OK.
149 Codeblock OK.
150 Page OK.
151 Partial OK.
152 Shortcode Angled Brackets OK.
153 Shortcode Percentage OK.
154 Shortcode Inner OK.
155 Shortcode Inline OK.
156 ExecuteAsTemplate OK.
157 RenderString OK.
158 Render OK.
159 Shortcode in bundled page OK.
160 `)
161
162 b.AssertFileContent("public/404.html", `404 Page OK.`)
163 b.AssertFileContent("public/robots.txt", `Robots OK.`)
164 b.AssertFileContent("public/homealias/index.html", `Alias OK.`)
165 b.AssertFileContent("public/page/1/index.html", `Alias OK.`)
166 b.AssertFileContent("public/page/2/index.html", `Page OK.`)
167 if multilingual {
168 b.AssertFileContent("public/sitemap.xml", `SitemapIndex OK: sitemapindex`)
169 } else {
170 b.AssertFileContent("public/sitemap.xml", `Sitemap OK.`)
171 }
172 }
173 })
174 }
175 }
176
177 // Issue 10791.
178 func TestPageTableOfContentsInShortcode(t *testing.T) {
179 t.Parallel()
180
181 files := `
182 -- hugo.toml --
183 baseURL = 'http://example.com/'
184 disableKinds = ["taxonomy", "term"]
185 -- content/p1.md --
186 ---
187 title: "P1"
188 ---
189 {{< toc >}}
190
191 # Heading 1
192 -- layouts/_shortcodes/toc.html --
193 {{ page.TableOfContents }}
194 -- layouts/single.html --
195 {{ .Content }}
196 `
197
198 b := hugolib.Test(t, files)
199
200 b.AssertFileContent("public/p1/index.html", "<nav id=\"TableOfContents\"></nav> \n<h1 id=\"heading-1\">Heading 1</h1>")
201 }
202
203 func TestFromStringRunning(t *testing.T) {
204 t.Parallel()
205
206 files := `
207 -- hugo.toml --
208 disableLiveReload = true
209 -- layouts/home.html --
210 {{ with resources.FromString "foo" "{{ seq 3 }}" }}
211 {{ with resources.ExecuteAsTemplate "bar" $ . }}
212 {{ .Content | safeHTML }}
213 {{ end }}
214 {{ end }}
215 `
216
217 b := hugolib.TestRunning(t, files)
218
219 b.AssertFileContent("public/index.html", "1\n2\n3")
220 }
221
222 func TestThatPageGitInfoShouldBeNil(t *testing.T) {
223 t.Parallel()
224
225 files := `
226 -- hugo.toml --
227 disableKinds = ["taxonomy", "term"]
228 -- content/p1.md --
229 ---
230 title: "P1"
231 ---
232 -- layouts/all.html --
233 GitInfo: {{ with .GitInfo }}FAIL{{ end }}
234
235 `
236
237 b := hugolib.Test(t, files)
238
239 b.AssertFileContent("public/p1/index.html", "! FAIL")
240 }