hugo_sites_multihost_test.go (4121B)
1 package hugolib
2
3 import (
4 "testing"
5
6 "github.com/gohugoio/hugo/resources/page"
7
8 qt "github.com/frankban/quicktest"
9 )
10
11 func TestMultihosts(t *testing.T) {
12 t.Parallel()
13
14 c := qt.New(t)
15
16 configTemplate := `
17 paginate = 1
18 disablePathToLower = true
19 defaultContentLanguage = "fr"
20 defaultContentLanguageInSubdir = false
21 staticDir = ["s1", "s2"]
22 enableRobotsTXT = true
23
24 [permalinks]
25 other = "/somewhere/else/:filename"
26
27 [Taxonomies]
28 tag = "tags"
29
30 [Languages]
31 [Languages.en]
32 staticDir2 = ["ens1", "ens2"]
33 baseURL = "https://example.com/docs"
34 weight = 10
35 title = "In English"
36 languageName = "English"
37
38 [Languages.fr]
39 staticDir2 = ["frs1", "frs2"]
40 baseURL = "https://example.fr"
41 weight = 20
42 title = "Le Français"
43 languageName = "Français"
44
45 [Languages.nn]
46 staticDir2 = ["nns1", "nns2"]
47 baseURL = "https://example.no"
48 weight = 30
49 title = "På nynorsk"
50 languageName = "Nynorsk"
51
52 `
53
54 b := newMultiSiteTestDefaultBuilder(t).WithConfigFile("toml", configTemplate)
55 b.CreateSites().Build(BuildCfg{})
56
57 b.AssertFileContent("public/en/sect/doc1-slug/index.html", "Hello")
58
59 s1 := b.H.Sites[0]
60
61 s1h := s1.getPage(page.KindHome)
62 c.Assert(s1h.IsTranslated(), qt.Equals, true)
63 c.Assert(len(s1h.Translations()), qt.Equals, 2)
64 c.Assert(s1h.Permalink(), qt.Equals, "https://example.com/docs/")
65
66 // For “regular multilingual” we kept the aliases pages with url in front matter
67 // as a literal value that we use as is.
68 // There is an ambiguity in the guessing.
69 // For multihost, we never want any content in the root.
70 //
71 // check url in front matter:
72 pageWithURLInFrontMatter := s1.getPage(page.KindPage, "sect/doc3.en.md")
73 c.Assert(pageWithURLInFrontMatter, qt.Not(qt.IsNil))
74 c.Assert(pageWithURLInFrontMatter.RelPermalink(), qt.Equals, "/docs/superbob/")
75 b.AssertFileContent("public/en/superbob/index.html", "doc3|Hello|en")
76
77 // the domain root is the language directory for each language, so the robots.txt is created in the language directories
78 b.AssertFileContent("public/en/robots.txt", "robots|en")
79 b.AssertFileContent("public/fr/robots.txt", "robots|fr")
80 b.AssertFileContent("public/nn/robots.txt", "robots|nn")
81 b.AssertFileDoesNotExist("public/robots.txt")
82
83 // check alias:
84 b.AssertFileContent("public/en/al/alias1/index.html", `content="0; url=https://example.com/docs/superbob/"`)
85 b.AssertFileContent("public/en/al/alias2/index.html", `content="0; url=https://example.com/docs/superbob/"`)
86
87 s2 := b.H.Sites[1]
88
89 s2h := s2.getPage(page.KindHome)
90 c.Assert(s2h.Permalink(), qt.Equals, "https://example.fr/")
91
92 b.AssertFileContent("public/fr/index.html", "French Home Page", "String Resource: /docs/text/pipes.txt")
93 b.AssertFileContent("public/fr/text/pipes.txt", "Hugo Pipes")
94 b.AssertFileContent("public/en/index.html", "Default Home Page", "String Resource: /docs/text/pipes.txt")
95 b.AssertFileContent("public/en/text/pipes.txt", "Hugo Pipes")
96
97 // Check paginators
98 b.AssertFileContent("public/en/page/1/index.html", `refresh" content="0; url=https://example.com/docs/"`)
99 b.AssertFileContent("public/nn/page/1/index.html", `refresh" content="0; url=https://example.no/"`)
100 b.AssertFileContent("public/en/sect/page/2/index.html", "List Page 2", "Hello", "https://example.com/docs/sect/", "\"/docs/sect/page/3/")
101 b.AssertFileContent("public/fr/sect/page/2/index.html", "List Page 2", "Bonjour", "https://example.fr/sect/")
102
103 // Check bundles
104
105 bundleEn := s1.getPage(page.KindPage, "bundles/b1/index.en.md")
106 c.Assert(bundleEn, qt.Not(qt.IsNil))
107 c.Assert(bundleEn.RelPermalink(), qt.Equals, "/docs/bundles/b1/")
108 c.Assert(len(bundleEn.Resources()), qt.Equals, 1)
109
110 b.AssertFileContent("public/en/bundles/b1/logo.png", "PNG Data")
111 b.AssertFileContent("public/en/bundles/b1/index.html", " image/png: /docs/bundles/b1/logo.png")
112
113 bundleFr := s2.getPage(page.KindPage, "bundles/b1/index.md")
114 c.Assert(bundleFr, qt.Not(qt.IsNil))
115 c.Assert(bundleFr.RelPermalink(), qt.Equals, "/bundles/b1/")
116 c.Assert(len(bundleFr.Resources()), qt.Equals, 1)
117 b.AssertFileContent("public/fr/bundles/b1/logo.png", "PNG Data")
118 b.AssertFileContent("public/fr/bundles/b1/index.html", " image/png: /bundles/b1/logo.png")
119 }