Fixes up logic for creating new versions (U4-2589 Save and Publish is not creating version of document)

This commit is contained in:
Shannon
2013-08-08 10:42:06 +10:00
parent 5fce741940
commit 0fbc32a87f
3 changed files with 72 additions and 15 deletions

View File

@@ -139,7 +139,10 @@ namespace Umbraco.Core.Models
/// <summary>
/// Boolean indicating whether this Content is Published or not
/// </summary>
/// <remarks>Setting Published to true/false should be private or internal</remarks>
/// <remarks>
/// 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.
/// </remarks>
[DataMember]
public bool Published
{
@@ -372,11 +375,25 @@ namespace Umbraco.Core.Models
/// <returns>True if entity is dirty, otherwise False</returns>
public override bool IsDirty()
{
bool dirtyEntity = base.IsDirty();
return IsEntityDirty() || IsAnyUserPropertyDirty();
}
bool dirtyProperties = Properties.Any(x => x.IsDirty());
/// <summary>
/// Returns true if only the entity properties are direty
/// </summary>
/// <returns></returns>
internal bool IsEntityDirty()
{
return base.IsDirty();
}
return dirtyEntity || dirtyProperties;
/// <summary>
/// Returns true if any of the properties are dirty
/// </summary>
/// <returns></returns>
internal bool IsAnyUserPropertyDirty()
{
return Properties.Any(x => x.IsDirty());
}
/// <summary>

View File

@@ -32,9 +32,9 @@ namespace Umbraco.Core.Models
/// <returns></returns>
/// <remarks>
/// 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)
/// </remarks>
internal static bool ShouldCreateNewVersion(this IContent entity)
{
@@ -50,20 +50,32 @@ namespace Umbraco.Core.Models
/// <returns></returns>
/// <remarks>
/// 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)
/// </remarks>
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;
}
/// <summary>

View File

@@ -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));
}
}
}