From 3914be43b84e94d45dc3a9bf375fbbb626aa7236 Mon Sep 17 00:00:00 2001 From: Warren Buckley Date: Mon, 11 Jan 2021 09:58:45 +0000 Subject: [PATCH] Merge pull request #9629 from umbraco/v8/bugfix/4637-variation-deleting-content V8: Fix bug where allowing culture variation would delete content from element types #7471 (cherry picked from commit 463bc46675bd5b54ef69f4b28c7b3a8182a78d6b) --- .../Implement/ContentTypeRepositoryBase.cs | 8 +- .../Repositories/ContentTypeRepositoryTest.cs | 83 +++++++++++++++++++ 2 files changed, 90 insertions(+), 1 deletion(-) diff --git a/src/Umbraco.Core/Persistence/Repositories/Implement/ContentTypeRepositoryBase.cs b/src/Umbraco.Core/Persistence/Repositories/Implement/ContentTypeRepositoryBase.cs index 357798a8a9..954d780a10 100644 --- a/src/Umbraco.Core/Persistence/Repositories/Implement/ContentTypeRepositoryBase.cs +++ b/src/Umbraco.Core/Persistence/Repositories/Implement/ContentTypeRepositoryBase.cs @@ -448,8 +448,14 @@ AND umbracoNode.id <> @id", // The composed property is only considered segment variant when the base content type is also segment variant. // Example: Culture variant content type with a Culture+Segment variant property type will become ContentVariation.Culture var target = newContentTypeVariation & composedPropertyType.Variations; + // Determine the previous variation + // We have to compare with the old content type variation because the composed property might already have changed + // Example: A property with variations in an element type with variations is used in a document without + // when you enable variations the property has already enabled variations from the element type, + // but it's still a change from nothing because the document did not have variations, but it does now. + var from = oldContentTypeVariation & composedPropertyType.Variations; - propertyTypeVariationChanges[composedPropertyType.Id] = (composedPropertyType.Variations, target); + propertyTypeVariationChanges[composedPropertyType.Id] = (from, target); } } diff --git a/src/Umbraco.Tests/Persistence/Repositories/ContentTypeRepositoryTest.cs b/src/Umbraco.Tests/Persistence/Repositories/ContentTypeRepositoryTest.cs index e592c5171a..76c8b9169e 100644 --- a/src/Umbraco.Tests/Persistence/Repositories/ContentTypeRepositoryTest.cs +++ b/src/Umbraco.Tests/Persistence/Repositories/ContentTypeRepositoryTest.cs @@ -14,6 +14,7 @@ using Umbraco.Tests.TestHelpers; using Umbraco.Tests.TestHelpers.Entities; using Umbraco.Tests.Testing; using Umbraco.Web.Models.ContentEditing; +using Content = Umbraco.Core.Models.Content; namespace Umbraco.Tests.Persistence.Repositories { @@ -997,6 +998,88 @@ namespace Umbraco.Tests.Persistence.Repositories } } + [Test] + public void Can_Update_Variation_Of_Element_Type_Property() + { + var provider = TestObjects.GetScopeProvider(Logger); + using (var scope = provider.CreateScope()) + { + ContentTypeRepository repository; + var contentRepository = CreateRepository((IScopeAccessor) provider, out repository); + + // Create elementType + var elementType = new ContentType(-1) + { + Alias = "elementType", + Name = "Element type", + Description = "Element type to use as compositions", + Icon = ".sprTreeDoc3", + Thumbnail = "doc.png", + SortOrder = 1, + CreatorId = 0, + Trashed = false, + IsElement = true, + Variations = ContentVariation.Nothing + }; + + var contentCollection = new PropertyTypeCollection(true); + contentCollection.Add(new PropertyType("test", ValueStorageType.Ntext) + { + Alias = "title", + Name = "Title", + Description = "", + Mandatory = false, + SortOrder = 1, + DataTypeId = Constants.DataTypes.Textbox, + LabelOnTop = true, + Variations = ContentVariation.Nothing + }); + elementType.PropertyGroups.Add(new PropertyGroup(contentCollection) {Name = "Content", SortOrder = 1}); + elementType.ResetDirtyProperties(false); + elementType.SetDefaultTemplate(new Template("ElementType", "elementType")); + repository.Save(elementType); + + // Create the basic "home" doc type that uses element type as comp + var docType = new ContentType(-1) + { + Alias = "home", + Name = "Home", + Description = "Home containing elementType", + Icon = ".sprTreeDoc3", + Thumbnail = "doc.png", + SortOrder = 1, + CreatorId = 0, + Trashed = false, + Variations = ContentVariation.Nothing + }; + var comp = new List(); + comp.Add(elementType); + docType.ContentTypeComposition = comp; + repository.Save(docType); + + // Create "home" content + var content = new Content("Home", -1, docType){Level = 1, SortOrder = 1, CreatorId = 0, WriterId = 0}; + object obj = new {title = "test title"}; + content.PropertyValues(obj); + content.ResetDirtyProperties(false); + contentRepository.Save(content); + + // Update variation on element type + elementType.Variations = ContentVariation.Culture; + elementType.PropertyTypes.First().Variations = ContentVariation.Culture; + repository.Save(elementType); + + // Update variation on doc type + docType.Variations = ContentVariation.Culture; + repository.Save(docType); + + // Re fetch renewedContent and make sure that the culture has been set. + var renewedContent = ServiceContext.ContentService.GetById(content.Id); + var hasCulture = renewedContent.Properties["title"].Values.First().Culture != null; + Assert.That(hasCulture, Is.True); + } + } + public void CreateTestData() { //Create and Save ContentType "umbTextpage" -> (NodeDto.NodeIdSeed)