@@ -783,6 +783,9 @@ public function it_skips_validation_for_import_fields()
783783 $ user = User ::make ()->assignRole ('test ' )->save ();
784784 $ form = tap (Form ::make ('test ' ))->save ();
785785
786+ $ fieldset = Fieldset ::make ('some_fieldset ' )->setContents (['fields ' => []]);
787+ Fieldset ::shouldReceive ('find ' )->with ('some_fieldset ' )->andReturn ($ fieldset );
788+
786789 $ payload = [
787790 'pages ' => [
788791 [
@@ -811,6 +814,116 @@ public function it_skips_validation_for_import_fields()
811814 ->assertSuccessful ();
812815 }
813816
817+ #[Test]
818+ public function it_validates_that_imported_fieldsets_exist ()
819+ {
820+ $ this ->setTestRoles (['test ' => ['access cp ' , 'configure forms ' ]]);
821+ $ user = User ::make ()->assignRole ('test ' )->save ();
822+ $ form = tap (Form ::make ('test ' ))->save ();
823+
824+ $ payload = [
825+ 'pages ' => [
826+ [
827+ '_id ' => 'page1 ' ,
828+ 'sections ' => [
829+ [
830+ '_id ' => 'section1 ' ,
831+ 'display ' => 'Section ' ,
832+ 'fields ' => [
833+ [
834+ '_id ' => 'import1 ' ,
835+ 'type ' => 'import ' ,
836+ 'fieldset ' => 'missing ' ,
837+ 'prefix ' => null ,
838+ ],
839+ ],
840+ ],
841+ ],
842+ ],
843+ ],
844+ ];
845+
846+ $ this
847+ ->actingAs ($ user )
848+ ->patch (cp_route ('forms.builder.update ' , $ form ->handle ()), $ payload )
849+ ->assertSessionHasErrors (['import1.fieldset ' => 'Fieldset missing does not exist. ' ]);
850+
851+ $ this ->assertEmpty (Form ::find ('test ' )->formFields ()->items ());
852+ }
853+
854+ #[Test]
855+ public function it_validates_that_fieldtypes_exist ()
856+ {
857+ $ this ->setTestRoles (['test ' => ['access cp ' , 'configure forms ' ]]);
858+ $ user = User ::make ()->assignRole ('test ' )->save ();
859+ $ form = tap (Form ::make ('test ' ))->save ();
860+
861+ $ payload = [
862+ 'pages ' => [
863+ [
864+ '_id ' => 'page1 ' ,
865+ 'sections ' => [
866+ [
867+ '_id ' => 'section1 ' ,
868+ 'display ' => 'Section ' ,
869+ 'fields ' => [
870+ [
871+ '_id ' => 'field1 ' ,
872+ 'handle ' => 'name ' ,
873+ 'type ' => 'inline ' ,
874+ 'fieldtype ' => 'missing ' ,
875+ 'config ' => ['type ' => 'missing ' , 'display ' => 'Name ' ],
876+ ],
877+ ],
878+ ],
879+ ],
880+ ],
881+ ],
882+ ];
883+
884+ $ this
885+ ->actingAs ($ user )
886+ ->patch (cp_route ('forms.builder.update ' , $ form ->handle ()), $ payload )
887+ ->assertSessionHasErrors (['field1.fieldtype ' => 'Fieldtype missing does not exist. ' ]);
888+ }
889+
890+ #[Test]
891+ #[DataProvider('malformedFieldProvider ' )]
892+ public function it_validates_malformed_fields ($ field , $ error )
893+ {
894+ $ this ->setTestRoles (['test ' => ['access cp ' , 'configure forms ' ]]);
895+ $ user = User ::make ()->assignRole ('test ' )->save ();
896+ $ form = tap (Form ::make ('test ' ))->save ();
897+
898+ $ payload = [
899+ 'pages ' => [
900+ [
901+ '_id ' => 'page1 ' ,
902+ 'sections ' => [
903+ ['_id ' => 'section1 ' , 'display ' => 'Section ' , 'fields ' => [$ field ]],
904+ ],
905+ ],
906+ ],
907+ ];
908+
909+ $ this
910+ ->actingAs ($ user )
911+ ->patch (cp_route ('forms.builder.update ' , $ form ->handle ()), $ payload )
912+ ->assertSessionHasErrors ("pages.0.sections.0.fields.0. {$ error }" );
913+ }
914+
915+ public static function malformedFieldProvider ()
916+ {
917+ return [
918+ 'missing id ' => [['type ' => 'import ' , 'fieldset ' => 'contact ' ], '_id ' ],
919+ 'missing type ' => [['_id ' => 'field1 ' , 'handle ' => 'name ' , 'fieldtype ' => 'short_answer ' ], 'type ' ],
920+ 'unknown type ' => [['_id ' => 'field1 ' , 'handle ' => 'name ' , 'type ' => 'unknown ' , 'fieldtype ' => 'short_answer ' ], 'type ' ],
921+ 'inline field without fieldtype ' => [['_id ' => 'field1 ' , 'handle ' => 'name ' , 'type ' => 'inline ' , 'config ' => []], 'fieldtype ' ],
922+ 'reference field without fieldtype ' => [['_id ' => 'field1 ' , 'handle ' => 'name ' , 'type ' => 'reference ' , 'field_reference ' => 'contact.name ' ], 'fieldtype ' ],
923+ 'import without fieldset ' => [['_id ' => 'import1 ' , 'type ' => 'import ' ], 'fieldset ' ],
924+ ];
925+ }
926+
814927 #[Test]
815928 public function it_filters_out_link_fields_placeholders_when_saving ()
816929 {
0 commit comments