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"
|
|
|
|
|
2014-10-18 14:25:10 -04:00
|
|
|
"github.com/BurntSushi/toml"
|
2014-11-04 00:41:47 -05:00
|
|
|
"github.com/spf13/afero"
|
|
|
|
"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"
|
2015-01-06 12:11:06 -05:00
|
|
|
"path/filepath"
|
2014-10-18 14:25:10 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
CONF_MENU1 = `
|
|
|
|
[[menu.main]]
|
|
|
|
name = "Go Home"
|
|
|
|
url = "/"
|
|
|
|
weight = 1
|
|
|
|
pre = "<div>"
|
|
|
|
post = "</div>"
|
|
|
|
[[menu.main]]
|
|
|
|
name = "Blog"
|
|
|
|
url = "/posts"
|
|
|
|
[[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"
|
|
|
|
url = "/two/key"
|
|
|
|
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
|
|
|
)
|
|
|
|
|
|
|
|
var MENU_PAGE_1 = []byte(`+++
|
|
|
|
title = "One"
|
|
|
|
[menu]
|
|
|
|
[menu.p_one]
|
|
|
|
weight = 1
|
|
|
|
+++
|
|
|
|
Front Matter with Menu Pages`)
|
|
|
|
|
|
|
|
var MENU_PAGE_2 = []byte(`+++
|
|
|
|
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`)
|
|
|
|
|
|
|
|
var MENU_PAGE_3 = []byte(`+++
|
|
|
|
title = "Three"
|
|
|
|
weight = 3
|
|
|
|
[menu]
|
|
|
|
[menu.p_two]
|
|
|
|
Name = "Three"
|
|
|
|
Parent = "Two"
|
|
|
|
+++
|
|
|
|
Front Matter with Menu Pages`)
|
|
|
|
|
|
|
|
var MENU_PAGE_SOURCES = []source.ByteSource{
|
2015-01-06 12:11:06 -05:00
|
|
|
{filepath.FromSlash("sect/doc1.md"), MENU_PAGE_1},
|
|
|
|
{filepath.FromSlash("sect/doc2.md"), MENU_PAGE_2},
|
|
|
|
{filepath.FromSlash("sect/doc3.md"), MENU_PAGE_3},
|
|
|
|
}
|
|
|
|
|
|
|
|
var MENU_PAGE_SECTIONS_SOURCES = []source.ByteSource{
|
|
|
|
{filepath.FromSlash("first/doc1.md"), MENU_PAGE_1},
|
|
|
|
{filepath.FromSlash("first/doc2.md"), MENU_PAGE_2},
|
|
|
|
{filepath.FromSlash("second-section/doc3.md"), MENU_PAGE_3},
|
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))
|
|
|
|
}
|
|
|
|
|
2014-10-18 14:25:10 -04:00
|
|
|
type testMenuState struct {
|
|
|
|
site *Site
|
|
|
|
oldMenu interface{}
|
2015-03-11 13:34:57 -04:00
|
|
|
oldBaseURL interface{}
|
2014-10-18 14:25:10 -04:00
|
|
|
}
|
|
|
|
|
2015-01-22 11:23:01 -05:00
|
|
|
// 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) {
|
|
|
|
|
|
|
|
ts := setupMenuTests(t, menuPageSources)
|
|
|
|
defer resetMenuTestState(ts)
|
|
|
|
|
|
|
|
assert.Equal(t, 3, len(ts.site.Pages), "Not enough pages")
|
|
|
|
|
2015-03-11 13:34:57 -04:00
|
|
|
me1 := ts.findTestMenuEntryByID("m1", "i1")
|
|
|
|
me2 := ts.findTestMenuEntryByID("m1", "i2")
|
2015-01-22 11:23:01 -05:00
|
|
|
|
|
|
|
assert.NotNil(t, me1)
|
|
|
|
assert.NotNil(t, me2)
|
|
|
|
|
2015-03-18 01:16:54 -04:00
|
|
|
assert.True(t, strings.Contains(me1.URL, "doc1"))
|
|
|
|
assert.True(t, strings.Contains(me2.URL, "doc2"))
|
2015-01-22 11:23:01 -05:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
// Issue 817 contd - name should be second identifier in
|
|
|
|
func TestPageMenuWithDuplicateName(t *testing.T) {
|
|
|
|
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) {
|
|
|
|
ts := setupMenuTests(t, menuPageSources)
|
|
|
|
defer resetMenuTestState(ts)
|
|
|
|
|
|
|
|
assert.Equal(t, 3, len(ts.site.Pages), "Not enough pages")
|
|
|
|
|
|
|
|
me1 := ts.findTestMenuEntryByName("m1", "n1")
|
|
|
|
me2 := ts.findTestMenuEntryByName("m1", "n2")
|
|
|
|
|
|
|
|
assert.NotNil(t, me1)
|
|
|
|
assert.NotNil(t, me2)
|
|
|
|
|
2015-03-18 01:16:54 -04:00
|
|
|
assert.True(t, strings.Contains(me1.URL, "doc1"))
|
|
|
|
assert.True(t, strings.Contains(me2.URL, "doc2"))
|
2015-01-22 11:23:01 -05:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2015-01-01 17:40:14 -05:00
|
|
|
func TestPageMenu(t *testing.T) {
|
2015-01-22 11:23:01 -05:00
|
|
|
ts := setupMenuTests(t, MENU_PAGE_SOURCES)
|
2014-10-18 14:25:10 -04:00
|
|
|
defer resetMenuTestState(ts)
|
|
|
|
|
|
|
|
if len(ts.site.Pages) != 3 {
|
|
|
|
t.Fatalf("Posts not created, expected 3 got %d", len(ts.site.Pages))
|
|
|
|
}
|
|
|
|
|
|
|
|
first := ts.site.Pages[0]
|
|
|
|
second := ts.site.Pages[1]
|
|
|
|
third := ts.site.Pages[2]
|
|
|
|
|
|
|
|
pOne := ts.findTestMenuEntryByName("p_one", "One")
|
2015-03-11 13:34:57 -04:00
|
|
|
pTwo := ts.findTestMenuEntryByID("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)
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2015-02-10 13:13:45 -05:00
|
|
|
// issue #888
|
2015-03-11 13:34:57 -04:00
|
|
|
func TestMenuWithHashInURL(t *testing.T) {
|
2015-02-10 13:13:45 -05:00
|
|
|
ts := setupMenuTests(t, MENU_PAGE_SOURCES)
|
|
|
|
defer resetMenuTestState(ts)
|
|
|
|
|
2015-03-11 13:34:57 -04:00
|
|
|
me := ts.findTestMenuEntryByID("hash", "hash")
|
2015-02-10 13:13:45 -05:00
|
|
|
|
|
|
|
assert.NotNil(t, me)
|
|
|
|
|
2015-03-18 01:16:54 -04:00
|
|
|
assert.Equal(t, "/Zoo/resource/#anchor", me.URL)
|
2015-02-10 13:13:45 -05:00
|
|
|
}
|
|
|
|
|
2014-12-16 06:08:16 -05:00
|
|
|
// issue #719
|
2015-03-11 13:34:57 -04:00
|
|
|
func TestMenuWithUnicodeURLs(t *testing.T) {
|
|
|
|
for _, uglyURLs := range []bool{true, false} {
|
|
|
|
for _, canonifyURLs := range []bool{true, false} {
|
|
|
|
doTestMenuWithUnicodeURLs(t, canonifyURLs, uglyURLs)
|
2014-12-12 14:28:28 -05:00
|
|
|
}
|
2014-12-16 06:08:16 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-11 13:34:57 -04:00
|
|
|
func doTestMenuWithUnicodeURLs(t *testing.T, canonifyURLs, uglyURLs bool) {
|
|
|
|
viper.Set("CanonifyURLs", canonifyURLs)
|
|
|
|
viper.Set("UglyURLs", uglyURLs)
|
2014-12-12 14:28:28 -05:00
|
|
|
|
2015-01-22 11:23:01 -05:00
|
|
|
ts := setupMenuTests(t, MENU_PAGE_SOURCES)
|
2014-12-16 06:08:16 -05:00
|
|
|
defer resetMenuTestState(ts)
|
|
|
|
|
2015-03-11 13:34:57 -04:00
|
|
|
unicodeRussian := ts.findTestMenuEntryByID("unicode", "unicode-russian")
|
2014-12-16 06:08:16 -05:00
|
|
|
|
2014-12-12 14:28:28 -05:00
|
|
|
expectedBase := "/%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"
|
|
|
|
|
2015-03-11 13:34:57 -04:00
|
|
|
if !canonifyURLs {
|
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
|
|
|
expectedBase = "/Zoo" + expectedBase
|
2014-12-12 14:28:28 -05:00
|
|
|
}
|
|
|
|
|
2014-12-16 06:08:16 -05:00
|
|
|
var expected string
|
2015-03-11 13:34:57 -04:00
|
|
|
if uglyURLs {
|
2014-12-16 06:08:16 -05:00
|
|
|
expected = expectedBase + ".html"
|
|
|
|
} else {
|
|
|
|
expected = expectedBase + "/"
|
|
|
|
}
|
|
|
|
|
2015-03-18 01:16:54 -04:00
|
|
|
assert.Equal(t, expected, unicodeRussian.URL, "uglyURLs[%t]", uglyURLs)
|
2014-12-16 06:08:16 -05:00
|
|
|
}
|
|
|
|
|
2014-10-18 14:25:10 -04:00
|
|
|
func TestTaxonomyNodeMenu(t *testing.T) {
|
2015-03-11 13:34:57 -04:00
|
|
|
viper.Set("CanonifyURLs", true)
|
2015-01-22 11:23:01 -05:00
|
|
|
ts := setupMenuTests(t, MENU_PAGE_SOURCES)
|
2014-10-18 14:25:10 -04:00
|
|
|
defer resetMenuTestState(ts)
|
|
|
|
|
|
|
|
for i, this := range []struct {
|
|
|
|
menu string
|
|
|
|
taxInfo taxRenderInfo
|
|
|
|
menuItem *MenuEntry
|
|
|
|
isMenuCurrent bool
|
|
|
|
hasMenuCurrent bool
|
|
|
|
}{
|
|
|
|
{"tax", taxRenderInfo{key: "key", singular: "one", plural: "two"},
|
2015-03-11 13:34:57 -04:00
|
|
|
ts.findTestMenuEntryByID("tax", "1"), true, false},
|
2014-10-18 14:25:10 -04:00
|
|
|
{"tax", taxRenderInfo{key: "key", singular: "one", plural: "two"},
|
2015-03-11 13:34:57 -04:00
|
|
|
ts.findTestMenuEntryByID("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
|
|
|
} {
|
|
|
|
|
|
|
|
n, _ := ts.site.newTaxonomyNode(this.taxInfo)
|
|
|
|
|
|
|
|
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-03-11 13:34:57 -04:00
|
|
|
menuEntryXML := ts.findTestMenuEntryByID("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")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestHomeNodeMenu(t *testing.T) {
|
2015-01-22 11:23:01 -05:00
|
|
|
ts := setupMenuTests(t, MENU_PAGE_SOURCES)
|
2014-10-18 14:25:10 -04:00
|
|
|
defer resetMenuTestState(ts)
|
|
|
|
|
|
|
|
home := ts.site.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-03-11 13:34:57 -04:00
|
|
|
{"grandparent", ts.findTestMenuEntryByID("grandparent", "grandparentId"), false, false},
|
|
|
|
{"grandparent", ts.findTestMenuEntryByID("grandparent", "parentId"), false, true},
|
|
|
|
{"grandparent", ts.findTestMenuEntryByID("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 {
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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-03-11 13:34:57 -04:00
|
|
|
func (ts testMenuState) findTestMenuEntryByID(mn string, id string) *MenuEntry {
|
2014-10-18 14:25:10 -04:00
|
|
|
return ts.findTestMenuEntry(mn, id, testMenuIdentityMatcher)
|
|
|
|
}
|
|
|
|
func (ts testMenuState) findTestMenuEntryByName(mn string, id string) *MenuEntry {
|
|
|
|
return ts.findTestMenuEntry(mn, id, testMenuNameMatcher)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ts testMenuState) findTestMenuEntry(mn string, 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 menu, ok := ts.site.Menus[mn]; ok {
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
descendant := ts.findDescendantTestMenuEntry(me, id, matcher)
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
func (ts testMenuState) 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
|
|
|
}
|
|
|
|
|
|
|
|
descendant := ts.findDescendantTestMenuEntry(child, id, matcher)
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
func getTestMenuState(s *Site, t *testing.T) *testMenuState {
|
2015-03-11 13:34:57 -04:00
|
|
|
menuState := &testMenuState{site: s, oldBaseURL: viper.Get("baseurl"), oldMenu: viper.Get("menu")}
|
2014-10-18 14:25:10 -04:00
|
|
|
|
|
|
|
menus, err := tomlToMap(CONF_MENU1)
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
return menuState
|
|
|
|
}
|
|
|
|
|
2015-01-22 11:23:01 -05:00
|
|
|
func setupMenuTests(t *testing.T, pageSources []source.ByteSource) *testMenuState {
|
|
|
|
s := createTestSite(pageSources)
|
2014-10-18 14:25:10 -04:00
|
|
|
testState := getTestMenuState(s, t)
|
|
|
|
testSiteSetup(s, t)
|
|
|
|
|
|
|
|
return testState
|
|
|
|
}
|
|
|
|
|
|
|
|
func resetMenuTestState(state *testMenuState) {
|
|
|
|
viper.Set("menu", state.oldMenu)
|
2015-03-11 13:34:57 -04:00
|
|
|
viper.Set("baseurl", state.oldBaseURL)
|
2014-10-18 14:25:10 -04:00
|
|
|
}
|
|
|
|
|
2015-01-22 11:23:01 -05:00
|
|
|
func createTestSite(pageSources []source.ByteSource) *Site {
|
2014-11-04 00:41:47 -05:00
|
|
|
hugofs.DestinationFS = new(afero.MemMapFs)
|
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()
|
|
|
|
|
|
|
|
if err := s.CreatePages(); err != nil {
|
|
|
|
t.Fatalf("Unable to create pages: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := s.BuildSiteMeta(); err != nil {
|
|
|
|
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
|
|
|
|
|
|
|
|
}
|