github.com/neohugo/neohugo@v0.123.8/hugolib/embedded_templates_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 hugolib
    15  
    16  import (
    17  	"testing"
    18  
    19  	qt "github.com/frankban/quicktest"
    20  )
    21  
    22  func TestInternalTemplatesImage(t *testing.T) {
    23  	config := `
    24  baseURL = "https://example.org"
    25  
    26  [params]
    27  images=["siteimg1.jpg", "siteimg2.jpg"]
    28  
    29  `
    30  	b := newTestSitesBuilder(t).WithConfigFile("toml", config)
    31  
    32  	b.WithContent("mybundle/index.md", `---
    33  title: My Bundle
    34  date: 2021-02-26T18:02:00-01:00
    35  lastmod: 2021-05-22T19:25:00-01:00
    36  ---
    37  `)
    38  
    39  	b.WithContent("mypage/index.md", `---
    40  title: My Page
    41  images: ["pageimg1.jpg", "pageimg2.jpg", "https://example.local/logo.png", "sample.jpg"]
    42  date: 2021-02-26T18:02:00+01:00
    43  lastmod: 2021-05-22T19:25:00+01:00
    44  ---
    45  `)
    46  
    47  	b.WithContent("mysite.md", `---
    48  title: My Site
    49  ---
    50  `)
    51  
    52  	b.WithTemplatesAdded("_default/single.html", `
    53  
    54  {{ template "_internal/twitter_cards.html" . }}
    55  {{ template "_internal/opengraph.html" . }}
    56  {{ template "_internal/schema.html" . }}
    57  
    58  `)
    59  
    60  	b.WithSunset("content/mybundle/featured-sunset.jpg")
    61  	b.WithSunset("content/mypage/sample.jpg")
    62  	b.Build(BuildCfg{})
    63  
    64  	b.AssertFileContent("public/mybundle/index.html", `
    65  <meta name="twitter:image" content="https://example.org/mybundle/featured-sunset.jpg" />
    66  <meta name="twitter:title" content="My Bundle"/>
    67  <meta property="og:title" content="My Bundle" />
    68  <meta property="og:url" content="https://example.org/mybundle/" />
    69  <meta property="og:image" content="https://example.org/mybundle/featured-sunset.jpg" />
    70  <meta property="article:published_time" content="2021-02-26T18:02:00-01:00" />
    71  <meta property="article:modified_time" content="2021-05-22T19:25:00-01:00" />
    72  <meta itemprop="name" content="My Bundle">
    73  <meta itemprop="image" content="https://example.org/mybundle/featured-sunset.jpg" />
    74  <meta itemprop="datePublished" content="2021-02-26T18:02:00-01:00" />
    75  <meta itemprop="dateModified" content="2021-05-22T19:25:00-01:00" />
    76  
    77  `)
    78  	b.AssertFileContent("public/mypage/index.html", `
    79  <meta name="twitter:image" content="https://example.org/pageimg1.jpg" />
    80  <meta property="og:image" content="https://example.org/pageimg1.jpg" />
    81  <meta property="og:image" content="https://example.org/pageimg2.jpg" />
    82  <meta property="og:image" content="https://example.local/logo.png" />
    83  <meta property="og:image" content="https://example.org/mypage/sample.jpg" />
    84  <meta property="article:published_time" content="2021-02-26T18:02:00+01:00" />
    85  <meta property="article:modified_time" content="2021-05-22T19:25:00+01:00" />
    86  <meta itemprop="image" content="https://example.org/pageimg1.jpg" />
    87  <meta itemprop="image" content="https://example.org/pageimg2.jpg" />
    88  <meta itemprop="image" content="https://example.local/logo.png" />
    89  <meta itemprop="image" content="https://example.org/mypage/sample.jpg" />
    90  <meta itemprop="datePublished" content="2021-02-26T18:02:00+01:00" />
    91  <meta itemprop="dateModified" content="2021-05-22T19:25:00+01:00" />
    92  `)
    93  	b.AssertFileContent("public/mysite/index.html", `
    94  <meta name="twitter:image" content="https://example.org/siteimg1.jpg" />
    95  <meta property="og:image" content="https://example.org/siteimg1.jpg" />
    96  <meta itemprop="image" content="https://example.org/siteimg1.jpg" />
    97  `)
    98  }
    99  
   100  // Just some simple test of the embedded templates to avoid
   101  // https://github.com/gohugoio/hugo/issues/4757 and similar.
   102  func TestEmbeddedTemplates(t *testing.T) {
   103  	t.Parallel()
   104  
   105  	c := qt.New(t)
   106  	c.Assert(true, qt.Equals, true)
   107  
   108  	home := []string{"index.html", `
   109  GA:
   110  {{ template "_internal/google_analytics.html" . }}
   111  
   112  GA async:
   113  
   114  {{ template "_internal/google_analytics_async.html" . }}
   115  
   116  Disqus:
   117  
   118  {{ template "_internal/disqus.html" . }}
   119  
   120  `}
   121  
   122  	b := newTestSitesBuilder(t)
   123  	b.WithSimpleConfigFile().WithTemplatesAdded(home...)
   124  
   125  	b.Build(BuildCfg{})
   126  
   127  	// Gheck GA regular and async
   128  	b.AssertFileContent("public/index.html",
   129  		"'anonymizeIp', true",
   130  		"'script','https://www.google-analytics.com/analytics.js','ga');\n\tga('create', 'UA-ga_id', 'auto')",
   131  		"<script async src='https://www.google-analytics.com/analytics.js'>")
   132  
   133  	// Disqus
   134  	b.AssertFileContent("public/index.html", "\"disqus_shortname\" + '.disqus.com/embed.js';")
   135  }
   136  
   137  func TestEmbeddedPaginationTemplate(t *testing.T) {
   138  	t.Parallel()
   139  
   140  	test := func(variant string, expectedOutput string) {
   141  		b := newTestSitesBuilder(t)
   142  		b.WithConfigFile("toml", `paginate = 1`)
   143  		b.WithContent(
   144  			"s1/p01.md", "---\ntitle: p01\n---",
   145  			"s1/p02.md", "---\ntitle: p02\n---",
   146  			"s1/p03.md", "---\ntitle: p03\n---",
   147  			"s1/p04.md", "---\ntitle: p04\n---",
   148  			"s1/p05.md", "---\ntitle: p05\n---",
   149  			"s1/p06.md", "---\ntitle: p06\n---",
   150  			"s1/p07.md", "---\ntitle: p07\n---",
   151  			"s1/p08.md", "---\ntitle: p08\n---",
   152  			"s1/p09.md", "---\ntitle: p09\n---",
   153  			"s1/p10.md", "---\ntitle: p10\n---",
   154  		)
   155  		b.WithTemplates("index.html", `{{ .Paginate (where site.RegularPages "Section" "s1") }}`+variant)
   156  		b.Build(BuildCfg{})
   157  		b.AssertFileContent("public/index.html", expectedOutput)
   158  	}
   159  
   160  	expectedOutputDefaultFormat := "Pager 1\n    <ul class=\"pagination pagination-default\">\n      <li class=\"page-item disabled\">\n        <a aria-disabled=\"true\" aria-label=\"First\" class=\"page-link\" role=\"button\" tabindex=\"-1\"><span aria-hidden=\"true\">&laquo;&laquo;</span></a>\n      </li>\n      <li class=\"page-item disabled\">\n        <a aria-disabled=\"true\" aria-label=\"Previous\" class=\"page-link\" role=\"button\" tabindex=\"-1\"><span aria-hidden=\"true\">&laquo;</span></a>\n      </li>\n      <li class=\"page-item active\">\n        <a aria-current=\"page\" aria-label=\"Page 1\" class=\"page-link\" role=\"button\">1</a>\n      </li>\n      <li class=\"page-item\">\n        <a href=\"/page/2/\" aria-label=\"Page 2\" class=\"page-link\" role=\"button\">2</a>\n      </li>\n      <li class=\"page-item\">\n        <a href=\"/page/3/\" aria-label=\"Page 3\" class=\"page-link\" role=\"button\">3</a>\n      </li>\n      <li class=\"page-item\">\n        <a href=\"/page/4/\" aria-label=\"Page 4\" class=\"page-link\" role=\"button\">4</a>\n      </li>\n      <li class=\"page-item\">\n        <a href=\"/page/5/\" aria-label=\"Page 5\" class=\"page-link\" role=\"button\">5</a>\n      </li>\n      <li class=\"page-item\">\n        <a href=\"/page/2/\" aria-label=\"Next\" class=\"page-link\" role=\"button\"><span aria-hidden=\"true\">&raquo;</span></a>\n      </li>\n      <li class=\"page-item\">\n        <a href=\"/page/10/\" aria-label=\"Last\" class=\"page-link\" role=\"button\"><span aria-hidden=\"true\">&raquo;&raquo;</span></a>\n      </li>\n    </ul>"
   161  	expectedOutputTerseFormat := "Pager 1\n    <ul class=\"pagination pagination-terse\">\n      <li class=\"page-item active\">\n        <a aria-current=\"page\" aria-label=\"Page 1\" class=\"page-link\" role=\"button\">1</a>\n      </li>\n      <li class=\"page-item\">\n        <a href=\"/page/2/\" aria-label=\"Page 2\" class=\"page-link\" role=\"button\">2</a>\n      </li>\n      <li class=\"page-item\">\n        <a href=\"/page/3/\" aria-label=\"Page 3\" class=\"page-link\" role=\"button\">3</a>\n      </li>\n      <li class=\"page-item\">\n        <a href=\"/page/2/\" aria-label=\"Next\" class=\"page-link\" role=\"button\"><span aria-hidden=\"true\">&raquo;</span></a>\n      </li>\n      <li class=\"page-item\">\n        <a href=\"/page/10/\" aria-label=\"Last\" class=\"page-link\" role=\"button\"><span aria-hidden=\"true\">&raquo;&raquo;</span></a>\n      </li>\n    </ul>"
   162  
   163  	variant := `{{ template "_internal/pagination.html" . }}`
   164  	test(variant, expectedOutputDefaultFormat)
   165  
   166  	variant = `{{ template "_internal/pagination.html" (dict "page" .) }}`
   167  	test(variant, expectedOutputDefaultFormat)
   168  
   169  	variant = `{{ template "_internal/pagination.html" (dict "page" . "format" "default") }}`
   170  	test(variant, expectedOutputDefaultFormat)
   171  
   172  	variant = `{{ template "_internal/pagination.html" (dict "page" . "format" "terse") }}`
   173  	test(variant, expectedOutputTerseFormat)
   174  }