1
- using Newtonsoft . Json ;
1
+ using Azure ;
2
+ using Newtonsoft . Json ;
2
3
using Newtonsoft . Json . Linq ;
3
4
using Umbraco . Cms . Core ;
4
5
using Umbraco . Cms . Core . Models ;
6
+ using Umbraco . Cms . Core . Models . ContentEditing ;
7
+ using Umbraco . Cms . Core . Models . Editors ;
5
8
using Umbraco . Cms . Core . PropertyEditors ;
6
9
using Umbraco . Cms . Core . Serialization ;
7
10
using Umbraco . Cms . Core . Services ;
@@ -17,6 +20,8 @@ namespace Our.Umbraco.Test.Examples.DataEditors;
17
20
18
21
public class DataEditor_Via_Hosted_Controller : UmbracoTestServerTestBase
19
22
{
23
+ private IContentType contentType ;
24
+
20
25
[ Test ]
21
26
public async Task Creates_Json_Model_For_New_Document ( )
22
27
{
@@ -33,6 +38,48 @@ public async Task Creates_Json_Model_For_New_Document()
33
38
await VerifyJson ( property . ToString ( Formatting . Indented ) ) ;
34
39
}
35
40
41
+ [ Test ]
42
+ public async Task Saves_Property_Value_To_Content ( )
43
+ {
44
+ var url = PrepareApiControllerUrl < ContentController > ( x => x . PostSave ( null ! ) ) ;
45
+
46
+ var content = new ContentBuilder ( )
47
+ . WithId ( 0 )
48
+ . WithName ( "My content" )
49
+ . WithContentType ( contentType )
50
+ . AddPropertyData ( )
51
+ . WithKeyValue ( "myProperty" ,
52
+ """
53
+ {
54
+ "dataForBackoffice": "Some data we don't want to store",
55
+ "dataValue": "Data from user"
56
+ }
57
+ """
58
+ )
59
+ . Done ( )
60
+ . Build ( ) ;
61
+
62
+ var model = new ContentItemSaveBuilder ( )
63
+ . WithContent ( content )
64
+ . WithParentId ( - 1 )
65
+ . WithAction ( ContentSaveAction . PublishNew )
66
+ . Build ( ) ;
67
+
68
+ var payload = JObject . FromObject ( model ) ;
69
+
70
+ var response = await Client . PostAsync ( url , new MultipartFormDataContent { { new StringContent ( payload . ToString ( ) ) , "contentItem" } } ) ;
71
+
72
+ var json = await response . Content . ReadAsStringAsync ( ) ;
73
+ json = json . TrimStart ( AngularJsonMediaTypeFormatter . XsrfPrefix ) ;
74
+ var responseModel = JObject . Parse ( json ) ;
75
+
76
+ var id = responseModel . Value < int > ( "id" ) ;
77
+ var createdContent = GetRequiredService < IContentService > ( ) . GetById ( id ) ! ;
78
+ var propValue = createdContent . GetValue ( "myProperty" ) ;
79
+
80
+ await Verify ( propValue ) ;
81
+ }
82
+
36
83
[ SetUp ]
37
84
public void SetupSchema ( )
38
85
{
@@ -51,12 +98,12 @@ public void SetupSchema()
51
98
52
99
GetRequiredService < IDataTypeService > ( ) . Save ( dataType ) ;
53
100
54
- var contentType = new ContentTypeBuilder ( )
101
+ contentType = new ContentTypeBuilder ( )
55
102
. WithoutIdentity ( )
56
103
. WithName ( "My doctype" )
57
104
. WithAlias ( "myDoctype" )
58
105
. AddPropertyType ( )
59
- . WithAlias ( "my.property " )
106
+ . WithAlias ( "myProperty " )
60
107
. WithName ( "My property" )
61
108
. WithPropertyEditorAlias ( "my.editor" )
62
109
. WithDataTypeId ( dataType . Id )
@@ -138,5 +185,17 @@ public MyDataValueEditor(ILocalizedTextService localizedTextService, IShortStrin
138
185
139
186
return jObj ;
140
187
}
188
+
189
+ public override object ? FromEditor ( ContentPropertyData editorValue , object ? currentValue )
190
+ {
191
+ var value = editorValue . Value as string ;
192
+ var jObj = JObject . Parse ( value ?? "{}" ) ;
193
+ if ( jObj . ContainsKey ( "dataForBackoffice" ) )
194
+ {
195
+ jObj . Remove ( "dataForBackoffice" ) ;
196
+ }
197
+
198
+ return jObj . ToString ( ) ;
199
+ }
141
200
}
142
201
0 commit comments