Skip to content

Commit b534863

Browse files
committed
Test modification of editor value before stored to content
1 parent 51e1741 commit b534863

6 files changed

+115
-3
lines changed

.gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@ msbuild.wrn
3636
# Visual Studio 2015
3737
.vs/
3838

39+
umbraco/
40+
3941
UmbracoV9.Testing.Tests/umbraco/Data/
4042
UmbracoV9.Testing.Web/umbraco/Data/
4143
UmbracoV9.Testing.Web/umbraco/Logs/
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"dataValue": "Data from user"
3+
}

Our.Umbraco.Test.Examples/DataEditors/DataEditor_Via_Hosted_Controller.cs

+62-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
1-
using Newtonsoft.Json;
1+
using Azure;
2+
using Newtonsoft.Json;
23
using Newtonsoft.Json.Linq;
34
using Umbraco.Cms.Core;
45
using Umbraco.Cms.Core.Models;
6+
using Umbraco.Cms.Core.Models.ContentEditing;
7+
using Umbraco.Cms.Core.Models.Editors;
58
using Umbraco.Cms.Core.PropertyEditors;
69
using Umbraco.Cms.Core.Serialization;
710
using Umbraco.Cms.Core.Services;
@@ -17,6 +20,8 @@ namespace Our.Umbraco.Test.Examples.DataEditors;
1720

1821
public class DataEditor_Via_Hosted_Controller : UmbracoTestServerTestBase
1922
{
23+
private IContentType contentType;
24+
2025
[Test]
2126
public async Task Creates_Json_Model_For_New_Document()
2227
{
@@ -33,6 +38,48 @@ public async Task Creates_Json_Model_For_New_Document()
3338
await VerifyJson(property.ToString(Formatting.Indented));
3439
}
3540

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+
3683
[SetUp]
3784
public void SetupSchema()
3885
{
@@ -51,12 +98,12 @@ public void SetupSchema()
5198

5299
GetRequiredService<IDataTypeService>().Save(dataType);
53100

54-
var contentType = new ContentTypeBuilder()
101+
contentType = new ContentTypeBuilder()
55102
.WithoutIdentity()
56103
.WithName("My doctype")
57104
.WithAlias("myDoctype")
58105
.AddPropertyType()
59-
.WithAlias("my.property")
106+
.WithAlias("myProperty")
60107
.WithName("My property")
61108
.WithPropertyEditorAlias("my.editor")
62109
.WithDataTypeId(dataType.Id)
@@ -138,5 +185,17 @@ public MyDataValueEditor(ILocalizedTextService localizedTextService, IShortStrin
138185

139186
return jObj;
140187
}
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+
}
141200
}
142201

Our.Umbraco.Test.Examples/Our.Umbraco.Test.Examples.csproj

+16
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,22 @@
66
<Nullable>enable</Nullable>
77
</PropertyGroup>
88

9+
<ItemGroup>
10+
<Compile Remove="umbraco\**" />
11+
<EmbeddedResource Remove="umbraco\**" />
12+
<None Remove="umbraco\**" />
13+
</ItemGroup>
14+
15+
<ItemGroup>
16+
<None Remove="appsettings.Tests.json" />
17+
</ItemGroup>
18+
19+
<ItemGroup>
20+
<Content Include="appsettings.Tests.json">
21+
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
22+
</Content>
23+
</ItemGroup>
24+
925
<ItemGroup>
1026
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.10.0-release-24177-07" />
1127
<PackageReference Include="NUnit" Version="3.14.0" />
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
{
2+
"$schema": "./appsettings-schema.json",
3+
"Logging": {
4+
"LogLevel": {
5+
"Default": "Warning",
6+
"Umbraco.Cms.Tests": "Information"
7+
},
8+
"Console": {
9+
"DisableColors": true
10+
}
11+
},
12+
"Tests": {
13+
"Database": {
14+
"DatabaseType": "SQLite",
15+
"PrepareThreadCount": 4,
16+
"SchemaDatabaseCount": 4,
17+
"EmptyDatabasesCount": 2,
18+
"SQLServerMasterConnectionString": ""
19+
}
20+
},
21+
"Umbraco": {
22+
"CMS": {
23+
"Content": {
24+
"AllowedUploadedFileExtensions": ["jpg", "png", "gif", "svg"]
25+
}
26+
}
27+
}
28+
}

diary.md

+4
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
Exclude the "umbraco" folder to avoid getting compilation problems from auto-enabled models builder.
2+
3+
-----
4+
15
Must change appsettings.Tests.json to "Copy to ouput dir" something.
26
Also this file has a physical reference when package is installed.
37
Change %userprofile%\nuget\packages to $(NuGetPackageRoot)

0 commit comments

Comments
 (0)