github.com/neohugo/neohugo@v0.123.8/hugolib/params_test.go (about)

     1  // Copyright 2024 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  	"testing"
    18  
    19  	qt "github.com/frankban/quicktest"
    20  )
    21  
    22  func TestFrontMatterParamsInItsOwnSection(t *testing.T) {
    23  	t.Parallel()
    24  
    25  	files := `
    26  -- hugo.toml --
    27  baseURL = "https://example.org/"
    28  -- content/_index.md --
    29  +++
    30  title = "Home"
    31  [[cascade]]
    32  background = 'yosemite.jpg'
    33  [cascade.params]
    34  a = "home-a"
    35  b = "home-b"
    36  [cascade._target]
    37  kind = 'page'
    38  +++
    39  -- content/p1.md --
    40  ---
    41  title: "P1"
    42  summary: "frontmatter.summary"
    43  params:
    44     a: "p1-a"
    45     summary: "params.summary"
    46  ---	
    47  -- layouts/_default/single.html --
    48  Params: {{ range $k, $v := .Params }}{{ $k }}: {{ $v }}|{{ end }}$
    49  Summary: {{ .Summary }}|
    50  `
    51  
    52  	b := Test(t, files)
    53  
    54  	b.AssertFileContent("public/p1/index.html",
    55  		"Params: a: p1-a|b: home-b|background: yosemite.jpg|draft: false|iscjklanguage: false|summary: params.summary|title: P1|$",
    56  		"Summary: frontmatter.summary|",
    57  	)
    58  }
    59  
    60  func TestFrontMatterParamsKindPath(t *testing.T) {
    61  	t.Parallel()
    62  
    63  	files := `
    64  -- hugo.toml --
    65  baseURL = "https://example.org/"
    66  disableKinds = ["taxonomy", "term"]
    67  
    68  -- content/p1.md --
    69  ---
    70  title: "P1"
    71  date: 2019-08-07
    72  path: "/a/b/c"
    73  slug: "s1"
    74  ---
    75  -- content/mysection.md --
    76  ---
    77  title: "My Section"
    78  kind: "section"
    79  date: 2022-08-07
    80  path: "/a/b"
    81  ---
    82  -- layouts/index.html --
    83  RegularPages: {{ range site.RegularPages }}{{ .Path }}|{{ .RelPermalink }}|{{ .Title }}|{{ .Date.Format "2006-02-01" }}| Slug: {{ .Params.slug }}|{{ end }}$
    84  Sections: {{ range site.Sections }}{{ .Path }}|{{ .RelPermalink }}|{{ .Title }}|{{ .Date.Format "2006-02-01" }}| Slug: {{ .Params.slug }}|{{ end }}$
    85  {{ $ab := site.GetPage "a/b" }}
    86  a/b pages: {{ range $ab.RegularPages }}{{ .Path }}|{{ .RelPermalink }}|{{ end }}$
    87  `
    88  
    89  	b := Test(t, files)
    90  
    91  	b.AssertFileContent("public/index.html",
    92  		"RegularPages: /a/b/c|/a/b/s1/|P1|2019-07-08| Slug: s1|$",
    93  		"Sections: /a|/a/|As",
    94  		"a/b pages: /a/b/c|/a/b/s1/|$",
    95  	)
    96  }
    97  
    98  func TestFrontMatterParamsLang(t *testing.T) {
    99  	t.Parallel()
   100  
   101  	files := `
   102  -- hugo.toml --
   103  baseURL = "https://example.org/"
   104  disableKinds = ["taxonomy", "term"]
   105  defaultContentLanguage = "en"
   106  defaultContentLanguageInSubdir = true
   107  [languages]
   108  [languages.en]
   109  weight = 1
   110  [languages.nn]
   111  weight = 2
   112  -- content/p1.md --
   113  ---
   114  title: "P1 nn"
   115  lang: "nn"
   116  ---
   117  -- content/p2.md --
   118  ---
   119  title: "P2"
   120  ---
   121  -- layouts/index.html --
   122  RegularPages: {{ range site.RegularPages }}{{ .Path }}|{{ .RelPermalink }}|{{ .Title }}|{{ end }}$
   123  
   124  `
   125  
   126  	b := Test(t, files)
   127  
   128  	b.AssertFileContent("public/en/index.html",
   129  		"RegularPages: /p2|/en/p2/|P2|$",
   130  	)
   131  	b.AssertFileContent("public/nn/index.html",
   132  		"RegularPages: /p1|/nn/p1/|P1 nn|$",
   133  	)
   134  }
   135  
   136  func TestFrontMatterTitleOverrideWarn(t *testing.T) {
   137  	t.Parallel()
   138  
   139  	files := `
   140  -- hugo.toml --
   141  baseURL = "https://example.org/"
   142  disableKinds = ["taxonomy", "term"]
   143  -- content/p1.md --
   144  ---
   145  title: "My title"
   146  params:
   147    title: "My title from params"
   148  ---
   149  
   150  
   151  `
   152  
   153  	b := Test(t, files, TestOptWarn())
   154  
   155  	b.AssertLogContains("ARN  Hugo front matter key \"title\" is overridden in params section", "You can suppress this warning")
   156  }
   157  
   158  func TestFrontMatterParamsLangNoCascade(t *testing.T) {
   159  	t.Parallel()
   160  
   161  	files := `
   162  -- hugo.toml --
   163  baseURL = "https://example.org/"
   164  disableKinds = ["taxonomy", "term"]
   165  defaultContentLanguage = "en"
   166  defaultContentLanguageInSubdir = true
   167  [languages]
   168  [languages.en]
   169  weight = 1
   170  [languages.nn]
   171  weight = 2
   172  -- content/_index.md --
   173  +++
   174  [[cascade]]
   175  background = 'yosemite.jpg'
   176  lang = 'nn'
   177  +++
   178  
   179  `
   180  
   181  	b, err := TestE(t, files)
   182  	b.Assert(err, qt.IsNotNil)
   183  }
   184  
   185  // Issue 11970.
   186  func TestFrontMatterBuildIsHugoKeyword(t *testing.T) {
   187  	t.Parallel()
   188  
   189  	files := `
   190  -- hugo.toml --
   191  baseURL = "https://example.org/"
   192  -- content/p1.md --
   193  ---
   194  title: "P1"
   195  build: "foo"
   196  ---
   197  -- layouts/_default/single.html --
   198  Params: {{ range $k, $v := .Params }}{{ $k }}: {{ $v }}|{{ end }}$
   199  `
   200  	b, err := TestE(t, files)
   201  
   202  	b.Assert(err, qt.IsNotNil)
   203  	b.Assert(err.Error(), qt.Contains, "We renamed the _build keyword")
   204  }