github.com/fighterlyt/hugo@v0.47.1/hugolib/hugo_sites_multihost_test.go (about)

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