From c7510ab50e78da5ebf0e621cab07d45b60992a3e Mon Sep 17 00:00:00 2001 From: Shannon Date: Sat, 6 Jun 2015 10:58:25 +0200 Subject: [PATCH] fixes prop group/prop type mapping for when there is a new property group with new property types assigned --- .../Repositories/ContentTypeRepositoryTest.cs | 14 +++++++++++--- .../Models/Mapping/ContentTypeModelMapper.cs | 14 ++++++++++++-- 2 files changed, 23 insertions(+), 5 deletions(-) diff --git a/src/Umbraco.Tests/Persistence/Repositories/ContentTypeRepositoryTest.cs b/src/Umbraco.Tests/Persistence/Repositories/ContentTypeRepositoryTest.cs index cd8c151f25..64ba766c8c 100644 --- a/src/Umbraco.Tests/Persistence/Repositories/ContentTypeRepositoryTest.cs +++ b/src/Umbraco.Tests/Persistence/Repositories/ContentTypeRepositoryTest.cs @@ -104,7 +104,7 @@ namespace Umbraco.Tests.Persistence.Repositories using (var repository = CreateRepository(unitOfWork)) { // Act - var contentType = MockedContentTypes.CreateSimpleContentType(); + var contentType = MockedContentTypes.CreateSimpleContentType("test", "Test", propertyGroupName:"testGroup"); repository.AddOrUpdate(contentType); unitOfWork.Commit(); @@ -113,7 +113,11 @@ namespace Umbraco.Tests.Persistence.Repositories Assert.That(contentType.PropertyGroups.All(x => x.HasIdentity), Is.True); Assert.That(contentType.PropertyTypes.All(x => x.HasIdentity), Is.True); Assert.That(contentType.Path.Contains(","), Is.True); - Assert.That(contentType.SortOrder, Is.GreaterThan(0)); + Assert.That(contentType.SortOrder, Is.GreaterThan(0)); + + Assert.That(contentType.PropertyGroups.ElementAt(0).Name == "testGroup", Is.True); + var groupId = contentType.PropertyGroups.ElementAt(0).Id; + Assert.That(contentType.PropertyTypes.All(x => x.PropertyGroupId.Value == groupId), Is.True); } } @@ -127,7 +131,7 @@ namespace Umbraco.Tests.Persistence.Repositories using (var repository = CreateRepository(unitOfWork)) { // Act - var contentType = (IContentType)MockedContentTypes.CreateSimpleContentType(); + var contentType = (IContentType)MockedContentTypes.CreateSimpleContentType("test", "Test", propertyGroupName: "testGroup"); var display = Mapper.Map(contentType); //simulate what would happen in the controller, we'd never map to a 'existing' content type, // we'd map to an new content type when updating. @@ -145,6 +149,10 @@ namespace Umbraco.Tests.Persistence.Repositories Assert.That(contentType.PropertyTypes.All(x => x.HasIdentity), Is.True); Assert.That(contentType.Path.Contains(","), Is.True); Assert.That(contentType.SortOrder, Is.GreaterThan(0)); + + Assert.That(contentType.PropertyGroups.ElementAt(0).Name == "testGroup", Is.True); + var groupId = contentType.PropertyGroups.ElementAt(0).Id; + Assert.That(contentType.PropertyTypes.All(x => x.PropertyGroupId.Value == groupId), Is.True); } } diff --git a/src/Umbraco.Web/Models/Mapping/ContentTypeModelMapper.cs b/src/Umbraco.Web/Models/Mapping/ContentTypeModelMapper.cs index c7ed92ec5b..632d9a0c81 100644 --- a/src/Umbraco.Web/Models/Mapping/ContentTypeModelMapper.cs +++ b/src/Umbraco.Web/Models/Mapping/ContentTypeModelMapper.cs @@ -131,7 +131,14 @@ namespace Umbraco.Web.Models.Mapping destination.PropertyTypes = new PropertyTypeCollection(); foreach (var propertyTypeDisplay in source.Properties.Where(x => x.Label.IsNullOrWhiteSpace() == false )) { - destination.PropertyTypes.Add(Mapper.Map(propertyTypeDisplay)); + var propertyType = Mapper.Map(propertyTypeDisplay); + if (source.Id <= 0) + { + //there is no id assigned which means it's a new group and the property type needs to be assigned to it, so + // we need to set the lazy id correctly to the PropertyGroup destination + propertyType.PropertyGroupId = new Lazy(() => destination.Id, false); + } + destination.PropertyTypes.Add(propertyType); } }); @@ -143,10 +150,13 @@ namespace Umbraco.Web.Models.Mapping if (dataType == null) throw new NullReferenceException("No data type found with id " + propertyTypeDisplay.DataTypeId); return new PropertyType(dataType, propertyTypeDisplay.Alias); }) + //only map if it is actually set .ForMember(dest => dest.Id, expression => expression.Condition(source => source.Id > 0)) .ForMember(dto => dto.CreateDate, expression => expression.Ignore()) .ForMember(dto => dto.UpdateDate, expression => expression.Ignore()) - .ForMember(type => type.PropertyGroupId, expression => expression.MapFrom(display => new Lazy(() => display.GroupId, LazyThreadSafetyMode.None))) + //only map if it is actually set, if it's not set, it needs to be handled differently! + .ForMember(dest => dest.PropertyGroupId, expression => expression.Condition(source => source.GroupId > 0)) + .ForMember(type => type.PropertyGroupId, expression => expression.MapFrom(display => new Lazy(() => display.GroupId, false))) .ForMember(type => type.Key, expression => expression.Ignore()) .ForMember(type => type.HelpText, expression => expression.Ignore()) .ForMember(type => type.HasIdentity, expression => expression.Ignore())