Debian Codegraph @

hugolib/content_map_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 "sync"
20 "testing"
21
22 qt "github.com/frankban/quicktest"
23 "github.com/gohugoio/hugo/hugolib/sitesmatrix"
24 )
25
26 func TestContentMapSite(t *testing.T) {
27 t.Parallel()
28
29 pageTempl := `
30 ---
31 title: "Page %d"
32 date: "2019-06-%02d"
33 lastMod: "2019-06-%02d"
34 categories: [%q]
35 ---
36
37 Page content.
38 `
39 createPage := func(i int) string {
40 return fmt.Sprintf(pageTempl, i, i, i+1, "funny")
41 }
42
43 createPageInCategory := func(i int, category string) string {
44 return fmt.Sprintf(pageTempl, i, i, i+1, category)
45 }
46
47 draftTemplate := `---
48 title: "Draft"
49 draft: true
50 ---
51
52 `
53
54 files := `
55 -- hugo.toml --
56 baseURL = "http://example.com"
57 -- layouts/home.html --
58 Num Regular: {{ len .Site.RegularPages }}|{{ range .Site.RegularPages }}{{ .RelPermalink }}|{{ end }}$
59 Main Sections: {{ .Site.Params.mainSections }}
60 Pag Num Pages: {{ len .Paginator.Pages }}
61 {{ $home := .Site.Home }}
62 {{ $blog := .Site.GetPage "blog" }}
63 {{ $categories := .Site.GetPage "categories" }}
64 {{ $funny := .Site.GetPage "categories/funny" }}
65 {{ $blogSub := .Site.GetPage "blog/subsection" }}
66 {{ $page := .Site.GetPage "blog/page1" }}
67 {{ $page2 := .Site.GetPage "blog/page2" }}
68 {{ $page4 := .Site.GetPage "blog/subsection/page4" }}
69 {{ $bundle := .Site.GetPage "blog/bundle" }}
70 {{ $overlap1 := .Site.GetPage "overlap" }}
71 {{ $overlap2 := .Site.GetPage "overlap2" }}
72
73 Home: {{ template "print-page" $home }}
74 Blog Section: {{ template "print-page" $blog }}
75 Blog Sub Section: {{ template "print-page" $blogSub }}
76 Page: {{ template "print-page" $page }}
77 Bundle: {{ template "print-page" $bundle }}
78 IsDescendant: true: {{ $page.IsDescendant $blog }} true: {{ $blogSub.IsDescendant $blog }} true: {{ $bundle.IsDescendant $blog }} true: {{ $page4.IsDescendant $blog }} true: {{ $blog.IsDescendant $home }} false: {{ $blog.IsDescendant $blog }} false: {{ $home.IsDescendant $blog }}
79 IsAncestor: true: {{ $blog.IsAncestor $page }} true: {{ $home.IsAncestor $blog }} true: {{ $blog.IsAncestor $blogSub }} true: {{ $blog.IsAncestor $bundle }} true: {{ $blog.IsAncestor $page4 }} true: {{ $home.IsAncestor $page }} false: {{ $blog.IsAncestor $blog }} false: {{ $page.IsAncestor $blog }} false: {{ $blog.IsAncestor $home }} false: {{ $blogSub.IsAncestor $blog }}
80 IsDescendant overlap1: false: {{ $overlap1.IsDescendant $overlap2 }}
81 IsDescendant overlap2: false: {{ $overlap2.IsDescendant $overlap1 }}
82 IsAncestor overlap1: false: {{ $overlap1.IsAncestor $overlap2 }}
83 IsAncestor overlap2: false: {{ $overlap2.IsAncestor $overlap1 }}
84 FirstSection: {{ $blogSub.FirstSection.RelPermalink }} {{ $blog.FirstSection.RelPermalink }} {{ $home.FirstSection.RelPermalink }} {{ $page.FirstSection.RelPermalink }}
85 InSection: true: {{ $page.InSection $blog }} false: {{ $page.InSection $blogSub }}
86 Next: {{ $page2.Next.RelPermalink }}
87 NextInSection: {{ $page2.NextInSection.RelPermalink }}
88 Pages: {{ range $blog.Pages }}{{ .RelPermalink }}|{{ end }}
89 Sections: {{ range $home.Sections }}{{ .RelPermalink }}|{{ end }}:END
90 Categories: {{ range .Site.Taxonomies.categories }}{{ .Page.RelPermalink }}; {{ .Page.Title }}; {{ .Count }}|{{ end }}:END
91 Category Terms: {{ $categories.Kind}}: {{ range $categories.Data.Terms.Alphabetical }}{{ .Page.RelPermalink }}; {{ .Page.Title }}; {{ .Count }}|{{ end }}:END
92 Category Funny: {{ $funny.Kind}}; {{ $funny.Data.Term }}: {{ range $funny.Pages }}{{ .RelPermalink }}|{{ end }}:END
93 Pag Num Pages: {{ len .Paginator.Pages }}
94 Pag Blog Num Pages: {{ len $blog.Paginator.Pages }}
95 Blog Num RegularPages: {{ len $blog.RegularPages }}|{{ range $blog.RegularPages }}P: {{ .RelPermalink }}|{{ end }}
96 Blog Num Pages: {{ len $blog.Pages }}
97
98 Draft1: {{ if (.Site.GetPage "blog/subsection/draft") }}FOUND{{ end }}|
99 Draft2: {{ if (.Site.GetPage "blog/draftsection") }}FOUND{{ end }}|
100 Draft3: {{ if (.Site.GetPage "blog/draftsection/page") }}FOUND{{ end }}|
101 Draft4: {{ if (.Site.GetPage "blog/draftsection/sub") }}FOUND{{ end }}|
102 Draft5: {{ if (.Site.GetPage "blog/draftsection/sub/page") }}FOUND{{ end }}|
103
104 {{ define "print-page" }}{{ .Title }}|{{ .RelPermalink }}|{{ .Date.Format "2006-01-02" }}|Current Section: {{ with .CurrentSection }}{{ .Path }}{{ else }}NIL{{ end }}|Resources: {{ range .Resources }}{{ .ResourceType }}: {{ .RelPermalink }}|{{ end }}{{ end }}
105 -- content/_index.md --
106 ---
107 title: "Hugo Home"
108 cascade:
109 description: "Common Description"
110 ---
111
112 Home Content.
113 -- content/blog/page1.md --
114 ` + createPage(1) + `
115 -- content/blog/page2.md --
116 ` + createPage(2) + `
117 -- content/blog/page3.md --
118 ` + createPage(3) + `
119 -- content/blog/bundle/index.md --
120 ` + createPage(4) + `
121 -- content/blog/bundle/data.json --
122 data
123 -- content/blog/bundle/page.md --
124 ` + createPage(5) + `
125 -- content/blog/subsection/_index.md --
126 ` + createPage(6) + `
127 -- content/blog/subsection/subdata.json --
128 data
129 -- content/blog/subsection/page4.md --
130 ` + createPage(7) + `
131 -- content/blog/subsection/page5.md --
132 ` + createPage(8) + `
133 -- content/blog/subsection/draft/index.md --
134 ` + draftTemplate + `
135 -- content/blog/subsection/draft/data.json --
136 data
137 -- content/blog/draftsection/_index.md --
138 ` + draftTemplate + `
139 -- content/blog/draftsection/page/index.md --
140 ` + createPage(9) + `
141 -- content/blog/draftsection/page/folder/data.json --
142 data
143 -- content/blog/draftsection/sub/_index.md --
144 ` + createPage(10) + `
145 -- content/blog/draftsection/sub/page.md --
146 ` + createPage(11) + `
147 -- content/docs/page6.md --
148 ` + createPage(12) + `
149 -- content/tags/_index.md --
150 ` + createPageInCategory(13, "sad") + `
151 -- content/overlap/_index.md --
152 ` + createPageInCategory(14, "sad") + `
153 -- content/overlap2/_index.md --
154 ` + createPage(15) + `
155 `
156
157 b := Test(t, files)
158
159 b.AssertFileContent("public/index.html",
160
161 `
162 Num Regular: 9
163 Main Sections: [blog]
164 Pag Num Pages: 9
165
166 Home: Hugo Home|/|2019-06-15|Current Section: /|Resources:
167 Blog Section: Blogs|/blog/|2019-06-11|Current Section: /blog|Resources:
168 Blog Sub Section: Page 6|/blog/subsection/|2019-06-06|Current Section: /blog/subsection|Resources: application: /blog/subsection/subdata.json|
169 Page: Page 1|/blog/page1/|2019-06-01|Current Section: /blog|Resources:
170 Bundle: Page 4|/blog/bundle/|2019-06-04|Current Section: /blog|Resources: application: /blog/bundle/data.json|page: |
171 IsDescendant: true: true true: true true: true true: true true: true false: false false: false
172 IsAncestor: true: true true: true true: true true: true true: true true: true false: false false: false false: false false: false
173 IsDescendant overlap1: false: false
174 IsDescendant overlap2: false: false
175 IsAncestor overlap1: false: false
176 IsAncestor overlap2: false: false
177 FirstSection: /blog/ /blog/ / /blog/
178 InSection: true: true false: false
179 Next: /blog/page3/
180 NextInSection: /blog/page3/
181 Pages: /blog/subsection/|/blog/bundle/|/blog/page3/|/blog/page2/|/blog/page1/|
182 Sections: /overlap2/|/overlap/|/docs/|/blog/|:END
183 Categories: /categories/funny/; Funny; 12|/categories/sad/; Sad; 2|:END
184 Category Terms: taxonomy: /categories/funny/; Funny; 12|/categories/sad/; Sad; 2|:END
185 Category Funny: term; funny: /overlap2/|/docs/page6/|/blog/draftsection/sub/page/|/blog/draftsection/sub/|/blog/draftsection/page/|/blog/subsection/page5/|/blog/subsection/page4/|/blog/subsection/|/blog/bundle/|/blog/page3/|/blog/page2/|/blog/page1/|:END
186 Pag Num Pages: 9
187 Pag Blog Num Pages: 4
188 Blog Num RegularPages: 4
189 Blog Num Pages: 5
190
191 Draft1: |
192 Draft2: FOUND|
193 Draft3: FOUND|
194 Draft4: FOUND|
195 Draft5: FOUND|
196
197 `)
198 }
199
200 func TestIntegrationTestTemplate(t *testing.T) {
201 t.Parallel()
202 c := qt.New(t)
203
204 files := `
205 -- hugo.toml --
206 title = "Integration Test"
207 disableKinds=["page", "section", "taxonomy", "term", "sitemap", "robotsTXT", "RSS"]
208 -- layouts/home.html --
209 Home: {{ .Title }}|
210
211 `
212
213 b := Test(c, files)
214
215 b.AssertFileContent("public/index.html", "Home: Integration Test|")
216 }
217
218 // Issue #11840
219 // Resource language fallback should be the closest language going up.
220 func TestBundleResourceLanguageBestMatch(t *testing.T) {
221 t.Parallel()
222
223 files := `
224 -- hugo.toml --
225 defaultContentLanguage = "fr"
226 defaultContentLanguageInSubdir = true
227 [languages]
228 [languages.en]
229 weight = 1
230 [languages.fr]
231 weight = 2
232 [languages.de]
233 weight = 3
234 -- layouts/home.html --
235 {{ $bundle := site.GetPage "bundle" }}
236 {{ $r := $bundle.Resources.GetMatch "*.txt" }}
237 {{ .Language.Lang }}: {{ with $r }}{{ .RelPermalink }}|{{ .Content }}{{ end}}
238 -- content/bundle/index.fr.md --
239 ---
240 title: "Bundle Fr"
241 ---
242 -- content/bundle/index.en.md --
243 ---
244 title: "Bundle En"
245 ---
246 -- content/bundle/index.de.md --
247 ---
248 title: "Bundle De"
249 ---
250 -- content/bundle/data.fr.txt --
251 Data fr
252 -- content/bundle/data.en.txt --
253 Data en
254
255 `
256 for range 5 {
257 b := Test(t, files)
258
259 b.AssertFileContent("public/fr/index.html", "fr: /fr/bundle/data.fr.txt|Data fr")
260 b.AssertFileContent("public/en/index.html", "en: /en/bundle/data.en.txt|Data en")
261
262 b.AssertFileContent("public/de/index.html", "de: /fr/bundle/data.fr.txt|Data fr")
263 }
264 }
265
266 // Issue #11944
267 func TestBundleResourcesGetWithSpacesInFilename(t *testing.T) {
268 files := `
269 -- hugo.toml --
270 baseURL = "https://example.com"
271 disableKinds = ["taxonomy", "term"]
272 -- content/bundle/index.md --
273 -- content/bundle/data with Spaces.txt --
274 Data.
275 -- layouts/home.html --
276 {{ $bundle := site.GetPage "bundle" }}
277 {{ $r := $bundle.Resources.Get "data with Spaces.txt" }}
278 R: {{ with $r }}{{ .Content }}{{ end }}|
279 `
280 b := Test(t, files)
281
282 b.AssertFileContent("public/index.html", "R: Data.")
283 }
284
285 // Issue #11946.
286 func TestBundleResourcesGetDuplicateSortOrder(t *testing.T) {
287 files := `
288 -- hugo.toml --
289 baseURL = "https://example.com"
290 -- content/bundle/index.md --
291 -- content/bundle/data-1.txt --
292 data-1.txt
293 -- content/bundle/data 1.txt --
294 data 1.txt
295 -- content/bundle/Data 1.txt --
296 Data 1.txt
297 -- content/bundle/Data-1.txt --
298 Data-1.txt
299 -- layouts/home.html --
300 {{ $bundle := site.GetPage "bundle" }}
301 {{ $r := $bundle.Resources.Get "data-1.txt" }}
302 R: {{ with $r }}{{ .Content }}{{ end }}|Len: {{ len $bundle.Resources }}|$
303
304 `
305
306 for range 3 {
307 b := Test(t, files)
308 b.AssertFileContent("public/index.html", "R: Data 1.txt|", "Len: 1|")
309 }
310 }
311
312 func TestBundleResourcesNoPublishedIssue12198(t *testing.T) {
313 t.Parallel()
314
315 files := `
316 -- hugo.toml --
317 disableKinds = ['home','rss','sitemap','taxonomy','term']
318 -- content/s1/p1.md --
319 ---
320 title: p1
321 ---
322 -- content/s1/foo.txt --
323 foo.txt
324 -- content/s1/p1.txt --
325 p1.txt
326 -- content/s1/p1-foo.txt --
327 p1-foo.txt
328 -- layouts/list.html --
329 {{.Title }}|
330 -- layouts/single.html --
331 {{.Title }}|
332 `
333
334 b := Test(t, files)
335
336 b.AssertFileExists("public/s1/index.html", true)
337 b.AssertFileExists("public/s1/foo.txt", true)
338 b.AssertFileExists("public/s1/p1.txt", true) // failing test
339 b.AssertFileExists("public/s1/p1-foo.txt", true) // failing test
340 b.AssertFileExists("public/s1/p1/index.html", true)
341 }
342
343 // Issue 13228.
344 func TestBranchResourceOverlap(t *testing.T) {
345 t.Parallel()
346
347 files := `
348 -- hugo.toml --
349 disableKinds = ['page','rss','section','sitemap','taxonomy','term']
350 -- content/_index.md --
351 ---
352 title: home
353 ---
354 -- content/s1/_index.md --
355 ---
356 title: s1
357 ---
358 -- content/s1x/a.txt --
359 a.txt
360 -- layouts/home.html --
361 Home.
362 {{ range .Resources.Match "**" }}
363 {{ .Name }}|
364 {{ end }}
365 `
366
367 b := Test(t, files)
368
369 b.AssertFileContent("public/index.html", "s1x/a.txt|")
370 }
371
372 func TestSitemapOverrideFilename(t *testing.T) {
373 t.Parallel()
374
375 files := `
376 -- hugo.toml --
377 baseURL = 'https://example.org/'
378 disableKinds = ['page','rss','section','taxonomy','term']
379 defaultContentLanguage = 'de'
380 defaultContentLanguageInSubdir = true
381 [languages.de]
382 [languages.en]
383 [sitemap]
384 filename = 'foo.xml'
385 -- layouts/home.html --
386 irrelevant
387 `
388
389 b := Test(t, files)
390
391 b.AssertFileExists("public/de/foo.xml", true)
392 b.AssertFileExists("public/en/foo.xml", true)
393 b.AssertFileContent("public/foo.xml",
394 "<loc>https://example.org/de/foo.xml</loc>",
395 "<loc>https://example.org/en/foo.xml</loc>",
396 )
397
398 files = strings.ReplaceAll(files, "filename = 'foo.xml'", "")
399
400 b = Test(t, files)
401
402 b.AssertFileExists("public/de/sitemap.xml", true)
403 b.AssertFileExists("public/en/sitemap.xml", true)
404 b.AssertFileContent("public/sitemap.xml",
405 "<loc>https://example.org/de/sitemap.xml</loc>",
406 "<loc>https://example.org/en/sitemap.xml</loc>",
407 )
408 }
409
410 func TestContentTreeReverseIndex(t *testing.T) {
411 t.Parallel()
412
413 c := qt.New(t)
414
415 pageReverseIndex := newContentTreeTreverseIndex(
416 func(get func(key any) (contentNode, bool), set func(key any, val contentNode)) {
417 for i := range 10 {
418 key := fmt.Sprint(i)
419 set(key, &testContentNode{key: key})
420 }
421 },
422 )
423
424 for i := range 10 {
425 key := fmt.Sprint(i)
426 v := pageReverseIndex.Get(key)
427 c.Assert(v, qt.Not(qt.IsNil))
428 c.Assert(v.Path(), qt.Equals, key)
429 }
430 }
431
432 // Issue 13019.
433 func TestContentTreeReverseIndexPara(t *testing.T) {
434 t.Parallel()
435
436 var wg sync.WaitGroup
437
438 for range 10 {
439 pageReverseIndex := newContentTreeTreverseIndex(
440 func(get func(key any) (contentNode, bool), set func(key any, val contentNode)) {
441 for i := range 10 {
442 key := fmt.Sprint(i)
443 set(key, &testContentNode{key: key})
444 }
445 },
446 )
447
448 for j := range 10 {
449 wg.Add(1)
450 go func(i int) {
451 defer wg.Done()
452 pageReverseIndex.Get(fmt.Sprint(i))
453 }(j)
454 }
455 }
456 }
457
458 type testContentNode struct {
459 key string
460 }
461
462 func (n *testContentNode) Path() string {
463 return n.key
464 }
465
466 func (n *testContentNode) forEeachContentNode(f func(v sitesmatrix.Vector, n contentNode) bool) bool {
467 panic("not supported")
468 }
469
470 // Issue 12274.
471 func TestHTMLNotContent(t *testing.T) {
472 filesTemplate := `
473 -- hugo.toml.temp --
474 [contentTypes]
475 [contentTypes."text/markdown"]
476 # Empty for now.
477 -- hugo.yaml.temp --
478 contentTypes:
479 text/markdown: {}
480 -- hugo.json.temp --
481 {
482 "contentTypes": {
483 "text/markdown": {}
484 }
485 }
486 -- content/p1/index.md --
487 ---
488 title: p1
489 ---
490 -- content/p1/a.html --
491 <p>a</p>
492 -- content/p1/b.html --
493 <p>b</p>
494 -- content/p1/c.html --
495 <p>c</p>
496 -- layouts/single.html --
497 Path: {{ .Path }}|{{.Kind }}
498 |{{ (.Resources.Get "a.html").RelPermalink -}}
499 |{{ (.Resources.Get "b.html").RelPermalink -}}
500 |{{ (.Resources.Get "c.html").Publish }}
501 `
502
503 for _, format := range []string{"toml", "yaml", "json"} {
504 t.Run(format, func(t *testing.T) {
505 t.Parallel()
506
507 files := strings.Replace(filesTemplate, format+".temp", format, 1)
508 b := Test(t, files)
509
510 b.AssertFileContent("public/p1/index.html", "|/p1/a.html|/p1/b.html|")
511 b.AssertFileContent("public/p1/a.html", "<p>a</p>")
512 b.AssertFileContent("public/p1/b.html", "<p>b</p>")
513 b.AssertFileContent("public/p1/c.html", "<p>c</p>")
514 })
515 }
516 }