hugo

Fork of github.com/gohugoio/hugo with reverse pagination support

git clone git://git.shimmy1996.com/hugo.git

pages_language_merge_test.go (6335B)

    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 	"fmt"
   18 	"testing"
   19 
   20 	qt "github.com/frankban/quicktest"
   21 	"github.com/gohugoio/hugo/resources/resource"
   22 )
   23 
   24 // TODO(bep) move and rewrite in resource/page.
   25 
   26 func TestMergeLanguages(t *testing.T) {
   27 	t.Parallel()
   28 	c := qt.New(t)
   29 
   30 	b := newTestSiteForLanguageMerge(t, 30)
   31 	b.CreateSites()
   32 
   33 	b.Build(BuildCfg{SkipRender: true})
   34 
   35 	h := b.H
   36 
   37 	enSite := h.Sites[0]
   38 	frSite := h.Sites[1]
   39 	nnSite := h.Sites[2]
   40 
   41 	c.Assert(len(enSite.RegularPages()), qt.Equals, 31)
   42 	c.Assert(len(frSite.RegularPages()), qt.Equals, 6)
   43 	c.Assert(len(nnSite.RegularPages()), qt.Equals, 12)
   44 
   45 	for i := 0; i < 2; i++ {
   46 		mergedNN := nnSite.RegularPages().MergeByLanguage(enSite.RegularPages())
   47 		c.Assert(len(mergedNN), qt.Equals, 31)
   48 		for i := 1; i <= 31; i++ {
   49 			expectedLang := "en"
   50 			if i == 2 || i%3 == 0 || i == 31 {
   51 				expectedLang = "nn"
   52 			}
   53 			p := mergedNN[i-1]
   54 			c.Assert(p.Language().Lang, qt.Equals, expectedLang)
   55 		}
   56 	}
   57 
   58 	mergedFR := frSite.RegularPages().MergeByLanguage(enSite.RegularPages())
   59 	c.Assert(len(mergedFR), qt.Equals, 31)
   60 	for i := 1; i <= 31; i++ {
   61 		expectedLang := "en"
   62 		if i%5 == 0 {
   63 			expectedLang = "fr"
   64 		}
   65 		p := mergedFR[i-1]
   66 		c.Assert(p.Language().Lang, qt.Equals, expectedLang)
   67 	}
   68 
   69 	firstNN := nnSite.RegularPages()[0]
   70 	c.Assert(len(firstNN.Sites()), qt.Equals, 4)
   71 	c.Assert(firstNN.Sites().First().Language().Lang, qt.Equals, "en")
   72 
   73 	nnBundle := nnSite.getPage("page", "bundle")
   74 	enBundle := enSite.getPage("page", "bundle")
   75 
   76 	c.Assert(len(enBundle.Resources()), qt.Equals, 6)
   77 	c.Assert(len(nnBundle.Resources()), qt.Equals, 2)
   78 
   79 	var ri any = nnBundle.Resources()
   80 
   81 	// This looks less ugly in the templates ...
   82 	mergedNNResources := ri.(resource.ResourcesLanguageMerger).MergeByLanguage(enBundle.Resources())
   83 	c.Assert(len(mergedNNResources), qt.Equals, 6)
   84 
   85 	unchanged, err := nnSite.RegularPages().MergeByLanguageInterface(nil)
   86 	c.Assert(err, qt.IsNil)
   87 	c.Assert(unchanged, deepEqualsPages, nnSite.RegularPages())
   88 }
   89 
   90 func TestMergeLanguagesTemplate(t *testing.T) {
   91 	t.Parallel()
   92 
   93 	b := newTestSiteForLanguageMerge(t, 15)
   94 	b.WithTemplates("home.html", `
   95 {{ $pages := .Site.RegularPages }}
   96 {{ .Scratch.Set "pages" $pages }}
   97 {{ $enSite := index .Sites 0 }}
   98 {{ $frSite := index .Sites 1 }}
   99 {{ if eq .Language.Lang "nn" }}:
  100 {{ $nnBundle := .Site.GetPage "page" "bundle" }}
  101 {{ $enBundle := $enSite.GetPage "page" "bundle" }}
  102 {{ .Scratch.Set "pages" ($pages | lang.Merge $frSite.RegularPages| lang.Merge $enSite.RegularPages) }}
  103 {{ .Scratch.Set "pages2" (sort ($nnBundle.Resources | lang.Merge $enBundle.Resources) "Title") }}
  104 {{ end }}
  105 {{ $pages := .Scratch.Get "pages" }}
  106 {{ $pages2 := .Scratch.Get "pages2" }}
  107 Pages1: {{ range $i, $p := $pages }}{{ add $i 1 }}: {{ .File.Path }} {{ .Language.Lang }} | {{ end }}
  108 Pages2: {{ range $i, $p := $pages2 }}{{ add $i 1 }}: {{ .Title }} {{ .Language.Lang }} | {{ end }}
  109 {{ $nil := resources.Get "asdfasdfasdf" }}
  110 Pages3: {{ $frSite.RegularPages | lang.Merge  $nil }}
  111 Pages4: {{  $nil | lang.Merge $frSite.RegularPages }}
  112 
  113 
  114 `,
  115 		"shortcodes/shortcode.html", "MyShort",
  116 		"shortcodes/lingo.html", "MyLingo",
  117 	)
  118 
  119 	b.CreateSites()
  120 	b.Build(BuildCfg{})
  121 
  122 	b.AssertFileContent("public/nn/index.html", "Pages1: 1: p1.md en | 2: p2.nn.md nn | 3: p3.nn.md nn | 4: p4.md en | 5: p5.fr.md fr | 6: p6.nn.md nn | 7: p7.md en | 8: p8.md en | 9: p9.nn.md nn | 10: p10.fr.md fr | 11: p11.md en | 12: p12.nn.md nn | 13: p13.md en | 14: p14.md en | 15: p15.nn.md nn")
  123 	b.AssertFileContent("public/nn/index.html", "Pages2: 1: doc100 en | 2: doc101 nn | 3: doc102 nn | 4: doc103 en | 5: doc104 en | 6: doc105 en")
  124 	b.AssertFileContent("public/nn/index.html", `
  125 Pages3: Pages(3)
  126 Pages4: Pages(3)
  127 	`)
  128 }
  129 
  130 func newTestSiteForLanguageMerge(t testing.TB, count int) *sitesBuilder {
  131 	contentTemplate := `---
  132 title: doc%d
  133 weight: %d
  134 date: "2018-02-28"
  135 ---
  136 # doc
  137 *some "content"*
  138 
  139 {{< shortcode >}}
  140 
  141 {{< lingo >}}
  142 `
  143 
  144 	builder := newTestSitesBuilder(t).WithDefaultMultiSiteConfig()
  145 
  146 	// We need some content with some missing translations.
  147 	// "en" is the main language, so add some English content + some Norwegian (nn, nynorsk) content.
  148 	var contentPairs []string
  149 	for i := 1; i <= count; i++ {
  150 		content := fmt.Sprintf(contentTemplate, i, i)
  151 		contentPairs = append(contentPairs, []string{fmt.Sprintf("p%d.md", i), content}...)
  152 		if i == 2 || i%3 == 0 {
  153 			// Add page 2,3, 6, 9 ... to both languages
  154 			contentPairs = append(contentPairs, []string{fmt.Sprintf("p%d.nn.md", i), content}...)
  155 		}
  156 		if i%5 == 0 {
  157 			// Add some French content, too.
  158 			contentPairs = append(contentPairs, []string{fmt.Sprintf("p%d.fr.md", i), content}...)
  159 		}
  160 	}
  161 
  162 	// See https://github.com/gohugoio/hugo/issues/4644
  163 	// Add a bundles
  164 	j := 100
  165 	contentPairs = append(contentPairs, []string{"bundle/index.md", fmt.Sprintf(contentTemplate, j, j)}...)
  166 	for i := 0; i < 6; i++ {
  167 		contentPairs = append(contentPairs, []string{fmt.Sprintf("bundle/pb%d.md", i), fmt.Sprintf(contentTemplate, i+j, i+j)}...)
  168 	}
  169 	contentPairs = append(contentPairs, []string{"bundle/index.nn.md", fmt.Sprintf(contentTemplate, j, j)}...)
  170 	for i := 1; i < 3; i++ {
  171 		contentPairs = append(contentPairs, []string{fmt.Sprintf("bundle/pb%d.nn.md", i), fmt.Sprintf(contentTemplate, i+j, i+j)}...)
  172 	}
  173 
  174 	builder.WithContent(contentPairs...)
  175 	return builder
  176 }
  177 
  178 func BenchmarkMergeByLanguage(b *testing.B) {
  179 	const count = 100
  180 
  181 	// newTestSiteForLanguageMerge creates count+1 pages.
  182 	builder := newTestSiteForLanguageMerge(b, count-1)
  183 	builder.CreateSites()
  184 	builder.Build(BuildCfg{SkipRender: true})
  185 	h := builder.H
  186 
  187 	enSite := h.Sites[0]
  188 	nnSite := h.Sites[2]
  189 
  190 	for i := 0; i < b.N; i++ {
  191 		merged := nnSite.RegularPages().MergeByLanguage(enSite.RegularPages())
  192 		if len(merged) != count {
  193 			b.Fatal("Count mismatch")
  194 		}
  195 	}
  196 }