2015-12-10 17:19:38 -05:00
|
|
|
// Copyright 2015 The Hugo Authors. All rights reserved.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
2014-04-08 23:15:57 -04:00
|
|
|
package hugolib
|
|
|
|
|
|
|
|
import (
|
2017-02-17 12:40:08 -05:00
|
|
|
"fmt"
|
2016-11-07 14:24:37 -05:00
|
|
|
"path/filepath"
|
2016-12-26 20:42:43 -05:00
|
|
|
"reflect"
|
2017-03-09 13:19:29 -05:00
|
|
|
"strings"
|
2014-04-08 23:15:57 -04:00
|
|
|
"testing"
|
2016-04-01 14:17:16 -04:00
|
|
|
|
2017-02-17 12:40:08 -05:00
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
|
2017-06-13 12:42:45 -04:00
|
|
|
"github.com/gohugoio/hugo/deps"
|
2014-04-08 23:15:57 -04:00
|
|
|
)
|
|
|
|
|
2016-04-01 14:17:16 -04:00
|
|
|
func TestByCountOrderOfTaxonomies(t *testing.T) {
|
2017-02-04 22:20:06 -05:00
|
|
|
t.Parallel()
|
2016-04-01 14:17:16 -04:00
|
|
|
taxonomies := make(map[string]string)
|
|
|
|
|
|
|
|
taxonomies["tag"] = "tags"
|
|
|
|
taxonomies["category"] = "categories"
|
|
|
|
|
2017-02-04 22:20:06 -05:00
|
|
|
cfg, fs := newTestCfg()
|
2016-04-01 14:17:16 -04:00
|
|
|
|
2017-02-04 22:20:06 -05:00
|
|
|
cfg.Set("taxonomies", taxonomies)
|
2016-11-07 14:24:37 -05:00
|
|
|
|
2017-01-10 04:55:03 -05:00
|
|
|
writeSource(t, fs, filepath.Join("content", "page.md"), pageYamlWithTaxonomiesA)
|
2016-11-07 14:24:37 -05:00
|
|
|
|
2017-02-04 22:20:06 -05:00
|
|
|
s := buildSingleSite(t, deps.DepsCfg{Fs: fs, Cfg: cfg}, BuildCfg{})
|
2016-04-01 14:17:16 -04:00
|
|
|
|
|
|
|
st := make([]string, 0)
|
2017-01-10 04:55:03 -05:00
|
|
|
for _, t := range s.Taxonomies["tags"].ByCount() {
|
2016-04-01 14:17:16 -04:00
|
|
|
st = append(st, t.Name)
|
|
|
|
}
|
|
|
|
|
2018-08-22 01:18:37 -04:00
|
|
|
expect := []string{"a", "b", "c", "x/y"}
|
|
|
|
if !reflect.DeepEqual(st, expect) {
|
|
|
|
t.Fatalf("ordered taxonomies do not match %v. Got: %s", expect, st)
|
2016-04-01 14:17:16 -04:00
|
|
|
}
|
|
|
|
}
|
2017-02-17 12:40:08 -05:00
|
|
|
|
2017-03-09 13:19:29 -05:00
|
|
|
//
|
2017-02-17 12:40:08 -05:00
|
|
|
func TestTaxonomiesWithAndWithoutContentFile(t *testing.T) {
|
2017-03-09 13:19:29 -05:00
|
|
|
for _, uglyURLs := range []bool{false, true} {
|
2017-06-06 02:43:33 -04:00
|
|
|
for _, preserveTaxonomyNames := range []bool{false, true} {
|
|
|
|
t.Run(fmt.Sprintf("uglyURLs=%t,preserveTaxonomyNames=%t", uglyURLs, preserveTaxonomyNames), func(t *testing.T) {
|
|
|
|
doTestTaxonomiesWithAndWithoutContentFile(t, preserveTaxonomyNames, uglyURLs)
|
|
|
|
})
|
|
|
|
}
|
2017-02-23 04:03:48 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-09 13:19:29 -05:00
|
|
|
func doTestTaxonomiesWithAndWithoutContentFile(t *testing.T, preserveTaxonomyNames, uglyURLs bool) {
|
2017-02-17 12:40:08 -05:00
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
siteConfig := `
|
|
|
|
baseURL = "http://example.com/blog"
|
2017-02-23 04:03:48 -05:00
|
|
|
preserveTaxonomyNames = %t
|
2017-03-09 13:19:29 -05:00
|
|
|
uglyURLs = %t
|
2017-02-17 12:40:08 -05:00
|
|
|
|
|
|
|
paginate = 1
|
|
|
|
defaultContentLanguage = "en"
|
|
|
|
|
|
|
|
[Taxonomies]
|
|
|
|
tag = "tags"
|
|
|
|
category = "categories"
|
|
|
|
other = "others"
|
2017-03-01 11:07:38 -05:00
|
|
|
empty = "empties"
|
2017-11-06 22:58:41 -05:00
|
|
|
permalinked = "permalinkeds"
|
|
|
|
|
|
|
|
[permalinks]
|
|
|
|
permalinkeds = "/perma/:slug/"
|
2017-02-17 12:40:08 -05:00
|
|
|
`
|
|
|
|
|
|
|
|
pageTemplate := `---
|
|
|
|
title: "%s"
|
|
|
|
tags:
|
|
|
|
%s
|
|
|
|
categories:
|
|
|
|
%s
|
|
|
|
others:
|
|
|
|
%s
|
2017-11-06 22:58:41 -05:00
|
|
|
permalinkeds:
|
|
|
|
%s
|
2017-02-17 12:40:08 -05:00
|
|
|
---
|
|
|
|
# Doc
|
|
|
|
`
|
|
|
|
|
2017-03-09 13:19:29 -05:00
|
|
|
siteConfig = fmt.Sprintf(siteConfig, preserveTaxonomyNames, uglyURLs)
|
2017-02-23 04:03:48 -05:00
|
|
|
|
2017-02-20 03:33:35 -05:00
|
|
|
th, h := newTestSitesFromConfigWithDefaultTemplates(t, siteConfig)
|
|
|
|
require.Len(t, h.Sites, 1)
|
2017-02-17 12:40:08 -05:00
|
|
|
|
2017-02-20 03:33:35 -05:00
|
|
|
fs := th.Fs
|
2017-02-17 12:40:08 -05:00
|
|
|
|
2018-08-22 01:18:37 -04:00
|
|
|
writeSource(t, fs, "content/p1.md", fmt.Sprintf(pageTemplate, "t1/c1", "- Tag1", "- cat1\n- \"cAt/dOg\"", "- o1", "- pl1"))
|
2017-11-06 22:58:41 -05:00
|
|
|
writeSource(t, fs, "content/p2.md", fmt.Sprintf(pageTemplate, "t2/c1", "- tag2", "- cat1", "- o1", "- pl1"))
|
|
|
|
writeSource(t, fs, "content/p3.md", fmt.Sprintf(pageTemplate, "t2/c12", "- tag2", "- cat2", "- o1", "- pl1"))
|
|
|
|
writeSource(t, fs, "content/p4.md", fmt.Sprintf(pageTemplate, "Hello World", "", "", "- \"Hello Hugo world\"", "- pl1"))
|
2017-02-17 12:40:08 -05:00
|
|
|
|
2018-03-21 12:21:46 -04:00
|
|
|
writeNewContentFile(t, fs.Source, "Category Terms", "2017-01-01", "content/categories/_index.md", 10)
|
|
|
|
writeNewContentFile(t, fs.Source, "Tag1 List", "2017-01-01", "content/tags/Tag1/_index.md", 10)
|
2017-02-17 12:40:08 -05:00
|
|
|
|
2018-08-22 01:18:37 -04:00
|
|
|
require.NoError(t, h.Build(BuildCfg{}))
|
2017-02-17 12:40:08 -05:00
|
|
|
|
|
|
|
// So what we have now is:
|
|
|
|
// 1. categories with terms content page, but no content page for the only c1 category
|
|
|
|
// 2. tags with no terms content page, but content page for one of 2 tags (tag1)
|
|
|
|
// 3. the "others" taxonomy with no content pages.
|
2017-11-07 17:42:47 -05:00
|
|
|
// 4. the "permalinkeds" taxonomy with permalinks configuration.
|
2017-02-17 12:40:08 -05:00
|
|
|
|
2017-03-09 13:19:29 -05:00
|
|
|
pathFunc := func(s string) string {
|
|
|
|
if uglyURLs {
|
|
|
|
return strings.Replace(s, "/index.html", ".html", 1)
|
|
|
|
}
|
|
|
|
return s
|
|
|
|
}
|
|
|
|
|
2017-02-17 12:40:08 -05:00
|
|
|
// 1.
|
2018-08-22 01:18:37 -04:00
|
|
|
if preserveTaxonomyNames {
|
2018-09-06 09:45:29 -04:00
|
|
|
th.assertFileContent(pathFunc("public/categories/cat1/index.html"), "List", "cat1")
|
|
|
|
th.assertFileContent(pathFunc("public/categories/cat-dog/index.html"), "List", "cAt/dOg")
|
2018-08-22 01:18:37 -04:00
|
|
|
} else {
|
2018-09-06 09:45:29 -04:00
|
|
|
th.assertFileContent(pathFunc("public/categories/cat1/index.html"), "List", "Cat1")
|
2018-08-22 01:18:37 -04:00
|
|
|
th.assertFileContent(pathFunc("public/categories/cat-dog/index.html"), "List", "Cat/Dog")
|
|
|
|
}
|
2017-03-09 13:19:29 -05:00
|
|
|
th.assertFileContent(pathFunc("public/categories/index.html"), "Terms List", "Category Terms")
|
2017-02-17 12:40:08 -05:00
|
|
|
|
|
|
|
// 2.
|
2018-09-06 09:45:29 -04:00
|
|
|
if preserveTaxonomyNames {
|
|
|
|
th.assertFileContent(pathFunc("public/tags/tag2/index.html"), "List", "tag2")
|
|
|
|
} else {
|
|
|
|
th.assertFileContent(pathFunc("public/tags/tag2/index.html"), "List", "Tag2")
|
|
|
|
}
|
2017-03-09 13:19:29 -05:00
|
|
|
th.assertFileContent(pathFunc("public/tags/tag1/index.html"), "List", "Tag1")
|
|
|
|
th.assertFileContent(pathFunc("public/tags/index.html"), "Terms List", "Tags")
|
2017-02-17 12:40:08 -05:00
|
|
|
|
|
|
|
// 3.
|
2018-09-06 09:45:29 -04:00
|
|
|
if preserveTaxonomyNames {
|
|
|
|
th.assertFileContent(pathFunc("public/others/o1/index.html"), "List", "o1")
|
|
|
|
} else {
|
|
|
|
th.assertFileContent(pathFunc("public/others/o1/index.html"), "List", "O1")
|
|
|
|
}
|
2017-03-09 13:19:29 -05:00
|
|
|
th.assertFileContent(pathFunc("public/others/index.html"), "Terms List", "Others")
|
2017-02-17 12:40:08 -05:00
|
|
|
|
2017-11-07 17:42:47 -05:00
|
|
|
// 4.
|
2018-09-06 09:45:29 -04:00
|
|
|
if preserveTaxonomyNames {
|
|
|
|
th.assertFileContent(pathFunc("public/perma/pl1/index.html"), "List", "pl1")
|
|
|
|
} else {
|
|
|
|
th.assertFileContent(pathFunc("public/perma/pl1/index.html"), "List", "Pl1")
|
|
|
|
}
|
2017-11-07 17:42:47 -05:00
|
|
|
// This looks kind of funky, but the taxonomy terms do not have a permalinks definition,
|
|
|
|
// for good reasons.
|
|
|
|
th.assertFileContent(pathFunc("public/permalinkeds/index.html"), "Terms List", "Permalinkeds")
|
|
|
|
|
2017-02-22 15:13:21 -05:00
|
|
|
s := h.Sites[0]
|
|
|
|
|
2017-03-05 15:24:14 -05:00
|
|
|
// Make sure that each KindTaxonomyTerm page has an appropriate number
|
|
|
|
// of KindTaxonomy pages in its Pages slice.
|
|
|
|
taxonomyTermPageCounts := map[string]int{
|
2017-11-06 22:58:41 -05:00
|
|
|
"tags": 2,
|
2018-08-22 01:18:37 -04:00
|
|
|
"categories": 3,
|
2017-11-06 22:58:41 -05:00
|
|
|
"others": 2,
|
|
|
|
"empties": 0,
|
|
|
|
"permalinkeds": 1,
|
2017-03-05 15:24:14 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
for taxonomy, count := range taxonomyTermPageCounts {
|
2018-07-17 05:18:29 -04:00
|
|
|
term := s.getPage(KindTaxonomyTerm, taxonomy)
|
2017-03-05 15:24:14 -05:00
|
|
|
require.NotNil(t, term)
|
2018-08-22 01:18:37 -04:00
|
|
|
require.Len(t, term.Pages, count, taxonomy)
|
2017-03-05 15:24:14 -05:00
|
|
|
|
|
|
|
for _, page := range term.Pages {
|
|
|
|
require.Equal(t, KindTaxonomy, page.Kind)
|
|
|
|
}
|
|
|
|
}
|
2017-03-01 06:30:41 -05:00
|
|
|
|
2018-08-22 01:18:37 -04:00
|
|
|
fixTerm := func(s string) string {
|
|
|
|
if preserveTaxonomyNames {
|
|
|
|
return s
|
|
|
|
}
|
|
|
|
return strings.ToLower(s)
|
|
|
|
}
|
|
|
|
|
|
|
|
fixURL := func(s string) string {
|
|
|
|
if uglyURLs {
|
|
|
|
return strings.TrimRight(s, "/") + ".html"
|
|
|
|
}
|
|
|
|
return s
|
|
|
|
}
|
|
|
|
|
2018-07-17 05:18:29 -04:00
|
|
|
cat1 := s.getPage(KindTaxonomy, "categories", "cat1")
|
2017-03-09 13:19:29 -05:00
|
|
|
require.NotNil(t, cat1)
|
2018-08-22 01:18:37 -04:00
|
|
|
require.Equal(t, fixURL("/blog/categories/cat1/"), cat1.RelPermalink())
|
|
|
|
|
|
|
|
catdog := s.getPage(KindTaxonomy, "categories", fixTerm("cAt/dOg"))
|
|
|
|
require.NotNil(t, catdog)
|
|
|
|
require.Equal(t, fixURL("/blog/categories/cat-dog/"), catdog.RelPermalink())
|
2017-03-09 13:19:29 -05:00
|
|
|
|
2018-07-17 05:18:29 -04:00
|
|
|
pl1 := s.getPage(KindTaxonomy, "permalinkeds", "pl1")
|
2017-11-06 22:58:41 -05:00
|
|
|
require.NotNil(t, pl1)
|
2018-08-22 01:18:37 -04:00
|
|
|
require.Equal(t, fixURL("/blog/perma/pl1/"), pl1.RelPermalink())
|
|
|
|
|
|
|
|
permalinkeds := s.getPage(KindTaxonomyTerm, "permalinkeds")
|
2017-11-07 17:42:47 -05:00
|
|
|
require.NotNil(t, permalinkeds)
|
2018-08-22 01:18:37 -04:00
|
|
|
require.Equal(t, fixURL("/blog/permalinkeds/"), permalinkeds.RelPermalink())
|
2017-11-06 22:58:41 -05:00
|
|
|
|
2017-02-23 04:03:48 -05:00
|
|
|
// Issue #3070 preserveTaxonomyNames
|
|
|
|
if preserveTaxonomyNames {
|
2018-07-17 05:18:29 -04:00
|
|
|
helloWorld := s.getPage(KindTaxonomy, "others", "Hello Hugo world")
|
2017-02-23 04:03:48 -05:00
|
|
|
require.NotNil(t, helloWorld)
|
2018-01-15 14:40:39 -05:00
|
|
|
require.Equal(t, "Hello Hugo world", helloWorld.title)
|
2017-02-23 04:03:48 -05:00
|
|
|
} else {
|
2018-07-17 05:18:29 -04:00
|
|
|
helloWorld := s.getPage(KindTaxonomy, "others", "hello-hugo-world")
|
2017-02-23 04:03:48 -05:00
|
|
|
require.NotNil(t, helloWorld)
|
2018-01-15 14:40:39 -05:00
|
|
|
require.Equal(t, "Hello Hugo World", helloWorld.title)
|
2017-02-23 04:03:48 -05:00
|
|
|
}
|
|
|
|
|
2017-03-01 11:07:38 -05:00
|
|
|
// Issue #2977
|
2017-03-09 13:19:29 -05:00
|
|
|
th.assertFileContent(pathFunc("public/empties/index.html"), "Terms List", "Empties")
|
2017-03-01 11:07:38 -05:00
|
|
|
|
2017-02-17 12:40:08 -05:00
|
|
|
}
|