github.com/anakojm/hugo-katex@v0.0.0-20231023141351-42d6f5de9c0b/resources/page/pagemeta/page_frontmatter_test.go (about)

     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_test
    15  
    16  import (
    17  	"strings"
    18  	"testing"
    19  	"time"
    20  
    21  	"github.com/gohugoio/hugo/config"
    22  	"github.com/gohugoio/hugo/config/testconfig"
    23  
    24  	"github.com/gohugoio/hugo/resources/page/pagemeta"
    25  	"github.com/gohugoio/hugo/resources/resource"
    26  
    27  	qt "github.com/frankban/quicktest"
    28  )
    29  
    30  func newTestFd() *pagemeta.FrontMatterDescriptor {
    31  	return &pagemeta.FrontMatterDescriptor{
    32  		Frontmatter: make(map[string]any),
    33  		Params:      make(map[string]any),
    34  		Dates:       &resource.Dates{},
    35  		PageURLs:    &pagemeta.URLPath{},
    36  		Location:    time.UTC,
    37  	}
    38  }
    39  
    40  func TestFrontMatterNewConfig(t *testing.T) {
    41  	c := qt.New(t)
    42  
    43  	cfg := config.New()
    44  
    45  	cfg.Set("frontmatter", map[string]any{
    46  		"date":        []string{"publishDate", "LastMod"},
    47  		"Lastmod":     []string{"publishDate"},
    48  		"expiryDate":  []string{"lastMod"},
    49  		"publishDate": []string{"date"},
    50  	})
    51  
    52  	fc, err := pagemeta.DecodeFrontMatterConfig(cfg)
    53  	c.Assert(err, qt.IsNil)
    54  	c.Assert(fc.Date, qt.DeepEquals, []string{"publishdate", "pubdate", "published", "lastmod", "modified"})
    55  	c.Assert(fc.Lastmod, qt.DeepEquals, []string{"publishdate", "pubdate", "published"})
    56  	c.Assert(fc.ExpiryDate, qt.DeepEquals, []string{"lastmod", "modified"})
    57  	c.Assert(fc.PublishDate, qt.DeepEquals, []string{"date"})
    58  
    59  	// Default
    60  	cfg = config.New()
    61  	fc, err = pagemeta.DecodeFrontMatterConfig(cfg)
    62  	c.Assert(err, qt.IsNil)
    63  	c.Assert(fc.Date, qt.DeepEquals, []string{"date", "publishdate", "pubdate", "published", "lastmod", "modified"})
    64  	c.Assert(fc.Lastmod, qt.DeepEquals, []string{":git", "lastmod", "modified", "date", "publishdate", "pubdate", "published"})
    65  	c.Assert(fc.ExpiryDate, qt.DeepEquals, []string{"expirydate", "unpublishdate"})
    66  	c.Assert(fc.PublishDate, qt.DeepEquals, []string{"publishdate", "pubdate", "published", "date"})
    67  
    68  	// :default keyword
    69  	cfg.Set("frontmatter", map[string]any{
    70  		"date":        []string{"d1", ":default"},
    71  		"lastmod":     []string{"d2", ":default"},
    72  		"expiryDate":  []string{"d3", ":default"},
    73  		"publishDate": []string{"d4", ":default"},
    74  	})
    75  	fc, err = pagemeta.DecodeFrontMatterConfig(cfg)
    76  	c.Assert(err, qt.IsNil)
    77  	c.Assert(fc.Date, qt.DeepEquals, []string{"d1", "date", "publishdate", "pubdate", "published", "lastmod", "modified"})
    78  	c.Assert(fc.Lastmod, qt.DeepEquals, []string{"d2", ":git", "lastmod", "modified", "date", "publishdate", "pubdate", "published"})
    79  	c.Assert(fc.ExpiryDate, qt.DeepEquals, []string{"d3", "expirydate", "unpublishdate"})
    80  	c.Assert(fc.PublishDate, qt.DeepEquals, []string{"d4", "publishdate", "pubdate", "published", "date"})
    81  }
    82  
    83  func TestFrontMatterDatesHandlers(t *testing.T) {
    84  	c := qt.New(t)
    85  
    86  	for _, handlerID := range []string{":filename", ":fileModTime", ":git"} {
    87  
    88  		cfg := config.New()
    89  
    90  		cfg.Set("frontmatter", map[string]any{
    91  			"date": []string{handlerID, "date"},
    92  		})
    93  		conf := testconfig.GetTestConfig(nil, cfg)
    94  		handler, err := pagemeta.NewFrontmatterHandler(nil, conf.GetConfigSection("frontmatter").(pagemeta.FrontmatterConfig))
    95  		c.Assert(err, qt.IsNil)
    96  
    97  		d1, _ := time.Parse("2006-01-02", "2018-02-01")
    98  		d2, _ := time.Parse("2006-01-02", "2018-02-02")
    99  
   100  		d := newTestFd()
   101  		switch strings.ToLower(handlerID) {
   102  		case ":filename":
   103  			d.BaseFilename = "2018-02-01-page.md"
   104  		case ":filemodtime":
   105  			d.ModTime = d1
   106  		case ":git":
   107  			d.GitAuthorDate = d1
   108  		}
   109  		d.Frontmatter["date"] = d2
   110  		c.Assert(handler.HandleDates(d), qt.IsNil)
   111  		c.Assert(d.Dates.FDate, qt.Equals, d1)
   112  		c.Assert(d.Params["date"], qt.Equals, d2)
   113  
   114  		d = newTestFd()
   115  		d.Frontmatter["date"] = d2
   116  		c.Assert(handler.HandleDates(d), qt.IsNil)
   117  		c.Assert(d.Dates.FDate, qt.Equals, d2)
   118  		c.Assert(d.Params["date"], qt.Equals, d2)
   119  
   120  	}
   121  }
   122  
   123  func TestFrontMatterDatesCustomConfig(t *testing.T) {
   124  	t.Parallel()
   125  
   126  	c := qt.New(t)
   127  
   128  	cfg := config.New()
   129  	cfg.Set("frontmatter", map[string]any{
   130  		"date":        []string{"mydate"},
   131  		"lastmod":     []string{"publishdate"},
   132  		"publishdate": []string{"publishdate"},
   133  	})
   134  
   135  	conf := testconfig.GetTestConfig(nil, cfg)
   136  	handler, err := pagemeta.NewFrontmatterHandler(nil, conf.GetConfigSection("frontmatter").(pagemeta.FrontmatterConfig))
   137  	c.Assert(err, qt.IsNil)
   138  
   139  	testDate, err := time.Parse("2006-01-02", "2018-02-01")
   140  	c.Assert(err, qt.IsNil)
   141  
   142  	d := newTestFd()
   143  	d.Frontmatter["mydate"] = testDate
   144  	testDate = testDate.Add(24 * time.Hour)
   145  	d.Frontmatter["date"] = testDate
   146  	testDate = testDate.Add(24 * time.Hour)
   147  	d.Frontmatter["lastmod"] = testDate
   148  	testDate = testDate.Add(24 * time.Hour)
   149  	d.Frontmatter["publishdate"] = testDate
   150  	testDate = testDate.Add(24 * time.Hour)
   151  	d.Frontmatter["expirydate"] = testDate
   152  
   153  	c.Assert(handler.HandleDates(d), qt.IsNil)
   154  
   155  	c.Assert(d.Dates.FDate.Day(), qt.Equals, 1)
   156  	c.Assert(d.Dates.FLastmod.Day(), qt.Equals, 4)
   157  	c.Assert(d.Dates.FPublishDate.Day(), qt.Equals, 4)
   158  	c.Assert(d.Dates.FExpiryDate.Day(), qt.Equals, 5)
   159  
   160  	c.Assert(d.Params["date"], qt.Equals, d.Dates.FDate)
   161  	c.Assert(d.Params["mydate"], qt.Equals, d.Dates.FDate)
   162  	c.Assert(d.Params["publishdate"], qt.Equals, d.Dates.FPublishDate)
   163  	c.Assert(d.Params["expirydate"], qt.Equals, d.Dates.FExpiryDate)
   164  
   165  	c.Assert(handler.IsDateKey("date"), qt.Equals, false) // This looks odd, but is configured like this.
   166  	c.Assert(handler.IsDateKey("mydate"), qt.Equals, true)
   167  	c.Assert(handler.IsDateKey("publishdate"), qt.Equals, true)
   168  	c.Assert(handler.IsDateKey("pubdate"), qt.Equals, true)
   169  }
   170  
   171  func TestFrontMatterDatesDefaultKeyword(t *testing.T) {
   172  	t.Parallel()
   173  
   174  	c := qt.New(t)
   175  
   176  	cfg := config.New()
   177  
   178  	cfg.Set("frontmatter", map[string]any{
   179  		"date":        []string{"mydate", ":default"},
   180  		"publishdate": []string{":default", "mypubdate"},
   181  	})
   182  
   183  	conf := testconfig.GetTestConfig(nil, cfg)
   184  	handler, err := pagemeta.NewFrontmatterHandler(nil, conf.GetConfigSection("frontmatter").(pagemeta.FrontmatterConfig))
   185  	c.Assert(err, qt.IsNil)
   186  
   187  	testDate, _ := time.Parse("2006-01-02", "2018-02-01")
   188  	d := newTestFd()
   189  	d.Frontmatter["mydate"] = testDate
   190  	d.Frontmatter["date"] = testDate.Add(1 * 24 * time.Hour)
   191  	d.Frontmatter["mypubdate"] = testDate.Add(2 * 24 * time.Hour)
   192  	d.Frontmatter["publishdate"] = testDate.Add(3 * 24 * time.Hour)
   193  
   194  	c.Assert(handler.HandleDates(d), qt.IsNil)
   195  
   196  	c.Assert(d.Dates.FDate.Day(), qt.Equals, 1)
   197  	c.Assert(d.Dates.FLastmod.Day(), qt.Equals, 2)
   198  	c.Assert(d.Dates.FPublishDate.Day(), qt.Equals, 4)
   199  	c.Assert(d.Dates.FExpiryDate.IsZero(), qt.Equals, true)
   200  }