Files
sm-seo-plugin/classes/alternates_test.go
Jakub Zych 2a288a9570 test: Journal-shaped records in the hreflang test
TestAlternates also runs on a record shaped as the Journal descriptors
describe one: every enabled locale present, the untranslated one with
URL "" and the default locale's title, description and word count; it
answers no hreflang for that locale.
2026-11-04 23:28:15 +01:00

94 lines
3.4 KiB
Go

package classes
import (
"testing"
"git.golem15.com/golem15/summercms/modules/pact"
)
// TestLangTag pins the language tags of locale codes.
func TestLangTag(t *testing.T) {
for in, want := range map[string]string{"pl": "pl", "PL": "pl", "pt_BR": "pt-BR", "pt-br": "pt-BR", " en-gb ": "en-GB", "de-": "de", "": ""} {
if got := LangTag(in); got != want {
t.Errorf("LangTag(%q) = %q, want %q", in, got, want)
}
}
}
// TestAlternates pins D-55 to D-57: each translated locale with its
// absolute URL (the page itself included), x-default on the default
// locale's URL, none for one locale, a locale without an address or a
// repeated locale absent, and no x-default when the default locale is not
// translated.
func TestAlternates(t *testing.T) {
const site = "https://example.test"
rec := &pact.ContentRecord{Locales: []pact.ContentLocale{
{Locale: "pl", URL: "/blog/grzyby"},
{Locale: "en", URL: "/en/blog/mushrooms"},
{Locale: "de", URL: " "},
{Locale: "pl", URL: "/blog/again"},
{Locale: "pt_BR", URL: "https://example.test/pt/cogumelos"},
}}
got := Alternates(site, rec, "pl")
want := []Alternate{
{Hreflang: "pl", URL: site + "/blog/grzyby"},
{Hreflang: "en", URL: site + "/en/blog/mushrooms"},
{Hreflang: "pt-BR", URL: site + "/pt/cogumelos"},
{Hreflang: "x-default", URL: site + "/blog/grzyby"},
}
if len(got) != len(want) {
t.Fatalf("alternates = %+v", got)
}
for i := range want {
if got[i] != want[i] {
t.Fatalf("alternate %d = %+v, want %+v", i, got[i], want[i])
}
}
if noDefault := Alternates(site, rec, "fr"); len(noDefault) != 3 || noDefault[2].Hreflang == "x-default" {
t.Fatalf("without the default locale = %+v", noDefault)
}
one := &pact.ContentRecord{Locales: []pact.ContentLocale{{Locale: "pl", URL: "/a"}, {Locale: "en", URL: ""}}}
if got := Alternates(site, one, "pl"); got == nil || len(got) != 0 {
t.Fatalf("one locale = %+v, want []", got)
}
if got := Alternates(site, nil, "pl"); len(got) != 0 {
t.Fatal("no record, no alternates")
}
// G3: a Journal-shaped record, as the Journal descriptors describe one
// (P33-64): every enabled locale in sort order, the untranslated de
// with URL "" and the default locale's title, description and word
// count. de gets no hreflang.
words := 120
journalShaped := &pact.ContentRecord{Locales: []pact.ContentLocale{
{Locale: "pl", URL: "/blog/grzyby", Title: "Grzyby", Description: "O grzybach", Words: &words},
{Locale: "en", URL: "/en/blog/mushrooms", Title: "Mushrooms", Description: "About mushrooms", Words: &words},
{Locale: "de", URL: "", Title: "Grzyby", Description: "O grzybach", Words: &words},
}}
got = Alternates(site, journalShaped, "pl")
want = []Alternate{
{Hreflang: "pl", URL: site + "/blog/grzyby"},
{Hreflang: "en", URL: site + "/en/blog/mushrooms"},
{Hreflang: "x-default", URL: site + "/blog/grzyby"},
}
if len(got) != len(want) {
t.Fatalf("Journal-shaped alternates = %+v", got)
}
for i := range want {
if got[i] != want[i] {
t.Fatalf("Journal-shaped alternate %d = %+v, want %+v", i, got[i], want[i])
}
}
}
// TestOGLocale pins og:locale without configuration: the default table,
// the region form and the doubled language.
func TestOGLocale(t *testing.T) {
s := &Service{}
for in, want := range map[string]string{"pl": "pl_PL", "en": "en_US", "de": "de_DE", "pt_BR": "pt_BR", "fr": "fr_FR", "": ""} {
if got := s.OGLocale(in); got != want {
t.Errorf("OGLocale(%q) = %q, want %q", in, got, want)
}
}
}