-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathDataEditor_Via_Hosted_Controller.cs
205 lines (171 loc) · 6.52 KB
/
DataEditor_Via_Hosted_Controller.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
using Azure;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using Umbraco.Cms.Core;
using Umbraco.Cms.Core.Models;
using Umbraco.Cms.Core.Models.ContentEditing;
using Umbraco.Cms.Core.Models.Editors;
using Umbraco.Cms.Core.PropertyEditors;
using Umbraco.Cms.Core.Serialization;
using Umbraco.Cms.Core.Services;
using Umbraco.Cms.Core.Strings;
using Umbraco.Cms.Tests.Common.Builders;
using Umbraco.Cms.Tests.Common.Builders.Extensions;
using Umbraco.Cms.Tests.Common.Testing;
using Umbraco.Cms.Tests.Integration.TestServerTest;
using Umbraco.Cms.Web.BackOffice.Controllers;
using Umbraco.Cms.Web.Common.Formatters;
using Umbraco.Extensions;
namespace Our.Umbraco.Test.Examples.DataEditors;
[UmbracoTest(Database = UmbracoTestOptions.Database.NewSchemaPerFixture)]
public class DataEditor_Via_Hosted_Controller : UmbracoTestServerTestBase
{
private IContentType? contentType;
[Test]
public async Task Creates_Json_Model_For_New_Document()
{
var url = PrepareApiControllerUrl<ContentController>(x => x.GetEmpty("myDoctype", -1));
var response = await Client.GetAsync(url);
var json = await response.Content.ReadAsStringAsync();
json = json.TrimStart(AngularJsonMediaTypeFormatter.XsrfPrefix);
var model = JObject.Parse(json);
var property = model.SelectToken("variants[0].tabs[0].properties[0]")!;
await VerifyJson(property.ToString(Formatting.Indented));
}
[Test]
public async Task Saves_Property_Value_To_Content()
{
var url = PrepareApiControllerUrl<ContentController>(x => x.PostSave(null!));
var content = new ContentBuilder()
.WithId(0)
.WithName("My content")
.WithContentType(contentType)
.AddPropertyData()
.WithKeyValue("myProperty",
"""
{
"dataForBackoffice": "Some data we don't want to store",
"dataValue": "Data from user"
}
"""
)
.Done()
.Build();
var model = new ContentItemSaveBuilder()
.WithContent(content)
.WithParentId(-1)
.WithAction(ContentSaveAction.PublishNew)
.Build();
var payload = JObject.FromObject(model);
var response = await Client.PostAsync(url, new MultipartFormDataContent { { new StringContent(payload.ToString()), "contentItem" } });
var json = await response.Content.ReadAsStringAsync();
json = json.TrimStart(AngularJsonMediaTypeFormatter.XsrfPrefix);
var responseModel = JObject.Parse(json);
var id = responseModel.Value<int>("id");
var createdContent = GetRequiredService<IContentService>().GetById(id)!;
var propValue = createdContent.GetValue("myProperty");
await Verify(propValue);
}
[SetUp]
public void SetupSchema()
{
if (contentType != null) return;
var dataType = new DataTypeBuilder()
.WithoutIdentity()
.WithKey(new Guid("87c4db0b-4cee-4c49-9668-7607bf93fdb2"))
.WithDatabaseType(ValueStorageType.Ntext)
.WithName("My editor")
.AddEditor()
.WithAlias("my.editor")
.AddExplicitValueEditorBuilder()
.WithValueType(ValueTypes.Json)
.Done()
.Done()
.Build();
GetRequiredService<IDataTypeService>().Save(dataType);
contentType = new ContentTypeBuilder()
.WithoutIdentity()
.WithName("My doctype")
.WithAlias("myDoctype")
.AddPropertyType()
.WithAlias("myProperty")
.WithName("My property")
.WithPropertyEditorAlias("my.editor")
.WithDataTypeId(dataType.Id)
.Done()
.Build();
GetRequiredService<IContentTypeService>().Save(contentType);
}
}
public class MyEditor : IDataEditor
{
public const string PreviewPath = "/App_Plugins/MyEditor/editor.html";
private readonly ILocalizedTextService localizedTextService;
private readonly IShortStringHelper shortStringHelper;
private readonly IJsonSerializer jsonSerializer;
public MyEditor(
ILocalizedTextService localizedTextService,
IShortStringHelper shortStringHelper,
IJsonSerializer jsonSerializer
)
{
this.localizedTextService = localizedTextService;
this.shortStringHelper = shortStringHelper;
this.jsonSerializer = jsonSerializer;
}
public IDataValueEditor GetValueEditor()
{
return GetValueEditor(null);
}
public IDataValueEditor GetValueEditor(object? configuration)
{
return new MyDataValueEditor(
localizedTextService,
shortStringHelper,
jsonSerializer
)
{
ValueType = ValueTypes.Json,
View = PreviewPath
};
}
public IConfigurationEditor GetConfigurationEditor()
{
return new ConfigurationEditor();
}
public string Alias => "my.editor";
public string Name => "My editor";
public EditorType Type => EditorType.PropertyValue;
public string Icon => "icon-user";
public string Group => Constants.PropertyEditors.Groups.Common;
public bool IsDeprecated => false;
public IDictionary<string, object>? DefaultConfiguration => new Dictionary<string, object>();
public IPropertyIndexValueFactory PropertyIndexValueFactory => new DefaultPropertyIndexValueFactory();
}
public class MyDataValueEditor : DataValueEditor
{
public MyDataValueEditor(ILocalizedTextService localizedTextService, IShortStringHelper shortStringHelper, IJsonSerializer jsonSerializer)
: base(localizedTextService, shortStringHelper, jsonSerializer)
{
}
public override object? ToEditor(IProperty property, string? culture = null, string? segment = null)
{
var value = property.GetValue(culture, segment) as string;
var jObj = JObject.Parse(value ?? "{}");
if (!jObj.ContainsKey("dataForBackoffice"))
{
jObj["dataForBackoffice"] = "Some extra data for the view";
}
return jObj;
}
public override object? FromEditor(ContentPropertyData editorValue, object? currentValue)
{
var value = editorValue.Value as string;
var jObj = JObject.Parse(value ?? "{}");
if (jObj.ContainsKey("dataForBackoffice"))
{
jObj.Remove("dataForBackoffice");
}
return jObj.ToString();
}
}