Files
sm-user-plugin/models/slug_test.go
Jakub Zych 493a3ff47c test: unit tests for the admin class functions, the models and the admin registration
- classes: every admin action for its changed-row count and idempotency, the ban across several throttle rows, the cleanup of a permanent delete
- classes: the privileged list and IsPrivilegedMember (D-30), also through a handle that carries conditions
- classes: the five-minute boundary of last_seen
- models: PermissionSet round trip, the slug table shared with the SPA, Fillable and Rules, fields that never marshal, the group filter value
- TestAdminRegistration: permissions, navigation, embedded files and phrase keys in en and pl; the admin secret is required
2026-10-05 15:15:28 +02:00

54 lines
1.7 KiB
Go

package models
import (
"strings"
"testing"
)
// slugCases is the table the admin SPA's slug preset is tested against as
// well (admin/tests/form/formState.test.ts in the framework repository,
// "slug preset matches the server rule"): the organisation model and the form
// must derive the same slug from the same name.
var slugCases = []struct{ text, want string }{
{"Acme Records", "acme-records"},
{" Acme -- Records ", "acme-records"},
{"ACME_Records 2", "acme-records-2"},
{"Zażółć gęślą", "za-g-l"},
{"Rock & Roll!!!", "rock-roll"},
{"...leading and trailing...", "leading-and-trailing"},
{"tab\tand\nnewline", "tab-and-newline"},
{"emoji 🎵 music", "emoji-music"},
{"CamelCaseText", "camelcasetext"},
{"9 lives", "9-lives"},
{"a", "a"},
{"---", ""},
{" ", ""},
{"", ""},
{"ąęść", ""},
{strings.Repeat("Long Name ", 40), strings.TrimSuffix(strings.Repeat("long-name-", 40), "-")},
}
func TestSlugifyTable(t *testing.T) {
for _, tc := range slugCases {
if got := Slugify(tc.text); got != tc.want {
t.Errorf("Slugify(%q) = %q, want %q", tc.text, got, tc.want)
}
}
// A slug is a fixed point, and it only holds lower-case ASCII letters,
// digits and single hyphens between them.
for _, tc := range slugCases {
slug := Slugify(tc.text)
if again := Slugify(slug); again != slug {
t.Errorf("Slugify(Slugify(%q)) = %q, want %q", tc.text, again, slug)
}
if strings.HasPrefix(slug, "-") || strings.HasSuffix(slug, "-") || strings.Contains(slug, "--") {
t.Errorf("Slugify(%q) = %q has a stray hyphen", tc.text, slug)
}
for _, r := range slug {
if !(r >= 'a' && r <= 'z') && !(r >= '0' && r <= '9') && r != '-' {
t.Errorf("Slugify(%q) = %q holds %q", tc.text, slug, r)
}
}
}
}