Issue #AR-1_serialization: add test to try to create escaped json - this still produces unescaped json.

However,
		    internal virtual PropertyEditor GetPropertyEditor(IDataTypeDefinition dtd)
		    {
		        return dtd.Id == 0
                    ? new ArchetypePropertyEditor()
                    : PropertyEditorResolver.Current.GetByAlias(dtd.PropertyEditorAlias);
		    }

would produce nested json. But this spec looks incorrect.
This commit is contained in:
cankoluman
2014-05-13 15:44:33 +01:00
parent f1df0f2ad9
commit ae242feed6
3 changed files with 79 additions and 7 deletions
@@ -1,10 +1,19 @@
using System.Collections;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using Archetype.Umbraco.PropertyEditors;
using Archetype.Umbraco.Serialization;
using Moq;
using Newtonsoft.Json;
using NUnit.Framework;
using Umbraco.Core;
using Umbraco.Core.Models;
using Umbraco.Core.Models.Editors;
using Umbraco.Core.Persistence;
using Umbraco.Core.PropertyEditors;
using Umbraco.Core.Services;
namespace Archetype.Tests.Serialization.Base
{
@@ -90,10 +99,64 @@ namespace Archetype.Tests.Serialization.Base
protected void NestedModel_WithEscapedJson_Regression_Battery<T>(T model)
{
var archetype = ConvertModelToArchetype(model);
Assert.IsNotNull(ConvertModelToArchetype(model));
var propGuid = new Guid();
var cacheHelper = CacheHelper.CreateDisabledCacheHelper();
var dtd = new DataTypeDefinition(-1, String.Empty) {Id = 0};
var dataTypeService = new Mock<IDataTypeService>();
dataTypeService.Setup(dts => dts.GetDataTypeDefinitionById(Guid.Parse(propGuid.ToString()))).Returns(dtd);
dataTypeService.Setup(dts => dts.GetPreValuesCollectionByDataTypeId(dtd.Id)).Returns(new PreValueCollection(new Dictionary<string, PreValue>()));
var nestedJson = archetype.SerializeForPersistence();
var propValueEditor = new Mock<ArchetypePropertyEditor.ArchetypePropertyValueEditor>(new PropertyValueEditor());
propValueEditor.Setup(pe => pe.GetPropertyEditor(dtd)).Returns(new ArchetypePropertyEditor());
var serviceContext = new ServiceContext(
Mock.Of<IContentService>(),
Mock.Of<IMediaService>(),
Mock.Of<IContentTypeService>(),
dataTypeService.Object,
Mock.Of<IFileService>(),
Mock.Of<ILocalizationService>(),
Mock.Of<IPackagingService>(),
Mock.Of<IEntityService>(),
Mock.Of<IRelationService>(),
Mock.Of<IMemberGroupService>(),
Mock.Of<ISectionService>(),
Mock.Of<IApplicationTreeService>(),
Mock.Of<ITagService>()
);
ApplicationContext.EnsureContext(
new DatabaseContext(Mock.Of<IDatabaseFactory>()), serviceContext, cacheHelper, true);
var archetype = ConvertModelToArchetype(model);
foreach (var prop in archetype.Fieldsets.SelectMany(fs => fs.Properties))
{
prop.DataTypeGuid = propGuid.ToString();
if (prop.Value.ToString().Contains("fieldsets"))
prop.PropertyEditorAlias = Umbraco.Constants.PropertyEditorAlias;
}
var archetypeJson = JsonConvert.SerializeObject(archetype);
Assert.IsNotNullOrEmpty(archetypeJson);
var propEditor = new ArchetypePropertyEditor.ArchetypePropertyValueEditor(new PropertyValueEditor());
var convertedJson = (string)propEditor.ConvertEditorToDb(new ContentPropertyData(archetypeJson,
new PreValueCollection(new Dictionary<string, PreValue>()), new Dictionary<string, object>()),
model);
Assert.IsNotNullOrEmpty(convertedJson);
var resultfromArchetypeJson = ConvertArchetypeJsonToModel<T>(archetypeJson);
var resultfromConvertedJson = ConvertArchetypeJsonToModel<T>(convertedJson);
Assert.IsInstanceOf<T>(resultfromArchetypeJson);
AssertAreEqual(model, resultfromArchetypeJson);
Assert.IsInstanceOf<T>(resultfromConvertedJson);
AssertAreEqual(model, resultfromConvertedJson);
}
protected void ComplexModel_Regression_Battery<T>(T model)
@@ -134,7 +134,7 @@ namespace Archetype.Tests.Serialization.Regression
public void NestedModel_WithEscapedJson_Regression_Battery()
{
var nestedModel = _testHelper.GetModel<NestedModelWithFieldset>();
NestedModel_WithEscapedJson_Regression_Battery(nestedModel);
NestedModel_WithEscapedJson_Regression_Battery(nestedModel);
}
[Test]
@@ -127,7 +127,7 @@ namespace Archetype.Umbraco.PropertyEditors
var dtd = ApplicationContext.Current.Services.DataTypeService.GetDataTypeDefinitionById(Guid.Parse(propDef.DataTypeGuid));
var preValues = ApplicationContext.Current.Services.DataTypeService.GetPreValuesCollectionByDataTypeId(dtd.Id);
var propData = new ContentPropertyData(propDef.Value, preValues, new Dictionary<string, object>());
var propEditor = PropertyEditorResolver.Current.GetByAlias(dtd.PropertyEditorAlias);
var propEditor = GetPropertyEditor(dtd);
propDef.Value = propEditor.ValueEditor.ConvertEditorToDb(propData, propDef.Value);
}
catch (Exception ex)
@@ -138,7 +138,16 @@ namespace Archetype.Umbraco.PropertyEditors
}
return archetype.SerializeForPersistence();
}
}
internal virtual PropertyEditor GetPropertyEditor(IDataTypeDefinition dtd)
{
return dtd.Id == 0
? dtd.PropertyEditorAlias.Equals(Constants.PropertyEditorAlias)
? new ArchetypePropertyEditor()
: new PropertyEditor()
: PropertyEditorResolver.Current.GetByAlias(dtd.PropertyEditorAlias);
}
}
#endregion