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