Gets the block list property value converter tests running and written
This commit is contained in:
@@ -25,6 +25,7 @@ namespace Umbraco.Core
|
||||
{ Unknown, UdiType.Unknown },
|
||||
|
||||
{ AnyGuid, UdiType.GuidUdi },
|
||||
{ Element, UdiType.GuidUdi },
|
||||
{ Document, UdiType.GuidUdi },
|
||||
{ DocumentBlueprint, UdiType.GuidUdi },
|
||||
{ Media, UdiType.GuidUdi },
|
||||
@@ -64,6 +65,8 @@ namespace Umbraco.Core
|
||||
|
||||
public const string AnyGuid = "any-guid"; // that one is for tests
|
||||
|
||||
public const string Element = "element";
|
||||
|
||||
public const string Document = "document";
|
||||
|
||||
public const string DocumentBlueprint = "document-blueprint";
|
||||
|
||||
@@ -13,7 +13,7 @@ namespace Umbraco.Core.Models.Blocks
|
||||
public BlockListLayoutReference(Udi udi, IPublishedElement settings)
|
||||
{
|
||||
Udi = udi ?? throw new ArgumentNullException(nameof(udi));
|
||||
Settings = settings ?? throw new ArgumentNullException(nameof(settings));
|
||||
Settings = settings; // can be null
|
||||
}
|
||||
|
||||
[DataMember(Name = "udi")]
|
||||
|
||||
@@ -30,18 +30,23 @@ namespace Umbraco.Core.PropertyEditors
|
||||
return value != null && (!(value is string) || string.IsNullOrWhiteSpace((string) value) == false);
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public virtual Type GetPropertyValueType(IPublishedPropertyType propertyType)
|
||||
=> typeof (object);
|
||||
|
||||
/// <inheritdoc />
|
||||
public virtual PropertyCacheLevel GetPropertyCacheLevel(IPublishedPropertyType propertyType)
|
||||
=> PropertyCacheLevel.Snapshot;
|
||||
|
||||
/// <inheritdoc />
|
||||
public virtual object ConvertSourceToIntermediate(IPublishedElement owner, IPublishedPropertyType propertyType, object source, bool preview)
|
||||
=> source;
|
||||
|
||||
/// <inheritdoc />
|
||||
public virtual object ConvertIntermediateToObject(IPublishedElement owner, IPublishedPropertyType propertyType, PropertyCacheLevel referenceCacheLevel, object inter, bool preview)
|
||||
=> inter;
|
||||
|
||||
/// <inheritdoc />
|
||||
public virtual object ConvertIntermediateToXPath(IPublishedElement owner, IPublishedPropertyType propertyType, PropertyCacheLevel referenceCacheLevel, object inter, bool preview)
|
||||
=> inter?.ToString() ?? string.Empty;
|
||||
}
|
||||
|
||||
@@ -6,7 +6,9 @@ using System.Linq;
|
||||
using System.Text;
|
||||
using Umbraco.Core;
|
||||
using Umbraco.Core.Logging;
|
||||
using Umbraco.Core.Models.Blocks;
|
||||
using Umbraco.Core.Models.PublishedContent;
|
||||
using Umbraco.Core.PropertyEditors;
|
||||
using Umbraco.Web.PropertyEditors;
|
||||
using Umbraco.Web.PropertyEditors.ValueConverters;
|
||||
using Umbraco.Web.PublishedCache;
|
||||
@@ -16,10 +18,26 @@ namespace Umbraco.Tests.PropertyEditors
|
||||
[TestFixture]
|
||||
public class BlockListPropertyValueConverterTests
|
||||
{
|
||||
private BlockListPropertyValueConverter Create()
|
||||
/// <summary>
|
||||
/// Setup mocks for IPublishedSnapshotAccessor
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
private IPublishedSnapshotAccessor GetPublishedSnapshotAccessor()
|
||||
{
|
||||
var publishedSnapshotAccessor = Mock.Of<IPublishedSnapshotAccessor>();
|
||||
var publishedModelFactory = Mock.Of<IPublishedModelFactory>();
|
||||
var homeContentType = Mock.Of<IPublishedContentType>(x =>
|
||||
x.IsElement == true
|
||||
&& x.Alias == "home");
|
||||
var contentCache = new Mock<IPublishedContentCache>();
|
||||
contentCache.Setup(x => x.GetContentType("home")).Returns(homeContentType);
|
||||
var publishedSnapshot = Mock.Of<IPublishedSnapshot>(x => x.Content == contentCache.Object);
|
||||
var publishedSnapshotAccessor = Mock.Of<IPublishedSnapshotAccessor>(x => x.PublishedSnapshot == publishedSnapshot);
|
||||
return publishedSnapshotAccessor;
|
||||
}
|
||||
|
||||
private BlockListPropertyValueConverter CreateConverter()
|
||||
{
|
||||
var publishedSnapshotAccessor = GetPublishedSnapshotAccessor();
|
||||
var publishedModelFactory = new NoopPublishedModelFactory();
|
||||
var editor = new BlockListPropertyValueConverter(
|
||||
Mock.Of<IProfilingLogger>(),
|
||||
publishedModelFactory,
|
||||
@@ -27,21 +45,9 @@ namespace Umbraco.Tests.PropertyEditors
|
||||
return editor;
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Is_Converter_For()
|
||||
private BlockListConfiguration ConfigForMany() => new BlockListConfiguration
|
||||
{
|
||||
var editor = Create();
|
||||
Assert.IsTrue(editor.IsConverter(Mock.Of<IPublishedPropertyType>(x => x.EditorAlias == Constants.PropertyEditors.Aliases.BlockList)));
|
||||
Assert.IsFalse(editor.IsConverter(Mock.Of<IPublishedPropertyType>(x => x.EditorAlias == Constants.PropertyEditors.Aliases.NestedContent)));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Get_Value_Type_Multiple()
|
||||
{
|
||||
var editor = Create();
|
||||
var config = new BlockListConfiguration
|
||||
{
|
||||
ElementTypes = new[] {
|
||||
ElementTypes = new[] {
|
||||
new BlockListConfiguration.ElementType
|
||||
{
|
||||
Alias = "Test1"
|
||||
@@ -51,7 +57,40 @@ namespace Umbraco.Tests.PropertyEditors
|
||||
Alias = "Test2"
|
||||
}
|
||||
}
|
||||
};
|
||||
};
|
||||
|
||||
private BlockListConfiguration ConfigForSingle() => new BlockListConfiguration
|
||||
{
|
||||
ElementTypes = new[] {
|
||||
new BlockListConfiguration.ElementType
|
||||
{
|
||||
Alias = "Test1"
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
private IPublishedPropertyType GetPropertyType(BlockListConfiguration config)
|
||||
{
|
||||
var dataType = new PublishedDataType(1, "test", new Lazy<object>(() => config));
|
||||
var propertyType = Mock.Of<IPublishedPropertyType>(x =>
|
||||
x.EditorAlias == Constants.PropertyEditors.Aliases.BlockList
|
||||
&& x.DataType == dataType);
|
||||
return propertyType;
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Is_Converter_For()
|
||||
{
|
||||
var editor = CreateConverter();
|
||||
Assert.IsTrue(editor.IsConverter(Mock.Of<IPublishedPropertyType>(x => x.EditorAlias == Constants.PropertyEditors.Aliases.BlockList)));
|
||||
Assert.IsFalse(editor.IsConverter(Mock.Of<IPublishedPropertyType>(x => x.EditorAlias == Constants.PropertyEditors.Aliases.NestedContent)));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Get_Value_Type_Multiple()
|
||||
{
|
||||
var editor = CreateConverter();
|
||||
var config = ConfigForMany();
|
||||
|
||||
var dataType = new PublishedDataType(1, "test", new Lazy<object>(() => config));
|
||||
var propType = Mock.Of<IPublishedPropertyType>(x => x.DataType == dataType);
|
||||
@@ -64,16 +103,8 @@ namespace Umbraco.Tests.PropertyEditors
|
||||
[Test]
|
||||
public void Get_Value_Type_Single()
|
||||
{
|
||||
var editor = Create();
|
||||
var config = new BlockListConfiguration
|
||||
{
|
||||
ElementTypes = new[] {
|
||||
new BlockListConfiguration.ElementType
|
||||
{
|
||||
Alias = "Test1"
|
||||
}
|
||||
}
|
||||
};
|
||||
var editor = CreateConverter();
|
||||
var config = ConfigForSingle();
|
||||
|
||||
var dataType = new PublishedDataType(1, "test", new Lazy<object>(() => config));
|
||||
var propType = Mock.Of<IPublishedPropertyType>(x => x.DataType == dataType);
|
||||
@@ -86,6 +117,135 @@ namespace Umbraco.Tests.PropertyEditors
|
||||
Assert.AreEqual(modelType.FullName, valueType.FullName);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Convert_Null_Empty()
|
||||
{
|
||||
var editor = CreateConverter();
|
||||
var config = ConfigForMany();
|
||||
var propertyType = GetPropertyType(config);
|
||||
var publishedElement = Mock.Of<IPublishedElement>();
|
||||
|
||||
string json = null;
|
||||
var converted = editor.ConvertIntermediateToObject(publishedElement, propertyType, PropertyCacheLevel.None, json, false) as BlockListModel;
|
||||
|
||||
Assert.IsNotNull(converted);
|
||||
Assert.AreEqual(0, converted.Data.Count());
|
||||
Assert.AreEqual(0, converted.Layout.Count());
|
||||
|
||||
json = string.Empty;
|
||||
converted = editor.ConvertIntermediateToObject(publishedElement, propertyType, PropertyCacheLevel.None, json, false) as BlockListModel;
|
||||
|
||||
Assert.IsNotNull(converted);
|
||||
Assert.AreEqual(0, converted.Data.Count());
|
||||
Assert.AreEqual(0, converted.Layout.Count());
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Convert_Valid_Empty_Json()
|
||||
{
|
||||
var editor = CreateConverter();
|
||||
var config = ConfigForMany();
|
||||
var propertyType = GetPropertyType(config);
|
||||
var publishedElement = Mock.Of<IPublishedElement>();
|
||||
|
||||
var json = "{}";
|
||||
var converted = editor.ConvertIntermediateToObject(publishedElement, propertyType, PropertyCacheLevel.None, json, false) as BlockListModel;
|
||||
|
||||
Assert.IsNotNull(converted);
|
||||
Assert.AreEqual(0, converted.Data.Count());
|
||||
Assert.AreEqual(0, converted.Layout.Count());
|
||||
|
||||
json = @"{
|
||||
layout: [],
|
||||
data: []}";
|
||||
converted = editor.ConvertIntermediateToObject(publishedElement, propertyType, PropertyCacheLevel.None, json, false) as BlockListModel;
|
||||
|
||||
Assert.IsNotNull(converted);
|
||||
Assert.AreEqual(0, converted.Data.Count());
|
||||
Assert.AreEqual(0, converted.Layout.Count());
|
||||
|
||||
// Even though there is a layout, there is no data, so the conversion will result in zero elements in total
|
||||
json = @"
|
||||
{
|
||||
layout: {
|
||||
'" + Constants.PropertyEditors.Aliases.BlockList + @"': [
|
||||
{
|
||||
'udi': 'umb://element/e7dba547615b4e9ab4ab2a7674845bc9',
|
||||
'settings': {}
|
||||
}
|
||||
]
|
||||
},
|
||||
data: []
|
||||
}";
|
||||
|
||||
converted = editor.ConvertIntermediateToObject(publishedElement, propertyType, PropertyCacheLevel.None, json, false) as BlockListModel;
|
||||
|
||||
Assert.IsNotNull(converted);
|
||||
Assert.AreEqual(0, converted.Data.Count());
|
||||
Assert.AreEqual(0, converted.Layout.Count());
|
||||
|
||||
// Even though there is a layout and data, the data is invalid (missing required keys) so the conversion will result in zero elements in total
|
||||
json = @"
|
||||
{
|
||||
layout: {
|
||||
'" + Constants.PropertyEditors.Aliases.BlockList + @"': [
|
||||
{
|
||||
'udi': 'umb://element/e7dba547615b4e9ab4ab2a7674845bc9',
|
||||
'settings': {}
|
||||
}
|
||||
]
|
||||
},
|
||||
data: [
|
||||
{
|
||||
'udi': 'umb://element/e7dba547615b4e9ab4ab2a7674845bc9'
|
||||
}
|
||||
]
|
||||
}";
|
||||
|
||||
converted = editor.ConvertIntermediateToObject(publishedElement, propertyType, PropertyCacheLevel.None, json, false) as BlockListModel;
|
||||
|
||||
Assert.IsNotNull(converted);
|
||||
Assert.AreEqual(0, converted.Data.Count());
|
||||
Assert.AreEqual(0, converted.Layout.Count());
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Convert_Valid_Json()
|
||||
{
|
||||
var editor = CreateConverter();
|
||||
var config = ConfigForMany();
|
||||
var propertyType = GetPropertyType(config);
|
||||
var publishedElement = Mock.Of<IPublishedElement>();
|
||||
|
||||
var json = @"
|
||||
{
|
||||
layout: {
|
||||
'" + Constants.PropertyEditors.Aliases.BlockList + @"': [
|
||||
{
|
||||
'udi': 'umb://element/1304E1DDAC87439684FE8A399231CB3D',
|
||||
'settings': {}
|
||||
}
|
||||
]
|
||||
},
|
||||
data: [
|
||||
{
|
||||
'contentTypeAlias': 'home',
|
||||
'key': '1304E1DD-AC87-4396-84FE-8A399231CB3D'
|
||||
}
|
||||
]
|
||||
}";
|
||||
var converted = editor.ConvertIntermediateToObject(publishedElement, propertyType, PropertyCacheLevel.None, json, false) as BlockListModel;
|
||||
|
||||
Assert.IsNotNull(converted);
|
||||
Assert.AreEqual(1, converted.Data.Count());
|
||||
var item0 = converted.Data.ElementAt(0);
|
||||
Assert.AreEqual(Guid.Parse("1304E1DD-AC87-4396-84FE-8A399231CB3D"), item0.Key);
|
||||
Assert.AreEqual("home", item0.ContentType.Alias);
|
||||
Assert.AreEqual(1, converted.Layout.Count());
|
||||
var layout0 = converted.Layout.ElementAt(0);
|
||||
Assert.IsNull(layout0.Settings);
|
||||
Assert.AreEqual(Udi.Parse("umb://element/1304E1DDAC87439684FE8A399231CB3D"), layout0.Udi);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@@ -53,6 +53,8 @@ namespace Umbraco.Web.PropertyEditors.ValueConverters
|
||||
/// <inheritdoc />
|
||||
public override object ConvertIntermediateToObject(IPublishedElement owner, IPublishedPropertyType propertyType, PropertyCacheLevel referenceCacheLevel, object inter, bool preview)
|
||||
{
|
||||
// NOTE: The intermediate object is just a json string, we don't actually convert from source -> intermediate since source is always just a json string
|
||||
|
||||
using (_proflog.DebugDuration<BlockListPropertyValueConverter>($"ConvertPropertyToBlockList ({propertyType.DataType.Id})"))
|
||||
{
|
||||
var configuration = propertyType.DataType.ConfigurationAs<BlockListConfiguration>();
|
||||
@@ -79,18 +81,7 @@ namespace Umbraco.Web.PropertyEditors.ValueConverters
|
||||
var blockListLayouts = jsonLayout[Constants.PropertyEditors.Aliases.BlockList] as JArray;
|
||||
if (blockListLayouts == null) return model;
|
||||
|
||||
foreach(var blockListLayout in blockListLayouts)
|
||||
{
|
||||
var settingsJson = blockListLayout["settings"] as JObject;
|
||||
if (settingsJson == null) continue;
|
||||
|
||||
var element = _blockConverter.ConvertToElement(settingsJson, BlockEditorPropertyEditor.ContentTypeAliasPropertyKey, referenceCacheLevel, preview);
|
||||
if (element == null) continue;
|
||||
|
||||
var layoutRef = new BlockListLayoutReference(blockListLayout.Value<Udi>("udi"), element);
|
||||
layout.Add(layoutRef);
|
||||
}
|
||||
|
||||
// parse the data elements
|
||||
foreach (var data in jsonData.Cast<JObject>())
|
||||
{
|
||||
var element = _blockConverter.ConvertToElement(data, BlockEditorPropertyEditor.ContentTypeAliasPropertyKey, referenceCacheLevel, preview);
|
||||
@@ -98,6 +89,26 @@ namespace Umbraco.Web.PropertyEditors.ValueConverters
|
||||
elements.Add(element);
|
||||
}
|
||||
|
||||
// if there's no elements just return since if there's no data it doesn't matter what is stored in layout
|
||||
if (elements.Count == 0) return model;
|
||||
|
||||
foreach (var blockListLayout in blockListLayouts)
|
||||
{
|
||||
var settingsJson = blockListLayout["settings"] as JObject;
|
||||
if (settingsJson == null) continue;
|
||||
|
||||
// the result of this can be null, that's ok
|
||||
var element = _blockConverter.ConvertToElement(settingsJson, BlockEditorPropertyEditor.ContentTypeAliasPropertyKey, referenceCacheLevel, preview);
|
||||
|
||||
if (!Udi.TryParse(blockListLayout.Value<string>("udi"), out var udi))
|
||||
continue;
|
||||
|
||||
var layoutRef = new BlockListLayoutReference(udi, element);
|
||||
layout.Add(layoutRef);
|
||||
}
|
||||
|
||||
|
||||
|
||||
return model;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user