github.com/anakojm/hugo-katex@v0.0.0-20231023141351-42d6f5de9c0b/resources/page/permalinks_integration_test.go (about)

     1  // Copyright 2023 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 page_test
    15  
    16  import (
    17  	"testing"
    18  
    19  	"github.com/bep/logg"
    20  	qt "github.com/frankban/quicktest"
    21  	"github.com/gohugoio/hugo/hugolib"
    22  )
    23  
    24  func TestPermalinks(t *testing.T) {
    25  	t.Parallel()
    26  
    27  	files := `
    28  -- layouts/_default/list.html --
    29  List|{{ .Kind }}|{{ .RelPermalink }}|
    30  -- layouts/_default/single.html --
    31  Single|{{ .Kind }}|{{ .RelPermalink }}|
    32  -- hugo.toml --
    33  [taxonomies]
    34  tag = "tags"
    35  [permalinks.page]
    36  withpageslug = '/pageslug/:slug/'
    37  withallbutlastsection = '/:sections[:last]/:slug/'
    38  [permalinks.section]
    39  withfilefilename = '/sectionwithfilefilename/:filename/'
    40  withfilefiletitle = '/sectionwithfilefiletitle/:title/'
    41  withfileslug = '/sectionwithfileslug/:slug/'
    42  nofileslug = '/sectionnofileslug/:slug/'
    43  nofilefilename = '/sectionnofilefilename/:filename/'
    44  nofiletitle1 = '/sectionnofiletitle1/:title/'
    45  nofiletitle2 = '/sectionnofiletitle2/:sections[:last]/'
    46  [permalinks.term]
    47  tags = '/tagsslug/tag/:slug/'
    48  [permalinks.taxonomy]
    49  tags = '/tagsslug/:slug/'
    50  -- content/withpageslug/p1.md --
    51  ---
    52  slug: "p1slugvalue"
    53  tags: ["mytag"]
    54  ---
    55  -- content/withfilefilename/_index.md --
    56  -- content/withfileslug/_index.md --
    57  ---
    58  slug: "withfileslugvalue"
    59  ---
    60  -- content/nofileslug/p1.md --
    61  -- content/nofilefilename/p1.md --
    62  -- content/nofiletitle1/p1.md --
    63  -- content/nofiletitle2/asdf/p1.md --
    64  -- content/withallbutlastsection/subsection/p1.md --
    65  -- content/tags/_index.md --
    66  ---
    67  slug: "tagsslug"
    68  ---
    69  -- content/tags/mytag/_index.md --
    70  ---
    71  slug: "mytagslug"
    72  ---
    73  
    74  
    75  `
    76  
    77  	b := hugolib.NewIntegrationTestBuilder(
    78  		hugolib.IntegrationTestConfig{
    79  			T:           t,
    80  			TxtarString: files,
    81  			LogLevel:    logg.LevelWarn,
    82  		}).Build()
    83  
    84  	t.Log(b.LogString())
    85  	// No .File.TranslationBaseName on zero object etc. warnings.
    86  	b.Assert(b.H.Log.LoggCount(logg.LevelWarn), qt.Equals, 0)
    87  	b.AssertFileContent("public/pageslug/p1slugvalue/index.html", "Single|page|/pageslug/p1slugvalue/|")
    88  	b.AssertFileContent("public/sectionwithfilefilename/index.html", "List|section|/sectionwithfilefilename/|")
    89  	b.AssertFileContent("public/sectionwithfileslug/withfileslugvalue/index.html", "List|section|/sectionwithfileslug/withfileslugvalue/|")
    90  	b.AssertFileContent("public/sectionnofilefilename/index.html", "List|section|/sectionnofilefilename/|")
    91  	b.AssertFileContent("public/sectionnofileslug/nofileslugs/index.html", "List|section|/sectionnofileslug/nofileslugs/|")
    92  	b.AssertFileContent("public/sectionnofiletitle1/nofiletitle1s/index.html", "List|section|/sectionnofiletitle1/nofiletitle1s/|")
    93  	b.AssertFileContent("public/sectionnofiletitle2/index.html", "List|section|/sectionnofiletitle2/|")
    94  
    95  	b.AssertFileContent("public/tagsslug/tag/mytagslug/index.html", "List|term|/tagsslug/tag/mytagslug/|")
    96  	b.AssertFileContent("public/tagsslug/tagsslug/index.html", "List|taxonomy|/tagsslug/tagsslug/|")
    97  
    98  	permalinksConf := b.H.Configs.Base.Permalinks
    99  	b.Assert(permalinksConf, qt.DeepEquals, map[string]map[string]string{
   100  		"page":     {"withallbutlastsection": "/:sections[:last]/:slug/", "withpageslug": "/pageslug/:slug/"},
   101  		"section":  {"nofilefilename": "/sectionnofilefilename/:filename/", "nofileslug": "/sectionnofileslug/:slug/", "nofiletitle1": "/sectionnofiletitle1/:title/", "nofiletitle2": "/sectionnofiletitle2/:sections[:last]/", "withfilefilename": "/sectionwithfilefilename/:filename/", "withfilefiletitle": "/sectionwithfilefiletitle/:title/", "withfileslug": "/sectionwithfileslug/:slug/"},
   102  		"taxonomy": {"tags": "/tagsslug/:slug/"},
   103  		"term":     {"tags": "/tagsslug/tag/:slug/"},
   104  	})
   105  
   106  }
   107  
   108  func TestPermalinksOldSetup(t *testing.T) {
   109  	t.Parallel()
   110  
   111  	files := `
   112  -- layouts/_default/list.html --
   113  List|{{ .Kind }}|{{ .RelPermalink }}|
   114  -- layouts/_default/single.html --
   115  Single|{{ .Kind }}|{{ .RelPermalink }}|
   116  -- hugo.toml --
   117  [permalinks]
   118  withpageslug = '/pageslug/:slug/'
   119  -- content/withpageslug/p1.md --
   120  ---
   121  slug: "p1slugvalue"
   122  ---
   123  
   124  
   125  
   126  
   127  `
   128  
   129  	b := hugolib.NewIntegrationTestBuilder(
   130  		hugolib.IntegrationTestConfig{
   131  			T:           t,
   132  			TxtarString: files,
   133  			LogLevel:    logg.LevelWarn,
   134  		}).Build()
   135  
   136  	t.Log(b.LogString())
   137  	// No .File.TranslationBaseName on zero object etc. warnings.
   138  	b.Assert(b.H.Log.LoggCount(logg.LevelWarn), qt.Equals, 0)
   139  	b.AssertFileContent("public/pageslug/p1slugvalue/index.html", "Single|page|/pageslug/p1slugvalue/|")
   140  
   141  	permalinksConf := b.H.Configs.Base.Permalinks
   142  	b.Assert(permalinksConf, qt.DeepEquals, map[string]map[string]string{
   143  		"page":     {"withpageslug": "/pageslug/:slug/"},
   144  		"section":  {},
   145  		"taxonomy": {},
   146  		"term":     {"withpageslug": "/pageslug/:slug/"},
   147  	})
   148  
   149  }
   150  
   151  func TestPermalinksNestedSections(t *testing.T) {
   152  	t.Parallel()
   153  
   154  	files := `
   155  -- hugo.toml --
   156  [permalinks.page]
   157  books = '/libros/:sections[1:]/:filename'
   158  
   159  [permalinks.section]
   160  books = '/libros/:sections[1:]'
   161  -- content/books/_index.md --
   162  ---
   163  title: Books
   164  ---
   165  -- content/books/fiction/_index.md --
   166  ---
   167  title: Fiction
   168  ---
   169  -- content/books/fiction/2023/_index.md --
   170  ---
   171  title: 2023
   172  ---
   173  -- content/books/fiction/2023/book1/index.md --
   174  ---
   175  title: Book 1
   176  ---
   177  -- layouts/_default/single.html --
   178  Single.
   179  -- layouts/_default/list.html --
   180  List.
   181  `
   182  
   183  	b := hugolib.NewIntegrationTestBuilder(
   184  		hugolib.IntegrationTestConfig{
   185  			T:           t,
   186  			TxtarString: files,
   187  			LogLevel:    logg.LevelWarn,
   188  		}).Build()
   189  
   190  	t.Log(b.LogString())
   191  	// No .File.TranslationBaseName on zero object etc. warnings.
   192  	b.Assert(b.H.Log.LoggCount(logg.LevelWarn), qt.Equals, 0)
   193  
   194  	b.AssertFileContent("public/libros/index.html", "List.")
   195  	b.AssertFileContent("public/libros/fiction/index.html", "List.")
   196  	b.AssertFileContent("public/libros/fiction/2023/book1/index.html", "Single.")
   197  
   198  }