github.com/linchen2chris/hugo@v0.0.0-20230307053224-cec209389705/hugolib/rss_test.go (about)

     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  	"path/filepath"
    18  	"strings"
    19  	"testing"
    20  
    21  	"github.com/gohugoio/hugo/deps"
    22  )
    23  
    24  func TestRSSOutput(t *testing.T) {
    25  	t.Parallel()
    26  	var (
    27  		cfg, fs = newTestCfg()
    28  		th      = newTestHelper(cfg, fs, t)
    29  	)
    30  
    31  	rssLimit := len(weightedSources) - 1
    32  
    33  	rssURI := "index.xml"
    34  
    35  	cfg.Set("baseURL", "http://auth/bub/")
    36  	cfg.Set("title", "RSSTest")
    37  	cfg.Set("rssLimit", rssLimit)
    38  
    39  	for _, src := range weightedSources {
    40  		writeSource(t, fs, filepath.Join("content", "sect", src[0]), src[1])
    41  	}
    42  
    43  	buildSingleSite(t, deps.DepsCfg{Fs: fs, Cfg: cfg}, BuildCfg{})
    44  
    45  	// Home RSS
    46  	th.assertFileContent(filepath.Join("public", rssURI), "<?xml", "rss version", "RSSTest")
    47  	// Section RSS
    48  	th.assertFileContent(filepath.Join("public", "sect", rssURI), "<?xml", "rss version", "Sects on RSSTest")
    49  	// Taxonomy RSS
    50  	th.assertFileContent(filepath.Join("public", "categories", "hugo", rssURI), "<?xml", "rss version", "hugo on RSSTest")
    51  
    52  	// RSS Item Limit
    53  	content := readWorkingDir(t, fs, filepath.Join("public", rssURI))
    54  	c := strings.Count(content, "<item>")
    55  	if c != rssLimit {
    56  		t.Errorf("incorrect RSS item count: expected %d, got %d", rssLimit, c)
    57  	}
    58  
    59  	// Encoded summary
    60  	th.assertFileContent(filepath.Join("public", rssURI), "<?xml", "description", "A &lt;em&gt;custom&lt;/em&gt; summary")
    61  }
    62  
    63  // Before Hugo 0.49 we set the pseudo page kind RSS on the page when output to RSS.
    64  // This had some unintended side effects, esp. when the only output format for that page
    65  // was RSS.
    66  // For the page kinds that can have multiple output formats, the Kind should be one of the
    67  // standard home, page etc.
    68  // This test has this single purpose: Check that the Kind is that of the source page.
    69  // See https://github.com/gohugoio/hugo/issues/5138
    70  func TestRSSKind(t *testing.T) {
    71  	t.Parallel()
    72  
    73  	b := newTestSitesBuilder(t)
    74  	b.WithSimpleConfigFile().WithTemplatesAdded("index.rss.xml", `RSS Kind: {{ .Kind }}`)
    75  
    76  	b.Build(BuildCfg{})
    77  
    78  	b.AssertFileContent("public/index.xml", "RSS Kind: home")
    79  }
    80  
    81  func TestRSSCanonifyURLs(t *testing.T) {
    82  	t.Parallel()
    83  
    84  	b := newTestSitesBuilder(t)
    85  	b.WithSimpleConfigFile().WithTemplatesAdded("index.rss.xml", `<rss>{{ range .Pages }}<item>{{ .Content | html }}</item>{{ end }}</rss>`)
    86  	b.WithContent("page.md", `---
    87  Title: My Page
    88  ---
    89  
    90  Figure:
    91  
    92  {{< figure src="/images/sunset.jpg" title="Sunset" >}}
    93  
    94  
    95  
    96  `)
    97  	b.Build(BuildCfg{})
    98  
    99  	b.AssertFileContent("public/index.xml", "img src=&#34;http://example.com/images/sunset.jpg")
   100  }