Debian Codegraph @

hugolib/taxonomy_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 "github.com/gohugoio/hugo/resources/kinds"
22
23 "github.com/gohugoio/hugo/resources/page"
24
25 qt "github.com/frankban/quicktest"
26 )
27
28 func TestTaxonomiesCountOrder(t *testing.T) {
29 t.Parallel()
30
31 files := `
32 -- hugo.toml --
33 baseURL = "http://example.com/"
34 titleCaseStyle = "none"
35 [taxonomies]
36 tag = "tags"
37 category = "categories"
38 -- content/page.md --
39 ---
40 tags: ['a', 'B', 'c']
41 categories: 'd'
42 ---
43 YAML frontmatter with tags and categories taxonomy.
44 `
45 b := Test(t, files)
46
47 s := b.H.Sites[0]
48
49 st := make([]string, 0)
50 for _, t := range s.Taxonomies()["tags"].ByCount() {
51 st = append(st, t.Page().Title()+":"+t.Name)
52 }
53
54 expect := []string{"a:a", "B:b", "c:c"}
55
56 b.Assert(st, qt.DeepEquals, expect)
57 }
58
59 // https://github.com/gohugoio/hugo/issues/5513
60 // https://github.com/gohugoio/hugo/issues/5571
61 func TestTaxonomiesPathSeparation(t *testing.T) {
62 t.Parallel()
63
64 files := `
65 -- hugo.toml --
66 baseURL = "https://example.com"
67 titleCaseStyle = "none"
68 [taxonomies]
69 "news/tag" = "news/tags"
70 "news/category" = "news/categories"
71 "t1/t2/t3" = "t1/t2/t3s"
72 "s1/s2/s3" = "s1/s2/s3s"
73 -- content/page.md --
74 +++
75 title = "foo"
76 "news/categories" = ["a", "b", "c", "d/e", "f/g/h"]
77 "t1/t2/t3s" = ["t4/t5", "t4/t5/t6"]
78 +++
79 Content.
80 -- content/news/categories/b/_index.md --
81 ---
82 title: "This is B"
83 ---
84 -- content/news/categories/f/g/h/_index.md --
85 ---
86 title: "This is H"
87 ---
88 -- content/t1/t2/t3s/t4/t5/_index.md --
89 ---
90 title: "This is T5"
91 ---
92 -- content/s1/s2/s3s/_index.md --
93 ---
94 title: "This is S3s"
95 ---
96 -- layouts/list.html --
97 Taxonomy List Page 1|{{ .Title }}|Hello|{{ .Permalink }}|
98 -- layouts/taxonomy.html --
99 Taxonomy Page 1|{{ .Title }}|Hello|{{ .Permalink }}|
100 `
101
102 b := Test(t, files)
103
104 s := b.H.Sites[0]
105
106 filterbyKind := func(kind string) page.Pages {
107 var pages page.Pages
108 for _, p := range s.Pages() {
109 if p.Kind() == kind {
110 pages = append(pages, p)
111 }
112 }
113 return pages
114 }
115
116 ta := filterbyKind(kinds.KindTerm)
117 te := filterbyKind(kinds.KindTaxonomy)
118
119 b.Assert(len(te), qt.Equals, 4)
120 b.Assert(len(ta), qt.Equals, 7)
121
122 b.AssertFileContent("public/news/categories/a/index.html", "Taxonomy List Page 1|a|Hello|https://example.com/news/categories/a/|")
123 b.AssertFileContent("public/news/categories/b/index.html", "Taxonomy List Page 1|This is B|Hello|https://example.com/news/categories/b/|")
124 b.AssertFileContent("public/news/categories/d/e/index.html", "Taxonomy List Page 1|d/e|Hello|https://example.com/news/categories/d/e/|")
125 b.AssertFileContent("public/news/categories/f/g/h/index.html", "Taxonomy List Page 1|This is H|Hello|https://example.com/news/categories/f/g/h/|")
126 b.AssertFileContent("public/t1/t2/t3s/t4/t5/index.html", "Taxonomy List Page 1|This is T5|Hello|https://example.com/t1/t2/t3s/t4/t5/|")
127 b.AssertFileContent("public/t1/t2/t3s/t4/t5/t6/index.html", "Taxonomy List Page 1|t4/t5/t6|Hello|https://example.com/t1/t2/t3s/t4/t5/t6/|")
128
129 b.AssertFileContent("public/news/categories/index.html", "Taxonomy Page 1|categories|Hello|https://example.com/news/categories/|")
130 b.AssertFileContent("public/t1/t2/t3s/index.html", "Taxonomy Page 1|t3s|Hello|https://example.com/t1/t2/t3s/|")
131 b.AssertFileContent("public/s1/s2/s3s/index.html", "Taxonomy Page 1|This is S3s|Hello|https://example.com/s1/s2/s3s/|")
132 }
133
134 // https://github.com/gohugoio/hugo/issues/5719
135 func TestTaxonomiesNextGenLoops(t *testing.T) {
136 t.Parallel()
137
138 pageContent := `
139 ---
140 Title: "Taxonomy!"
141 tags: ["Hugo Rocks!", "Rocks I say!" ]
142 categories: ["This is Cool", "And new" ]
143 ---
144
145 Content.
146 `
147
148 var files strings.Builder
149 files.WriteString(`
150 -- hugo.toml --
151 baseURL = "http://example.com"
152 -- layouts/home.html --
153 <h1>Tags</h1>
154 <ul>
155 {{ range .Site.Taxonomies.tags }}
156 <li><a href="{{ .Page.Permalink }}">{{ .Page.Title }}</a> {{ .Count }}</li>
157 {{ end }}
158 </ul>
159 -- layouts/taxonomy.html --
160 <h1>Terms</h1>
161 <ul>
162 {{ range .Data.Terms.Alphabetical }}
163 <li><a href="{{ .Page.Permalink }}">{{ .Page.Title }}</a> {{ .Count }}</li>
164 {{ end }}
165 </ul>
166 `)
167
168 for i := range 10 {
169 files.WriteString(fmt.Sprintf("\n-- content/page%d.md --\n%s", i+1, pageContent))
170 }
171
172 b := Test(t, files.String())
173
174 b.AssertFileContent("public/index.html", `<li><a href="http://example.com/tags/hugo-rocks/">Hugo Rocks!</a> 10</li>`)
175 b.AssertFileContent("public/categories/index.html", `<li><a href="http://example.com/categories/this-is-cool/">This Is Cool</a> 10</li>`)
176 b.AssertFileContent("public/tags/index.html", `<li><a href="http://example.com/tags/rocks-i-say/">Rocks I Say!</a> 10</li>`)
177 }
178
179 // Issue 6213
180 func TestTaxonomiesNotForDrafts(t *testing.T) {
181 t.Parallel()
182
183 files := `
184 -- hugo.toml --
185 baseURL = "http://example.com/"
186 -- layouts/list.html --
187 List page.
188 -- content/draft.md --
189 ---
190 title: "Draft"
191 draft: true
192 categories: ["drafts"]
193 ---
194 -- content/regular.md --
195 ---
196 title: "Not Draft"
197 categories: ["regular"]
198 ---
199 `
200 b := Test(t, files)
201
202 s := b.H.Sites[0]
203
204 b.AssertFileExists("public/categories/regular/index.html", true)
205 b.AssertFileExists("public/categories/drafts/index.html", false)
206
207 reg, _ := s.getPage(nil, "categories/regular")
208 dra, _ := s.getPage(nil, "categories/draft")
209 b.Assert(reg, qt.Not(qt.IsNil))
210 b.Assert(dra, qt.IsNil)
211 }
212
213 func TestTaxonomiesIndexDraft(t *testing.T) {
214 t.Parallel()
215
216 files := `
217 -- hugo.toml --
218 baseURL = "http://example.com"
219 -- content/categories/_index.md --
220 ---
221 title: "The Categories"
222 draft: true
223 ---
224
225 Content.
226 -- content/page.md --
227 ---
228 title: "The Page"
229 categories: ["cool"]
230 ---
231
232 Content.
233 -- layouts/home.html --
234 {{ range .Site.Pages }}
235 {{ .RelPermalink }}|{{ .Title }}|{{ .WordCount }}|{{ .Content }}|
236 {{ end }}
237 `
238
239 b := Test(t, files)
240
241 b.AssertFileContent("public/index.html", "! /categories/|")
242 }
243
244 // https://github.com/gohugoio/hugo/issues/6927
245 func TestTaxonomiesHomeDraft(t *testing.T) {
246 t.Parallel()
247
248 files := `
249 -- hugo.toml --
250 baseURL = "http://example.com"
251 -- content/_index.md --
252 ---
253 title: "Home"
254 draft: true
255 ---
256
257 Content.
258 -- content/posts/_index.md --
259 ---
260 title: "Posts"
261 draft: true
262 ---
263
264 Content.
265 -- content/posts/page.md --
266 ---
267 title: "The Page"
268 categories: ["cool"]
269 ---
270
271 Content.
272 -- layouts/home.html --
273 NO HOME FOR YOU
274 `
275
276 b := Test(t, files)
277
278 b.AssertFileExists("public/index.html", false)
279 b.AssertFileExists("public/categories/index.html", false)
280 b.AssertFileExists("public/posts/index.html", false)
281 }
282
283 // https://github.com/gohugoio/hugo/issues/6173
284 func TestTaxonomiesWithBundledResources(t *testing.T) {
285 t.Parallel()
286
287 files := `
288 -- hugo.toml --
289 baseURL = "http://example.com"
290 -- layouts/list.html --
291 List {{ .Title }}:
292 {{ range .Resources }}
293 Resource: {{ .RelPermalink }}|{{ .MediaType }}
294 {{ end }}
295 -- content/p1.md --
296 ---
297 title: Page
298 categories: ["funny"]
299 ---
300 -- content/categories/_index.md --
301 ---
302 title: Categories Page
303 ---
304 -- content/categories/data.json --
305 Category data
306 -- content/categories/funny/_index.md --
307 ---
308 title: Funny Category
309 ---
310 -- content/categories/funny/funnydata.json --
311 Category funny data
312 `
313
314 b := Test(t, files)
315
316 b.AssertFileContent("public/categories/index.html", `Resource: /categories/data.json|application/json`)
317 b.AssertFileContent("public/categories/funny/index.html", `Resource: /categories/funny/funnydata.json|application/json`)
318 }
319
320 func TestTaxonomiesRemoveOne(t *testing.T) {
321 files := `
322 -- hugo.toml --
323 disableLiveReload = true
324 -- layouts/home.html --
325 {{ $cats := .Site.Taxonomies.categories.cats }}
326 {{ if $cats }}
327 Len cats: {{ len $cats }}
328 {{ range $cats }}
329 Cats:|{{ .Page.RelPermalink }}|
330 {{ end }}
331 {{ end }}
332 {{ $funny := .Site.Taxonomies.categories.funny }}
333 {{ if $funny }}
334 Len funny: {{ len $funny }}
335 {{ range $funny }}
336 Funny:|{{ .Page.RelPermalink }}|
337 {{ end }}
338 {{ end }}
339 -- content/p1.md --
340 ---
341 title: Page
342 categories: ["funny", "cats"]
343 ---
344 -- content/p2.md --
345 ---
346 title: Page2
347 categories: ["funny", "cats"]
348 ---
349
350 `
351 b := TestRunning(t, files)
352
353 b.AssertFileContent("public/index.html", `
354 Len cats: 2
355 Len funny: 2
356 Cats:|/p1/|
357 Cats:|/p2/|
358 Funny:|/p1/|
359 Funny:|/p2/|`)
360
361 // Remove one category from one of the pages.
362 b.EditFiles("content/p1.md", `---
363 title: Page
364 categories: ["funny"]
365 ---
366 `)
367
368 b.Build()
369
370 b.AssertFileContent("public/index.html", `
371 Len cats: 1
372 Len funny: 2
373 Cats:|/p2/|
374 Funny:|/p1/|
375 Funny:|/p2/|`)
376 }
377
378 // https://github.com/gohugoio/hugo/issues/6590
379 func TestTaxonomiesListPages(t *testing.T) {
380 t.Parallel()
381
382 files := `
383 -- hugo.toml --
384 baseURL = "http://example.com"
385 -- layouts/list.html --
386 {{ template "print-taxo" "categories.cats" }}
387 {{ template "print-taxo" "categories.funny" }}
388
389 {{ define "print-taxo" }}
390 {{ $node := index site.Taxonomies (split $ ".") }}
391 {{ if $node }}
392 Len {{ $ }}: {{ len $node }}
393 {{ range $node }}
394 {{ $ }}:|{{ .Page.RelPermalink }}|
395 {{ end }}
396 {{ else }}
397 {{ $ }} not found.
398 {{ end }}
399 {{ end }}
400 -- content/_index.md --
401 ---
402 title: Home
403 categories: ["funny", "cats"]
404 ---
405 -- content/blog/p1.md --
406 ---
407 title: Page1
408 categories: ["funny"]
409 ---
410 -- content/blog/_index.md --
411 ---
412 title: Blog Section
413 categories: ["cats"]
414 ---
415 `
416
417 b := Test(t, files)
418
419 b.AssertFileContent("public/index.html", `
420
421 Len categories.cats: 2
422 categories.cats:|/blog/|
423 categories.cats:|/|
424
425 Len categories.funny: 2
426 categories.funny:|/|
427 categories.funny:|/blog/p1/|
428 `)
429 }
430
431 func TestTaxonomiesPageCollections(t *testing.T) {
432 t.Parallel()
433
434 files := `
435 -- hugo.toml --
436 baseURL = "http://example.com"
437 -- layouts/home.html --
438 {{ $home := site.Home }}
439 {{ $section := site.GetPage "section" }}
440 {{ $categories := site.GetPage "categories" }}
441 {{ $funny := site.GetPage "categories/funny" }}
442 {{ $cats := site.GetPage "categories/cats" }}
443 {{ $p1 := site.GetPage "section/p1" }}
444
445 Categories Pages: {{ range $categories.Pages}}{{.RelPermalink }}|{{ end }}:END
446 Funny Pages: {{ range $funny.Pages}}{{.RelPermalink }}|{{ end }}:END
447 Cats Pages: {{ range $cats.Pages}}{{.RelPermalink }}|{{ end }}:END
448 P1 Terms: {{ range $p1.GetTerms "categories" }}{{.RelPermalink }}|{{ end }}:END
449 Section Terms: {{ range $section.GetTerms "categories" }}{{.RelPermalink }}|{{ end }}:END
450 Home Terms: {{ range $home.GetTerms "categories" }}{{.RelPermalink }}|{{ end }}:END
451 Category Paginator {{ range $categories.Paginator.Pages }}{{ .RelPermalink }}|{{ end }}:END
452 Cats Paginator {{ range $cats.Paginator.Pages }}{{ .RelPermalink }}|{{ end }}:END
453 -- layouts/404.html --
454 404 Terms: {{ range .GetTerms "categories" }}{{.RelPermalink }}|{{ end }}:END
455 -- content/_index.md --
456 ---
457 title: "Home Sweet Home"
458 categories: [ "dogs", "gorillas"]
459 ---
460 -- content/section/_index.md --
461 ---
462 title: "Section"
463 categories: [ "cats", "dogs", "birds"]
464 ---
465 -- content/section/p1.md --
466 ---
467 title: "Page1"
468 categories: ["funny", "cats"]
469 ---
470 -- content/section/p2.md --
471 ---
472 title: "Page2"
473 categories: ["funny"]
474 ---
475 `
476
477 b := Test(t, files)
478
479 cat, _ := b.H.Sites[0].GetPage("categories")
480 funny, _ := b.H.Sites[0].GetPage("categories/funny")
481
482 b.Assert(cat, qt.Not(qt.IsNil))
483 b.Assert(funny, qt.Not(qt.IsNil))
484
485 b.Assert(cat.Parent().IsHome(), qt.Equals, true)
486 b.Assert(funny.Kind(), qt.Equals, "term")
487 b.Assert(funny.Parent(), qt.Equals, cat)
488
489 b.AssertFileContent("public/index.html", `
490 Categories Pages: /categories/birds/|/categories/cats/|/categories/dogs/|/categories/funny/|/categories/gorillas/|:END
491 Funny Pages: /section/p1/|/section/p2/|:END
492 Cats Pages: /section/p1/|/section/|:END
493 P1 Terms: /categories/funny/|/categories/cats/|:END
494 Section Terms: /categories/cats/|/categories/dogs/|/categories/birds/|:END
495 Home Terms: /categories/dogs/|/categories/gorillas/|:END
496 Cats Paginator /section/p1/|/section/|:END
497 Category Paginator /categories/birds/|/categories/cats/|/categories/dogs/|/categories/funny/|/categories/gorillas/|:END`,
498 )
499 b.AssertFileContent("public/404.html", "\n404 Terms: :END\n\t")
500 b.AssertFileContent("public/categories/funny/index.xml", `<link>http://example.com/section/p1/</link>`)
501 b.AssertFileContent("public/categories/index.xml", `<link>http://example.com/categories/funny/</link>`)
502 }
503
504 func TestTaxonomiesDirectoryOverlaps(t *testing.T) {
505 t.Parallel()
506
507 files := `
508 -- hugo.toml --
509 baseURL = "https://example.org"
510 titleCaseStyle = "none"
511 [taxonomies]
512 abcdef = "abcdefs"
513 abcdefg = "abcdefgs"
514 abcdefghi = "abcdefghis"
515 -- layouts/home.html --
516 {{ range site.Pages }}Page: {{ template "print-page" . }}
517 {{ end }}
518 {{ $abc := site.GetPage "abcdefgs/abc" }}
519 {{ $abcdefgs := site.GetPage "abcdefgs" }}
520 abc: {{ template "print-page" $abc }}|IsAncestor: {{ $abc.IsAncestor $abcdefgs }}|IsDescendant: {{ $abc.IsDescendant $abcdefgs }}
521 abcdefgs: {{ template "print-page" $abcdefgs }}|IsAncestor: {{ $abcdefgs.IsAncestor $abc }}|IsDescendant: {{ $abcdefgs.IsDescendant $abc }}
522
523 {{ define "print-page" }}{{ .RelPermalink }}|{{ .Title }}|{{.Kind }}|Parent: {{ with .Parent }}{{ .RelPermalink }}{{ end }}|CurrentSection: {{ .CurrentSection.RelPermalink}}|FirstSection: {{ .FirstSection.RelPermalink }}{{ end }}
524 -- content/abc/_index.md --
525 ---
526 title: "abc"
527 abcdefgs: [abc]
528 ---
529 -- content/abc/p1.md --
530 ---
531 title: "abc-p"
532 ---
533 -- content/abcdefgh/_index.md --
534 ---
535 title: "abcdefgh"
536 ---
537 -- content/abcdefgh/p1.md --
538 ---
539 title: "abcdefgh-p"
540 ---
541 -- content/abcdefghijk/index.md --
542 ---
543 title: "abcdefghijk"
544 ---
545 `
546
547 b := Test(t, files)
548
549 b.AssertFileContent("public/index.html", `
550 Page: /||home|Parent: |CurrentSection: /|
551 Page: /abc/|abc|section|Parent: /|CurrentSection: /abc/|
552 Page: /abc/p1/|abc-p|page|Parent: /abc/|CurrentSection: /abc/|
553 Page: /abcdefgh/|abcdefgh|section|Parent: /|CurrentSection: /abcdefgh/|
554 Page: /abcdefgh/p1/|abcdefgh-p|page|Parent: /abcdefgh/|CurrentSection: /abcdefgh/|
555 Page: /abcdefghijk/|abcdefghijk|page|Parent: /|CurrentSection: /|
556 Page: /abcdefghis/|abcdefghis|taxonomy|Parent: /|CurrentSection: /abcdefghis/|
557 Page: /abcdefgs/|abcdefgs|taxonomy|Parent: /|CurrentSection: /abcdefgs/|
558 Page: /abcdefs/|abcdefs|taxonomy|Parent: /|CurrentSection: /abcdefs/|
559 abc: /abcdefgs/abc/|abc|term|Parent: /abcdefgs/|CurrentSection: /abcdefgs/abc/|
560 abcdefgs: /abcdefgs/|abcdefgs|taxonomy|Parent: /|CurrentSection: /abcdefgs/|
561 abc: /abcdefgs/abc/|abc|term|Parent: /abcdefgs/|CurrentSection: /abcdefgs/abc/|FirstSection: /abcdefgs/|IsAncestor: false|IsDescendant: true
562 abcdefgs: /abcdefgs/|abcdefgs|taxonomy|Parent: /|CurrentSection: /abcdefgs/|FirstSection: /abcdefgs/|IsAncestor: true|IsDescendant: false
563 `)
564 }
565
566 func TestTaxonomiesWeightSort(t *testing.T) {
567 files := `
568 -- layouts/home.html --
569 {{ $a := site.GetPage "tags/a"}}
570 :{{ range $a.Pages }}{{ .RelPermalink }}|{{ end }}:
571 -- content/p1.md --
572 ---
573 title: P1
574 weight: 100
575 tags: ['a']
576 tags_weight: 20
577 ---
578 -- content/p3.md --
579 ---
580 title: P2
581 weight: 200
582 tags: ['a']
583 tags_weight: 30
584 ---
585 -- content/p2.md --
586 ---
587 title: P3
588 weight: 50
589 tags: ['a']
590 tags_weight: 40
591 ---
592 `
593
594 b := Test(t, files)
595
596 b.AssertFileContent("public/index.html", `:/p1/|/p3/|/p2/|:`)
597 }
598
599 func TestTaxonomiesEmptyTagsString(t *testing.T) {
600 t.Parallel()
601
602 files := `
603 -- hugo.toml --
604 [taxonomies]
605 tag = 'tags'
606 -- content/p1.md --
607 +++
608 title = "P1"
609 tags = ''
610 +++
611 -- layouts/single.html --
612 Single.
613
614 `
615 Test(t, files)
616 }
617
618 func TestTaxonomiesSpaceInName(t *testing.T) {
619 t.Parallel()
620
621 files := `
622 -- hugo.toml --
623 [taxonomies]
624 "book author" = "book authors"
625 -- content/posts/post1.md --
626 ---
627 title: Post 1
628 book authors: ["Author One"]
629 ---
630 -- content/posts/post2.md --
631 ---
632 title: Post 2
633 book authors: ["Author One", "Author Two"]
634 ---
635 -- layouts/home.html --
636 {{- $taxonomy := "book authors" }}
637 Len Book Authors: {{ len (index .Site.Taxonomies $taxonomy) }}
638 -- layouts/_default/taxonomy.html --
639 {{ range .Data.Terms.ByCount }}
640 Term: {{ .Term }}|Count: {{ .Count }}|Name: {{ .Name }}|Page title: {{ .Page.Title }}|
641 {{ end }}
642 `
643 b := Test(t, files)
644
645 b.AssertFileContent("public/index.html", "Len Book Authors: 2")
646 b.AssertFileContent("public/book-authors/index.html",
647 "Term: author one|Count: 2|Name: author one|Page title: Author One|",
648 "Term: author two|Count: 1|Name: author two|Page title: Author Two|",
649 )
650 }
651
652 func TestTaxonomiesListTermsHome(t *testing.T) {
653 files := `
654 -- hugo.toml --
655 baseURL = "https://example.com"
656 [taxonomies]
657 tag = "tags"
658 -- content/_index.md --
659 ---
660 title: "Home"
661 tags: ["a", "b", "c", "hello world"]
662 ---
663 -- content/tags/a/_index.md --
664 ---
665 title: "A"
666 ---
667 -- content/tags/b/_index.md --
668 ---
669 title: "B"
670 ---
671 -- content/tags/c/_index.md --
672 ---
673 title: "C"
674 ---
675 -- content/tags/d/_index.md --
676 ---
677 title: "D"
678 ---
679 -- content/tags/hello-world/_index.md --
680 ---
681 title: "Hello World!"
682 ---
683 -- layouts/home.html --
684 Terms: {{ range site.Taxonomies.tags }}{{ .Page.Title }}: {{ .Count }}|{{ end }}$
685 `
686 b := Test(t, files)
687
688 b.AssertFileContent("public/index.html", "Terms: A: 1|B: 1|C: 1|Hello World!: 1|$")
689 }
690
691 func TestTaxonomiesTermTitleAndTerm(t *testing.T) {
692 files := `
693 -- hugo.toml --
694 baseURL = "https://example.com"
695 [taxonomies]
696 tag = "tags"
697 -- content/_index.md --
698 ---
699 title: "Home"
700 tags: ["hellO world"]
701 ---
702 -- layouts/term.html --
703 {{ .Title }}|{{ .Kind }}|{{ .Data.Singular }}|{{ .Data.Plural }}|{{ .Page.Data.Term }}|
704 `
705
706 b := Test(t, files)
707
708 b.AssertFileContent("public/tags/hello-world/index.html", "HellO World|term|tag|tags|hellO world|")
709 }
710
711 func TestTermDraft(t *testing.T) {
712 t.Parallel()
713
714 files := `
715 -- layouts/list.html --
716 |{{ .Title }}|
717 -- content/p1.md --
718 ---
719 title: p1
720 tags: [a]
721 ---
722 -- content/tags/a/_index.md --
723 ---
724 title: tag-a-title-override
725 draft: true
726 ---
727 `
728
729 b := Test(t, files)
730
731 b.AssertFileExists("public/tags/a/index.html", false)
732 }
733
734 func TestTermBuildNeverRenderNorList(t *testing.T) {
735 t.Parallel()
736
737 files := `
738 -- layouts/home.html --
739 |{{ len site.Taxonomies.tags }}|
740 -- content/p1.md --
741 ---
742 title: p1
743 tags: [a]
744 ---
745 -- content/tags/a/_index.md --
746 ---
747 title: tag-a-title-override
748 build:
749 render: never
750 list: never
751 ---
752
753 `
754
755 b := Test(t, files)
756
757 b.AssertFileExists("public/tags/a/index.html", false)
758 b.AssertFileContent("public/index.html", "|0|")
759 }
760
761 func TestTaxonomiesTermLookup(t *testing.T) {
762 t.Parallel()
763
764 files := `
765 -- hugo.toml --
766 baseURL = "https://example.com"
767 [taxonomies]
768 tag = "tags"
769 -- content/_index.md --
770 ---
771 title: "Home"
772 tags: ["a", "b"]
773 ---
774 -- layouts/taxonomy/tag.html --
775 Tag: {{ .Title }}|
776 -- content/tags/a/_index.md --
777 ---
778 title: tag-a-title-override
779 ---
780 `
781
782 b := Test(t, files)
783
784 b.AssertFileContent("public/tags/a/index.html", "Tag: tag-a-title-override|")
785 }
786
787 func TestTaxonomyLookupIssue12193(t *testing.T) {
788 t.Parallel()
789
790 files := `
791 -- hugo.toml --
792 disableKinds = ['page','rss','section','sitemap']
793 [taxonomies]
794 author = 'authors'
795 -- layouts/list.html --
796 list.html
797 -- layouts/authors/taxonomy.html --
798 authors/taxonomy.html
799 -- content/authors/_index.md --
800 ---
801 title: Authors Page
802 ---
803 `
804
805 b := Test(t, files)
806
807 b.AssertFileExists("public/index.html", true)
808 b.AssertFileExists("public/authors/index.html", true)
809 b.AssertFileContent("public/authors/index.html", "authors/taxonomy.html")
810 }
811
812 func TestTaxonomyNestedEmptySectionsIssue12188(t *testing.T) {
813 t.Parallel()
814
815 files := `
816 -- hugo.toml --
817 disableKinds = ['rss','sitemap']
818 defaultContentLanguage = 'en'
819 defaultContentLanguageInSubdir = true
820 [languages.en]
821 weight = 1
822 [languages.ja]
823 weight = 2
824 [taxonomies]
825 's1/category' = 's1/category'
826 -- layouts/single.html --
827 {{ .Title }}|
828 -- layouts/list.html --
829 {{ .Title }}|
830 -- content/s1/p1.en.md --
831 ---
832 title: p1
833 ---
834 `
835
836 b := Test(t, files)
837
838 b.AssertFileExists("public/en/s1/index.html", true)
839 b.AssertFileExists("public/en/s1/p1/index.html", true)
840 b.AssertFileExists("public/en/s1/category/index.html", true)
841
842 b.AssertFileExists("public/ja/s1/index.html", false) // failing test
843 b.AssertFileExists("public/ja/s1/category/index.html", true)
844 }
845
846 func BenchmarkTaxonomiesGetTerms(b *testing.B) {
847 createBuilder := func(b *testing.B, numPages int) *IntegrationTestBuilder {
848 b.StopTimer()
849
850 var files strings.Builder
851 files.WriteString(`
852 -- hugo.toml --
853 baseURL = "https://example.com"
854 disableKinds = ["RSS", "sitemap", "section"]
855 [taxononomies]
856 tag = "tags"
857 -- layouts/list.html --
858 List.
859 -- layouts/single.html --
860 GetTerms.tags: {{ range .GetTerms "tags" }}{{ .Title }}|{{ end }}
861 -- content/_index.md --
862 `)
863
864 tagsVariants := []string{
865 "tags: ['a']",
866 "tags: ['a', 'b']",
867 "tags: ['a', 'b', 'c']",
868 "tags: ['a', 'b', 'c', 'd']",
869 "tags: ['a', 'b', 'd', 'e']",
870 "tags: ['a', 'b', 'c', 'd', 'e']",
871 "tags: ['a', 'd']",
872 "tags: ['a', 'f']",
873 }
874
875 for i := 1; i < numPages; i++ {
876 tags := tagsVariants[i%len(tagsVariants)]
877 files.WriteString(fmt.Sprintf("\n-- content/posts/p%d.md --\n---\n%s\n---", i+1, tags))
878 }
879
880 cfg := IntegrationTestConfig{
881 T: b,
882 TxtarString: files.String(),
883 }
884
885 bb := NewIntegrationTestBuilder(cfg)
886
887 b.StartTimer()
888
889 return bb
890 }
891
892 for _, numPages := range []int{100, 1000, 10000, 20000} {
893 b.Run(fmt.Sprintf("pages_%d", numPages), func(b *testing.B) {
894 for i := 0; b.Loop(); i++ {
895 createBuilder(b, numPages).Build()
896 }
897 })
898 }
899 }