public void Convert_Valid_Json() { BlockListPropertyValueConverter editor = CreateConverter(); BlockListConfiguration config = ConfigForMany(); IPublishedPropertyType propertyType = GetPropertyType(config); IPublishedElement publishedElement = Mock.Of <IPublishedElement>(); string json = @" { layout: { '" + Constants.PropertyEditors.Aliases.BlockList + @"': [ { 'contentUdi': 'umb://element/1304E1DDAC87439684FE8A399231CB3D' } ] }, contentData: [ { 'contentTypeKey': '" + _contentKey1 + @"', 'udi': 'umb://element/1304E1DDAC87439684FE8A399231CB3D' } ] }"; var converted = editor.ConvertIntermediateToObject(publishedElement, propertyType, PropertyCacheLevel.None, json, false) as BlockListModel; Assert.IsNotNull(converted); Assert.AreEqual(1, converted.Count); IPublishedElement item0 = converted[0].Content; Assert.AreEqual(Guid.Parse("1304E1DD-AC87-4396-84FE-8A399231CB3D"), item0.Key); Assert.AreEqual("Test1", item0.ContentType.Alias); Assert.IsNull(converted[0].Settings); Assert.AreEqual(UdiParser.Parse("umb://element/1304E1DDAC87439684FE8A399231CB3D"), converted[0].ContentUdi); }
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); }
public void Get_Value_Type_Single() { BlockListPropertyValueConverter editor = CreateConverter(); BlockListConfiguration config = ConfigForSingle(); var dataType = new PublishedDataType(1, "test", new Lazy <object>(() => config)); IPublishedPropertyType propType = Mock.Of <IPublishedPropertyType>(x => x.DataType == dataType); Type valueType = editor.GetPropertyValueType(propType); // the result is always block list model Assert.AreEqual(typeof(BlockListModel), valueType); }
public void Convert_Null_Empty() { BlockListPropertyValueConverter editor = CreateConverter(); BlockListConfiguration config = ConfigForMany(); IPublishedPropertyType propertyType = GetPropertyType(config); IPublishedElement 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.Count); json = string.Empty; converted = editor.ConvertIntermediateToObject(publishedElement, propertyType, PropertyCacheLevel.None, json, false) as BlockListModel; Assert.IsNotNull(converted); Assert.AreEqual(0, converted.Count); }
//A copy of the core BlockListValuePropertyConverter method, but if the propertyType is null, it doesn't validate each block agains the property type configuration and just returns the block model. //Could move this to the core, but it feels like there's a bigger problem with blocks here if they are supposed to be share across data types, there shuold be a single resolver here just to get the block models public object ConvertIntermediateToObjectCustom(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 BlockListConfiguration configuration = propertyType != null?propertyType.DataType.ConfigurationAs <BlockListConfiguration>() : null; Dictionary <Guid, BlockListConfiguration.BlockConfiguration> blockConfigMap = configuration != null?configuration.Blocks.ToDictionary(x => x.ContentElementTypeKey) : null; IList <Guid?> validSettingElementTypes = blockConfigMap != null?blockConfigMap.Values.Select(x => x.SettingsElementTypeKey).Where(x => x.HasValue).Distinct().ToList() : null; var contentPublishedElements = new Dictionary <Guid, IPublishedElement>(); var settingsPublishedElements = new Dictionary <Guid, IPublishedElement>(); var layout = new List <BlockListItem>(); var value = (string)inter; if (string.IsNullOrWhiteSpace(value)) { return(BlockListModel.Empty); } var converted = _blockListEditorDataConverter.Deserialize(value); if (converted.BlockValue.ContentData.Count == 0) { return(BlockListModel.Empty); } var blockListLayout = converted.Layout.ToObject <IEnumerable <BlockListLayoutItem> >(); // convert the content data foreach (var data in converted.BlockValue.ContentData) { if (blockConfigMap != null && !blockConfigMap.ContainsKey(data.ContentTypeKey)) { continue; } var element = _blockConverter.ConvertToElement(data, referenceCacheLevel, preview); if (element == null) { continue; } contentPublishedElements[element.Key] = element; } // convert the settings data foreach (var data in converted.BlockValue.SettingsData) { if (validSettingElementTypes != null && !validSettingElementTypes.Contains(data.ContentTypeKey)) { continue; } var element = _blockConverter.ConvertToElement(data, referenceCacheLevel, preview); if (element == null) { continue; } settingsPublishedElements[element.Key] = element; } // if there's no elements just return since if there's no data it doesn't matter what is stored in layout if (contentPublishedElements.Count == 0) { return(BlockListModel.Empty); } foreach (var layoutItem in blockListLayout) { // get the content reference var contentGuidUdi = (GuidUdi)layoutItem.ContentUdi; if (!contentPublishedElements.TryGetValue(contentGuidUdi.Guid, out var contentData)) { continue; } // get the setting reference IPublishedElement settingsData = null; var settingGuidUdi = layoutItem.SettingsUdi != null ? (GuidUdi)layoutItem.SettingsUdi : null; if (settingGuidUdi != null) { settingsPublishedElements.TryGetValue(settingGuidUdi.Guid, out settingsData); } if (!contentData.ContentType.TryGetKey(out var contentTypeKey)) { throw new InvalidOperationException("The content type was not of type " + typeof(IPublishedContentType2)); } BlockListConfiguration.BlockConfiguration blockConfig = null; if (blockConfigMap != null && !blockConfigMap.TryGetValue(contentTypeKey, out blockConfig)) { continue; } // this can happen if they have a settings type, save content, remove the settings type, and display the front-end page before saving the content again // we also ensure that the content type's match since maybe the settings type has been changed after this has been persisted. if (settingsData != null) { if (!settingsData.ContentType.TryGetKey(out var settingsElementTypeKey)) { throw new InvalidOperationException("The settings element type was not of type " + typeof(IPublishedContentType2)); } if (blockConfig != null && (!blockConfig.SettingsElementTypeKey.HasValue || settingsElementTypeKey != blockConfig.SettingsElementTypeKey)) { settingsData = null; } } var layoutType = typeof(BlockListItem <,>).MakeGenericType(contentData.GetType(), settingsData?.GetType() ?? typeof(IPublishedElement)); var layoutRef = (BlockListItem)Activator.CreateInstance(layoutType, contentGuidUdi, contentData, settingGuidUdi, settingsData); layout.Add(layoutRef); } var model = new BlockListModel(layout); return(model); }
public void Data_Item_Removed_If_Removed_From_Config() { var editor = CreateConverter(); // The data below expects that ContentKey1 + ContentKey2 + SettingsKey1 + SettingsKey2 exist but only ContentKey2 exists so // the data should all be filtered. var config = new BlockListConfiguration { Blocks = new[] { new BlockListConfiguration.BlockConfiguration { ContentElementTypeKey = ContentKey2, SettingsElementTypeKey = null } } }; var propertyType = GetPropertyType(config); var publishedElement = Mock.Of <IPublishedElement>(); var json = @" { layout: { '" + Constants.PropertyEditors.Aliases.BlockList + @"': [ { 'contentUdi': 'umb://element/1304E1DDAC87439684FE8A399231CB3D', 'settingsUdi': 'umb://element/1F613E26CE274898908A561437AF5100' }, { 'contentUdi': 'umb://element/0A4A416E547D464FABCC6F345C17809A', 'settingsUdi': 'umb://element/63027539B0DB45E7B70459762D4E83DD' } ] }, contentData: [ { 'contentTypeKey': '" + ContentKey1 + @"', 'udi': 'umb://element/1304E1DDAC87439684FE8A399231CB3D' }, { 'contentTypeKey': '" + ContentKey2 + @"', 'udi': 'umb://element/E05A034704424AB3A520E048E6197E79' }, { 'contentTypeKey': '" + ContentKey2 + @"', 'udi': 'umb://element/0A4A416E547D464FABCC6F345C17809A' } ], settingsData: [ { 'contentTypeKey': '" + SettingKey1 + @"', 'udi': 'umb://element/63027539B0DB45E7B70459762D4E83DD' }, { 'contentTypeKey': '" + SettingKey2 + @"', 'udi': 'umb://element/1F613E26CE274898908A561437AF5100' }, { 'contentTypeKey': '" + SettingKey2 + @"', 'udi': 'umb://element/BCF4BA3DA40C496C93EC58FAC85F18B9' } ], }"; var converted = editor.ConvertIntermediateToObject(publishedElement, propertyType, PropertyCacheLevel.None, json, false) as BlockListModel; Assert.IsNotNull(converted); Assert.AreEqual(2, converted.ContentData.Count()); Assert.AreEqual(0, converted.SettingsData.Count()); Assert.AreEqual(1, converted.Layout.Count()); var item0 = converted.Layout.ElementAt(0); Assert.AreEqual(Guid.Parse("0A4A416E-547D-464F-ABCC-6F345C17809A"), item0.Content.Key); Assert.AreEqual("Test2", item0.Content.ContentType.Alias); Assert.IsNull(item0.Settings); }
public void Get_Data_From_Layout_Item() { BlockListPropertyValueConverter editor = CreateConverter(); BlockListConfiguration config = ConfigForMany(); IPublishedPropertyType propertyType = GetPropertyType(config); IPublishedElement publishedElement = Mock.Of <IPublishedElement>(); string json = @" { layout: { '" + Constants.PropertyEditors.Aliases.BlockList + @"': [ { 'contentUdi': 'umb://element/1304E1DDAC87439684FE8A399231CB3D', 'settingsUdi': 'umb://element/1F613E26CE274898908A561437AF5100' }, { 'contentUdi': 'umb://element/0A4A416E547D464FABCC6F345C17809A', 'settingsUdi': 'umb://element/63027539B0DB45E7B70459762D4E83DD' } ] }, contentData: [ { 'contentTypeKey': '" + _contentKey1 + @"', 'udi': 'umb://element/1304E1DDAC87439684FE8A399231CB3D' }, { 'contentTypeKey': '" + _contentKey2 + @"', 'udi': 'umb://element/E05A034704424AB3A520E048E6197E79' }, { 'contentTypeKey': '" + _contentKey2 + @"', 'udi': 'umb://element/0A4A416E547D464FABCC6F345C17809A' } ], settingsData: [ { 'contentTypeKey': '" + _settingKey1 + @"', 'udi': 'umb://element/63027539B0DB45E7B70459762D4E83DD' }, { 'contentTypeKey': '" + _settingKey2 + @"', 'udi': 'umb://element/1F613E26CE274898908A561437AF5100' }, { 'contentTypeKey': '" + _settingKey2 + @"', 'udi': 'umb://element/BCF4BA3DA40C496C93EC58FAC85F18B9' } ], }"; var converted = editor.ConvertIntermediateToObject(publishedElement, propertyType, PropertyCacheLevel.None, json, false) as BlockListModel; Assert.IsNotNull(converted); Assert.AreEqual(2, converted.Count); BlockListItem item0 = converted[0]; Assert.AreEqual(Guid.Parse("1304E1DD-AC87-4396-84FE-8A399231CB3D"), item0.Content.Key); Assert.AreEqual("Test1", item0.Content.ContentType.Alias); Assert.AreEqual(Guid.Parse("1F613E26CE274898908A561437AF5100"), item0.Settings.Key); Assert.AreEqual("Setting2", item0.Settings.ContentType.Alias); BlockListItem item1 = converted[1]; Assert.AreEqual(Guid.Parse("0A4A416E-547D-464F-ABCC-6F345C17809A"), item1.Content.Key); Assert.AreEqual("Test2", item1.Content.ContentType.Alias); Assert.AreEqual(Guid.Parse("63027539B0DB45E7B70459762D4E83DD"), item1.Settings.Key); Assert.AreEqual("Setting1", item1.Settings.ContentType.Alias); }
public void Convert_Valid_Empty_Json() { BlockListPropertyValueConverter editor = CreateConverter(); BlockListConfiguration config = ConfigForMany(); IPublishedPropertyType propertyType = GetPropertyType(config); IPublishedElement publishedElement = Mock.Of <IPublishedElement>(); string json = "{}"; var converted = editor.ConvertIntermediateToObject(publishedElement, propertyType, PropertyCacheLevel.None, json, false) as BlockListModel; Assert.IsNotNull(converted); Assert.AreEqual(0, converted.Count); json = @"{ layout: {}, data: []}"; converted = editor.ConvertIntermediateToObject(publishedElement, propertyType, PropertyCacheLevel.None, json, false) as BlockListModel; Assert.IsNotNull(converted); Assert.AreEqual(0, converted.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 + @"': [ { 'contentUdi': 'umb://element/e7dba547615b4e9ab4ab2a7674845bc9' } ] }, contentData: [] }"; converted = editor.ConvertIntermediateToObject(publishedElement, propertyType, PropertyCacheLevel.None, json, false) as BlockListModel; Assert.IsNotNull(converted); Assert.AreEqual(0, converted.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 + @"': [ { 'contentUdi': 'umb://element/e7dba547615b4e9ab4ab2a7674845bc9' } ] }, contentData: [ { 'udi': 'umb://element/e7dba547615b4e9ab4ab2a7674845bc9' } ] }"; converted = editor.ConvertIntermediateToObject(publishedElement, propertyType, PropertyCacheLevel.None, json, false) as BlockListModel; Assert.IsNotNull(converted); Assert.AreEqual(0, converted.Count); // Everthing is ok except the udi reference in the layout doesn't match the data so it will be empty json = @" { layout: { '" + Constants.PropertyEditors.Aliases.BlockList + @"': [ { 'contentUdi': 'umb://element/1304E1DDAC87439684FE8A399231CB3D' } ] }, contentData: [ { 'contentTypeKey': '" + _contentKey1 + @"', 'key': '1304E1DD-0000-4396-84FE-8A399231CB3D' } ] }"; converted = editor.ConvertIntermediateToObject(publishedElement, propertyType, PropertyCacheLevel.None, json, false) as BlockListModel; Assert.IsNotNull(converted); Assert.AreEqual(0, converted.Count); }