Files
RenewCMS/test/localization_rules_test.go
T

126 lines
5.6 KiB
Go

package test
import (
"RenewCMS/internal/domain/localization"
"errors"
"testing"
)
func socialLocale(value string) *string {
return &value
}
func TestLocalizationRowEquality(t *testing.T) {
base := localization.Row{Tag: "fr", IsDefault: true, Enabled: true, Confirmed: true, SocialLocale: socialLocale("fr_FR")}
differing := map[string]localization.Row{
"tag": {Tag: "de", IsDefault: true, Enabled: true, Confirmed: true, SocialLocale: socialLocale("fr_FR")},
"default": {Tag: "fr", IsDefault: false, Enabled: true, Confirmed: true, SocialLocale: socialLocale("fr_FR")},
"enabled": {Tag: "fr", IsDefault: true, Enabled: false, Confirmed: true, SocialLocale: socialLocale("fr_FR")},
"confirmed": {Tag: "fr", IsDefault: true, Enabled: true, Confirmed: false, SocialLocale: socialLocale("fr_FR")},
"social locale": {Tag: "fr", IsDefault: true, Enabled: true, Confirmed: true, SocialLocale: socialLocale("fr_CA")},
"missing social": {Tag: "fr", IsDefault: true, Enabled: true, Confirmed: true},
"same row": base,
"same social copied": {Tag: "fr", IsDefault: true, Enabled: true, Confirmed: true, SocialLocale: socialLocale("fr_FR")},
}
for name, other := range differing {
t.Run(name, func(t *testing.T) {
want := name == "same row" || name == "same social copied"
if base.Equal(other) != want || other.Equal(base) != want {
t.Fatalf("Equal = %v, want %v", base.Equal(other), want)
}
})
}
t.Run("two rows without a social locale are equal", func(t *testing.T) {
plain := localization.Row{Tag: "en", Enabled: true}
if !plain.Equal(localization.Row{Tag: "en", Enabled: true}) {
t.Fatal("rows without social locale differ")
}
})
}
func TestLocalizationRevisionTracksEveryField(t *testing.T) {
rows := []localization.Row{{Tag: "en", IsDefault: true, Enabled: true}, {Tag: "fr", Enabled: true, SocialLocale: socialLocale("fr_FR")}}
changed := [][]localization.Row{
{{Tag: "en", IsDefault: false, Enabled: true}, {Tag: "fr", IsDefault: true, Enabled: true, SocialLocale: socialLocale("fr_FR")}},
{{Tag: "en", IsDefault: true, Enabled: true}, {Tag: "fr", Enabled: false, SocialLocale: socialLocale("fr_FR")}},
{{Tag: "en", IsDefault: true, Enabled: true}, {Tag: "fr", Enabled: true}},
}
for _, other := range changed {
if localization.SameRows(rows, other) {
t.Errorf("%+v has the revision of %+v", other, rows)
}
}
reordered := []localization.Row{rows[1], rows[0]}
if !localization.SameRows(rows, reordered) {
t.Error("the order of the rows changed the revision")
}
}
func TestLocalizationSocialLocaleState(t *testing.T) {
cases := []struct {
name string
row localization.Row
siteLocale *string
want string
}{
{"an explicit social locale is set", localization.Row{Tag: "fr", SocialLocale: socialLocale("fr_FR")}, nil, localization.SocialLocaleSet},
{"a regional tag derives one", localization.Row{Tag: "fr-CA"}, nil, localization.SocialLocaleDerived},
{"the site locale lends one", localization.Row{Tag: "fr"}, socialLocale("fr-BE"), localization.SocialLocaleDerived},
{"nothing to derive from", localization.Row{Tag: "fr"}, socialLocale("de-DE"), localization.SocialLocaleNotSet},
}
for _, c := range cases {
t.Run(c.name, func(t *testing.T) {
if got := localization.SocialLocaleState(c.row, c.siteLocale); got != c.want {
t.Fatalf("state = %q, want %q", got, c.want)
}
})
}
}
func TestLocalizationPlanKeepsLanguagesInUse(t *testing.T) {
current := []localization.Row{{Tag: "en", IsDefault: true, Enabled: true, Confirmed: true}, {Tag: "fr", Enabled: true}}
keepEnglishOnly := localization.Change{Default: "en", Languages: []localization.Entry{{Tag: "en", Enabled: true}}}
cases := []struct {
name string
counts map[string]localization.VersionCount
want error
}{
{"a removed language with live versions is refused", map[string]localization.VersionCount{"fr": {Total: 1}}, localization.ErrLanguageInUse},
{"a removed language with only trashed versions is refused", map[string]localization.VersionCount{"fr": {Trashed: 1}}, localization.ErrLanguageInUse},
{"a removed language without versions is dropped", map[string]localization.VersionCount{"en": {Total: 4}}, nil},
}
for _, c := range cases {
t.Run(c.name, func(t *testing.T) {
rows, err := localization.Plan(current, c.counts, keepEnglishOnly)
if !errors.Is(err, c.want) {
t.Fatalf("err = %v, want %v", err, c.want)
}
if c.want == nil && (len(rows) != 1 || rows[0].Tag != "en" || !rows[0].IsDefault) {
t.Fatalf("rows = %+v", rows)
}
})
}
t.Run("a kept language with versions stays", func(t *testing.T) {
keepBoth := localization.Change{Default: "en", Languages: []localization.Entry{{Tag: "en", Enabled: true}, {Tag: "fr", Enabled: false}}}
rows, err := localization.Plan(current, map[string]localization.VersionCount{"fr": {Total: 2}}, keepBoth)
if err != nil || len(rows) != 2 {
t.Fatalf("rows = %+v, err = %v", rows, err)
}
})
}
func TestLocalizationRowLookups(t *testing.T) {
rows := []localization.Row{{Tag: "en", IsDefault: true, Enabled: true}, {Tag: "fr", Enabled: false}}
t.Run("HasRow finds a disabled row and nothing else", func(t *testing.T) {
if !localization.HasRow(rows, "fr") || !localization.HasRow(rows, "en") || localization.HasRow(rows, "de") {
t.Fatal("HasRow answered wrongly")
}
})
t.Run("MissingTags lists each tag that is not enabled once, sorted, never the unassigned tag", func(t *testing.T) {
missing := localization.MissingTags(rows, []string{"fr", "", "de", "en", "fr"})
if len(missing) != 2 || missing[0] != "de" || missing[1] != "fr" {
t.Fatalf("missing = %v", missing)
}
})
}