Debian Codegraph @

hugolib/sitemap_test.go

raw
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 "strings"
19 "testing"
20
21 "github.com/gohugoio/hugo/config"
22 )
23
24 func TestSitemapBasic(t *testing.T) {
25 t.Parallel()
26
27 files := `
28 -- hugo.toml --
29 baseURL = "https://example.com"
30 disableKinds = ["term", "taxonomy"]
31 -- content/sect/doc1.md --
32 ---
33 title: doc1
34 ---
35 Doc1
36 -- content/sect/doc2.md --
37 ---
38 title: doc2
39 ---
40 Doc2
41 `
42
43 b := Test(t, files)
44
45 b.AssertFileContentStartsWith("public/sitemap.xml", `<?xml`) // Issue 14977
46 b.AssertFileContent("public/sitemap.xml",
47 "<loc>https://example.com/sect/doc1/</loc>", "doc2",
48 )
49 }
50
51 func TestSitemapMultilingual(t *testing.T) {
52 t.Parallel()
53
54 files := `
55 -- hugo.toml --
56 baseURL = "https://example.com"
57 disableKinds = ["term", "taxonomy"]
58 defaultContentLanguage = "en"
59 [languages]
60 [languages.en]
61 weight = 1
62 label = "English"
63 [languages.nn]
64 weight = 2
65 label = "Nynorsk"
66 -- content/sect/doc1.md --
67 ---
68 title: doc1
69 ---
70 Doc1
71 -- content/sect/doc2.md --
72 ---
73 title: doc2
74 ---
75 Doc2
76 -- content/sect/doc2.nn.md --
77 ---
78 title: doc2
79 ---
80 Doc2
81 `
82
83 b := Test(t, files)
84
85 b.AssertFileContent("public/sitemap.xml", "<loc>https://example.com/en/sitemap.xml</loc>", "<loc>https://example.com/nn/sitemap.xml</loc>")
86 b.AssertFileContent("public/en/sitemap.xml", " <loc>https://example.com/sect/doc1/</loc>", "doc2", `hreflang="en"`, `hreflang="nn"`)
87 b.AssertFileContent("public/nn/sitemap.xml", " <loc>https://example.com/nn/sect/doc2/</loc>", `hreflang="en"`, `hreflang="nn"`)
88 }
89
90 // https://github.com/gohugoio/hugo/issues/5910
91 func TestSitemapOutputFormats(t *testing.T) {
92 t.Parallel()
93
94 files := `
95 -- hugo.toml --
96 baseURL = "https://example.com"
97 disableKinds = ["term", "taxonomy"]
98 -- content/blog/html-amp.md --
99 ---
100 Title: AMP and HTML
101 outputs: [ "html", "amp" ]
102 ---
103
104 `
105
106 b := Test(t, files)
107
108 // Should link to the HTML version.
109 b.AssertFileContent("public/sitemap.xml", " <loc>https://example.com/blog/html-amp/</loc>")
110 }
111
112 func TestParseSitemap(t *testing.T) {
113 t.Parallel()
114 expected := config.SitemapConfig{ChangeFreq: "3", Disable: true, Filename: "doo.xml", Priority: 3.0}
115 input := map[string]any{
116 "changefreq": "3",
117 "disable": true,
118 "filename": "doo.xml",
119 "priority": 3.0,
120 "unknown": "ignore",
121 }
122 result, err := config.DecodeSitemap(config.SitemapConfig{}, input)
123 if err != nil {
124 t.Fatalf("Failed to parse sitemap: %s", err)
125 }
126
127 if !reflect.DeepEqual(expected, result) {
128 t.Errorf("Got \n%v expected \n%v", result, expected)
129 }
130 }
131
132 func TestSitemapShouldNotUseListXML(t *testing.T) {
133 t.Parallel()
134
135 files := `
136 -- hugo.toml --
137 baseURL = "https://example.com"
138 disableKinds = ["term", "taxonomy"]
139 [languages]
140 [languages.en]
141 weight = 1
142 label = "English"
143 [languages.nn]
144 weight = 2
145 -- layouts/list.xml --
146 Site: {{ .Site.Title }}|
147 -- layouts/home --
148 Home.
149
150 `
151
152 b := Test(t, files)
153
154 b.AssertFileContent("public/sitemap.xml", "https://example.com/en/sitemap.xml")
155 }
156
157 func TestSitemapAndContentBundleNamedSitemap(t *testing.T) {
158 t.Parallel()
159
160 files := `
161 -- hugo.toml --
162 disableKinds = ['home','rss','section','taxonomy','term']
163 -- layouts/single.html --
164 layouts/_default/single.html
165 -- layouts/sitemap/single.html --
166 layouts/sitemap/single.html
167 -- content/sitemap/index.md --
168 ---
169 title: My sitemap
170 type: sitemap
171 ---
172 `
173
174 b := Test(t, files)
175
176 b.AssertFileExists("public/sitemap.xml", true)
177 }
178
179 // Issue 12266
180 func TestSitemapIssue12266(t *testing.T) {
181 t.Parallel()
182
183 files := `
184 -- hugo.toml --
185 baseURL = 'https://example.org/'
186 disableKinds = ['rss','taxonomy','term']
187 defaultContentLanguage = 'en'
188 defaultContentLanguageInSubdir = true
189 [languages.de]
190 [languages.en]
191 `
192
193 // Test A: multilingual with defaultContentLanguageInSubdir = true
194 b := Test(t, files)
195
196 b.AssertFileContent("public/sitemap.xml",
197 "<loc>https://example.org/de/sitemap.xml</loc>",
198 "<loc>https://example.org/en/sitemap.xml</loc>",
199 )
200 b.AssertFileContent("public/de/sitemap.xml", "<loc>https://example.org/de/</loc>")
201 b.AssertFileContent("public/en/sitemap.xml", "<loc>https://example.org/en/</loc>")
202
203 // Test B: multilingual with defaultContentLanguageInSubdir = false
204 files = strings.ReplaceAll(files, "defaultContentLanguageInSubdir = true", "defaultContentLanguageInSubdir = false")
205
206 b = Test(t, files)
207
208 b.AssertFileContent("public/sitemap.xml",
209 "<loc>https://example.org/de/sitemap.xml</loc>",
210 "<loc>https://example.org/en/sitemap.xml</loc>",
211 )
212 b.AssertFileContent("public/de/sitemap.xml", "<loc>https://example.org/de/</loc>")
213 b.AssertFileContent("public/en/sitemap.xml", "<loc>https://example.org/</loc>")
214
215 // Test C: monolingual with defaultContentLanguageInSubdir = false
216 files = strings.ReplaceAll(files, "[languages.de]", "")
217 files = strings.ReplaceAll(files, "[languages.en]", "")
218
219 b = Test(t, files)
220
221 b.AssertFileExists("public/en/sitemap.xml", false)
222 b.AssertFileContent("public/sitemap.xml", "<loc>https://example.org/</loc>")
223
224 // Test D: monolingual with defaultContentLanguageInSubdir = true
225 files = strings.ReplaceAll(files, "defaultContentLanguageInSubdir = false", "defaultContentLanguageInSubdir = true")
226
227 b = Test(t, files)
228
229 b.AssertFileContent("public/sitemap.xml", "<loc>https://example.org/en/sitemap.xml</loc>")
230 b.AssertFileContent("public/en/sitemap.xml", "<loc>https://example.org/en/</loc>")
231 }