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);
        }
        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);
        }
        public HttpResponseMessage GetBlockPreviewMarkup([FromBody] BlockPreview data)
        {
            var contentType          = Services.ContentTypeService.Get(data.ContentTypeAlias);
            var publishedContentType = new Lazy <IPublishedContentType>(() => _publishedContentTypeFactory.CreateContentType(contentType)).Value;
            var propertyType         = publishedContentType.PropertyTypes.FirstOrDefault(x => x.Alias == data.PropertyAlias);

            var editor = new BlockListPropertyValueConverter(
                _profilingLogger,
                new BlockEditorConverter(_publishedSnapshotAccessor, _publishedModelFactory));

            var page = default(IPublishedContent);

            // If the page is new, then the ID will be zero
            if (data.PageId > 0)
            {
                // Get page container node
                page = Umbraco.Content(data.PageId);
                if (page == null)
                {
                    // If unpublished, then fake PublishedContent
                    page = new UnpublishedContent(data.PageId, Services, _publishedContentTypeFactory, Current.PropertyEditors);
                }
            }

            var converted = editor.ConvertIntermediateToObject(page, propertyType, PropertyCacheLevel.None, data.Value, false) as BlockListModel;
            var model     = converted[0];
            // Render view
            var partialName = string.Format(ConfigurationManager.AppSettings["BlockTypeGridViewPreviewPath"] ?? "~/Views/Partials/BlockList/Components/{0}.cshtml", model.Content.ContentType.Alias);
            var markup      = Helpers.ViewHelper.RenderPartial(partialName, model, UmbracoContext.HttpContext, UmbracoContext);

            // Return response
            var response = new HttpResponseMessage
            {
                Content = new StringContent(markup ?? string.Empty)
            };

            response.Content.Headers.ContentType = new MediaTypeHeaderValue(MediaTypeNames.Text.Html);

            return(response);
        }
        public void Data_Item_Removed_If_Removed_From_Config()
        {
            BlockListPropertyValueConverter 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
                    }
                }
            };

            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(1, converted.Count);

            BlockListItem item0 = converted[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);
        }