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, "baseURL": "/"}},
40 {".html", "", ".html", map[string]any{"uglyURLs": true, "baseURL": "/"}},
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(
277 "public/foo/s1-alias/index.html",
278 `<title>https://example.org/foo/s1/</title>`,
279 `<link rel="canonical" href="https://example.org/foo/s1/">`,
280 `<meta http-equiv="refresh" content="0; url=https://example.org/foo/s1/">`,
281 )
282 b.AssertFileContent(
283 "public/foo/s1/p1-alias/index.html",
284 `<title>https://example.org/foo/s1/p1/</title>`,
285 `<link rel="canonical" href="https://example.org/foo/s1/p1/">`,
286 `<meta http-equiv="refresh" content="0; url=https://example.org/foo/s1/p1/">`,
287 )
288 b.AssertFileContent(
289 "public/s2-alias/index.html",
290 `<title>https://example.org/foo/s2/</title>`,
291 `<link rel="canonical" href="https://example.org/foo/s2/">`,
292 `<meta http-equiv="refresh" content="0; url=https://example.org/foo/s2/">`,
293 )
294 b.AssertFileContent(
295 "public/p2-alias/index.html",
296 `<title>https://example.org/foo/s2/p2/</title>`,
297 `<link rel="canonical" href="https://example.org/foo/s2/p2/">`,
298 `<meta http-equiv="refresh" content="0; url=https://example.org/foo/s2/p2/">`,
299 )
300
301 // output format: print
302 b.AssertFileExists("public/print/foo/s1-alias/index.html", false)
303 b.AssertFileExists("public/print/foo/s1/p1-alias/index.html", false)
304 b.AssertFileExists("public/print/s2-alias/index.html", false)
305 b.AssertFileExists("public/print/p2-alias/index.html", false)
306
307 // ------------------------------------------------------------------------
308 // Test 2: Create aliases for the html and print output formats
309 // ------------------------------------------------------------------------
310
311 // public/
312 // ├── foo/
313 // │ ├── s1/
314 // │ │ ├── p1/
315 // │ │ │ └── index.html
316 // │ │ ├── p1-alias/
317 // │ │ │ └── index.html
318 // │ │ └── index.html
319 // │ ├── s1-alias/
320 // │ │ └── index.html
321 // │ ├── s2/
322 // │ │ ├── p2/
323 // │ │ │ └── index.html
324 // │ │ └── index.html
325 // │ └── index.html
326 // ├── p2-alias/
327 // │ └── index.html
328 // ├── print/
329 // │ ├── foo/
330 // │ │ ├── s1/
331 // │ │ │ ├── p1/
332 // │ │ │ │ └── index.html
333 // │ │ │ ├── p1-alias/
334 // │ │ │ │ └── index.html
335 // │ │ │ └── index.html
336 // │ │ ├── s1-alias/
337 // │ │ │ └── index.html
338 // │ │ ├── s2/
339 // │ │ │ ├── p2/
340 // │ │ │ │ └── index.html
341 // │ │ │ └── index.html
342 // │ │ └── index.html
343 // │ ├── p2-alias/
344 // │ │ └── index.html
345 // │ └── s2-alias/
346 // │ └── index.html
347 // └── s2-alias/
348 // └── index.html
349
350 f = strings.ReplaceAll(files, "IS_HTML", "true")
351 b = Test(t, f)
352
353 // output format: html
354 b.AssertFileContent(
355 "public/foo/s1-alias/index.html",
356 `<title>https://example.org/foo/s1/</title>`,
357 `<link rel="canonical" href="https://example.org/foo/s1/">`,
358 `<meta http-equiv="refresh" content="0; url=https://example.org/foo/s1/">`,
359 )
360 b.AssertFileContent(
361 "public/foo/s1/p1-alias/index.html",
362 `<title>https://example.org/foo/s1/p1/</title>`,
363 `<link rel="canonical" href="https://example.org/foo/s1/p1/">`,
364 `<meta http-equiv="refresh" content="0; url=https://example.org/foo/s1/p1/">`,
365 )
366 b.AssertFileContent(
367 "public/s2-alias/index.html",
368 `<title>https://example.org/foo/s2/</title>`,
369 `<link rel="canonical" href="https://example.org/foo/s2/">`,
370 `<meta http-equiv="refresh" content="0; url=https://example.org/foo/s2/">`,
371 )
372 b.AssertFileContent(
373 "public/p2-alias/index.html",
374 `<title>https://example.org/foo/s2/p2/</title>`,
375 `<link rel="canonical" href="https://example.org/foo/s2/p2/">`,
376 `<meta http-equiv="refresh" content="0; url=https://example.org/foo/s2/p2/">`,
377 )
378
379 // output format: print
380 b.AssertFileContent(
381 "public/print/foo/s1-alias/index.html",
382 `<title>https://example.org/print/foo/s1/</title>`,
383 `<link rel="canonical" href="https://example.org/foo/s1/">`,
384 `<meta http-equiv="refresh" content="0; url=https://example.org/print/foo/s1/">`,
385 )
386 b.AssertFileContent(
387 "public/print/foo/s1/p1-alias/index.html",
388 `<title>https://example.org/print/foo/s1/p1/</title>`,
389 `<link rel="canonical" href="https://example.org/foo/s1/p1/">`,
390 `<meta http-equiv="refresh" content="0; url=https://example.org/print/foo/s1/p1/">`,
391 )
392 b.AssertFileContent(
393 "public/print/s2-alias/index.html",
394 `<title>https://example.org/print/foo/s2/</title>`,
395 `<link rel="canonical" href="https://example.org/foo/s2/">`,
396 `<meta http-equiv="refresh" content="0; url=https://example.org/print/foo/s2/">`,
397 )
398 b.AssertFileContent(
399 "public/print/p2-alias/index.html",
400 `<title>https://example.org/print/foo/s2/p2/</title>`,
401 `<link rel="canonical" href="https://example.org/foo/s2/p2/">`,
402 `<meta http-equiv="refresh" content="0; url=https://example.org/print/foo/s2/p2/">`,
403 )
404 }
405
406 // Issue 14388
407 func TestIssue14388(t *testing.T) {
408 t.Parallel()
409
410 files := `
411 -- hugo.toml --
412 baseURL = 'https://example.org'
413 disableKinds = ['home', 'rss', 'sitemap', 'taxonomy', 'term']
414
415 defaultContentLanguage = 'en'
416
417 defaultContentLanguageInSubdir = true
418 defaultContentRoleInSubdir = true
419 defaultContentVersionInSubdir = true
420
421 [languages.en]
422 weight = 1
423 # baseURL = 'https://en.example.org/'
424
425 [languages.de]
426 weight = 2
427 # baseURL = 'https://de.example.org/'
428
429 [outputFormats.print]
430 isHTML = true
431 mediaType = 'text/html'
432 path = 'print'
433 permalinkable = true
434
435 [outputs]
436 page = ['html', 'print']
437 section = ['html', 'print']
438 -- content/foo/s1/_index.de.md --
439 ---
440 title: s1 de
441 aliases: [s1-alias]
442 ---
443 -- content/foo/s1/_index.en.md --
444 ---
445 title: s1 en
446 aliases: [s1-alias]
447 ---
448 -- content/foo/s1/p1.de.md --
449 ---
450 title: p1 de
451 aliases: [p1-alias]
452 ---
453 -- content/foo/s1/p1.en.md --
454 ---
455 title: p1 en
456 aliases: [p1-alias]
457 ---
458 -- content/foo/s2/_index.de.md --
459 ---
460 title: s2 de
461 aliases: [/s2-alias]
462 ---
463 -- content/foo/s2/_index.en.md --
464 ---
465 title: s2 en
466 aliases: [/s2-alias]
467 ---
468 -- content/foo/s2/p2.de.md --
469 ---
470 title: p2 de
471 aliases: [/p2-alias]
472 ---
473 -- content/foo/s2/p2.en.md --
474 ---
475 title: p2 en
476 aliases: [/p2-alias]
477 ---
478 -- layouts/all.html --
479 {{ .Title }}
480 `
481 // ------------------------------------------------------------------------
482 // Test 1: Multilingual single-host
483 // ------------------------------------------------------------------------
484
485 b := Test(t, files)
486
487 // language: de, output format: html
488 b.AssertFileContent(
489 "public/guest/v1.0.0/de/foo/s1-alias/index.html",
490 `<title>https://example.org/guest/v1.0.0/de/foo/s1/</title>`,
491 `<link rel="canonical" href="https://example.org/guest/v1.0.0/de/foo/s1/">`,
492 `<meta http-equiv="refresh" content="0; url=https://example.org/guest/v1.0.0/de/foo/s1/">`,
493 )
494 b.AssertFileContent(
495 "public/guest/v1.0.0/de/foo/s1/p1-alias/index.html",
496 `<title>https://example.org/guest/v1.0.0/de/foo/s1/p1/</title>`,
497 `<link rel="canonical" href="https://example.org/guest/v1.0.0/de/foo/s1/p1/">`,
498 `<meta http-equiv="refresh" content="0; url=https://example.org/guest/v1.0.0/de/foo/s1/p1/">`,
499 )
500 b.AssertFileContent(
501 "public/guest/v1.0.0/de/s2-alias/index.html",
502 `<title>https://example.org/guest/v1.0.0/de/foo/s2/</title>`,
503 `<link rel="canonical" href="https://example.org/guest/v1.0.0/de/foo/s2/">`,
504 `<meta http-equiv="refresh" content="0; url=https://example.org/guest/v1.0.0/de/foo/s2/">`,
505 )
506 b.AssertFileContent(
507 "public/guest/v1.0.0/de/p2-alias/index.html",
508 `<title>https://example.org/guest/v1.0.0/de/foo/s2/p2/</title>`,
509 `<link rel="canonical" href="https://example.org/guest/v1.0.0/de/foo/s2/p2/">`,
510 `<meta http-equiv="refresh" content="0; url=https://example.org/guest/v1.0.0/de/foo/s2/p2/">`,
511 )
512
513 // language: de, output format: print
514 b.AssertFileContent(
515 "public/guest/v1.0.0/de/print/foo/s1-alias/index.html",
516 `<title>https://example.org/guest/v1.0.0/de/print/foo/s1/</title>`,
517 `<link rel="canonical" href="https://example.org/guest/v1.0.0/de/foo/s1/">`,
518 `<meta http-equiv="refresh" content="0; url=https://example.org/guest/v1.0.0/de/print/foo/s1/">`,
519 )
520 b.AssertFileContent(
521 "public/guest/v1.0.0/de/print/foo/s1/p1-alias/index.html",
522 `<title>https://example.org/guest/v1.0.0/de/print/foo/s1/p1/</title>`,
523 `<link rel="canonical" href="https://example.org/guest/v1.0.0/de/foo/s1/p1/">`,
524 `<meta http-equiv="refresh" content="0; url=https://example.org/guest/v1.0.0/de/print/foo/s1/p1/">`,
525 )
526 b.AssertFileContent(
527 "public/guest/v1.0.0/de/print/s2-alias/index.html",
528 `<title>https://example.org/guest/v1.0.0/de/print/foo/s2/</title>`,
529 `<link rel="canonical" href="https://example.org/guest/v1.0.0/de/foo/s2/">`,
530 `<meta http-equiv="refresh" content="0; url=https://example.org/guest/v1.0.0/de/print/foo/s2/">`,
531 )
532 b.AssertFileContent(
533 "public/guest/v1.0.0/de/print/p2-alias/index.html",
534 `<title>https://example.org/guest/v1.0.0/de/print/foo/s2/p2/</title>`,
535 `<link rel="canonical" href="https://example.org/guest/v1.0.0/de/foo/s2/p2/">`,
536 `<meta http-equiv="refresh" content="0; url=https://example.org/guest/v1.0.0/de/print/foo/s2/p2/">`,
537 )
538
539 // language: en, output format: html
540 b.AssertFileContent(
541 "public/guest/v1.0.0/en/foo/s1-alias/index.html",
542 `<title>https://example.org/guest/v1.0.0/en/foo/s1/</title>`,
543 `<link rel="canonical" href="https://example.org/guest/v1.0.0/en/foo/s1/">`,
544 `<meta http-equiv="refresh" content="0; url=https://example.org/guest/v1.0.0/en/foo/s1/">`,
545 )
546 b.AssertFileContent(
547 "public/guest/v1.0.0/en/foo/s1/p1-alias/index.html",
548 `<title>https://example.org/guest/v1.0.0/en/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/foo/s1/p1/">`,
551 )
552 b.AssertFileContent(
553 "public/guest/v1.0.0/en/s2-alias/index.html",
554 `<title>https://example.org/guest/v1.0.0/en/foo/s2/</title>`,
555 `<link rel="canonical" href="https://example.org/guest/v1.0.0/en/foo/s2/">`,
556 `<meta http-equiv="refresh" content="0; url=https://example.org/guest/v1.0.0/en/foo/s2/">`,
557 )
558 b.AssertFileContent(
559 "public/guest/v1.0.0/en/p2-alias/index.html",
560 `<title>https://example.org/guest/v1.0.0/en/foo/s2/p2/</title>`,
561 `<link rel="canonical" href="https://example.org/guest/v1.0.0/en/foo/s2/p2/">`,
562 `<meta http-equiv="refresh" content="0; url=https://example.org/guest/v1.0.0/en/foo/s2/p2/">`,
563 )
564
565 // language: en, output format: print
566 b.AssertFileContent(
567 "public/guest/v1.0.0/en/print/foo/s1-alias/index.html",
568 `<title>https://example.org/guest/v1.0.0/en/print/foo/s1/</title>`,
569 `<link rel="canonical" href="https://example.org/guest/v1.0.0/en/foo/s1/">`,
570 `<meta http-equiv="refresh" content="0; url=https://example.org/guest/v1.0.0/en/print/foo/s1/">`,
571 )
572 b.AssertFileContent(
573 "public/guest/v1.0.0/en/print/foo/s1/p1-alias/index.html",
574 `<title>https://example.org/guest/v1.0.0/en/print/foo/s1/p1/</title>`,
575 `<link rel="canonical" href="https://example.org/guest/v1.0.0/en/foo/s1/p1/">`,
576 `<meta http-equiv="refresh" content="0; url=https://example.org/guest/v1.0.0/en/print/foo/s1/p1/">`,
577 )
578 b.AssertFileContent(
579 "public/guest/v1.0.0/en/print/s2-alias/index.html",
580 `<title>https://example.org/guest/v1.0.0/en/print/foo/s2/</title>`,
581 `<link rel="canonical" href="https://example.org/guest/v1.0.0/en/foo/s2/">`,
582 `<meta http-equiv="refresh" content="0; url=https://example.org/guest/v1.0.0/en/print/foo/s2/">`,
583 )
584 b.AssertFileContent(
585 "public/guest/v1.0.0/en/print/p2-alias/index.html",
586 `<title>https://example.org/guest/v1.0.0/en/print/foo/s2/p2/</title>`,
587 `<link rel="canonical" href="https://example.org/guest/v1.0.0/en/foo/s2/p2/">`,
588 `<meta http-equiv="refresh" content="0; url=https://example.org/guest/v1.0.0/en/print/foo/s2/p2/">`,
589 )
590
591 // ------------------------------------------------------------------------
592 // Test 2: Multilingual multihost
593 // ------------------------------------------------------------------------
594
595 files = strings.ReplaceAll(files, "# baseURL", "baseURL")
596 b = Test(t, files)
597
598 // language: de, output format: html
599 b.AssertFileContent(
600 "public/de/guest/v1.0.0/foo/s1-alias/index.html",
601 `<title>https://de.example.org/guest/v1.0.0/foo/s1/</title>`,
602 `<link rel="canonical" href="https://de.example.org/guest/v1.0.0/foo/s1/">`,
603 `<meta http-equiv="refresh" content="0; url=https://de.example.org/guest/v1.0.0/foo/s1/">`,
604 )
605 b.AssertFileContent(
606 "public/de/guest/v1.0.0/foo/s1/p1-alias/index.html",
607 `<title>https://de.example.org/guest/v1.0.0/foo/s1/p1/</title>`,
608 `<link rel="canonical" href="https://de.example.org/guest/v1.0.0/foo/s1/p1/">`,
609 `<meta http-equiv="refresh" content="0; url=https://de.example.org/guest/v1.0.0/foo/s1/p1/">`,
610 )
611 b.AssertFileContent(
612 "public/de/guest/v1.0.0/s2-alias/index.html",
613 `<title>https://de.example.org/guest/v1.0.0/foo/s2/</title>`,
614 `<link rel="canonical" href="https://de.example.org/guest/v1.0.0/foo/s2/">`,
615 `<meta http-equiv="refresh" content="0; url=https://de.example.org/guest/v1.0.0/foo/s2/">`,
616 )
617 b.AssertFileContent(
618 "public/de/guest/v1.0.0/p2-alias/index.html",
619 `<title>https://de.example.org/guest/v1.0.0/foo/s2/p2/</title>`,
620 `<link rel="canonical" href="https://de.example.org/guest/v1.0.0/foo/s2/p2/">`,
621 `<meta http-equiv="refresh" content="0; url=https://de.example.org/guest/v1.0.0/foo/s2/p2/">`,
622 )
623
624 // language: de, output format: print
625 b.AssertFileContent(
626 "public/de/guest/v1.0.0/print/foo/s1-alias/index.html",
627 `<title>https://de.example.org/guest/v1.0.0/print/foo/s1/</title>`,
628 `<link rel="canonical" href="https://de.example.org/guest/v1.0.0/foo/s1/">`,
629 `<meta http-equiv="refresh" content="0; url=https://de.example.org/guest/v1.0.0/print/foo/s1/">`,
630 )
631 b.AssertFileContent(
632 "public/de/guest/v1.0.0/print/foo/s1/p1-alias/index.html",
633 `<title>https://de.example.org/guest/v1.0.0/print/foo/s1/p1/</title>`,
634 `<link rel="canonical" href="https://de.example.org/guest/v1.0.0/foo/s1/p1/">`,
635 `<meta http-equiv="refresh" content="0; url=https://de.example.org/guest/v1.0.0/print/foo/s1/p1/">`,
636 )
637 b.AssertFileContent(
638 "public/de/guest/v1.0.0/print/s2-alias/index.html",
639 `<title>https://de.example.org/guest/v1.0.0/print/foo/s2/</title>`,
640 `<link rel="canonical" href="https://de.example.org/guest/v1.0.0/foo/s2/">`,
641 `<meta http-equiv="refresh" content="0; url=https://de.example.org/guest/v1.0.0/print/foo/s2/">`,
642 )
643 b.AssertFileContent(
644 "public/de/guest/v1.0.0/print/p2-alias/index.html",
645 `<title>https://de.example.org/guest/v1.0.0/print/foo/s2/p2/</title>`,
646 `<link rel="canonical" href="https://de.example.org/guest/v1.0.0/foo/s2/p2/">`,
647 `<meta http-equiv="refresh" content="0; url=https://de.example.org/guest/v1.0.0/print/foo/s2/p2/">`,
648 )
649
650 // language: en, output format: html
651 b.AssertFileContent(
652 "public/en/guest/v1.0.0/foo/s1-alias/index.html",
653 `<title>https://en.example.org/guest/v1.0.0/foo/s1/</title>`,
654 `<link rel="canonical" href="https://en.example.org/guest/v1.0.0/foo/s1/">`,
655 `<meta http-equiv="refresh" content="0; url=https://en.example.org/guest/v1.0.0/foo/s1/">`,
656 )
657 b.AssertFileContent(
658 "public/en/guest/v1.0.0/foo/s1/p1-alias/index.html",
659 `<title>https://en.example.org/guest/v1.0.0/foo/s1/p1/</title>`,
660 `<link rel="canonical" href="https://en.example.org/guest/v1.0.0/foo/s1/p1/">`,
661 `<meta http-equiv="refresh" content="0; url=https://en.example.org/guest/v1.0.0/foo/s1/p1/">`,
662 )
663 b.AssertFileContent(
664 "public/en/guest/v1.0.0/s2-alias/index.html",
665 `<title>https://en.example.org/guest/v1.0.0/foo/s2/</title>`,
666 `<link rel="canonical" href="https://en.example.org/guest/v1.0.0/foo/s2/">`,
667 `<meta http-equiv="refresh" content="0; url=https://en.example.org/guest/v1.0.0/foo/s2/">`,
668 )
669 b.AssertFileContent(
670 "public/en/guest/v1.0.0/p2-alias/index.html",
671 `<title>https://en.example.org/guest/v1.0.0/foo/s2/p2/</title>`,
672 `<link rel="canonical" href="https://en.example.org/guest/v1.0.0/foo/s2/p2/">`,
673 `<meta http-equiv="refresh" content="0; url=https://en.example.org/guest/v1.0.0/foo/s2/p2/">`,
674 )
675
676 // language: en, output format: print
677 b.AssertFileContent(
678 "public/en/guest/v1.0.0/print/foo/s1-alias/index.html",
679 `<title>https://en.example.org/guest/v1.0.0/print/foo/s1/</title>`,
680 `<link rel="canonical" href="https://en.example.org/guest/v1.0.0/foo/s1/">`,
681 `<meta http-equiv="refresh" content="0; url=https://en.example.org/guest/v1.0.0/print/foo/s1/">`,
682 )
683 b.AssertFileContent(
684 "public/en/guest/v1.0.0/print/foo/s1/p1-alias/index.html",
685 `<title>https://en.example.org/guest/v1.0.0/print/foo/s1/p1/</title>`,
686 `<link rel="canonical" href="https://en.example.org/guest/v1.0.0/foo/s1/p1/">`,
687 `<meta http-equiv="refresh" content="0; url=https://en.example.org/guest/v1.0.0/print/foo/s1/p1/">`,
688 )
689 b.AssertFileContent(
690 "public/en/guest/v1.0.0/print/s2-alias/index.html",
691 `<title>https://en.example.org/guest/v1.0.0/print/foo/s2/</title>`,
692 `<link rel="canonical" href="https://en.example.org/guest/v1.0.0/foo/s2/">`,
693 `<meta http-equiv="refresh" content="0; url=https://en.example.org/guest/v1.0.0/print/foo/s2/">`,
694 )
695 b.AssertFileContent(
696 "public/en/guest/v1.0.0/print/p2-alias/index.html",
697 `<title>https://en.example.org/guest/v1.0.0/print/foo/s2/p2/</title>`,
698 `<link rel="canonical" href="https://en.example.org/guest/v1.0.0/foo/s2/p2/">`,
699 `<meta http-equiv="refresh" content="0; url=https://en.example.org/guest/v1.0.0/print/foo/s2/p2/">`,
700 )
701 }
702
703 // Issue #14402.
704 func TestComprehensiveAliasesRedirectsFile(t *testing.T) {
705 t.Parallel()
706
707 files := `
708 -- hugo.toml --
709 disableAliases = true
710 defaultContentLanguageInSubdir = true
711 defaultContentRoleInSubdir = true
712 defaultContentVersionInSubdir = true
713 baseURL = "https://example.org/"
714 -- content/foo/p1.md --
715 ---
716 aliases: ["/foo/p2/", "../p3/"]
717 ---
718 -- content/foo/p2.md --
719 -- content/p3.md --
720 -- layouts/home.html --
721 Home.
722 {{ range $p := site.RegularPages }}{{ range .Aliases }}{{ . | printf "%-35s" }}=>{{ $p.RelPermalink -}}|{{ end -}}{{ end }}
723 `
724 b := Test(t, files)
725
726 b.AssertFileContent(
727 "public/guest/v1.0.0/en/index.html",
728 "/guest/v1.0.0/en/foo/p2 =>/guest/v1.0.0/en/foo/p1/|",
729 "/guest/v1.0.0/en/p3 =>/guest/v1.0.0/en/foo/p1/|",
730 )
731 }
732
733 // Issue 14482
734 func TestOutputFormatIsHTMLWithMultilangAliases(t *testing.T) {
735 t.Parallel()
736
737 files := `
738 -- hugo.toml --
739 baseURL = "/"
740 defaultContentLanguageInSubdir = true
741 disableKinds = ['page', 'section', 'rss', 'sitemap', 'taxonomy', 'term']
742
743 [languages.en]
744 [languages.de]
745
746 [outputFormats.foo]
747 baseName = 'foo'
748 isHTML = false
749 mediaType = 'text/html'
750
751 [outputs]
752 home = ['html', 'foo']
753 -- content/index.de.md --
754 ---
755 title: home de
756 ---
757 -- content/index.en.md --
758 ---
759 title: home en
760 ---
761 -- layouts/home.html --
762 Output format: html|Title: {{ .Title }}
763 -- layouts/home.foo.html --
764 Output format: foo|Title: {{ .Title }}
765 `
766
767 b := Test(t, files)
768
769 b.AssertFileContent("public/en/index.html", `Output format: html|Title: home en`)
770 b.AssertFileContent("public/en/foo.html", `Output format: foo|Title: home en`)
771 b.AssertFileContent("public/de/index.html", `Output format: html|Title: home de`)
772 b.AssertFileContent("public/de/foo.html", `Output format: foo|Title: home de`)
773 b.AssertFileContent("public/index.html", `
774 <head>
775 <title>/en/</title>
776 <link rel="canonical" href="/en/">
777 <meta charset="utf-8">
778 <meta http-equiv="refresh" content="0; url=/en/">
779 </head>
780 `)
781
782 files = strings.ReplaceAll(files, "isHTML = false", "isHTML = true")
783
784 b = Test(t, files)
785
786 b.AssertFileContent("public/en/index.html", `Output format: html|Title: home en`)
787 b.AssertFileContent("public/en/foo.html", `Output format: foo|Title: home en`)
788 b.AssertFileContent("public/de/index.html", `Output format: html|Title: home de`)
789 b.AssertFileContent("public/de/foo.html", `Output format: foo|Title: home de`)
790 b.AssertFileContent("public/index.html", `
791 <head>
792 <title>/en/</title>
793 <link rel="canonical" href="/en/">
794 <meta charset="utf-8">
795 <meta http-equiv="refresh" content="0; url=/en/">
796 </head>
797 `)
798 }
799
800 // Issue 14807
801 func TestDoNotRenderAliasesIfPageNotRendered14807(t *testing.T) {
802 t.Parallel()
803
804 files := `
805 -- hugo.toml --
806 disableKinds = ['home','rss','section','sitemap','taxonomy','term']
807 -- content/p1.md --
808 ---
809 title: p1
810 aliases: [p1-alias]
811 ---
812 -- content/p2.md --
813 ---
814 title: p2
815 aliases: [p2-alias]
816 build:
817 render: never
818 ---
819 -- content/p3.md --
820 ---
821 title: p3
822 aliases: [p3-alias]
823 build:
824 render: link
825 ---
826 -- layouts/page.html --
827 {{ .Title }}
828 `
829
830 b := Test(t, files)
831
832 b.AssertFileExists("public/p1/index.html", true)
833 b.AssertFileExists("public/p2/index.html", false)
834 b.AssertFileExists("public/p3/index.html", false)
835 b.AssertFileExists("public/p1-alias/index.html", true)
836 b.AssertFileExists("public/p2-alias/index.html", false)
837 b.AssertFileExists("public/p3-alias/index.html", false)
838 }
839
840 // See issue 15066.
841 func TestAliasExplicitExtension(t *testing.T) {
842 t.Parallel()
843
844 files := `
845 -- hugo.toml --
846 disableKinds = ['home','rss','section','sitemap','taxonomy','term']
847 uglyURLs = UGLYURLS
848 baseURL = "/"
849
850 [mediaTypes]
851 [mediaTypes.'text/html']
852 suffixes = SUFFIXES
853
854 [outputFormats]
855 [outputFormats.html]
856 mediaType = 'text/html'
857
858 -- content/s1/p1.md --
859 ---
860 title: p1
861 aliases: [a, b.foo, c.html, d.htm, e.foo.html, f.foo.htm]
862 ---
863 -- layouts/page.html --
864 {{ .Title }}
865 `
866
867 r := strings.NewReplacer("UGLYURLS", "false", "SUFFIXES", "['html', 'htm']")
868 f := r.Replace(files)
869
870 b := Test(t, f)
871
872 b.AssertFileContent("public/s1/a/index.html", "url=/s1/p1/")
873 b.AssertFileContent("public/s1/b.foo/index.html", "url=/s1/p1/")
874 b.AssertFileContent("public/s1/c.html", "url=/s1/p1/")
875 b.AssertFileContent("public/s1/d.htm", "url=/s1/p1/")
876 b.AssertFileContent("public/s1/e.foo.html", "url=/s1/p1/")
877 b.AssertFileContent("public/s1/f.foo.htm", "url=/s1/p1/")
878
879 r = strings.NewReplacer("UGLYURLS", "false", "SUFFIXES", "['htm', 'html']")
880 f = r.Replace(files)
881
882 b = Test(t, f)
883
884 b.AssertFileContent("public/s1/a/index.htm", "url=/s1/p1/")
885 b.AssertFileContent("public/s1/b.foo/index.htm", "url=/s1/p1/")
886 b.AssertFileContent("public/s1/c.html", "url=/s1/p1/")
887 b.AssertFileContent("public/s1/d.htm", "url=/s1/p1/")
888 b.AssertFileContent("public/s1/e.foo.html", "url=/s1/p1/")
889 b.AssertFileContent("public/s1/f.foo.htm", "url=/s1/p1/")
890
891 r = strings.NewReplacer("UGLYURLS", "true", "SUFFIXES", "['html', 'htm']")
892 f = r.Replace(files)
893
894 b = Test(t, f)
895
896 b.AssertFileContent("public/s1/a.html", "url=/s1/p1.html")
897 b.AssertFileContent("public/s1/b.foo.html", "url=/s1/p1.html")
898 b.AssertFileContent("public/s1/c.html", "url=/s1/p1.html")
899 b.AssertFileContent("public/s1/d.htm", "url=/s1/p1.html")
900 b.AssertFileContent("public/s1/e.foo.html", "url=/s1/p1.html")
901 b.AssertFileContent("public/s1/f.foo.htm", "url=/s1/p1.html")
902
903 r = strings.NewReplacer("UGLYURLS", "true", "SUFFIXES", "['htm', 'html']")
904 f = r.Replace(files)
905
906 b = Test(t, f)
907
908 b.AssertFileContent("public/s1/a.htm", "url=/s1/p1.htm")
909 b.AssertFileContent("public/s1/b.foo.htm", "url=/s1/p1.htm")
910 b.AssertFileContent("public/s1/c.html", "url=/s1/p1.htm")
911 b.AssertFileContent("public/s1/d.htm", "url=/s1/p1.htm")
912 b.AssertFileContent("public/s1/e.foo.html", "url=/s1/p1.htm")
913 b.AssertFileContent("public/s1/f.foo.htm", "url=/s1/p1.htm")
914 }