test: unit tests for heading conversion, image alt, content facts and related settings

- ConvertContentH1 and ConvertTranslationH1 for post bodies and category
  pillar text
- SetImageAlt with several matches, repeated alt attributes and markup
  that only looks like an img
- the content facts outline, images, decorative images and embeds
- the related-posts mode and count
This commit is contained in:
Jakub Zych
2026-11-04 19:44:41 +01:00
parent fc3c25530c
commit e7832a8d26
4 changed files with 179 additions and 0 deletions

View File

@@ -28,3 +28,41 @@ func TestConvertH1(t *testing.T) {
}
}
}
// TestConvertContentH1 pins the body pair: content_html's count when its
// HTML had H1s, content's otherwise (a markdown body's H1 is only in its
// rendered HTML), both converted.
func TestConvertContentH1(t *testing.T) {
c, h, n := ConvertContentH1("# Title\n\ntext", "<h1>Title</h1><p>text</p>")
if c != "# Title\n\ntext" || h != "<h2>Title</h2><p>text</p>" || n != 1 {
t.Fatalf("markdown body = %q %q %d", c, h, n)
}
c, h, n = ConvertContentH1("<h1>a</h1><h1>b</h1>", "")
if c != "<h2>a</h2><h2>b</h2>" || h != "" || n != 2 {
t.Fatalf("HTML body without rendered HTML = %q %q %d", c, h, n)
}
if _, _, n := ConvertContentH1("<p>x</p>", "<p>x</p>"); n != 0 {
t.Fatal("no H1, nothing converted")
}
if CountH1(`<h1>a</h1><div><h1 class="x">b</h1></div><h2>c</h2>`) != 2 || CountH1("") != 0 {
t.Fatal("CountH1 is wrong")
}
}
// TestConvertTranslationH1 pins a locale's translated attributes,
// changed in place: content and content_html converted, other attributes
// and values that are not strings left alone (a category's pillar text
// takes the same path as a post body).
func TestConvertTranslationH1(t *testing.T) {
attrs := map[string]any{"title": "<h1>not content</h1>", "content": "<h1>Pillar</h1><p>x</p>", "content_html": 5}
if n := ConvertTranslationH1(attrs); n != 1 || attrs["content"] != "<h2>Pillar</h2><p>x</p>" || attrs["title"] != "<h1>not content</h1>" || attrs["content_html"] != 5 {
t.Fatalf("attrs = %v n=%d", attrs, n)
}
both := map[string]any{"content": "# T", "content_html": "<h1>T</h1>"}
if n := ConvertTranslationH1(both); n != 1 || both["content_html"] != "<h2>T</h2>" || both["content"] != "# T" {
t.Fatalf("both = %v n=%d", both, n)
}
if ConvertTranslationH1(nil) != 0 || ConvertTranslationH1(map[string]any{"title": "x"}) != 0 {
t.Fatal("nothing to convert")
}
}