diff --git a/src/Umbraco.Core/Models/Content.cs b/src/Umbraco.Core/Models/Content.cs index 0e5c3340fa..be6fd9543a 100644 --- a/src/Umbraco.Core/Models/Content.cs +++ b/src/Umbraco.Core/Models/Content.cs @@ -139,7 +139,10 @@ namespace Umbraco.Core.Models /// /// Boolean indicating whether this Content is Published or not /// - /// Setting Published to true/false should be private or internal + /// + /// Setting Published to true/false should be private or internal and should ONLY be used for wiring up the value + /// from the db or modifying it based on changing the published state. + /// [DataMember] public bool Published { @@ -372,11 +375,25 @@ namespace Umbraco.Core.Models /// True if entity is dirty, otherwise False public override bool IsDirty() { - bool dirtyEntity = base.IsDirty(); + return IsEntityDirty() || IsAnyUserPropertyDirty(); + } - bool dirtyProperties = Properties.Any(x => x.IsDirty()); + /// + /// Returns true if only the entity properties are direty + /// + /// + internal bool IsEntityDirty() + { + return base.IsDirty(); + } - return dirtyEntity || dirtyProperties; + /// + /// Returns true if any of the properties are dirty + /// + /// + internal bool IsAnyUserPropertyDirty() + { + return Properties.Any(x => x.IsDirty()); } /// diff --git a/src/Umbraco.Core/Models/ContentExtensions.cs b/src/Umbraco.Core/Models/ContentExtensions.cs index c30f96c214..c08983c866 100644 --- a/src/Umbraco.Core/Models/ContentExtensions.cs +++ b/src/Umbraco.Core/Models/ContentExtensions.cs @@ -32,9 +32,9 @@ namespace Umbraco.Core.Models /// /// /// A new version needs to be created when: - /// * Any property value is changed (to enable a rollback) /// * The publish status is changed /// * The language is changed + /// * The item is already published and is being published again and any property value is changed (to enable a rollback) /// internal static bool ShouldCreateNewVersion(this IContent entity) { @@ -50,20 +50,32 @@ namespace Umbraco.Core.Models /// /// /// A new version needs to be created when: - /// * Any property value is changed (to enable a rollback) /// * The publish status is changed /// * The language is changed + /// * The item is already published and is being published again and any property value is changed (to enable a rollback) /// internal static bool ShouldCreateNewVersion(this IContent entity, PublishedState publishedState) { var dirtyEntity = (ICanBeDirty)entity; + + //check if the published state has changed or the language var contentChanged = (dirtyEntity.IsPropertyDirty("Published") && publishedState != PublishedState.Unpublished) || dirtyEntity.IsPropertyDirty("Language"); - var propertyValueChanged = entity.Properties.Any(x => ((ICanBeDirty)x).IsDirty()); + //return true if published or language has changed + if (contentChanged) + { + return true; + } - return contentChanged || propertyValueChanged; + //check if any user prop has changed + var propertyValueChanged = ((Content) entity).IsAnyUserPropertyDirty(); + //check if any content prop has changed + var contentDataChanged = ((Content) entity).IsEntityDirty(); + + //return true if the item is published and a property has changed or if any content property has changed + return (propertyValueChanged && publishedState == PublishedState.Published) || contentDataChanged; } /// diff --git a/src/Umbraco.Tests/Models/ContentExtensionsTests.cs b/src/Umbraco.Tests/Models/ContentExtensionsTests.cs index 3da5b12a7f..0e0cd41c15 100644 --- a/src/Umbraco.Tests/Models/ContentExtensionsTests.cs +++ b/src/Umbraco.Tests/Models/ContentExtensionsTests.cs @@ -9,15 +9,29 @@ namespace Umbraco.Tests.Models public class ContentExtensionsTests { [Test] - public void Should_Create_New_Version_When_Publish_Status_Changed() + public void Should_Create_New_Version_When_Publishing() { var contentType = MockedContentTypes.CreateTextpageContentType(); var content = MockedContent.CreateTextpageContent(contentType, "Textpage", -1); content.ResetDirtyProperties(false); + + content.ChangePublishedState(PublishedState.Published); + Assert.IsTrue(content.ShouldCreateNewVersion()); + } - content.Published = true; - Assert.IsTrue(content.ShouldCreateNewVersion(PublishedState.Published)); + [Test] + public void Should_Create_New_Version_When_Saving_After_Publishing() + { + var contentType = MockedContentTypes.CreateTextpageContentType(); + var content = MockedContent.CreateTextpageContent(contentType, "Textpage", -1); + + content.ResetDirtyProperties(false); + content.ChangePublishedState(PublishedState.Published); + content.ResetDirtyProperties(false); + + content.ChangePublishedState(PublishedState.Saved); + Assert.IsTrue(content.ShouldCreateNewVersion()); } [Test] @@ -33,7 +47,7 @@ namespace Umbraco.Tests.Models } [Test] - public void Should_Create_New_Version_When_Any_Property_Value_Changed() + public void Should_Create_New_Version_When_Any_Property_Value_Changed_And_Its_Already_Published() { var contentType = MockedContentTypes.CreateTextpageContentType(); var content = MockedContent.CreateTextpageContent(contentType, "Textpage", -1); @@ -41,11 +55,11 @@ namespace Umbraco.Tests.Models content.ResetDirtyProperties(false); content.Properties.First().Value = "hello world"; - Assert.IsTrue(content.ShouldCreateNewVersion(PublishedState.Unpublished)); + Assert.IsTrue(content.ShouldCreateNewVersion(PublishedState.Published)); } - + [Test] - public void Should_Not_Create_New_Version_When_Anything_Other_Than_Published_Language_Or_Property_Vals_Changed() + public void Should_Not_Create_New_Version_When_Published_Status_Not_Changed() { var contentType = MockedContentTypes.CreateTextpageContentType(); var content = MockedContent.CreateTextpageContent(contentType, "Textpage", -1); @@ -55,5 +69,19 @@ namespace Umbraco.Tests.Models Assert.IsFalse(content.ShouldCreateNewVersion(PublishedState.Unpublished)); } + [Test] + public void Should_Not_Create_New_Version_When_Not_Published_And_Property_Changed() + { + var contentType = MockedContentTypes.CreateTextpageContentType(); + var content = MockedContent.CreateTextpageContent(contentType, "Textpage", -1); + + content.ResetDirtyProperties(false); + + content.Properties.First().Value = "hello world"; + Assert.IsFalse(content.ShouldCreateNewVersion(PublishedState.Unpublished)); + } + + + } } \ No newline at end of file