pagecollections_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
---
pagecollections_test.go (22601B)
---
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 "math/rand"
19 "path"
20 "path/filepath"
21 "testing"
22 "time"
23
24 qt "github.com/frankban/quicktest"
25 "github.com/gohugoio/hugo/resources/kinds"
26 "github.com/gohugoio/hugo/resources/page"
27
28 "github.com/gohugoio/hugo/deps"
29 )
30
31 const pageCollectionsPageTemplate = `---
32 title: "%s"
33 categories:
34 - Hugo
35 ---
36 # Doc
37 `
38
39 func BenchmarkGetPage(b *testing.B) {
40 var (
41 cfg, fs = newTestCfg()
42 r = rand.New(rand.NewSource(time.Now().UnixNano()))
43 )
44
45 configs, err := loadTestConfigFromProvider(cfg)
46 if err != nil {
47 b.Fatal(err)
48 }
49
50 for i := range 10 {
51 for j := range 100 {
52 writeSource(b, fs, filepath.Join("content", fmt.Sprintf("sect%d", i), fmt.Sprintf("page%d.md", j)), "CONTENT")
53 }
54 }
55
56 s := buildSingleSite(b, deps.DepsCfg{Fs: fs, Configs: configs}, BuildCfg{SkipRender: true})
57
58 pagePaths := make([]string, b.N)
59
60 for i := 0; i < b.N; i++ {
61 pagePaths[i] = fmt.Sprintf("sect%d", r.Intn(10))
62 }
63
64 b.ResetTimer()
65 for i := 0; i < b.N; i++ {
66 home, _ := s.getPage(nil, "/")
67 if home == nil {
68 b.Fatal("Home is nil")
69 }
70
71 p, _ := s.getPage(nil, pagePaths[i])
72 if p == nil {
73 b.Fatal("Section is nil")
74 }
75
76 }
77 }
78
79 func createGetPageRegularBenchmarkSite(t testing.TB) *Site {
80 var (
81 c = qt.New(t)
82 cfg, fs = newTestCfg()
83 )
84
85 configs, err := loadTestConfigFromProvider(cfg)
86 if err != nil {
87 t.Fatal(err)
88 }
89
90 pc := func(title string) string {
91 return fmt.Sprintf(pageCollectionsPageTemplate, title)
92 }
93
94 for i := range 10 {
95 for j := range 100 {
96 content := pc(fmt.Sprintf("Title%d_%d", i, j))
97 writeSource(c, fs, filepath.Join("content", fmt.Sprintf("sect%d", i), fmt.Sprintf("page%d.md", j)), content)
98 }
99 }
100
101 return buildSingleSite(c, deps.DepsCfg{Fs: fs, Configs: configs}, BuildCfg{SkipRender: true})
102 }
103
104 func TestBenchmarkGetPageRegular(t *testing.T) {
105 c := qt.New(t)
106 s := createGetPageRegularBenchmarkSite(t)
107
108 for i := range 10 {
109 pp := path.Join("/", fmt.Sprintf("sect%d", i), fmt.Sprintf("page%d.md", i))
110 page, _ := s.getPage(nil, pp)
111 c.Assert(page, qt.Not(qt.IsNil), qt.Commentf(pp))
112 }
113 }
114
115 func BenchmarkGetPageRegular(b *testing.B) {
116 r := rand.New(rand.NewSource(time.Now().UnixNano()))
117
118 b.Run("From root", func(b *testing.B) {
119 s := createGetPageRegularBenchmarkSite(b)
120 c := qt.New(b)
121
122 pagePaths := make([]string, b.N)
123
124 for i := 0; i < b.N; i++ {
125 pagePaths[i] = path.Join(fmt.Sprintf("/sect%d", r.Intn(10)), fmt.Sprintf("page%d.md", r.Intn(100)))
126 }
127
128 b.ResetTimer()
129 for i := 0; i < b.N; i++ {
130 page, _ := s.getPage(nil, pagePaths[i])
131 c.Assert(page, qt.Not(qt.IsNil))
132 }
133 })
134
135 b.Run("Page relative", func(b *testing.B) {
136 s := createGetPageRegularBenchmarkSite(b)
137 c := qt.New(b)
138 allPages := s.RegularPages()
139
140 pagePaths := make([]string, b.N)
141 pages := make([]page.Page, b.N)
142
143 for i := 0; i < b.N; i++ {
144 pagePaths[i] = fmt.Sprintf("page%d.md", r.Intn(100))
145 pages[i] = allPages[r.Intn(len(allPages)/3)]
146 }
147
148 b.ResetTimer()
149 for i := 0; i < b.N; i++ {
150 page, _ := s.getPage(pages[i], pagePaths[i])
151 c.Assert(page, qt.Not(qt.IsNil))
152 }
153 })
154 }
155
156 type getPageTest struct {
157 name string
158 kind string
159 context page.Page
160 pathVariants []string
161 expectedTitle string
162 }
163
164 func (t *getPageTest) check(p page.Page, err error, errorMsg string, c *qt.C) {
165 c.Helper()
166 errorComment := qt.Commentf(errorMsg)
167 switch t.kind {
168 case "Ambiguous":
169 c.Assert(err, qt.Not(qt.IsNil))
170 c.Assert(p, qt.IsNil, errorComment)
171 case "NoPage":
172 c.Assert(err, qt.IsNil)
173 c.Assert(p, qt.IsNil, errorComment)
174 default:
175 c.Assert(err, qt.IsNil, errorComment)
176 c.Assert(p, qt.Not(qt.IsNil), errorComment)
177 c.Assert(p.Kind(), qt.Equals, t.kind, errorComment)
178 c.Assert(p.Title(), qt.Equals, t.expectedTitle, errorComment)
179 }
180 }
181
182 func TestGetPage(t *testing.T) {
183 var (
184 cfg, fs = newTestCfg()
185 c = qt.New(t)
186 )
187
188 configs, err := loadTestConfigFromProvider(cfg)
189 c.Assert(err, qt.IsNil)
190
191 pc := func(title string) string {
192 return fmt.Sprintf(pageCollectionsPageTemplate, title)
193 }
194
195 for i := range 10 {
196 for j := range 10 {
197 content := pc(fmt.Sprintf("Title%d_%d", i, j))
198 writeSource(t, fs, filepath.Join("content", fmt.Sprintf("sect%d", i), fmt.Sprintf("page%d.md", j)), content)
199 }
200 }
201
202 content := pc("home page")
203 writeSource(t, fs, filepath.Join("content", "_index.md"), content)
204
205 content = pc("about page")
206 writeSource(t, fs, filepath.Join("content", "about.md"), content)
207
208 content = pc("section 3")
209 writeSource(t, fs, filepath.Join("content", "sect3", "_index.md"), content)
210
211 writeSource(t, fs, filepath.Join("content", "sect3", "unique.md"), pc("UniqueBase"))
212 writeSource(t, fs, filepath.Join("content", "sect3", "Unique2.md"), pc("UniqueBase2"))
213
214 content = pc("another sect7")
215 writeSource(t, fs, filepath.Join("content", "sect3", "sect7", "_index.md"), content)
216
217 content = pc("deep page")
218 writeSource(t, fs, filepath.Join("content", "sect3", "subsect", "deep.md"), content)
219
220 // Bundle variants
221 writeSource(t, fs, filepath.Join("content", "sect3", "b1", "index.md"), pc("b1 bundle"))
222 writeSource(t, fs, filepath.Join("content", "sect3", "index", "index.md"), pc("index bundle"))
223
224 writeSource(t, fs, filepath.Join("content", "section_bundle_overlap", "_index.md"), pc("index overlap section"))
225 writeSource(t, fs, filepath.Join("content", "section_bundle_overlap_bundle", "index.md"), pc("index overlap bundle"))
226
227 s := buildSingleSite(t, deps.DepsCfg{Fs: fs, Configs: configs}, BuildCfg{SkipRender: true})
228
229 sec3, err := s.getPage(nil, "/sect3")
230 c.Assert(err, qt.IsNil)
231 c.Assert(sec3, qt.Not(qt.IsNil))
232
233 tests := []getPageTest{
234 // legacy content root relative paths
235 {"Root relative, no slash, home", kinds.KindHome, nil, []string{""}, "home page"},
236 {"Root relative, no slash, root page", kinds.KindPage, nil, []string{"about.md", "ABOUT.md"}, "about page"},
237 {"Root relative, no slash, section", kinds.KindSection, nil, []string{"sect3"}, "section 3"},
238 {"Root relative, no slash, section page", kinds.KindPage, nil, []string{"sect3/page1.md"}, "Title3_1"},
239 {"Root relative, no slash, sub section", kinds.KindSection, nil, []string{"sect3/sect7"}, "another sect7"},
240 {"Root relative, no slash, nested page", kinds.KindPage, nil, []string{"sect3/subsect/deep.md"}, "deep page"},
241 {"Root relative, no slash, OS slashes", kinds.KindPage, nil, []string{filepath.FromSlash("sect5/page3.md")}, "Title5_3"},
242
243 {"Short ref, unique", kinds.KindPage, nil, []string{"unique.md", "unique"}, "UniqueBase"},
244 {"Short ref, unique, upper case", kinds.KindPage, nil, []string{"Unique2.md", "unique2.md", "unique2"}, "UniqueBase2"},
245 {"Short ref, ambiguous", "Ambiguous", nil, []string{"page1.md"}, ""},
246
247 // ISSUE: This is an ambiguous ref, but because we have to support the legacy
248 // content root relative paths without a leading slash, the lookup
249 // returns /sect7. This undermines ambiguity detection, but we have no choice.
250 //{"Ambiguous", nil, []string{"sect7"}, ""},
251 {"Section, ambiguous", kinds.KindSection, nil, []string{"sect7"}, "Sect7s"},
252
253 {"Absolute, home", kinds.KindHome, nil, []string{"/", ""}, "home page"},
254 {"Absolute, page", kinds.KindPage, nil, []string{"/about.md", "/about"}, "about page"},
255 {"Absolute, sect", kinds.KindSection, nil, []string{"/sect3"}, "section 3"},
256 {"Absolute, page in subsection", kinds.KindPage, nil, []string{"/sect3/page1.md", "/Sect3/Page1.md"}, "Title3_1"},
257 {"Absolute, section, subsection with same name", kinds.KindSection, nil, []string{"/sect3/sect7"}, "another sect7"},
258 {"Absolute, page, deep", kinds.KindPage, nil, []string{"/sect3/subsect/deep.md"}, "deep page"},
259 {"Absolute, page, OS slashes", kinds.KindPage, nil, []string{filepath.FromSlash("/sect5/page3.md")}, "Title5_3"}, // test OS-specific path
260 {"Absolute, unique", kinds.KindPage, nil, []string{"/sect3/unique.md"}, "UniqueBase"},
261 {"Absolute, unique, case", kinds.KindPage, nil, []string{"/sect3/Unique2.md", "/sect3/unique2.md", "/sect3/unique2", "/sect3/Unique2"}, "UniqueBase2"},
262 // next test depends on this page existing
263 // {"NoPage", nil, []string{"/unique.md"}, ""}, // ISSUE #4969: this is resolving to /sect3/unique.md
264 {"Absolute, missing page", "NoPage", nil, []string{"/missing-page.md"}, ""},
265 {"Absolute, missing section", "NoPage", nil, []string{"/missing-section"}, ""},
266
267 // relative paths
268 {"Dot relative, home", kinds.KindHome, sec3, []string{".."}, "home page"},
269 {"Dot relative, home, slash", kinds.KindHome, sec3, []string{"../"}, "home page"},
270 {"Dot relative about", kinds.KindPage, sec3, []string{"../about.md"}, "about page"},
271 {"Dot", kinds.KindSection, sec3, []string{"."}, "section 3"},
272 {"Dot slash", kinds.KindSection, sec3, []string{"./"}, "section 3"},
273 {"Page relative, no dot", kinds.KindPage, sec3, []string{"page1.md"}, "Title3_1"},
274 {"Page relative, dot", kinds.KindPage, sec3, []string{"./page1.md"}, "Title3_1"},
275 {"Up and down another section", kinds.KindPage, sec3, []string{"../sect4/page2.md"}, "Title4_2"},
276 {"Rel sect7", kinds.KindSection, sec3, []string{"sect7"}, "another sect7"},
277 {"Rel sect7 dot", kinds.KindSection, sec3, []string{"./sect7"}, "another sect7"},
278 {"Dot deep", kinds.KindPage, sec3, []string{"./subsect/deep.md"}, "deep page"},
279 {"Dot dot inner", kinds.KindPage, sec3, []string{"./subsect/../../sect7/page9.md"}, "Title7_9"},
280 {"Dot OS slash", kinds.KindPage, sec3, []string{filepath.FromSlash("../sect5/page3.md")}, "Title5_3"}, // test OS-specific path
281 {"Dot unique", kinds.KindPage, sec3, []string{"./unique.md"}, "UniqueBase"},
282 {"Dot sect", "NoPage", sec3, []string{"./sect2"}, ""},
283 //{"NoPage", sec3, []string{"sect2"}, ""}, // ISSUE: /sect3 page relative query is resolving to /sect2
284
285 {"Abs, ignore context, home", kinds.KindHome, sec3, []string{"/"}, "home page"},
286 {"Abs, ignore context, about", kinds.KindPage, sec3, []string{"/about.md"}, "about page"},
287 {"Abs, ignore context, page in section", kinds.KindPage, sec3, []string{"/sect4/page2.md"}, "Title4_2"},
288 {"Abs, ignore context, page subsect deep", kinds.KindPage, sec3, []string{"/sect3/subsect/deep.md"}, "deep page"}, // next test depends on this page existing
289 {"Abs, ignore context, page deep", "NoPage", sec3, []string{"/subsect/deep.md"}, ""},
290
291 // Taxonomies
292 {"Taxonomy term", kinds.KindTaxonomy, nil, []string{"categories"}, "Categories"},
293 {"Taxonomy", kinds.KindTerm, nil, []string{"categories/hugo", "categories/Hugo"}, "Hugo"},
294
295 // Bundle variants
296 {"Bundle regular", kinds.KindPage, nil, []string{"sect3/b1", "sect3/b1/index.md", "sect3/b1/index.en.md"}, "b1 bundle"},
297 {"Bundle index name", kinds.KindPage, nil, []string{"sect3/index/index.md", "sect3/index"}, "index bundle"},
298
299 // https://github.com/gohugoio/hugo/issues/7301
300 {"Section and bundle overlap", kinds.KindPage, nil, []string{"section_bundle_overlap_bundle"}, "index overlap bundle"},
301 }
302
303 for _, test := range tests {
304 c.Run(test.name, func(c *qt.C) {
305 errorMsg := fmt.Sprintf("Test case %v %v -> %s", test.context, test.pathVariants, test.expectedTitle)
306
307 // test legacy public Site.GetPage (which does not support page context relative queries)
308 if test.context == nil {
309 for _, ref := range test.pathVariants {
310 args := append([]string{test.kind}, ref)
311 page, err := s.GetPage(args...)
312 test.check(page, err, errorMsg, c)
313 }
314 }
315
316 // test new internal Site.getPage
317 for _, ref := range test.pathVariants {
318 page2, err := s.getPage(test.context, ref)
319 test.check(page2, err, errorMsg, c)
320 }
321 })
322 }
323 }
324
325 // #11664
326 func TestGetPageIndexIndex(t *testing.T) {
327 files := `
328 -- hugo.toml --
329 disableKinds = ["taxonomy", "term"]
330 -- content/mysect/index/index.md --
331 ---
332 title: "Mysect Index"
333 ---
334 -- layouts/index.html --
335 GetPage 1: {{ with site.GetPage "mysect/index/index.md" }}{{ .Title }}|{{ .RelPermalink }}|{{ .Path }}{{ end }}|
336 GetPage 2: {{ with site.GetPage "mysect/index" }}{{ .Title }}|{{ .RelPermalink }}|{{ .Path }}{{ end }}|
337 `
338
339 b := Test(t, files)
340 b.AssertFileContent("public/index.html",
341 "GetPage 1: Mysect Index|/mysect/index/|/mysect/index|",
342 "GetPage 2: Mysect Index|/mysect/index/|/mysect/index|",
343 )
344 }
345
346 // https://github.com/gohugoio/hugo/issues/6034
347 func TestGetPageRelative(t *testing.T) {
348 b := newTestSitesBuilder(t)
349 for i, section := range []string{"what", "where", "who"} {
350 isDraft := i == 2
351 b.WithContent(
352 section+"/_index.md", fmt.Sprintf("---title: %s\n---", section),
353 section+"/members.md", fmt.Sprintf("---title: members %s\ndraft: %t\n---", section, isDraft),
354 )
355 }
356
357 b.WithTemplates("_default/list.html", `
358 {{ with .GetPage "members.md" }}
359 Members: {{ .Title }}
360 {{ else }}
361 NOT FOUND
362 {{ end }}
363 `)
364
365 b.Build(BuildCfg{})
366
367 b.AssertFileContent("public/what/index.html", `Members: members what`)
368 b.AssertFileContent("public/where/index.html", `Members: members where`)
369 b.AssertFileContent("public/who/index.html", `NOT FOUND`)
370 }
371
372 func TestGetPageIssue11883(t *testing.T) {
373 files := `
374 -- hugo.toml --
375 -- p1/index.md --
376 ---
377 title: p1
378 ---
379 -- p1/p1.xyz --
380 xyz.
381 -- layouts/index.html --
382 Home. {{ with .Page.GetPage "p1.xyz" }}{{ else }}OK 1{{ end }} {{ with .Site.GetPage "p1.xyz" }}{{ else }}OK 2{{ end }}
383 `
384
385 b := Test(t, files)
386 b.AssertFileContent("public/index.html", "Home. OK 1 OK 2")
387 }
388
389 func TestGetPageIssue12120(t *testing.T) {
390 t.Parallel()
391
392 files := `
393 -- hugo.toml --
394 disableKinds = ['home','rss','section','sitemap','taxonomy','term']
395 -- content/s1/p1/index.md --
396 ---
397 title: p1
398 layout: p1
399 ---
400 -- content/s1/p2.md --
401 ---
402 title: p2
403 layout: p2
404 ---
405 -- layouts/_default/p1.html --
406 {{ (.GetPage "p2.md").Title }}|
407 -- layouts/_default/p2.html --
408 {{ (.GetPage "p1").Title }}|
409 `
410
411 b := Test(t, files)
412 b.AssertFileContent("public/s1/p1/index.html", "p2") // failing test
413 b.AssertFileContent("public/s1/p2/index.html", "p1")
414 }
415
416 func TestGetPageNewsVsTagsNewsIssue12638(t *testing.T) {
417 t.Parallel()
418
419 files := `
420 -- hugo.toml --
421 disableKinds = ['rss','section','sitemap']
422 [taxonomies]
423 tag = "tags"
424 -- content/p1.md --
425 ---
426 title: p1
427 tags: [news]
428 ---
429 -- layouts/index.html --
430 /tags/news: {{ with .Site.GetPage "/tags/news" }}{{ .Title }}{{ end }}|
431 news: {{ with .Site.GetPage "news" }}{{ .Title }}{{ end }}|
432 /news: {{ with .Site.GetPage "/news" }}{{ .Title }}{{ end }}|
433
434 `
435
436 b := Test(t, files)
437
438 b.AssertFileContent("public/index.html",
439 "/tags/news: News|",
440 "news: News|",
441 "/news: |",
442 )
443 }
444
445 func TestGetPageBundleToRegular(t *testing.T) {
446 files := `
447 -- hugo.toml --
448 -- content/s1/p1/index.md --
449 ---
450 title: p1
451 ---
452 -- content/s1/p2.md --
453 ---
454 title: p2
455 ---
456 -- layouts/_default/single.html --
457 {{ with .GetPage "p2" }}
458 OK: {{ .LinkTitle }}
459 {{ else }}
460 Unable to get p2.
461 {{ end }}
462 `
463
464 b := Test(t, files)
465 b.AssertFileContent("public/s1/p1/index.html", "OK: p2")
466 b.AssertFileContent("public/s1/p2/index.html", "OK: p2")
467 }
468
469 func TestPageGetPageVariations(t *testing.T) {
470 files := `
471 -- hugo.toml --
472 -- content/s1/_index.md --
473 ---
474 title: s1 section
475 ---
476 -- content/s1/p1/index.md --
477 ---
478 title: p1
479 ---
480 -- content/s1/p2.md --
481 ---
482 title: p2
483 ---
484 -- content/s2/p3/index.md --
485 ---
486 title: p3
487 ---
488 -- content/p2.md --
489 ---
490 title: p2_root
491 ---
492 -- layouts/index.html --
493 /s1: {{ with .GetPage "/s1" }}{{ .Title }}{{ end }}|
494 /s1/: {{ with .GetPage "/s1/" }}{{ .Title }}{{ end }}|
495 /s1/p2.md: {{ with .GetPage "/s1/p2.md" }}{{ .Title }}{{ end }}|
496 /s1/p2: {{ with .GetPage "/s1/p2" }}{{ .Title }}{{ end }}|
497 /s1/p1/index.md: {{ with .GetPage "/s1/p1/index.md" }}{{ .Title }}{{ end }}|
498 /s1/p1: {{ with .GetPage "/s1/p1" }}{{ .Title }}{{ end }}|
499 -- layouts/_default/single.html --
500 ../p2: {{ with .GetPage "../p2" }}{{ .Title }}{{ end }}|
501 ../p2.md: {{ with .GetPage "../p2.md" }}{{ .Title }}{{ end }}|
502 p1/index.md: {{ with .GetPage "p1/index.md" }}{{ .Title }}{{ end }}|
503 ../s2/p3/index.md: {{ with .GetPage "../s2/p3/index.md" }}{{ .Title }}{{ end }}|
504 `
505
506 b := Test(t, files)
507
508 b.AssertFileContent("public/index.html", `
509 /s1: s1 section|
510 /s1/: s1 section|
511 /s1/p2.md: p2|
512 /s1/p2: p2|
513 /s1/p1/index.md: p1|
514 /s1/p1: p1|
515 `)
516
517 b.AssertFileContent("public/s1/p1/index.html", `
518 ../p2: p2_root|
519 ../p2.md: p2|
520
521 `)
522
523 b.AssertFileContent("public/s1/p2/index.html", `
524 ../p2: p2_root|
525 ../p2.md: p2_root|
526 p1/index.md: p1|
527 ../s2/p3/index.md: p3|
528
529 `)
530 }
531
532 func TestPageGetPageMountsReverseLookup(t *testing.T) {
533 tempDir := t.TempDir()
534
535 files := `
536 -- README.md --
537 ---
538 title: README
539 ---
540 -- blog/b1.md --
541 ---
542 title: b1
543 ---
544 {{< ref "../docs/d1.md" >}}
545 -- blog/b2/index.md --
546 ---
547 title: b2
548 ---
549 {{< ref "../../docs/d1.md" >}}
550 -- docs/d1.md --
551 ---
552 title: d1
553 ---
554 -- hugo.toml --
555 baseURL = "https://example.com/"
556 [module]
557 [[module.mounts]]
558 source = "layouts"
559 target = "layouts"
560 [[module.mounts]]
561 source = "README.md"
562 target = "content/_index.md"
563 [[module.mounts]]
564 source = "blog"
565 target = "content/posts"
566 [[module.mounts]]
567 source = "docs"
568 target = "content/mydocs"
569 -- layouts/shortcodes/ref.html --
570 {{ $ref := .Get 0 }}
571 .Page.GetPage({{ $ref }}).Title: {{ with .Page.GetPage $ref }}{{ .Title }}{{ end }}|
572 -- layouts/index.html --
573 Home.
574 /blog/b1.md: {{ with .GetPage "/blog/b1.md" }}{{ .Title }}{{ end }}|
575 /blog/b2/index.md: {{ with .GetPage "/blog/b2/index.md" }}{{ .Title }}{{ end }}|
576 /docs/d1.md: {{ with .GetPage "/docs/d1.md" }}{{ .Title }}{{ end }}|
577 /README.md: {{ with .GetPage "/README.md" }}{{ .Title }}{{ end }}|
578 -- layouts/_default/single.html --
579 Single.
580 /README.md: {{ with .GetPage "/README.md" }}{{ .Title }}{{ end }}|
581 {{ .Content }}
582
583
584 `
585 b := Test(t, files, TestOptWithConfig(func(cfg *IntegrationTestConfig) { cfg.WorkingDir = tempDir }))
586
587 b.AssertFileContent("public/index.html",
588 `
589 /blog/b1.md: b1|
590 /blog/b2/index.md: b2|
591 /docs/d1.md: d1|
592 /README.md: README
593 `,
594 )
595
596 b.AssertFileContent("public/mydocs/d1/index.html", `README.md: README|`)
597
598 b.AssertFileContent("public/posts/b1/index.html", `.Page.GetPage(../docs/d1.md).Title: d1|`)
599 b.AssertFileContent("public/posts/b2/index.html", `.Page.GetPage(../../docs/d1.md).Title: d1|`)
600 }
601
602 // https://github.com/gohugoio/hugo/issues/7016
603 func TestGetPageMultilingual(t *testing.T) {
604 b := newTestSitesBuilder(t)
605
606 b.WithConfigFile("yaml", `
607 baseURL: "http://example.org/"
608 languageCode: "en-us"
609 defaultContentLanguage: ru
610 title: "My New Hugo Site"
611 uglyurls: true
612
613 languages:
614 ru: {}
615 en: {}
616 `)
617
618 b.WithContent(
619 "docs/1.md", "\n---title: p1\n---",
620 "news/1.md", "\n---title: p1\n---",
621 "news/1.en.md", "\n---title: p1en\n---",
622 "news/about/1.md", "\n---title: about1\n---",
623 "news/about/1.en.md", "\n---title: about1en\n---",
624 )
625
626 b.WithTemplates("index.html", `
627 {{ with site.GetPage "docs/1" }}
628 Docs p1: {{ .Title }}
629 {{ else }}
630 NOT FOUND
631 {{ end }}
632 `)
633
634 b.Build(BuildCfg{})
635
636 b.AssertFileContent("public/index.html", `Docs p1: p1`)
637 b.AssertFileContent("public/en/index.html", `NOT FOUND`)
638 }
639
640 func TestRegularPagesRecursive(t *testing.T) {
641 b := newTestSitesBuilder(t)
642
643 b.WithConfigFile("yaml", `
644 baseURL: "http://example.org/"
645 title: "My New Hugo Site"
646
647 `)
648
649 b.WithContent(
650 "docs/1.md", "\n---title: docs1\n---",
651 "docs/sect1/_index.md", "\n---title: docs_sect1\n---",
652 "docs/sect1/ps1.md", "\n---title: docs_sect1_ps1\n---",
653 "docs/sect1/ps2.md", "\n---title: docs_sect1_ps2\n---",
654 "docs/sect1/sect1_s2/_index.md", "\n---title: docs_sect1_s2\n---",
655 "docs/sect1/sect1_s2/ps2_1.md", "\n---title: docs_sect1_s2_1\n---",
656 "docs/sect2/_index.md", "\n---title: docs_sect2\n---",
657 "docs/sect2/ps1.md", "\n---title: docs_sect2_ps1\n---",
658 "docs/sect2/ps2.md", "\n---title: docs_sect2_ps2\n---",
659 "news/1.md", "\n---title: news1\n---",
660 )
661
662 b.WithTemplates("index.html", `
663 {{ $sect1 := site.GetPage "sect1" }}
664
665 Sect1 RegularPagesRecursive: {{ range $sect1.RegularPagesRecursive }}{{ .Kind }}:{{ .RelPermalink}}|{{ end }}|End.
666
667 `)
668
669 b.Build(BuildCfg{})
670
671 b.AssertFileContent("public/index.html", `
672 Sect1 RegularPagesRecursive: page:/docs/sect1/ps1/|page:/docs/sect1/ps2/|page:/docs/sect1/sect1_s2/ps2_1/||End.
673
674
675 `)
676 }
677
678 func TestRegularPagesRecursiveHome(t *testing.T) {
679 files := `
680 -- hugo.toml --
681 -- content/p1.md --
682 -- content/post/p2.md --
683 -- layouts/index.html --
684 RegularPagesRecursive: {{ range .RegularPagesRecursive }}{{ .Kind }}:{{ .RelPermalink}}|{{ end }}|End.
685 `
686
687 b := NewIntegrationTestBuilder(
688 IntegrationTestConfig{
689 T: t,
690 TxtarString: files,
691 }).Build()
692
693 b.AssertFileContent("public/index.html", `RegularPagesRecursive: page:/p1/|page:/post/p2/||End.`)
694 }
695
696 // Issue #12169.
697 func TestPagesSimilarSectionNames(t *testing.T) {
698 files := `
699 -- hugo.toml --
700 -- content/draftsection/_index.md --
701 ---
702 draft: true
703 ---
704 -- content/draftsection/sub/_index.md --got
705 -- content/draftsection/sub/d1.md --
706 -- content/s1/_index.md --
707 -- content/s1/p1.md --
708 -- content/s1-foo/_index.md --
709 -- content/s1-foo/p2.md --
710 -- content/s1-foo/s2/_index.md --
711 -- content/s1-foo/s2/p3.md --
712 -- content/s1-foo/s2-foo/_index.md --
713 -- content/s1-foo/s2-foo/p4.md --
714 -- layouts/_default/list.html --
715 {{ .RelPermalink }}: Pages: {{ range .Pages }}{{ .RelPermalink }}|{{ end }}$
716
717 `
718 b := Test(t, files)
719
720 b.AssertFileContent("public/index.html", "/: Pages: /s1-foo/|/s1/|$")
721 b.AssertFileContent("public/s1/index.html", "/s1/: Pages: /s1/p1/|$")
722 b.AssertFileContent("public/s1-foo/index.html", "/s1-foo/: Pages: /s1-foo/p2/|/s1-foo/s2-foo/|/s1-foo/s2/|$")
723 b.AssertFileContent("public/s1-foo/s2/index.html", "/s1-foo/s2/: Pages: /s1-foo/s2/p3/|$")
724 }
725
726 func TestGetPageContentAdapterBaseIssue12561(t *testing.T) {
727 t.Parallel()
728
729 files := `
730 -- hugo.toml --
731 disableKinds = ['rss','section','sitemap','taxonomy','term']
732 -- layouts/index.html --
733 Test A: {{ (site.GetPage "/s1/p1").Title }}
734 Test B: {{ (site.GetPage "p1").Title }}
735 Test C: {{ (site.GetPage "/s2/p2").Title }}
736 Test D: {{ (site.GetPage "p2").Title }}
737 -- layouts/_default/single.html --
738 {{ .Title }}
739 -- content/s1/p1.md --
740 ---
741 title: p1
742 ---
743 -- content/s2/_content.gotmpl --
744 {{ .AddPage (dict "path" "p2" "title" "p2") }}
745 `
746
747 b := Test(t, files)
748
749 b.AssertFileExists("public/s1/p1/index.html", true)
750 b.AssertFileExists("public/s2/p2/index.html", true)
751 b.AssertFileContent("public/index.html",
752 "Test A: p1",
753 "Test B: p1",
754 "Test C: p2",
755 "Test D: p2", // fails
756 )
757 }