github.com/linchen2chris/hugo@v0.0.0-20230307053224-cec209389705/resources/page/integration_test.go (about)

     1  // Copyright 2021 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/gohugoio/hugo/hugolib"
    20  )
    21  
    22  func TestGroupByLocalizedDate(t *testing.T) {
    23  
    24  	files := `
    25  -- config.toml --
    26  defaultContentLanguage = 'en'
    27  defaultContentLanguageInSubdir = true
    28  [languages]
    29  [languages.en]
    30  title = 'My blog'
    31  weight = 1
    32  [languages.fr]
    33  title = 'Mon blogue'
    34  weight = 2
    35  [languages.nn]
    36  title = 'Bloggen min'
    37  weight = 3
    38  -- content/p1.md --
    39  ---
    40  title: "Post 1"
    41  date: "2020-01-01"
    42  ---
    43  -- content/p2.md --
    44  ---
    45  title: "Post 2"
    46  date: "2020-02-01"
    47  ---
    48  -- content/p1.fr.md --
    49  ---
    50  title: "Post 1"
    51  date: "2020-01-01"
    52  ---
    53  -- content/p2.fr.md --
    54  ---
    55  title: "Post 2"
    56  date: "2020-02-01"
    57  ---
    58  -- layouts/index.html --
    59  {{ range $k, $v := site.RegularPages.GroupByDate "January, 2006" }}{{ $k }}|{{ $v.Key }}|{{ $v.Pages }}{{ end }}
    60  
    61  	`
    62  
    63  	b := hugolib.NewIntegrationTestBuilder(
    64  		hugolib.IntegrationTestConfig{
    65  			T:           t,
    66  			TxtarString: files,
    67  			NeedsOsFS:   true,
    68  		}).Build()
    69  
    70  	b.AssertFileContent("public/en/index.html", "0|February, 2020|Pages(1)1|January, 2020|Pages(1)")
    71  	b.AssertFileContent("public/fr/index.html", "0|février, 2020|Pages(1)1|janvier, 2020|Pages(1)")
    72  }
    73  
    74  func TestPagesSortCollation(t *testing.T) {
    75  
    76  	files := `
    77  -- config.toml --
    78  defaultContentLanguage = 'en'
    79  defaultContentLanguageInSubdir = true
    80  [languages]
    81  [languages.en]
    82  title = 'My blog'
    83  weight = 1
    84  [languages.fr]
    85  title = 'Mon blogue'
    86  weight = 2
    87  [languages.nn]
    88  title = 'Bloggen min'
    89  weight = 3
    90  -- content/p1.md --
    91  ---
    92  title: "zulu"
    93  date: "2020-01-01"
    94  param1: "xylophone"
    95  tags: ["xylophone", "éclair", "zulu", "emma"]
    96  ---
    97  -- content/p2.md --
    98  ---
    99  title: "émotion"
   100  date: "2020-01-01"
   101  param1: "violin"
   102  ---
   103  -- content/p3.md --
   104  ---
   105  title: "alpha"
   106  date: "2020-01-01"
   107  param1: "éclair"
   108  ---
   109  -- layouts/index.html --
   110  ByTitle: {{ range site.RegularPages.ByTitle }}{{ .Title }}|{{ end }}
   111  ByLinkTitle: {{ range site.RegularPages.ByLinkTitle }}{{ .Title }}|{{ end }}
   112  ByParam: {{ range site.RegularPages.ByParam "param1" }}{{ .Params.param1 }}|{{ end }}
   113  Tags Alphabetical: {{  range site.Taxonomies.tags.Alphabetical }}{{ .Term }}|{{ end }}
   114  GroupBy: {{ range site.RegularPages.GroupBy "Title" }}{{ .Key }}|{{ end }}
   115  {{ with (site.GetPage "p1").Params.tags }}
   116  Sort: {{  sort . }}
   117  ByWeight: {{ range site.RegularPages.ByWeight }}{{ .Title }}|{{ end }}
   118  {{ end }}
   119  
   120  	`
   121  
   122  	b := hugolib.NewIntegrationTestBuilder(
   123  		hugolib.IntegrationTestConfig{
   124  			T:           t,
   125  			TxtarString: files,
   126  			NeedsOsFS:   true,
   127  		}).Build()
   128  
   129  	b.AssertFileContent("public/en/index.html", `
   130  ByTitle: alpha|émotion|zulu|
   131  ByLinkTitle: alpha|émotion|zulu|
   132  ByParam: éclair|violin|xylophone
   133  Tags Alphabetical: éclair|emma|xylophone|zulu|
   134  GroupBy: alpha|émotion|zulu|
   135  Sort: [éclair emma xylophone zulu]
   136  ByWeight: alpha|émotion|zulu|
   137  `)
   138  }
   139  
   140  // See #10377
   141  func TestPermalinkExpansionSectionsRepeated(t *testing.T) {
   142  	t.Parallel()
   143  
   144  	files := `
   145  -- hugo.toml --
   146  disableKinds = ["home", "taxonomy", "taxonomyTerm", "sitemap"]
   147  [outputs]
   148  home = ["HTML"]
   149  page = ["HTML"]
   150  section = ["HTML"]
   151  [outputFormats]
   152  [permalinks]
   153  posts = '/:sections[1]/:sections[last]/:slug'
   154  -- content/posts/_index.md --
   155  -- content/posts/a/_index.md --
   156  -- content/posts/a/b/_index.md --
   157  -- content/posts/a/b/c/_index.md --
   158  -- content/posts/a/b/c/d.md --
   159  ---
   160  title: "D"
   161  slug: "d"
   162  ---
   163  D
   164  -- layouts/_default/single.html --
   165  RelPermalink: {{ .RelPermalink }}
   166  
   167  `
   168  
   169  	b := hugolib.NewIntegrationTestBuilder(
   170  		hugolib.IntegrationTestConfig{
   171  			T:           t,
   172  			TxtarString: files,
   173  		}).Build()
   174  
   175  	b.AssertFileContent("public/a/c/d/index.html", "RelPermalink: /a/c/d/")
   176  
   177  }