@@ -652,3 +652,63 @@ func TestCreateItemOptionsPrimaryKeyOverride(t *testing.T) {
652652 t .Fatalf ("no options must merge to the zero value, got %#v" , merged )
653653 }
654654}
655+
656+ // TestChangeAttrViewLayoutAcceptsCalendar 回归测试:
657+ // /api/av/changeAttrViewLayout 的后端校验必须接受日历布局,否则切换到日历视图会被
658+ // av.ErrWrongLayoutType 拒绝;切换后布局、名称、字段迁移和日期字段选择都必须正确,
659+ // 非法布局仍然必须被拒绝。
660+ func TestChangeAttrViewLayoutAcceptsCalendar (t * testing.T ) {
661+ oldLangs := util .AttrViewLangs [util .Lang ]
662+ util .AttrViewLangs [util .Lang ] = map [string ]any {
663+ "table" : "table" ,
664+ "gallery" : "gallery" ,
665+ "kanban" : "kanban" ,
666+ "calendar" : "calendar" ,
667+ }
668+ t .Cleanup (func () {
669+ util .AttrViewLangs [util .Lang ] = oldLangs
670+ })
671+
672+ attrView := & av.AttributeView {
673+ KeyValues : []* av.KeyValues {
674+ {Key : & av.Key {ID : "date" , Type : av .KeyTypeDate }},
675+ },
676+ }
677+ view := & av.View {
678+ ID : "view1" ,
679+ Name : av .GetAttributeViewI18n ("table" ),
680+ LayoutType : av .LayoutTypeTable ,
681+ Table : & av.LayoutTable {
682+ Columns : []* av.ViewTableColumn {
683+ {BaseField : & av.BaseField {ID : "date" }},
684+ },
685+ },
686+ }
687+
688+ if err := changeAttrViewLayout (attrView , view , av .LayoutTypeCalendar ); nil != err {
689+ t .Fatalf ("calendar layout must be accepted: %v" , err )
690+ }
691+ if av .LayoutTypeCalendar != view .LayoutType {
692+ t .Fatalf ("expected calendar layout, got %s" , view .LayoutType )
693+ }
694+ if nil == view .Calendar {
695+ t .Fatal ("expected calendar layout instance" )
696+ }
697+ if av .GetAttributeViewI18n ("calendar" ) != view .Name {
698+ t .Fatalf ("expected view name to switch to calendar, got %s" , view .Name )
699+ }
700+ if 1 != len (view .Calendar .Fields ) || "date" != view .Calendar .Fields [0 ].ID {
701+ t .Fatalf ("expected the table column to migrate into calendar fields, got %v" , view .Calendar .Fields )
702+ }
703+ if "date" != view .Calendar .DateFieldID {
704+ t .Fatalf ("expected the date field to be selected as calendar date field, got %s" , view .Calendar .DateFieldID )
705+ }
706+
707+ // 切换到相同布局必须是无操作,非法布局仍然必须被拒绝
708+ if err := changeAttrViewLayout (attrView , view , av .LayoutTypeCalendar ); nil != err {
709+ t .Fatalf ("switching to the same layout should be a no-op: %v" , err )
710+ }
711+ if err := changeAttrViewLayout (attrView , view , av .LayoutType ("bogus" )); av .ErrWrongLayoutType != err {
712+ t .Fatalf ("invalid layout must still be rejected, got %v" , err )
713+ }
714+ }
0 commit comments