github.com/neohugo/neohugo@v0.123.8/resources/page/pagemeta/page_frontmatter_test.go (about)

     1  // Copyright 2024 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/neohugo/neohugo/config"
    22  	"github.com/neohugo/neohugo/config/testconfig"
    23  
    24  	"github.com/neohugo/neohugo/resources/page/pagemeta"
    25  
    26  	qt "github.com/frankban/quicktest"
    27  )
    28  
    29  func newTestFd() *pagemeta.FrontMatterDescriptor {
    30  	return &pagemeta.FrontMatterDescriptor{
    31  		PageConfig: &pagemeta.PageConfig{
    32  			Params: make(map[string]interface{}),
    33  		},
    34  		Location: time.UTC,
    35  	}
    36  }
    37  
    38  func TestFrontMatterNewConfig(t *testing.T) {
    39  	c := qt.New(t)
    40  
    41  	cfg := config.New()
    42  
    43  	cfg.Set("frontmatter", map[string]any{
    44  		"date":        []string{"publishDate", "LastMod"},
    45  		"Lastmod":     []string{"publishDate"},
    46  		"expiryDate":  []string{"lastMod"},
    47  		"publishDate": []string{"date"},
    48  	})
    49  
    50  	fc, err := pagemeta.DecodeFrontMatterConfig(cfg)
    51  	c.Assert(err, qt.IsNil)
    52  	c.Assert(fc.Date, qt.DeepEquals, []string{"publishdate", "pubdate", "published", "lastmod", "modified"})
    53  	c.Assert(fc.Lastmod, qt.DeepEquals, []string{"publishdate", "pubdate", "published"})
    54  	c.Assert(fc.ExpiryDate, qt.DeepEquals, []string{"lastmod", "modified"})
    55  	c.Assert(fc.PublishDate, qt.DeepEquals, []string{"date"})
    56  
    57  	// Default
    58  	cfg = config.New()
    59  	fc, err = pagemeta.DecodeFrontMatterConfig(cfg)
    60  	c.Assert(err, qt.IsNil)
    61  	c.Assert(fc.Date, qt.DeepEquals, []string{"date", "publishdate", "pubdate", "published", "lastmod", "modified"})
    62  	c.Assert(fc.Lastmod, qt.DeepEquals, []string{":git", "lastmod", "modified", "date", "publishdate", "pubdate", "published"})
    63  	c.Assert(fc.ExpiryDate, qt.DeepEquals, []string{"expirydate", "unpublishdate"})
    64  	c.Assert(fc.PublishDate, qt.DeepEquals, []string{"publishdate", "pubdate", "published", "date"})
    65  
    66  	// :default keyword
    67  	cfg.Set("frontmatter", map[string]any{
    68  		"date":        []string{"d1", ":default"},
    69  		"lastmod":     []string{"d2", ":default"},
    70  		"expiryDate":  []string{"d3", ":default"},
    71  		"publishDate": []string{"d4", ":default"},
    72  	})
    73  	fc, err = pagemeta.DecodeFrontMatterConfig(cfg)
    74  	c.Assert(err, qt.IsNil)
    75  	c.Assert(fc.Date, qt.DeepEquals, []string{"d1", "date", "publishdate", "pubdate", "published", "lastmod", "modified"})
    76  	c.Assert(fc.Lastmod, qt.DeepEquals, []string{"d2", ":git", "lastmod", "modified", "date", "publishdate", "pubdate", "published"})
    77  	c.Assert(fc.ExpiryDate, qt.DeepEquals, []string{"d3", "expirydate", "unpublishdate"})
    78  	c.Assert(fc.PublishDate, qt.DeepEquals, []string{"d4", "publishdate", "pubdate", "published", "date"})
    79  }
    80  
    81  func TestFrontMatterDatesHandlers(t *testing.T) {
    82  	c := qt.New(t)
    83  
    84  	for _, handlerID := range []string{":filename", ":fileModTime", ":git"} {
    85  
    86  		cfg := config.New()
    87  
    88  		cfg.Set("frontmatter", map[string]any{
    89  			"date": []string{handlerID, "date"},
    90  		})
    91  		conf := testconfig.GetTestConfig(nil, cfg)
    92  		handler, err := pagemeta.NewFrontmatterHandler(nil, conf.GetConfigSection("frontmatter").(pagemeta.FrontmatterConfig))
    93  		c.Assert(err, qt.IsNil)
    94  
    95  		d1, _ := time.Parse("2006-01-02", "2018-02-01")
    96  		d2, _ := time.Parse("2006-01-02", "2018-02-02")
    97  
    98  		d := newTestFd()
    99  		switch strings.ToLower(handlerID) {
   100  		case ":filename":
   101  			d.BaseFilename = "2018-02-01-page.md"
   102  		case ":filemodtime":
   103  			d.ModTime = d1
   104  		case ":git":
   105  			d.GitAuthorDate = d1
   106  		}
   107  		d.PageConfig.Params["date"] = d2
   108  		c.Assert(handler.HandleDates(d), qt.IsNil)
   109  		c.Assert(d.PageConfig.Dates.Date, qt.Equals, d1)
   110  		c.Assert(d.PageConfig.Params["date"], qt.Equals, d2)
   111  
   112  		d = newTestFd()
   113  		d.PageConfig.Params["date"] = d2
   114  		c.Assert(handler.HandleDates(d), qt.IsNil)
   115  		c.Assert(d.PageConfig.Dates.Date, qt.Equals, d2)
   116  		c.Assert(d.PageConfig.Params["date"], qt.Equals, d2)
   117  
   118  	}
   119  }
   120  
   121  func TestFrontMatterDatesDefaultKeyword(t *testing.T) {
   122  	t.Parallel()
   123  
   124  	c := qt.New(t)
   125  
   126  	cfg := config.New()
   127  
   128  	cfg.Set("frontmatter", map[string]any{
   129  		"date":        []string{"mydate", ":default"},
   130  		"publishdate": []string{":default", "mypubdate"},
   131  	})
   132  
   133  	conf := testconfig.GetTestConfig(nil, cfg)
   134  	handler, err := pagemeta.NewFrontmatterHandler(nil, conf.GetConfigSection("frontmatter").(pagemeta.FrontmatterConfig))
   135  	c.Assert(err, qt.IsNil)
   136  
   137  	testDate, _ := time.Parse("2006-01-02", "2018-02-01")
   138  	d := newTestFd()
   139  	d.PageConfig.Params["mydate"] = testDate
   140  	d.PageConfig.Params["date"] = testDate.Add(1 * 24 * time.Hour)
   141  	d.PageConfig.Params["mypubdate"] = testDate.Add(2 * 24 * time.Hour)
   142  	d.PageConfig.Params["publishdate"] = testDate.Add(3 * 24 * time.Hour)
   143  
   144  	c.Assert(handler.HandleDates(d), qt.IsNil)
   145  
   146  	c.Assert(d.PageConfig.Dates.Date.Day(), qt.Equals, 1)
   147  	c.Assert(d.PageConfig.Dates.Lastmod.Day(), qt.Equals, 2)
   148  	c.Assert(d.PageConfig.Dates.PublishDate.Day(), qt.Equals, 4)
   149  	c.Assert(d.PageConfig.Dates.ExpiryDate.IsZero(), qt.Equals, true)
   150  }