github.com/neohugo/neohugo@v0.123.8/hugolib/sitemap_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  	"reflect"
    18  	"testing"
    19  
    20  	"github.com/neohugo/neohugo/config"
    21  )
    22  
    23  func TestSitemapBasic(t *testing.T) {
    24  	t.Parallel()
    25  
    26  	files := `
    27  -- hugo.toml --
    28  baseURL = "https://example.com"
    29  disableKinds = ["term", "taxonomy"]
    30  -- content/sect/doc1.md --
    31  ---
    32  title: doc1
    33  ---
    34  Doc1
    35  -- content/sect/doc2.md --
    36  ---
    37  title: doc2
    38  ---
    39  Doc2
    40  `
    41  
    42  	b := Test(t, files)
    43  
    44  	b.AssertFileContent("public/sitemap.xml", " <loc>https://example.com/sect/doc1/</loc>", "doc2")
    45  }
    46  
    47  func TestSitemapMultilingual(t *testing.T) {
    48  	t.Parallel()
    49  
    50  	files := `
    51  -- hugo.toml --
    52  baseURL = "https://example.com"
    53  disableKinds = ["term", "taxonomy"]
    54  defaultContentLanguage = "en"
    55  [languages]
    56  [languages.en]
    57  weight = 1
    58  languageName = "English"
    59  [languages.nn]
    60  weight = 2
    61  languageName = "Nynorsk"
    62  -- content/sect/doc1.md --
    63  ---
    64  title: doc1
    65  ---
    66  Doc1
    67  -- content/sect/doc2.md --
    68  ---
    69  title: doc2
    70  ---
    71  Doc2
    72  -- content/sect/doc2.nn.md --
    73  ---
    74  title: doc2
    75  ---
    76  Doc2
    77  `
    78  
    79  	b := Test(t, files)
    80  
    81  	b.AssertFileContent("public/sitemap.xml", "<loc>https://example.com/en/sitemap.xml</loc>", "<loc>https://example.com/nn/sitemap.xml</loc>")
    82  	b.AssertFileContent("public/en/sitemap.xml", " <loc>https://example.com/sect/doc1/</loc>", "doc2")
    83  	b.AssertFileContent("public/nn/sitemap.xml", " <loc>https://example.com/nn/sect/doc2/</loc>")
    84  }
    85  
    86  // https://github.com/gohugoio/hugo/issues/5910
    87  func TestSitemapOutputFormats(t *testing.T) {
    88  	t.Parallel()
    89  
    90  	files := `
    91  -- hugo.toml --
    92  baseURL = "https://example.com"
    93  disableKinds = ["term", "taxonomy"]
    94  -- content/blog/html-amp.md --
    95  ---
    96  Title: AMP and HTML
    97  outputs: [ "html", "amp" ]
    98  ---
    99  
   100  `
   101  
   102  	b := Test(t, files)
   103  
   104  	// Should link to the HTML version.
   105  	b.AssertFileContent("public/sitemap.xml", " <loc>https://example.com/blog/html-amp/</loc>")
   106  }
   107  
   108  func TestParseSitemap(t *testing.T) {
   109  	t.Parallel()
   110  	expected := config.SitemapConfig{Priority: 3.0, Filename: "doo.xml", ChangeFreq: "3"}
   111  	input := map[string]any{
   112  		"changefreq": "3",
   113  		"priority":   3.0,
   114  		"filename":   "doo.xml",
   115  		"unknown":    "ignore",
   116  	}
   117  	result, err := config.DecodeSitemap(config.SitemapConfig{}, input)
   118  	if err != nil {
   119  		t.Fatalf("Failed to parse sitemap: %s", err)
   120  	}
   121  
   122  	if !reflect.DeepEqual(expected, result) {
   123  		t.Errorf("Got \n%v expected \n%v", result, expected)
   124  	}
   125  }
   126  
   127  func TestSitemapShouldNotUseListXML(t *testing.T) {
   128  	t.Parallel()
   129  
   130  	files := `		
   131  -- hugo.toml --
   132  baseURL = "https://example.com"
   133  disableKinds = ["term", "taxonomy"]
   134  [languages]
   135  [languages.en]
   136  weight = 1
   137  languageName = "English"
   138  [languages.nn]
   139  weight = 2
   140  -- layouts/_default/list.xml --
   141  Site: {{ .Site.Title }}|
   142  -- layouts/home --
   143  Home.
   144  
   145  `
   146  
   147  	b := Test(t, files)
   148  
   149  	b.AssertFileContent("public/sitemap.xml", "https://example.com/en/sitemap.xml")
   150  }
   151  
   152  func TestSitemapAndContentBundleNamedSitemap(t *testing.T) {
   153  	t.Parallel()
   154  
   155  	files := `
   156  -- hugo.toml --
   157  disableKinds = ['home','rss','section','taxonomy','term']
   158  -- layouts/_default/single.html --
   159  layouts/_default/single.html
   160  -- layouts/sitemap/single.html --
   161  layouts/sitemap/single.html
   162  -- content/sitemap/index.md --
   163  ---
   164  title: My sitemap
   165  type: sitemap
   166  ---
   167  `
   168  
   169  	b := Test(t, files)
   170  
   171  	b.AssertFileExists("public/sitemap.xml", true)
   172  }