Debian Codegraph @

hugolib/alias_test.go

raw
1 // Copyright 2025 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 "path/filepath"
18 "runtime"
19 "strings"
20 "testing"
21
22 "github.com/gohugoio/hugo/common/loggers"
23 "github.com/gohugoio/hugo/config"
24 "github.com/gohugoio/hugo/output"
25 )
26
27 func TestAlias(t *testing.T) {
28 t.Parallel()
29
30 tests := []struct {
31 fileSuffix string
32 urlPrefix string
33 urlSuffix string
34 settings map[string]any
35 }{
36 {"/index.html", "http://example.com", "/", map[string]any{"baseURL": "http://example.com"}},
37 {"/index.html", "http://example.com/some/path", "/", map[string]any{"baseURL": "http://example.com/some/path"}},
38 {"/index.html", "http://example.com", "/", map[string]any{"baseURL": "http://example.com", "canonifyURLs": true}},
39 {"/index.html", "../..", "/", map[string]any{"relativeURLs": true}},
40 {".html", "", ".html", map[string]any{"uglyURLs": true}},
41 }
42
43 for _, test := range tests {
44 files := `
45 -- hugo.toml --
46 disableKinds = ["rss", "sitemap", "taxonomy", "term"]
47 CONFIG
48 -- content/blog/page.md --
49 ---
50 title: Has Alias
51 aliases: ["/foo/bar/", "rel"]
52 ---
53 For some moments the old man did not reply. He stood with bowed head, buried in deep thought. But at last he spoke.
54 -- layouts/all.html --
55 Title: {{ .Title }}|Content: {{ .Content }}|
56 `
57 files = strings.Replace(files, "CONFIG", config.FromMapToTOMLString(test.settings), 1)
58
59 b := Test(t, files)
60
61 // the real page
62 b.AssertFileContent("public/blog/page"+test.fileSuffix, "For some moments the old man")
63
64 // the alias redirectors
65 b.AssertFileContent("public/foo/bar"+test.fileSuffix, "<meta http-equiv=\"refresh\" content=\"0; url="+test.urlPrefix+"/blog/page"+test.urlSuffix+"\">")
66 b.AssertFileContent("public/blog/rel"+test.fileSuffix, "<meta http-equiv=\"refresh\" content=\"0; url="+test.urlPrefix+"/blog/page"+test.urlSuffix+"\">")
67 }
68 }
69
70 func TestAliasMultipleOutputFormats(t *testing.T) {
71 t.Parallel()
72
73 files := `
74 -- hugo.toml --
75 baseURL = "http://example.com"
76 -- layouts/single.html --
77 {{ .Content }}
78 -- layouts/single.amp.html --
79 {{ .Content }}
80 -- layouts/single.json --
81 {{ .Content }}
82 -- content/blog/page.md --
83 ---
84 title: Has Alias for HTML and AMP
85 aliases: ["/foo/bar/"]
86 outputs: ["html", "amp", "json"]
87 ---
88 For some moments the old man did not reply. He stood with bowed head, buried in deep thought. But at last he spoke.
89 `
90
91 b := Test(t, files)
92
93 // the real pages
94 b.AssertFileContent("public/blog/page/index.html", "For some moments the old man")
95 b.AssertFileContent("public/amp/blog/page/index.html", "For some moments the old man")
96 b.AssertFileContent("public/blog/page/index.json", "For some moments the old man")
97
98 // the alias redirectors
99 b.AssertFileContent("public/foo/bar/index.html", "<meta http-equiv=\"refresh\" content=\"0; ")
100 b.AssertFileContent("public/amp/foo/bar/index.html", "<meta http-equiv=\"refresh\" content=\"0; ")
101 b.AssertFileExists("public/foo/bar/index.json", false)
102 }
103
104 func TestAliasTemplate(t *testing.T) {
105 t.Parallel()
106
107 files := `
108 -- hugo.toml --
109 baseURL = "http://example.com"
110 -- layouts/single.html --
111 Single.
112 -- layouts/home.html --
113 Home.
114 -- layouts/alias.html --
115 ALIASTEMPLATE
116 -- content/page.md --
117 ---
118 title: "Page"
119 aliases: ["/foo/bar/"]
120 ---
121 `
122
123 b := Test(t, files)
124
125 // the real page
126 b.AssertFileContent("public/page/index.html", "Single.")
127 // the alias redirector
128 b.AssertFileContent("public/foo/bar/index.html", "ALIASTEMPLATE")
129 }
130
131 func TestTargetPathHTMLRedirectAlias(t *testing.T) {
132 h := newAliasHandler(nil, loggers.NewDefault(), false)
133
134 errIsNilForThisOS := runtime.GOOS != "windows"
135
136 tests := []struct {
137 value string
138 expected string
139 errIsNil bool
140 }{
141 {"", "", false},
142 {"s", filepath.FromSlash("s/index.html"), true},
143 {"/", "", false},
144 {"alias 1", filepath.FromSlash("alias 1/index.html"), true},
145 {"alias 2/", filepath.FromSlash("alias 2/index.html"), true},
146 {"alias 3.html", "alias 3.html", true},
147 {"alias4.html", "alias4.html", true},
148 {"/alias 5.html", "alias 5.html", true},
149 {"/трям.html", "трям.html", true},
150 {"../../../../tmp/passwd", "", false},
151 {"/foo/../../../../tmp/passwd", filepath.FromSlash("tmp/passwd/index.html"), true},
152 {"foo/../../../../tmp/passwd", "", false},
153 {"C:\\Windows", filepath.FromSlash("C:\\Windows/index.html"), errIsNilForThisOS},
154 {"/trailing-space /", filepath.FromSlash("trailing-space /index.html"), errIsNilForThisOS},
155 {"/trailing-period./", filepath.FromSlash("trailing-period./index.html"), errIsNilForThisOS},
156 {"/tab\tseparated/", filepath.FromSlash("tab\tseparated/index.html"), errIsNilForThisOS},
157 {"/chrome/?p=help&ctx=keyboard#topic=3227046", filepath.FromSlash("chrome/?p=help&ctx=keyboard#topic=3227046/index.html"), errIsNilForThisOS},
158 {"/LPT1/Printer/", filepath.FromSlash("LPT1/Printer/index.html"), errIsNilForThisOS},
159 }
160
161 for _, test := range tests {
162 path, err := h.targetPathAlias(test.value, output.HTMLFormat)
163 if (err == nil) != test.errIsNil {
164 t.Errorf("Expected err == nil => %t, got: %t. err: %s", test.errIsNil, err == nil, err)
165 continue
166 }
167 if err == nil && path != test.expected {
168 t.Errorf("Expected: %q, got: %q", test.expected, path)
169 }
170 }
171 }
172
173 func TestAliasNIssue14053(t *testing.T) {
174 t.Parallel()
175
176 files := `
177 -- hugo.toml --
178 baseURL = "http://example.com"
179 -- layouts/all.html --
180 All.
181 -- content/page.md --
182 ---
183 title: "Page"
184 aliases:
185 - n
186 - y
187 - no
188 - yes
189 ---
190 `
191 b := Test(t, files)
192
193 b.AssertPublishDir("n/index.html", "yes/index.html", "no/index.html", "yes/index.html")
194 }
195
196 // Issue 14381
197 func TestIssue14381(t *testing.T) {
198 t.Parallel()
199
200 files := `
201 -- hugo.toml --
202 baseURL = 'https://example.org'
203 disableKinds = ['home', 'rss', 'sitemap', 'taxonomy', 'term']
204
205 [outputFormats.print]
206 isHTML = IS_HTML
207 permalinkable = true
208 mediaType = 'text/html'
209 path = 'print'
210
211 [outputs]
212 page = ['html', 'print']
213 section = ['html', 'print']
214 -- content/foo/s1/_index.md --
215 ---
216 title: s1
217 aliases: [s1-alias]
218 ---
219 -- content/foo/s1/p1.md --
220 ---
221 title: p1
222 aliases: [p1-alias]
223 ---
224 -- content/foo/s2/_index.md --
225 ---
226 title: s2
227 aliases: [/s2-alias]
228 ---
229 -- content/foo/s2/p2.md --
230 ---
231 title: p2
232 aliases: [/p2-alias]
233 ---
234 -- layouts/all.html --
235 {{ .Title }}
236 `
237 // ------------------------------------------------------------------------
238 // Test 1: Create aliases for the html output format only
239 // ------------------------------------------------------------------------
240
241 // public/
242 // ├── foo/
243 // │ ├── s1/
244 // │ │ ├── p1/
245 // │ │ │ └── index.html
246 // │ │ ├── p1-alias/
247 // │ │ │ └── index.html
248 // │ │ └── index.html
249 // │ ├── s1-alias/
250 // │ │ └── index.html
251 // │ ├── s2/
252 // │ │ ├── p2/
253 // │ │ │ └── index.html
254 // │ │ └── index.html
255 // │ └── index.html
256 // ├── p2-alias/
257 // │ └── index.html
258 // ├── print/
259 // │ └── foo/
260 // │ ├── s1/
261 // │ │ ├── p1/
262 // │ │ │ └── index.html
263 // │ │ └── index.html
264 // │ ├── s2/
265 // │ │ ├── p2/
266 // │ │ │ └── index.html
267 // │ │ └── index.html
268 // │ └── index.html
269 // └── s2-alias/
270 // └── index.html
271
272 f := strings.ReplaceAll(files, "IS_HTML", "false")
273 b := Test(t, f)
274
275 // output format: html
276 b.AssertFileContent("public/foo/s1-alias/index.html",
277 `<title>https://example.org/foo/s1/</title>`,
278 `<link rel="canonical" href="https://example.org/foo/s1/">`,
279 `<meta http-equiv="refresh" content="0; url=https://example.org/foo/s1/">`,
280 )
281 b.AssertFileContent("public/foo/s1/p1-alias/index.html",
282 `<title>https://example.org/foo/s1/p1/</title>`,
283 `<link rel="canonical" href="https://example.org/foo/s1/p1/">`,
284 `<meta http-equiv="refresh" content="0; url=https://example.org/foo/s1/p1/">`,
285 )
286 b.AssertFileContent("public/s2-alias/index.html",
287 `<title>https://example.org/foo/s2/</title>`,
288 `<link rel="canonical" href="https://example.org/foo/s2/">`,
289 `<meta http-equiv="refresh" content="0; url=https://example.org/foo/s2/">`,
290 )
291 b.AssertFileContent("public/p2-alias/index.html",
292 `<title>https://example.org/foo/s2/p2/</title>`,
293 `<link rel="canonical" href="https://example.org/foo/s2/p2/">`,
294 `<meta http-equiv="refresh" content="0; url=https://example.org/foo/s2/p2/">`,
295 )
296
297 // output format: print
298 b.AssertFileExists("public/print/foo/s1-alias/index.html", false)
299 b.AssertFileExists("public/print/foo/s1/p1-alias/index.html", false)
300 b.AssertFileExists("public/print/s2-alias/index.html", false)
301 b.AssertFileExists("public/print/p2-alias/index.html", false)
302
303 // ------------------------------------------------------------------------
304 // Test 2: Create aliases for the html and print output formats
305 // ------------------------------------------------------------------------
306
307 // public/
308 // ├── foo/
309 // │ ├── s1/
310 // │ │ ├── p1/
311 // │ │ │ └── index.html
312 // │ │ ├── p1-alias/
313 // │ │ │ └── index.html
314 // │ │ └── index.html
315 // │ ├── s1-alias/
316 // │ │ └── index.html
317 // │ ├── s2/
318 // │ │ ├── p2/
319 // │ │ │ └── index.html
320 // │ │ └── index.html
321 // │ └── index.html
322 // ├── p2-alias/
323 // │ └── index.html
324 // ├── print/
325 // │ ├── foo/
326 // │ │ ├── s1/
327 // │ │ │ ├── p1/
328 // │ │ │ │ └── index.html
329 // │ │ │ ├── p1-alias/
330 // │ │ │ │ └── index.html
331 // │ │ │ └── index.html
332 // │ │ ├── s1-alias/
333 // │ │ │ └── index.html
334 // │ │ ├── s2/
335 // │ │ │ ├── p2/
336 // │ │ │ │ └── index.html
337 // │ │ │ └── index.html
338 // │ │ └── index.html
339 // │ ├── p2-alias/
340 // │ │ └── index.html
341 // │ └── s2-alias/
342 // │ └── index.html
343 // └── s2-alias/
344 // └── index.html
345
346 f = strings.ReplaceAll(files, "IS_HTML", "true")
347 b = Test(t, f)
348
349 // output format: html
350 b.AssertFileContent("public/foo/s1-alias/index.html",
351 `<title>https://example.org/foo/s1/</title>`,
352 `<link rel="canonical" href="https://example.org/foo/s1/">`,
353 `<meta http-equiv="refresh" content="0; url=https://example.org/foo/s1/">`,
354 )
355 b.AssertFileContent("public/foo/s1/p1-alias/index.html",
356 `<title>https://example.org/foo/s1/p1/</title>`,
357 `<link rel="canonical" href="https://example.org/foo/s1/p1/">`,
358 `<meta http-equiv="refresh" content="0; url=https://example.org/foo/s1/p1/">`,
359 )
360 b.AssertFileContent("public/s2-alias/index.html",
361 `<title>https://example.org/foo/s2/</title>`,
362 `<link rel="canonical" href="https://example.org/foo/s2/">`,
363 `<meta http-equiv="refresh" content="0; url=https://example.org/foo/s2/">`,
364 )
365 b.AssertFileContent("public/p2-alias/index.html",
366 `<title>https://example.org/foo/s2/p2/</title>`,
367 `<link rel="canonical" href="https://example.org/foo/s2/p2/">`,
368 `<meta http-equiv="refresh" content="0; url=https://example.org/foo/s2/p2/">`,
369 )
370
371 // output format: print
372 b.AssertFileContent("public/print/foo/s1-alias/index.html",
373 `<title>https://example.org/print/foo/s1/</title>`,
374 `<link rel="canonical" href="https://example.org/foo/s1/">`,
375 `<meta http-equiv="refresh" content="0; url=https://example.org/print/foo/s1/">`,
376 )
377 b.AssertFileContent("public/print/foo/s1/p1-alias/index.html",
378 `<title>https://example.org/print/foo/s1/p1/</title>`,
379 `<link rel="canonical" href="https://example.org/foo/s1/p1/">`,
380 `<meta http-equiv="refresh" content="0; url=https://example.org/print/foo/s1/p1/">`,
381 )
382 b.AssertFileContent("public/print/s2-alias/index.html",
383 `<title>https://example.org/print/foo/s2/</title>`,
384 `<link rel="canonical" href="https://example.org/foo/s2/">`,
385 `<meta http-equiv="refresh" content="0; url=https://example.org/print/foo/s2/">`,
386 )
387 b.AssertFileContent("public/print/p2-alias/index.html",
388 `<title>https://example.org/print/foo/s2/p2/</title>`,
389 `<link rel="canonical" href="https://example.org/foo/s2/p2/">`,
390 `<meta http-equiv="refresh" content="0; url=https://example.org/print/foo/s2/p2/">`,
391 )
392 }
393
394 // Issue 14388
395 func TestIssue14388(t *testing.T) {
396 t.Parallel()
397
398 files := `
399 -- hugo.toml --
400 baseURL = 'https://example.org'
401 disableKinds = ['home', 'rss', 'sitemap', 'taxonomy', 'term']
402
403 defaultContentLanguage = 'en'
404
405 defaultContentLanguageInSubdir = true
406 defaultContentRoleInSubdir = true
407 defaultContentVersionInSubdir = true
408
409 [languages.en]
410 weight = 1
411 # baseURL = 'https://en.example.org/'
412
413 [languages.de]
414 weight = 2
415 # baseURL = 'https://de.example.org/'
416
417 [outputFormats.print]
418 isHTML = true
419 mediaType = 'text/html'
420 path = 'print'
421 permalinkable = true
422
423 [outputs]
424 page = ['html', 'print']
425 section = ['html', 'print']
426 -- content/foo/s1/_index.de.md --
427 ---
428 title: s1 de
429 aliases: [s1-alias]
430 ---
431 -- content/foo/s1/_index.en.md --
432 ---
433 title: s1 en
434 aliases: [s1-alias]
435 ---
436 -- content/foo/s1/p1.de.md --
437 ---
438 title: p1 de
439 aliases: [p1-alias]
440 ---
441 -- content/foo/s1/p1.en.md --
442 ---
443 title: p1 en
444 aliases: [p1-alias]
445 ---
446 -- content/foo/s2/_index.de.md --
447 ---
448 title: s2 de
449 aliases: [/s2-alias]
450 ---
451 -- content/foo/s2/_index.en.md --
452 ---
453 title: s2 en
454 aliases: [/s2-alias]
455 ---
456 -- content/foo/s2/p2.de.md --
457 ---
458 title: p2 de
459 aliases: [/p2-alias]
460 ---
461 -- content/foo/s2/p2.en.md --
462 ---
463 title: p2 en
464 aliases: [/p2-alias]
465 ---
466 -- layouts/all.html --
467 {{ .Title }}
468 `
469 // ------------------------------------------------------------------------
470 // Test 1: Multilingual single-host
471 // ------------------------------------------------------------------------
472
473 b := Test(t, files)
474
475 // language: de, output format: html
476 b.AssertFileContent("public/guest/v1.0.0/de/foo/s1-alias/index.html",
477 `<title>https://example.org/guest/v1.0.0/de/foo/s1/</title>`,
478 `<link rel="canonical" href="https://example.org/guest/v1.0.0/de/foo/s1/">`,
479 `<meta http-equiv="refresh" content="0; url=https://example.org/guest/v1.0.0/de/foo/s1/">`,
480 )
481 b.AssertFileContent("public/guest/v1.0.0/de/foo/s1/p1-alias/index.html",
482 `<title>https://example.org/guest/v1.0.0/de/foo/s1/p1/</title>`,
483 `<link rel="canonical" href="https://example.org/guest/v1.0.0/de/foo/s1/p1/">`,
484 `<meta http-equiv="refresh" content="0; url=https://example.org/guest/v1.0.0/de/foo/s1/p1/">`,
485 )
486 b.AssertFileContent("public/guest/v1.0.0/de/s2-alias/index.html",
487 `<title>https://example.org/guest/v1.0.0/de/foo/s2/</title>`,
488 `<link rel="canonical" href="https://example.org/guest/v1.0.0/de/foo/s2/">`,
489 `<meta http-equiv="refresh" content="0; url=https://example.org/guest/v1.0.0/de/foo/s2/">`,
490 )
491 b.AssertFileContent("public/guest/v1.0.0/de/p2-alias/index.html",
492 `<title>https://example.org/guest/v1.0.0/de/foo/s2/p2/</title>`,
493 `<link rel="canonical" href="https://example.org/guest/v1.0.0/de/foo/s2/p2/">`,
494 `<meta http-equiv="refresh" content="0; url=https://example.org/guest/v1.0.0/de/foo/s2/p2/">`,
495 )
496
497 // language: de, output format: print
498 b.AssertFileContent("public/guest/v1.0.0/de/print/foo/s1-alias/index.html",
499 `<title>https://example.org/guest/v1.0.0/de/print/foo/s1/</title>`,
500 `<link rel="canonical" href="https://example.org/guest/v1.0.0/de/foo/s1/">`,
501 `<meta http-equiv="refresh" content="0; url=https://example.org/guest/v1.0.0/de/print/foo/s1/">`,
502 )
503 b.AssertFileContent("public/guest/v1.0.0/de/print/foo/s1/p1-alias/index.html",
504 `<title>https://example.org/guest/v1.0.0/de/print/foo/s1/p1/</title>`,
505 `<link rel="canonical" href="https://example.org/guest/v1.0.0/de/foo/s1/p1/">`,
506 `<meta http-equiv="refresh" content="0; url=https://example.org/guest/v1.0.0/de/print/foo/s1/p1/">`,
507 )
508 b.AssertFileContent("public/guest/v1.0.0/de/print/s2-alias/index.html",
509 `<title>https://example.org/guest/v1.0.0/de/print/foo/s2/</title>`,
510 `<link rel="canonical" href="https://example.org/guest/v1.0.0/de/foo/s2/">`,
511 `<meta http-equiv="refresh" content="0; url=https://example.org/guest/v1.0.0/de/print/foo/s2/">`,
512 )
513 b.AssertFileContent("public/guest/v1.0.0/de/print/p2-alias/index.html",
514 `<title>https://example.org/guest/v1.0.0/de/print/foo/s2/p2/</title>`,
515 `<link rel="canonical" href="https://example.org/guest/v1.0.0/de/foo/s2/p2/">`,
516 `<meta http-equiv="refresh" content="0; url=https://example.org/guest/v1.0.0/de/print/foo/s2/p2/">`,
517 )
518
519 // language: en, output format: html
520 b.AssertFileContent("public/guest/v1.0.0/en/foo/s1-alias/index.html",
521 `<title>https://example.org/guest/v1.0.0/en/foo/s1/</title>`,
522 `<link rel="canonical" href="https://example.org/guest/v1.0.0/en/foo/s1/">`,
523 `<meta http-equiv="refresh" content="0; url=https://example.org/guest/v1.0.0/en/foo/s1/">`,
524 )
525 b.AssertFileContent("public/guest/v1.0.0/en/foo/s1/p1-alias/index.html",
526 `<title>https://example.org/guest/v1.0.0/en/foo/s1/p1/</title>`,
527 `<link rel="canonical" href="https://example.org/guest/v1.0.0/en/foo/s1/p1/">`,
528 `<meta http-equiv="refresh" content="0; url=https://example.org/guest/v1.0.0/en/foo/s1/p1/">`,
529 )
530 b.AssertFileContent("public/guest/v1.0.0/en/s2-alias/index.html",
531 `<title>https://example.org/guest/v1.0.0/en/foo/s2/</title>`,
532 `<link rel="canonical" href="https://example.org/guest/v1.0.0/en/foo/s2/">`,
533 `<meta http-equiv="refresh" content="0; url=https://example.org/guest/v1.0.0/en/foo/s2/">`,
534 )
535 b.AssertFileContent("public/guest/v1.0.0/en/p2-alias/index.html",
536 `<title>https://example.org/guest/v1.0.0/en/foo/s2/p2/</title>`,
537 `<link rel="canonical" href="https://example.org/guest/v1.0.0/en/foo/s2/p2/">`,
538 `<meta http-equiv="refresh" content="0; url=https://example.org/guest/v1.0.0/en/foo/s2/p2/">`,
539 )
540
541 // language: en, output format: print
542 b.AssertFileContent("public/guest/v1.0.0/en/print/foo/s1-alias/index.html",
543 `<title>https://example.org/guest/v1.0.0/en/print/foo/s1/</title>`,
544 `<link rel="canonical" href="https://example.org/guest/v1.0.0/en/foo/s1/">`,
545 `<meta http-equiv="refresh" content="0; url=https://example.org/guest/v1.0.0/en/print/foo/s1/">`,
546 )
547 b.AssertFileContent("public/guest/v1.0.0/en/print/foo/s1/p1-alias/index.html",
548 `<title>https://example.org/guest/v1.0.0/en/print/foo/s1/p1/</title>`,
549 `<link rel="canonical" href="https://example.org/guest/v1.0.0/en/foo/s1/p1/">`,
550 `<meta http-equiv="refresh" content="0; url=https://example.org/guest/v1.0.0/en/print/foo/s1/p1/">`,
551 )
552 b.AssertFileContent("public/guest/v1.0.0/en/print/s2-alias/index.html",
553 `<title>https://example.org/guest/v1.0.0/en/print/foo/s2/</title>`,
554 `<link rel="canonical" href="https://example.org/guest/v1.0.0/en/foo/s2/">`,
555 `<meta http-equiv="refresh" content="0; url=https://example.org/guest/v1.0.0/en/print/foo/s2/">`,
556 )
557 b.AssertFileContent("public/guest/v1.0.0/en/print/p2-alias/index.html",
558 `<title>https://example.org/guest/v1.0.0/en/print/foo/s2/p2/</title>`,
559 `<link rel="canonical" href="https://example.org/guest/v1.0.0/en/foo/s2/p2/">`,
560 `<meta http-equiv="refresh" content="0; url=https://example.org/guest/v1.0.0/en/print/foo/s2/p2/">`,
561 )
562
563 // ------------------------------------------------------------------------
564 // Test 2: Multilingual multihost
565 // ------------------------------------------------------------------------
566
567 files = strings.ReplaceAll(files, "# baseURL", "baseURL")
568 b = Test(t, files)
569
570 // language: de, output format: html
571 b.AssertFileContent("public/de/guest/v1.0.0/foo/s1-alias/index.html",
572 `<title>https://de.example.org/guest/v1.0.0/foo/s1/</title>`,
573 `<link rel="canonical" href="https://de.example.org/guest/v1.0.0/foo/s1/">`,
574 `<meta http-equiv="refresh" content="0; url=https://de.example.org/guest/v1.0.0/foo/s1/">`,
575 )
576 b.AssertFileContent("public/de/guest/v1.0.0/foo/s1/p1-alias/index.html",
577 `<title>https://de.example.org/guest/v1.0.0/foo/s1/p1/</title>`,
578 `<link rel="canonical" href="https://de.example.org/guest/v1.0.0/foo/s1/p1/">`,
579 `<meta http-equiv="refresh" content="0; url=https://de.example.org/guest/v1.0.0/foo/s1/p1/">`,
580 )
581 b.AssertFileContent("public/de/guest/v1.0.0/s2-alias/index.html",
582 `<title>https://de.example.org/guest/v1.0.0/foo/s2/</title>`,
583 `<link rel="canonical" href="https://de.example.org/guest/v1.0.0/foo/s2/">`,
584 `<meta http-equiv="refresh" content="0; url=https://de.example.org/guest/v1.0.0/foo/s2/">`,
585 )
586 b.AssertFileContent("public/de/guest/v1.0.0/p2-alias/index.html",
587 `<title>https://de.example.org/guest/v1.0.0/foo/s2/p2/</title>`,
588 `<link rel="canonical" href="https://de.example.org/guest/v1.0.0/foo/s2/p2/">`,
589 `<meta http-equiv="refresh" content="0; url=https://de.example.org/guest/v1.0.0/foo/s2/p2/">`,
590 )
591
592 // language: de, output format: print
593 b.AssertFileContent("public/de/guest/v1.0.0/print/foo/s1-alias/index.html",
594 `<title>https://de.example.org/guest/v1.0.0/print/foo/s1/</title>`,
595 `<link rel="canonical" href="https://de.example.org/guest/v1.0.0/foo/s1/">`,
596 `<meta http-equiv="refresh" content="0; url=https://de.example.org/guest/v1.0.0/print/foo/s1/">`,
597 )
598 b.AssertFileContent("public/de/guest/v1.0.0/print/foo/s1/p1-alias/index.html",
599 `<title>https://de.example.org/guest/v1.0.0/print/foo/s1/p1/</title>`,
600 `<link rel="canonical" href="https://de.example.org/guest/v1.0.0/foo/s1/p1/">`,
601 `<meta http-equiv="refresh" content="0; url=https://de.example.org/guest/v1.0.0/print/foo/s1/p1/">`,
602 )
603 b.AssertFileContent("public/de/guest/v1.0.0/print/s2-alias/index.html",
604 `<title>https://de.example.org/guest/v1.0.0/print/foo/s2/</title>`,
605 `<link rel="canonical" href="https://de.example.org/guest/v1.0.0/foo/s2/">`,
606 `<meta http-equiv="refresh" content="0; url=https://de.example.org/guest/v1.0.0/print/foo/s2/">`,
607 )
608 b.AssertFileContent("public/de/guest/v1.0.0/print/p2-alias/index.html",
609 `<title>https://de.example.org/guest/v1.0.0/print/foo/s2/p2/</title>`,
610 `<link rel="canonical" href="https://de.example.org/guest/v1.0.0/foo/s2/p2/">`,
611 `<meta http-equiv="refresh" content="0; url=https://de.example.org/guest/v1.0.0/print/foo/s2/p2/">`,
612 )
613
614 // language: en, output format: html
615 b.AssertFileContent("public/en/guest/v1.0.0/foo/s1-alias/index.html",
616 `<title>https://en.example.org/guest/v1.0.0/foo/s1/</title>`,
617 `<link rel="canonical" href="https://en.example.org/guest/v1.0.0/foo/s1/">`,
618 `<meta http-equiv="refresh" content="0; url=https://en.example.org/guest/v1.0.0/foo/s1/">`,
619 )
620 b.AssertFileContent("public/en/guest/v1.0.0/foo/s1/p1-alias/index.html",
621 `<title>https://en.example.org/guest/v1.0.0/foo/s1/p1/</title>`,
622 `<link rel="canonical" href="https://en.example.org/guest/v1.0.0/foo/s1/p1/">`,
623 `<meta http-equiv="refresh" content="0; url=https://en.example.org/guest/v1.0.0/foo/s1/p1/">`,
624 )
625 b.AssertFileContent("public/en/guest/v1.0.0/s2-alias/index.html",
626 `<title>https://en.example.org/guest/v1.0.0/foo/s2/</title>`,
627 `<link rel="canonical" href="https://en.example.org/guest/v1.0.0/foo/s2/">`,
628 `<meta http-equiv="refresh" content="0; url=https://en.example.org/guest/v1.0.0/foo/s2/">`,
629 )
630 b.AssertFileContent("public/en/guest/v1.0.0/p2-alias/index.html",
631 `<title>https://en.example.org/guest/v1.0.0/foo/s2/p2/</title>`,
632 `<link rel="canonical" href="https://en.example.org/guest/v1.0.0/foo/s2/p2/">`,
633 `<meta http-equiv="refresh" content="0; url=https://en.example.org/guest/v1.0.0/foo/s2/p2/">`,
634 )
635
636 // language: en, output format: print
637 b.AssertFileContent("public/en/guest/v1.0.0/print/foo/s1-alias/index.html",
638 `<title>https://en.example.org/guest/v1.0.0/print/foo/s1/</title>`,
639 `<link rel="canonical" href="https://en.example.org/guest/v1.0.0/foo/s1/">`,
640 `<meta http-equiv="refresh" content="0; url=https://en.example.org/guest/v1.0.0/print/foo/s1/">`,
641 )
642 b.AssertFileContent("public/en/guest/v1.0.0/print/foo/s1/p1-alias/index.html",
643 `<title>https://en.example.org/guest/v1.0.0/print/foo/s1/p1/</title>`,
644 `<link rel="canonical" href="https://en.example.org/guest/v1.0.0/foo/s1/p1/">`,
645 `<meta http-equiv="refresh" content="0; url=https://en.example.org/guest/v1.0.0/print/foo/s1/p1/">`,
646 )
647 b.AssertFileContent("public/en/guest/v1.0.0/print/s2-alias/index.html",
648 `<title>https://en.example.org/guest/v1.0.0/print/foo/s2/</title>`,
649 `<link rel="canonical" href="https://en.example.org/guest/v1.0.0/foo/s2/">`,
650 `<meta http-equiv="refresh" content="0; url=https://en.example.org/guest/v1.0.0/print/foo/s2/">`,
651 )
652 b.AssertFileContent("public/en/guest/v1.0.0/print/p2-alias/index.html",
653 `<title>https://en.example.org/guest/v1.0.0/print/foo/s2/p2/</title>`,
654 `<link rel="canonical" href="https://en.example.org/guest/v1.0.0/foo/s2/p2/">`,
655 `<meta http-equiv="refresh" content="0; url=https://en.example.org/guest/v1.0.0/print/foo/s2/p2/">`,
656 )
657 }
658
659 // Issue #14402.
660 func TestComprehensiveAliasesRedirectsFile(t *testing.T) {
661 t.Parallel()
662
663 files := `
664 -- hugo.toml --
665 disableAliases = true
666 defaultContentLanguageInSubdir = true
667 defaultContentRoleInSubdir = true
668 defaultContentVersionInSubdir = true
669 baseURL = "https://example.org/"
670 -- content/foo/p1.md --
671 ---
672 aliases: ["/foo/p2/", "../p3/"]
673 ---
674 -- content/foo/p2.md --
675 -- content/p3.md --
676 -- layouts/home.html --
677 Home.
678 {{ range $p := site.RegularPages }}{{ range .Aliases }}{{ . | printf "%-35s" }}=>{{ $p.RelPermalink -}}|{{ end -}}{{ end }}
679 `
680 b := Test(t, files)
681
682 b.AssertFileContent("public/guest/v1.0.0/en/index.html",
683 "/guest/v1.0.0/en/foo/p2 =>/guest/v1.0.0/en/foo/p1/|",
684 "/guest/v1.0.0/en/p3 =>/guest/v1.0.0/en/foo/p1/|",
685 )
686 }
687
688 // Issue 14482
689 func TestOutputFormatIsHTMLWithMultilangAliases(t *testing.T) {
690 t.Parallel()
691
692 files := `
693 -- hugo.toml --
694 defaultContentLanguageInSubdir = true
695 disableKinds = ['page', 'section', 'rss', 'sitemap', 'taxonomy', 'term']
696
697 [languages.en]
698 [languages.de]
699
700 [outputFormats.foo]
701 baseName = 'foo'
702 isHTML = false
703 mediaType = 'text/html'
704
705 [outputs]
706 home = ['html', 'foo']
707 -- content/index.de.md --
708 ---
709 title: home de
710 ---
711 -- content/index.en.md --
712 ---
713 title: home en
714 ---
715 -- layouts/home.html --
716 Output format: html|Title: {{ .Title }}
717 -- layouts/home.foo.html --
718 Output format: foo|Title: {{ .Title }}
719 `
720
721 b := Test(t, files)
722
723 b.AssertFileContent("public/en/index.html", `Output format: html|Title: home en`)
724 b.AssertFileContent("public/en/foo.html", `Output format: foo|Title: home en`)
725 b.AssertFileContent("public/de/index.html", `Output format: html|Title: home de`)
726 b.AssertFileContent("public/de/foo.html", `Output format: foo|Title: home de`)
727 b.AssertFileContent("public/index.html", `
728 <head>
729 <title>/en/</title>
730 <link rel="canonical" href="/en/">
731 <meta charset="utf-8">
732 <meta http-equiv="refresh" content="0; url=/en/">
733 </head>
734 `)
735
736 files = strings.ReplaceAll(files, "isHTML = false", "isHTML = true")
737
738 b = Test(t, files)
739
740 b.AssertFileContent("public/en/index.html", `Output format: html|Title: home en`)
741 b.AssertFileContent("public/en/foo.html", `Output format: foo|Title: home en`)
742 b.AssertFileContent("public/de/index.html", `Output format: html|Title: home de`)
743 b.AssertFileContent("public/de/foo.html", `Output format: foo|Title: home de`)
744 b.AssertFileContent("public/index.html", `
745 <head>
746 <title>/en/</title>
747 <link rel="canonical" href="/en/">
748 <meta charset="utf-8">
749 <meta http-equiv="refresh" content="0; url=/en/">
750 </head>
751 `)
752 }
753
754 // Issue 14807
755 func TestDoNotRenderAliasesIfPageNotRendered14807(t *testing.T) {
756 t.Parallel()
757
758 files := `
759 -- hugo.toml --
760 disableKinds = ['home','rss','section','sitemap','taxonomy','term']
761 -- content/p1.md --
762 ---
763 title: p1
764 aliases: [p1-alias]
765 ---
766 -- content/p2.md --
767 ---
768 title: p2
769 aliases: [p2-alias]
770 build:
771 render: never
772 ---
773 -- content/p3.md --
774 ---
775 title: p3
776 aliases: [p3-alias]
777 build:
778 render: link
779 ---
780 -- layouts/page.html --
781 {{ .Title }}
782 `
783
784 b := Test(t, files)
785
786 b.AssertFileExists("public/p1/index.html", true)
787 b.AssertFileExists("public/p2/index.html", false)
788 b.AssertFileExists("public/p3/index.html", false)
789 b.AssertFileExists("public/p1-alias/index.html", true)
790 b.AssertFileExists("public/p2-alias/index.html", false)
791 b.AssertFileExists("public/p3-alias/index.html", false)
792 }
793
794 // See issue 15066.
795 func TestAliasExplicitExtension(t *testing.T) {
796 t.Parallel()
797
798 files := `
799 -- hugo.toml --
800 disableKinds = ['home','rss','section','sitemap','taxonomy','term']
801 uglyURLs = UGLYURLS
802
803 [mediaTypes]
804 [mediaTypes.'text/html']
805 suffixes = SUFFIXES
806
807 [outputFormats]
808 [outputFormats.html]
809 mediaType = 'text/html'
810
811 -- content/s1/p1.md --
812 ---
813 title: p1
814 aliases: [a, b.foo, c.html, d.htm, e.foo.html, f.foo.htm]
815 ---
816 -- layouts/page.html --
817 {{ .Title }}
818 `
819
820 r := strings.NewReplacer("UGLYURLS", "false", "SUFFIXES", "['html', 'htm']")
821 f := r.Replace(files)
822
823 b := Test(t, f)
824
825 b.AssertFileContent("public/s1/a/index.html", "url=/s1/p1/")
826 b.AssertFileContent("public/s1/b.foo/index.html", "url=/s1/p1/")
827 b.AssertFileContent("public/s1/c.html", "url=/s1/p1/")
828 b.AssertFileContent("public/s1/d.htm", "url=/s1/p1/")
829 b.AssertFileContent("public/s1/e.foo.html", "url=/s1/p1/")
830 b.AssertFileContent("public/s1/f.foo.htm", "url=/s1/p1/")
831
832 r = strings.NewReplacer("UGLYURLS", "false", "SUFFIXES", "['htm', 'html']")
833 f = r.Replace(files)
834
835 b = Test(t, f)
836
837 b.AssertFileContent("public/s1/a/index.htm", "url=/s1/p1/")
838 b.AssertFileContent("public/s1/b.foo/index.htm", "url=/s1/p1/")
839 b.AssertFileContent("public/s1/c.html", "url=/s1/p1/")
840 b.AssertFileContent("public/s1/d.htm", "url=/s1/p1/")
841 b.AssertFileContent("public/s1/e.foo.html", "url=/s1/p1/")
842 b.AssertFileContent("public/s1/f.foo.htm", "url=/s1/p1/")
843
844 r = strings.NewReplacer("UGLYURLS", "true", "SUFFIXES", "['html', 'htm']")
845 f = r.Replace(files)
846
847 b = Test(t, f)
848
849 b.AssertFileContent("public/s1/a.html", "url=/s1/p1.html")
850 b.AssertFileContent("public/s1/b.foo.html", "url=/s1/p1.html")
851 b.AssertFileContent("public/s1/c.html", "url=/s1/p1.html")
852 b.AssertFileContent("public/s1/d.htm", "url=/s1/p1.html")
853 b.AssertFileContent("public/s1/e.foo.html", "url=/s1/p1.html")
854 b.AssertFileContent("public/s1/f.foo.htm", "url=/s1/p1.html")
855
856 r = strings.NewReplacer("UGLYURLS", "true", "SUFFIXES", "['htm', 'html']")
857 f = r.Replace(files)
858
859 b = Test(t, f)
860
861 b.AssertFileContent("public/s1/a.htm", "url=/s1/p1.htm")
862 b.AssertFileContent("public/s1/b.foo.htm", "url=/s1/p1.htm")
863 b.AssertFileContent("public/s1/c.html", "url=/s1/p1.htm")
864 b.AssertFileContent("public/s1/d.htm", "url=/s1/p1.htm")
865 b.AssertFileContent("public/s1/e.foo.html", "url=/s1/p1.htm")
866 b.AssertFileContent("public/s1/f.foo.htm", "url=/s1/p1.htm")
867 }