diff --git a/src/Umbraco.Core/Models/PropertyTypeCollection.cs b/src/Umbraco.Core/Models/PropertyTypeCollection.cs index ea153ccae4..f6a3c85510 100644 --- a/src/Umbraco.Core/Models/PropertyTypeCollection.cs +++ b/src/Umbraco.Core/Models/PropertyTypeCollection.cs @@ -83,6 +83,14 @@ namespace Umbraco.Core.Models return; } } + + //check if the item's sort order is already in use + if (this.Select(x => x.SortOrder).Contains(item.SortOrder)) + { + //make it the next iteration + item.SortOrder = this.Max(x => x.SortOrder) + 1; + } + base.Add(item); OnAdd.IfNotNull(x => x.Invoke());//Could this not be replaced by a Mandate/Contract for ensuring item is not null diff --git a/src/Umbraco.Tests/Services/ContentTypeServiceTests.cs b/src/Umbraco.Tests/Services/ContentTypeServiceTests.cs index 7a19b681ce..6cff3e47b4 100644 --- a/src/Umbraco.Tests/Services/ContentTypeServiceTests.cs +++ b/src/Umbraco.Tests/Services/ContentTypeServiceTests.cs @@ -199,6 +199,36 @@ namespace Umbraco.Tests.Services Assert.That(homeDoc.ContentTypeId, Is.EqualTo(ctHomePage.Id)); } + [Test] + public void Create_Content_Type_Ensures_Sort_Orders() + { + var service = ServiceContext.ContentTypeService; + + var contentType = new ContentType(-1) + { + Alias = "test", + Name = "Test", + Description = "ContentType used for simple text pages", + Icon = ".sprTreeDoc3", + Thumbnail = "doc2.png", + SortOrder = 1, + CreatorId = 0, + Trashed = false + }; + + contentType.AddPropertyType(new PropertyType(Constants.PropertyEditors.TextboxAlias, DataTypeDatabaseType.Ntext) { Alias = "title", Name = "Title", Description = "", Mandatory = false, DataTypeDefinitionId = -88 }); + contentType.AddPropertyType(new PropertyType(Constants.PropertyEditors.TinyMCEAlias, DataTypeDatabaseType.Ntext) { Alias = "bodyText", Name = "Body Text", Description = "", Mandatory = false, DataTypeDefinitionId = -87 }); + contentType.AddPropertyType(new PropertyType(Constants.PropertyEditors.TextboxAlias, DataTypeDatabaseType.Ntext) { Alias = "author", Name = "Author", Description = "Name of the author", Mandatory = false, DataTypeDefinitionId = -88 }); + + service.Save(contentType); + + var sortOrders = contentType.PropertyTypes.Select(x => x.SortOrder).ToArray(); + + Assert.AreEqual(1, sortOrders.Count(x => x == 0)); + Assert.AreEqual(1, sortOrders.Count(x => x == 1)); + Assert.AreEqual(1, sortOrders.Count(x => x == 2)); + } + [Test] public void Can_Create_And_Save_ContentType_Composition() {