2016-03-21 19:28:42 -04:00
|
|
|
// Copyright 2016 The Hugo Authors. All rights reserved.
|
2015-12-10 17:19:38 -05:00
|
|
|
//
|
|
|
|
// 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-10-18 14:25:10 -04:00
|
|
|
package hugolib
|
|
|
|
|
|
|
|
import (
|
2015-01-22 11:23:01 -05:00
|
|
|
"fmt"
|
2014-11-03 10:14:10 -05:00
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
|
2015-05-20 02:21:21 -04:00
|
|
|
"path/filepath"
|
|
|
|
|
2014-10-18 14:25:10 -04:00
|
|
|
"github.com/BurntSushi/toml"
|
2014-11-04 00:41:47 -05:00
|
|
|
"github.com/spf13/hugo/hugofs"
|
2014-10-18 14:25:10 -04:00
|
|
|
"github.com/spf13/hugo/source"
|
|
|
|
"github.com/spf13/viper"
|
2014-12-16 06:08:16 -05:00
|
|
|
"github.com/stretchr/testify/assert"
|
2014-10-18 14:25:10 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2016-03-22 18:59:07 -04:00
|
|
|
confMenu1 = `
|
2014-10-18 14:25:10 -04:00
|
|
|
[[menu.main]]
|
|
|
|
name = "Go Home"
|
|
|
|
url = "/"
|
|
|
|
weight = 1
|
|
|
|
pre = "<div>"
|
|
|
|
post = "</div>"
|
|
|
|
[[menu.main]]
|
|
|
|
name = "Blog"
|
|
|
|
url = "/posts"
|
2016-01-14 09:14:21 -05:00
|
|
|
[[menu.main]]
|
|
|
|
name = "ext"
|
|
|
|
url = "http://gohugo.io"
|
|
|
|
identifier = "ext"
|
2016-01-14 11:30:05 -05:00
|
|
|
[[menu.main]]
|
|
|
|
name = "ext2"
|
|
|
|
url = "http://foo.local/Zoo/foo"
|
|
|
|
identifier = "ext2"
|
2014-10-18 14:25:10 -04:00
|
|
|
[[menu.grandparent]]
|
|
|
|
name = "grandparent"
|
|
|
|
url = "/grandparent"
|
|
|
|
identifier = "grandparentId"
|
|
|
|
[[menu.grandparent]]
|
|
|
|
name = "parent"
|
|
|
|
url = "/parent"
|
|
|
|
identifier = "parentId"
|
|
|
|
parent = "grandparentId"
|
|
|
|
[[menu.grandparent]]
|
|
|
|
name = "Go Home3"
|
|
|
|
url = "/"
|
|
|
|
identifier = "grandchildId"
|
|
|
|
parent = "parentId"
|
|
|
|
[[menu.tax]]
|
|
|
|
name = "Tax1"
|
|
|
|
url = "/two/key/"
|
|
|
|
identifier="1"
|
|
|
|
[[menu.tax]]
|
|
|
|
name = "Tax2"
|
2015-12-02 17:58:54 -05:00
|
|
|
url = "/two/key/"
|
2014-10-18 14:25:10 -04:00
|
|
|
identifier="2"
|
|
|
|
[[menu.tax]]
|
|
|
|
name = "Tax RSS"
|
|
|
|
url = "/two/key.xml"
|
2014-12-16 06:08:16 -05:00
|
|
|
identifier="xml"
|
2015-02-10 13:13:45 -05:00
|
|
|
[[menu.hash]]
|
|
|
|
name = "Tax With #"
|
|
|
|
url = "/resource#anchor"
|
|
|
|
identifier="hash"
|
2014-12-16 06:08:16 -05:00
|
|
|
[[menu.unicode]]
|
|
|
|
name = "Unicode Russian"
|
|
|
|
identifier = "unicode-russian"
|
|
|
|
url = "/новости-проекта"` // Russian => "news-project"
|
2014-10-18 14:25:10 -04:00
|
|
|
)
|
|
|
|
|
2016-03-22 18:59:07 -04:00
|
|
|
var menuPage1 = []byte(`+++
|
2014-10-18 14:25:10 -04:00
|
|
|
title = "One"
|
|
|
|
[menu]
|
|
|
|
[menu.p_one]
|
|
|
|
weight = 1
|
|
|
|
+++
|
|
|
|
Front Matter with Menu Pages`)
|
|
|
|
|
2016-03-22 18:59:07 -04:00
|
|
|
var menuPage2 = []byte(`+++
|
2014-10-18 14:25:10 -04:00
|
|
|
title = "Two"
|
|
|
|
weight = 2
|
|
|
|
[menu]
|
|
|
|
[menu.p_one]
|
|
|
|
[menu.p_two]
|
2015-01-22 11:23:01 -05:00
|
|
|
identifier = "Two"
|
2014-11-03 10:14:10 -05:00
|
|
|
|
2014-10-18 14:25:10 -04:00
|
|
|
+++
|
|
|
|
Front Matter with Menu Pages`)
|
|
|
|
|
2016-03-22 18:59:07 -04:00
|
|
|
var menuPage3 = []byte(`+++
|
2014-10-18 14:25:10 -04:00
|
|
|
title = "Three"
|
|
|
|
weight = 3
|
|
|
|
[menu]
|
|
|
|
[menu.p_two]
|
|
|
|
Name = "Three"
|
|
|
|
Parent = "Two"
|
|
|
|
+++
|
|
|
|
Front Matter with Menu Pages`)
|
|
|
|
|
2016-03-22 18:59:07 -04:00
|
|
|
var menuPage4 = []byte(`+++
|
2015-05-28 17:05:13 -04:00
|
|
|
title = "Four"
|
|
|
|
weight = 4
|
|
|
|
[menu]
|
|
|
|
[menu.p_two]
|
|
|
|
Name = "Four"
|
|
|
|
Parent = "Three"
|
|
|
|
+++
|
|
|
|
Front Matter with Menu Pages`)
|
|
|
|
|
2016-03-22 18:59:07 -04:00
|
|
|
var menuPageSources = []source.ByteSource{
|
|
|
|
{filepath.FromSlash("sect/doc1.md"), menuPage1},
|
|
|
|
{filepath.FromSlash("sect/doc2.md"), menuPage2},
|
|
|
|
{filepath.FromSlash("sect/doc3.md"), menuPage3},
|
2015-01-06 12:11:06 -05:00
|
|
|
}
|
|
|
|
|
2016-03-22 18:59:07 -04:00
|
|
|
var menuPageSectionsSources = []source.ByteSource{
|
|
|
|
{filepath.FromSlash("first/doc1.md"), menuPage1},
|
|
|
|
{filepath.FromSlash("first/doc2.md"), menuPage2},
|
|
|
|
{filepath.FromSlash("second-section/doc3.md"), menuPage3},
|
|
|
|
{filepath.FromSlash("Fish and Chips/doc4.md"), menuPage4},
|
2014-10-18 14:25:10 -04:00
|
|
|
}
|
|
|
|
|
2015-03-11 13:34:57 -04:00
|
|
|
func tstCreateMenuPageWithNameTOML(title, menu, name string) []byte {
|
2015-01-22 11:23:01 -05:00
|
|
|
return []byte(fmt.Sprintf(`+++
|
|
|
|
title = "%s"
|
|
|
|
weight = 1
|
|
|
|
[menu]
|
|
|
|
[menu.%s]
|
|
|
|
name = "%s"
|
|
|
|
+++
|
|
|
|
Front Matter with Menu with Name`, title, menu, name))
|
|
|
|
}
|
|
|
|
|
2015-03-11 13:34:57 -04:00
|
|
|
func tstCreateMenuPageWithIdentifierTOML(title, menu, identifier string) []byte {
|
2015-01-22 11:23:01 -05:00
|
|
|
return []byte(fmt.Sprintf(`+++
|
|
|
|
title = "%s"
|
|
|
|
weight = 1
|
|
|
|
[menu]
|
|
|
|
[menu.%s]
|
|
|
|
identifier = "%s"
|
|
|
|
name = "somename"
|
|
|
|
+++
|
|
|
|
Front Matter with Menu with Identifier`, title, menu, identifier))
|
|
|
|
}
|
|
|
|
|
2015-03-11 13:34:57 -04:00
|
|
|
func tstCreateMenuPageWithNameYAML(title, menu, name string) []byte {
|
2015-01-22 11:23:01 -05:00
|
|
|
return []byte(fmt.Sprintf(`---
|
|
|
|
title: "%s"
|
|
|
|
weight: 1
|
|
|
|
menu:
|
|
|
|
%s:
|
|
|
|
name: "%s"
|
|
|
|
---
|
|
|
|
Front Matter with Menu with Name`, title, menu, name))
|
|
|
|
}
|
|
|
|
|
2015-03-11 13:34:57 -04:00
|
|
|
func tstCreateMenuPageWithIdentifierYAML(title, menu, identifier string) []byte {
|
2015-01-22 11:23:01 -05:00
|
|
|
return []byte(fmt.Sprintf(`---
|
|
|
|
title: "%s"
|
|
|
|
weight: 1
|
|
|
|
menu:
|
|
|
|
%s:
|
|
|
|
identifier: "%s"
|
|
|
|
name: "somename"
|
|
|
|
---
|
|
|
|
Front Matter with Menu with Identifier`, title, menu, identifier))
|
|
|
|
}
|
|
|
|
|
|
|
|
// Issue 817 - identifier should trump everything
|
|
|
|
func TestPageMenuWithIdentifier(t *testing.T) {
|
|
|
|
|
|
|
|
toml := []source.ByteSource{
|
2015-03-11 13:34:57 -04:00
|
|
|
{"sect/doc1.md", tstCreateMenuPageWithIdentifierTOML("t1", "m1", "i1")},
|
|
|
|
{"sect/doc2.md", tstCreateMenuPageWithIdentifierTOML("t1", "m1", "i2")},
|
|
|
|
{"sect/doc3.md", tstCreateMenuPageWithIdentifierTOML("t1", "m1", "i2")}, // duplicate
|
2015-01-22 11:23:01 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
yaml := []source.ByteSource{
|
2015-03-11 13:34:57 -04:00
|
|
|
{"sect/doc1.md", tstCreateMenuPageWithIdentifierYAML("t1", "m1", "i1")},
|
|
|
|
{"sect/doc2.md", tstCreateMenuPageWithIdentifierYAML("t1", "m1", "i2")},
|
|
|
|
{"sect/doc3.md", tstCreateMenuPageWithIdentifierYAML("t1", "m1", "i2")}, // duplicate
|
2015-01-22 11:23:01 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
doTestPageMenuWithIdentifier(t, toml)
|
|
|
|
doTestPageMenuWithIdentifier(t, yaml)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
func doTestPageMenuWithIdentifier(t *testing.T, menuPageSources []source.ByteSource) {
|
|
|
|
|
2015-06-11 16:41:26 -04:00
|
|
|
viper.Reset()
|
|
|
|
defer viper.Reset()
|
|
|
|
|
2015-05-20 02:21:21 -04:00
|
|
|
s := setupMenuTests(t, menuPageSources)
|
2015-01-22 11:23:01 -05:00
|
|
|
|
2015-05-20 02:21:21 -04:00
|
|
|
assert.Equal(t, 3, len(s.Pages), "Not enough pages")
|
2015-01-22 11:23:01 -05:00
|
|
|
|
2015-05-20 02:21:21 -04:00
|
|
|
me1 := findTestMenuEntryByID(s, "m1", "i1")
|
|
|
|
me2 := findTestMenuEntryByID(s, "m1", "i2")
|
2015-01-22 11:23:01 -05:00
|
|
|
|
|
|
|
assert.NotNil(t, me1)
|
|
|
|
assert.NotNil(t, me2)
|
|
|
|
|
2015-06-21 17:09:39 -04:00
|
|
|
assert.True(t, strings.Contains(me1.URL, "doc1"), me1.URL)
|
2015-06-21 17:36:58 -04:00
|
|
|
assert.True(t, strings.Contains(me2.URL, "doc2") || strings.Contains(me2.URL, "doc3"), me2.URL)
|
2015-01-22 11:23:01 -05:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
// Issue 817 contd - name should be second identifier in
|
|
|
|
func TestPageMenuWithDuplicateName(t *testing.T) {
|
2015-05-20 02:21:21 -04:00
|
|
|
|
2015-01-22 11:23:01 -05:00
|
|
|
toml := []source.ByteSource{
|
2015-03-11 13:34:57 -04:00
|
|
|
{"sect/doc1.md", tstCreateMenuPageWithNameTOML("t1", "m1", "n1")},
|
|
|
|
{"sect/doc2.md", tstCreateMenuPageWithNameTOML("t1", "m1", "n2")},
|
|
|
|
{"sect/doc3.md", tstCreateMenuPageWithNameTOML("t1", "m1", "n2")}, // duplicate
|
2015-01-22 11:23:01 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
yaml := []source.ByteSource{
|
2015-03-11 13:34:57 -04:00
|
|
|
{"sect/doc1.md", tstCreateMenuPageWithNameYAML("t1", "m1", "n1")},
|
|
|
|
{"sect/doc2.md", tstCreateMenuPageWithNameYAML("t1", "m1", "n2")},
|
|
|
|
{"sect/doc3.md", tstCreateMenuPageWithNameYAML("t1", "m1", "n2")}, // duplicate
|
2015-01-22 11:23:01 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
doTestPageMenuWithDuplicateName(t, toml)
|
|
|
|
doTestPageMenuWithDuplicateName(t, yaml)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
func doTestPageMenuWithDuplicateName(t *testing.T, menuPageSources []source.ByteSource) {
|
2015-06-11 15:22:35 -04:00
|
|
|
viper.Reset()
|
|
|
|
defer viper.Reset()
|
|
|
|
|
2015-05-20 02:21:21 -04:00
|
|
|
s := setupMenuTests(t, menuPageSources)
|
2015-01-22 11:23:01 -05:00
|
|
|
|
2015-05-20 02:21:21 -04:00
|
|
|
assert.Equal(t, 3, len(s.Pages), "Not enough pages")
|
2015-01-22 11:23:01 -05:00
|
|
|
|
2015-05-20 02:21:21 -04:00
|
|
|
me1 := findTestMenuEntryByName(s, "m1", "n1")
|
|
|
|
me2 := findTestMenuEntryByName(s, "m1", "n2")
|
2015-01-22 11:23:01 -05:00
|
|
|
|
|
|
|
assert.NotNil(t, me1)
|
|
|
|
assert.NotNil(t, me2)
|
|
|
|
|
2015-06-21 17:36:58 -04:00
|
|
|
assert.True(t, strings.Contains(me1.URL, "doc1"), me1.URL)
|
|
|
|
assert.True(t, strings.Contains(me2.URL, "doc2") || strings.Contains(me2.URL, "doc3"), me2.URL)
|
2015-01-22 11:23:01 -05:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2015-01-01 17:40:14 -05:00
|
|
|
func TestPageMenu(t *testing.T) {
|
2015-05-20 02:21:21 -04:00
|
|
|
viper.Reset()
|
|
|
|
defer viper.Reset()
|
|
|
|
|
2016-03-22 18:59:07 -04:00
|
|
|
s := setupMenuTests(t, menuPageSources)
|
2014-10-18 14:25:10 -04:00
|
|
|
|
2015-05-20 02:21:21 -04:00
|
|
|
if len(s.Pages) != 3 {
|
|
|
|
t.Fatalf("Posts not created, expected 3 got %d", len(s.Pages))
|
2014-10-18 14:25:10 -04:00
|
|
|
}
|
|
|
|
|
2015-05-20 02:21:21 -04:00
|
|
|
first := s.Pages[0]
|
|
|
|
second := s.Pages[1]
|
|
|
|
third := s.Pages[2]
|
2014-10-18 14:25:10 -04:00
|
|
|
|
2015-05-20 02:21:21 -04:00
|
|
|
pOne := findTestMenuEntryByName(s, "p_one", "One")
|
|
|
|
pTwo := findTestMenuEntryByID(s, "p_two", "Two")
|
2014-10-18 14:25:10 -04:00
|
|
|
|
|
|
|
for i, this := range []struct {
|
|
|
|
menu string
|
|
|
|
page *Page
|
|
|
|
menuItem *MenuEntry
|
|
|
|
isMenuCurrent bool
|
|
|
|
hasMenuCurrent bool
|
|
|
|
}{
|
|
|
|
{"p_one", first, pOne, true, false},
|
|
|
|
{"p_one", first, pTwo, false, false},
|
2015-01-22 11:23:01 -05:00
|
|
|
{"p_one", second, pTwo, false, false},
|
2014-10-18 14:25:10 -04:00
|
|
|
{"p_two", second, pTwo, true, false},
|
|
|
|
{"p_two", third, pTwo, false, true},
|
|
|
|
{"p_one", third, pTwo, false, false},
|
|
|
|
} {
|
|
|
|
|
|
|
|
isMenuCurrent := this.page.IsMenuCurrent(this.menu, this.menuItem)
|
|
|
|
hasMenuCurrent := this.page.HasMenuCurrent(this.menu, this.menuItem)
|
|
|
|
|
|
|
|
if isMenuCurrent != this.isMenuCurrent {
|
|
|
|
t.Errorf("[%d] Wrong result from IsMenuCurrent: %v", i, isMenuCurrent)
|
|
|
|
}
|
|
|
|
|
|
|
|
if hasMenuCurrent != this.hasMenuCurrent {
|
|
|
|
t.Errorf("[%d] Wrong result for menuItem %v for HasMenuCurrent: %v", i, this.menuItem, hasMenuCurrent)
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2016-01-14 09:14:21 -05:00
|
|
|
func TestMenuURL(t *testing.T) {
|
2015-05-20 02:21:21 -04:00
|
|
|
viper.Reset()
|
|
|
|
defer viper.Reset()
|
2016-01-14 11:30:05 -05:00
|
|
|
|
2016-03-22 18:59:07 -04:00
|
|
|
s := setupMenuTests(t, menuPageSources)
|
2015-02-10 13:13:45 -05:00
|
|
|
|
2016-01-14 09:14:21 -05:00
|
|
|
for i, this := range []struct {
|
|
|
|
me *MenuEntry
|
|
|
|
expectedURL string
|
|
|
|
}{
|
|
|
|
// issue #888
|
|
|
|
{findTestMenuEntryByID(s, "hash", "hash"), "/Zoo/resource#anchor"},
|
|
|
|
// issue #1774
|
|
|
|
{findTestMenuEntryByID(s, "main", "ext"), "http://gohugo.io"},
|
2016-01-14 11:30:05 -05:00
|
|
|
{findTestMenuEntryByID(s, "main", "ext2"), "http://foo.local/Zoo/foo"},
|
2016-01-14 09:14:21 -05:00
|
|
|
} {
|
|
|
|
|
|
|
|
if this.me == nil {
|
|
|
|
t.Errorf("[%d] MenuEntry not found", i)
|
|
|
|
continue
|
|
|
|
}
|
2015-02-10 13:13:45 -05:00
|
|
|
|
2016-01-14 09:14:21 -05:00
|
|
|
if this.me.URL != this.expectedURL {
|
|
|
|
t.Errorf("[%d] Got URL %s expected %s", i, this.me.URL, this.expectedURL)
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2015-02-10 13:13:45 -05:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2016-03-10 04:31:12 -05:00
|
|
|
// Issue #1934
|
|
|
|
func TestYAMLMenuWithMultipleEntries(t *testing.T) {
|
|
|
|
viper.Reset()
|
|
|
|
defer viper.Reset()
|
|
|
|
|
|
|
|
ps1 := []byte(`---
|
|
|
|
title: "Yaml 1"
|
|
|
|
weight: 5
|
|
|
|
menu: ["p_one", "p_two"]
|
|
|
|
---
|
|
|
|
Yaml Front Matter with Menu Pages`)
|
|
|
|
|
|
|
|
ps2 := []byte(`---
|
|
|
|
title: "Yaml 2"
|
|
|
|
weight: 5
|
|
|
|
menu:
|
|
|
|
p_three:
|
|
|
|
p_four:
|
|
|
|
---
|
|
|
|
Yaml Front Matter with Menu Pages`)
|
|
|
|
|
|
|
|
s := setupMenuTests(t, []source.ByteSource{
|
|
|
|
{filepath.FromSlash("sect/yaml1.md"), ps1},
|
|
|
|
{filepath.FromSlash("sect/yaml2.md"), ps2}})
|
|
|
|
|
|
|
|
p1 := s.Pages[0]
|
|
|
|
assert.Len(t, p1.Menus(), 2, "List YAML")
|
|
|
|
p2 := s.Pages[1]
|
|
|
|
assert.Len(t, p2.Menus(), 2, "Map YAML")
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2014-12-16 06:08:16 -05:00
|
|
|
// issue #719
|
2015-03-11 13:34:57 -04:00
|
|
|
func TestMenuWithUnicodeURLs(t *testing.T) {
|
2015-05-20 02:21:21 -04:00
|
|
|
|
2015-12-02 17:58:54 -05:00
|
|
|
for _, canonifyURLs := range []bool{true, false} {
|
|
|
|
doTestMenuWithUnicodeURLs(t, canonifyURLs)
|
2014-12-16 06:08:16 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-12-02 17:58:54 -05:00
|
|
|
func doTestMenuWithUnicodeURLs(t *testing.T, canonifyURLs bool) {
|
2015-06-11 16:41:26 -04:00
|
|
|
viper.Reset()
|
|
|
|
defer viper.Reset()
|
|
|
|
|
2015-03-11 13:34:57 -04:00
|
|
|
viper.Set("CanonifyURLs", canonifyURLs)
|
2014-12-12 14:28:28 -05:00
|
|
|
|
2016-03-22 18:59:07 -04:00
|
|
|
s := setupMenuTests(t, menuPageSources)
|
2014-12-16 06:08:16 -05:00
|
|
|
|
2015-05-20 02:21:21 -04:00
|
|
|
unicodeRussian := findTestMenuEntryByID(s, "unicode", "unicode-russian")
|
2014-12-16 06:08:16 -05:00
|
|
|
|
2015-12-02 17:58:54 -05:00
|
|
|
expected := "/%D0%BD%D0%BE%D0%B2%D0%BE%D1%81%D1%82%D0%B8-%D0%BF%D1%80%D0%BE%D0%B5%D0%BA%D1%82%D0%B0"
|
2014-12-12 14:28:28 -05:00
|
|
|
|
2015-03-11 13:34:57 -04:00
|
|
|
if !canonifyURLs {
|
2015-12-02 17:58:54 -05:00
|
|
|
expected = "/Zoo" + expected
|
2014-12-16 06:08:16 -05:00
|
|
|
}
|
|
|
|
|
2015-12-02 17:58:54 -05:00
|
|
|
assert.Equal(t, expected, unicodeRussian.URL)
|
2014-12-16 06:08:16 -05:00
|
|
|
}
|
|
|
|
|
2015-05-09 14:54:11 -04:00
|
|
|
// Issue #1114
|
|
|
|
func TestSectionPagesMenu(t *testing.T) {
|
|
|
|
|
2015-05-20 02:21:21 -04:00
|
|
|
doTestSectionPagesMenu(true, t)
|
|
|
|
doTestSectionPagesMenu(false, t)
|
2015-05-09 14:54:11 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func doTestSectionPagesMenu(canonifyUrls bool, t *testing.T) {
|
2015-05-28 17:05:13 -04:00
|
|
|
viper.Reset()
|
|
|
|
defer viper.Reset()
|
|
|
|
|
|
|
|
viper.Set("SectionPagesMenu", "spm")
|
|
|
|
|
2015-05-09 14:54:11 -04:00
|
|
|
viper.Set("CanonifyURLs", canonifyUrls)
|
2016-03-22 18:59:07 -04:00
|
|
|
s := setupMenuTests(t, menuPageSectionsSources)
|
2015-05-09 14:54:11 -04:00
|
|
|
|
2015-05-28 17:05:13 -04:00
|
|
|
assert.Equal(t, 3, len(s.Sections))
|
2015-05-09 14:54:11 -04:00
|
|
|
|
2015-05-20 02:21:21 -04:00
|
|
|
firstSectionPages := s.Sections["first"]
|
2015-05-09 14:54:11 -04:00
|
|
|
assert.Equal(t, 2, len(firstSectionPages))
|
2015-05-20 02:21:21 -04:00
|
|
|
secondSectionPages := s.Sections["second-section"]
|
2015-05-09 14:54:11 -04:00
|
|
|
assert.Equal(t, 1, len(secondSectionPages))
|
2015-05-28 17:05:13 -04:00
|
|
|
fishySectionPages := s.Sections["fish-and-chips"]
|
|
|
|
assert.Equal(t, 1, len(fishySectionPages))
|
2015-05-09 14:54:11 -04:00
|
|
|
|
2015-05-28 17:05:13 -04:00
|
|
|
nodeFirst := s.newSectionListNode("First", "first", firstSectionPages)
|
|
|
|
nodeSecond := s.newSectionListNode("Second Section", "second-section", secondSectionPages)
|
|
|
|
nodeFishy := s.newSectionListNode("Fish and Chips", "fish-and-chips", fishySectionPages)
|
2015-05-20 02:21:21 -04:00
|
|
|
firstSectionMenuEntry := findTestMenuEntryByID(s, "spm", "first")
|
|
|
|
secondSectionMenuEntry := findTestMenuEntryByID(s, "spm", "second-section")
|
2015-05-28 17:05:13 -04:00
|
|
|
fishySectionMenuEntry := findTestMenuEntryByID(s, "spm", "Fish and Chips")
|
2015-05-09 14:54:11 -04:00
|
|
|
|
|
|
|
assert.NotNil(t, firstSectionMenuEntry)
|
|
|
|
assert.NotNil(t, secondSectionMenuEntry)
|
|
|
|
assert.NotNil(t, nodeFirst)
|
|
|
|
assert.NotNil(t, nodeSecond)
|
2015-05-28 17:05:13 -04:00
|
|
|
assert.NotNil(t, fishySectionMenuEntry)
|
|
|
|
assert.NotNil(t, nodeFishy)
|
|
|
|
|
|
|
|
assert.True(t, nodeFirst.IsMenuCurrent("spm", firstSectionMenuEntry))
|
|
|
|
assert.False(t, nodeFirst.IsMenuCurrent("spm", secondSectionMenuEntry))
|
|
|
|
assert.False(t, nodeFirst.IsMenuCurrent("spm", fishySectionMenuEntry))
|
|
|
|
assert.True(t, nodeFishy.IsMenuCurrent("spm", fishySectionMenuEntry))
|
|
|
|
assert.Equal(t, "Fish and Chips", fishySectionMenuEntry.Name)
|
2015-05-09 14:54:11 -04:00
|
|
|
|
|
|
|
for _, p := range firstSectionPages {
|
|
|
|
assert.True(t, p.Page.HasMenuCurrent("spm", firstSectionMenuEntry))
|
|
|
|
assert.False(t, p.Page.HasMenuCurrent("spm", secondSectionMenuEntry))
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, p := range secondSectionPages {
|
|
|
|
assert.False(t, p.Page.HasMenuCurrent("spm", firstSectionMenuEntry))
|
|
|
|
assert.True(t, p.Page.HasMenuCurrent("spm", secondSectionMenuEntry))
|
|
|
|
}
|
2015-05-28 17:05:13 -04:00
|
|
|
|
|
|
|
for _, p := range fishySectionPages {
|
|
|
|
assert.False(t, p.Page.HasMenuCurrent("spm", firstSectionMenuEntry))
|
|
|
|
assert.False(t, p.Page.HasMenuCurrent("spm", secondSectionMenuEntry))
|
|
|
|
assert.True(t, p.Page.HasMenuCurrent("spm", fishySectionMenuEntry))
|
|
|
|
}
|
2015-05-09 14:54:11 -04:00
|
|
|
}
|
|
|
|
|
2014-10-18 14:25:10 -04:00
|
|
|
func TestTaxonomyNodeMenu(t *testing.T) {
|
2015-05-20 02:21:21 -04:00
|
|
|
viper.Reset()
|
|
|
|
defer viper.Reset()
|
|
|
|
|
2015-03-11 13:34:57 -04:00
|
|
|
viper.Set("CanonifyURLs", true)
|
2016-03-22 18:59:07 -04:00
|
|
|
s := setupMenuTests(t, menuPageSources)
|
2014-10-18 14:25:10 -04:00
|
|
|
|
|
|
|
for i, this := range []struct {
|
|
|
|
menu string
|
|
|
|
taxInfo taxRenderInfo
|
|
|
|
menuItem *MenuEntry
|
|
|
|
isMenuCurrent bool
|
|
|
|
hasMenuCurrent bool
|
|
|
|
}{
|
|
|
|
{"tax", taxRenderInfo{key: "key", singular: "one", plural: "two"},
|
2015-05-20 02:21:21 -04:00
|
|
|
findTestMenuEntryByID(s, "tax", "1"), true, false},
|
2014-10-18 14:25:10 -04:00
|
|
|
{"tax", taxRenderInfo{key: "key", singular: "one", plural: "two"},
|
2015-05-20 02:21:21 -04:00
|
|
|
findTestMenuEntryByID(s, "tax", "2"), true, false},
|
2014-10-18 14:25:10 -04:00
|
|
|
{"tax", taxRenderInfo{key: "key", singular: "one", plural: "two"},
|
2015-03-18 01:16:54 -04:00
|
|
|
&MenuEntry{Name: "Somewhere else", URL: "/somewhereelse"}, false, false},
|
2014-10-18 14:25:10 -04:00
|
|
|
} {
|
|
|
|
|
2015-05-20 02:21:21 -04:00
|
|
|
n, _ := s.newTaxonomyNode(this.taxInfo)
|
2014-10-18 14:25:10 -04:00
|
|
|
|
|
|
|
isMenuCurrent := n.IsMenuCurrent(this.menu, this.menuItem)
|
|
|
|
hasMenuCurrent := n.HasMenuCurrent(this.menu, this.menuItem)
|
|
|
|
|
|
|
|
if isMenuCurrent != this.isMenuCurrent {
|
|
|
|
t.Errorf("[%d] Wrong result from IsMenuCurrent: %v", i, isMenuCurrent)
|
|
|
|
}
|
|
|
|
|
|
|
|
if hasMenuCurrent != this.hasMenuCurrent {
|
|
|
|
t.Errorf("[%d] Wrong result for menuItem %v for HasMenuCurrent: %v", i, this.menuItem, hasMenuCurrent)
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2015-05-20 02:21:21 -04:00
|
|
|
menuEntryXML := findTestMenuEntryByID(s, "tax", "xml")
|
2014-10-18 14:25:10 -04:00
|
|
|
|
2015-03-18 01:16:54 -04:00
|
|
|
if strings.HasSuffix(menuEntryXML.URL, "/") {
|
2014-10-18 14:25:10 -04:00
|
|
|
t.Error("RSS menu item should not be padded with trailing slash")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-02-06 16:53:59 -05:00
|
|
|
func TestMenuLimit(t *testing.T) {
|
|
|
|
viper.Reset()
|
|
|
|
defer viper.Reset()
|
|
|
|
|
2016-03-22 18:59:07 -04:00
|
|
|
s := setupMenuTests(t, menuPageSources)
|
2016-02-06 16:53:59 -05:00
|
|
|
m := *s.Menus["main"]
|
|
|
|
|
|
|
|
// main menu has 4 entries
|
|
|
|
firstTwo := m.Limit(2)
|
|
|
|
assert.Equal(t, 2, len(firstTwo))
|
|
|
|
for i := 0; i < 2; i++ {
|
|
|
|
assert.Equal(t, m[i], firstTwo[i])
|
|
|
|
}
|
|
|
|
assert.Equal(t, m, m.Limit(4))
|
|
|
|
assert.Equal(t, m, m.Limit(5))
|
|
|
|
}
|
|
|
|
|
2016-02-07 06:34:43 -05:00
|
|
|
func TestMenuSortByN(t *testing.T) {
|
|
|
|
|
|
|
|
for i, this := range []struct {
|
|
|
|
sortFunc func(p Menu) Menu
|
|
|
|
assertFunc func(p Menu) bool
|
|
|
|
}{
|
|
|
|
{(Menu).Sort, func(p Menu) bool { return p[0].Weight == 1 && p[1].Name == "nx" && p[2].Identifier == "ib" }},
|
|
|
|
{(Menu).ByWeight, func(p Menu) bool { return p[0].Weight == 1 && p[1].Name == "nx" && p[2].Identifier == "ib" }},
|
|
|
|
{(Menu).ByName, func(p Menu) bool { return p[0].Name == "na" }},
|
|
|
|
{(Menu).Reverse, func(p Menu) bool { return p[0].Identifier == "ib" && p[len(p)-1].Identifier == "ia" }},
|
|
|
|
} {
|
|
|
|
menu := Menu{&MenuEntry{Weight: 3, Name: "nb", Identifier: "ia"},
|
|
|
|
&MenuEntry{Weight: 1, Name: "na", Identifier: "ic"},
|
|
|
|
&MenuEntry{Weight: 1, Name: "nx", Identifier: "ic"},
|
|
|
|
&MenuEntry{Weight: 2, Name: "nb", Identifier: "ix"},
|
|
|
|
&MenuEntry{Weight: 2, Name: "nb", Identifier: "ib"}}
|
|
|
|
|
|
|
|
sorted := this.sortFunc(menu)
|
|
|
|
|
|
|
|
if !this.assertFunc(sorted) {
|
|
|
|
t.Errorf("[%d] sort error", i)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2014-10-18 14:25:10 -04:00
|
|
|
func TestHomeNodeMenu(t *testing.T) {
|
2015-05-20 02:21:21 -04:00
|
|
|
viper.Reset()
|
|
|
|
defer viper.Reset()
|
|
|
|
|
|
|
|
viper.Set("CanonifyURLs", true)
|
|
|
|
viper.Set("UglyURLs", true)
|
|
|
|
|
2016-03-22 18:59:07 -04:00
|
|
|
s := setupMenuTests(t, menuPageSources)
|
2014-10-18 14:25:10 -04:00
|
|
|
|
2015-05-20 02:21:21 -04:00
|
|
|
home := s.newHomeNode()
|
2015-03-18 01:16:54 -04:00
|
|
|
homeMenuEntry := &MenuEntry{Name: home.Title, URL: home.URL}
|
2014-10-18 14:25:10 -04:00
|
|
|
|
|
|
|
for i, this := range []struct {
|
|
|
|
menu string
|
|
|
|
menuItem *MenuEntry
|
|
|
|
isMenuCurrent bool
|
|
|
|
hasMenuCurrent bool
|
|
|
|
}{
|
|
|
|
{"main", homeMenuEntry, true, false},
|
|
|
|
{"doesnotexist", homeMenuEntry, false, false},
|
2015-03-18 01:16:54 -04:00
|
|
|
{"main", &MenuEntry{Name: "Somewhere else", URL: "/somewhereelse"}, false, false},
|
2015-07-13 16:07:04 -04:00
|
|
|
{"grandparent", findTestMenuEntryByID(s, "grandparent", "grandparentId"), false, true},
|
2015-05-20 02:21:21 -04:00
|
|
|
{"grandparent", findTestMenuEntryByID(s, "grandparent", "parentId"), false, true},
|
|
|
|
{"grandparent", findTestMenuEntryByID(s, "grandparent", "grandchildId"), true, false},
|
2014-10-18 14:25:10 -04:00
|
|
|
} {
|
|
|
|
|
|
|
|
isMenuCurrent := home.IsMenuCurrent(this.menu, this.menuItem)
|
|
|
|
hasMenuCurrent := home.HasMenuCurrent(this.menu, this.menuItem)
|
|
|
|
|
|
|
|
if isMenuCurrent != this.isMenuCurrent {
|
2015-05-20 02:21:21 -04:00
|
|
|
fmt.Println("isMenuCurrent", isMenuCurrent)
|
2016-05-08 07:15:58 -04:00
|
|
|
fmt.Printf("this: %#v\n", this)
|
2015-05-20 02:21:21 -04:00
|
|
|
t.Errorf("[%d] Wrong result from IsMenuCurrent: %v for %q", i, isMenuCurrent, this.menu)
|
2014-10-18 14:25:10 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if hasMenuCurrent != this.hasMenuCurrent {
|
2015-05-20 02:21:21 -04:00
|
|
|
fmt.Println("hasMenuCurrent", hasMenuCurrent)
|
2016-05-08 07:15:58 -04:00
|
|
|
fmt.Printf("this: %#v\n", this)
|
2015-05-20 02:21:21 -04:00
|
|
|
t.Errorf("[%d] Wrong result for menu %q menuItem %v for HasMenuCurrent: %v", i, this.menu, this.menuItem, hasMenuCurrent)
|
2014-10-18 14:25:10 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-02-07 07:43:42 -05:00
|
|
|
func TestHopefullyUniqueID(t *testing.T) {
|
|
|
|
assert.Equal(t, "i", (&MenuEntry{Identifier: "i", URL: "u", Name: "n"}).hopefullyUniqueID())
|
|
|
|
assert.Equal(t, "u", (&MenuEntry{Identifier: "", URL: "u", Name: "n"}).hopefullyUniqueID())
|
|
|
|
assert.Equal(t, "n", (&MenuEntry{Identifier: "", URL: "", Name: "n"}).hopefullyUniqueID())
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestAddMenuEntryChild(t *testing.T) {
|
|
|
|
root := &MenuEntry{Weight: 1}
|
2016-03-22 19:29:39 -04:00
|
|
|
root.addChild(&MenuEntry{Weight: 2})
|
|
|
|
root.addChild(&MenuEntry{Weight: 1})
|
2016-02-07 07:43:42 -05:00
|
|
|
assert.Equal(t, 2, len(root.Children))
|
|
|
|
assert.Equal(t, 1, root.Children[0].Weight)
|
|
|
|
}
|
|
|
|
|
2014-10-18 14:25:10 -04:00
|
|
|
var testMenuIdentityMatcher = func(me *MenuEntry, id string) bool { return me.Identifier == id }
|
|
|
|
var testMenuNameMatcher = func(me *MenuEntry, id string) bool { return me.Name == id }
|
|
|
|
|
2015-05-20 02:21:21 -04:00
|
|
|
func findTestMenuEntryByID(s *Site, mn string, id string) *MenuEntry {
|
|
|
|
return findTestMenuEntry(s, mn, id, testMenuIdentityMatcher)
|
2014-10-18 14:25:10 -04:00
|
|
|
}
|
2015-05-20 02:21:21 -04:00
|
|
|
func findTestMenuEntryByName(s *Site, mn string, id string) *MenuEntry {
|
|
|
|
return findTestMenuEntry(s, mn, id, testMenuNameMatcher)
|
2014-10-18 14:25:10 -04:00
|
|
|
}
|
|
|
|
|
2015-05-20 02:21:21 -04:00
|
|
|
func findTestMenuEntry(s *Site, mn string, id string, matcher func(me *MenuEntry, id string) bool) *MenuEntry {
|
2015-03-07 06:52:29 -05:00
|
|
|
var found *MenuEntry
|
2015-05-20 02:21:21 -04:00
|
|
|
if menu, ok := s.Menus[mn]; ok {
|
2014-10-18 14:25:10 -04:00
|
|
|
for _, me := range *menu {
|
|
|
|
|
|
|
|
if matcher(me, id) {
|
2015-01-22 11:23:01 -05:00
|
|
|
if found != nil {
|
|
|
|
panic(fmt.Sprintf("Duplicate menu entry in menu %s with id/name %s", mn, id))
|
|
|
|
}
|
|
|
|
found = me
|
2014-10-18 14:25:10 -04:00
|
|
|
}
|
|
|
|
|
2015-05-20 02:21:21 -04:00
|
|
|
descendant := findDescendantTestMenuEntry(me, id, matcher)
|
2014-10-18 14:25:10 -04:00
|
|
|
if descendant != nil {
|
2015-01-22 11:23:01 -05:00
|
|
|
if found != nil {
|
|
|
|
panic(fmt.Sprintf("Duplicate menu entry in menu %s with id/name %s", mn, id))
|
|
|
|
}
|
|
|
|
found = descendant
|
2014-10-18 14:25:10 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-01-22 11:23:01 -05:00
|
|
|
return found
|
2014-10-18 14:25:10 -04:00
|
|
|
}
|
|
|
|
|
2015-05-20 02:21:21 -04:00
|
|
|
func findDescendantTestMenuEntry(parent *MenuEntry, id string, matcher func(me *MenuEntry, id string) bool) *MenuEntry {
|
2015-03-07 06:52:29 -05:00
|
|
|
var found *MenuEntry
|
2014-10-18 14:25:10 -04:00
|
|
|
if parent.HasChildren() {
|
|
|
|
for _, child := range parent.Children {
|
|
|
|
|
|
|
|
if matcher(child, id) {
|
2015-01-22 11:23:01 -05:00
|
|
|
if found != nil {
|
|
|
|
panic(fmt.Sprintf("Duplicate menu entry in menuitem %s with id/name %s", parent.KeyName(), id))
|
|
|
|
}
|
|
|
|
found = child
|
2014-10-18 14:25:10 -04:00
|
|
|
}
|
|
|
|
|
2015-05-20 02:21:21 -04:00
|
|
|
descendant := findDescendantTestMenuEntry(child, id, matcher)
|
2014-10-18 14:25:10 -04:00
|
|
|
if descendant != nil {
|
2015-01-22 11:23:01 -05:00
|
|
|
if found != nil {
|
|
|
|
panic(fmt.Sprintf("Duplicate menu entry in menuitem %s with id/name %s", parent.KeyName(), id))
|
|
|
|
}
|
|
|
|
found = descendant
|
2014-10-18 14:25:10 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-01-22 11:23:01 -05:00
|
|
|
return found
|
2014-10-18 14:25:10 -04:00
|
|
|
}
|
|
|
|
|
2015-05-20 02:21:21 -04:00
|
|
|
func setupTestMenuState(s *Site, t *testing.T) {
|
2016-03-22 18:59:07 -04:00
|
|
|
menus, err := tomlToMap(confMenu1)
|
2014-10-18 14:25:10 -04:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Unable to Read menus: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
viper.Set("menu", menus["menu"])
|
Move s.prepUrl() before prepending BaseUrl.
When we have an absolute menu url specified in the config file
(e.g., `menu: { main: { name: "News", url: "/news" } }`),
its menu entry is generated by prefixing it with the BaseUrl.
The result is then run through prepUrl(), which uses helpers.Urlize to
convert urls such as 'My First Link' to 'my-first-link'.
The behaviour is backwards: we do not want to run helpers.Urlize on the
BaseUrl, only on the absolute component. Currently, a BaseUrl such as
'http://my.edu/ENG101' will be converted to 'http://my.edu/eng101',
resulting in broken links in all of my menus.
This commit switches the URL prep and BaseUrl prepending actions around. I
would argue that these URLs shouldn't be run through prepUrl anyway
because the site developer has specified them explicitly in a config file
and might be surprised for, e.g., URLs to change case, but that's another
commit for another time.
2015-01-25 06:39:28 -05:00
|
|
|
viper.Set("baseurl", "http://foo.local/Zoo/")
|
2014-10-18 14:25:10 -04:00
|
|
|
}
|
|
|
|
|
2015-05-20 02:21:21 -04:00
|
|
|
func setupMenuTests(t *testing.T, pageSources []source.ByteSource) *Site {
|
2015-01-22 11:23:01 -05:00
|
|
|
s := createTestSite(pageSources)
|
2015-05-20 02:21:21 -04:00
|
|
|
setupTestMenuState(s, t)
|
2014-10-18 14:25:10 -04:00
|
|
|
testSiteSetup(s, t)
|
|
|
|
|
2015-05-20 02:21:21 -04:00
|
|
|
return s
|
2014-10-18 14:25:10 -04:00
|
|
|
}
|
|
|
|
|
2015-01-22 11:23:01 -05:00
|
|
|
func createTestSite(pageSources []source.ByteSource) *Site {
|
2016-03-21 19:28:42 -04:00
|
|
|
hugofs.InitMemFs()
|
2014-10-18 14:25:10 -04:00
|
|
|
|
|
|
|
s := &Site{
|
2015-01-22 11:23:01 -05:00
|
|
|
Source: &source.InMemorySource{ByteSource: pageSources},
|
2014-10-18 14:25:10 -04:00
|
|
|
}
|
|
|
|
return s
|
|
|
|
}
|
|
|
|
|
|
|
|
func testSiteSetup(s *Site, t *testing.T) {
|
|
|
|
s.Menus = Menus{}
|
|
|
|
s.initializeSiteInfo()
|
|
|
|
|
2016-04-07 14:03:03 -04:00
|
|
|
if err := s.createPages(); err != nil {
|
2014-10-18 14:25:10 -04:00
|
|
|
t.Fatalf("Unable to create pages: %s", err)
|
|
|
|
}
|
|
|
|
|
2016-04-07 14:03:03 -04:00
|
|
|
if err := s.buildSiteMeta(); err != nil {
|
2014-10-18 14:25:10 -04:00
|
|
|
t.Fatalf("Unable to build site metadata: %s", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func tomlToMap(s string) (map[string]interface{}, error) {
|
2015-03-07 06:52:29 -05:00
|
|
|
var data = make(map[string]interface{})
|
2014-10-18 14:25:10 -04:00
|
|
|
if _, err := toml.Decode(s, &data); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return data, nil
|
|
|
|
|
|
|
|
}
|