github.com/neohugo/neohugo@v0.123.8/hugolib/page_permalink_test.go (about) 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 "html/template" 19 "testing" 20 21 qt "github.com/frankban/quicktest" 22 23 "github.com/neohugo/neohugo/config" 24 ) 25 26 func TestPermalink(t *testing.T) { 27 t.Parallel() 28 29 tests := []struct { 30 file string 31 base template.URL 32 slug string 33 url string 34 uglyURLs bool 35 canonifyURLs bool 36 expectedAbs string 37 expectedRel string 38 }{ 39 {"x/y/z/boofar.md", "", "", "", false, false, "/x/y/z/boofar/", "/x/y/z/boofar/"}, 40 {"x/y/z/boofar.md", "", "", "", false, false, "/x/y/z/boofar/", "/x/y/z/boofar/"}, 41 // Issue #1174 42 {"x/y/z/boofar.md", "http://gopher.com/", "", "", false, true, "http://gopher.com/x/y/z/boofar/", "/x/y/z/boofar/"}, 43 {"x/y/z/boofar.md", "http://gopher.com/", "", "", true, true, "http://gopher.com/x/y/z/boofar.html", "/x/y/z/boofar.html"}, 44 {"x/y/z/boofar.md", "", "boofar", "", false, false, "/x/y/z/boofar/", "/x/y/z/boofar/"}, 45 {"x/y/z/boofar.md", "http://barnew/", "", "", false, false, "http://barnew/x/y/z/boofar/", "/x/y/z/boofar/"}, 46 {"x/y/z/boofar.md", "http://barnew/", "boofar", "", false, false, "http://barnew/x/y/z/boofar/", "/x/y/z/boofar/"}, 47 {"x/y/z/boofar.md", "", "", "", true, false, "/x/y/z/boofar.html", "/x/y/z/boofar.html"}, 48 {"x/y/z/boofar.md", "", "", "", true, false, "/x/y/z/boofar.html", "/x/y/z/boofar.html"}, 49 {"x/y/z/boofar.md", "", "boofar", "", true, false, "/x/y/z/boofar.html", "/x/y/z/boofar.html"}, 50 {"x/y/z/boofar.md", "http://barnew/", "", "", true, false, "http://barnew/x/y/z/boofar.html", "/x/y/z/boofar.html"}, 51 {"x/y/z/boofar.md", "http://barnew/", "boofar", "", true, false, "http://barnew/x/y/z/boofar.html", "/x/y/z/boofar.html"}, 52 {"x/y/z/boofar.md", "http://barnew/boo/", "booslug", "", true, false, "http://barnew/boo/x/y/z/booslug.html", "/boo/x/y/z/booslug.html"}, 53 {"x/y/z/boofar.md", "http://barnew/boo/", "booslug", "", false, true, "http://barnew/boo/x/y/z/booslug/", "/x/y/z/booslug/"}, 54 {"x/y/z/boofar.md", "http://barnew/boo/", "booslug", "", false, false, "http://barnew/boo/x/y/z/booslug/", "/boo/x/y/z/booslug/"}, 55 {"x/y/z/boofar.md", "http://barnew/boo/", "booslug", "", true, true, "http://barnew/boo/x/y/z/booslug.html", "/x/y/z/booslug.html"}, 56 {"x/y/z/boofar.md", "http://barnew/boo", "booslug", "", true, true, "http://barnew/boo/x/y/z/booslug.html", "/x/y/z/booslug.html"}, 57 // Issue #4666 58 {"x/y/z/boo-makeindex.md", "http://barnew/boo", "", "", true, true, "http://barnew/boo/x/y/z/boo-makeindex.html", "/x/y/z/boo-makeindex.html"}, 59 60 // test URL overrides 61 {"x/y/z/boofar.md", "", "", "/z/y/q/", false, false, "/z/y/q/", "/z/y/q/"}, 62 } 63 64 for i, test := range tests { 65 i := i 66 test := test 67 t.Run(fmt.Sprintf("%s-%d", test.file, i), func(t *testing.T) { 68 t.Parallel() 69 c := qt.New(t) 70 cfg := config.New() 71 cfg.Set("uglyURLs", test.uglyURLs) 72 cfg.Set("canonifyURLs", test.canonifyURLs) 73 74 files := fmt.Sprintf(` 75 -- hugo.toml -- 76 baseURL = %q 77 -- content/%s -- 78 --- 79 title: Page 80 slug: %q 81 url: %q 82 output: ["HTML"] 83 --- 84 `, test.base, test.file, test.slug, test.url) 85 86 if i > 0 { 87 t.Skip() 88 } 89 90 b := NewIntegrationTestBuilder( 91 IntegrationTestConfig{ 92 T: t, 93 TxtarString: files, 94 BaseCfg: cfg, 95 }, 96 ) 97 98 b.Build() 99 s := b.H.Sites[0] 100 c.Assert(len(s.RegularPages()), qt.Equals, 1) 101 p := s.RegularPages()[0] 102 u := p.Permalink() 103 104 expected := test.expectedAbs 105 if u != expected { 106 t.Fatalf("[%d] Expected abs url: %s, got: %s", i, expected, u) 107 } 108 109 u = p.RelPermalink() 110 111 expected = test.expectedRel 112 if u != expected { 113 t.Errorf("[%d] Expected rel url: %s, got: %s", i, expected, u) 114 } 115 }) 116 } 117 } 118 119 func TestRelativeURLInFrontMatter(t *testing.T) { 120 config := ` 121 baseURL = "https://example.com" 122 defaultContentLanguage = "en" 123 defaultContentLanguageInSubdir = false 124 125 [Languages] 126 [Languages.en] 127 weight = 10 128 contentDir = "content/en" 129 [Languages.nn] 130 weight = 20 131 contentDir = "content/nn" 132 133 ` 134 135 pageTempl := `--- 136 title: "A page" 137 url: %q 138 --- 139 140 Some content. 141 ` 142 143 b := newTestSitesBuilder(t).WithConfigFile("toml", config) 144 b.WithContent("content/en/blog/page1.md", fmt.Sprintf(pageTempl, "myblog/p1/")) 145 b.WithContent("content/en/blog/page2.md", fmt.Sprintf(pageTempl, "../../../../../myblog/p2/")) 146 b.WithContent("content/en/blog/page3.md", fmt.Sprintf(pageTempl, "../myblog/../myblog/p3/")) 147 b.WithContent("content/en/blog/_index.md", fmt.Sprintf(pageTempl, "this-is-my-english-blog")) 148 b.WithContent("content/nn/blog/page1.md", fmt.Sprintf(pageTempl, "myblog/p1/")) 149 b.WithContent("content/nn/blog/_index.md", fmt.Sprintf(pageTempl, "this-is-my-blog")) 150 151 b.Build(BuildCfg{}) 152 153 b.AssertFileContent("public/nn/myblog/p1/index.html", "Single: A page|Hello|nn|RelPermalink: /nn/myblog/p1/|") 154 b.AssertFileContent("public/nn/this-is-my-blog/index.html", "List Page 1|A page|Hello|https://example.com/nn/this-is-my-blog/|") 155 b.AssertFileContent("public/this-is-my-english-blog/index.html", "List Page 1|A page|Hello|https://example.com/this-is-my-english-blog/|") 156 b.AssertFileContent("public/myblog/p1/index.html", "Single: A page|Hello|en|RelPermalink: /myblog/p1/|Permalink: https://example.com/myblog/p1/|") 157 b.AssertFileContent("public/myblog/p2/index.html", "Single: A page|Hello|en|RelPermalink: /myblog/p2/|Permalink: https://example.com/myblog/p2/|") 158 b.AssertFileContent("public/myblog/p3/index.html", "Single: A page|Hello|en|RelPermalink: /myblog/p3/|Permalink: https://example.com/myblog/p3/|") 159 }