hugo

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

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

sitemap_test.go (3489B)

    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 	"reflect"
   18 	"testing"
   19 
   20 	qt "github.com/frankban/quicktest"
   21 	"github.com/gohugoio/hugo/config"
   22 	"github.com/gohugoio/hugo/deps"
   23 	"github.com/gohugoio/hugo/tpl"
   24 )
   25 
   26 const sitemapTemplate = `<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
   27   {{ range .Data.Pages }}
   28     {{- if .Permalink -}}
   29   <url>
   30     <loc>{{ .Permalink }}</loc>{{ if not .Lastmod.IsZero }}
   31     <lastmod>{{ safeHTML ( .Lastmod.Format "2006-01-02T15:04:05-07:00" ) }}</lastmod>{{ end }}{{ with .Sitemap.ChangeFreq }}
   32     <changefreq>{{ . }}</changefreq>{{ end }}{{ if ge .Sitemap.Priority 0.0 }}
   33     <priority>{{ .Sitemap.Priority }}</priority>{{ end }}
   34   </url>
   35     {{- end -}}
   36   {{ end }}
   37 </urlset>`
   38 
   39 func TestSitemapOutput(t *testing.T) {
   40 	t.Parallel()
   41 	for _, internal := range []bool{false, true} {
   42 		doTestSitemapOutput(t, internal)
   43 	}
   44 }
   45 
   46 func doTestSitemapOutput(t *testing.T, internal bool) {
   47 	c := qt.New(t)
   48 	cfg, fs := newTestCfg()
   49 	cfg.Set("baseURL", "http://auth/bub/")
   50 
   51 	depsCfg := deps.DepsCfg{Fs: fs, Cfg: cfg}
   52 
   53 	depsCfg.WithTemplate = func(templ tpl.TemplateManager) error {
   54 		if !internal {
   55 			templ.AddTemplate("sitemap.xml", sitemapTemplate)
   56 		}
   57 
   58 		// We want to check that the 404 page is not included in the sitemap
   59 		// output. This template should have no effect either way, but include
   60 		// it for the clarity.
   61 		templ.AddTemplate("404.html", "Not found")
   62 		return nil
   63 	}
   64 
   65 	writeSourcesToSource(t, "content", fs, weightedSources...)
   66 	s := buildSingleSite(t, depsCfg, BuildCfg{})
   67 	th := newTestHelper(s.Cfg, s.Fs, t)
   68 	outputSitemap := "public/sitemap.xml"
   69 
   70 	th.assertFileContent(outputSitemap,
   71 		// Regular page
   72 		" <loc>http://auth/bub/sect/doc1/</loc>",
   73 		// Home page
   74 		"<loc>http://auth/bub/</loc>",
   75 		// Section
   76 		"<loc>http://auth/bub/sect/</loc>",
   77 		// Tax terms
   78 		"<loc>http://auth/bub/categories/</loc>",
   79 		// Tax list
   80 		"<loc>http://auth/bub/categories/hugo/</loc>",
   81 	)
   82 
   83 	content := readWorkingDir(th, th.Fs, outputSitemap)
   84 	c.Assert(content, qt.Not(qt.Contains), "404")
   85 	c.Assert(content, qt.Not(qt.Contains), "<loc></loc>")
   86 }
   87 
   88 func TestParseSitemap(t *testing.T) {
   89 	t.Parallel()
   90 	expected := config.Sitemap{Priority: 3.0, Filename: "doo.xml", ChangeFreq: "3"}
   91 	input := map[string]any{
   92 		"changefreq": "3",
   93 		"priority":   3.0,
   94 		"filename":   "doo.xml",
   95 		"unknown":    "ignore",
   96 	}
   97 	result := config.DecodeSitemap(config.Sitemap{}, input)
   98 
   99 	if !reflect.DeepEqual(expected, result) {
  100 		t.Errorf("Got \n%v expected \n%v", result, expected)
  101 	}
  102 }
  103 
  104 // https://github.com/gohugoio/hugo/issues/5910
  105 func TestSitemapOutputFormats(t *testing.T) {
  106 	b := newTestSitesBuilder(t).WithSimpleConfigFile()
  107 
  108 	b.WithContent("blog/html-amp.md", `
  109 ---
  110 Title: AMP and HTML
  111 outputs: [ "html", "amp" ]
  112 ---
  113 
  114 `)
  115 
  116 	b.Build(BuildCfg{})
  117 
  118 	// Should link to the HTML version.
  119 	b.AssertFileContent("public/sitemap.xml", " <loc>http://example.com/blog/html-amp/</loc>")
  120 }