Debian Codegraph @

hugolib/menu_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 "testing"
18
19 qt "github.com/frankban/quicktest"
20 )
21
22 func TestMenusSectionPagesMenu(t *testing.T) {
23 t.Parallel()
24
25 files := `
26 -- hugo.toml --
27 baseurl = "http://example.com/"
28 title = "Section Menu"
29 sectionPagesMenu = "sect"
30 -- layouts/_partials/menu.html --
31 {{- $p := .page -}}
32 {{- $m := .menu -}}
33 {{ range (index $p.Site.Menus $m) -}}
34 {{- .URL }}|{{ .Name }}|{{ .Title }}|{{ .Weight -}}|
35 {{- if $p.IsMenuCurrent $m . }}IsMenuCurrent{{ else }}-{{ end -}}|
36 {{- if $p.HasMenuCurrent $m . }}HasMenuCurrent{{ else }}-{{ end -}}|
37 {{- end -}}
38 -- layouts/single.html --
39 Single|{{ .Title }}
40 Menu Sect: {{ partial "menu.html" (dict "page" . "menu" "sect") }}
41 Menu Main: {{ partial "menu.html" (dict "page" . "menu" "main") }}
42 -- layouts/list.html --
43 List|{{ .Title }}|{{ .Content }}
44 -- content/sect1/p1.md --
45 ---
46 title: "p1"
47 weight: 1
48 menu:
49 main:
50 title: "atitle1"
51 weight: 40
52 ---
53 # Doc Menu
54 -- content/sect1/p2.md --
55 ---
56 title: "p2"
57 weight: 2
58 menu:
59 main:
60 title: "atitle2"
61 weight: 30
62 ---
63 # Doc Menu
64 -- content/sect2/p3.md --
65 ---
66 title: "p3"
67 weight: 3
68 menu:
69 main:
70 title: "atitle3"
71 weight: 20
72 ---
73 # Doc Menu
74 -- content/sect2/p4.md --
75 ---
76 title: "p4"
77 weight: 4
78 menu:
79 main:
80 title: "atitle4"
81 weight: 10
82 ---
83 # Doc Menu
84 -- content/sect3/p5.md --
85 ---
86 title: "p5"
87 weight: 5
88 menu:
89 main:
90 title: "atitle5"
91 weight: 5
92 ---
93 # Doc Menu
94 -- content/sect1/_index.md --
95 ---
96 title: "Section One"
97 date: "2017-01-01"
98 weight: 100
99 ---
100 -- content/sect5/_index.md --
101 ---
102 title: "Section Five"
103 date: "2017-01-01"
104 weight: 10
105 ---
106 `
107
108 b := Test(t, files)
109 h := b.H
110
111 s := h.Sites[0]
112
113 b.Assert(len(s.Menus()), qt.Equals, 2)
114
115 p1 := s.RegularPages()[0].Menus()
116
117 // There is only one menu in the page, but it is "member of" 2
118 b.Assert(len(p1), qt.Equals, 1)
119
120 b.AssertFileContent("public/sect1/p1/index.html", "Single",
121 "Menu Sect: "+
122 "/sect5/|Section Five|Section Five|10|-|-|"+
123 "/sect1/|Section One|Section One|100|-|HasMenuCurrent|"+
124 "/sect2/|Sect2s|Sect2s|0|-|-|"+
125 "/sect3/|Sect3s|Sect3s|0|-|-|",
126 "Menu Main: "+
127 "/sect3/p5/|p5|atitle5|5|-|-|"+
128 "/sect2/p4/|p4|atitle4|10|-|-|"+
129 "/sect2/p3/|p3|atitle3|20|-|-|"+
130 "/sect1/p2/|p2|atitle2|30|-|-|"+
131 "/sect1/p1/|p1|atitle1|40|IsMenuCurrent|-|",
132 )
133
134 b.AssertFileContent("public/sect2/p3/index.html", "Single",
135 "Menu Sect: "+
136 "/sect5/|Section Five|Section Five|10|-|-|"+
137 "/sect1/|Section One|Section One|100|-|-|"+
138 "/sect2/|Sect2s|Sect2s|0|-|HasMenuCurrent|"+
139 "/sect3/|Sect3s|Sect3s|0|-|-|")
140 }
141
142 func TestMenusFrontMatter(t *testing.T) {
143 files := `
144 -- hugo.toml --
145 baseURL = "http://example.com/"
146 -- layouts/home.html --
147 Main: {{ len .Site.Menus.main }}
148 Other: {{ len .Site.Menus.other }}
149 {{ range .Site.Menus.main }}
150 * Main|{{ .Name }}: {{ .URL }}
151 {{ end }}
152 {{ range .Site.Menus.other }}
153 * Other|{{ .Name }}: {{ .URL }}
154 {{ end }}
155 -- content/blog/page1.md --
156 ---
157 title: "P1"
158 menu: main
159 ---
160
161 -- content/blog/page2.md --
162 ---
163 title: "P2"
164 menu: [main,other]
165 ---
166
167 -- content/blog/page3.md --
168 ---
169 title: "P3"
170 menu:
171 main:
172 weight: 30
173 ---
174 `
175 b := Test(t, files)
176
177 b.AssertFileContent("public/index.html",
178 "Main: 3", "Other: 1",
179 "Main|P1: /blog/page1/",
180 "Other|P2: /blog/page2/",
181 )
182 }
183
184 // https://github.com/gohugoio/hugo/issues/5849
185 func TestMenusPageMultipleOutputFormats(t *testing.T) {
186 files := `
187 -- hugo.toml --
188 baseURL = "https://example.com"
189
190 # DAMP is similar to AMP, but not permalinkable.
191 [outputFormats]
192 [outputFormats.damp]
193 mediaType = "text/html"
194 path = "damp"
195
196 -- layouts/home.html --
197 {{ range .Site.Menus.main }}{{ .Title }}|{{ .URL }}|{{ end }}
198 -- content/_index.md --
199 ---
200 Title: Home Sweet Home
201 outputs: [ "html", "amp" ]
202 menu: "main"
203 ---
204
205 -- content/blog/html-amp.md --
206 ---
207 Title: AMP and HTML
208 outputs: [ "html", "amp" ]
209 menu: "main"
210 ---
211
212 -- content/blog/html.md --
213 ---
214 Title: HTML only
215 outputs: [ "html" ]
216 menu: "main"
217 ---
218
219 -- content/blog/amp.md --
220 ---
221 Title: AMP only
222 outputs: [ "amp" ]
223 menu: "main"
224 ---
225
226 `
227 b := Test(t, files)
228
229 b.AssertFileContent("public/index.html", "AMP and HTML|/blog/html-amp/|AMP only|/amp/blog/amp/|Home Sweet Home|/|HTML only|/blog/html/|")
230 b.AssertFileContent("public/amp/index.html", "AMP and HTML|/amp/blog/html-amp/|AMP only|/amp/blog/amp/|Home Sweet Home|/amp/|HTML only|/blog/html/|")
231 }
232
233 // https://github.com/gohugoio/hugo/issues/5989
234 func TestMenusPageSortByDate(t *testing.T) {
235 files := `
236 -- hugo.toml --
237 baseURL = "http://example.com/"
238 -- layouts/home.html --
239 {{ range .Site.Menus.main }}{{ .Title }}|Children:
240 {{- $children := sort .Children ".Page.Date" "desc" }}{{ range $children }}{{ .Title }}|{{ end }}{{ end }}
241
242 -- content/blog/a.md --
243 ---
244 Title: A
245 date: 2019-01-01
246 menu:
247 main:
248 identifier: "a"
249 weight: 1
250 ---
251
252 -- content/blog/b.md --
253 ---
254 Title: B
255 date: 2018-01-02
256 menu:
257 main:
258 parent: "a"
259 weight: 100
260 ---
261
262 -- content/blog/c.md --
263 ---
264 Title: C
265 date: 2019-01-03
266 menu:
267 main:
268 parent: "a"
269 weight: 10
270 ---
271
272 `
273 b := Test(t, files)
274
275 b.AssertFileContent("public/index.html", "A|Children:C|B|")
276 }
277
278 func TestMenuParams(t *testing.T) {
279 files := `
280 -- hugo.toml --
281 baseURL = "http://example.com/"
282 [[menus.main]]
283 identifier = "contact"
284 title = "Contact Us"
285 url = "mailto:[email protected]"
286 weight = 300
287 [menus.main.params]
288 foo = "foo_config"
289 key2 = "key2_config"
290 camelCase = "camelCase_config"
291 -- layouts/home.html --
292 Main: {{ len .Site.Menus.main }}
293 {{ range .Site.Menus.main }}
294 foo: {{ .Params.foo }}
295 key2: {{ .Params.KEy2 }}
296 camelCase: {{ .Params.camelcase }}
297 {{ end }}
298 -- content/_index.md --
299 ---
300 title: "Home"
301 menu:
302 main:
303 weight: 10
304 params:
305 foo: "foo_content"
306 key2: "key2_content"
307 camelCase: "camelCase_content"
308 ---
309 `
310 b := Test(t, files)
311
312 b.AssertFileContent("public/index.html", `
313 Main: 2
314
315 foo: foo_content
316 key2: key2_content
317 camelCase: camelCase_content
318
319 foo: foo_config
320 key2: key2_config
321 camelCase: camelCase_config
322 `)
323 }
324
325 func TestMenusShadowMembers(t *testing.T) {
326 files := `
327 -- hugo.toml --
328 baseURL = "http://example.com/"
329 [[menus.main]]
330 identifier = "contact"
331 pageRef = "contact"
332 title = "Contact Us"
333 url = "mailto:[email protected]"
334 weight = 1
335 [[menus.main]]
336 pageRef = "/blog/post3"
337 title = "My Post 3"
338 url = "/blog/post3"
339
340 -- layouts/home.html --
341 Main: {{ len .Site.Menus.main }}
342 {{ range .Site.Menus.main }}
343 {{ .Title }}|HasMenuCurrent: {{ $.HasMenuCurrent "main" . }}|Page: {{ .Page.Path }}
344 {{ .Title }}|IsMenuCurrent: {{ $.IsMenuCurrent "main" . }}|Page: {{ .Page.Path }}
345 {{ end }}
346 -- layouts/single.html --
347 Main: {{ len .Site.Menus.main }}
348 {{ range .Site.Menus.main }}
349 {{ .Title }}|HasMenuCurrent: {{ $.HasMenuCurrent "main" . }}|Page: {{ .Page.Path }}
350 {{ .Title }}|IsMenuCurrent: {{ $.IsMenuCurrent "main" . }}|Page: {{ .Page.Path }}
351 {{ end }}
352 -- content/_index.md --
353 ---
354 title: "Home"
355 menu:
356 main:
357 weight: 10
358 ---
359 -- content/blog/_index.md --
360 ---
361 title: "Blog"
362 menu:
363 main:
364 weight: 20
365 ---
366 -- content/blog/post1.md --
367 ---
368 title: "My Post 1: With No Menu Defined"
369 ---
370 -- content/blog/post2.md --
371 ---
372 title: "My Post 2: With Menu Defined"
373 menu:
374 main:
375 weight: 30
376 ---
377 -- content/blog/post3.md --
378 ---
379 title: "My Post 2: With No Menu Defined"
380 ---
381 -- content/contact.md --
382 ---
383 title: "Contact: With No Menu Defined"
384 ---
385 `
386 b := Test(t, files)
387
388 b.AssertFileContent("public/index.html", `
389 Main: 5
390 Home|HasMenuCurrent: false|Page: /
391 Blog|HasMenuCurrent: false|Page: /blog
392 My Post 2: With Menu Defined|HasMenuCurrent: false|Page: /blog/post2
393 My Post 3|HasMenuCurrent: false|Page: /blog/post3
394 Contact Us|HasMenuCurrent: false|Page: /contact
395 `)
396
397 b.AssertFileContent("public/blog/post1/index.html", `
398 Home|HasMenuCurrent: false|Page: /
399 Blog|HasMenuCurrent: true|Page: /blog
400 `)
401
402 b.AssertFileContent("public/blog/post2/index.html", `
403 Home|HasMenuCurrent: false|Page: /
404 Blog|HasMenuCurrent: true|Page: /blog
405 Blog|IsMenuCurrent: false|Page: /blog
406 `)
407
408 b.AssertFileContent("public/blog/post3/index.html", `
409 Home|HasMenuCurrent: false|Page: /
410 Blog|HasMenuCurrent: true|Page: /blog
411 `)
412
413 b.AssertFileContent("public/contact/index.html", `
414 Contact Us|HasMenuCurrent: false|Page: /contact
415 Contact Us|IsMenuCurrent: true|Page: /contact
416 Blog|HasMenuCurrent: false|Page: /blog
417 Blog|IsMenuCurrent: false|Page: /blog
418 `)
419 }
420
421 // Issue 9846
422 func TestMenuHasMenuCurrentSection(t *testing.T) {
423 t.Parallel()
424
425 files := `
426 -- hugo.toml --
427 disableKinds = ['RSS','sitemap','taxonomy','term']
428 [[menu.main]]
429 name = 'Home'
430 pageRef = '/'
431 weight = 1
432
433 [[menu.main]]
434 name = 'Tests'
435 pageRef = '/tests'
436 weight = 2
437 [[menu.main]]
438 name = 'Test 1'
439 pageRef = '/tests/test-1'
440 parent = 'Tests'
441 weight = 1
442
443 -- content/tests/test-1.md --
444 ---
445 title: "Test 1"
446 ---
447 -- layouts/list.html --
448 {{ range site.Menus.main }}
449 {{ .Name }}|{{ .URL }}|IsMenuCurrent = {{ $.IsMenuCurrent "main" . }}|HasMenuCurrent = {{ $.HasMenuCurrent "main" . }}|
450 {{ range .Children }}
451 {{ .Name }}|{{ .URL }}|IsMenuCurrent = {{ $.IsMenuCurrent "main" . }}|HasMenuCurrent = {{ $.HasMenuCurrent "main" . }}|
452 {{ end }}
453 {{ end }}
454
455 {{/* Some tests for issue 9925 */}}
456 {{ $page := .Site.GetPage "tests/test-1" }}
457 {{ $section := site.GetPage "tests" }}
458
459 Home IsAncestor Self: {{ site.Home.IsAncestor site.Home }}
460 Home IsDescendant Self: {{ site.Home.IsDescendant site.Home }}
461 Section IsAncestor Self: {{ $section.IsAncestor $section }}
462 Section IsDescendant Self: {{ $section.IsDescendant $section}}
463 Page IsAncestor Self: {{ $page.IsAncestor $page }}
464 Page IsDescendant Self: {{ $page.IsDescendant $page}}
465 `
466
467 b := Test(t, files)
468
469 b.AssertFileContent("public/tests/index.html", `
470 Tests|/tests/|IsMenuCurrent = true|HasMenuCurrent = false
471 Home IsAncestor Self: false
472 Home IsDescendant Self: false
473 Section IsAncestor Self: false
474 Section IsDescendant Self: false
475 Page IsAncestor Self: false
476 Page IsDescendant Self: false
477 `)
478 }
479
480 func TestMenusNewConfigSetup(t *testing.T) {
481 t.Parallel()
482
483 files := `
484 -- hugo.toml --
485 baseURL = "https://example.com"
486 title = "Hugo Menu Test"
487 [menus]
488 [[menus.main]]
489 name = "Home"
490 url = "/"
491 pre = "<span>"
492 post = "</span>"
493 weight = 1
494 -- layouts/home.html --
495 {{ range $i, $e := site.Menus.main }}
496 Menu Item: {{ $i }}: {{ .Pre }}{{ .Name }}{{ .Post }}|{{ .URL }}|
497 {{ end }}
498 `
499
500 b := Test(t, files)
501
502 b.AssertFileContent("public/index.html", `
503 Menu Item: 0: <span>Home</span>|/|
504 `)
505 }
506
507 // Issue #11062
508 func TestMenusSubDirInBaseURL(t *testing.T) {
509 t.Parallel()
510
511 files := `
512 -- hugo.toml --
513 baseURL = "https://example.com/foo/"
514 title = "Hugo Menu Test"
515 [menus]
516 [[menus.main]]
517 name = "Posts"
518 url = "/posts"
519 weight = 1
520 -- layouts/home.html --
521 {{ range $i, $e := site.Menus.main }}
522 Menu Item: {{ $i }}|{{ .URL }}|
523 {{ end }}
524 `
525
526 b := Test(t, files)
527
528 b.AssertFileContent("public/index.html", `
529 Menu Item: 0|/foo/posts|
530 `)
531 }
532
533 func TestSectionPagesMenuMultilingualWarningIssue12306(t *testing.T) {
534 t.Parallel()
535
536 files := `
537 -- hugo.toml --
538 disableKinds = ['section','rss','sitemap','taxonomy','term']
539 defaultContentLanguageInSubdir = true
540 sectionPagesMenu = "main"
541 [languages.en]
542 [languages.fr]
543 -- layouts/home.html --
544 {{- range site.Menus.main -}}
545 <a href="{{ .URL }}">{{ .Name }}</a>
546 {{- end -}}
547 -- layouts/single.html --
548 {{ .Title }}
549 -- content/p1.en.md --
550 ---
551 title: p1
552 menu: main
553 ---
554 -- content/p1.fr.md --
555 ---
556 title: p1
557 menu: main
558 ---
559 -- content/p2.en.md --
560 ---
561 title: p2
562 menu: main
563 ---
564 `
565
566 b := Test(t, files, TestOptWarn())
567
568 b.AssertFileContent("public/en/index.html", `<a href="/en/p1/">p1</a><a href="/en/p2/">p2</a>`)
569 b.AssertFileContent("public/fr/index.html", `<a href="/fr/p1/">p1</a>`)
570 b.AssertLogContains("! duplicate menu entry")
571 }
572
573 func TestSectionPagesIssue12399(t *testing.T) {
574 t.Parallel()
575
576 files := `
577 -- hugo.toml --
578 disableKinds = ['rss','sitemap','taxonomy','term']
579 capitalizeListTitles = false
580 pluralizeListTitles = false
581 sectionPagesMenu = 'main'
582 -- content/p1.md --
583 ---
584 title: p1
585 ---
586 -- content/s1/p2.md --
587 ---
588 title: p2
589 menus: main
590 ---
591 -- content/s1/p3.md --
592 ---
593 title: p3
594 ---
595 -- layouts/list.html --
596 {{ range site.Menus.main }}<a href="{{ .URL }}">{{ .Name }}</a>{{ end }}
597 -- layouts/single.html --
598 {{ .Title }}
599 `
600
601 b := Test(t, files)
602
603 b.AssertFileExists("public/index.html", true)
604 b.AssertFileContent("public/index.html", `<a href="/s1/p2/">p2</a><a href="/s1/">s1</a>`)
605 }
606
607 // Issue 14566
608 func TestMenusPageRefSitesMatrix(t *testing.T) {
609 t.Parallel()
610
611 files := `
612 -- hugo.toml --
613 baseURL = "http://example.com/"
614 defaultContentVersionInSubdir = true
615 [versions]
616 [versions.v1]
617 [versions.v2]
618
619 [[menus.main]]
620 name = "Home"
621 pageRef = "/"
622 weight = 1
623
624 -- layouts/home.html --
625 {{ range .Site.Menus.main }}
626 {{ .Name }}|{{ .URL }}|{{ .Page.Path }}|{{ .Page.Site.Version.Name }}
627 {{ end }}
628 `
629
630 b := Test(t, files)
631
632 b.AssertFileContent("public/v1/index.html", `
633 Home|/v1/|/|v1
634 `)
635
636 b.AssertFileContent("public/v2/index.html", `
637 Home|/v2/|/|v2
638 `)
639 }
640
641 // Issue 13161
642 func TestMenuNameAndTitleFallback(t *testing.T) {
643 t.Parallel()
644
645 files := `
646 -- hugo.toml --
647 disableKinds = ['rss','sitemap','taxonomy','term']
648 [[menus.main]]
649 name = 'P1_ME_Name'
650 title = 'P1_ME_Title'
651 pageRef = '/p1'
652 weight = 10
653 [[menus.main]]
654 pageRef = '/p2'
655 weight = 20
656 [[menus.main]]
657 pageRef = '/p3'
658 weight = 30
659 [[menus.main]]
660 name = 'S1_ME_Name'
661 title = 'S1_ME_Title'
662 pageRef = '/s1'
663 weight = 40
664 [[menus.main]]
665 pageRef = '/s2'
666 weight = 50
667 [[menus.main]]
668 pageRef = '/s3'
669 weight = 60
670 -- content/p1.md --
671 ---
672 title: P1_Title
673 ---
674 -- content/p2.md --
675 ---
676 title: P2_Title
677 ---
678 -- content/p3.md --
679 ---
680 title: P3_Title
681 linkTitle: P3_LinkTitle
682 ---
683 -- content/s1/_index.md --
684 ---
685 title: S1_Title
686 ---
687 -- content/s2/_index.md --
688 ---
689 title: S2_Title
690 ---
691 -- content/s3/_index.md --
692 ---
693 title: S3_Title
694 linkTitle: S3_LinkTitle
695 ---
696 -- layouts/single.html --
697 {{ .Content }}
698 -- layouts/list.html --
699 {{ .Content }}
700 -- layouts/home.html --
701 {{- range site.Menus.main }}
702 URL: {{ .URL }}| Name: {{ .Name }}| Title: {{ .Title }}| PageRef: {{ .PageRef }}| Page.Title: {{ .Page.Title }}| Page.LinkTitle: {{ .Page.LinkTitle }}|
703 {{- end }}
704 `
705
706 b := Test(t, files)
707 b.AssertFileContent("public/index.html",
708 `URL: /p1/| Name: P1_ME_Name| Title: P1_ME_Title| PageRef: /p1| Page.Title: P1_Title| Page.LinkTitle: P1_Title|`,
709 `URL: /p2/| Name: P2_Title| Title: P2_Title| PageRef: /p2| Page.Title: P2_Title| Page.LinkTitle: P2_Title|`,
710 `URL: /p3/| Name: P3_LinkTitle| Title: P3_Title| PageRef: /p3| Page.Title: P3_Title| Page.LinkTitle: P3_LinkTitle|`,
711 `URL: /s1/| Name: S1_ME_Name| Title: S1_ME_Title| PageRef: /s1| Page.Title: S1_Title| Page.LinkTitle: S1_Title|`,
712 `URL: /s2/| Name: S2_Title| Title: S2_Title| PageRef: /s2| Page.Title: S2_Title| Page.LinkTitle: S2_Title|`,
713 `URL: /s3/| Name: S3_LinkTitle| Title: S3_Title| PageRef: /s3| Page.Title: S3_Title| Page.LinkTitle: S3_LinkTitle|`,
714 )
715 }