From c52c452b3633db0bdcb21ebee477684e49a5f428 Mon Sep 17 00:00:00 2001 From: Shannon Date: Tue, 13 Aug 2013 11:01:49 +1000 Subject: [PATCH] Fixes issue relating to the fix for U4-2589 where we need to clear the published db flag for previous versions when creating a new published version. --- src/Umbraco.Core/Models/ContentExtensions.cs | 47 ++++++++++++++++ .../Repositories/ContentRepository.cs | 3 +- .../Models/ContentExtensionsTests.cs | 55 +++++++++++++++++++ 3 files changed, 104 insertions(+), 1 deletion(-) diff --git a/src/Umbraco.Core/Models/ContentExtensions.cs b/src/Umbraco.Core/Models/ContentExtensions.cs index c08983c866..f88fe7f4d3 100644 --- a/src/Umbraco.Core/Models/ContentExtensions.cs +++ b/src/Umbraco.Core/Models/ContentExtensions.cs @@ -78,6 +78,53 @@ namespace Umbraco.Core.Models return (propertyValueChanged && publishedState == PublishedState.Published) || contentDataChanged; } + /// + /// Determines if the published db flag should be set to true for the current entity version and all other db + /// versions should have their flag set to false. + /// + /// + /// + /// + /// This is determined by: + /// * If a new version is being created and the entity is published + /// * If the published state has changed and the entity is published OR the entity has been un-published. + /// + internal static bool ShouldClearPublishedFlagForPreviousVersions(this IContent entity) + { + var publishedState = ((Content)entity).PublishedState; + return entity.ShouldClearPublishedFlagForPreviousVersions(publishedState, entity.ShouldCreateNewVersion(publishedState)); + } + + /// + /// Determines if the published db flag should be set to true for the current entity version and all other db + /// versions should have their flag set to false. + /// + /// + /// + /// + /// + /// + /// This is determined by: + /// * If a new version is being created and the entity is published + /// * If the published state has changed and the entity is published OR the entity has been un-published. + /// + internal static bool ShouldClearPublishedFlagForPreviousVersions(this IContent entity, PublishedState publishedState, bool isCreatingNewVersion) + { + if (isCreatingNewVersion && entity.Published) + { + return true; + } + + //If Published state has changed then previous versions should have their publish state reset. + //If state has been changed to unpublished the previous versions publish state should also be reset. + if (((ICanBeDirty)entity).IsPropertyDirty("Published") && (entity.Published || publishedState == PublishedState.Unpublished)) + { + return true; + } + + return false; + } + /// /// Returns a list of the current contents ancestors, not including the content itself. /// diff --git a/src/Umbraco.Core/Persistence/Repositories/ContentRepository.cs b/src/Umbraco.Core/Persistence/Repositories/ContentRepository.cs index 954eac582a..d02c48b639 100644 --- a/src/Umbraco.Core/Persistence/Repositories/ContentRepository.cs +++ b/src/Umbraco.Core/Persistence/Repositories/ContentRepository.cs @@ -328,7 +328,8 @@ namespace Umbraco.Core.Persistence.Repositories //If Published state has changed then previous versions should have their publish state reset. //If state has been changed to unpublished the previous versions publish state should also be reset. - if (((ICanBeDirty)entity).IsPropertyDirty("Published") && (entity.Published || publishedState == PublishedState.Unpublished)) + //if (((ICanBeDirty)entity).IsPropertyDirty("Published") && (entity.Published || publishedState == PublishedState.Unpublished)) + if (entity.ShouldClearPublishedFlagForPreviousVersions(publishedState, shouldCreateNewVersion)) { var publishedDocs = Database.Fetch("WHERE nodeId = @Id AND published = @IsPublished", new { Id = entity.Id, IsPublished = true }); foreach (var doc in publishedDocs) diff --git a/src/Umbraco.Tests/Models/ContentExtensionsTests.cs b/src/Umbraco.Tests/Models/ContentExtensionsTests.cs index 0e0cd41c15..fa9797e155 100644 --- a/src/Umbraco.Tests/Models/ContentExtensionsTests.cs +++ b/src/Umbraco.Tests/Models/ContentExtensionsTests.cs @@ -82,6 +82,61 @@ namespace Umbraco.Tests.Models } + [Test] + public void Should_Clear_Published_Flag_When_Newly_Published_Version() + { + var contentType = MockedContentTypes.CreateTextpageContentType(); + var content = MockedContent.CreateTextpageContent(contentType, "Textpage", -1); + + content.ResetDirtyProperties(false); + + content.ChangePublishedState(PublishedState.Published); + Assert.IsTrue(content.ShouldClearPublishedFlagForPreviousVersions()); + } + + [Test] + public void Should_Not_Clear_Published_Flag_When_Saving_Version() + { + 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.IsFalse(content.ShouldClearPublishedFlagForPreviousVersions()); + } + + [Test] + public void Should_Clear_Published_Flag_When_Unpublishing_From_Published() + { + var contentType = MockedContentTypes.CreateTextpageContentType(); + var content = MockedContent.CreateTextpageContent(contentType, "Textpage", -1); + + content.ResetDirtyProperties(false); + content.ChangePublishedState(PublishedState.Published); + content.ResetDirtyProperties(false); + + content.ChangePublishedState(PublishedState.Unpublished); + Assert.IsTrue(content.ShouldClearPublishedFlagForPreviousVersions()); + } + + [Test] + public void Should_Not_Clear_Published_Flag_When_Unpublishing_From_Saved() + { + var contentType = MockedContentTypes.CreateTextpageContentType(); + var content = MockedContent.CreateTextpageContent(contentType, "Textpage", -1); + + content.ResetDirtyProperties(false); + content.ChangePublishedState(PublishedState.Saved); + content.ResetDirtyProperties(false); + + content.ChangePublishedState(PublishedState.Unpublished); + Assert.IsFalse(content.ShouldClearPublishedFlagForPreviousVersions()); + } + + } } \ No newline at end of file