github.com/graemephi/kahugo@v0.62.3-0.20211121071557-d78c0423784d/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.md", `---
    40  title: My Page
    41  images: ["pageimg1.jpg", "pageimg2.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.Build(BuildCfg{})
    62  
    63  	b.AssertFileContent("public/mybundle/index.html", `
    64  <meta name="twitter:image" content="https://example.org/mybundle/featured-sunset.jpg"/>
    65  <meta name="twitter:title" content="My Bundle"/>
    66  <meta property="og:title" content="My Bundle" />
    67  <meta property="og:url" content="https://example.org/mybundle/" />
    68  <meta property="og:image" content="https://example.org/mybundle/featured-sunset.jpg"/>
    69  <meta property="article:published_time" content="2021-02-26T18:02:00-01:00" />
    70  <meta property="article:modified_time" content="2021-05-22T19:25:00-01:00" />
    71  <meta itemprop="name" content="My Bundle">
    72  <meta itemprop="image" content="https://example.org/mybundle/featured-sunset.jpg">
    73  <meta itemprop="datePublished" content="2021-02-26T18:02:00-01:00" />
    74  <meta itemprop="dateModified" content="2021-05-22T19:25:00-01:00" />
    75  
    76  `)
    77  	b.AssertFileContent("public/mypage/index.html", `
    78  <meta name="twitter:image" content="https://example.org/pageimg1.jpg"/>
    79  <meta property="og:image" content="https://example.org/pageimg1.jpg" />
    80  <meta property="og:image" content="https://example.org/pageimg2.jpg" />
    81  <meta property="article:published_time" content="2021-02-26T18:02:00+01:00" />
    82  <meta property="article:modified_time" content="2021-05-22T19:25:00+01:00" />
    83  <meta itemprop="image" content="https://example.org/pageimg1.jpg">
    84  <meta itemprop="image" content="https://example.org/pageimg2.jpg">
    85  <meta itemprop="datePublished" content="2021-02-26T18:02:00+01:00" />
    86  <meta itemprop="dateModified" content="2021-05-22T19:25:00+01:00" />
    87  `)
    88  	b.AssertFileContent("public/mysite/index.html", `
    89  <meta name="twitter:image" content="https://example.org/siteimg1.jpg"/>
    90  <meta property="og:image" content="https://example.org/siteimg1.jpg"/>
    91  <meta itemprop="image" content="https://example.org/siteimg1.jpg"/>
    92  `)
    93  }
    94  
    95  // Just some simple test of the embedded templates to avoid
    96  // https://github.com/gohugoio/hugo/issues/4757 and similar.
    97  func TestEmbeddedTemplates(t *testing.T) {
    98  	t.Parallel()
    99  
   100  	c := qt.New(t)
   101  	c.Assert(true, qt.Equals, true)
   102  
   103  	home := []string{"index.html", `
   104  GA:
   105  {{ template "_internal/google_analytics.html" . }}
   106  
   107  GA async:
   108  
   109  {{ template "_internal/google_analytics_async.html" . }}
   110  
   111  Disqus:
   112  
   113  {{ template "_internal/disqus.html" . }}
   114  
   115  `}
   116  
   117  	b := newTestSitesBuilder(t)
   118  	b.WithSimpleConfigFile().WithTemplatesAdded(home...)
   119  
   120  	b.Build(BuildCfg{})
   121  
   122  	// Gheck GA regular and async
   123  	b.AssertFileContent("public/index.html",
   124  		"'anonymizeIp', true",
   125  		"'script','https://www.google-analytics.com/analytics.js','ga');\n\tga('create', 'UA-ga_id', 'auto')",
   126  		"<script async src='https://www.google-analytics.com/analytics.js'>")
   127  
   128  	// Disqus
   129  	b.AssertFileContent("public/index.html", "\"disqus_shortname\" + '.disqus.com/embed.js';")
   130  }
   131  
   132  func TestEmbeddedPaginationTemplate(t *testing.T) {
   133  	t.Parallel()
   134  
   135  	test := func(variant string, expectedOutput string) {
   136  		b := newTestSitesBuilder(t)
   137  		b.WithConfigFile("toml", `paginate = 1`)
   138  		b.WithContent(
   139  			"s1/p01.md", "---\ntitle: p01\n---",
   140  			"s1/p02.md", "---\ntitle: p02\n---",
   141  			"s1/p03.md", "---\ntitle: p03\n---",
   142  			"s1/p04.md", "---\ntitle: p04\n---",
   143  			"s1/p05.md", "---\ntitle: p05\n---",
   144  			"s1/p06.md", "---\ntitle: p06\n---",
   145  			"s1/p07.md", "---\ntitle: p07\n---",
   146  			"s1/p08.md", "---\ntitle: p08\n---",
   147  			"s1/p09.md", "---\ntitle: p09\n---",
   148  			"s1/p10.md", "---\ntitle: p10\n---",
   149  		)
   150  		b.WithTemplates("index.html", `{{ .Paginate (where site.RegularPages "Section" "s1") }}`+variant)
   151  		b.Build(BuildCfg{})
   152  		b.AssertFileContent("public/index.html", expectedOutput)
   153  	}
   154  
   155  	expectedOutputDefaultFormat := "Pager 1\n    <ul class=\"pagination pagination-default\">\n      <li class=\"page-item disabled\">\n        <a href=\"#\" 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 href=\"#\" 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 href=\"#\" 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>"
   156  	expectedOutputTerseFormat := "Pager 1\n    <ul class=\"pagination pagination-terse\">\n      <li class=\"page-item active\">\n        <a href=\"#\" 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>"
   157  
   158  	variant := `{{ template "_internal/pagination.html" . }}`
   159  	test(variant, expectedOutputDefaultFormat)
   160  
   161  	variant = `{{ template "_internal/pagination.html" (dict "page" .) }}`
   162  	test(variant, expectedOutputDefaultFormat)
   163  
   164  	variant = `{{ template "_internal/pagination.html" (dict "page" . "format" "default") }}`
   165  	test(variant, expectedOutputDefaultFormat)
   166  
   167  	variant = `{{ template "_internal/pagination.html" (dict "page" . "format" "terse") }}`
   168  	test(variant, expectedOutputTerseFormat)
   169  }