gitee.com/mirrors/Hugo-Go@v0.47.1/hugolib/sitemap_test.go (about)

     1  // Copyright 2016 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  	"testing"
    18  
    19  	"reflect"
    20  
    21  	"github.com/stretchr/testify/require"
    22  
    23  	"github.com/gohugoio/hugo/deps"
    24  	"github.com/gohugoio/hugo/tpl"
    25  )
    26  
    27  const sitemapTemplate = `<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
    28    {{ range .Data.Pages }}
    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  </urlset>`
    37  
    38  func TestSitemapOutput(t *testing.T) {
    39  	t.Parallel()
    40  	for _, internal := range []bool{false, true} {
    41  		doTestSitemapOutput(t, internal)
    42  	}
    43  }
    44  
    45  func doTestSitemapOutput(t *testing.T, internal bool) {
    46  
    47  	cfg, fs := newTestCfg()
    48  	cfg.Set("baseURL", "http://auth/bub/")
    49  
    50  	depsCfg := deps.DepsCfg{Fs: fs, Cfg: cfg}
    51  
    52  	depsCfg.WithTemplate = func(templ tpl.TemplateHandler) error {
    53  		if !internal {
    54  			templ.AddTemplate("sitemap.xml", sitemapTemplate)
    55  		}
    56  
    57  		// We want to check that the 404 page is not included in the sitemap
    58  		// output. This template should have no effect either way, but include
    59  		// it for the clarity.
    60  		templ.AddTemplate("404.html", "Not found")
    61  		return nil
    62  	}
    63  
    64  	writeSourcesToSource(t, "content", fs, weightedSources...)
    65  	s := buildSingleSite(t, depsCfg, BuildCfg{})
    66  	th := testHelper{s.Cfg, s.Fs, t}
    67  	outputSitemap := "public/sitemap.xml"
    68  
    69  	th.assertFileContent(outputSitemap,
    70  		// Regular page
    71  		" <loc>http://auth/bub/sect/doc1/</loc>",
    72  		// Home page
    73  		"<loc>http://auth/bub/</loc>",
    74  		// Section
    75  		"<loc>http://auth/bub/sect/</loc>",
    76  		// Tax terms
    77  		"<loc>http://auth/bub/categories/</loc>",
    78  		// Tax list
    79  		"<loc>http://auth/bub/categories/hugo/</loc>",
    80  	)
    81  
    82  	content := readDestination(th.T, th.Fs, outputSitemap)
    83  	require.NotContains(t, content, "404")
    84  
    85  }
    86  
    87  func TestParseSitemap(t *testing.T) {
    88  	t.Parallel()
    89  	expected := Sitemap{Priority: 3.0, Filename: "doo.xml", ChangeFreq: "3"}
    90  	input := map[string]interface{}{
    91  		"changefreq": "3",
    92  		"priority":   3.0,
    93  		"filename":   "doo.xml",
    94  		"unknown":    "ignore",
    95  	}
    96  	result := parseSitemap(input)
    97  
    98  	if !reflect.DeepEqual(expected, result) {
    99  		t.Errorf("Got \n%v expected \n%v", result, expected)
   100  	}
   101  
   102  }