github.com/olliephillips/hugo@v0.42.2/hugolib/pagemeta/page_frontmatter_test.go (about) 1 // Copyright 2018 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 pagemeta 15 16 import ( 17 "fmt" 18 "strings" 19 "testing" 20 "time" 21 22 "github.com/spf13/viper" 23 24 "github.com/stretchr/testify/require" 25 ) 26 27 func TestDateAndSlugFromBaseFilename(t *testing.T) { 28 29 t.Parallel() 30 31 assert := require.New(t) 32 33 tests := []struct { 34 name string 35 date string 36 slug string 37 }{ 38 {"page.md", "0001-01-01", ""}, 39 {"2012-09-12-page.md", "2012-09-12", "page"}, 40 {"2018-02-28-page.md", "2018-02-28", "page"}, 41 {"2018-02-28_page.md", "2018-02-28", "page"}, 42 {"2018-02-28 page.md", "2018-02-28", "page"}, 43 {"2018-02-28page.md", "2018-02-28", "page"}, 44 {"2018-02-28-.md", "2018-02-28", ""}, 45 {"2018-02-28-.md", "2018-02-28", ""}, 46 {"2018-02-28.md", "2018-02-28", ""}, 47 {"2018-02-28-page", "2018-02-28", "page"}, 48 {"2012-9-12-page.md", "0001-01-01", ""}, 49 {"asdfasdf.md", "0001-01-01", ""}, 50 } 51 52 for i, test := range tests { 53 expectedDate, err := time.Parse("2006-01-02", test.date) 54 assert.NoError(err) 55 56 errMsg := fmt.Sprintf("Test %d", i) 57 gotDate, gotSlug := dateAndSlugFromBaseFilename(test.name) 58 59 assert.Equal(expectedDate, gotDate, errMsg) 60 assert.Equal(test.slug, gotSlug, errMsg) 61 62 } 63 } 64 65 func newTestFd() *FrontMatterDescriptor { 66 return &FrontMatterDescriptor{ 67 Frontmatter: make(map[string]interface{}), 68 Params: make(map[string]interface{}), 69 Dates: &PageDates{}, 70 PageURLs: &URLPath{}, 71 } 72 } 73 74 func TestFrontMatterNewConfig(t *testing.T) { 75 assert := require.New(t) 76 77 cfg := viper.New() 78 79 cfg.Set("frontmatter", map[string]interface{}{ 80 "date": []string{"publishDate", "LastMod"}, 81 "Lastmod": []string{"publishDate"}, 82 "expiryDate": []string{"lastMod"}, 83 "publishDate": []string{"date"}, 84 }) 85 86 fc, err := newFrontmatterConfig(cfg) 87 assert.NoError(err) 88 assert.Equal([]string{"publishdate", "pubdate", "published", "lastmod", "modified"}, fc.date) 89 assert.Equal([]string{"publishdate", "pubdate", "published"}, fc.lastmod) 90 assert.Equal([]string{"lastmod", "modified"}, fc.expiryDate) 91 assert.Equal([]string{"date"}, fc.publishDate) 92 93 // Default 94 cfg = viper.New() 95 fc, err = newFrontmatterConfig(cfg) 96 assert.NoError(err) 97 assert.Equal([]string{"date", "publishdate", "pubdate", "published", "lastmod", "modified"}, fc.date) 98 assert.Equal([]string{":git", "lastmod", "modified", "date", "publishdate", "pubdate", "published"}, fc.lastmod) 99 assert.Equal([]string{"expirydate", "unpublishdate"}, fc.expiryDate) 100 assert.Equal([]string{"publishdate", "pubdate", "published", "date"}, fc.publishDate) 101 102 // :default keyword 103 cfg.Set("frontmatter", map[string]interface{}{ 104 "date": []string{"d1", ":default"}, 105 "lastmod": []string{"d2", ":default"}, 106 "expiryDate": []string{"d3", ":default"}, 107 "publishDate": []string{"d4", ":default"}, 108 }) 109 fc, err = newFrontmatterConfig(cfg) 110 assert.NoError(err) 111 assert.Equal([]string{"d1", "date", "publishdate", "pubdate", "published", "lastmod", "modified"}, fc.date) 112 assert.Equal([]string{"d2", ":git", "lastmod", "modified", "date", "publishdate", "pubdate", "published"}, fc.lastmod) 113 assert.Equal([]string{"d3", "expirydate", "unpublishdate"}, fc.expiryDate) 114 assert.Equal([]string{"d4", "publishdate", "pubdate", "published", "date"}, fc.publishDate) 115 116 } 117 118 func TestFrontMatterDatesHandlers(t *testing.T) { 119 assert := require.New(t) 120 121 for _, handlerID := range []string{":filename", ":fileModTime", ":git"} { 122 123 cfg := viper.New() 124 125 cfg.Set("frontmatter", map[string]interface{}{ 126 "date": []string{handlerID, "date"}, 127 }) 128 129 handler, err := NewFrontmatterHandler(nil, cfg) 130 assert.NoError(err) 131 132 d1, _ := time.Parse("2006-01-02", "2018-02-01") 133 d2, _ := time.Parse("2006-01-02", "2018-02-02") 134 135 d := newTestFd() 136 switch strings.ToLower(handlerID) { 137 case ":filename": 138 d.BaseFilename = "2018-02-01-page.md" 139 case ":filemodtime": 140 d.ModTime = d1 141 case ":git": 142 d.GitAuthorDate = d1 143 } 144 d.Frontmatter["date"] = d2 145 assert.NoError(handler.HandleDates(d)) 146 assert.Equal(d1, d.Dates.Date) 147 assert.Equal(d2, d.Params["date"]) 148 149 d = newTestFd() 150 d.Frontmatter["date"] = d2 151 assert.NoError(handler.HandleDates(d)) 152 assert.Equal(d2, d.Dates.Date) 153 assert.Equal(d2, d.Params["date"]) 154 155 } 156 } 157 158 func TestFrontMatterDatesCustomConfig(t *testing.T) { 159 t.Parallel() 160 161 assert := require.New(t) 162 163 cfg := viper.New() 164 cfg.Set("frontmatter", map[string]interface{}{ 165 "date": []string{"mydate"}, 166 "lastmod": []string{"publishdate"}, 167 "publishdate": []string{"publishdate"}, 168 }) 169 170 handler, err := NewFrontmatterHandler(nil, cfg) 171 assert.NoError(err) 172 173 testDate, err := time.Parse("2006-01-02", "2018-02-01") 174 assert.NoError(err) 175 176 d := newTestFd() 177 d.Frontmatter["mydate"] = testDate 178 testDate = testDate.Add(24 * time.Hour) 179 d.Frontmatter["date"] = testDate 180 testDate = testDate.Add(24 * time.Hour) 181 d.Frontmatter["lastmod"] = testDate 182 testDate = testDate.Add(24 * time.Hour) 183 d.Frontmatter["publishdate"] = testDate 184 testDate = testDate.Add(24 * time.Hour) 185 d.Frontmatter["expirydate"] = testDate 186 187 assert.NoError(handler.HandleDates(d)) 188 189 assert.Equal(1, d.Dates.Date.Day()) 190 assert.Equal(4, d.Dates.Lastmod.Day()) 191 assert.Equal(4, d.Dates.PublishDate.Day()) 192 assert.Equal(5, d.Dates.ExpiryDate.Day()) 193 194 assert.Equal(d.Dates.Date, d.Params["date"]) 195 assert.Equal(d.Dates.Date, d.Params["mydate"]) 196 assert.Equal(d.Dates.PublishDate, d.Params["publishdate"]) 197 assert.Equal(d.Dates.ExpiryDate, d.Params["expirydate"]) 198 199 assert.False(handler.IsDateKey("date")) // This looks odd, but is configured like this. 200 assert.True(handler.IsDateKey("mydate")) 201 assert.True(handler.IsDateKey("publishdate")) 202 assert.True(handler.IsDateKey("pubdate")) 203 204 } 205 206 func TestFrontMatterDatesDefaultKeyword(t *testing.T) { 207 t.Parallel() 208 209 assert := require.New(t) 210 211 cfg := viper.New() 212 213 cfg.Set("frontmatter", map[string]interface{}{ 214 "date": []string{"mydate", ":default"}, 215 "publishdate": []string{":default", "mypubdate"}, 216 }) 217 218 handler, err := NewFrontmatterHandler(nil, cfg) 219 assert.NoError(err) 220 221 testDate, _ := time.Parse("2006-01-02", "2018-02-01") 222 d := newTestFd() 223 d.Frontmatter["mydate"] = testDate 224 d.Frontmatter["date"] = testDate.Add(1 * 24 * time.Hour) 225 d.Frontmatter["mypubdate"] = testDate.Add(2 * 24 * time.Hour) 226 d.Frontmatter["publishdate"] = testDate.Add(3 * 24 * time.Hour) 227 228 assert.NoError(handler.HandleDates(d)) 229 230 assert.Equal(1, d.Dates.Date.Day()) 231 assert.Equal(2, d.Dates.Lastmod.Day()) 232 assert.Equal(4, d.Dates.PublishDate.Day()) 233 assert.True(d.Dates.ExpiryDate.IsZero()) 234 235 } 236 237 func TestExpandDefaultValues(t *testing.T) { 238 assert := require.New(t) 239 assert.Equal([]string{"a", "b", "c", "d"}, expandDefaultValues([]string{"a", ":default", "d"}, []string{"b", "c"})) 240 assert.Equal([]string{"a", "b", "c"}, expandDefaultValues([]string{"a", "b", "c"}, []string{"a", "b", "c"})) 241 assert.Equal([]string{"b", "c", "a", "b", "c", "d"}, expandDefaultValues([]string{":default", "a", ":default", "d"}, []string{"b", "c"})) 242 243 } 244 245 func TestFrontMatterDateFieldHandler(t *testing.T) { 246 t.Parallel() 247 248 assert := require.New(t) 249 250 handlers := new(frontmatterFieldHandlers) 251 252 fd := newTestFd() 253 d, _ := time.Parse("2006-01-02", "2018-02-01") 254 fd.Frontmatter["date"] = d 255 h := handlers.newDateFieldHandler("date", func(d *FrontMatterDescriptor, t time.Time) { d.Dates.Date = t }) 256 257 handled, err := h(fd) 258 assert.True(handled) 259 assert.NoError(err) 260 assert.Equal(d, fd.Dates.Date) 261 }