github.com/olliephillips/hugo@v0.42.2/hugolib/taxonomy_test.go (about)

     1  // Copyright 2015 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  	"path/filepath"
    19  	"reflect"
    20  	"strings"
    21  	"testing"
    22  
    23  	"github.com/stretchr/testify/require"
    24  
    25  	"github.com/gohugoio/hugo/deps"
    26  )
    27  
    28  func TestByCountOrderOfTaxonomies(t *testing.T) {
    29  	t.Parallel()
    30  	taxonomies := make(map[string]string)
    31  
    32  	taxonomies["tag"] = "tags"
    33  	taxonomies["category"] = "categories"
    34  
    35  	cfg, fs := newTestCfg()
    36  
    37  	cfg.Set("taxonomies", taxonomies)
    38  
    39  	writeSource(t, fs, filepath.Join("content", "page.md"), pageYamlWithTaxonomiesA)
    40  
    41  	s := buildSingleSite(t, deps.DepsCfg{Fs: fs, Cfg: cfg}, BuildCfg{})
    42  
    43  	st := make([]string, 0)
    44  	for _, t := range s.Taxonomies["tags"].ByCount() {
    45  		st = append(st, t.Name)
    46  	}
    47  
    48  	if !reflect.DeepEqual(st, []string{"a", "b", "c"}) {
    49  		t.Fatalf("ordered taxonomies do not match [a, b, c].  Got: %s", st)
    50  	}
    51  }
    52  
    53  //
    54  func TestTaxonomiesWithAndWithoutContentFile(t *testing.T) {
    55  	for _, uglyURLs := range []bool{false, true} {
    56  		for _, preserveTaxonomyNames := range []bool{false, true} {
    57  			t.Run(fmt.Sprintf("uglyURLs=%t,preserveTaxonomyNames=%t", uglyURLs, preserveTaxonomyNames), func(t *testing.T) {
    58  				doTestTaxonomiesWithAndWithoutContentFile(t, preserveTaxonomyNames, uglyURLs)
    59  			})
    60  		}
    61  	}
    62  }
    63  
    64  func doTestTaxonomiesWithAndWithoutContentFile(t *testing.T, preserveTaxonomyNames, uglyURLs bool) {
    65  	t.Parallel()
    66  
    67  	siteConfig := `
    68  baseURL = "http://example.com/blog"
    69  preserveTaxonomyNames = %t
    70  uglyURLs = %t
    71  
    72  paginate = 1
    73  defaultContentLanguage = "en"
    74  
    75  [Taxonomies]
    76  tag = "tags"
    77  category = "categories"
    78  other = "others"
    79  empty = "empties"
    80  permalinked = "permalinkeds"
    81  
    82  [permalinks]
    83  permalinkeds = "/perma/:slug/"
    84  `
    85  
    86  	pageTemplate := `---
    87  title: "%s"
    88  tags:
    89  %s
    90  categories:
    91  %s
    92  others:
    93  %s
    94  permalinkeds:
    95  %s
    96  ---
    97  # Doc
    98  `
    99  
   100  	siteConfig = fmt.Sprintf(siteConfig, preserveTaxonomyNames, uglyURLs)
   101  
   102  	th, h := newTestSitesFromConfigWithDefaultTemplates(t, siteConfig)
   103  	require.Len(t, h.Sites, 1)
   104  
   105  	fs := th.Fs
   106  
   107  	if preserveTaxonomyNames {
   108  		writeSource(t, fs, "content/p1.md", fmt.Sprintf(pageTemplate, "t1/c1", "- tag1", "- cat1", "- o1", "- pl1"))
   109  	} else {
   110  		// Check lower-casing of tags
   111  		writeSource(t, fs, "content/p1.md", fmt.Sprintf(pageTemplate, "t1/c1", "- Tag1", "- cAt1", "- o1", "- pl1"))
   112  
   113  	}
   114  	writeSource(t, fs, "content/p2.md", fmt.Sprintf(pageTemplate, "t2/c1", "- tag2", "- cat1", "- o1", "- pl1"))
   115  	writeSource(t, fs, "content/p3.md", fmt.Sprintf(pageTemplate, "t2/c12", "- tag2", "- cat2", "- o1", "- pl1"))
   116  	writeSource(t, fs, "content/p4.md", fmt.Sprintf(pageTemplate, "Hello World", "", "", "- \"Hello Hugo world\"", "- pl1"))
   117  
   118  	writeNewContentFile(t, fs.Source, "Category Terms", "2017-01-01", "content/categories/_index.md", 10)
   119  	writeNewContentFile(t, fs.Source, "Tag1 List", "2017-01-01", "content/tags/Tag1/_index.md", 10)
   120  
   121  	err := h.Build(BuildCfg{})
   122  
   123  	require.NoError(t, err)
   124  
   125  	// So what we have now is:
   126  	// 1. categories with terms content page, but no content page for the only c1 category
   127  	// 2. tags with no terms content page, but content page for one of 2 tags (tag1)
   128  	// 3. the "others" taxonomy with no content pages.
   129  	// 4. the "permalinkeds" taxonomy with permalinks configuration.
   130  
   131  	pathFunc := func(s string) string {
   132  		if uglyURLs {
   133  			return strings.Replace(s, "/index.html", ".html", 1)
   134  		}
   135  		return s
   136  	}
   137  
   138  	// 1.
   139  	th.assertFileContent(pathFunc("public/categories/cat1/index.html"), "List", "Cat1")
   140  	th.assertFileContent(pathFunc("public/categories/index.html"), "Terms List", "Category Terms")
   141  
   142  	// 2.
   143  	th.assertFileContent(pathFunc("public/tags/tag2/index.html"), "List", "Tag2")
   144  	th.assertFileContent(pathFunc("public/tags/tag1/index.html"), "List", "Tag1")
   145  	th.assertFileContent(pathFunc("public/tags/index.html"), "Terms List", "Tags")
   146  
   147  	// 3.
   148  	th.assertFileContent(pathFunc("public/others/o1/index.html"), "List", "O1")
   149  	th.assertFileContent(pathFunc("public/others/index.html"), "Terms List", "Others")
   150  
   151  	// 4.
   152  	th.assertFileContent(pathFunc("public/perma/pl1/index.html"), "List", "Pl1")
   153  	// This looks kind of funky, but the taxonomy terms do not have a permalinks definition,
   154  	// for good reasons.
   155  	th.assertFileContent(pathFunc("public/permalinkeds/index.html"), "Terms List", "Permalinkeds")
   156  
   157  	s := h.Sites[0]
   158  
   159  	// Make sure that each KindTaxonomyTerm page has an appropriate number
   160  	// of KindTaxonomy pages in its Pages slice.
   161  	taxonomyTermPageCounts := map[string]int{
   162  		"tags":         2,
   163  		"categories":   2,
   164  		"others":       2,
   165  		"empties":      0,
   166  		"permalinkeds": 1,
   167  	}
   168  
   169  	for taxonomy, count := range taxonomyTermPageCounts {
   170  		term := s.getPage(KindTaxonomyTerm, taxonomy)
   171  		require.NotNil(t, term)
   172  		require.Len(t, term.Pages, count)
   173  
   174  		for _, page := range term.Pages {
   175  			require.Equal(t, KindTaxonomy, page.Kind)
   176  		}
   177  	}
   178  
   179  	cat1 := s.getPage(KindTaxonomy, "categories", "cat1")
   180  	require.NotNil(t, cat1)
   181  	if uglyURLs {
   182  		require.Equal(t, "/blog/categories/cat1.html", cat1.RelPermalink())
   183  	} else {
   184  		require.Equal(t, "/blog/categories/cat1/", cat1.RelPermalink())
   185  	}
   186  
   187  	pl1 := s.getPage(KindTaxonomy, "permalinkeds", "pl1")
   188  	permalinkeds := s.getPage(KindTaxonomyTerm, "permalinkeds")
   189  	require.NotNil(t, pl1)
   190  	require.NotNil(t, permalinkeds)
   191  	if uglyURLs {
   192  		require.Equal(t, "/blog/perma/pl1.html", pl1.RelPermalink())
   193  		require.Equal(t, "/blog/permalinkeds.html", permalinkeds.RelPermalink())
   194  	} else {
   195  		require.Equal(t, "/blog/perma/pl1/", pl1.RelPermalink())
   196  		require.Equal(t, "/blog/permalinkeds/", permalinkeds.RelPermalink())
   197  	}
   198  
   199  	// Issue #3070 preserveTaxonomyNames
   200  	if preserveTaxonomyNames {
   201  		helloWorld := s.getPage(KindTaxonomy, "others", "Hello Hugo world")
   202  		require.NotNil(t, helloWorld)
   203  		require.Equal(t, "Hello Hugo world", helloWorld.title)
   204  	} else {
   205  		helloWorld := s.getPage(KindTaxonomy, "others", "hello-hugo-world")
   206  		require.NotNil(t, helloWorld)
   207  		require.Equal(t, "Hello Hugo World", helloWorld.title)
   208  	}
   209  
   210  	// Issue #2977
   211  	th.assertFileContent(pathFunc("public/empties/index.html"), "Terms List", "Empties")
   212  
   213  }