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