github.com/neohugo/neohugo@v0.123.8/hugolib/paginator_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 hugolib
    15  
    16  import (
    17  	"fmt"
    18  	"path/filepath"
    19  	"testing"
    20  
    21  	qt "github.com/frankban/quicktest"
    22  )
    23  
    24  func TestPaginator(t *testing.T) {
    25  	configFile := `
    26  baseURL = "https://example.com/foo/"
    27  paginate = 3
    28  paginatepath = "thepage"
    29  
    30  [languages.en]
    31  weight = 1
    32  contentDir = "content/en"
    33  
    34  [languages.nn]
    35  weight = 2
    36  contentDir = "content/nn"
    37  
    38  `
    39  	b := newTestSitesBuilder(t).WithConfigFile("toml", configFile)
    40  	var content []string
    41  	for i := 0; i < 9; i++ {
    42  		for _, contentDir := range []string{"content/en", "content/nn"} {
    43  			content = append(content, fmt.Sprintf(contentDir+"/blog/page%d.md", i), fmt.Sprintf(`---
    44  title: Page %d
    45  ---
    46  
    47  Content.
    48  `, i))
    49  		}
    50  	}
    51  
    52  	b.WithContent(content...)
    53  
    54  	pagTemplate := `
    55  {{ $pag := $.Paginator }}
    56  Total: {{ $pag.TotalPages }}
    57  First: {{ $pag.First.URL }}
    58  Page Number: {{ $pag.PageNumber }}
    59  URL: {{ $pag.URL }}
    60  {{ with $pag.Next }}Next: {{ .URL }}{{ end }}
    61  {{ with $pag.Prev }}Prev: {{ .URL }}{{ end }}
    62  {{ range $i, $e := $pag.Pagers }}
    63  {{ printf "%d: %d/%d  %t" $i $pag.PageNumber .PageNumber (eq . $pag) -}}
    64  {{ end }}
    65  `
    66  
    67  	b.WithTemplatesAdded("index.html", pagTemplate)
    68  	b.WithTemplatesAdded("index.xml", pagTemplate)
    69  
    70  	b.Build(BuildCfg{})
    71  
    72  	b.AssertFileContent("public/index.html",
    73  		"Page Number: 1",
    74  		"0: 1/1  true")
    75  
    76  	b.AssertFileContent("public/thepage/2/index.html",
    77  		"Total: 3",
    78  		"Page Number: 2",
    79  		"URL: /foo/thepage/2/",
    80  		"Next: /foo/thepage/3/",
    81  		"Prev: /foo/",
    82  		"1: 2/2  true",
    83  	)
    84  
    85  	b.AssertFileContent("public/index.xml",
    86  		"Page Number: 1",
    87  		"0: 1/1  true")
    88  	b.AssertFileContent("public/thepage/2/index.xml",
    89  		"Page Number: 2",
    90  		"1: 2/2  true")
    91  
    92  	b.AssertFileContent("public/nn/index.html",
    93  		"Page Number: 1",
    94  		"0: 1/1  true")
    95  
    96  	b.AssertFileContent("public/nn/index.xml",
    97  		"Page Number: 1",
    98  		"0: 1/1  true")
    99  }
   100  
   101  // Issue 6023
   102  func TestPaginateWithSort(t *testing.T) {
   103  	b := newTestSitesBuilder(t).WithSimpleConfigFile()
   104  	b.WithTemplatesAdded("index.html", `{{ range (.Paginate (sort .Site.RegularPages ".File.Filename" "desc")).Pages }}|{{ .File.Filename }}{{ end }}`)
   105  	b.Build(BuildCfg{}).AssertFileContent("public/index.html",
   106  		filepath.FromSlash("|content/sect/doc1.nn.md|content/sect/doc1.nb.md|content/sect/doc1.fr.md|content/sect/doc1.en.md"))
   107  }
   108  
   109  // https://github.com/gohugoio/hugo/issues/6797
   110  func TestPaginateOutputFormat(t *testing.T) {
   111  	b := newTestSitesBuilder(t).WithSimpleConfigFile()
   112  	b.WithContent("_index.md", `---
   113  title: "Home"
   114  cascade:
   115    outputs:
   116      - JSON
   117  ---`)
   118  
   119  	for i := 0; i < 22; i++ {
   120  		b.WithContent(fmt.Sprintf("p%d.md", i+1), fmt.Sprintf(`---
   121  title: "Page"
   122  weight: %d
   123  ---`, i+1))
   124  	}
   125  
   126  	b.WithTemplatesAdded("index.json", `JSON: {{ .Paginator.TotalNumberOfElements }}: {{ range .Paginator.Pages }}|{{ .RelPermalink }}{{ end }}:DONE`)
   127  	b.Build(BuildCfg{})
   128  
   129  	b.AssertFileContent("public/index.json",
   130  		`JSON: 22
   131  |/p1/index.json|/p2/index.json|
   132  `)
   133  
   134  	// This looks odd, so are most bugs.
   135  	b.Assert(b.CheckExists("public/page/1/index.json/index.html"), qt.Equals, false)
   136  	b.Assert(b.CheckExists("public/page/1/index.json"), qt.Equals, false)
   137  	b.AssertFileContent("public/page/2/index.json", `JSON: 22: |/p11/index.json|/p12/index.json`)
   138  }
   139  
   140  // Issue 10802
   141  func TestPaginatorEmptyPageGroups(t *testing.T) {
   142  	t.Parallel()
   143  
   144  	files := `
   145  -- config.toml --
   146  baseURL = "https://example.com/"
   147  -- content/p1.md --
   148  -- content/p2.md --
   149  -- layouts/index.html --
   150  {{ $empty := site.RegularPages | complement site.RegularPages }}
   151  Len: {{ len $empty }}: Type: {{ printf "%T" $empty }}
   152  {{ $pgs := $empty.GroupByPublishDate "January 2006" }}
   153  {{ $pag := .Paginate $pgs }}
   154  Len Pag: {{ len $pag.Pages }}
   155  `
   156  	b := Test(t, files)
   157  
   158  	b.AssertFileContent("public/index.html", "Len: 0", "Len Pag: 0")
   159  }
   160  
   161  func TestPaginatorNodePagesOnly(t *testing.T) {
   162  	files := `
   163  -- hugo.toml --
   164  paginate = 1
   165  -- content/p1.md --
   166  -- layouts/_default/single.html --
   167  Paginator: {{ .Paginator }}	
   168  `
   169  	b, err := TestE(t, files)
   170  	b.Assert(err, qt.IsNotNil)
   171  	b.Assert(err.Error(), qt.Contains, `error calling Paginator: pagination not supported for this page: kind: "page"`)
   172  }
   173  
   174  func TestNilPointerErrorMessage(t *testing.T) {
   175  	files := `
   176  -- hugo.toml --
   177  -- content/p1.md --
   178  -- layouts/_default/single.html --
   179  Home Filename: {{ site.Home.File.Filename }}
   180  `
   181  	b, err := TestE(t, files)
   182  	b.Assert(err, qt.IsNotNil)
   183  	b.Assert(err.Error(), qt.Contains, `_default/single.html:1:22: executing "_default/single.html" – File is nil; wrap it in if or with: {{ with site.Home.File }}{{ .Filename }}{{ end }}`)
   184  }