test(17-06): menu plugin unit tests
- TestAllowedURL and TestMenuTree (Docker-free): the URL scheme and target rules, BuildTree order, unreachable and looping entries, list and single registry types, generated children, locale alternates, left-out entries logged, the JSON shape - TestMenuEntryModel (Docker-free): fillable, rules, filter scope - TestMenuEntryBeforeSave, TestMenuAPI and TestMenuImport (Postgres): PHP beforeSave rules, D-16 API rules beyond the smoke test, import drops for page subtrees, foreign menus, missing parents, maps to nothing and unregistered types
This commit is contained in:
64
models/menu_entry_test.go
Normal file
64
models/menu_entry_test.go
Normal file
@@ -0,0 +1,64 @@
|
||||
package models_test
|
||||
|
||||
import (
|
||||
"slices"
|
||||
"testing"
|
||||
|
||||
"git.golem15.com/golem15/sm-menu-plugin/models"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
// TestMenuEntryModel pins the Docker-free contract of the menu entry model:
|
||||
// the built-in types, the PHP class string and table, the fillable list
|
||||
// (no nest columns, no sort_order, no id), the required type rule and the
|
||||
// menu filter scope's no-op cases. BeforeSave runs on Postgres
|
||||
// (TestMenuEntryBeforeSave).
|
||||
func TestMenuEntryModel(t *testing.T) {
|
||||
for typ, want := range map[string]bool{"url": true, "divider": true, "link": false, "acme-page": false, "": false} {
|
||||
if models.Builtin(typ) != want {
|
||||
t.Errorf("Builtin(%q) != %v", typ, want)
|
||||
}
|
||||
}
|
||||
e := models.MenuEntry{}
|
||||
if e.TableName() != "golem15_menu_menu_entries" || e.MorphName() != `Golem15\Menu\Models\MenuEntry` {
|
||||
t.Fatal("table or morph name changed")
|
||||
}
|
||||
fill := e.Fillable()
|
||||
for _, key := range []string{"menu_id", "parent_id", "type", "title", "url", "url_pattern", "reference", "code", "icon", "css_class", "target", "is_published"} {
|
||||
if !slices.Contains(fill, key) {
|
||||
t.Errorf("Fillable lacks %s", key)
|
||||
}
|
||||
}
|
||||
for _, key := range []string{"id", "nest_left", "nest_right", "nest_depth", "sort_order", "created_at", "updated_at"} {
|
||||
if slices.Contains(fill, key) {
|
||||
t.Errorf("Fillable holds %s", key)
|
||||
}
|
||||
}
|
||||
if rules := e.Rules(); len(rules) != 1 || rules["type"] != "required" {
|
||||
t.Fatalf("Rules = %v", rules)
|
||||
}
|
||||
if !slices.Equal(e.FilterScopes(), []string{models.FilterMenu}) {
|
||||
t.Fatalf("FilterScopes = %v", e.FilterScopes())
|
||||
}
|
||||
db := &gorm.DB{}
|
||||
for _, tc := range []struct {
|
||||
name string
|
||||
scope string
|
||||
value any
|
||||
}{
|
||||
{"another scope", "Other", "1"},
|
||||
{"no value", models.FilterMenu, nil},
|
||||
{"empty list", models.FilterMenu, []any{}},
|
||||
{"only invalid ids", models.FilterMenu, []string{"x", "0", "-1", ""}},
|
||||
} {
|
||||
if got := e.FilterScope(tc.scope, db, tc.value); got != db {
|
||||
t.Errorf("%s: FilterScope changed the query", tc.name)
|
||||
}
|
||||
}
|
||||
if e.FilterScope(models.FilterMenu, nil, "1") != nil {
|
||||
t.Fatal("a nil query was replaced")
|
||||
}
|
||||
if models.Text(nil) != "" {
|
||||
t.Fatal("Text(nil)")
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user