page_frontmatter_test.go (8282B)
1 // Copyright 2019 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 "strings"
18 "testing"
19 "time"
20
21 "github.com/gohugoio/hugo/config"
22
23 "github.com/gohugoio/hugo/resources/resource"
24
25 qt "github.com/frankban/quicktest"
26 )
27
28 func TestDateAndSlugFromBaseFilename(t *testing.T) {
29 t.Parallel()
30
31 c := qt.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 _, test := range tests {
53 expecteFDate, err := time.Parse("2006-01-02", test.date)
54 c.Assert(err, qt.IsNil)
55
56 gotDate, gotSlug := dateAndSlugFromBaseFilename(time.UTC, test.name)
57
58 c.Assert(gotDate, qt.Equals, expecteFDate)
59 c.Assert(gotSlug, qt.Equals, test.slug)
60
61 }
62 }
63
64 func newTestFd() *FrontMatterDescriptor {
65 return &FrontMatterDescriptor{
66 Frontmatter: make(map[string]any),
67 Params: make(map[string]any),
68 Dates: &resource.Dates{},
69 PageURLs: &URLPath{},
70 Location: time.UTC,
71 }
72 }
73
74 func TestFrontMatterNewConfig(t *testing.T) {
75 c := qt.New(t)
76
77 cfg := config.New()
78
79 cfg.Set("frontmatter", map[string]any{
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 c.Assert(err, qt.IsNil)
88 c.Assert(fc.date, qt.DeepEquals, []string{"publishdate", "pubdate", "published", "lastmod", "modified"})
89 c.Assert(fc.lastmod, qt.DeepEquals, []string{"publishdate", "pubdate", "published"})
90 c.Assert(fc.expiryDate, qt.DeepEquals, []string{"lastmod", "modified"})
91 c.Assert(fc.publishDate, qt.DeepEquals, []string{"date"})
92
93 // Default
94 cfg = config.New()
95 fc, err = newFrontmatterConfig(cfg)
96 c.Assert(err, qt.IsNil)
97 c.Assert(fc.date, qt.DeepEquals, []string{"date", "publishdate", "pubdate", "published", "lastmod", "modified"})
98 c.Assert(fc.lastmod, qt.DeepEquals, []string{":git", "lastmod", "modified", "date", "publishdate", "pubdate", "published"})
99 c.Assert(fc.expiryDate, qt.DeepEquals, []string{"expirydate", "unpublishdate"})
100 c.Assert(fc.publishDate, qt.DeepEquals, []string{"publishdate", "pubdate", "published", "date"})
101
102 // :default keyword
103 cfg.Set("frontmatter", map[string]any{
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 c.Assert(err, qt.IsNil)
111 c.Assert(fc.date, qt.DeepEquals, []string{"d1", "date", "publishdate", "pubdate", "published", "lastmod", "modified"})
112 c.Assert(fc.lastmod, qt.DeepEquals, []string{"d2", ":git", "lastmod", "modified", "date", "publishdate", "pubdate", "published"})
113 c.Assert(fc.expiryDate, qt.DeepEquals, []string{"d3", "expirydate", "unpublishdate"})
114 c.Assert(fc.publishDate, qt.DeepEquals, []string{"d4", "publishdate", "pubdate", "published", "date"})
115 }
116
117 func TestFrontMatterDatesHandlers(t *testing.T) {
118 c := qt.New(t)
119
120 for _, handlerID := range []string{":filename", ":fileModTime", ":git"} {
121
122 cfg := config.New()
123
124 cfg.Set("frontmatter", map[string]any{
125 "date": []string{handlerID, "date"},
126 })
127
128 handler, err := NewFrontmatterHandler(nil, cfg)
129 c.Assert(err, qt.IsNil)
130
131 d1, _ := time.Parse("2006-01-02", "2018-02-01")
132 d2, _ := time.Parse("2006-01-02", "2018-02-02")
133
134 d := newTestFd()
135 switch strings.ToLower(handlerID) {
136 case ":filename":
137 d.BaseFilename = "2018-02-01-page.md"
138 case ":filemodtime":
139 d.ModTime = d1
140 case ":git":
141 d.GitAuthorDate = d1
142 }
143 d.Frontmatter["date"] = d2
144 c.Assert(handler.HandleDates(d), qt.IsNil)
145 c.Assert(d.Dates.FDate, qt.Equals, d1)
146 c.Assert(d.Params["date"], qt.Equals, d2)
147
148 d = newTestFd()
149 d.Frontmatter["date"] = d2
150 c.Assert(handler.HandleDates(d), qt.IsNil)
151 c.Assert(d.Dates.FDate, qt.Equals, d2)
152 c.Assert(d.Params["date"], qt.Equals, d2)
153
154 }
155 }
156
157 func TestFrontMatterDatesCustomConfig(t *testing.T) {
158 t.Parallel()
159
160 c := qt.New(t)
161
162 cfg := config.New()
163 cfg.Set("frontmatter", map[string]any{
164 "date": []string{"mydate"},
165 "lastmod": []string{"publishdate"},
166 "publishdate": []string{"publishdate"},
167 })
168
169 handler, err := NewFrontmatterHandler(nil, cfg)
170 c.Assert(err, qt.IsNil)
171
172 testDate, err := time.Parse("2006-01-02", "2018-02-01")
173 c.Assert(err, qt.IsNil)
174
175 d := newTestFd()
176 d.Frontmatter["mydate"] = testDate
177 testDate = testDate.Add(24 * time.Hour)
178 d.Frontmatter["date"] = testDate
179 testDate = testDate.Add(24 * time.Hour)
180 d.Frontmatter["lastmod"] = testDate
181 testDate = testDate.Add(24 * time.Hour)
182 d.Frontmatter["publishdate"] = testDate
183 testDate = testDate.Add(24 * time.Hour)
184 d.Frontmatter["expirydate"] = testDate
185
186 c.Assert(handler.HandleDates(d), qt.IsNil)
187
188 c.Assert(d.Dates.FDate.Day(), qt.Equals, 1)
189 c.Assert(d.Dates.FLastmod.Day(), qt.Equals, 4)
190 c.Assert(d.Dates.FPublishDate.Day(), qt.Equals, 4)
191 c.Assert(d.Dates.FExpiryDate.Day(), qt.Equals, 5)
192
193 c.Assert(d.Params["date"], qt.Equals, d.Dates.FDate)
194 c.Assert(d.Params["mydate"], qt.Equals, d.Dates.FDate)
195 c.Assert(d.Params["publishdate"], qt.Equals, d.Dates.FPublishDate)
196 c.Assert(d.Params["expirydate"], qt.Equals, d.Dates.FExpiryDate)
197
198 c.Assert(handler.IsDateKey("date"), qt.Equals, false) // This looks odd, but is configured like this.
199 c.Assert(handler.IsDateKey("mydate"), qt.Equals, true)
200 c.Assert(handler.IsDateKey("publishdate"), qt.Equals, true)
201 c.Assert(handler.IsDateKey("pubdate"), qt.Equals, true)
202 }
203
204 func TestFrontMatterDatesDefaultKeyword(t *testing.T) {
205 t.Parallel()
206
207 c := qt.New(t)
208
209 cfg := config.New()
210
211 cfg.Set("frontmatter", map[string]any{
212 "date": []string{"mydate", ":default"},
213 "publishdate": []string{":default", "mypubdate"},
214 })
215
216 handler, err := NewFrontmatterHandler(nil, cfg)
217 c.Assert(err, qt.IsNil)
218
219 testDate, _ := time.Parse("2006-01-02", "2018-02-01")
220 d := newTestFd()
221 d.Frontmatter["mydate"] = testDate
222 d.Frontmatter["date"] = testDate.Add(1 * 24 * time.Hour)
223 d.Frontmatter["mypubdate"] = testDate.Add(2 * 24 * time.Hour)
224 d.Frontmatter["publishdate"] = testDate.Add(3 * 24 * time.Hour)
225
226 c.Assert(handler.HandleDates(d), qt.IsNil)
227
228 c.Assert(d.Dates.FDate.Day(), qt.Equals, 1)
229 c.Assert(d.Dates.FLastmod.Day(), qt.Equals, 2)
230 c.Assert(d.Dates.FPublishDate.Day(), qt.Equals, 4)
231 c.Assert(d.Dates.FExpiryDate.IsZero(), qt.Equals, true)
232 }
233
234 func TestExpandDefaultValues(t *testing.T) {
235 c := qt.New(t)
236 c.Assert(expandDefaultValues([]string{"a", ":default", "d"}, []string{"b", "c"}), qt.DeepEquals, []string{"a", "b", "c", "d"})
237 c.Assert(expandDefaultValues([]string{"a", "b", "c"}, []string{"a", "b", "c"}), qt.DeepEquals, []string{"a", "b", "c"})
238 c.Assert(expandDefaultValues([]string{":default", "a", ":default", "d"}, []string{"b", "c"}), qt.DeepEquals, []string{"b", "c", "a", "b", "c", "d"})
239 }
240
241 func TestFrontMatterDateFieldHandler(t *testing.T) {
242 t.Parallel()
243
244 c := qt.New(t)
245
246 handlers := new(frontmatterFieldHandlers)
247
248 fd := newTestFd()
249 d, _ := time.Parse("2006-01-02", "2018-02-01")
250 fd.Frontmatter["date"] = d
251 h := handlers.newDateFieldHandler("date", func(d *FrontMatterDescriptor, t time.Time) { d.Dates.FDate = t })
252
253 handled, err := h(fd)
254 c.Assert(handled, qt.Equals, true)
255 c.Assert(err, qt.IsNil)
256 c.Assert(fd.Dates.FDate, qt.Equals, d)
257 }