Skip to content

Commit b24ee38

Browse files
committed
Added unit-test for PropertyList value-connector
1 parent 7b765fb commit b24ee38

File tree

3 files changed

+191
-1
lines changed

3 files changed

+191
-1
lines changed

src/Umbraco.Deploy.Contrib.Connectors/ValueConnectors/PropertyListValueConnector.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ public void SetValue(IContentBase content, string alias, string value)
106106
}
107107

108108
// serialize the JSON values
109-
content.SetValue(alias, JObject.FromObject(model).ToString());
109+
content.SetValue(alias, JObject.FromObject(model).ToString(Formatting.None));
110110
}
111111

112112
public class PropertyListValue
Lines changed: 189 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,189 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using Moq;
5+
using NUnit.Framework;
6+
using Umbraco.Core;
7+
using Umbraco.Core.Configuration;
8+
using Umbraco.Core.Configuration.UmbracoSettings;
9+
using Umbraco.Core.Deploy;
10+
using Umbraco.Core.Models;
11+
using Umbraco.Core.Services;
12+
using Umbraco.Deploy.Contrib.Connectors.ValueConnectors;
13+
using Umbraco.Deploy.ValueConnectors;
14+
15+
namespace Umbraco.Deploy.Contrib.Tests.Connectors
16+
{
17+
[TestFixture]
18+
public class PropertyListValueConnectorTests
19+
{
20+
[Test]
21+
public void GetValueTest()
22+
{
23+
var dataTypeService = Mock.Of<IDataTypeService>();
24+
25+
var propListDataType = new DataTypeDefinition("propListEditorAlias")
26+
{
27+
Id = 1,
28+
Key = Guid.Parse("74AFF355-537A-4443-9801-C131FE83FF1F"),
29+
DatabaseType = DataTypeDatabaseType.Ntext
30+
};
31+
32+
var innerDataType = new DataTypeDefinition("innerEditorAlias")
33+
{
34+
Id = 2,
35+
Key = Guid.Parse("D21BA417-98AC-4D05-8EF9-0ED3D75A8C0D"),
36+
DatabaseType = DataTypeDatabaseType.Integer // for true/false
37+
};
38+
39+
var dataTypes = new[] { propListDataType, innerDataType };
40+
41+
Mock.Get(dataTypeService)
42+
.Setup(x => x.GetDataTypeDefinitionById(It.IsAny<Guid>()))
43+
.Returns<Guid>(id => dataTypes.FirstOrDefault(x => x.Key == id));
44+
45+
var preValues = new Dictionary<int, PreValueCollection>
46+
{
47+
{ 1, new PreValueCollection(new Dictionary<string, PreValue>
48+
{
49+
{ "dataType", new PreValue(innerDataType.Key.ToString()) }
50+
})
51+
}
52+
};
53+
54+
Mock.Get(dataTypeService)
55+
.Setup(x => x.GetPreValuesCollectionByDataTypeId(It.IsAny<int>()))
56+
.Returns<int>(id => preValues.TryGetValue(id, out var collection) ? collection : null);
57+
58+
ValueConnectorCollection connectors = null;
59+
var defaultConnector = new DefaultValueConnector();
60+
var propListConnector = new PropertyListValueConnector(dataTypeService, new Lazy<ValueConnectorCollection>(() => connectors));
61+
connectors = new ValueConnectorCollection(new Dictionary<string, IValueConnector>
62+
{
63+
{ "innerEditorAlias", defaultConnector },
64+
{ "propListEditorAlias", propListConnector }
65+
});
66+
67+
var input = $"{{\"dtd\":\"{innerDataType.Key}\",\"values\":[0]}}";
68+
69+
var propertyType = new PropertyType(propListDataType);
70+
var property = new Property(propertyType, input);
71+
var dependencies = new List<ArtifactDependency>();
72+
var output = propListConnector.GetValue(property, dependencies);
73+
74+
Console.WriteLine(output);
75+
76+
var expected = $"{{\"dtd\":\"{innerDataType.Key}\",\"values\":[\"i0\"]}}";
77+
Assert.AreEqual(expected, output);
78+
}
79+
80+
[Test]
81+
public void SetValueTest()
82+
{
83+
var dataTypeService = Mock.Of<IDataTypeService>();
84+
85+
var propListDataType = new DataTypeDefinition("propListEditorAlias")
86+
{
87+
Id = 1,
88+
Key = Guid.Parse("74AFF355-537A-4443-9801-C131FE83FF1F"),
89+
DatabaseType = DataTypeDatabaseType.Ntext
90+
};
91+
92+
var innerDataType = new DataTypeDefinition("innerEditorAlias")
93+
{
94+
Id = 2,
95+
Key = Guid.Parse("D21BA417-98AC-4D05-8EF9-0ED3D75A8C0D"),
96+
DatabaseType = DataTypeDatabaseType.Integer // for true/false
97+
};
98+
99+
var dataTypes = new[] { propListDataType, innerDataType };
100+
101+
Mock.Get(dataTypeService)
102+
.Setup(x => x.GetDataTypeDefinitionById(It.IsAny<Guid>()))
103+
.Returns<Guid>(id => dataTypes.FirstOrDefault(x => x.Key == id));
104+
105+
var preValues = new Dictionary<int, PreValueCollection>
106+
{
107+
{ 1, new PreValueCollection(new Dictionary<string, PreValue>
108+
{
109+
{ "dataType", new PreValue(innerDataType.Key.ToString()) }
110+
})
111+
}
112+
};
113+
114+
Mock.Get(dataTypeService)
115+
.Setup(x => x.GetPreValuesCollectionByDataTypeId(It.IsAny<int>()))
116+
.Returns<int>(id => preValues.TryGetValue(id, out var collection) ? collection : null);
117+
118+
ValueConnectorCollection connectors = null;
119+
var defaultConnector = new DefaultValueConnector();
120+
var propListConnector = new PropertyListValueConnector(dataTypeService, new Lazy<ValueConnectorCollection>(() => connectors));
121+
connectors = new ValueConnectorCollection(new Dictionary<string, IValueConnector>
122+
{
123+
{ "innerEditorAlias", defaultConnector },
124+
{ "propListEditorAlias", propListConnector }
125+
});
126+
127+
var input = $"{{\"dtd\":\"{innerDataType.Key}\",\"values\":[\"i0\"]}}";
128+
129+
UmbracoConfig.For.SetUmbracoSettings(GenerateMockSettings());
130+
131+
var propListPropertyType = new PropertyType(propListDataType, "propListProperty");
132+
var propListProperty = new Property(propListPropertyType, null); // value is going to be replaced
133+
var propListContent = new Content("mockContent", -1, new ContentType(-1), new PropertyCollection(new List<Property> { propListProperty }));
134+
propListConnector.SetValue(propListContent, "propListProperty", input);
135+
136+
var output = propListContent.GetValue("propListProperty");
137+
138+
Assert.IsInstanceOf<string>(output);
139+
140+
Console.WriteLine(output);
141+
142+
var expected = $"{{\"dtd\":\"{innerDataType.Key}\",\"values\":[0]}}";
143+
Assert.AreEqual(expected, output);
144+
}
145+
146+
public static IUmbracoSettingsSection GenerateMockSettings()
147+
{
148+
var settings = new Mock<IUmbracoSettingsSection>();
149+
150+
var content = new Mock<IContentSection>();
151+
var security = new Mock<ISecuritySection>();
152+
var requestHandler = new Mock<IRequestHandlerSection>();
153+
var templates = new Mock<ITemplatesSection>();
154+
var dev = new Mock<IDeveloperSection>();
155+
var logging = new Mock<ILoggingSection>();
156+
var tasks = new Mock<IScheduledTasksSection>();
157+
var distCall = new Mock<IDistributedCallSection>();
158+
var repos = new Mock<IRepositoriesSection>();
159+
var providers = new Mock<IProvidersSection>();
160+
var routing = new Mock<IWebRoutingSection>();
161+
162+
settings.Setup(x => x.Content).Returns(content.Object);
163+
settings.Setup(x => x.Security).Returns(security.Object);
164+
settings.Setup(x => x.RequestHandler).Returns(requestHandler.Object);
165+
settings.Setup(x => x.Templates).Returns(templates.Object);
166+
settings.Setup(x => x.Developer).Returns(dev.Object);
167+
settings.Setup(x => x.Logging).Returns(logging.Object);
168+
settings.Setup(x => x.ScheduledTasks).Returns(tasks.Object);
169+
settings.Setup(x => x.DistributedCall).Returns(distCall.Object);
170+
settings.Setup(x => x.PackageRepositories).Returns(repos.Object);
171+
settings.Setup(x => x.Providers).Returns(providers.Object);
172+
settings.Setup(x => x.WebRouting).Returns(routing.Object);
173+
174+
//Now configure some defaults - the defaults in the config section classes do NOT pertain to the mocked data!!
175+
settings.Setup(x => x.Content.ForceSafeAliases).Returns(true);
176+
//settings.Setup(x => x.Content.ImageAutoFillProperties).Returns(ContentImagingElement.GetDefaultImageAutoFillProperties());
177+
//settings.Setup(x => x.Content.ImageFileTypes).Returns(ContentImagingElement.GetDefaultImageFileTypes());
178+
settings.Setup(x => x.RequestHandler.AddTrailingSlash).Returns(true);
179+
settings.Setup(x => x.RequestHandler.UseDomainPrefixes).Returns(false);
180+
//settings.Setup(x => x.RequestHandler.CharCollection).Returns(RequestHandlerElement.GetDefaultCharReplacements());
181+
settings.Setup(x => x.Content.UmbracoLibraryCacheDuration).Returns(1800);
182+
settings.Setup(x => x.WebRouting.UrlProviderMode).Returns("AutoLegacy");
183+
settings.Setup(x => x.Templates.DefaultRenderingEngine).Returns(RenderingEngine.Mvc);
184+
settings.Setup(x => x.Providers.DefaultBackOfficeUserProvider).Returns("UsersMembershipProvider");
185+
186+
return settings.Object;
187+
}
188+
}
189+
}

src/Umbraco.Deploy.Contrib.Tests/Umbraco.Deploy.Contrib.Tests.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -260,6 +260,7 @@
260260
<Compile Include="Connectors\LeBlenderGridCellValueConnectorTests.cs" />
261261
<Compile Include="Connectors\NestedContentValueConnectorTests.cs" />
262262
<Compile Include="Connectors\UrlPickerValueConnectorTests.cs" />
263+
<Compile Include="Connectors\PropertyListValueConnectorTests.cs" />
263264
<Compile Include="Connectors\VortoValueConnectorTests.cs" />
264265
<Compile Include="Properties\AssemblyInfo.cs" />
265266
<Compile Include="TestHelpers\MemoryFileTypeCollection.cs" />

0 commit comments

Comments
 (0)